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