OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/webui/options/chromeos/bluetooth_options_handler.h" | 5 #include "chrome/browser/ui/webui/options/chromeos/bluetooth_options_handler.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
218 bool bluetooth_enabled; | 218 bool bluetooth_enabled; |
219 args->GetBoolean(0, &bluetooth_enabled); | 219 args->GetBoolean(0, &bluetooth_enabled); |
220 | 220 |
221 adapter_->SetPowered(bluetooth_enabled, | 221 adapter_->SetPowered(bluetooth_enabled, |
222 base::Bind(&base::DoNothing), | 222 base::Bind(&base::DoNothing), |
223 base::Bind(&BluetoothOptionsHandler::EnableChangeError, | 223 base::Bind(&BluetoothOptionsHandler::EnableChangeError, |
224 weak_ptr_factory_.GetWeakPtr())); | 224 weak_ptr_factory_.GetWeakPtr())); |
225 } | 225 } |
226 | 226 |
227 void BluetoothOptionsHandler::EnableChangeError() { | 227 void BluetoothOptionsHandler::EnableChangeError() { |
228 DVLOG(1) << "Failed to change power state."; | 228 VLOG(1) << "Failed to change power state."; |
229 ReportError("bluetoothChangePowerFailed", std::string()); | 229 ReportError("bluetoothChangePowerFailed", std::string()); |
230 } | 230 } |
231 | 231 |
232 void BluetoothOptionsHandler::FindDevicesCallback( | 232 void BluetoothOptionsHandler::FindDevicesCallback( |
233 const ListValue* args) { | 233 const ListValue* args) { |
234 if (!discovering_) { | 234 if (!discovering_) { |
235 discovering_ = true; | 235 discovering_ = true; |
236 adapter_->StartDiscovering( | 236 adapter_->StartDiscovering( |
237 base::Bind(&base::DoNothing), | 237 base::Bind(&base::DoNothing), |
238 base::Bind(&BluetoothOptionsHandler::FindDevicesError, | 238 base::Bind(&BluetoothOptionsHandler::FindDevicesError, |
239 weak_ptr_factory_.GetWeakPtr())); | 239 weak_ptr_factory_.GetWeakPtr())); |
240 } | 240 } |
241 } | 241 } |
242 | 242 |
243 void BluetoothOptionsHandler::FindDevicesError() { | 243 void BluetoothOptionsHandler::FindDevicesError() { |
244 DVLOG(1) << "Failed to start discovery."; | 244 VLOG(1) << "Failed to start discovery."; |
245 ReportError("bluetoothStartDiscoveryFailed", std::string()); | 245 ReportError("bluetoothStartDiscoveryFailed", std::string()); |
246 } | 246 } |
247 | 247 |
248 void BluetoothOptionsHandler::UpdateDeviceCallback( | 248 void BluetoothOptionsHandler::UpdateDeviceCallback( |
249 const ListValue* args) { | 249 const ListValue* args) { |
250 std::string address; | 250 std::string address; |
251 args->GetString(kUpdateDeviceAddressIndex, &address); | 251 args->GetString(kUpdateDeviceAddressIndex, &address); |
252 | 252 |
253 device::BluetoothDevice* device = adapter_->GetDevice(address); | 253 device::BluetoothDevice* device = adapter_->GetDevice(address); |
254 if (!device) | 254 if (!device) |
255 return; | 255 return; |
256 | 256 |
257 std::string command; | 257 std::string command; |
258 args->GetString(kUpdateDeviceCommandIndex, &command); | 258 args->GetString(kUpdateDeviceCommandIndex, &command); |
259 | 259 |
260 if (command == kConnectCommand) { | 260 if (command == kConnectCommand) { |
261 int size = args->GetSize(); | 261 int size = args->GetSize(); |
262 if (size > kUpdateDeviceAuthTokenIndex) { | 262 if (size > kUpdateDeviceAuthTokenIndex) { |
263 // PIN code or Passkey entry during the pairing process. | 263 // PIN code or Passkey entry during the pairing process. |
264 std::string auth_token; | 264 std::string auth_token; |
265 args->GetString(kUpdateDeviceAuthTokenIndex, &auth_token); | 265 args->GetString(kUpdateDeviceAuthTokenIndex, &auth_token); |
266 | 266 |
267 if (device->ExpectingPinCode()) { | 267 if (device->ExpectingPinCode()) { |
268 DeviceConnecting(device); | 268 DeviceConnecting(device); |
269 // PIN Code is an array of 1 to 16 8-bit bytes, the usual | 269 // PIN Code is an array of 1 to 16 8-bit bytes, the usual |
270 // interpretation, and the one shared by BlueZ, is a UTF-8 string | 270 // interpretation, and the one shared by BlueZ, is a UTF-8 string |
271 // of as many characters that will fit in that space, thus we | 271 // of as many characters that will fit in that space, thus we |
272 // can use the auth token from JavaScript unmodified. | 272 // can use the auth token from JavaScript unmodified. |
273 DVLOG(1) << "PIN Code supplied: " << address << ": " << auth_token; | 273 VLOG(1) << "PIN Code supplied: " << address << ": " << auth_token; |
274 device->SetPinCode(auth_token); | 274 device->SetPinCode(auth_token); |
275 } else if (device->ExpectingPasskey()) { | 275 } else if (device->ExpectingPasskey()) { |
276 DeviceConnecting(device); | 276 DeviceConnecting(device); |
277 // Passkey is a numeric in the range 0-999999, in this case the | 277 // Passkey is a numeric in the range 0-999999, in this case the |
278 // JavaScript code should have ensured the auth token string only | 278 // JavaScript code should have ensured the auth token string only |
279 // contains digits so a simple conversion is sufficient. In the | 279 // contains digits so a simple conversion is sufficient. In the |
280 // failure case, just use 0 since that's the most likely Passkey | 280 // failure case, just use 0 since that's the most likely Passkey |
281 // anyway, and if it's refused the device will request a new one. | 281 // anyway, and if it's refused the device will request a new one. |
282 unsigned passkey = 0; | 282 unsigned passkey = 0; |
283 base::StringToUint(auth_token, &passkey); | 283 base::StringToUint(auth_token, &passkey); |
284 | 284 |
285 DVLOG(1) << "Passkey supplied: " << address << ": " << passkey; | 285 VLOG(1) << "Passkey supplied: " << address << ": " << passkey; |
286 device->SetPasskey(passkey); | 286 device->SetPasskey(passkey); |
287 } else { | 287 } else { |
288 LOG(WARNING) << "Auth token supplied after pairing ended: " << address | 288 LOG(WARNING) << "Auth token supplied after pairing ended: " << address |
289 << ": " << auth_token; | 289 << ": " << auth_token; |
290 } | 290 } |
291 } else { | 291 } else { |
292 // Connection request. | 292 // Connection request. |
293 DVLOG(1) << "Connect: " << address; | 293 VLOG(1) << "Connect: " << address; |
294 device->Connect( | 294 device->Connect( |
295 this, | 295 this, |
296 base::Bind(&base::DoNothing), | 296 base::Bind(&base::DoNothing), |
297 base::Bind(&BluetoothOptionsHandler::ConnectError, | 297 base::Bind(&BluetoothOptionsHandler::ConnectError, |
298 weak_ptr_factory_.GetWeakPtr(), | 298 weak_ptr_factory_.GetWeakPtr(), |
299 device->address())); | 299 device->address())); |
300 } | 300 } |
301 } else if (command == kCancelCommand) { | 301 } else if (command == kCancelCommand) { |
302 // Cancel pairing. | 302 // Cancel pairing. |
303 DVLOG(1) << "Cancel pairing: " << address; | 303 VLOG(1) << "Cancel pairing: " << address; |
304 device->CancelPairing(); | 304 device->CancelPairing(); |
305 } else if (command == kAcceptCommand) { | 305 } else if (command == kAcceptCommand) { |
306 // Confirm displayed Passkey. | 306 // Confirm displayed Passkey. |
307 DVLOG(1) << "Confirm pairing: " << address; | 307 VLOG(1) << "Confirm pairing: " << address; |
308 device->ConfirmPairing(); | 308 device->ConfirmPairing(); |
309 } else if (command == kRejectCommand) { | 309 } else if (command == kRejectCommand) { |
310 // Reject displayed Passkey. | 310 // Reject displayed Passkey. |
311 DVLOG(1) << "Reject pairing: " << address; | 311 VLOG(1) << "Reject pairing: " << address; |
312 device->RejectPairing(); | 312 device->RejectPairing(); |
313 } else if (command == kDisconnectCommand) { | 313 } else if (command == kDisconnectCommand) { |
314 // Disconnect from device. | 314 // Disconnect from device. |
315 DVLOG(1) << "Disconnect device: " << address; | 315 VLOG(1) << "Disconnect device: " << address; |
316 device->Disconnect( | 316 device->Disconnect( |
317 base::Bind(&base::DoNothing), | 317 base::Bind(&base::DoNothing), |
318 base::Bind(&BluetoothOptionsHandler::DisconnectError, | 318 base::Bind(&BluetoothOptionsHandler::DisconnectError, |
319 weak_ptr_factory_.GetWeakPtr(), | 319 weak_ptr_factory_.GetWeakPtr(), |
320 device->address())); | 320 device->address())); |
321 } else if (command == kForgetCommand) { | 321 } else if (command == kForgetCommand) { |
322 // Disconnect from device and delete pairing information. | 322 // Disconnect from device and delete pairing information. |
323 DVLOG(1) << "Forget device: " << address; | 323 VLOG(1) << "Forget device: " << address; |
324 device->Forget(base::Bind(&BluetoothOptionsHandler::ForgetError, | 324 device->Forget(base::Bind(&BluetoothOptionsHandler::ForgetError, |
325 weak_ptr_factory_.GetWeakPtr(), | 325 weak_ptr_factory_.GetWeakPtr(), |
326 device->address())); | 326 device->address())); |
327 } else { | 327 } else { |
328 LOG(WARNING) << "Unknown updateBluetoothDevice command: " << command; | 328 LOG(WARNING) << "Unknown updateBluetoothDevice command: " << command; |
329 } | 329 } |
330 } | 330 } |
331 | 331 |
332 void BluetoothOptionsHandler::ConnectError( | 332 void BluetoothOptionsHandler::ConnectError( |
333 const std::string& address, | 333 const std::string& address, |
334 device::BluetoothDevice::ConnectErrorCode error_code) { | 334 device::BluetoothDevice::ConnectErrorCode error_code) { |
335 const char* error_name = NULL; | 335 const char* error_name = NULL; |
336 | 336 |
337 DVLOG(1) << "Failed to connect to device: " << address; | 337 VLOG(1) << "Failed to connect to device: " << address; |
338 switch (error_code) { | 338 switch (error_code) { |
339 case device::BluetoothDevice::ERROR_UNKNOWN: | 339 case device::BluetoothDevice::ERROR_UNKNOWN: |
340 error_name = "bluetoothConnectUnknownError"; | 340 error_name = "bluetoothConnectUnknownError"; |
341 break; | 341 break; |
342 case device::BluetoothDevice::ERROR_INPROGRESS: | 342 case device::BluetoothDevice::ERROR_INPROGRESS: |
343 error_name = "bluetoothConnectInProgress"; | 343 error_name = "bluetoothConnectInProgress"; |
344 break; | 344 break; |
345 case device::BluetoothDevice::ERROR_FAILED: | 345 case device::BluetoothDevice::ERROR_FAILED: |
346 error_name = "bluetoothConnectFailed"; | 346 error_name = "bluetoothConnectFailed"; |
347 break; | 347 break; |
(...skipping 12 matching lines...) Expand all Loading... |
360 case device::BluetoothDevice::ERROR_UNSUPPORTED_DEVICE: | 360 case device::BluetoothDevice::ERROR_UNSUPPORTED_DEVICE: |
361 error_name = "bluetoothConnectUnsupportedDevice"; | 361 error_name = "bluetoothConnectUnsupportedDevice"; |
362 break; | 362 break; |
363 } | 363 } |
364 // Report an error only if there's an error to report. | 364 // Report an error only if there's an error to report. |
365 if (error_name) | 365 if (error_name) |
366 ReportError(error_name, address); | 366 ReportError(error_name, address); |
367 } | 367 } |
368 | 368 |
369 void BluetoothOptionsHandler::DisconnectError(const std::string& address) { | 369 void BluetoothOptionsHandler::DisconnectError(const std::string& address) { |
370 DVLOG(1) << "Failed to disconnect from device: " << address; | 370 VLOG(1) << "Failed to disconnect from device: " << address; |
371 ReportError("bluetoothDisconnectFailed", address); | 371 ReportError("bluetoothDisconnectFailed", address); |
372 } | 372 } |
373 | 373 |
374 void BluetoothOptionsHandler::ForgetError(const std::string& address) { | 374 void BluetoothOptionsHandler::ForgetError(const std::string& address) { |
375 DVLOG(1) << "Failed to disconnect and unpair device: " << address; | 375 VLOG(1) << "Failed to disconnect and unpair device: " << address; |
376 ReportError("bluetoothForgetFailed", address); | 376 ReportError("bluetoothForgetFailed", address); |
377 } | 377 } |
378 | 378 |
379 void BluetoothOptionsHandler::StopDiscoveryCallback( | 379 void BluetoothOptionsHandler::StopDiscoveryCallback( |
380 const ListValue* args) { | 380 const ListValue* args) { |
381 if (discovering_) { | 381 if (discovering_) { |
382 adapter_->StopDiscovering( | 382 adapter_->StopDiscovering( |
383 base::Bind(&base::DoNothing), | 383 base::Bind(&base::DoNothing), |
384 base::Bind(&BluetoothOptionsHandler::StopDiscoveryError, | 384 base::Bind(&BluetoothOptionsHandler::StopDiscoveryError, |
385 weak_ptr_factory_.GetWeakPtr())); | 385 weak_ptr_factory_.GetWeakPtr())); |
386 discovering_ = false; | 386 discovering_ = false; |
387 } | 387 } |
388 } | 388 } |
389 | 389 |
390 void BluetoothOptionsHandler::StopDiscoveryError() { | 390 void BluetoothOptionsHandler::StopDiscoveryError() { |
391 DVLOG(1) << "Failed to stop discovery."; | 391 VLOG(1) << "Failed to stop discovery."; |
392 ReportError("bluetoothStopDiscoveryFailed", std::string()); | 392 ReportError("bluetoothStopDiscoveryFailed", std::string()); |
393 } | 393 } |
394 | 394 |
395 void BluetoothOptionsHandler::GetPairedDevicesCallback( | 395 void BluetoothOptionsHandler::GetPairedDevicesCallback( |
396 const ListValue* args) { | 396 const ListValue* args) { |
397 device::BluetoothAdapter::DeviceList devices = adapter_->GetDevices(); | 397 device::BluetoothAdapter::DeviceList devices = adapter_->GetDevices(); |
398 | 398 |
399 for (device::BluetoothAdapter::DeviceList::iterator iter = devices.begin(); | 399 for (device::BluetoothAdapter::DeviceList::iterator iter = devices.begin(); |
400 iter != devices.end(); ++iter) | 400 iter != devices.end(); ++iter) |
401 SendDeviceNotification(*iter, NULL); | 401 SendDeviceNotification(*iter, NULL); |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
498 void BluetoothOptionsHandler::DeviceConnecting( | 498 void BluetoothOptionsHandler::DeviceConnecting( |
499 device::BluetoothDevice* device) { | 499 device::BluetoothDevice* device) { |
500 DCHECK(device); | 500 DCHECK(device); |
501 DictionaryValue params; | 501 DictionaryValue params; |
502 params.SetString("pairing", kStartConnecting); | 502 params.SetString("pairing", kStartConnecting); |
503 SendDeviceNotification(device, ¶ms); | 503 SendDeviceNotification(device, ¶ms); |
504 } | 504 } |
505 | 505 |
506 } // namespace options | 506 } // namespace options |
507 } // namespace chromeos | 507 } // namespace chromeos |
OLD | NEW |