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