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