OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "device/bluetooth/bluetooth_device_chromeos.h" | 5 #include "device/bluetooth/bluetooth_device_chromeos.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
11 #include "chromeos/dbus/bluetooth_adapter_client.h" | 11 #include "chromeos/dbus/bluetooth_adapter_client.h" |
12 #include "chromeos/dbus/bluetooth_agent_manager_client.h" | |
13 #include "chromeos/dbus/bluetooth_agent_service_provider.h" | |
14 #include "chromeos/dbus/bluetooth_device_client.h" | 12 #include "chromeos/dbus/bluetooth_device_client.h" |
15 #include "chromeos/dbus/bluetooth_input_client.h" | 13 #include "chromeos/dbus/bluetooth_input_client.h" |
16 #include "chromeos/dbus/dbus_thread_manager.h" | 14 #include "chromeos/dbus/dbus_thread_manager.h" |
17 #include "dbus/bus.h" | 15 #include "dbus/bus.h" |
18 #include "device/bluetooth/bluetooth_adapter_chromeos.h" | 16 #include "device/bluetooth/bluetooth_adapter_chromeos.h" |
19 #include "device/bluetooth/bluetooth_profile_chromeos.h" | 17 #include "device/bluetooth/bluetooth_profile_chromeos.h" |
20 #include "device/bluetooth/bluetooth_socket.h" | 18 #include "device/bluetooth/bluetooth_socket.h" |
21 #include "third_party/cros_system_api/dbus/service_constants.h" | 19 #include "third_party/cros_system_api/dbus/service_constants.h" |
22 | 20 |
23 using device::BluetoothDevice; | 21 using device::BluetoothDevice; |
24 | 22 |
25 namespace { | 23 namespace { |
26 | 24 |
27 // The agent path is relatively meaningless since BlueZ only supports one | |
28 // at time and will fail in an attempt to register another with "Already Exists" | |
29 // (which we fail in OnRegisterAgentError with ERROR_INPROGRESS). | |
30 const char kAgentPath[] = "/org/chromium/bluetooth_agent"; | |
31 | |
32 // Histogram enumerations for pairing methods. | |
33 enum UMAPairingMethod { | |
34 UMA_PAIRING_METHOD_NONE, | |
35 UMA_PAIRING_METHOD_REQUEST_PINCODE, | |
36 UMA_PAIRING_METHOD_REQUEST_PASSKEY, | |
37 UMA_PAIRING_METHOD_DISPLAY_PINCODE, | |
38 UMA_PAIRING_METHOD_DISPLAY_PASSKEY, | |
39 UMA_PAIRING_METHOD_CONFIRM_PASSKEY, | |
40 // NOTE: Add new pairing methods immediately above this line. Make sure to | |
41 // update the enum list in tools/histogram/histograms.xml accordinly. | |
42 UMA_PAIRING_METHOD_COUNT | |
43 }; | |
44 | |
45 // Histogram enumerations for pairing results. | 25 // Histogram enumerations for pairing results. |
46 enum UMAPairingResult { | 26 enum UMAPairingResult { |
47 UMA_PAIRING_RESULT_SUCCESS, | 27 UMA_PAIRING_RESULT_SUCCESS, |
48 UMA_PAIRING_RESULT_INPROGRESS, | 28 UMA_PAIRING_RESULT_INPROGRESS, |
49 UMA_PAIRING_RESULT_FAILED, | 29 UMA_PAIRING_RESULT_FAILED, |
50 UMA_PAIRING_RESULT_AUTH_FAILED, | 30 UMA_PAIRING_RESULT_AUTH_FAILED, |
51 UMA_PAIRING_RESULT_AUTH_CANCELED, | 31 UMA_PAIRING_RESULT_AUTH_CANCELED, |
52 UMA_PAIRING_RESULT_AUTH_REJECTED, | 32 UMA_PAIRING_RESULT_AUTH_REJECTED, |
53 UMA_PAIRING_RESULT_AUTH_TIMEOUT, | 33 UMA_PAIRING_RESULT_AUTH_TIMEOUT, |
54 UMA_PAIRING_RESULT_UNSUPPORTED_DEVICE, | 34 UMA_PAIRING_RESULT_UNSUPPORTED_DEVICE, |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 } // namespace | 106 } // namespace |
127 | 107 |
128 namespace chromeos { | 108 namespace chromeos { |
129 | 109 |
130 BluetoothDeviceChromeOS::BluetoothDeviceChromeOS( | 110 BluetoothDeviceChromeOS::BluetoothDeviceChromeOS( |
131 BluetoothAdapterChromeOS* adapter, | 111 BluetoothAdapterChromeOS* adapter, |
132 const dbus::ObjectPath& object_path) | 112 const dbus::ObjectPath& object_path) |
133 : adapter_(adapter), | 113 : adapter_(adapter), |
134 object_path_(object_path), | 114 object_path_(object_path), |
135 num_connecting_calls_(0), | 115 num_connecting_calls_(0), |
136 pairing_delegate_(NULL), | |
137 pairing_delegate_used_(false), | |
138 weak_ptr_factory_(this) { | 116 weak_ptr_factory_(this) { |
139 } | 117 } |
140 | 118 |
141 BluetoothDeviceChromeOS::~BluetoothDeviceChromeOS() { | 119 BluetoothDeviceChromeOS::~BluetoothDeviceChromeOS() { |
142 } | 120 } |
143 | 121 |
144 uint32 BluetoothDeviceChromeOS::GetBluetoothClass() const { | 122 uint32 BluetoothDeviceChromeOS::GetBluetoothClass() const { |
145 BluetoothDeviceClient::Properties* properties = | 123 BluetoothDeviceClient::Properties* properties = |
146 DBusThreadManager::Get()->GetBluetoothDeviceClient()-> | 124 DBusThreadManager::Get()->GetBluetoothDeviceClient()-> |
147 GetProperties(object_path_); | 125 GetProperties(object_path_); |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 } | 219 } |
242 | 220 |
243 void BluetoothDeviceChromeOS::ProvidesServiceWithName( | 221 void BluetoothDeviceChromeOS::ProvidesServiceWithName( |
244 const std::string& name, | 222 const std::string& name, |
245 const ProvidesServiceCallback& callback) { | 223 const ProvidesServiceCallback& callback) { |
246 // TODO(keybuk): not implemented; remove | 224 // TODO(keybuk): not implemented; remove |
247 callback.Run(false); | 225 callback.Run(false); |
248 } | 226 } |
249 | 227 |
250 bool BluetoothDeviceChromeOS::ExpectingPinCode() const { | 228 bool BluetoothDeviceChromeOS::ExpectingPinCode() const { |
251 return !pincode_callback_.is_null(); | 229 return pairing_context_.get() && pairing_context_->ExpectingPinCode(); |
252 } | 230 } |
253 | 231 |
254 bool BluetoothDeviceChromeOS::ExpectingPasskey() const { | 232 bool BluetoothDeviceChromeOS::ExpectingPasskey() const { |
255 return !passkey_callback_.is_null(); | 233 return pairing_context_.get() && pairing_context_->ExpectingPasskey(); |
256 } | 234 } |
257 | 235 |
258 bool BluetoothDeviceChromeOS::ExpectingConfirmation() const { | 236 bool BluetoothDeviceChromeOS::ExpectingConfirmation() const { |
259 return !confirmation_callback_.is_null(); | 237 return pairing_context_.get() && pairing_context_->ExpectingConfirmation(); |
260 } | 238 } |
261 | 239 |
262 void BluetoothDeviceChromeOS::Connect( | 240 void BluetoothDeviceChromeOS::Connect( |
263 BluetoothDevice::PairingDelegate* pairing_delegate, | 241 BluetoothDevice::PairingDelegate* pairing_delegate, |
264 const base::Closure& callback, | 242 const base::Closure& callback, |
265 const ConnectErrorCallback& error_callback) { | 243 const ConnectErrorCallback& error_callback) { |
266 if (num_connecting_calls_++ == 0) | 244 if (num_connecting_calls_++ == 0) |
267 adapter_->NotifyDeviceChanged(this); | 245 adapter_->NotifyDeviceChanged(this); |
268 | 246 |
269 VLOG(1) << object_path_.value() << ": Connecting, " << num_connecting_calls_ | 247 VLOG(1) << object_path_.value() << ": Connecting, " << num_connecting_calls_ |
270 << " in progress"; | 248 << " in progress"; |
271 | 249 |
272 if (IsPaired() || !pairing_delegate || !IsPairable()) { | 250 if (IsPaired() || !pairing_delegate || !IsPairable()) { |
273 // No need to pair, or unable to, skip straight to connection. | 251 // No need to pair, or unable to, skip straight to connection. |
274 ConnectInternal(false, callback, error_callback); | 252 ConnectInternal(false, callback, error_callback); |
275 } else { | 253 } else { |
276 // Initiate high-security connection with pairing. | 254 // Initiate high-security connection with pairing. |
277 DCHECK(!pairing_delegate_); | 255 DCHECK(!pairing_context_); |
278 DCHECK(agent_.get() == NULL); | 256 pairing_context_.reset( |
| 257 new BluetoothAdapterChromeOS::PairingContext(pairing_delegate)); |
279 | 258 |
280 pairing_delegate_ = pairing_delegate; | 259 DBusThreadManager::Get()->GetBluetoothDeviceClient()-> |
281 pairing_delegate_used_ = false; | 260 Pair(object_path_, |
282 | 261 base::Bind(&BluetoothDeviceChromeOS::OnPair, |
283 // The agent path is relatively meaningless since BlueZ only supports | 262 weak_ptr_factory_.GetWeakPtr(), |
284 // one per application at a time. | 263 callback, error_callback), |
285 dbus::Bus* system_bus = DBusThreadManager::Get()->GetSystemBus(); | 264 base::Bind(&BluetoothDeviceChromeOS::OnPairError, |
286 agent_.reset(BluetoothAgentServiceProvider::Create( | 265 weak_ptr_factory_.GetWeakPtr(), |
287 system_bus, dbus::ObjectPath(kAgentPath), this)); | 266 error_callback)); |
288 DCHECK(agent_.get()); | |
289 | |
290 VLOG(1) << object_path_.value() << ": Registering agent for pairing"; | |
291 DBusThreadManager::Get()->GetBluetoothAgentManagerClient()-> | |
292 RegisterAgent( | |
293 dbus::ObjectPath(kAgentPath), | |
294 bluetooth_agent_manager::kKeyboardDisplayCapability, | |
295 base::Bind(&BluetoothDeviceChromeOS::OnRegisterAgent, | |
296 weak_ptr_factory_.GetWeakPtr(), | |
297 callback, | |
298 error_callback), | |
299 base::Bind(&BluetoothDeviceChromeOS::OnRegisterAgentError, | |
300 weak_ptr_factory_.GetWeakPtr(), | |
301 error_callback)); | |
302 } | 267 } |
303 } | 268 } |
304 | 269 |
305 void BluetoothDeviceChromeOS::SetPinCode(const std::string& pincode) { | 270 void BluetoothDeviceChromeOS::SetPinCode(const std::string& pincode) { |
306 if (!agent_.get() || pincode_callback_.is_null()) | 271 if (!pairing_context_.get()) |
307 return; | 272 return; |
308 | 273 |
309 pincode_callback_.Run(SUCCESS, pincode); | 274 pairing_context_->SetPinCode(pincode); |
310 pincode_callback_.Reset(); | |
311 } | 275 } |
312 | 276 |
313 void BluetoothDeviceChromeOS::SetPasskey(uint32 passkey) { | 277 void BluetoothDeviceChromeOS::SetPasskey(uint32 passkey) { |
314 if (!agent_.get() || passkey_callback_.is_null()) | 278 if (!pairing_context_.get()) |
315 return; | 279 return; |
316 | 280 |
317 passkey_callback_.Run(SUCCESS, passkey); | 281 pairing_context_->SetPasskey(passkey); |
318 passkey_callback_.Reset(); | |
319 } | 282 } |
320 | 283 |
321 void BluetoothDeviceChromeOS::ConfirmPairing() { | 284 void BluetoothDeviceChromeOS::ConfirmPairing() { |
322 if (!agent_.get() || confirmation_callback_.is_null()) | 285 if (!pairing_context_.get()) |
323 return; | 286 return; |
324 | 287 |
325 confirmation_callback_.Run(SUCCESS); | 288 pairing_context_->ConfirmPairing(); |
326 confirmation_callback_.Reset(); | |
327 } | 289 } |
328 | 290 |
329 void BluetoothDeviceChromeOS::RejectPairing() { | 291 void BluetoothDeviceChromeOS::RejectPairing() { |
330 RunPairingCallbacks(REJECTED); | 292 if (!pairing_context_.get()) |
| 293 return; |
| 294 |
| 295 pairing_context_->RejectPairing(); |
331 } | 296 } |
332 | 297 |
333 void BluetoothDeviceChromeOS::CancelPairing() { | 298 void BluetoothDeviceChromeOS::CancelPairing() { |
334 // If there wasn't a callback in progress that we can reply to then we | 299 // If there wasn't a callback in progress that we can reply to then we |
335 // have to send a CancelPairing() to the device instead. | 300 // have to send a CancelPairing() to the device instead. |
336 if (!RunPairingCallbacks(CANCELLED)) { | 301 if (!pairing_context_.get() || !pairing_context_->CancelPairing()) { |
| 302 VLOG(1) << object_path_.value() << ": No pairing context or callback. " |
| 303 << "Sending explicit cancel"; |
337 DBusThreadManager::Get()->GetBluetoothDeviceClient()-> | 304 DBusThreadManager::Get()->GetBluetoothDeviceClient()-> |
338 CancelPairing( | 305 CancelPairing( |
339 object_path_, | 306 object_path_, |
340 base::Bind(&base::DoNothing), | 307 base::Bind(&base::DoNothing), |
341 base::Bind(&BluetoothDeviceChromeOS::OnCancelPairingError, | 308 base::Bind(&BluetoothDeviceChromeOS::OnCancelPairingError, |
342 weak_ptr_factory_.GetWeakPtr())); | 309 weak_ptr_factory_.GetWeakPtr())); |
343 | 310 |
344 // Since there's no calback to this method, it's possible that the pairing | 311 // Since there's no calback to this method, it's possible that the pairing |
345 // delegate is going to be freed before things complete. | 312 // delegate is going to be freed before things complete, so clear out the |
346 UnregisterAgent(); | 313 // context holding it. |
| 314 pairing_context_.reset(); |
347 } | 315 } |
348 } | 316 } |
349 | 317 |
350 void BluetoothDeviceChromeOS::Disconnect(const base::Closure& callback, | 318 void BluetoothDeviceChromeOS::Disconnect(const base::Closure& callback, |
351 const ErrorCallback& error_callback) { | 319 const ErrorCallback& error_callback) { |
352 VLOG(1) << object_path_.value() << ": Disconnecting"; | 320 VLOG(1) << object_path_.value() << ": Disconnecting"; |
353 DBusThreadManager::Get()->GetBluetoothDeviceClient()-> | 321 DBusThreadManager::Get()->GetBluetoothDeviceClient()-> |
354 Disconnect( | 322 Disconnect( |
355 object_path_, | 323 object_path_, |
356 base::Bind(&BluetoothDeviceChromeOS::OnDisconnect, | 324 base::Bind(&BluetoothDeviceChromeOS::OnDisconnect, |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
412 error_callback.Run(); | 380 error_callback.Run(); |
413 } | 381 } |
414 | 382 |
415 void BluetoothDeviceChromeOS::ClearOutOfBandPairingData( | 383 void BluetoothDeviceChromeOS::ClearOutOfBandPairingData( |
416 const base::Closure& callback, | 384 const base::Closure& callback, |
417 const ErrorCallback& error_callback) { | 385 const ErrorCallback& error_callback) { |
418 // TODO(keybuk): implement | 386 // TODO(keybuk): implement |
419 error_callback.Run(); | 387 error_callback.Run(); |
420 } | 388 } |
421 | 389 |
422 | |
423 void BluetoothDeviceChromeOS::Release() { | |
424 DCHECK(agent_.get()); | |
425 DCHECK(pairing_delegate_); | |
426 VLOG(1) << object_path_.value() << ": Release"; | |
427 | |
428 pincode_callback_.Reset(); | |
429 passkey_callback_.Reset(); | |
430 confirmation_callback_.Reset(); | |
431 | |
432 UnregisterAgent(); | |
433 } | |
434 | |
435 void BluetoothDeviceChromeOS::RequestPinCode( | |
436 const dbus::ObjectPath& device_path, | |
437 const PinCodeCallback& callback) { | |
438 DCHECK(agent_.get()); | |
439 DCHECK(device_path == object_path_); | |
440 VLOG(1) << object_path_.value() << ": RequestPinCode"; | |
441 | |
442 UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingMethod", | |
443 UMA_PAIRING_METHOD_REQUEST_PINCODE, | |
444 UMA_PAIRING_METHOD_COUNT); | |
445 | |
446 DCHECK(pairing_delegate_); | |
447 DCHECK(pincode_callback_.is_null()); | |
448 pincode_callback_ = callback; | |
449 pairing_delegate_->RequestPinCode(this); | |
450 pairing_delegate_used_ = true; | |
451 } | |
452 | |
453 void BluetoothDeviceChromeOS::DisplayPinCode( | |
454 const dbus::ObjectPath& device_path, | |
455 const std::string& pincode) { | |
456 DCHECK(agent_.get()); | |
457 DCHECK(device_path == object_path_); | |
458 VLOG(1) << object_path_.value() << ": DisplayPinCode: " << pincode; | |
459 | |
460 UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingMethod", | |
461 UMA_PAIRING_METHOD_DISPLAY_PINCODE, | |
462 UMA_PAIRING_METHOD_COUNT); | |
463 | |
464 DCHECK(pairing_delegate_); | |
465 pairing_delegate_->DisplayPinCode(this, pincode); | |
466 pairing_delegate_used_ = true; | |
467 } | |
468 | |
469 void BluetoothDeviceChromeOS::RequestPasskey( | |
470 const dbus::ObjectPath& device_path, | |
471 const PasskeyCallback& callback) { | |
472 DCHECK(agent_.get()); | |
473 DCHECK(device_path == object_path_); | |
474 VLOG(1) << object_path_.value() << ": RequestPasskey"; | |
475 | |
476 UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingMethod", | |
477 UMA_PAIRING_METHOD_REQUEST_PASSKEY, | |
478 UMA_PAIRING_METHOD_COUNT); | |
479 | |
480 DCHECK(pairing_delegate_); | |
481 DCHECK(passkey_callback_.is_null()); | |
482 passkey_callback_ = callback; | |
483 pairing_delegate_->RequestPasskey(this); | |
484 pairing_delegate_used_ = true; | |
485 } | |
486 | |
487 void BluetoothDeviceChromeOS::DisplayPasskey( | |
488 const dbus::ObjectPath& device_path, | |
489 uint32 passkey, | |
490 uint16 entered) { | |
491 DCHECK(agent_.get()); | |
492 DCHECK(device_path == object_path_); | |
493 VLOG(1) << object_path_.value() << ": DisplayPasskey: " << passkey | |
494 << " (" << entered << " entered)"; | |
495 | |
496 if (entered == 0) | |
497 UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingMethod", | |
498 UMA_PAIRING_METHOD_DISPLAY_PASSKEY, | |
499 UMA_PAIRING_METHOD_COUNT); | |
500 | |
501 DCHECK(pairing_delegate_); | |
502 if (entered == 0) | |
503 pairing_delegate_->DisplayPasskey(this, passkey); | |
504 pairing_delegate_->KeysEntered(this, entered); | |
505 pairing_delegate_used_ = true; | |
506 } | |
507 | |
508 void BluetoothDeviceChromeOS::RequestConfirmation( | |
509 const dbus::ObjectPath& device_path, | |
510 uint32 passkey, | |
511 const ConfirmationCallback& callback) { | |
512 DCHECK(agent_.get()); | |
513 DCHECK(device_path == object_path_); | |
514 VLOG(1) << object_path_.value() << ": RequestConfirmation: " << passkey; | |
515 | |
516 UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingMethod", | |
517 UMA_PAIRING_METHOD_CONFIRM_PASSKEY, | |
518 UMA_PAIRING_METHOD_COUNT); | |
519 | |
520 DCHECK(pairing_delegate_); | |
521 DCHECK(confirmation_callback_.is_null()); | |
522 confirmation_callback_ = callback; | |
523 pairing_delegate_->ConfirmPasskey(this, passkey); | |
524 pairing_delegate_used_ = true; | |
525 } | |
526 | |
527 void BluetoothDeviceChromeOS::RequestAuthorization( | |
528 const dbus::ObjectPath& device_path, | |
529 const ConfirmationCallback& callback) { | |
530 // TODO(keybuk): implement | |
531 callback.Run(CANCELLED); | |
532 } | |
533 | |
534 void BluetoothDeviceChromeOS::AuthorizeService( | |
535 const dbus::ObjectPath& device_path, | |
536 const std::string& uuid, | |
537 const ConfirmationCallback& callback) { | |
538 // TODO(keybuk): implement | |
539 callback.Run(CANCELLED); | |
540 } | |
541 | |
542 void BluetoothDeviceChromeOS::Cancel() { | |
543 DCHECK(agent_.get()); | |
544 VLOG(1) << object_path_.value() << ": Cancel"; | |
545 | |
546 DCHECK(pairing_delegate_); | |
547 pairing_delegate_->DismissDisplayOrConfirm(); | |
548 } | |
549 | |
550 void BluetoothDeviceChromeOS::ConnectInternal( | 390 void BluetoothDeviceChromeOS::ConnectInternal( |
551 bool after_pairing, | 391 bool after_pairing, |
552 const base::Closure& callback, | 392 const base::Closure& callback, |
553 const ConnectErrorCallback& error_callback) { | 393 const ConnectErrorCallback& error_callback) { |
554 VLOG(1) << object_path_.value() << ": Connecting"; | 394 VLOG(1) << object_path_.value() << ": Connecting"; |
555 DBusThreadManager::Get()->GetBluetoothDeviceClient()-> | 395 DBusThreadManager::Get()->GetBluetoothDeviceClient()-> |
556 Connect( | 396 Connect( |
557 object_path_, | 397 object_path_, |
558 base::Bind(&BluetoothDeviceChromeOS::OnConnect, | 398 base::Bind(&BluetoothDeviceChromeOS::OnConnect, |
559 weak_ptr_factory_.GetWeakPtr(), | 399 weak_ptr_factory_.GetWeakPtr(), |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
606 error_code = ERROR_INPROGRESS; | 446 error_code = ERROR_INPROGRESS; |
607 } else if (error_name == bluetooth_device::kErrorNotSupported) { | 447 } else if (error_name == bluetooth_device::kErrorNotSupported) { |
608 error_code = ERROR_UNSUPPORTED_DEVICE; | 448 error_code = ERROR_UNSUPPORTED_DEVICE; |
609 } | 449 } |
610 | 450 |
611 if (after_pairing) | 451 if (after_pairing) |
612 RecordPairingResult(error_code); | 452 RecordPairingResult(error_code); |
613 error_callback.Run(error_code); | 453 error_callback.Run(error_code); |
614 } | 454 } |
615 | 455 |
616 void BluetoothDeviceChromeOS::OnRegisterAgent( | |
617 const base::Closure& callback, | |
618 const ConnectErrorCallback& error_callback) { | |
619 VLOG(1) << object_path_.value() << ": Agent registered, now pairing"; | |
620 | |
621 DBusThreadManager::Get()->GetBluetoothDeviceClient()-> | |
622 Pair(object_path_, | |
623 base::Bind(&BluetoothDeviceChromeOS::OnPair, | |
624 weak_ptr_factory_.GetWeakPtr(), | |
625 callback, error_callback), | |
626 base::Bind(&BluetoothDeviceChromeOS::OnPairError, | |
627 weak_ptr_factory_.GetWeakPtr(), | |
628 error_callback)); | |
629 } | |
630 | |
631 void BluetoothDeviceChromeOS::OnRegisterAgentError( | |
632 const ConnectErrorCallback& error_callback, | |
633 const std::string& error_name, | |
634 const std::string& error_message) { | |
635 if (--num_connecting_calls_ == 0) | |
636 adapter_->NotifyDeviceChanged(this); | |
637 | |
638 DCHECK(num_connecting_calls_ >= 0); | |
639 LOG(WARNING) << object_path_.value() << ": Failed to register agent: " | |
640 << error_name << ": " << error_message; | |
641 VLOG(1) << object_path_.value() << ": " << num_connecting_calls_ | |
642 << " still in progress"; | |
643 | |
644 UnregisterAgent(); | |
645 | |
646 // Determine the error code from error_name. | |
647 ConnectErrorCode error_code = ERROR_UNKNOWN; | |
648 if (error_name == bluetooth_agent_manager::kErrorAlreadyExists) | |
649 error_code = ERROR_INPROGRESS; | |
650 | |
651 RecordPairingResult(error_code); | |
652 error_callback.Run(error_code); | |
653 } | |
654 | |
655 void BluetoothDeviceChromeOS::OnPair( | 456 void BluetoothDeviceChromeOS::OnPair( |
656 const base::Closure& callback, | 457 const base::Closure& callback, |
657 const ConnectErrorCallback& error_callback) { | 458 const ConnectErrorCallback& error_callback) { |
658 VLOG(1) << object_path_.value() << ": Paired"; | 459 VLOG(1) << object_path_.value() << ": Paired"; |
659 | 460 |
660 if (!pairing_delegate_used_) | 461 pairing_context_.reset(); |
661 UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingMethod", | 462 |
662 UMA_PAIRING_METHOD_NONE, | |
663 UMA_PAIRING_METHOD_COUNT); | |
664 UnregisterAgent(); | |
665 SetTrusted(); | 463 SetTrusted(); |
666 ConnectInternal(true, callback, error_callback); | 464 ConnectInternal(true, callback, error_callback); |
667 } | 465 } |
668 | 466 |
669 void BluetoothDeviceChromeOS::OnPairError( | 467 void BluetoothDeviceChromeOS::OnPairError( |
670 const ConnectErrorCallback& error_callback, | 468 const ConnectErrorCallback& error_callback, |
671 const std::string& error_name, | 469 const std::string& error_name, |
672 const std::string& error_message) { | 470 const std::string& error_message) { |
673 if (--num_connecting_calls_ == 0) | 471 if (--num_connecting_calls_ == 0) |
674 adapter_->NotifyDeviceChanged(this); | 472 adapter_->NotifyDeviceChanged(this); |
675 | 473 |
676 DCHECK(num_connecting_calls_ >= 0); | 474 DCHECK(num_connecting_calls_ >= 0); |
677 LOG(WARNING) << object_path_.value() << ": Failed to pair device: " | 475 LOG(WARNING) << object_path_.value() << ": Failed to pair device: " |
678 << error_name << ": " << error_message; | 476 << error_name << ": " << error_message; |
679 VLOG(1) << object_path_.value() << ": " << num_connecting_calls_ | 477 VLOG(1) << object_path_.value() << ": " << num_connecting_calls_ |
680 << " still in progress"; | 478 << " still in progress"; |
681 | 479 |
682 UnregisterAgent(); | 480 pairing_context_.reset(); |
683 | 481 |
684 // Determine the error code from error_name. | 482 // Determine the error code from error_name. |
685 ConnectErrorCode error_code = ERROR_UNKNOWN; | 483 ConnectErrorCode error_code = ERROR_UNKNOWN; |
686 if (error_name == bluetooth_device::kErrorConnectionAttemptFailed) { | 484 if (error_name == bluetooth_device::kErrorConnectionAttemptFailed) { |
687 error_code = ERROR_FAILED; | 485 error_code = ERROR_FAILED; |
688 } else if (error_name == bluetooth_device::kErrorFailed) { | 486 } else if (error_name == bluetooth_device::kErrorFailed) { |
689 error_code = ERROR_FAILED; | 487 error_code = ERROR_FAILED; |
690 } else if (error_name == bluetooth_device::kErrorAuthenticationFailed) { | 488 } else if (error_name == bluetooth_device::kErrorAuthenticationFailed) { |
691 error_code = ERROR_AUTH_FAILED; | 489 error_code = ERROR_AUTH_FAILED; |
692 } else if (error_name == bluetooth_device::kErrorAuthenticationCanceled) { | 490 } else if (error_name == bluetooth_device::kErrorAuthenticationCanceled) { |
(...skipping 25 matching lines...) Expand all Loading... |
718 true, | 516 true, |
719 base::Bind(&BluetoothDeviceChromeOS::OnSetTrusted, | 517 base::Bind(&BluetoothDeviceChromeOS::OnSetTrusted, |
720 weak_ptr_factory_.GetWeakPtr())); | 518 weak_ptr_factory_.GetWeakPtr())); |
721 } | 519 } |
722 | 520 |
723 void BluetoothDeviceChromeOS::OnSetTrusted(bool success) { | 521 void BluetoothDeviceChromeOS::OnSetTrusted(bool success) { |
724 LOG_IF(WARNING, !success) << object_path_.value() | 522 LOG_IF(WARNING, !success) << object_path_.value() |
725 << ": Failed to set device as trusted"; | 523 << ": Failed to set device as trusted"; |
726 } | 524 } |
727 | 525 |
728 void BluetoothDeviceChromeOS::UnregisterAgent() { | |
729 if (!agent_.get()) | |
730 return; | |
731 | |
732 DCHECK(pairing_delegate_); | |
733 | |
734 DCHECK(pincode_callback_.is_null()); | |
735 DCHECK(passkey_callback_.is_null()); | |
736 DCHECK(confirmation_callback_.is_null()); | |
737 | |
738 pairing_delegate_->DismissDisplayOrConfirm(); | |
739 pairing_delegate_ = NULL; | |
740 | |
741 agent_.reset(); | |
742 | |
743 // Clean up after ourselves. | |
744 VLOG(1) << object_path_.value() << ": Unregistering pairing agent"; | |
745 DBusThreadManager::Get()->GetBluetoothAgentManagerClient()-> | |
746 UnregisterAgent( | |
747 dbus::ObjectPath(kAgentPath), | |
748 base::Bind(&base::DoNothing), | |
749 base::Bind(&BluetoothDeviceChromeOS::OnUnregisterAgentError, | |
750 weak_ptr_factory_.GetWeakPtr())); | |
751 } | |
752 | |
753 void BluetoothDeviceChromeOS::OnUnregisterAgentError( | |
754 const std::string& error_name, | |
755 const std::string& error_message) { | |
756 LOG(WARNING) << object_path_.value() << ": Failed to unregister agent: " | |
757 << error_name << ": " << error_message; | |
758 } | |
759 | |
760 void BluetoothDeviceChromeOS::OnDisconnect(const base::Closure& callback) { | 526 void BluetoothDeviceChromeOS::OnDisconnect(const base::Closure& callback) { |
761 VLOG(1) << object_path_.value() << ": Disconnected"; | 527 VLOG(1) << object_path_.value() << ": Disconnected"; |
762 callback.Run(); | 528 callback.Run(); |
763 } | 529 } |
764 | 530 |
765 void BluetoothDeviceChromeOS::OnDisconnectError( | 531 void BluetoothDeviceChromeOS::OnDisconnectError( |
766 const ErrorCallback& error_callback, | 532 const ErrorCallback& error_callback, |
767 const std::string& error_name, | 533 const std::string& error_name, |
768 const std::string& error_message) { | 534 const std::string& error_message) { |
769 LOG(WARNING) << object_path_.value() << ": Failed to disconnect device: " | 535 LOG(WARNING) << object_path_.value() << ": Failed to disconnect device: " |
770 << error_name << ": " << error_message; | 536 << error_name << ": " << error_message; |
771 error_callback.Run(); | 537 error_callback.Run(); |
772 } | 538 } |
773 | 539 |
774 void BluetoothDeviceChromeOS::OnForgetError( | 540 void BluetoothDeviceChromeOS::OnForgetError( |
775 const ErrorCallback& error_callback, | 541 const ErrorCallback& error_callback, |
776 const std::string& error_name, | 542 const std::string& error_name, |
777 const std::string& error_message) { | 543 const std::string& error_message) { |
778 LOG(WARNING) << object_path_.value() << ": Failed to remove device: " | 544 LOG(WARNING) << object_path_.value() << ": Failed to remove device: " |
779 << error_name << ": " << error_message; | 545 << error_name << ": " << error_message; |
780 error_callback.Run(); | 546 error_callback.Run(); |
781 } | 547 } |
782 | 548 |
783 bool BluetoothDeviceChromeOS::RunPairingCallbacks(Status status) { | |
784 if (!agent_.get()) | |
785 return false; | |
786 | |
787 bool callback_run = false; | |
788 if (!pincode_callback_.is_null()) { | |
789 pincode_callback_.Run(status, ""); | |
790 pincode_callback_.Reset(); | |
791 callback_run = true; | |
792 } | |
793 | |
794 if (!passkey_callback_.is_null()) { | |
795 passkey_callback_.Run(status, 0); | |
796 passkey_callback_.Reset(); | |
797 callback_run = true; | |
798 } | |
799 | |
800 if (!confirmation_callback_.is_null()) { | |
801 confirmation_callback_.Run(status); | |
802 confirmation_callback_.Reset(); | |
803 callback_run = true; | |
804 } | |
805 | |
806 return callback_run; | |
807 } | |
808 | |
809 void BluetoothDeviceChromeOS::OnConnectProfile( | 549 void BluetoothDeviceChromeOS::OnConnectProfile( |
810 device::BluetoothProfile* profile, | 550 device::BluetoothProfile* profile, |
811 const base::Closure& callback) { | 551 const base::Closure& callback) { |
812 BluetoothProfileChromeOS* profile_chromeos = | 552 BluetoothProfileChromeOS* profile_chromeos = |
813 static_cast<BluetoothProfileChromeOS*>(profile); | 553 static_cast<BluetoothProfileChromeOS*>(profile); |
814 VLOG(1) << object_path_.value() << ": Profile connected: " | 554 VLOG(1) << object_path_.value() << ": Profile connected: " |
815 << profile_chromeos->uuid(); | 555 << profile_chromeos->uuid(); |
816 callback.Run(); | 556 callback.Run(); |
817 } | 557 } |
818 | 558 |
819 void BluetoothDeviceChromeOS::OnConnectProfileError( | 559 void BluetoothDeviceChromeOS::OnConnectProfileError( |
820 device::BluetoothProfile* profile, | 560 device::BluetoothProfile* profile, |
821 const ErrorCallback& error_callback, | 561 const ErrorCallback& error_callback, |
822 const std::string& error_name, | 562 const std::string& error_name, |
823 const std::string& error_message) { | 563 const std::string& error_message) { |
824 BluetoothProfileChromeOS* profile_chromeos = | 564 BluetoothProfileChromeOS* profile_chromeos = |
825 static_cast<BluetoothProfileChromeOS*>(profile); | 565 static_cast<BluetoothProfileChromeOS*>(profile); |
826 VLOG(1) << object_path_.value() << ": Profile connection failed: " | 566 VLOG(1) << object_path_.value() << ": Profile connection failed: " |
827 << profile_chromeos->uuid() << ": " | 567 << profile_chromeos->uuid() << ": " |
828 << error_name << ": " << error_message; | 568 << error_name << ": " << error_message; |
829 error_callback.Run(); | 569 error_callback.Run(); |
830 } | 570 } |
831 | 571 |
832 } // namespace chromeos | 572 } // namespace chromeos |
OLD | NEW |