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