| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/chromeos/bluetooth/bluetooth_adapter_chromeos.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/logging.h" | |
| 11 #include "base/stl_util.h" | |
| 12 #include "base/values.h" | |
| 13 #include "chrome/browser/chromeos/bluetooth/bluetooth_device_chromeos.h" | |
| 14 #include "chromeos/dbus/bluetooth_adapter_client.h" | |
| 15 #include "chromeos/dbus/bluetooth_device_client.h" | |
| 16 #include "chromeos/dbus/bluetooth_manager_client.h" | |
| 17 #include "chromeos/dbus/bluetooth_out_of_band_client.h" | |
| 18 #include "chromeos/dbus/bluetooth_out_of_band_pairing_data.h" | |
| 19 #include "chromeos/dbus/dbus_thread_manager.h" | |
| 20 #include "dbus/object_path.h" | |
| 21 | |
| 22 namespace chromeos { | |
| 23 | |
| 24 BluetoothAdapterChromeOs::BluetoothAdapterChromeOs() : track_default_(false), | |
| 25 powered_(false), | |
| 26 discovering_(false), | |
| 27 weak_ptr_factory_(this) { | |
| 28 DBusThreadManager::Get()->GetBluetoothManagerClient()-> | |
| 29 AddObserver(this); | |
| 30 DBusThreadManager::Get()->GetBluetoothAdapterClient()-> | |
| 31 AddObserver(this); | |
| 32 DBusThreadManager::Get()->GetBluetoothDeviceClient()-> | |
| 33 AddObserver(this); | |
| 34 } | |
| 35 | |
| 36 BluetoothAdapterChromeOs::~BluetoothAdapterChromeOs() { | |
| 37 DBusThreadManager::Get()->GetBluetoothDeviceClient()-> | |
| 38 RemoveObserver(this); | |
| 39 DBusThreadManager::Get()->GetBluetoothAdapterClient()-> | |
| 40 RemoveObserver(this); | |
| 41 DBusThreadManager::Get()->GetBluetoothManagerClient()-> | |
| 42 RemoveObserver(this); | |
| 43 | |
| 44 STLDeleteValues(&devices_); | |
| 45 } | |
| 46 | |
| 47 void BluetoothAdapterChromeOs::AddObserver( | |
| 48 BluetoothAdapter::Observer* observer) { | |
| 49 DCHECK(observer); | |
| 50 observers_.AddObserver(observer); | |
| 51 } | |
| 52 | |
| 53 void BluetoothAdapterChromeOs::RemoveObserver( | |
| 54 BluetoothAdapter::Observer* observer) { | |
| 55 DCHECK(observer); | |
| 56 observers_.RemoveObserver(observer); | |
| 57 } | |
| 58 | |
| 59 bool BluetoothAdapterChromeOs::IsPresent() const { | |
| 60 return !object_path_.value().empty() && !address_.empty(); | |
| 61 } | |
| 62 | |
| 63 bool BluetoothAdapterChromeOs::IsPowered() const { | |
| 64 return powered_; | |
| 65 } | |
| 66 | |
| 67 void BluetoothAdapterChromeOs::SetPowered(bool powered, | |
| 68 const base::Closure& callback, | |
| 69 const ErrorCallback& error_callback) { | |
| 70 DBusThreadManager::Get()->GetBluetoothAdapterClient()-> | |
| 71 GetProperties(object_path_)->powered.Set( | |
| 72 powered, | |
| 73 base::Bind(&BluetoothAdapterChromeOs::OnSetPowered, | |
| 74 weak_ptr_factory_.GetWeakPtr(), | |
| 75 callback, | |
| 76 error_callback)); | |
| 77 } | |
| 78 | |
| 79 bool BluetoothAdapterChromeOs::IsDiscovering() const { | |
| 80 return discovering_; | |
| 81 } | |
| 82 | |
| 83 void BluetoothAdapterChromeOs::SetDiscovering( | |
| 84 bool discovering, | |
| 85 const base::Closure& callback, | |
| 86 const ErrorCallback& error_callback) { | |
| 87 if (discovering) { | |
| 88 DBusThreadManager::Get()->GetBluetoothAdapterClient()-> | |
| 89 StartDiscovery(object_path_, | |
| 90 base::Bind(&BluetoothAdapterChromeOs::OnStartDiscovery, | |
| 91 weak_ptr_factory_.GetWeakPtr(), | |
| 92 callback, | |
| 93 error_callback)); | |
| 94 } else { | |
| 95 DBusThreadManager::Get()->GetBluetoothAdapterClient()-> | |
| 96 StopDiscovery(object_path_, | |
| 97 base::Bind(&BluetoothAdapterChromeOs::OnStopDiscovery, | |
| 98 weak_ptr_factory_.GetWeakPtr(), | |
| 99 callback, | |
| 100 error_callback)); | |
| 101 } | |
| 102 } | |
| 103 | |
| 104 BluetoothAdapter::ConstDeviceList BluetoothAdapterChromeOs::GetDevices() const { | |
| 105 ConstDeviceList devices; | |
| 106 for (DevicesMap::const_iterator iter = devices_.begin(); | |
| 107 iter != devices_.end(); | |
| 108 ++iter) | |
| 109 devices.push_back(iter->second); | |
| 110 | |
| 111 return devices; | |
| 112 } | |
| 113 | |
| 114 BluetoothDevice* BluetoothAdapterChromeOs::GetDevice( | |
| 115 const std::string& address) { | |
| 116 return const_cast<BluetoothDevice *>( | |
| 117 const_cast<const BluetoothAdapterChromeOs *>(this)->GetDevice(address)); | |
| 118 } | |
| 119 | |
| 120 const BluetoothDevice* BluetoothAdapterChromeOs::GetDevice( | |
| 121 const std::string& address) const { | |
| 122 DevicesMap::const_iterator iter = devices_.find(address); | |
| 123 if (iter != devices_.end()) | |
| 124 return iter->second; | |
| 125 | |
| 126 return NULL; | |
| 127 } | |
| 128 | |
| 129 void BluetoothAdapterChromeOs::ReadLocalOutOfBandPairingData( | |
| 130 const BluetoothOutOfBandPairingDataCallback& callback, | |
| 131 const ErrorCallback& error_callback) { | |
| 132 DBusThreadManager::Get()->GetBluetoothOutOfBandClient()-> | |
| 133 ReadLocalData(object_path_, | |
| 134 base::Bind(&BluetoothAdapterChromeOs::OnReadLocalData, | |
| 135 weak_ptr_factory_.GetWeakPtr(), | |
| 136 callback, | |
| 137 error_callback)); | |
| 138 } | |
| 139 | |
| 140 void BluetoothAdapterChromeOs::TrackDefaultAdapter() { | |
| 141 DVLOG(1) << "Tracking default adapter"; | |
| 142 track_default_ = true; | |
| 143 DBusThreadManager::Get()->GetBluetoothManagerClient()-> | |
| 144 DefaultAdapter(base::Bind(&BluetoothAdapterChromeOs::AdapterCallback, | |
| 145 weak_ptr_factory_.GetWeakPtr())); | |
| 146 } | |
| 147 | |
| 148 void BluetoothAdapterChromeOs::FindAdapter(const std::string& address) { | |
| 149 DVLOG(1) << "Using adapter " << address; | |
| 150 track_default_ = false; | |
| 151 DBusThreadManager::Get()->GetBluetoothManagerClient()-> | |
| 152 FindAdapter(address, | |
| 153 base::Bind(&BluetoothAdapterChromeOs::AdapterCallback, | |
| 154 weak_ptr_factory_.GetWeakPtr())); | |
| 155 } | |
| 156 | |
| 157 void BluetoothAdapterChromeOs::AdapterCallback( | |
| 158 const dbus::ObjectPath& adapter_path, | |
| 159 bool success) { | |
| 160 if (success) { | |
| 161 ChangeAdapter(adapter_path); | |
| 162 } else if (!object_path_.value().empty()) { | |
| 163 RemoveAdapter(); | |
| 164 } | |
| 165 } | |
| 166 | |
| 167 void BluetoothAdapterChromeOs::DefaultAdapterChanged( | |
| 168 const dbus::ObjectPath& adapter_path) { | |
| 169 if (track_default_) | |
| 170 ChangeAdapter(adapter_path); | |
| 171 } | |
| 172 | |
| 173 void BluetoothAdapterChromeOs::AdapterRemoved( | |
| 174 const dbus::ObjectPath& adapter_path) { | |
| 175 if (adapter_path == object_path_) | |
| 176 RemoveAdapter(); | |
| 177 } | |
| 178 | |
| 179 void BluetoothAdapterChromeOs::ChangeAdapter( | |
| 180 const dbus::ObjectPath& adapter_path) { | |
| 181 if (object_path_.value().empty()) { | |
| 182 DVLOG(1) << "Adapter path initialized to " << adapter_path.value(); | |
| 183 } else if (object_path_.value() != adapter_path.value()) { | |
| 184 DVLOG(1) << "Adapter path changed from " << object_path_.value() | |
| 185 << " to " << adapter_path.value(); | |
| 186 | |
| 187 RemoveAdapter(); | |
| 188 } else { | |
| 189 DVLOG(1) << "Adapter address updated"; | |
| 190 } | |
| 191 | |
| 192 object_path_ = adapter_path; | |
| 193 | |
| 194 // Update properties to their new values. | |
| 195 BluetoothAdapterClient::Properties* properties = | |
| 196 DBusThreadManager::Get()->GetBluetoothAdapterClient()-> | |
| 197 GetProperties(object_path_); | |
| 198 | |
| 199 address_ = properties->address.value(); | |
| 200 name_ = properties->name.value(); | |
| 201 | |
| 202 // Delay announcing a new adapter until we have an address. | |
| 203 if (address_.empty()) { | |
| 204 DVLOG(1) << "Adapter address not yet known"; | |
| 205 return; | |
| 206 } | |
| 207 | |
| 208 PoweredChanged(properties->powered.value()); | |
| 209 DiscoveringChanged(properties->discovering.value()); | |
| 210 DevicesChanged(properties->devices.value()); | |
| 211 | |
| 212 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | |
| 213 AdapterPresentChanged(this, true)); | |
| 214 } | |
| 215 | |
| 216 void BluetoothAdapterChromeOs::RemoveAdapter() { | |
| 217 const bool adapter_was_present = IsPresent(); | |
| 218 | |
| 219 DVLOG(1) << "Adapter lost."; | |
| 220 PoweredChanged(false); | |
| 221 DiscoveringChanged(false); | |
| 222 ClearDevices(); | |
| 223 | |
| 224 object_path_ = dbus::ObjectPath(""); | |
| 225 address_.clear(); | |
| 226 name_.clear(); | |
| 227 | |
| 228 if (adapter_was_present) | |
| 229 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | |
| 230 AdapterPresentChanged(this, false)); | |
| 231 } | |
| 232 | |
| 233 void BluetoothAdapterChromeOs::OnSetPowered(const base::Closure& callback, | |
| 234 const ErrorCallback& error_callback, | |
| 235 bool success) { | |
| 236 if (success) | |
| 237 callback.Run(); | |
| 238 else | |
| 239 error_callback.Run(); | |
| 240 } | |
| 241 | |
| 242 void BluetoothAdapterChromeOs::PoweredChanged(bool powered) { | |
| 243 if (powered == powered_) | |
| 244 return; | |
| 245 | |
| 246 powered_ = powered; | |
| 247 | |
| 248 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | |
| 249 AdapterPoweredChanged(this, powered_)); | |
| 250 } | |
| 251 | |
| 252 void BluetoothAdapterChromeOs::OnStartDiscovery( | |
| 253 const base::Closure& callback, | |
| 254 const ErrorCallback& error_callback, | |
| 255 const dbus::ObjectPath& adapter_path, | |
| 256 bool success) { | |
| 257 if (success) { | |
| 258 DVLOG(1) << object_path_.value() << ": started discovery."; | |
| 259 | |
| 260 // Clear devices found in previous discovery attempts | |
| 261 ClearDiscoveredDevices(); | |
| 262 callback.Run(); | |
| 263 } else { | |
| 264 // TODO(keybuk): in future, don't run the callback if the error was just | |
| 265 // that we were already discovering. | |
| 266 error_callback.Run(); | |
| 267 } | |
| 268 } | |
| 269 | |
| 270 void BluetoothAdapterChromeOs::OnStopDiscovery( | |
| 271 const base::Closure& callback, | |
| 272 const ErrorCallback& error_callback, | |
| 273 const dbus::ObjectPath& adapter_path, | |
| 274 bool success) { | |
| 275 if (success) { | |
| 276 DVLOG(1) << object_path_.value() << ": stopped discovery."; | |
| 277 callback.Run(); | |
| 278 // Leave found devices available for perusing. | |
| 279 } else { | |
| 280 // TODO(keybuk): in future, don't run the callback if the error was just | |
| 281 // that we weren't discovering. | |
| 282 error_callback.Run(); | |
| 283 } | |
| 284 } | |
| 285 | |
| 286 void BluetoothAdapterChromeOs::DiscoveringChanged(bool discovering) { | |
| 287 if (discovering == discovering_) | |
| 288 return; | |
| 289 | |
| 290 discovering_ = discovering; | |
| 291 | |
| 292 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | |
| 293 AdapterDiscoveringChanged(this, discovering_)); | |
| 294 } | |
| 295 | |
| 296 void BluetoothAdapterChromeOs::OnReadLocalData( | |
| 297 const BluetoothOutOfBandPairingDataCallback& callback, | |
| 298 const ErrorCallback& error_callback, | |
| 299 const BluetoothOutOfBandPairingData& data, | |
| 300 bool success) { | |
| 301 if (success) | |
| 302 callback.Run(data); | |
| 303 else | |
| 304 error_callback.Run(); | |
| 305 } | |
| 306 | |
| 307 void BluetoothAdapterChromeOs::AdapterPropertyChanged( | |
| 308 const dbus::ObjectPath& adapter_path, | |
| 309 const std::string& property_name) { | |
| 310 if (adapter_path != object_path_) | |
| 311 return; | |
| 312 | |
| 313 BluetoothAdapterClient::Properties* properties = | |
| 314 DBusThreadManager::Get()->GetBluetoothAdapterClient()-> | |
| 315 GetProperties(object_path_); | |
| 316 | |
| 317 if (property_name == properties->address.name()) { | |
| 318 ChangeAdapter(object_path_); | |
| 319 | |
| 320 } else if (!address_.empty()) { | |
| 321 if (property_name == properties->powered.name()) { | |
| 322 PoweredChanged(properties->powered.value()); | |
| 323 | |
| 324 } else if (property_name == properties->discovering.name()) { | |
| 325 DiscoveringChanged(properties->discovering.value()); | |
| 326 | |
| 327 } else if (property_name == properties->devices.name()) { | |
| 328 DevicesChanged(properties->devices.value()); | |
| 329 | |
| 330 } else if (property_name == properties->name.name()) { | |
| 331 name_ = properties->name.value(); | |
| 332 | |
| 333 } | |
| 334 } | |
| 335 } | |
| 336 | |
| 337 void BluetoothAdapterChromeOs::DevicePropertyChanged( | |
| 338 const dbus::ObjectPath& device_path, | |
| 339 const std::string& property_name) { | |
| 340 UpdateDevice(device_path); | |
| 341 } | |
| 342 | |
| 343 void BluetoothAdapterChromeOs::UpdateDevice( | |
| 344 const dbus::ObjectPath& device_path) { | |
| 345 BluetoothDeviceClient::Properties* properties = | |
| 346 DBusThreadManager::Get()->GetBluetoothDeviceClient()-> | |
| 347 GetProperties(device_path); | |
| 348 | |
| 349 // When we first see a device, we may not know the address yet and need to | |
| 350 // wait for the DevicePropertyChanged signal before adding the device. | |
| 351 const std::string address = properties->address.value(); | |
| 352 if (address.empty()) | |
| 353 return; | |
| 354 | |
| 355 // The device may be already known to us, either because this is an update | |
| 356 // to properties, or the device going from discovered to connected and | |
| 357 // pairing gaining an object path in the process. In any case, we want | |
| 358 // to update the existing object, not create a new one. | |
| 359 DevicesMap::iterator iter = devices_.find(address); | |
| 360 BluetoothDeviceChromeOs* device; | |
| 361 const bool update_device = (iter != devices_.end()); | |
| 362 if (update_device) { | |
| 363 device = iter->second; | |
| 364 } else { | |
| 365 device = BluetoothDeviceChromeOs::Create(this); | |
| 366 devices_[address] = device; | |
| 367 } | |
| 368 | |
| 369 const bool was_paired = device->IsPaired(); | |
| 370 if (!was_paired) { | |
| 371 DVLOG(1) << "Assigned object path " << device_path.value() << " to device " | |
| 372 << address; | |
| 373 device->SetObjectPath(device_path); | |
| 374 } | |
| 375 device->Update(properties, true); | |
| 376 | |
| 377 if (update_device) { | |
| 378 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | |
| 379 DeviceChanged(this, device)); | |
| 380 } else { | |
| 381 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | |
| 382 DeviceAdded(this, device)); | |
| 383 } | |
| 384 } | |
| 385 | |
| 386 void BluetoothAdapterChromeOs::ClearDevices() { | |
| 387 DevicesMap replace; | |
| 388 devices_.swap(replace); | |
| 389 for (DevicesMap::iterator iter = replace.begin(); | |
| 390 iter != replace.end(); ++iter) { | |
| 391 BluetoothDeviceChromeOs* device = iter->second; | |
| 392 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | |
| 393 DeviceRemoved(this, device)); | |
| 394 | |
| 395 delete device; | |
| 396 } | |
| 397 } | |
| 398 | |
| 399 void BluetoothAdapterChromeOs::DeviceCreated( | |
| 400 const dbus::ObjectPath& adapter_path, | |
| 401 const dbus::ObjectPath& device_path) { | |
| 402 if (adapter_path != object_path_) | |
| 403 return; | |
| 404 | |
| 405 UpdateDevice(device_path); | |
| 406 } | |
| 407 | |
| 408 void BluetoothAdapterChromeOs::DeviceRemoved( | |
| 409 const dbus::ObjectPath& adapter_path, | |
| 410 const dbus::ObjectPath& device_path) { | |
| 411 if (adapter_path != object_path_) | |
| 412 return; | |
| 413 | |
| 414 DevicesMap::iterator iter = devices_.begin(); | |
| 415 while (iter != devices_.end()) { | |
| 416 BluetoothDeviceChromeOs* device = iter->second; | |
| 417 DevicesMap::iterator temp = iter; | |
| 418 ++iter; | |
| 419 | |
| 420 if (device->object_path_ != device_path) | |
| 421 continue; | |
| 422 | |
| 423 // DeviceRemoved can also be called to indicate a device that is visible | |
| 424 // during discovery has disconnected, but it is still visible to the | |
| 425 // adapter, so don't remove in that case and only clear the object path. | |
| 426 if (!device->IsVisible()) { | |
| 427 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | |
| 428 DeviceRemoved(this, device)); | |
| 429 | |
| 430 DVLOG(1) << "Removed device " << device->address(); | |
| 431 | |
| 432 delete device; | |
| 433 devices_.erase(temp); | |
| 434 } else { | |
| 435 DVLOG(1) << "Removed object path from device " << device->address(); | |
| 436 device->RemoveObjectPath(); | |
| 437 | |
| 438 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | |
| 439 DeviceChanged(this, device)); | |
| 440 } | |
| 441 } | |
| 442 } | |
| 443 | |
| 444 void BluetoothAdapterChromeOs::DevicesChanged( | |
| 445 const std::vector<dbus::ObjectPath>& devices) { | |
| 446 for (std::vector<dbus::ObjectPath>::const_iterator iter = | |
| 447 devices.begin(); iter != devices.end(); ++iter) | |
| 448 UpdateDevice(*iter); | |
| 449 } | |
| 450 | |
| 451 void BluetoothAdapterChromeOs::ClearDiscoveredDevices() { | |
| 452 DevicesMap::iterator iter = devices_.begin(); | |
| 453 while (iter != devices_.end()) { | |
| 454 BluetoothDeviceChromeOs* device = iter->second; | |
| 455 DevicesMap::iterator temp = iter; | |
| 456 ++iter; | |
| 457 | |
| 458 if (!device->IsPaired()) { | |
| 459 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | |
| 460 DeviceRemoved(this, device)); | |
| 461 | |
| 462 delete device; | |
| 463 devices_.erase(temp); | |
| 464 } | |
| 465 } | |
| 466 } | |
| 467 | |
| 468 void BluetoothAdapterChromeOs::DeviceFound( | |
| 469 const dbus::ObjectPath& adapter_path, | |
| 470 const std::string& address, | |
| 471 const BluetoothDeviceClient::Properties& properties) { | |
| 472 if (adapter_path != object_path_) | |
| 473 return; | |
| 474 | |
| 475 // DeviceFound can also be called to indicate that a device we've | |
| 476 // paired with is now visible to the adapter during discovery, in which | |
| 477 // case we want to update the existing object, not create a new one. | |
| 478 BluetoothDeviceChromeOs* device; | |
| 479 DevicesMap::iterator iter = devices_.find(address); | |
| 480 const bool update_device = (iter != devices_.end()); | |
| 481 if (update_device) { | |
| 482 device = iter->second; | |
| 483 } else { | |
| 484 device = BluetoothDeviceChromeOs::Create(this); | |
| 485 devices_[address] = device; | |
| 486 } | |
| 487 | |
| 488 DVLOG(1) << "Device " << address << " is visible to the adapter"; | |
| 489 device->SetVisible(true); | |
| 490 device->Update(&properties, false); | |
| 491 | |
| 492 if (update_device) { | |
| 493 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | |
| 494 DeviceChanged(this, device)); | |
| 495 } else { | |
| 496 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | |
| 497 DeviceAdded(this, device)); | |
| 498 } | |
| 499 } | |
| 500 | |
| 501 void BluetoothAdapterChromeOs::DeviceDisappeared( | |
| 502 const dbus::ObjectPath& adapter_path, | |
| 503 const std::string& address) { | |
| 504 if (adapter_path != object_path_) | |
| 505 return; | |
| 506 | |
| 507 DevicesMap::iterator iter = devices_.find(address); | |
| 508 if (iter == devices_.end()) | |
| 509 return; | |
| 510 | |
| 511 BluetoothDeviceChromeOs* device = iter->second; | |
| 512 | |
| 513 // DeviceDisappeared can also be called to indicate that a device we've | |
| 514 // paired with is no longer visible to the adapter, so don't remove | |
| 515 // in that case and only clear the visible flag. | |
| 516 if (!device->IsPaired()) { | |
| 517 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | |
| 518 DeviceRemoved(this, device)); | |
| 519 | |
| 520 DVLOG(1) << "Discovered device " << device->address() | |
| 521 << " is no longer visible to the adapter"; | |
| 522 | |
| 523 delete device; | |
| 524 devices_.erase(iter); | |
| 525 } else { | |
| 526 DVLOG(1) << "Paired device " << device->address() | |
| 527 << " is no longer visible to the adapter"; | |
| 528 device->SetVisible(false); | |
| 529 | |
| 530 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | |
| 531 DeviceChanged(this, device)); | |
| 532 } | |
| 533 } | |
| 534 | |
| 535 } // namespace chromeos | |
| OLD | NEW |