| 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_adapter_chromeos.h" | 5 #include "device/bluetooth/bluetooth_adapter_chromeos.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 } | 65 } |
| 66 | 66 |
| 67 namespace chromeos { | 67 namespace chromeos { |
| 68 | 68 |
| 69 // static | 69 // static |
| 70 base::WeakPtr<BluetoothAdapter> BluetoothAdapterChromeOS::CreateAdapter() { | 70 base::WeakPtr<BluetoothAdapter> BluetoothAdapterChromeOS::CreateAdapter() { |
| 71 BluetoothAdapterChromeOS* adapter = new BluetoothAdapterChromeOS(); | 71 BluetoothAdapterChromeOS* adapter = new BluetoothAdapterChromeOS(); |
| 72 return adapter->weak_ptr_factory_.GetWeakPtr(); | 72 return adapter->weak_ptr_factory_.GetWeakPtr(); |
| 73 } | 73 } |
| 74 | 74 |
| 75 void BluetoothAdapterChromeOS::OnDBusThreadManagerShutdown() { |
| 76 if (dbus_is_shutdown_) |
| 77 return; |
| 78 DCHECK(DBusThreadManager::IsInitialized()) |
| 79 << "Call BluetoothAdapterFactory::OnDBusThreadManagerShutdown() before " |
| 80 "DBusThreadManager::Shutdown()."; |
| 81 |
| 82 if (IsPresent()) |
| 83 RemoveAdapter(); // Also deletes devices_. |
| 84 DCHECK(devices_.empty()); |
| 85 |
| 86 DBusThreadManager::Get()->GetBluetoothAdapterClient()->RemoveObserver(this); |
| 87 DBusThreadManager::Get()->GetBluetoothDeviceClient()->RemoveObserver(this); |
| 88 DBusThreadManager::Get()->GetBluetoothInputClient()->RemoveObserver(this); |
| 89 |
| 90 VLOG(1) << "Unregistering pairing agent"; |
| 91 DBusThreadManager::Get()->GetBluetoothAgentManagerClient()->UnregisterAgent( |
| 92 dbus::ObjectPath(kAgentPath), base::Bind(&base::DoNothing), |
| 93 base::Bind(&OnUnregisterAgentError)); |
| 94 |
| 95 agent_.reset(); |
| 96 dbus_is_shutdown_ = true; |
| 97 } |
| 98 |
| 75 BluetoothAdapterChromeOS::BluetoothAdapterChromeOS() | 99 BluetoothAdapterChromeOS::BluetoothAdapterChromeOS() |
| 76 : num_discovery_sessions_(0), | 100 : dbus_is_shutdown_(false), |
| 101 num_discovery_sessions_(0), |
| 77 discovery_request_pending_(false), | 102 discovery_request_pending_(false), |
| 78 weak_ptr_factory_(this) { | 103 weak_ptr_factory_(this) { |
| 79 ui_task_runner_ = base::ThreadTaskRunnerHandle::Get(); | 104 ui_task_runner_ = base::ThreadTaskRunnerHandle::Get(); |
| 80 socket_thread_ = device::BluetoothSocketThread::Get(); | 105 socket_thread_ = device::BluetoothSocketThread::Get(); |
| 81 | 106 |
| 82 DBusThreadManager::Get()->GetBluetoothAdapterClient()->AddObserver(this); | 107 DBusThreadManager::Get()->GetBluetoothAdapterClient()->AddObserver(this); |
| 83 DBusThreadManager::Get()->GetBluetoothDeviceClient()->AddObserver(this); | 108 DBusThreadManager::Get()->GetBluetoothDeviceClient()->AddObserver(this); |
| 84 DBusThreadManager::Get()->GetBluetoothInputClient()->AddObserver(this); | 109 DBusThreadManager::Get()->GetBluetoothInputClient()->AddObserver(this); |
| 85 | 110 |
| 86 // Register the pairing agent. | 111 // Register the pairing agent. |
| 87 dbus::Bus* system_bus = DBusThreadManager::Get()->GetSystemBus(); | 112 dbus::Bus* system_bus = DBusThreadManager::Get()->GetSystemBus(); |
| 88 agent_.reset(BluetoothAgentServiceProvider::Create( | 113 agent_.reset(BluetoothAgentServiceProvider::Create( |
| 89 system_bus, dbus::ObjectPath(kAgentPath), this)); | 114 system_bus, dbus::ObjectPath(kAgentPath), this)); |
| 90 DCHECK(agent_.get()); | 115 DCHECK(agent_.get()); |
| 91 | 116 |
| 92 std::vector<dbus::ObjectPath> object_paths = | 117 std::vector<dbus::ObjectPath> object_paths = |
| 93 DBusThreadManager::Get()->GetBluetoothAdapterClient()->GetAdapters(); | 118 DBusThreadManager::Get()->GetBluetoothAdapterClient()->GetAdapters(); |
| 94 | 119 |
| 95 if (!object_paths.empty()) { | 120 if (!object_paths.empty()) { |
| 96 VLOG(1) << object_paths.size() << " Bluetooth adapter(s) available."; | 121 VLOG(1) << object_paths.size() << " Bluetooth adapter(s) available."; |
| 97 SetAdapter(object_paths[0]); | 122 SetAdapter(object_paths[0]); |
| 98 } | 123 } |
| 99 } | 124 } |
| 100 | 125 |
| 101 BluetoothAdapterChromeOS::~BluetoothAdapterChromeOS() { | 126 BluetoothAdapterChromeOS::~BluetoothAdapterChromeOS() { |
| 102 DBusThreadManager::Get()->GetBluetoothAdapterClient()->RemoveObserver(this); | 127 OnDBusThreadManagerShutdown(); |
| 103 DBusThreadManager::Get()->GetBluetoothDeviceClient()->RemoveObserver(this); | |
| 104 DBusThreadManager::Get()->GetBluetoothInputClient()->RemoveObserver(this); | |
| 105 | |
| 106 VLOG(1) << "Unregistering pairing agent"; | |
| 107 DBusThreadManager::Get()->GetBluetoothAgentManagerClient()-> | |
| 108 UnregisterAgent( | |
| 109 dbus::ObjectPath(kAgentPath), | |
| 110 base::Bind(&base::DoNothing), | |
| 111 base::Bind(&OnUnregisterAgentError)); | |
| 112 } | 128 } |
| 113 | 129 |
| 114 void BluetoothAdapterChromeOS::DeleteOnCorrectThread() const { | 130 void BluetoothAdapterChromeOS::DeleteOnCorrectThread() const { |
| 115 if (ui_task_runner_->RunsTasksOnCurrentThread() || | 131 if (ui_task_runner_->RunsTasksOnCurrentThread() || |
| 116 !ui_task_runner_->DeleteSoon(FROM_HERE, this)) | 132 !ui_task_runner_->DeleteSoon(FROM_HERE, this)) |
| 117 delete this; | 133 delete this; |
| 118 } | 134 } |
| 119 | 135 |
| 120 void BluetoothAdapterChromeOS::AddObserver( | 136 void BluetoothAdapterChromeOS::AddObserver( |
| 121 BluetoothAdapter::Observer* observer) { | 137 BluetoothAdapter::Observer* observer) { |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 weak_ptr_factory_.GetWeakPtr(), | 184 weak_ptr_factory_.GetWeakPtr(), |
| 169 callback, | 185 callback, |
| 170 error_callback)); | 186 error_callback)); |
| 171 } | 187 } |
| 172 | 188 |
| 173 bool BluetoothAdapterChromeOS::IsInitialized() const { | 189 bool BluetoothAdapterChromeOS::IsInitialized() const { |
| 174 return true; | 190 return true; |
| 175 } | 191 } |
| 176 | 192 |
| 177 bool BluetoothAdapterChromeOS::IsPresent() const { | 193 bool BluetoothAdapterChromeOS::IsPresent() const { |
| 178 return !object_path_.value().empty(); | 194 return !dbus_is_shutdown_ && !object_path_.value().empty(); |
| 179 } | 195 } |
| 180 | 196 |
| 181 bool BluetoothAdapterChromeOS::IsPowered() const { | 197 bool BluetoothAdapterChromeOS::IsPowered() const { |
| 182 if (!IsPresent()) | 198 if (!IsPresent()) |
| 183 return false; | 199 return false; |
| 184 | 200 |
| 185 BluetoothAdapterClient::Properties* properties = | 201 BluetoothAdapterClient::Properties* properties = |
| 186 DBusThreadManager::Get()->GetBluetoothAdapterClient()-> | 202 DBusThreadManager::Get()->GetBluetoothAdapterClient()-> |
| 187 GetProperties(object_path_); | 203 GetProperties(object_path_); |
| 188 | 204 |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 245 GetProperties(object_path_); | 261 GetProperties(object_path_); |
| 246 | 262 |
| 247 return properties->discovering.value(); | 263 return properties->discovering.value(); |
| 248 } | 264 } |
| 249 | 265 |
| 250 void BluetoothAdapterChromeOS::CreateRfcommService( | 266 void BluetoothAdapterChromeOS::CreateRfcommService( |
| 251 const BluetoothUUID& uuid, | 267 const BluetoothUUID& uuid, |
| 252 const ServiceOptions& options, | 268 const ServiceOptions& options, |
| 253 const CreateServiceCallback& callback, | 269 const CreateServiceCallback& callback, |
| 254 const CreateServiceErrorCallback& error_callback) { | 270 const CreateServiceErrorCallback& error_callback) { |
| 271 DCHECK(!dbus_is_shutdown_); |
| 255 VLOG(1) << object_path_.value() << ": Creating RFCOMM service: " | 272 VLOG(1) << object_path_.value() << ": Creating RFCOMM service: " |
| 256 << uuid.canonical_value(); | 273 << uuid.canonical_value(); |
| 257 scoped_refptr<BluetoothSocketChromeOS> socket = | 274 scoped_refptr<BluetoothSocketChromeOS> socket = |
| 258 BluetoothSocketChromeOS::CreateBluetoothSocket( | 275 BluetoothSocketChromeOS::CreateBluetoothSocket( |
| 259 ui_task_runner_, socket_thread_); | 276 ui_task_runner_, socket_thread_); |
| 260 socket->Listen(this, | 277 socket->Listen(this, |
| 261 BluetoothSocketChromeOS::kRfcomm, | 278 BluetoothSocketChromeOS::kRfcomm, |
| 262 uuid, | 279 uuid, |
| 263 options, | 280 options, |
| 264 base::Bind(callback, socket), | 281 base::Bind(callback, socket), |
| 265 error_callback); | 282 error_callback); |
| 266 } | 283 } |
| 267 | 284 |
| 268 void BluetoothAdapterChromeOS::CreateL2capService( | 285 void BluetoothAdapterChromeOS::CreateL2capService( |
| 269 const BluetoothUUID& uuid, | 286 const BluetoothUUID& uuid, |
| 270 const ServiceOptions& options, | 287 const ServiceOptions& options, |
| 271 const CreateServiceCallback& callback, | 288 const CreateServiceCallback& callback, |
| 272 const CreateServiceErrorCallback& error_callback) { | 289 const CreateServiceErrorCallback& error_callback) { |
| 290 DCHECK(!dbus_is_shutdown_); |
| 273 VLOG(1) << object_path_.value() << ": Creating L2CAP service: " | 291 VLOG(1) << object_path_.value() << ": Creating L2CAP service: " |
| 274 << uuid.canonical_value(); | 292 << uuid.canonical_value(); |
| 275 scoped_refptr<BluetoothSocketChromeOS> socket = | 293 scoped_refptr<BluetoothSocketChromeOS> socket = |
| 276 BluetoothSocketChromeOS::CreateBluetoothSocket( | 294 BluetoothSocketChromeOS::CreateBluetoothSocket( |
| 277 ui_task_runner_, socket_thread_); | 295 ui_task_runner_, socket_thread_); |
| 278 socket->Listen(this, | 296 socket->Listen(this, |
| 279 BluetoothSocketChromeOS::kL2cap, | 297 BluetoothSocketChromeOS::kL2cap, |
| 280 uuid, | 298 uuid, |
| 281 options, | 299 options, |
| 282 base::Bind(callback, socket), | 300 base::Bind(callback, socket), |
| 283 error_callback); | 301 error_callback); |
| 284 } | 302 } |
| 285 | 303 |
| 286 void BluetoothAdapterChromeOS::RegisterAudioSink( | 304 void BluetoothAdapterChromeOS::RegisterAudioSink( |
| 287 const device::BluetoothAudioSink::Options& options, | 305 const device::BluetoothAudioSink::Options& options, |
| 288 const device::BluetoothAdapter::AcquiredCallback& callback, | 306 const device::BluetoothAdapter::AcquiredCallback& callback, |
| 289 const device::BluetoothAudioSink::ErrorCallback& error_callback) { | 307 const device::BluetoothAudioSink::ErrorCallback& error_callback) { |
| 290 // TODO(mcchou): Create and register a BluetoothAudioSink. Add the | 308 // TODO(mcchou): Create and register a BluetoothAudioSink. Add the |
| 291 // newly-created audio sink as an observer of the adapter. | 309 // newly-created audio sink as an observer of the adapter. |
| 292 // Add OnRegisterAudioSink(AcquiredCallback& , BluetoothAudioSink*) and pass | 310 // Add OnRegisterAudioSink(AcquiredCallback& , BluetoothAudioSink*) and pass |
| 293 // it as an argument to BluetoothAudioSinkChromeOS::Register. | 311 // it as an argument to BluetoothAudioSinkChromeOS::Register. |
| 294 error_callback.Run(device::BluetoothAudioSink::ERRORCODE_NOT_SUPPORTED); | 312 error_callback.Run(device::BluetoothAudioSink::ERRORCODE_NOT_SUPPORTED); |
| 295 } | 313 } |
| 296 | 314 |
| 297 void BluetoothAdapterChromeOS::RemovePairingDelegateInternal( | 315 void BluetoothAdapterChromeOS::RemovePairingDelegateInternal( |
| 298 BluetoothDevice::PairingDelegate* pairing_delegate) { | 316 BluetoothDevice::PairingDelegate* pairing_delegate) { |
| 317 DCHECK(IsPresent()); |
| 299 // Before removing a pairing delegate make sure that there aren't any devices | 318 // Before removing a pairing delegate make sure that there aren't any devices |
| 300 // currently using it; if there are, clear the pairing context which will | 319 // currently using it; if there are, clear the pairing context which will |
| 301 // make any responses no-ops. | 320 // make any responses no-ops. |
| 302 for (DevicesMap::iterator iter = devices_.begin(); | 321 for (DevicesMap::iterator iter = devices_.begin(); |
| 303 iter != devices_.end(); ++iter) { | 322 iter != devices_.end(); ++iter) { |
| 304 BluetoothDeviceChromeOS* device_chromeos = | 323 BluetoothDeviceChromeOS* device_chromeos = |
| 305 static_cast<BluetoothDeviceChromeOS*>(iter->second); | 324 static_cast<BluetoothDeviceChromeOS*>(iter->second); |
| 306 | 325 |
| 307 BluetoothPairingChromeOS* pairing = device_chromeos->GetPairing(); | 326 BluetoothPairingChromeOS* pairing = device_chromeos->GetPairing(); |
| 308 if (pairing && pairing->GetPairingDelegate() == pairing_delegate) | 327 if (pairing && pairing->GetPairingDelegate() == pairing_delegate) |
| (...skipping 10 matching lines...) Expand all Loading... |
| 319 | 338 |
| 320 void BluetoothAdapterChromeOS::AdapterRemoved( | 339 void BluetoothAdapterChromeOS::AdapterRemoved( |
| 321 const dbus::ObjectPath& object_path) { | 340 const dbus::ObjectPath& object_path) { |
| 322 if (object_path == object_path_) | 341 if (object_path == object_path_) |
| 323 RemoveAdapter(); | 342 RemoveAdapter(); |
| 324 } | 343 } |
| 325 | 344 |
| 326 void BluetoothAdapterChromeOS::AdapterPropertyChanged( | 345 void BluetoothAdapterChromeOS::AdapterPropertyChanged( |
| 327 const dbus::ObjectPath& object_path, | 346 const dbus::ObjectPath& object_path, |
| 328 const std::string& property_name) { | 347 const std::string& property_name) { |
| 348 DCHECK(IsPresent()); |
| 329 if (object_path != object_path_) | 349 if (object_path != object_path_) |
| 330 return; | 350 return; |
| 331 | 351 |
| 332 BluetoothAdapterClient::Properties* properties = | 352 BluetoothAdapterClient::Properties* properties = |
| 333 DBusThreadManager::Get()->GetBluetoothAdapterClient()-> | 353 DBusThreadManager::Get()->GetBluetoothAdapterClient()-> |
| 334 GetProperties(object_path_); | 354 GetProperties(object_path_); |
| 335 | 355 |
| 336 if (property_name == properties->powered.name()) | 356 if (property_name == properties->powered.name()) |
| 337 PoweredChanged(properties->powered.value()); | 357 PoweredChanged(properties->powered.value()); |
| 338 else if (property_name == properties->discoverable.name()) | 358 else if (property_name == properties->discoverable.name()) |
| 339 DiscoverableChanged(properties->discoverable.value()); | 359 DiscoverableChanged(properties->discoverable.value()); |
| 340 else if (property_name == properties->discovering.name()) | 360 else if (property_name == properties->discovering.name()) |
| 341 DiscoveringChanged(properties->discovering.value()); | 361 DiscoveringChanged(properties->discovering.value()); |
| 342 } | 362 } |
| 343 | 363 |
| 344 void BluetoothAdapterChromeOS::DeviceAdded( | 364 void BluetoothAdapterChromeOS::DeviceAdded( |
| 345 const dbus::ObjectPath& object_path) { | 365 const dbus::ObjectPath& object_path) { |
| 366 DCHECK(IsPresent()); |
| 346 BluetoothDeviceClient::Properties* properties = | 367 BluetoothDeviceClient::Properties* properties = |
| 347 DBusThreadManager::Get()->GetBluetoothDeviceClient()-> | 368 DBusThreadManager::Get()->GetBluetoothDeviceClient()-> |
| 348 GetProperties(object_path); | 369 GetProperties(object_path); |
| 349 if (properties->adapter.value() != object_path_) | 370 if (properties->adapter.value() != object_path_) |
| 350 return; | 371 return; |
| 351 | 372 |
| 352 BluetoothDeviceChromeOS* device_chromeos = | 373 BluetoothDeviceChromeOS* device_chromeos = |
| 353 new BluetoothDeviceChromeOS(this, | 374 new BluetoothDeviceChromeOS(this, |
| 354 object_path, | 375 object_path, |
| 355 ui_task_runner_, | 376 ui_task_runner_, |
| 356 socket_thread_); | 377 socket_thread_); |
| 357 DCHECK(devices_.find(device_chromeos->GetAddress()) == devices_.end()); | 378 DCHECK(devices_.find(device_chromeos->GetAddress()) == devices_.end()); |
| 358 | 379 |
| 359 devices_[device_chromeos->GetAddress()] = device_chromeos; | 380 devices_[device_chromeos->GetAddress()] = device_chromeos; |
| 360 | 381 |
| 361 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | 382 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, |
| 362 DeviceAdded(this, device_chromeos)); | 383 DeviceAdded(this, device_chromeos)); |
| 363 } | 384 } |
| 364 | 385 |
| 365 void BluetoothAdapterChromeOS::DeviceRemoved( | 386 void BluetoothAdapterChromeOS::DeviceRemoved( |
| 366 const dbus::ObjectPath& object_path) { | 387 const dbus::ObjectPath& object_path) { |
| 388 DCHECK(IsPresent()); |
| 367 for (DevicesMap::iterator iter = devices_.begin(); | 389 for (DevicesMap::iterator iter = devices_.begin(); |
| 368 iter != devices_.end(); ++iter) { | 390 iter != devices_.end(); ++iter) { |
| 369 BluetoothDeviceChromeOS* device_chromeos = | 391 BluetoothDeviceChromeOS* device_chromeos = |
| 370 static_cast<BluetoothDeviceChromeOS*>(iter->second); | 392 static_cast<BluetoothDeviceChromeOS*>(iter->second); |
| 371 if (device_chromeos->object_path() == object_path) { | 393 if (device_chromeos->object_path() == object_path) { |
| 372 devices_.erase(iter); | 394 devices_.erase(iter); |
| 373 | 395 |
| 374 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | 396 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, |
| 375 DeviceRemoved(this, device_chromeos)); | 397 DeviceRemoved(this, device_chromeos)); |
| 376 delete device_chromeos; | 398 delete device_chromeos; |
| 377 return; | 399 return; |
| 378 } | 400 } |
| 379 } | 401 } |
| 380 } | 402 } |
| 381 | 403 |
| 382 void BluetoothAdapterChromeOS::DevicePropertyChanged( | 404 void BluetoothAdapterChromeOS::DevicePropertyChanged( |
| 383 const dbus::ObjectPath& object_path, | 405 const dbus::ObjectPath& object_path, |
| 384 const std::string& property_name) { | 406 const std::string& property_name) { |
| 407 DCHECK(IsPresent()); |
| 385 BluetoothDeviceChromeOS* device_chromeos = GetDeviceWithPath(object_path); | 408 BluetoothDeviceChromeOS* device_chromeos = GetDeviceWithPath(object_path); |
| 386 if (!device_chromeos) | 409 if (!device_chromeos) |
| 387 return; | 410 return; |
| 388 | 411 |
| 389 BluetoothDeviceClient::Properties* properties = | 412 BluetoothDeviceClient::Properties* properties = |
| 390 DBusThreadManager::Get()->GetBluetoothDeviceClient()-> | 413 DBusThreadManager::Get()->GetBluetoothDeviceClient()-> |
| 391 GetProperties(object_path); | 414 GetProperties(object_path); |
| 392 | 415 |
| 393 if (property_name == properties->bluetooth_class.name() || | 416 if (property_name == properties->bluetooth_class.name() || |
| 394 property_name == properties->address.name() || | 417 property_name == properties->address.name() || |
| (...skipping 28 matching lines...) Expand all Loading... |
| 423 ++count; | 446 ++count; |
| 424 } | 447 } |
| 425 | 448 |
| 426 UMA_HISTOGRAM_COUNTS_100("Bluetooth.ConnectedDeviceCount", count); | 449 UMA_HISTOGRAM_COUNTS_100("Bluetooth.ConnectedDeviceCount", count); |
| 427 } | 450 } |
| 428 } | 451 } |
| 429 | 452 |
| 430 void BluetoothAdapterChromeOS::InputPropertyChanged( | 453 void BluetoothAdapterChromeOS::InputPropertyChanged( |
| 431 const dbus::ObjectPath& object_path, | 454 const dbus::ObjectPath& object_path, |
| 432 const std::string& property_name) { | 455 const std::string& property_name) { |
| 456 DCHECK(IsPresent()); |
| 433 BluetoothDeviceChromeOS* device_chromeos = GetDeviceWithPath(object_path); | 457 BluetoothDeviceChromeOS* device_chromeos = GetDeviceWithPath(object_path); |
| 434 if (!device_chromeos) | 458 if (!device_chromeos) |
| 435 return; | 459 return; |
| 436 | 460 |
| 437 BluetoothInputClient::Properties* properties = | 461 BluetoothInputClient::Properties* properties = |
| 438 DBusThreadManager::Get()->GetBluetoothInputClient()-> | 462 DBusThreadManager::Get()->GetBluetoothInputClient()-> |
| 439 GetProperties(object_path); | 463 GetProperties(object_path); |
| 440 | 464 |
| 441 // Properties structure can be removed, which triggers a change in the | 465 // Properties structure can be removed, which triggers a change in the |
| 442 // BluetoothDevice::IsConnectable() property, as does a change in the | 466 // BluetoothDevice::IsConnectable() property, as does a change in the |
| 443 // actual reconnect_mode property. | 467 // actual reconnect_mode property. |
| 444 if (!properties || | 468 if (!properties || |
| 445 property_name == properties->reconnect_mode.name()) | 469 property_name == properties->reconnect_mode.name()) |
| 446 NotifyDeviceChanged(device_chromeos); | 470 NotifyDeviceChanged(device_chromeos); |
| 447 } | 471 } |
| 448 | 472 |
| 449 void BluetoothAdapterChromeOS::Released() { | 473 void BluetoothAdapterChromeOS::Released() { |
| 474 DCHECK(IsPresent()); |
| 450 DCHECK(agent_.get()); | 475 DCHECK(agent_.get()); |
| 451 VLOG(1) << "Release"; | 476 VLOG(1) << "Release"; |
| 452 | 477 |
| 453 // Called after we unregister the pairing agent, e.g. when changing I/O | 478 // Called after we unregister the pairing agent, e.g. when changing I/O |
| 454 // capabilities. Nothing much to be done right now. | 479 // capabilities. Nothing much to be done right now. |
| 455 } | 480 } |
| 456 | 481 |
| 457 void BluetoothAdapterChromeOS::RequestPinCode( | 482 void BluetoothAdapterChromeOS::RequestPinCode( |
| 458 const dbus::ObjectPath& device_path, | 483 const dbus::ObjectPath& device_path, |
| 459 const PinCodeCallback& callback) { | 484 const PinCodeCallback& callback) { |
| 485 DCHECK(IsPresent()); |
| 460 DCHECK(agent_.get()); | 486 DCHECK(agent_.get()); |
| 461 VLOG(1) << device_path.value() << ": RequestPinCode"; | 487 VLOG(1) << device_path.value() << ": RequestPinCode"; |
| 462 | 488 |
| 463 BluetoothPairingChromeOS* pairing = GetPairing(device_path); | 489 BluetoothPairingChromeOS* pairing = GetPairing(device_path); |
| 464 if (!pairing) { | 490 if (!pairing) { |
| 465 callback.Run(REJECTED, ""); | 491 callback.Run(REJECTED, ""); |
| 466 return; | 492 return; |
| 467 } | 493 } |
| 468 | 494 |
| 469 pairing->RequestPinCode(callback); | 495 pairing->RequestPinCode(callback); |
| 470 } | 496 } |
| 471 | 497 |
| 472 void BluetoothAdapterChromeOS::DisplayPinCode( | 498 void BluetoothAdapterChromeOS::DisplayPinCode( |
| 473 const dbus::ObjectPath& device_path, | 499 const dbus::ObjectPath& device_path, |
| 474 const std::string& pincode) { | 500 const std::string& pincode) { |
| 501 DCHECK(IsPresent()); |
| 475 DCHECK(agent_.get()); | 502 DCHECK(agent_.get()); |
| 476 VLOG(1) << device_path.value() << ": DisplayPinCode: " << pincode; | 503 VLOG(1) << device_path.value() << ": DisplayPinCode: " << pincode; |
| 477 | 504 |
| 478 BluetoothPairingChromeOS* pairing = GetPairing(device_path); | 505 BluetoothPairingChromeOS* pairing = GetPairing(device_path); |
| 479 if (!pairing) | 506 if (!pairing) |
| 480 return; | 507 return; |
| 481 | 508 |
| 482 pairing->DisplayPinCode(pincode); | 509 pairing->DisplayPinCode(pincode); |
| 483 } | 510 } |
| 484 | 511 |
| 485 void BluetoothAdapterChromeOS::RequestPasskey( | 512 void BluetoothAdapterChromeOS::RequestPasskey( |
| 486 const dbus::ObjectPath& device_path, | 513 const dbus::ObjectPath& device_path, |
| 487 const PasskeyCallback& callback) { | 514 const PasskeyCallback& callback) { |
| 515 DCHECK(IsPresent()); |
| 488 DCHECK(agent_.get()); | 516 DCHECK(agent_.get()); |
| 489 VLOG(1) << device_path.value() << ": RequestPasskey"; | 517 VLOG(1) << device_path.value() << ": RequestPasskey"; |
| 490 | 518 |
| 491 BluetoothPairingChromeOS* pairing = GetPairing(device_path); | 519 BluetoothPairingChromeOS* pairing = GetPairing(device_path); |
| 492 if (!pairing) { | 520 if (!pairing) { |
| 493 callback.Run(REJECTED, 0); | 521 callback.Run(REJECTED, 0); |
| 494 return; | 522 return; |
| 495 } | 523 } |
| 496 | 524 |
| 497 pairing->RequestPasskey(callback); | 525 pairing->RequestPasskey(callback); |
| 498 } | 526 } |
| 499 | 527 |
| 500 void BluetoothAdapterChromeOS::DisplayPasskey( | 528 void BluetoothAdapterChromeOS::DisplayPasskey( |
| 501 const dbus::ObjectPath& device_path, | 529 const dbus::ObjectPath& device_path, |
| 502 uint32 passkey, | 530 uint32 passkey, |
| 503 uint16 entered) { | 531 uint16 entered) { |
| 532 DCHECK(IsPresent()); |
| 504 DCHECK(agent_.get()); | 533 DCHECK(agent_.get()); |
| 505 VLOG(1) << device_path.value() << ": DisplayPasskey: " << passkey | 534 VLOG(1) << device_path.value() << ": DisplayPasskey: " << passkey |
| 506 << " (" << entered << " entered)"; | 535 << " (" << entered << " entered)"; |
| 507 | 536 |
| 508 BluetoothPairingChromeOS* pairing = GetPairing(device_path); | 537 BluetoothPairingChromeOS* pairing = GetPairing(device_path); |
| 509 if (!pairing) | 538 if (!pairing) |
| 510 return; | 539 return; |
| 511 | 540 |
| 512 if (entered == 0) | 541 if (entered == 0) |
| 513 pairing->DisplayPasskey(passkey); | 542 pairing->DisplayPasskey(passkey); |
| 514 | 543 |
| 515 pairing->KeysEntered(entered); | 544 pairing->KeysEntered(entered); |
| 516 } | 545 } |
| 517 | 546 |
| 518 void BluetoothAdapterChromeOS::RequestConfirmation( | 547 void BluetoothAdapterChromeOS::RequestConfirmation( |
| 519 const dbus::ObjectPath& device_path, | 548 const dbus::ObjectPath& device_path, |
| 520 uint32 passkey, | 549 uint32 passkey, |
| 521 const ConfirmationCallback& callback) { | 550 const ConfirmationCallback& callback) { |
| 551 DCHECK(IsPresent()); |
| 522 DCHECK(agent_.get()); | 552 DCHECK(agent_.get()); |
| 523 VLOG(1) << device_path.value() << ": RequestConfirmation: " << passkey; | 553 VLOG(1) << device_path.value() << ": RequestConfirmation: " << passkey; |
| 524 | 554 |
| 525 BluetoothPairingChromeOS* pairing = GetPairing(device_path); | 555 BluetoothPairingChromeOS* pairing = GetPairing(device_path); |
| 526 if (!pairing) { | 556 if (!pairing) { |
| 527 callback.Run(REJECTED); | 557 callback.Run(REJECTED); |
| 528 return; | 558 return; |
| 529 } | 559 } |
| 530 | 560 |
| 531 pairing->RequestConfirmation(passkey, callback); | 561 pairing->RequestConfirmation(passkey, callback); |
| 532 } | 562 } |
| 533 | 563 |
| 534 void BluetoothAdapterChromeOS::RequestAuthorization( | 564 void BluetoothAdapterChromeOS::RequestAuthorization( |
| 535 const dbus::ObjectPath& device_path, | 565 const dbus::ObjectPath& device_path, |
| 536 const ConfirmationCallback& callback) { | 566 const ConfirmationCallback& callback) { |
| 567 DCHECK(IsPresent()); |
| 537 DCHECK(agent_.get()); | 568 DCHECK(agent_.get()); |
| 538 VLOG(1) << device_path.value() << ": RequestAuthorization"; | 569 VLOG(1) << device_path.value() << ": RequestAuthorization"; |
| 539 | 570 |
| 540 BluetoothPairingChromeOS* pairing = GetPairing(device_path); | 571 BluetoothPairingChromeOS* pairing = GetPairing(device_path); |
| 541 if (!pairing) { | 572 if (!pairing) { |
| 542 callback.Run(REJECTED); | 573 callback.Run(REJECTED); |
| 543 return; | 574 return; |
| 544 } | 575 } |
| 545 | 576 |
| 546 pairing->RequestAuthorization(callback); | 577 pairing->RequestAuthorization(callback); |
| 547 } | 578 } |
| 548 | 579 |
| 549 void BluetoothAdapterChromeOS::AuthorizeService( | 580 void BluetoothAdapterChromeOS::AuthorizeService( |
| 550 const dbus::ObjectPath& device_path, | 581 const dbus::ObjectPath& device_path, |
| 551 const std::string& uuid, | 582 const std::string& uuid, |
| 552 const ConfirmationCallback& callback) { | 583 const ConfirmationCallback& callback) { |
| 584 DCHECK(IsPresent()); |
| 553 DCHECK(agent_.get()); | 585 DCHECK(agent_.get()); |
| 554 VLOG(1) << device_path.value() << ": AuthorizeService: " << uuid; | 586 VLOG(1) << device_path.value() << ": AuthorizeService: " << uuid; |
| 555 | 587 |
| 556 BluetoothDeviceChromeOS* device_chromeos = GetDeviceWithPath(device_path); | 588 BluetoothDeviceChromeOS* device_chromeos = GetDeviceWithPath(device_path); |
| 557 if (!device_chromeos) { | 589 if (!device_chromeos) { |
| 558 callback.Run(CANCELLED); | 590 callback.Run(CANCELLED); |
| 559 return; | 591 return; |
| 560 } | 592 } |
| 561 | 593 |
| 562 // We always set paired devices to Trusted, so the only reason that this | 594 // We always set paired devices to Trusted, so the only reason that this |
| 563 // method call would ever be called is in the case of a race condition where | 595 // method call would ever be called is in the case of a race condition where |
| 564 // our "Set('Trusted', true)" method call is still pending in the Bluetooth | 596 // our "Set('Trusted', true)" method call is still pending in the Bluetooth |
| 565 // daemon because it's busy handling the incoming connection. | 597 // daemon because it's busy handling the incoming connection. |
| 566 if (device_chromeos->IsPaired()) { | 598 if (device_chromeos->IsPaired()) { |
| 567 callback.Run(SUCCESS); | 599 callback.Run(SUCCESS); |
| 568 return; | 600 return; |
| 569 } | 601 } |
| 570 | 602 |
| 571 // TODO(keybuk): reject service authorizations when not paired, determine | 603 // TODO(keybuk): reject service authorizations when not paired, determine |
| 572 // whether this is acceptable long-term. | 604 // whether this is acceptable long-term. |
| 573 LOG(WARNING) << "Rejecting service connection from unpaired device " | 605 LOG(WARNING) << "Rejecting service connection from unpaired device " |
| 574 << device_chromeos->GetAddress() << " for UUID " << uuid; | 606 << device_chromeos->GetAddress() << " for UUID " << uuid; |
| 575 callback.Run(REJECTED); | 607 callback.Run(REJECTED); |
| 576 } | 608 } |
| 577 | 609 |
| 578 void BluetoothAdapterChromeOS::Cancel() { | 610 void BluetoothAdapterChromeOS::Cancel() { |
| 611 DCHECK(IsPresent()); |
| 579 DCHECK(agent_.get()); | 612 DCHECK(agent_.get()); |
| 580 VLOG(1) << "Cancel"; | 613 VLOG(1) << "Cancel"; |
| 581 } | 614 } |
| 582 | 615 |
| 583 void BluetoothAdapterChromeOS::OnRegisterAgent() { | 616 void BluetoothAdapterChromeOS::OnRegisterAgent() { |
| 617 DCHECK(IsPresent()); |
| 584 VLOG(1) << "Pairing agent registered, requesting to be made default"; | 618 VLOG(1) << "Pairing agent registered, requesting to be made default"; |
| 585 | 619 |
| 586 DBusThreadManager::Get()->GetBluetoothAgentManagerClient()-> | 620 DBusThreadManager::Get()->GetBluetoothAgentManagerClient()-> |
| 587 RequestDefaultAgent( | 621 RequestDefaultAgent( |
| 588 dbus::ObjectPath(kAgentPath), | 622 dbus::ObjectPath(kAgentPath), |
| 589 base::Bind(&BluetoothAdapterChromeOS::OnRequestDefaultAgent, | 623 base::Bind(&BluetoothAdapterChromeOS::OnRequestDefaultAgent, |
| 590 weak_ptr_factory_.GetWeakPtr()), | 624 weak_ptr_factory_.GetWeakPtr()), |
| 591 base::Bind(&BluetoothAdapterChromeOS::OnRequestDefaultAgentError, | 625 base::Bind(&BluetoothAdapterChromeOS::OnRequestDefaultAgentError, |
| 592 weak_ptr_factory_.GetWeakPtr())); | 626 weak_ptr_factory_.GetWeakPtr())); |
| 593 } | 627 } |
| 594 | 628 |
| 595 void BluetoothAdapterChromeOS::OnRegisterAgentError( | 629 void BluetoothAdapterChromeOS::OnRegisterAgentError( |
| 596 const std::string& error_name, | 630 const std::string& error_name, |
| 597 const std::string& error_message) { | 631 const std::string& error_message) { |
| 632 DCHECK(IsPresent()); |
| 598 // Our agent being already registered isn't an error. | 633 // Our agent being already registered isn't an error. |
| 599 if (error_name == bluetooth_agent_manager::kErrorAlreadyExists) | 634 if (error_name == bluetooth_agent_manager::kErrorAlreadyExists) |
| 600 return; | 635 return; |
| 601 | 636 |
| 602 LOG(WARNING) << ": Failed to register pairing agent: " | 637 LOG(WARNING) << ": Failed to register pairing agent: " |
| 603 << error_name << ": " << error_message; | 638 << error_name << ": " << error_message; |
| 604 } | 639 } |
| 605 | 640 |
| 606 void BluetoothAdapterChromeOS::OnRequestDefaultAgent() { | 641 void BluetoothAdapterChromeOS::OnRequestDefaultAgent() { |
| 642 DCHECK(IsPresent()); |
| 607 VLOG(1) << "Pairing agent now default"; | 643 VLOG(1) << "Pairing agent now default"; |
| 608 } | 644 } |
| 609 | 645 |
| 610 void BluetoothAdapterChromeOS::OnRequestDefaultAgentError( | 646 void BluetoothAdapterChromeOS::OnRequestDefaultAgentError( |
| 611 const std::string& error_name, | 647 const std::string& error_name, |
| 612 const std::string& error_message) { | 648 const std::string& error_message) { |
| 649 DCHECK(IsPresent()); |
| 613 LOG(WARNING) << ": Failed to make pairing agent default: " | 650 LOG(WARNING) << ": Failed to make pairing agent default: " |
| 614 << error_name << ": " << error_message; | 651 << error_name << ": " << error_message; |
| 615 } | 652 } |
| 616 | 653 |
| 617 BluetoothDeviceChromeOS* | 654 BluetoothDeviceChromeOS* |
| 618 BluetoothAdapterChromeOS::GetDeviceWithPath( | 655 BluetoothAdapterChromeOS::GetDeviceWithPath( |
| 619 const dbus::ObjectPath& object_path) { | 656 const dbus::ObjectPath& object_path) { |
| 620 for (DevicesMap::iterator iter = devices_.begin(); | 657 if (!IsPresent()) |
| 621 iter != devices_.end(); ++iter) { | 658 return NULL; |
| 659 |
| 660 for (DevicesMap::iterator iter = devices_.begin(); iter != devices_.end(); |
| 661 ++iter) { |
| 622 BluetoothDeviceChromeOS* device_chromeos = | 662 BluetoothDeviceChromeOS* device_chromeos = |
| 623 static_cast<BluetoothDeviceChromeOS*>(iter->second); | 663 static_cast<BluetoothDeviceChromeOS*>(iter->second); |
| 624 if (device_chromeos->object_path() == object_path) | 664 if (device_chromeos->object_path() == object_path) |
| 625 return device_chromeos; | 665 return device_chromeos; |
| 626 } | 666 } |
| 627 | 667 |
| 628 return NULL; | 668 return NULL; |
| 629 } | 669 } |
| 630 | 670 |
| 631 BluetoothPairingChromeOS* BluetoothAdapterChromeOS::GetPairing( | 671 BluetoothPairingChromeOS* BluetoothAdapterChromeOS::GetPairing( |
| 632 const dbus::ObjectPath& object_path) { | 672 const dbus::ObjectPath& object_path) { |
| 673 DCHECK(IsPresent()); |
| 633 BluetoothDeviceChromeOS* device_chromeos = GetDeviceWithPath(object_path); | 674 BluetoothDeviceChromeOS* device_chromeos = GetDeviceWithPath(object_path); |
| 634 if (!device_chromeos) { | 675 if (!device_chromeos) { |
| 635 LOG(WARNING) << "Pairing Agent request for unknown device: " | 676 LOG(WARNING) << "Pairing Agent request for unknown device: " |
| 636 << object_path.value(); | 677 << object_path.value(); |
| 637 return NULL; | 678 return NULL; |
| 638 } | 679 } |
| 639 | 680 |
| 640 BluetoothPairingChromeOS* pairing = device_chromeos->GetPairing(); | 681 BluetoothPairingChromeOS* pairing = device_chromeos->GetPairing(); |
| 641 if (pairing) | 682 if (pairing) |
| 642 return pairing; | 683 return pairing; |
| 643 | 684 |
| 644 // The device doesn't have its own pairing context, so this is an incoming | 685 // The device doesn't have its own pairing context, so this is an incoming |
| 645 // pairing request that should use our best default delegate (if we have one). | 686 // pairing request that should use our best default delegate (if we have one). |
| 646 BluetoothDevice::PairingDelegate* pairing_delegate = DefaultPairingDelegate(); | 687 BluetoothDevice::PairingDelegate* pairing_delegate = DefaultPairingDelegate(); |
| 647 if (!pairing_delegate) | 688 if (!pairing_delegate) |
| 648 return NULL; | 689 return NULL; |
| 649 | 690 |
| 650 return device_chromeos->BeginPairing(pairing_delegate); | 691 return device_chromeos->BeginPairing(pairing_delegate); |
| 651 } | 692 } |
| 652 | 693 |
| 653 void BluetoothAdapterChromeOS::SetAdapter(const dbus::ObjectPath& object_path) { | 694 void BluetoothAdapterChromeOS::SetAdapter(const dbus::ObjectPath& object_path) { |
| 654 DCHECK(!IsPresent()); | 695 DCHECK(!IsPresent()); |
| 696 DCHECK(!dbus_is_shutdown_); |
| 655 object_path_ = object_path; | 697 object_path_ = object_path; |
| 656 | 698 |
| 657 VLOG(1) << object_path_.value() << ": using adapter."; | 699 VLOG(1) << object_path_.value() << ": using adapter."; |
| 658 | 700 |
| 659 VLOG(1) << "Registering pairing agent"; | 701 VLOG(1) << "Registering pairing agent"; |
| 660 DBusThreadManager::Get()->GetBluetoothAgentManagerClient()-> | 702 DBusThreadManager::Get()->GetBluetoothAgentManagerClient()-> |
| 661 RegisterAgent( | 703 RegisterAgent( |
| 662 dbus::ObjectPath(kAgentPath), | 704 dbus::ObjectPath(kAgentPath), |
| 663 bluetooth_agent_manager::kKeyboardDisplayCapability, | 705 bluetooth_agent_manager::kKeyboardDisplayCapability, |
| 664 base::Bind(&BluetoothAdapterChromeOS::OnRegisterAgent, | 706 base::Bind(&BluetoothAdapterChromeOS::OnRegisterAgent, |
| (...skipping 20 matching lines...) Expand all Loading... |
| 685 DBusThreadManager::Get()->GetBluetoothDeviceClient()-> | 727 DBusThreadManager::Get()->GetBluetoothDeviceClient()-> |
| 686 GetDevicesForAdapter(object_path_); | 728 GetDevicesForAdapter(object_path_); |
| 687 | 729 |
| 688 for (std::vector<dbus::ObjectPath>::iterator iter = device_paths.begin(); | 730 for (std::vector<dbus::ObjectPath>::iterator iter = device_paths.begin(); |
| 689 iter != device_paths.end(); ++iter) { | 731 iter != device_paths.end(); ++iter) { |
| 690 DeviceAdded(*iter); | 732 DeviceAdded(*iter); |
| 691 } | 733 } |
| 692 } | 734 } |
| 693 | 735 |
| 694 void BluetoothAdapterChromeOS::SetDefaultAdapterName() { | 736 void BluetoothAdapterChromeOS::SetDefaultAdapterName() { |
| 737 DCHECK(IsPresent()); |
| 695 std::string board = base::SysInfo::GetLsbReleaseBoard(); | 738 std::string board = base::SysInfo::GetLsbReleaseBoard(); |
| 696 std::string alias; | 739 std::string alias; |
| 697 if (board.substr(0, 6) == "stumpy") { | 740 if (board.substr(0, 6) == "stumpy") { |
| 698 alias = "Chromebox"; | 741 alias = "Chromebox"; |
| 699 } else if (board.substr(0, 4) == "link") { | 742 } else if (board.substr(0, 4) == "link") { |
| 700 alias = "Chromebook Pixel"; | 743 alias = "Chromebook Pixel"; |
| 701 } else { | 744 } else { |
| 702 alias = "Chromebook"; | 745 alias = "Chromebook"; |
| 703 } | 746 } |
| 704 | 747 |
| (...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 883 | 926 |
| 884 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, | 927 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, |
| 885 observers_, | 928 observers_, |
| 886 GattDescriptorValueChanged(this, descriptor, value)); | 929 GattDescriptorValueChanged(this, descriptor, value)); |
| 887 } | 930 } |
| 888 | 931 |
| 889 void BluetoothAdapterChromeOS::OnSetDiscoverable( | 932 void BluetoothAdapterChromeOS::OnSetDiscoverable( |
| 890 const base::Closure& callback, | 933 const base::Closure& callback, |
| 891 const ErrorCallback& error_callback, | 934 const ErrorCallback& error_callback, |
| 892 bool success) { | 935 bool success) { |
| 936 DCHECK(IsPresent()); |
| 893 // Set the discoverable_timeout property to zero so the adapter remains | 937 // Set the discoverable_timeout property to zero so the adapter remains |
| 894 // discoverable forever. | 938 // discoverable forever. |
| 895 DBusThreadManager::Get()->GetBluetoothAdapterClient()-> | 939 DBusThreadManager::Get()->GetBluetoothAdapterClient()-> |
| 896 GetProperties(object_path_)->discoverable_timeout.Set( | 940 GetProperties(object_path_)->discoverable_timeout.Set( |
| 897 0, | 941 0, |
| 898 base::Bind(&BluetoothAdapterChromeOS::OnPropertyChangeCompleted, | 942 base::Bind(&BluetoothAdapterChromeOS::OnPropertyChangeCompleted, |
| 899 weak_ptr_factory_.GetWeakPtr(), | 943 weak_ptr_factory_.GetWeakPtr(), |
| 900 callback, | 944 callback, |
| 901 error_callback)); | 945 error_callback)); |
| 902 } | 946 } |
| 903 | 947 |
| 904 void BluetoothAdapterChromeOS::OnPropertyChangeCompleted( | 948 void BluetoothAdapterChromeOS::OnPropertyChangeCompleted( |
| 905 const base::Closure& callback, | 949 const base::Closure& callback, |
| 906 const ErrorCallback& error_callback, | 950 const ErrorCallback& error_callback, |
| 907 bool success) { | 951 bool success) { |
| 952 DCHECK(IsPresent()); |
| 908 if (success) | 953 if (success) |
| 909 callback.Run(); | 954 callback.Run(); |
| 910 else | 955 else |
| 911 error_callback.Run(); | 956 error_callback.Run(); |
| 912 } | 957 } |
| 913 | 958 |
| 914 void BluetoothAdapterChromeOS::AddDiscoverySession( | 959 void BluetoothAdapterChromeOS::AddDiscoverySession( |
| 915 const base::Closure& callback, | 960 const base::Closure& callback, |
| 916 const ErrorCallback& error_callback) { | 961 const ErrorCallback& error_callback) { |
| 962 DCHECK(IsPresent()); |
| 917 VLOG(1) << __func__; | 963 VLOG(1) << __func__; |
| 918 if (discovery_request_pending_) { | 964 if (discovery_request_pending_) { |
| 919 // The pending request is either to stop a previous session or to start a | 965 // The pending request is either to stop a previous session or to start a |
| 920 // new one. Either way, queue this one. | 966 // new one. Either way, queue this one. |
| 921 DCHECK(num_discovery_sessions_ == 1 || num_discovery_sessions_ == 0); | 967 DCHECK(num_discovery_sessions_ == 1 || num_discovery_sessions_ == 0); |
| 922 VLOG(1) << "Pending request to start/stop device discovery. Queueing " | 968 VLOG(1) << "Pending request to start/stop device discovery. Queueing " |
| 923 << "request to start a new discovery session."; | 969 << "request to start a new discovery session."; |
| 924 discovery_request_queue_.push(std::make_pair(callback, error_callback)); | 970 discovery_request_queue_.push(std::make_pair(callback, error_callback)); |
| 925 return; | 971 return; |
| 926 } | 972 } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 947 callback), | 993 callback), |
| 948 base::Bind(&BluetoothAdapterChromeOS::OnStartDiscoveryError, | 994 base::Bind(&BluetoothAdapterChromeOS::OnStartDiscoveryError, |
| 949 weak_ptr_factory_.GetWeakPtr(), | 995 weak_ptr_factory_.GetWeakPtr(), |
| 950 callback, | 996 callback, |
| 951 error_callback)); | 997 error_callback)); |
| 952 } | 998 } |
| 953 | 999 |
| 954 void BluetoothAdapterChromeOS::RemoveDiscoverySession( | 1000 void BluetoothAdapterChromeOS::RemoveDiscoverySession( |
| 955 const base::Closure& callback, | 1001 const base::Closure& callback, |
| 956 const ErrorCallback& error_callback) { | 1002 const ErrorCallback& error_callback) { |
| 1003 DCHECK(IsPresent()); |
| 957 VLOG(1) << __func__; | 1004 VLOG(1) << __func__; |
| 958 // There are active sessions other than the one currently being removed. | 1005 // There are active sessions other than the one currently being removed. |
| 959 if (num_discovery_sessions_ > 1) { | 1006 if (num_discovery_sessions_ > 1) { |
| 960 DCHECK(IsDiscovering()); | 1007 DCHECK(IsDiscovering()); |
| 961 DCHECK(!discovery_request_pending_); | 1008 DCHECK(!discovery_request_pending_); |
| 962 num_discovery_sessions_--; | 1009 num_discovery_sessions_--; |
| 963 callback.Run(); | 1010 callback.Run(); |
| 964 return; | 1011 return; |
| 965 } | 1012 } |
| 966 | 1013 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 991 object_path_, | 1038 object_path_, |
| 992 base::Bind(&BluetoothAdapterChromeOS::OnStopDiscovery, | 1039 base::Bind(&BluetoothAdapterChromeOS::OnStopDiscovery, |
| 993 weak_ptr_factory_.GetWeakPtr(), | 1040 weak_ptr_factory_.GetWeakPtr(), |
| 994 callback), | 1041 callback), |
| 995 base::Bind(&BluetoothAdapterChromeOS::OnStopDiscoveryError, | 1042 base::Bind(&BluetoothAdapterChromeOS::OnStopDiscoveryError, |
| 996 weak_ptr_factory_.GetWeakPtr(), | 1043 weak_ptr_factory_.GetWeakPtr(), |
| 997 error_callback)); | 1044 error_callback)); |
| 998 } | 1045 } |
| 999 | 1046 |
| 1000 void BluetoothAdapterChromeOS::OnStartDiscovery(const base::Closure& callback) { | 1047 void BluetoothAdapterChromeOS::OnStartDiscovery(const base::Closure& callback) { |
| 1048 DCHECK(IsPresent()); |
| 1001 // Report success on the original request and increment the count. | 1049 // Report success on the original request and increment the count. |
| 1002 VLOG(1) << __func__; | 1050 VLOG(1) << __func__; |
| 1003 DCHECK(discovery_request_pending_); | 1051 DCHECK(discovery_request_pending_); |
| 1004 DCHECK(num_discovery_sessions_ == 0); | 1052 DCHECK(num_discovery_sessions_ == 0); |
| 1005 discovery_request_pending_ = false; | 1053 discovery_request_pending_ = false; |
| 1006 num_discovery_sessions_++; | 1054 num_discovery_sessions_++; |
| 1007 callback.Run(); | 1055 callback.Run(); |
| 1008 | 1056 |
| 1009 // Try to add a new discovery session for each queued request. | 1057 // Try to add a new discovery session for each queued request. |
| 1010 ProcessQueuedDiscoveryRequests(); | 1058 ProcessQueuedDiscoveryRequests(); |
| 1011 } | 1059 } |
| 1012 | 1060 |
| 1013 void BluetoothAdapterChromeOS::OnStartDiscoveryError( | 1061 void BluetoothAdapterChromeOS::OnStartDiscoveryError( |
| 1014 const base::Closure& callback, | 1062 const base::Closure& callback, |
| 1015 const ErrorCallback& error_callback, | 1063 const ErrorCallback& error_callback, |
| 1016 const std::string& error_name, | 1064 const std::string& error_name, |
| 1017 const std::string& error_message) { | 1065 const std::string& error_message) { |
| 1066 DCHECK(IsPresent()); |
| 1018 LOG(WARNING) << object_path_.value() << ": Failed to start discovery: " | 1067 LOG(WARNING) << object_path_.value() << ": Failed to start discovery: " |
| 1019 << error_name << ": " << error_message; | 1068 << error_name << ": " << error_message; |
| 1020 | 1069 |
| 1021 // Failed to start discovery. This can only happen if the count is at 0. | 1070 // Failed to start discovery. This can only happen if the count is at 0. |
| 1022 DCHECK(num_discovery_sessions_ == 0); | 1071 DCHECK(num_discovery_sessions_ == 0); |
| 1023 DCHECK(discovery_request_pending_); | 1072 DCHECK(discovery_request_pending_); |
| 1024 discovery_request_pending_ = false; | 1073 discovery_request_pending_ = false; |
| 1025 | 1074 |
| 1026 // Discovery request may fail if discovery was previously initiated by Chrome, | 1075 // Discovery request may fail if discovery was previously initiated by Chrome, |
| 1027 // but the session were invalidated due to the discovery state unexpectedly | 1076 // but the session were invalidated due to the discovery state unexpectedly |
| 1028 // changing to false and then back to true. In this case, report success. | 1077 // changing to false and then back to true. In this case, report success. |
| 1029 if (error_name == bluetooth_device::kErrorInProgress && IsDiscovering()) { | 1078 if (error_name == bluetooth_device::kErrorInProgress && IsDiscovering()) { |
| 1030 VLOG(1) << "Discovery previously initiated. Reporting success."; | 1079 VLOG(1) << "Discovery previously initiated. Reporting success."; |
| 1031 num_discovery_sessions_++; | 1080 num_discovery_sessions_++; |
| 1032 callback.Run(); | 1081 callback.Run(); |
| 1033 } else { | 1082 } else { |
| 1034 error_callback.Run(); | 1083 error_callback.Run(); |
| 1035 } | 1084 } |
| 1036 | 1085 |
| 1037 // Try to add a new discovery session for each queued request. | 1086 // Try to add a new discovery session for each queued request. |
| 1038 ProcessQueuedDiscoveryRequests(); | 1087 ProcessQueuedDiscoveryRequests(); |
| 1039 } | 1088 } |
| 1040 | 1089 |
| 1041 void BluetoothAdapterChromeOS::OnStopDiscovery(const base::Closure& callback) { | 1090 void BluetoothAdapterChromeOS::OnStopDiscovery(const base::Closure& callback) { |
| 1091 DCHECK(IsPresent()); |
| 1042 // Report success on the original request and decrement the count. | 1092 // Report success on the original request and decrement the count. |
| 1043 VLOG(1) << __func__; | 1093 VLOG(1) << __func__; |
| 1044 DCHECK(discovery_request_pending_); | 1094 DCHECK(discovery_request_pending_); |
| 1045 DCHECK(num_discovery_sessions_ == 1); | 1095 DCHECK(num_discovery_sessions_ == 1); |
| 1046 discovery_request_pending_ = false; | 1096 discovery_request_pending_ = false; |
| 1047 num_discovery_sessions_--; | 1097 num_discovery_sessions_--; |
| 1048 callback.Run(); | 1098 callback.Run(); |
| 1049 | 1099 |
| 1050 // Try to add a new discovery session for each queued request. | 1100 // Try to add a new discovery session for each queued request. |
| 1051 ProcessQueuedDiscoveryRequests(); | 1101 ProcessQueuedDiscoveryRequests(); |
| 1052 } | 1102 } |
| 1053 | 1103 |
| 1054 void BluetoothAdapterChromeOS::OnStopDiscoveryError( | 1104 void BluetoothAdapterChromeOS::OnStopDiscoveryError( |
| 1055 const ErrorCallback& error_callback, | 1105 const ErrorCallback& error_callback, |
| 1056 const std::string& error_name, | 1106 const std::string& error_name, |
| 1057 const std::string& error_message) { | 1107 const std::string& error_message) { |
| 1108 DCHECK(IsPresent()); |
| 1058 LOG(WARNING) << object_path_.value() << ": Failed to stop discovery: " | 1109 LOG(WARNING) << object_path_.value() << ": Failed to stop discovery: " |
| 1059 << error_name << ": " << error_message; | 1110 << error_name << ": " << error_message; |
| 1060 | 1111 |
| 1061 // Failed to stop discovery. This can only happen if the count is at 1. | 1112 // Failed to stop discovery. This can only happen if the count is at 1. |
| 1062 DCHECK(discovery_request_pending_); | 1113 DCHECK(discovery_request_pending_); |
| 1063 DCHECK(num_discovery_sessions_ == 1); | 1114 DCHECK(num_discovery_sessions_ == 1); |
| 1064 discovery_request_pending_ = false; | 1115 discovery_request_pending_ = false; |
| 1065 error_callback.Run(); | 1116 error_callback.Run(); |
| 1066 | 1117 |
| 1067 // Try to add a new discovery session for each queued request. | 1118 // Try to add a new discovery session for each queued request. |
| 1068 ProcessQueuedDiscoveryRequests(); | 1119 ProcessQueuedDiscoveryRequests(); |
| 1069 } | 1120 } |
| 1070 | 1121 |
| 1071 void BluetoothAdapterChromeOS::ProcessQueuedDiscoveryRequests() { | 1122 void BluetoothAdapterChromeOS::ProcessQueuedDiscoveryRequests() { |
| 1072 while (!discovery_request_queue_.empty()) { | 1123 while (!discovery_request_queue_.empty()) { |
| 1073 VLOG(1) << "Process queued discovery request."; | 1124 VLOG(1) << "Process queued discovery request."; |
| 1074 DiscoveryCallbackPair callbacks = discovery_request_queue_.front(); | 1125 DiscoveryCallbackPair callbacks = discovery_request_queue_.front(); |
| 1075 discovery_request_queue_.pop(); | 1126 discovery_request_queue_.pop(); |
| 1076 AddDiscoverySession(callbacks.first, callbacks.second); | 1127 AddDiscoverySession(callbacks.first, callbacks.second); |
| 1077 | 1128 |
| 1078 // If the queued request resulted in a pending call, then let it | 1129 // If the queued request resulted in a pending call, then let it |
| 1079 // asynchonously process the remaining queued requests once the pending | 1130 // asynchonously process the remaining queued requests once the pending |
| 1080 // call returns. | 1131 // call returns. |
| 1081 if (discovery_request_pending_) | 1132 if (discovery_request_pending_) |
| 1082 return; | 1133 return; |
| 1083 } | 1134 } |
| 1084 } | 1135 } |
| 1085 | 1136 |
| 1086 } // namespace chromeos | 1137 } // namespace chromeos |
| OLD | NEW |