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::RemovePairingDelegateInternal( | 304 void BluetoothAdapterChromeOS::RemovePairingDelegateInternal( |
287 BluetoothDevice::PairingDelegate* pairing_delegate) { | 305 BluetoothDevice::PairingDelegate* pairing_delegate) { |
| 306 DCHECK(IsPresent()); |
288 // Before removing a pairing delegate make sure that there aren't any devices | 307 // Before removing a pairing delegate make sure that there aren't any devices |
289 // currently using it; if there are, clear the pairing context which will | 308 // currently using it; if there are, clear the pairing context which will |
290 // make any responses no-ops. | 309 // make any responses no-ops. |
291 for (DevicesMap::iterator iter = devices_.begin(); | 310 for (DevicesMap::iterator iter = devices_.begin(); |
292 iter != devices_.end(); ++iter) { | 311 iter != devices_.end(); ++iter) { |
293 BluetoothDeviceChromeOS* device_chromeos = | 312 BluetoothDeviceChromeOS* device_chromeos = |
294 static_cast<BluetoothDeviceChromeOS*>(iter->second); | 313 static_cast<BluetoothDeviceChromeOS*>(iter->second); |
295 | 314 |
296 BluetoothPairingChromeOS* pairing = device_chromeos->GetPairing(); | 315 BluetoothPairingChromeOS* pairing = device_chromeos->GetPairing(); |
297 if (pairing && pairing->GetPairingDelegate() == pairing_delegate) | 316 if (pairing && pairing->GetPairingDelegate() == pairing_delegate) |
(...skipping 10 matching lines...) Expand all Loading... |
308 | 327 |
309 void BluetoothAdapterChromeOS::AdapterRemoved( | 328 void BluetoothAdapterChromeOS::AdapterRemoved( |
310 const dbus::ObjectPath& object_path) { | 329 const dbus::ObjectPath& object_path) { |
311 if (object_path == object_path_) | 330 if (object_path == object_path_) |
312 RemoveAdapter(); | 331 RemoveAdapter(); |
313 } | 332 } |
314 | 333 |
315 void BluetoothAdapterChromeOS::AdapterPropertyChanged( | 334 void BluetoothAdapterChromeOS::AdapterPropertyChanged( |
316 const dbus::ObjectPath& object_path, | 335 const dbus::ObjectPath& object_path, |
317 const std::string& property_name) { | 336 const std::string& property_name) { |
| 337 DCHECK(IsPresent()); |
318 if (object_path != object_path_) | 338 if (object_path != object_path_) |
319 return; | 339 return; |
320 | 340 |
321 BluetoothAdapterClient::Properties* properties = | 341 BluetoothAdapterClient::Properties* properties = |
322 DBusThreadManager::Get()->GetBluetoothAdapterClient()-> | 342 DBusThreadManager::Get()->GetBluetoothAdapterClient()-> |
323 GetProperties(object_path_); | 343 GetProperties(object_path_); |
324 | 344 |
325 if (property_name == properties->powered.name()) | 345 if (property_name == properties->powered.name()) |
326 PoweredChanged(properties->powered.value()); | 346 PoweredChanged(properties->powered.value()); |
327 else if (property_name == properties->discoverable.name()) | 347 else if (property_name == properties->discoverable.name()) |
328 DiscoverableChanged(properties->discoverable.value()); | 348 DiscoverableChanged(properties->discoverable.value()); |
329 else if (property_name == properties->discovering.name()) | 349 else if (property_name == properties->discovering.name()) |
330 DiscoveringChanged(properties->discovering.value()); | 350 DiscoveringChanged(properties->discovering.value()); |
331 } | 351 } |
332 | 352 |
333 void BluetoothAdapterChromeOS::DeviceAdded( | 353 void BluetoothAdapterChromeOS::DeviceAdded( |
334 const dbus::ObjectPath& object_path) { | 354 const dbus::ObjectPath& object_path) { |
| 355 DCHECK(IsPresent()); |
335 BluetoothDeviceClient::Properties* properties = | 356 BluetoothDeviceClient::Properties* properties = |
336 DBusThreadManager::Get()->GetBluetoothDeviceClient()-> | 357 DBusThreadManager::Get()->GetBluetoothDeviceClient()-> |
337 GetProperties(object_path); | 358 GetProperties(object_path); |
338 if (properties->adapter.value() != object_path_) | 359 if (properties->adapter.value() != object_path_) |
339 return; | 360 return; |
340 | 361 |
341 BluetoothDeviceChromeOS* device_chromeos = | 362 BluetoothDeviceChromeOS* device_chromeos = |
342 new BluetoothDeviceChromeOS(this, | 363 new BluetoothDeviceChromeOS(this, |
343 object_path, | 364 object_path, |
344 ui_task_runner_, | 365 ui_task_runner_, |
345 socket_thread_); | 366 socket_thread_); |
346 DCHECK(devices_.find(device_chromeos->GetAddress()) == devices_.end()); | 367 DCHECK(devices_.find(device_chromeos->GetAddress()) == devices_.end()); |
347 | 368 |
348 devices_[device_chromeos->GetAddress()] = device_chromeos; | 369 devices_[device_chromeos->GetAddress()] = device_chromeos; |
349 | 370 |
350 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | 371 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, |
351 DeviceAdded(this, device_chromeos)); | 372 DeviceAdded(this, device_chromeos)); |
352 } | 373 } |
353 | 374 |
354 void BluetoothAdapterChromeOS::DeviceRemoved( | 375 void BluetoothAdapterChromeOS::DeviceRemoved( |
355 const dbus::ObjectPath& object_path) { | 376 const dbus::ObjectPath& object_path) { |
| 377 DCHECK(IsPresent()); |
356 for (DevicesMap::iterator iter = devices_.begin(); | 378 for (DevicesMap::iterator iter = devices_.begin(); |
357 iter != devices_.end(); ++iter) { | 379 iter != devices_.end(); ++iter) { |
358 BluetoothDeviceChromeOS* device_chromeos = | 380 BluetoothDeviceChromeOS* device_chromeos = |
359 static_cast<BluetoothDeviceChromeOS*>(iter->second); | 381 static_cast<BluetoothDeviceChromeOS*>(iter->second); |
360 if (device_chromeos->object_path() == object_path) { | 382 if (device_chromeos->object_path() == object_path) { |
361 devices_.erase(iter); | 383 devices_.erase(iter); |
362 | 384 |
363 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | 385 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, |
364 DeviceRemoved(this, device_chromeos)); | 386 DeviceRemoved(this, device_chromeos)); |
365 delete device_chromeos; | 387 delete device_chromeos; |
366 return; | 388 return; |
367 } | 389 } |
368 } | 390 } |
369 } | 391 } |
370 | 392 |
371 void BluetoothAdapterChromeOS::DevicePropertyChanged( | 393 void BluetoothAdapterChromeOS::DevicePropertyChanged( |
372 const dbus::ObjectPath& object_path, | 394 const dbus::ObjectPath& object_path, |
373 const std::string& property_name) { | 395 const std::string& property_name) { |
| 396 DCHECK(IsPresent()); |
374 BluetoothDeviceChromeOS* device_chromeos = GetDeviceWithPath(object_path); | 397 BluetoothDeviceChromeOS* device_chromeos = GetDeviceWithPath(object_path); |
375 if (!device_chromeos) | 398 if (!device_chromeos) |
376 return; | 399 return; |
377 | 400 |
378 BluetoothDeviceClient::Properties* properties = | 401 BluetoothDeviceClient::Properties* properties = |
379 DBusThreadManager::Get()->GetBluetoothDeviceClient()-> | 402 DBusThreadManager::Get()->GetBluetoothDeviceClient()-> |
380 GetProperties(object_path); | 403 GetProperties(object_path); |
381 | 404 |
382 if (property_name == properties->bluetooth_class.name() || | 405 if (property_name == properties->bluetooth_class.name() || |
383 property_name == properties->address.name() || | 406 property_name == properties->address.name() || |
(...skipping 28 matching lines...) Expand all Loading... |
412 ++count; | 435 ++count; |
413 } | 436 } |
414 | 437 |
415 UMA_HISTOGRAM_COUNTS_100("Bluetooth.ConnectedDeviceCount", count); | 438 UMA_HISTOGRAM_COUNTS_100("Bluetooth.ConnectedDeviceCount", count); |
416 } | 439 } |
417 } | 440 } |
418 | 441 |
419 void BluetoothAdapterChromeOS::InputPropertyChanged( | 442 void BluetoothAdapterChromeOS::InputPropertyChanged( |
420 const dbus::ObjectPath& object_path, | 443 const dbus::ObjectPath& object_path, |
421 const std::string& property_name) { | 444 const std::string& property_name) { |
| 445 DCHECK(IsPresent()); |
422 BluetoothDeviceChromeOS* device_chromeos = GetDeviceWithPath(object_path); | 446 BluetoothDeviceChromeOS* device_chromeos = GetDeviceWithPath(object_path); |
423 if (!device_chromeos) | 447 if (!device_chromeos) |
424 return; | 448 return; |
425 | 449 |
426 BluetoothInputClient::Properties* properties = | 450 BluetoothInputClient::Properties* properties = |
427 DBusThreadManager::Get()->GetBluetoothInputClient()-> | 451 DBusThreadManager::Get()->GetBluetoothInputClient()-> |
428 GetProperties(object_path); | 452 GetProperties(object_path); |
429 | 453 |
430 // Properties structure can be removed, which triggers a change in the | 454 // Properties structure can be removed, which triggers a change in the |
431 // BluetoothDevice::IsConnectable() property, as does a change in the | 455 // BluetoothDevice::IsConnectable() property, as does a change in the |
432 // actual reconnect_mode property. | 456 // actual reconnect_mode property. |
433 if (!properties || | 457 if (!properties || |
434 property_name == properties->reconnect_mode.name()) | 458 property_name == properties->reconnect_mode.name()) |
435 NotifyDeviceChanged(device_chromeos); | 459 NotifyDeviceChanged(device_chromeos); |
436 } | 460 } |
437 | 461 |
438 void BluetoothAdapterChromeOS::Released() { | 462 void BluetoothAdapterChromeOS::Released() { |
| 463 DCHECK(IsPresent()); |
439 DCHECK(agent_.get()); | 464 DCHECK(agent_.get()); |
440 VLOG(1) << "Release"; | 465 VLOG(1) << "Release"; |
441 | 466 |
442 // Called after we unregister the pairing agent, e.g. when changing I/O | 467 // Called after we unregister the pairing agent, e.g. when changing I/O |
443 // capabilities. Nothing much to be done right now. | 468 // capabilities. Nothing much to be done right now. |
444 } | 469 } |
445 | 470 |
446 void BluetoothAdapterChromeOS::RequestPinCode( | 471 void BluetoothAdapterChromeOS::RequestPinCode( |
447 const dbus::ObjectPath& device_path, | 472 const dbus::ObjectPath& device_path, |
448 const PinCodeCallback& callback) { | 473 const PinCodeCallback& callback) { |
| 474 DCHECK(IsPresent()); |
449 DCHECK(agent_.get()); | 475 DCHECK(agent_.get()); |
450 VLOG(1) << device_path.value() << ": RequestPinCode"; | 476 VLOG(1) << device_path.value() << ": RequestPinCode"; |
451 | 477 |
452 BluetoothPairingChromeOS* pairing = GetPairing(device_path); | 478 BluetoothPairingChromeOS* pairing = GetPairing(device_path); |
453 if (!pairing) { | 479 if (!pairing) { |
454 callback.Run(REJECTED, ""); | 480 callback.Run(REJECTED, ""); |
455 return; | 481 return; |
456 } | 482 } |
457 | 483 |
458 pairing->RequestPinCode(callback); | 484 pairing->RequestPinCode(callback); |
459 } | 485 } |
460 | 486 |
461 void BluetoothAdapterChromeOS::DisplayPinCode( | 487 void BluetoothAdapterChromeOS::DisplayPinCode( |
462 const dbus::ObjectPath& device_path, | 488 const dbus::ObjectPath& device_path, |
463 const std::string& pincode) { | 489 const std::string& pincode) { |
| 490 DCHECK(IsPresent()); |
464 DCHECK(agent_.get()); | 491 DCHECK(agent_.get()); |
465 VLOG(1) << device_path.value() << ": DisplayPinCode: " << pincode; | 492 VLOG(1) << device_path.value() << ": DisplayPinCode: " << pincode; |
466 | 493 |
467 BluetoothPairingChromeOS* pairing = GetPairing(device_path); | 494 BluetoothPairingChromeOS* pairing = GetPairing(device_path); |
468 if (!pairing) | 495 if (!pairing) |
469 return; | 496 return; |
470 | 497 |
471 pairing->DisplayPinCode(pincode); | 498 pairing->DisplayPinCode(pincode); |
472 } | 499 } |
473 | 500 |
474 void BluetoothAdapterChromeOS::RequestPasskey( | 501 void BluetoothAdapterChromeOS::RequestPasskey( |
475 const dbus::ObjectPath& device_path, | 502 const dbus::ObjectPath& device_path, |
476 const PasskeyCallback& callback) { | 503 const PasskeyCallback& callback) { |
| 504 DCHECK(IsPresent()); |
477 DCHECK(agent_.get()); | 505 DCHECK(agent_.get()); |
478 VLOG(1) << device_path.value() << ": RequestPasskey"; | 506 VLOG(1) << device_path.value() << ": RequestPasskey"; |
479 | 507 |
480 BluetoothPairingChromeOS* pairing = GetPairing(device_path); | 508 BluetoothPairingChromeOS* pairing = GetPairing(device_path); |
481 if (!pairing) { | 509 if (!pairing) { |
482 callback.Run(REJECTED, 0); | 510 callback.Run(REJECTED, 0); |
483 return; | 511 return; |
484 } | 512 } |
485 | 513 |
486 pairing->RequestPasskey(callback); | 514 pairing->RequestPasskey(callback); |
487 } | 515 } |
488 | 516 |
489 void BluetoothAdapterChromeOS::DisplayPasskey( | 517 void BluetoothAdapterChromeOS::DisplayPasskey( |
490 const dbus::ObjectPath& device_path, | 518 const dbus::ObjectPath& device_path, |
491 uint32 passkey, | 519 uint32 passkey, |
492 uint16 entered) { | 520 uint16 entered) { |
| 521 DCHECK(IsPresent()); |
493 DCHECK(agent_.get()); | 522 DCHECK(agent_.get()); |
494 VLOG(1) << device_path.value() << ": DisplayPasskey: " << passkey | 523 VLOG(1) << device_path.value() << ": DisplayPasskey: " << passkey |
495 << " (" << entered << " entered)"; | 524 << " (" << entered << " entered)"; |
496 | 525 |
497 BluetoothPairingChromeOS* pairing = GetPairing(device_path); | 526 BluetoothPairingChromeOS* pairing = GetPairing(device_path); |
498 if (!pairing) | 527 if (!pairing) |
499 return; | 528 return; |
500 | 529 |
501 if (entered == 0) | 530 if (entered == 0) |
502 pairing->DisplayPasskey(passkey); | 531 pairing->DisplayPasskey(passkey); |
503 | 532 |
504 pairing->KeysEntered(entered); | 533 pairing->KeysEntered(entered); |
505 } | 534 } |
506 | 535 |
507 void BluetoothAdapterChromeOS::RequestConfirmation( | 536 void BluetoothAdapterChromeOS::RequestConfirmation( |
508 const dbus::ObjectPath& device_path, | 537 const dbus::ObjectPath& device_path, |
509 uint32 passkey, | 538 uint32 passkey, |
510 const ConfirmationCallback& callback) { | 539 const ConfirmationCallback& callback) { |
| 540 DCHECK(IsPresent()); |
511 DCHECK(agent_.get()); | 541 DCHECK(agent_.get()); |
512 VLOG(1) << device_path.value() << ": RequestConfirmation: " << passkey; | 542 VLOG(1) << device_path.value() << ": RequestConfirmation: " << passkey; |
513 | 543 |
514 BluetoothPairingChromeOS* pairing = GetPairing(device_path); | 544 BluetoothPairingChromeOS* pairing = GetPairing(device_path); |
515 if (!pairing) { | 545 if (!pairing) { |
516 callback.Run(REJECTED); | 546 callback.Run(REJECTED); |
517 return; | 547 return; |
518 } | 548 } |
519 | 549 |
520 pairing->RequestConfirmation(passkey, callback); | 550 pairing->RequestConfirmation(passkey, callback); |
521 } | 551 } |
522 | 552 |
523 void BluetoothAdapterChromeOS::RequestAuthorization( | 553 void BluetoothAdapterChromeOS::RequestAuthorization( |
524 const dbus::ObjectPath& device_path, | 554 const dbus::ObjectPath& device_path, |
525 const ConfirmationCallback& callback) { | 555 const ConfirmationCallback& callback) { |
| 556 DCHECK(IsPresent()); |
526 DCHECK(agent_.get()); | 557 DCHECK(agent_.get()); |
527 VLOG(1) << device_path.value() << ": RequestAuthorization"; | 558 VLOG(1) << device_path.value() << ": RequestAuthorization"; |
528 | 559 |
529 BluetoothPairingChromeOS* pairing = GetPairing(device_path); | 560 BluetoothPairingChromeOS* pairing = GetPairing(device_path); |
530 if (!pairing) { | 561 if (!pairing) { |
531 callback.Run(REJECTED); | 562 callback.Run(REJECTED); |
532 return; | 563 return; |
533 } | 564 } |
534 | 565 |
535 pairing->RequestAuthorization(callback); | 566 pairing->RequestAuthorization(callback); |
536 } | 567 } |
537 | 568 |
538 void BluetoothAdapterChromeOS::AuthorizeService( | 569 void BluetoothAdapterChromeOS::AuthorizeService( |
539 const dbus::ObjectPath& device_path, | 570 const dbus::ObjectPath& device_path, |
540 const std::string& uuid, | 571 const std::string& uuid, |
541 const ConfirmationCallback& callback) { | 572 const ConfirmationCallback& callback) { |
| 573 DCHECK(IsPresent()); |
542 DCHECK(agent_.get()); | 574 DCHECK(agent_.get()); |
543 VLOG(1) << device_path.value() << ": AuthorizeService: " << uuid; | 575 VLOG(1) << device_path.value() << ": AuthorizeService: " << uuid; |
544 | 576 |
545 BluetoothDeviceChromeOS* device_chromeos = GetDeviceWithPath(device_path); | 577 BluetoothDeviceChromeOS* device_chromeos = GetDeviceWithPath(device_path); |
546 if (!device_chromeos) { | 578 if (!device_chromeos) { |
547 callback.Run(CANCELLED); | 579 callback.Run(CANCELLED); |
548 return; | 580 return; |
549 } | 581 } |
550 | 582 |
551 // We always set paired devices to Trusted, so the only reason that this | 583 // We always set paired devices to Trusted, so the only reason that this |
552 // method call would ever be called is in the case of a race condition where | 584 // method call would ever be called is in the case of a race condition where |
553 // our "Set('Trusted', true)" method call is still pending in the Bluetooth | 585 // our "Set('Trusted', true)" method call is still pending in the Bluetooth |
554 // daemon because it's busy handling the incoming connection. | 586 // daemon because it's busy handling the incoming connection. |
555 if (device_chromeos->IsPaired()) { | 587 if (device_chromeos->IsPaired()) { |
556 callback.Run(SUCCESS); | 588 callback.Run(SUCCESS); |
557 return; | 589 return; |
558 } | 590 } |
559 | 591 |
560 // TODO(keybuk): reject service authorizations when not paired, determine | 592 // TODO(keybuk): reject service authorizations when not paired, determine |
561 // whether this is acceptable long-term. | 593 // whether this is acceptable long-term. |
562 LOG(WARNING) << "Rejecting service connection from unpaired device " | 594 LOG(WARNING) << "Rejecting service connection from unpaired device " |
563 << device_chromeos->GetAddress() << " for UUID " << uuid; | 595 << device_chromeos->GetAddress() << " for UUID " << uuid; |
564 callback.Run(REJECTED); | 596 callback.Run(REJECTED); |
565 } | 597 } |
566 | 598 |
567 void BluetoothAdapterChromeOS::Cancel() { | 599 void BluetoothAdapterChromeOS::Cancel() { |
| 600 DCHECK(IsPresent()); |
568 DCHECK(agent_.get()); | 601 DCHECK(agent_.get()); |
569 VLOG(1) << "Cancel"; | 602 VLOG(1) << "Cancel"; |
570 } | 603 } |
571 | 604 |
572 void BluetoothAdapterChromeOS::OnRegisterAgent() { | 605 void BluetoothAdapterChromeOS::OnRegisterAgent() { |
| 606 DCHECK(IsPresent()); |
573 VLOG(1) << "Pairing agent registered, requesting to be made default"; | 607 VLOG(1) << "Pairing agent registered, requesting to be made default"; |
574 | 608 |
575 DBusThreadManager::Get()->GetBluetoothAgentManagerClient()-> | 609 DBusThreadManager::Get()->GetBluetoothAgentManagerClient()-> |
576 RequestDefaultAgent( | 610 RequestDefaultAgent( |
577 dbus::ObjectPath(kAgentPath), | 611 dbus::ObjectPath(kAgentPath), |
578 base::Bind(&BluetoothAdapterChromeOS::OnRequestDefaultAgent, | 612 base::Bind(&BluetoothAdapterChromeOS::OnRequestDefaultAgent, |
579 weak_ptr_factory_.GetWeakPtr()), | 613 weak_ptr_factory_.GetWeakPtr()), |
580 base::Bind(&BluetoothAdapterChromeOS::OnRequestDefaultAgentError, | 614 base::Bind(&BluetoothAdapterChromeOS::OnRequestDefaultAgentError, |
581 weak_ptr_factory_.GetWeakPtr())); | 615 weak_ptr_factory_.GetWeakPtr())); |
582 | 616 |
583 } | 617 } |
584 | 618 |
585 void BluetoothAdapterChromeOS::OnRegisterAgentError( | 619 void BluetoothAdapterChromeOS::OnRegisterAgentError( |
586 const std::string& error_name, | 620 const std::string& error_name, |
587 const std::string& error_message) { | 621 const std::string& error_message) { |
| 622 DCHECK(IsPresent()); |
588 // Our agent being already registered isn't an error. | 623 // Our agent being already registered isn't an error. |
589 if (error_name == bluetooth_agent_manager::kErrorAlreadyExists) | 624 if (error_name == bluetooth_agent_manager::kErrorAlreadyExists) |
590 return; | 625 return; |
591 | 626 |
592 LOG(WARNING) << ": Failed to register pairing agent: " | 627 LOG(WARNING) << ": Failed to register pairing agent: " |
593 << error_name << ": " << error_message; | 628 << error_name << ": " << error_message; |
594 } | 629 } |
595 | 630 |
596 void BluetoothAdapterChromeOS::OnRequestDefaultAgent() { | 631 void BluetoothAdapterChromeOS::OnRequestDefaultAgent() { |
| 632 DCHECK(IsPresent()); |
597 VLOG(1) << "Pairing agent now default"; | 633 VLOG(1) << "Pairing agent now default"; |
598 } | 634 } |
599 | 635 |
600 void BluetoothAdapterChromeOS::OnRequestDefaultAgentError( | 636 void BluetoothAdapterChromeOS::OnRequestDefaultAgentError( |
601 const std::string& error_name, | 637 const std::string& error_name, |
602 const std::string& error_message) { | 638 const std::string& error_message) { |
| 639 DCHECK(IsPresent()); |
603 LOG(WARNING) << ": Failed to make pairing agent default: " | 640 LOG(WARNING) << ": Failed to make pairing agent default: " |
604 << error_name << ": " << error_message; | 641 << error_name << ": " << error_message; |
605 } | 642 } |
606 | 643 |
607 BluetoothDeviceChromeOS* | 644 BluetoothDeviceChromeOS* |
608 BluetoothAdapterChromeOS::GetDeviceWithPath( | 645 BluetoothAdapterChromeOS::GetDeviceWithPath( |
609 const dbus::ObjectPath& object_path) { | 646 const dbus::ObjectPath& object_path) { |
610 for (DevicesMap::iterator iter = devices_.begin(); | 647 if (!IsPresent()) |
611 iter != devices_.end(); ++iter) { | 648 return NULL; |
| 649 |
| 650 for (DevicesMap::iterator iter = devices_.begin(); iter != devices_.end(); |
| 651 ++iter) { |
612 BluetoothDeviceChromeOS* device_chromeos = | 652 BluetoothDeviceChromeOS* device_chromeos = |
613 static_cast<BluetoothDeviceChromeOS*>(iter->second); | 653 static_cast<BluetoothDeviceChromeOS*>(iter->second); |
614 if (device_chromeos->object_path() == object_path) | 654 if (device_chromeos->object_path() == object_path) |
615 return device_chromeos; | 655 return device_chromeos; |
616 } | 656 } |
617 | 657 |
618 return NULL; | 658 return NULL; |
619 } | 659 } |
620 | 660 |
621 BluetoothPairingChromeOS* BluetoothAdapterChromeOS::GetPairing( | 661 BluetoothPairingChromeOS* BluetoothAdapterChromeOS::GetPairing( |
622 const dbus::ObjectPath& object_path) | 662 const dbus::ObjectPath& object_path) |
623 { | 663 { |
| 664 DCHECK(IsPresent()); |
624 BluetoothDeviceChromeOS* device_chromeos = GetDeviceWithPath(object_path); | 665 BluetoothDeviceChromeOS* device_chromeos = GetDeviceWithPath(object_path); |
625 if (!device_chromeos) { | 666 if (!device_chromeos) { |
626 LOG(WARNING) << "Pairing Agent request for unknown device: " | 667 LOG(WARNING) << "Pairing Agent request for unknown device: " |
627 << object_path.value(); | 668 << object_path.value(); |
628 return NULL; | 669 return NULL; |
629 } | 670 } |
630 | 671 |
631 BluetoothPairingChromeOS* pairing = device_chromeos->GetPairing(); | 672 BluetoothPairingChromeOS* pairing = device_chromeos->GetPairing(); |
632 if (pairing) | 673 if (pairing) |
633 return pairing; | 674 return pairing; |
634 | 675 |
635 // The device doesn't have its own pairing context, so this is an incoming | 676 // The device doesn't have its own pairing context, so this is an incoming |
636 // pairing request that should use our best default delegate (if we have one). | 677 // pairing request that should use our best default delegate (if we have one). |
637 BluetoothDevice::PairingDelegate* pairing_delegate = DefaultPairingDelegate(); | 678 BluetoothDevice::PairingDelegate* pairing_delegate = DefaultPairingDelegate(); |
638 if (!pairing_delegate) | 679 if (!pairing_delegate) |
639 return NULL; | 680 return NULL; |
640 | 681 |
641 return device_chromeos->BeginPairing(pairing_delegate); | 682 return device_chromeos->BeginPairing(pairing_delegate); |
642 } | 683 } |
643 | 684 |
644 void BluetoothAdapterChromeOS::SetAdapter(const dbus::ObjectPath& object_path) { | 685 void BluetoothAdapterChromeOS::SetAdapter(const dbus::ObjectPath& object_path) { |
645 DCHECK(!IsPresent()); | 686 DCHECK(!IsPresent()); |
| 687 DCHECK(!dbus_is_shutdown_); |
646 object_path_ = object_path; | 688 object_path_ = object_path; |
647 | 689 |
648 VLOG(1) << object_path_.value() << ": using adapter."; | 690 VLOG(1) << object_path_.value() << ": using adapter."; |
649 | 691 |
650 VLOG(1) << "Registering pairing agent"; | 692 VLOG(1) << "Registering pairing agent"; |
651 DBusThreadManager::Get()->GetBluetoothAgentManagerClient()-> | 693 DBusThreadManager::Get()->GetBluetoothAgentManagerClient()-> |
652 RegisterAgent( | 694 RegisterAgent( |
653 dbus::ObjectPath(kAgentPath), | 695 dbus::ObjectPath(kAgentPath), |
654 bluetooth_agent_manager::kKeyboardDisplayCapability, | 696 bluetooth_agent_manager::kKeyboardDisplayCapability, |
655 base::Bind(&BluetoothAdapterChromeOS::OnRegisterAgent, | 697 base::Bind(&BluetoothAdapterChromeOS::OnRegisterAgent, |
(...skipping 20 matching lines...) Expand all Loading... |
676 DBusThreadManager::Get()->GetBluetoothDeviceClient()-> | 718 DBusThreadManager::Get()->GetBluetoothDeviceClient()-> |
677 GetDevicesForAdapter(object_path_); | 719 GetDevicesForAdapter(object_path_); |
678 | 720 |
679 for (std::vector<dbus::ObjectPath>::iterator iter = device_paths.begin(); | 721 for (std::vector<dbus::ObjectPath>::iterator iter = device_paths.begin(); |
680 iter != device_paths.end(); ++iter) { | 722 iter != device_paths.end(); ++iter) { |
681 DeviceAdded(*iter); | 723 DeviceAdded(*iter); |
682 } | 724 } |
683 } | 725 } |
684 | 726 |
685 void BluetoothAdapterChromeOS::SetDefaultAdapterName() { | 727 void BluetoothAdapterChromeOS::SetDefaultAdapterName() { |
| 728 DCHECK(IsPresent()); |
686 std::string board = base::SysInfo::GetLsbReleaseBoard(); | 729 std::string board = base::SysInfo::GetLsbReleaseBoard(); |
687 std::string alias; | 730 std::string alias; |
688 if (board.substr(0, 6) == "stumpy") { | 731 if (board.substr(0, 6) == "stumpy") { |
689 alias = "Chromebox"; | 732 alias = "Chromebox"; |
690 } else if (board.substr(0, 4) == "link") { | 733 } else if (board.substr(0, 4) == "link") { |
691 alias = "Chromebook Pixel"; | 734 alias = "Chromebook Pixel"; |
692 } else { | 735 } else { |
693 alias = "Chromebook"; | 736 alias = "Chromebook"; |
694 } | 737 } |
695 | 738 |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
874 | 917 |
875 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, | 918 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, |
876 observers_, | 919 observers_, |
877 GattDescriptorValueChanged(this, descriptor, value)); | 920 GattDescriptorValueChanged(this, descriptor, value)); |
878 } | 921 } |
879 | 922 |
880 void BluetoothAdapterChromeOS::OnSetDiscoverable( | 923 void BluetoothAdapterChromeOS::OnSetDiscoverable( |
881 const base::Closure& callback, | 924 const base::Closure& callback, |
882 const ErrorCallback& error_callback, | 925 const ErrorCallback& error_callback, |
883 bool success) { | 926 bool success) { |
| 927 DCHECK(IsPresent()); |
884 // Set the discoverable_timeout property to zero so the adapter remains | 928 // Set the discoverable_timeout property to zero so the adapter remains |
885 // discoverable forever. | 929 // discoverable forever. |
886 DBusThreadManager::Get()->GetBluetoothAdapterClient()-> | 930 DBusThreadManager::Get()->GetBluetoothAdapterClient()-> |
887 GetProperties(object_path_)->discoverable_timeout.Set( | 931 GetProperties(object_path_)->discoverable_timeout.Set( |
888 0, | 932 0, |
889 base::Bind(&BluetoothAdapterChromeOS::OnPropertyChangeCompleted, | 933 base::Bind(&BluetoothAdapterChromeOS::OnPropertyChangeCompleted, |
890 weak_ptr_factory_.GetWeakPtr(), | 934 weak_ptr_factory_.GetWeakPtr(), |
891 callback, | 935 callback, |
892 error_callback)); | 936 error_callback)); |
893 } | 937 } |
894 | 938 |
895 void BluetoothAdapterChromeOS::OnPropertyChangeCompleted( | 939 void BluetoothAdapterChromeOS::OnPropertyChangeCompleted( |
896 const base::Closure& callback, | 940 const base::Closure& callback, |
897 const ErrorCallback& error_callback, | 941 const ErrorCallback& error_callback, |
898 bool success) { | 942 bool success) { |
| 943 DCHECK(IsPresent()); |
899 if (success) | 944 if (success) |
900 callback.Run(); | 945 callback.Run(); |
901 else | 946 else |
902 error_callback.Run(); | 947 error_callback.Run(); |
903 } | 948 } |
904 | 949 |
905 void BluetoothAdapterChromeOS::AddDiscoverySession( | 950 void BluetoothAdapterChromeOS::AddDiscoverySession( |
906 const base::Closure& callback, | 951 const base::Closure& callback, |
907 const ErrorCallback& error_callback) { | 952 const ErrorCallback& error_callback) { |
| 953 DCHECK(IsPresent()); |
908 VLOG(1) << __func__; | 954 VLOG(1) << __func__; |
909 if (discovery_request_pending_) { | 955 if (discovery_request_pending_) { |
910 // The pending request is either to stop a previous session or to start a | 956 // The pending request is either to stop a previous session or to start a |
911 // new one. Either way, queue this one. | 957 // new one. Either way, queue this one. |
912 DCHECK(num_discovery_sessions_ == 1 || num_discovery_sessions_ == 0); | 958 DCHECK(num_discovery_sessions_ == 1 || num_discovery_sessions_ == 0); |
913 VLOG(1) << "Pending request to start/stop device discovery. Queueing " | 959 VLOG(1) << "Pending request to start/stop device discovery. Queueing " |
914 << "request to start a new discovery session."; | 960 << "request to start a new discovery session."; |
915 discovery_request_queue_.push(std::make_pair(callback, error_callback)); | 961 discovery_request_queue_.push(std::make_pair(callback, error_callback)); |
916 return; | 962 return; |
917 } | 963 } |
(...skipping 20 matching lines...) Expand all Loading... |
938 callback), | 984 callback), |
939 base::Bind(&BluetoothAdapterChromeOS::OnStartDiscoveryError, | 985 base::Bind(&BluetoothAdapterChromeOS::OnStartDiscoveryError, |
940 weak_ptr_factory_.GetWeakPtr(), | 986 weak_ptr_factory_.GetWeakPtr(), |
941 callback, | 987 callback, |
942 error_callback)); | 988 error_callback)); |
943 } | 989 } |
944 | 990 |
945 void BluetoothAdapterChromeOS::RemoveDiscoverySession( | 991 void BluetoothAdapterChromeOS::RemoveDiscoverySession( |
946 const base::Closure& callback, | 992 const base::Closure& callback, |
947 const ErrorCallback& error_callback) { | 993 const ErrorCallback& error_callback) { |
| 994 DCHECK(IsPresent()); |
948 VLOG(1) << __func__; | 995 VLOG(1) << __func__; |
949 // There are active sessions other than the one currently being removed. | 996 // There are active sessions other than the one currently being removed. |
950 if (num_discovery_sessions_ > 1) { | 997 if (num_discovery_sessions_ > 1) { |
951 DCHECK(IsDiscovering()); | 998 DCHECK(IsDiscovering()); |
952 DCHECK(!discovery_request_pending_); | 999 DCHECK(!discovery_request_pending_); |
953 num_discovery_sessions_--; | 1000 num_discovery_sessions_--; |
954 callback.Run(); | 1001 callback.Run(); |
955 return; | 1002 return; |
956 } | 1003 } |
957 | 1004 |
(...skipping 24 matching lines...) Expand all Loading... |
982 object_path_, | 1029 object_path_, |
983 base::Bind(&BluetoothAdapterChromeOS::OnStopDiscovery, | 1030 base::Bind(&BluetoothAdapterChromeOS::OnStopDiscovery, |
984 weak_ptr_factory_.GetWeakPtr(), | 1031 weak_ptr_factory_.GetWeakPtr(), |
985 callback), | 1032 callback), |
986 base::Bind(&BluetoothAdapterChromeOS::OnStopDiscoveryError, | 1033 base::Bind(&BluetoothAdapterChromeOS::OnStopDiscoveryError, |
987 weak_ptr_factory_.GetWeakPtr(), | 1034 weak_ptr_factory_.GetWeakPtr(), |
988 error_callback)); | 1035 error_callback)); |
989 } | 1036 } |
990 | 1037 |
991 void BluetoothAdapterChromeOS::OnStartDiscovery(const base::Closure& callback) { | 1038 void BluetoothAdapterChromeOS::OnStartDiscovery(const base::Closure& callback) { |
| 1039 DCHECK(IsPresent()); |
992 // Report success on the original request and increment the count. | 1040 // Report success on the original request and increment the count. |
993 VLOG(1) << __func__; | 1041 VLOG(1) << __func__; |
994 DCHECK(discovery_request_pending_); | 1042 DCHECK(discovery_request_pending_); |
995 DCHECK(num_discovery_sessions_ == 0); | 1043 DCHECK(num_discovery_sessions_ == 0); |
996 discovery_request_pending_ = false; | 1044 discovery_request_pending_ = false; |
997 num_discovery_sessions_++; | 1045 num_discovery_sessions_++; |
998 callback.Run(); | 1046 callback.Run(); |
999 | 1047 |
1000 // Try to add a new discovery session for each queued request. | 1048 // Try to add a new discovery session for each queued request. |
1001 ProcessQueuedDiscoveryRequests(); | 1049 ProcessQueuedDiscoveryRequests(); |
1002 } | 1050 } |
1003 | 1051 |
1004 void BluetoothAdapterChromeOS::OnStartDiscoveryError( | 1052 void BluetoothAdapterChromeOS::OnStartDiscoveryError( |
1005 const base::Closure& callback, | 1053 const base::Closure& callback, |
1006 const ErrorCallback& error_callback, | 1054 const ErrorCallback& error_callback, |
1007 const std::string& error_name, | 1055 const std::string& error_name, |
1008 const std::string& error_message) { | 1056 const std::string& error_message) { |
| 1057 DCHECK(IsPresent()); |
1009 LOG(WARNING) << object_path_.value() << ": Failed to start discovery: " | 1058 LOG(WARNING) << object_path_.value() << ": Failed to start discovery: " |
1010 << error_name << ": " << error_message; | 1059 << error_name << ": " << error_message; |
1011 | 1060 |
1012 // Failed to start discovery. This can only happen if the count is at 0. | 1061 // Failed to start discovery. This can only happen if the count is at 0. |
1013 DCHECK(num_discovery_sessions_ == 0); | 1062 DCHECK(num_discovery_sessions_ == 0); |
1014 DCHECK(discovery_request_pending_); | 1063 DCHECK(discovery_request_pending_); |
1015 discovery_request_pending_ = false; | 1064 discovery_request_pending_ = false; |
1016 | 1065 |
1017 // Discovery request may fail if discovery was previously initiated by Chrome, | 1066 // Discovery request may fail if discovery was previously initiated by Chrome, |
1018 // but the session were invalidated due to the discovery state unexpectedly | 1067 // but the session were invalidated due to the discovery state unexpectedly |
1019 // changing to false and then back to true. In this case, report success. | 1068 // changing to false and then back to true. In this case, report success. |
1020 if (error_name == bluetooth_device::kErrorInProgress && IsDiscovering()) { | 1069 if (error_name == bluetooth_device::kErrorInProgress && IsDiscovering()) { |
1021 VLOG(1) << "Discovery previously initiated. Reporting success."; | 1070 VLOG(1) << "Discovery previously initiated. Reporting success."; |
1022 num_discovery_sessions_++; | 1071 num_discovery_sessions_++; |
1023 callback.Run(); | 1072 callback.Run(); |
1024 } else { | 1073 } else { |
1025 error_callback.Run(); | 1074 error_callback.Run(); |
1026 } | 1075 } |
1027 | 1076 |
1028 // Try to add a new discovery session for each queued request. | 1077 // Try to add a new discovery session for each queued request. |
1029 ProcessQueuedDiscoveryRequests(); | 1078 ProcessQueuedDiscoveryRequests(); |
1030 } | 1079 } |
1031 | 1080 |
1032 void BluetoothAdapterChromeOS::OnStopDiscovery(const base::Closure& callback) { | 1081 void BluetoothAdapterChromeOS::OnStopDiscovery(const base::Closure& callback) { |
| 1082 DCHECK(IsPresent()); |
1033 // Report success on the original request and decrement the count. | 1083 // Report success on the original request and decrement the count. |
1034 VLOG(1) << __func__; | 1084 VLOG(1) << __func__; |
1035 DCHECK(discovery_request_pending_); | 1085 DCHECK(discovery_request_pending_); |
1036 DCHECK(num_discovery_sessions_ == 1); | 1086 DCHECK(num_discovery_sessions_ == 1); |
1037 discovery_request_pending_ = false; | 1087 discovery_request_pending_ = false; |
1038 num_discovery_sessions_--; | 1088 num_discovery_sessions_--; |
1039 callback.Run(); | 1089 callback.Run(); |
1040 | 1090 |
1041 // Try to add a new discovery session for each queued request. | 1091 // Try to add a new discovery session for each queued request. |
1042 ProcessQueuedDiscoveryRequests(); | 1092 ProcessQueuedDiscoveryRequests(); |
1043 } | 1093 } |
1044 | 1094 |
1045 void BluetoothAdapterChromeOS::OnStopDiscoveryError( | 1095 void BluetoothAdapterChromeOS::OnStopDiscoveryError( |
1046 const ErrorCallback& error_callback, | 1096 const ErrorCallback& error_callback, |
1047 const std::string& error_name, | 1097 const std::string& error_name, |
1048 const std::string& error_message) { | 1098 const std::string& error_message) { |
| 1099 DCHECK(IsPresent()); |
1049 LOG(WARNING) << object_path_.value() << ": Failed to stop discovery: " | 1100 LOG(WARNING) << object_path_.value() << ": Failed to stop discovery: " |
1050 << error_name << ": " << error_message; | 1101 << error_name << ": " << error_message; |
1051 | 1102 |
1052 // Failed to stop discovery. This can only happen if the count is at 1. | 1103 // Failed to stop discovery. This can only happen if the count is at 1. |
1053 DCHECK(discovery_request_pending_); | 1104 DCHECK(discovery_request_pending_); |
1054 DCHECK(num_discovery_sessions_ == 1); | 1105 DCHECK(num_discovery_sessions_ == 1); |
1055 discovery_request_pending_ = false; | 1106 discovery_request_pending_ = false; |
1056 error_callback.Run(); | 1107 error_callback.Run(); |
1057 | 1108 |
1058 // Try to add a new discovery session for each queued request. | 1109 // Try to add a new discovery session for each queued request. |
1059 ProcessQueuedDiscoveryRequests(); | 1110 ProcessQueuedDiscoveryRequests(); |
1060 } | 1111 } |
1061 | 1112 |
1062 void BluetoothAdapterChromeOS::ProcessQueuedDiscoveryRequests() { | 1113 void BluetoothAdapterChromeOS::ProcessQueuedDiscoveryRequests() { |
1063 while (!discovery_request_queue_.empty()) { | 1114 while (!discovery_request_queue_.empty()) { |
1064 VLOG(1) << "Process queued discovery request."; | 1115 VLOG(1) << "Process queued discovery request."; |
1065 DiscoveryCallbackPair callbacks = discovery_request_queue_.front(); | 1116 DiscoveryCallbackPair callbacks = discovery_request_queue_.front(); |
1066 discovery_request_queue_.pop(); | 1117 discovery_request_queue_.pop(); |
1067 AddDiscoverySession(callbacks.first, callbacks.second); | 1118 AddDiscoverySession(callbacks.first, callbacks.second); |
1068 | 1119 |
1069 // If the queued request resulted in a pending call, then let it | 1120 // If the queued request resulted in a pending call, then let it |
1070 // asynchonously process the remaining queued requests once the pending | 1121 // asynchonously process the remaining queued requests once the pending |
1071 // call returns. | 1122 // call returns. |
1072 if (discovery_request_pending_) | 1123 if (discovery_request_pending_) |
1073 return; | 1124 return; |
1074 } | 1125 } |
1075 } | 1126 } |
1076 | 1127 |
1077 } // namespace chromeos | 1128 } // namespace chromeos |
OLD | NEW |