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