OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/chromeos/dbus/bluetooth_adapter_client.h" | |
6 | |
7 #include <map> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/logging.h" | |
11 #include "base/stl_util.h" | |
12 #include "chrome/browser/chromeos/dbus/bluetooth_device_client.h" | |
13 #include "chrome/browser/chromeos/dbus/bluetooth_manager_client.h" | |
14 #include "chrome/browser/chromeos/dbus/bluetooth_property.h" | |
15 #include "dbus/bus.h" | |
16 #include "dbus/message.h" | |
17 #include "dbus/object_path.h" | |
18 #include "dbus/object_proxy.h" | |
19 #include "third_party/cros_system_api/dbus/service_constants.h" | |
20 | |
21 namespace chromeos { | |
22 | |
23 BluetoothAdapterClient::Properties::Properties(dbus::ObjectProxy* object_proxy, | |
24 PropertyChangedCallback callback) | |
25 : BluetoothPropertySet(object_proxy, | |
26 bluetooth_adapter::kBluetoothAdapterInterface, | |
27 callback) { | |
28 RegisterProperty(bluetooth_adapter::kAddressProperty, &address); | |
29 RegisterProperty(bluetooth_adapter::kNameProperty, &name); | |
30 RegisterProperty(bluetooth_adapter::kClassProperty, &bluetooth_class); | |
31 RegisterProperty(bluetooth_adapter::kPoweredProperty, &powered); | |
32 RegisterProperty(bluetooth_adapter::kDiscoverableProperty, &discoverable); | |
33 RegisterProperty(bluetooth_adapter::kPairableProperty, &pairable); | |
34 RegisterProperty(bluetooth_adapter::kPairableTimeoutProperty, | |
35 &pairable_timeout); | |
36 RegisterProperty(bluetooth_adapter::kDiscoverableTimeoutProperty, | |
37 &discoverable_timeout); | |
38 RegisterProperty(bluetooth_adapter::kDiscoveringProperty, &discovering); | |
39 RegisterProperty(bluetooth_adapter::kDevicesProperty, &devices); | |
40 RegisterProperty(bluetooth_adapter::kUUIDsProperty, &uuids); | |
41 } | |
42 | |
43 BluetoothAdapterClient::Properties::~Properties() { | |
44 } | |
45 | |
46 | |
47 // The BluetoothAdapterClient implementation used in production. | |
48 class BluetoothAdapterClientImpl: public BluetoothAdapterClient, | |
49 private BluetoothManagerClient::Observer { | |
50 public: | |
51 explicit BluetoothAdapterClientImpl(dbus::Bus* bus, | |
52 BluetoothManagerClient* manager_client) | |
53 : weak_ptr_factory_(this), | |
54 bus_(bus) { | |
55 DVLOG(1) << "Creating BluetoothAdapterClientImpl"; | |
56 | |
57 DCHECK(manager_client); | |
58 manager_client->AddObserver(this); | |
59 } | |
60 | |
61 virtual ~BluetoothAdapterClientImpl() { | |
62 // Clean up Properties structures | |
63 for (ObjectMap::iterator iter = object_map_.begin(); | |
64 iter != object_map_.end(); ++iter) { | |
65 Object object = iter->second; | |
66 Properties* properties = object.second; | |
67 delete properties; | |
68 } | |
69 } | |
70 | |
71 // BluetoothAdapterClient override. | |
72 virtual void AddObserver(BluetoothAdapterClient::Observer* observer) | |
73 OVERRIDE { | |
74 DCHECK(observer); | |
75 observers_.AddObserver(observer); | |
76 } | |
77 | |
78 // BluetoothAdapterClient override. | |
79 virtual void RemoveObserver(BluetoothAdapterClient::Observer* observer) | |
80 OVERRIDE { | |
81 DCHECK(observer); | |
82 observers_.RemoveObserver(observer); | |
83 } | |
84 | |
85 // BluetoothAdapterClient override. | |
86 virtual Properties* GetProperties(const dbus::ObjectPath& object_path) | |
87 OVERRIDE { | |
88 return GetObject(object_path).second; | |
89 } | |
90 | |
91 // BluetoothAdapterClient override. | |
92 virtual void RequestSession(const dbus::ObjectPath& object_path, | |
93 const AdapterCallback& callback) OVERRIDE { | |
94 dbus::MethodCall method_call( | |
95 bluetooth_adapter::kBluetoothAdapterInterface, | |
96 bluetooth_adapter::kRequestSession); | |
97 | |
98 dbus::ObjectProxy* object_proxy = GetObjectProxy(object_path); | |
99 | |
100 object_proxy->CallMethod( | |
101 &method_call, | |
102 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
103 base::Bind(&BluetoothAdapterClientImpl::OnRequestSession, | |
104 weak_ptr_factory_.GetWeakPtr(), object_path, callback)); | |
105 } | |
106 | |
107 // BluetoothAdapterClient override. | |
108 virtual void ReleaseSession(const dbus::ObjectPath& object_path, | |
109 const AdapterCallback& callback) OVERRIDE { | |
110 dbus::MethodCall method_call( | |
111 bluetooth_adapter::kBluetoothAdapterInterface, | |
112 bluetooth_adapter::kReleaseSession); | |
113 | |
114 dbus::ObjectProxy* object_proxy = GetObjectProxy(object_path); | |
115 | |
116 object_proxy->CallMethod( | |
117 &method_call, | |
118 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
119 base::Bind(&BluetoothAdapterClientImpl::OnReleaseSession, | |
120 weak_ptr_factory_.GetWeakPtr(), object_path, callback)); | |
121 } | |
122 | |
123 // BluetoothAdapterClient override. | |
124 virtual void StartDiscovery(const dbus::ObjectPath& object_path, | |
125 const AdapterCallback& callback) OVERRIDE { | |
126 dbus::MethodCall method_call( | |
127 bluetooth_adapter::kBluetoothAdapterInterface, | |
128 bluetooth_adapter::kStartDiscovery); | |
129 | |
130 dbus::ObjectProxy* object_proxy = GetObjectProxy(object_path); | |
131 | |
132 object_proxy->CallMethod( | |
133 &method_call, | |
134 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
135 base::Bind(&BluetoothAdapterClientImpl::OnStartDiscovery, | |
136 weak_ptr_factory_.GetWeakPtr(), object_path, callback)); | |
137 } | |
138 | |
139 // BluetoothAdapterClient override. | |
140 virtual void StopDiscovery(const dbus::ObjectPath& object_path, | |
141 const AdapterCallback& callback) OVERRIDE { | |
142 dbus::MethodCall method_call( | |
143 bluetooth_adapter::kBluetoothAdapterInterface, | |
144 bluetooth_adapter::kStopDiscovery); | |
145 | |
146 dbus::ObjectProxy* object_proxy = GetObjectProxy(object_path); | |
147 | |
148 object_proxy->CallMethod( | |
149 &method_call, | |
150 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
151 base::Bind(&BluetoothAdapterClientImpl::OnStopDiscovery, | |
152 weak_ptr_factory_.GetWeakPtr(), object_path, callback)); | |
153 } | |
154 | |
155 // BluetoothAdapterClient override. | |
156 virtual void FindDevice(const dbus::ObjectPath& object_path, | |
157 const std::string& address, | |
158 const DeviceCallback& callback) OVERRIDE { | |
159 dbus::MethodCall method_call( | |
160 bluetooth_adapter::kBluetoothAdapterInterface, | |
161 bluetooth_adapter::kFindDevice); | |
162 | |
163 dbus::MessageWriter writer(&method_call); | |
164 writer.AppendString(address); | |
165 | |
166 dbus::ObjectProxy* object_proxy = GetObjectProxy(object_path); | |
167 | |
168 object_proxy->CallMethod( | |
169 &method_call, | |
170 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
171 base::Bind(&BluetoothAdapterClientImpl::OnFindDevice, | |
172 weak_ptr_factory_.GetWeakPtr(), object_path, callback)); | |
173 } | |
174 | |
175 // BluetoothAdapterClient override. | |
176 virtual void CreateDevice(const dbus::ObjectPath& object_path, | |
177 const std::string& address, | |
178 const DeviceCallback& callback) OVERRIDE { | |
179 dbus::MethodCall method_call( | |
180 bluetooth_adapter::kBluetoothAdapterInterface, | |
181 bluetooth_adapter::kCreateDevice); | |
182 | |
183 dbus::MessageWriter writer(&method_call); | |
184 writer.AppendString(address); | |
185 | |
186 dbus::ObjectProxy* object_proxy = GetObjectProxy(object_path); | |
187 | |
188 object_proxy->CallMethod( | |
189 &method_call, | |
190 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
191 base::Bind(&BluetoothAdapterClientImpl::OnCreateDevice, | |
192 weak_ptr_factory_.GetWeakPtr(), object_path, callback)); | |
193 } | |
194 | |
195 // BluetoothAdapterClient override. | |
196 virtual void CreatePairedDevice(const dbus::ObjectPath& object_path, | |
197 const std::string& address, | |
198 const dbus::ObjectPath& agent_path, | |
199 const std::string& capability, | |
200 const DeviceCallback& callback) OVERRIDE { | |
201 dbus::MethodCall method_call( | |
202 bluetooth_adapter::kBluetoothAdapterInterface, | |
203 bluetooth_adapter::kCreatePairedDevice); | |
204 | |
205 dbus::MessageWriter writer(&method_call); | |
206 writer.AppendString(address); | |
207 writer.AppendObjectPath(agent_path); | |
208 writer.AppendString(capability); | |
209 | |
210 dbus::ObjectProxy* object_proxy = GetObjectProxy(object_path); | |
211 | |
212 object_proxy->CallMethod( | |
213 &method_call, | |
214 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
215 base::Bind(&BluetoothAdapterClientImpl::OnCreatePairedDevice, | |
216 weak_ptr_factory_.GetWeakPtr(), object_path, callback)); | |
217 } | |
218 | |
219 // BluetoothAdapterClient override. | |
220 virtual void CancelDeviceCreation(const dbus::ObjectPath& object_path, | |
221 const std::string& address, | |
222 const AdapterCallback& callback) OVERRIDE { | |
223 dbus::MethodCall method_call( | |
224 bluetooth_adapter::kBluetoothAdapterInterface, | |
225 bluetooth_adapter::kCancelDeviceCreation); | |
226 | |
227 dbus::MessageWriter writer(&method_call); | |
228 writer.AppendString(address); | |
229 | |
230 dbus::ObjectProxy* object_proxy = GetObjectProxy(object_path); | |
231 | |
232 object_proxy->CallMethod( | |
233 &method_call, | |
234 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
235 base::Bind(&BluetoothAdapterClientImpl::OnCancelDeviceCreation, | |
236 weak_ptr_factory_.GetWeakPtr(), object_path, callback)); | |
237 } | |
238 | |
239 // BluetoothAdapterClient override. | |
240 virtual void RemoveDevice(const dbus::ObjectPath& object_path, | |
241 const dbus::ObjectPath& device_path, | |
242 const AdapterCallback& callback) OVERRIDE { | |
243 dbus::MethodCall method_call( | |
244 bluetooth_adapter::kBluetoothAdapterInterface, | |
245 bluetooth_adapter::kRemoveDevice); | |
246 | |
247 dbus::MessageWriter writer(&method_call); | |
248 writer.AppendObjectPath(device_path); | |
249 | |
250 dbus::ObjectProxy* object_proxy = GetObjectProxy(object_path); | |
251 | |
252 object_proxy->CallMethod( | |
253 &method_call, | |
254 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
255 base::Bind(&BluetoothAdapterClientImpl::OnRemoveDevice, | |
256 weak_ptr_factory_.GetWeakPtr(), object_path, callback)); | |
257 } | |
258 | |
259 // BluetoothAdapterClient override. | |
260 virtual void RegisterAgent(const dbus::ObjectPath& object_path, | |
261 const dbus::ObjectPath& agent_path, | |
262 const std::string& capability, | |
263 const AdapterCallback& callback) OVERRIDE { | |
264 dbus::MethodCall method_call( | |
265 bluetooth_adapter::kBluetoothAdapterInterface, | |
266 bluetooth_adapter::kRegisterAgent); | |
267 | |
268 dbus::MessageWriter writer(&method_call); | |
269 writer.AppendObjectPath(agent_path); | |
270 writer.AppendString(capability); | |
271 | |
272 dbus::ObjectProxy* object_proxy = GetObjectProxy(object_path); | |
273 | |
274 object_proxy->CallMethod( | |
275 &method_call, | |
276 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
277 base::Bind(&BluetoothAdapterClientImpl::OnRegisterAgent, | |
278 weak_ptr_factory_.GetWeakPtr(), object_path, callback)); | |
279 } | |
280 | |
281 // BluetoothAdapterClient override. | |
282 virtual void UnregisterAgent(const dbus::ObjectPath& object_path, | |
283 const dbus::ObjectPath& agent_path, | |
284 const AdapterCallback& callback) OVERRIDE { | |
285 dbus::MethodCall method_call( | |
286 bluetooth_adapter::kBluetoothAdapterInterface, | |
287 bluetooth_adapter::kUnregisterAgent); | |
288 | |
289 dbus::MessageWriter writer(&method_call); | |
290 writer.AppendObjectPath(agent_path); | |
291 | |
292 dbus::ObjectProxy* object_proxy = GetObjectProxy(object_path); | |
293 | |
294 object_proxy->CallMethod( | |
295 &method_call, | |
296 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
297 base::Bind(&BluetoothAdapterClientImpl::OnCreateDevice, | |
298 weak_ptr_factory_.GetWeakPtr(), object_path, callback)); | |
299 } | |
300 | |
301 private: | |
302 // We maintain a collection of dbus object proxies and properties structures | |
303 // for each adapter. | |
304 typedef std::pair<dbus::ObjectProxy*, Properties*> Object; | |
305 typedef std::map<const dbus::ObjectPath, Object> ObjectMap; | |
306 ObjectMap object_map_; | |
307 | |
308 // BluetoothManagerClient::Observer override. | |
309 virtual void AdapterAdded(const dbus::ObjectPath& object_path) OVERRIDE { | |
310 } | |
311 | |
312 // BluetoothManagerClient::Observer override. | |
313 virtual void AdapterRemoved(const dbus::ObjectPath& object_path) OVERRIDE { | |
314 RemoveObject(object_path); | |
315 } | |
316 | |
317 // Ensures that we have an object proxy and properties structure for | |
318 // an adapter with object path |object_path|, creating it if not and | |
319 // storing in our |object_map_| map. | |
320 Object GetObject(const dbus::ObjectPath& object_path) { | |
321 ObjectMap::iterator iter = object_map_.find(object_path); | |
322 if (iter != object_map_.end()) | |
323 return iter->second; | |
324 | |
325 // Create the object proxy. | |
326 DCHECK(bus_); | |
327 dbus::ObjectProxy* object_proxy = bus_->GetObjectProxy( | |
328 bluetooth_adapter::kBluetoothAdapterServiceName, object_path); | |
329 | |
330 object_proxy->ConnectToSignal( | |
331 bluetooth_adapter::kBluetoothAdapterInterface, | |
332 bluetooth_adapter::kDeviceCreatedSignal, | |
333 base::Bind(&BluetoothAdapterClientImpl::DeviceCreatedReceived, | |
334 weak_ptr_factory_.GetWeakPtr(), object_path), | |
335 base::Bind(&BluetoothAdapterClientImpl::DeviceCreatedConnected, | |
336 weak_ptr_factory_.GetWeakPtr(), object_path)); | |
337 | |
338 object_proxy->ConnectToSignal( | |
339 bluetooth_adapter::kBluetoothAdapterInterface, | |
340 bluetooth_adapter::kDeviceRemovedSignal, | |
341 base::Bind(&BluetoothAdapterClientImpl::DeviceRemovedReceived, | |
342 weak_ptr_factory_.GetWeakPtr(), object_path), | |
343 base::Bind(&BluetoothAdapterClientImpl::DeviceRemovedConnected, | |
344 weak_ptr_factory_.GetWeakPtr(), object_path)); | |
345 | |
346 object_proxy->ConnectToSignal( | |
347 bluetooth_adapter::kBluetoothAdapterInterface, | |
348 bluetooth_adapter::kDeviceFoundSignal, | |
349 base::Bind(&BluetoothAdapterClientImpl::DeviceFoundReceived, | |
350 weak_ptr_factory_.GetWeakPtr(), object_path), | |
351 base::Bind(&BluetoothAdapterClientImpl::DeviceFoundConnected, | |
352 weak_ptr_factory_.GetWeakPtr(), object_path)); | |
353 | |
354 object_proxy->ConnectToSignal( | |
355 bluetooth_adapter::kBluetoothAdapterInterface, | |
356 bluetooth_adapter::kDeviceDisappearedSignal, | |
357 base::Bind(&BluetoothAdapterClientImpl::DeviceDisappearedReceived, | |
358 weak_ptr_factory_.GetWeakPtr(), object_path), | |
359 base::Bind(&BluetoothAdapterClientImpl::DeviceDisappearedConnected, | |
360 weak_ptr_factory_.GetWeakPtr(), object_path)); | |
361 | |
362 // Create the properties structure. | |
363 Properties* properties = new Properties( | |
364 object_proxy, | |
365 base::Bind(&BluetoothAdapterClientImpl::OnPropertyChanged, | |
366 weak_ptr_factory_.GetWeakPtr(), object_path)); | |
367 | |
368 properties->ConnectSignals(); | |
369 properties->GetAll(); | |
370 | |
371 Object object = std::make_pair(object_proxy, properties); | |
372 object_map_[object_path] = object; | |
373 return object; | |
374 } | |
375 | |
376 // Removes the dbus object proxy and properties for the adapter with | |
377 // dbus object path |object_path| from our |object_map_| map. | |
378 void RemoveObject(const dbus::ObjectPath& object_path) { | |
379 ObjectMap::iterator iter = object_map_.find(object_path); | |
380 if (iter != object_map_.end()) { | |
381 // Clean up the Properties structure. | |
382 Object object = iter->second; | |
383 Properties* properties = object.second; | |
384 delete properties; | |
385 | |
386 object_map_.erase(iter); | |
387 } | |
388 } | |
389 | |
390 // Returns a pointer to the object proxy for |object_path|, creating | |
391 // it if necessary. | |
392 dbus::ObjectProxy* GetObjectProxy(const dbus::ObjectPath& object_path) { | |
393 return GetObject(object_path).first; | |
394 } | |
395 | |
396 // Called by BluetoothPropertySet when a property value is changed, | |
397 // either by result of a signal or response to a GetAll() or Get() | |
398 // call. Informs observers. | |
399 void OnPropertyChanged(const dbus::ObjectPath& object_path, | |
400 const std::string& property_name) { | |
401 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer, observers_, | |
402 AdapterPropertyChanged(object_path, property_name)); | |
403 } | |
404 | |
405 // Called by dbus:: when a DeviceCreated signal is received. | |
406 void DeviceCreatedReceived(const dbus::ObjectPath& object_path, | |
407 dbus::Signal* signal) { | |
408 DCHECK(signal); | |
409 dbus::MessageReader reader(signal); | |
410 dbus::ObjectPath device_path; | |
411 if (!reader.PopObjectPath(&device_path)) { | |
412 LOG(WARNING) << object_path.value() | |
413 << ": DeviceCreated signal has incorrect parameters: " | |
414 << signal->ToString(); | |
415 return; | |
416 } | |
417 | |
418 DVLOG(1) << object_path.value() << ": Device created: " | |
419 << device_path.value(); | |
420 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer, observers_, | |
421 DeviceCreated(object_path, device_path)); | |
422 } | |
423 | |
424 // Called by dbus:: when the DeviceCreated signal is initially connected. | |
425 void DeviceCreatedConnected(const dbus::ObjectPath& object_path, | |
426 const std::string& interface_name, | |
427 const std::string& signal_name, | |
428 bool success) { | |
429 LOG_IF(WARNING, !success) << object_path.value() | |
430 << ": Failed to connect to DeviceCreated signal."; | |
431 } | |
432 | |
433 // Called by dbus:: when a DeviceRemoved signal is received. | |
434 void DeviceRemovedReceived(const dbus::ObjectPath& object_path, | |
435 dbus::Signal* signal) { | |
436 DCHECK(signal); | |
437 dbus::MessageReader reader(signal); | |
438 dbus::ObjectPath device_path; | |
439 if (!reader.PopObjectPath(&device_path)) { | |
440 LOG(WARNING) << object_path.value() | |
441 << ": DeviceRemoved signal has incorrect parameters: " | |
442 << signal->ToString(); | |
443 return; | |
444 } | |
445 | |
446 DVLOG(1) << object_path.value() << ": Device removed: " | |
447 << device_path.value(); | |
448 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer, observers_, | |
449 DeviceRemoved(object_path, device_path)); | |
450 } | |
451 | |
452 // Called by dbus:: when the DeviceRemoved signal is initially connected. | |
453 void DeviceRemovedConnected(const dbus::ObjectPath& object_path, | |
454 const std::string& interface_name, | |
455 const std::string& signal_name, | |
456 bool success) { | |
457 LOG_IF(WARNING, !success) << object_path.value() | |
458 << ": Failed to connect to DeviceRemoved signal."; | |
459 } | |
460 | |
461 // Called by dbus:: when a DeviceFound signal is received. | |
462 void DeviceFoundReceived(const dbus::ObjectPath& object_path, | |
463 dbus::Signal* signal) { | |
464 DCHECK(signal); | |
465 dbus::MessageReader reader(signal); | |
466 std::string address; | |
467 if (!reader.PopString(&address)) { | |
468 LOG(WARNING) << object_path.value() | |
469 << ": DeviceFound signal has incorrect parameters: " | |
470 << signal->ToString(); | |
471 return; | |
472 } | |
473 | |
474 // Create device properties structure without an attached object_proxy | |
475 // and a NULL callback; value() functions will work on this, but not | |
476 // Get() or Set() calls. | |
477 BluetoothDeviceClient::Properties device_properties( | |
478 NULL, BluetoothDeviceClient::Properties::PropertyChangedCallback()); | |
479 if (!device_properties.UpdatePropertiesFromReader(&reader)) { | |
480 LOG(WARNING) << object_path.value() | |
481 << ": DeviceFound signal has incorrect parameters: " | |
482 << signal->ToString(); | |
483 return; | |
484 } | |
485 | |
486 DVLOG(1) << object_path.value() << ": Device found: " << address; | |
487 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer, observers_, | |
488 DeviceFound(object_path, address, device_properties)); | |
489 } | |
490 | |
491 // Called by dbus:: when the DeviceFound signal is initially connected. | |
492 void DeviceFoundConnected(const dbus::ObjectPath& object_path, | |
493 const std::string& interface_name, | |
494 const std::string& signal_name, | |
495 bool success) { | |
496 LOG_IF(WARNING, !success) << object_path.value() | |
497 << ": Failed to connect to DeviceFound signal."; | |
498 } | |
499 | |
500 // Called by dbus:: when a DeviceDisappeared signal is received. | |
501 void DeviceDisappearedReceived(const dbus::ObjectPath& object_path, | |
502 dbus::Signal* signal) { | |
503 DCHECK(signal); | |
504 dbus::MessageReader reader(signal); | |
505 std::string address; | |
506 if (!reader.PopString(&address)) { | |
507 LOG(WARNING) << object_path.value() | |
508 << ": DeviceDisappeared signal has incorrect parameters: " | |
509 << signal->ToString(); | |
510 return; | |
511 } | |
512 | |
513 DVLOG(1) << object_path.value() << ": Device disappeared: " << address; | |
514 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer, observers_, | |
515 DeviceDisappeared(object_path, address)); | |
516 } | |
517 | |
518 // Called by dbus:: when the DeviceDisappeared signal is initially connected. | |
519 void DeviceDisappearedConnected(const dbus::ObjectPath& object_path, | |
520 const std::string& interface_name, | |
521 const std::string& signal_name, | |
522 bool success) { | |
523 LOG_IF(WARNING, !success) | |
524 << object_path.value() | |
525 << ": Failed to connect to DeviceDisappeared signal."; | |
526 } | |
527 | |
528 // Called when a response for RequestSession() is received. | |
529 void OnRequestSession(const dbus::ObjectPath& object_path, | |
530 const AdapterCallback& callback, | |
531 dbus::Response* response) { | |
532 LOG_IF(WARNING, !response) << object_path.value() | |
533 << ": OnRequestSession: failed."; | |
534 callback.Run(object_path, response); | |
535 } | |
536 | |
537 // Called when a response for ReleaseSession() is received. | |
538 void OnReleaseSession(const dbus::ObjectPath& object_path, | |
539 const AdapterCallback& callback, | |
540 dbus::Response* response) { | |
541 LOG_IF(WARNING, !response) << object_path.value() | |
542 << ": OnReleaseSession: failed."; | |
543 callback.Run(object_path, response); | |
544 } | |
545 | |
546 // Called when a response for StartDiscovery() is received. | |
547 void OnStartDiscovery(const dbus::ObjectPath& object_path, | |
548 const AdapterCallback& callback, | |
549 dbus::Response* response) { | |
550 LOG_IF(WARNING, !response) << object_path.value() | |
551 << ": OnStartDiscovery: failed."; | |
552 callback.Run(object_path, response); | |
553 } | |
554 | |
555 // Called when a response for StopDiscovery() is received. | |
556 void OnStopDiscovery(const dbus::ObjectPath& object_path, | |
557 const AdapterCallback& callback, | |
558 dbus::Response* response) { | |
559 LOG_IF(WARNING, !response) << object_path.value() | |
560 << ": OnStopDiscovery: failed."; | |
561 callback.Run(object_path, response); | |
562 } | |
563 | |
564 // Called when a response for FindDevice() is received. | |
565 void OnFindDevice(const dbus::ObjectPath& object_path, | |
566 const DeviceCallback& callback, | |
567 dbus::Response* response) { | |
568 // Parse response. | |
569 bool success = false; | |
570 dbus::ObjectPath device_path; | |
571 if (response != NULL) { | |
572 dbus::MessageReader reader(response); | |
573 if (!reader.PopObjectPath(&device_path)) { | |
574 LOG(WARNING) << "FindDevice response has incorrect parameters: " | |
575 << response->ToString(); | |
576 } else { | |
577 success = true; | |
578 } | |
579 } else { | |
580 LOG(WARNING) << "Failed to find device."; | |
581 } | |
582 | |
583 // Notify client. | |
584 callback.Run(device_path, success); | |
585 } | |
586 | |
587 // Called when a response for CreateDevice() is received. | |
588 void OnCreateDevice(const dbus::ObjectPath& object_path, | |
589 const DeviceCallback& callback, | |
590 dbus::Response* response) { | |
591 // Parse response. | |
592 bool success = false; | |
593 dbus::ObjectPath device_path; | |
594 if (response != NULL) { | |
595 dbus::MessageReader reader(response); | |
596 if (!reader.PopObjectPath(&device_path)) { | |
597 LOG(WARNING) << "CreateDevice response has incorrect parameters: " | |
598 << response->ToString(); | |
599 } else { | |
600 success = true; | |
601 } | |
602 } else { | |
603 LOG(WARNING) << "Failed to create device."; | |
604 } | |
605 | |
606 // Notify client. | |
607 callback.Run(device_path, success); | |
608 } | |
609 | |
610 // Called when a response for CreatePairedDevice() is received. | |
611 void OnCreatePairedDevice(const dbus::ObjectPath& object_path, | |
612 const DeviceCallback& callback, | |
613 dbus::Response* response) { | |
614 // Parse response. | |
615 bool success = false; | |
616 dbus::ObjectPath device_path; | |
617 if (response != NULL) { | |
618 dbus::MessageReader reader(response); | |
619 if (!reader.PopObjectPath(&device_path)) { | |
620 LOG(WARNING) << "CreatePairedDevice response has incorrect parameters: " | |
621 << response->ToString(); | |
622 } else { | |
623 success = true; | |
624 } | |
625 } else { | |
626 LOG(WARNING) << "Failed to create paired device."; | |
627 } | |
628 | |
629 // Notify client. | |
630 callback.Run(device_path, success); | |
631 } | |
632 | |
633 // Called when a response for CancelDeviceCreation() is received. | |
634 void OnCancelDeviceCreation(const dbus::ObjectPath& object_path, | |
635 const AdapterCallback& callback, | |
636 dbus::Response* response) { | |
637 LOG_IF(WARNING, !response) << object_path.value() | |
638 << ": OnCancelDeviceCreation: failed."; | |
639 callback.Run(object_path, response); | |
640 } | |
641 | |
642 // Called when a response for RemoveDevice() is received. | |
643 void OnRemoveDevice(const dbus::ObjectPath& object_path, | |
644 const AdapterCallback& callback, | |
645 dbus::Response* response) { | |
646 LOG_IF(WARNING, !response) << object_path.value() | |
647 << ": OnRemoveDevice: failed."; | |
648 callback.Run(object_path, response); | |
649 } | |
650 | |
651 // Called when a response for RegisterAgent() is received. | |
652 void OnRegisterAgent(const dbus::ObjectPath& object_path, | |
653 const AdapterCallback& callback, | |
654 dbus::Response* response) { | |
655 LOG_IF(WARNING, !response) << object_path.value() | |
656 << ": OnRegisterAgent: failed."; | |
657 callback.Run(object_path, response); | |
658 } | |
659 | |
660 // Called when a response for UnregisterAgent() is received. | |
661 void OnUnregisterAgent(const dbus::ObjectPath& object_path, | |
662 const AdapterCallback& callback, | |
663 dbus::Response* response) { | |
664 LOG_IF(WARNING, !response) << object_path.value() | |
665 << ": OnUnregisterAgent: failed."; | |
666 callback.Run(object_path, response); | |
667 } | |
668 | |
669 // Weak pointer factory for generating 'this' pointers that might live longer | |
670 // than we do. | |
671 base::WeakPtrFactory<BluetoothAdapterClientImpl> weak_ptr_factory_; | |
672 | |
673 dbus::Bus* bus_; | |
674 | |
675 // List of observers interested in event notifications from us. | |
676 ObserverList<BluetoothAdapterClient::Observer> observers_; | |
677 | |
678 DISALLOW_COPY_AND_ASSIGN(BluetoothAdapterClientImpl); | |
679 }; | |
680 | |
681 // The BluetoothAdapterClient implementation used on Linux desktop, which does | |
682 // nothing. | |
683 class BluetoothAdapterClientStubImpl : public BluetoothAdapterClient { | |
684 public: | |
685 // BluetoothAdapterClient override. | |
686 virtual void AddObserver(Observer* observer) OVERRIDE { | |
687 } | |
688 | |
689 // BluetoothAdapterClient override. | |
690 virtual void RemoveObserver(Observer* observer) OVERRIDE { | |
691 } | |
692 | |
693 // BluetoothAdapterClient override. | |
694 virtual Properties* GetProperties(const dbus::ObjectPath& object_path) | |
695 OVERRIDE { | |
696 VLOG(1) << "GetProperties: " << object_path.value(); | |
697 return NULL; | |
698 } | |
699 | |
700 // BluetoothAdapterClient override. | |
701 virtual void RequestSession(const dbus::ObjectPath& object_path, | |
702 const AdapterCallback& callback) OVERRIDE { | |
703 VLOG(1) << "RequestSession: " << object_path.value(); | |
704 callback.Run(object_path, false); | |
705 } | |
706 | |
707 // BluetoothAdapterClient override. | |
708 virtual void ReleaseSession(const dbus::ObjectPath& object_path, | |
709 const AdapterCallback& callback) OVERRIDE { | |
710 VLOG(1) << "ReleaseSession: " << object_path.value(); | |
711 callback.Run(object_path, false); | |
712 } | |
713 | |
714 // BluetoothAdapterClient override. | |
715 virtual void StartDiscovery(const dbus::ObjectPath& object_path, | |
716 const AdapterCallback& callback) OVERRIDE { | |
717 VLOG(1) << "StartDiscovery: " << object_path.value(); | |
718 callback.Run(object_path, false); | |
719 } | |
720 | |
721 // BluetoothAdapterClient override. | |
722 virtual void StopDiscovery(const dbus::ObjectPath& object_path, | |
723 const AdapterCallback& callback) OVERRIDE { | |
724 VLOG(1) << "StopDiscovery: " << object_path.value(); | |
725 callback.Run(object_path, false); | |
726 } | |
727 | |
728 // BluetoothAdapterClient override. | |
729 virtual void FindDevice(const dbus::ObjectPath& object_path, | |
730 const std::string& address, | |
731 const DeviceCallback& callback) OVERRIDE { | |
732 VLOG(1) << "FindDevice: " << object_path.value() << " " << address; | |
733 callback.Run(dbus::ObjectPath(), false); | |
734 } | |
735 | |
736 // BluetoothAdapterClient override. | |
737 virtual void CreateDevice(const dbus::ObjectPath& object_path, | |
738 const std::string& address, | |
739 const DeviceCallback& callback) OVERRIDE { | |
740 VLOG(1) << "CreateDevice: " << object_path.value() << " " << address; | |
741 callback.Run(dbus::ObjectPath(), false); | |
742 } | |
743 | |
744 // BluetoothAdapterClient override. | |
745 virtual void CreatePairedDevice(const dbus::ObjectPath& object_path, | |
746 const std::string& address, | |
747 const dbus::ObjectPath& agent_path, | |
748 const std::string& capability, | |
749 const DeviceCallback& callback) OVERRIDE { | |
750 VLOG(1) << "CreatePairedDevice: " << object_path.value() << " " << address | |
751 << " " << agent_path.value() << " " << capability; | |
752 callback.Run(dbus::ObjectPath(), false); | |
753 } | |
754 | |
755 // BluetoothAdapterClient override. | |
756 virtual void CancelDeviceCreation(const dbus::ObjectPath& object_path, | |
757 const std::string& address, | |
758 const AdapterCallback& callback) OVERRIDE { | |
759 VLOG(1) << "CancelDeviceCreation: " << object_path.value() | |
760 << " " << address; | |
761 callback.Run(object_path, false); | |
762 } | |
763 | |
764 // BluetoothAdapterClient override. | |
765 virtual void RemoveDevice(const dbus::ObjectPath& object_path, | |
766 const dbus::ObjectPath& device_path, | |
767 const AdapterCallback& callback) OVERRIDE { | |
768 VLOG(1) << "RemoveDevice: " << object_path.value() | |
769 << " " << device_path.value(); | |
770 callback.Run(object_path, false); | |
771 } | |
772 | |
773 // BluetoothAdapterClient override. | |
774 virtual void RegisterAgent(const dbus::ObjectPath& object_path, | |
775 const dbus::ObjectPath& agent_path, | |
776 const std::string& capability, | |
777 const AdapterCallback& callback) OVERRIDE { | |
778 VLOG(1) << "RegisterAgent: " << object_path.value() | |
779 << " " << agent_path.value(); | |
780 callback.Run(object_path, false); | |
781 } | |
782 | |
783 // BluetoothAdapterClient override. | |
784 virtual void UnregisterAgent(const dbus::ObjectPath& object_path, | |
785 const dbus::ObjectPath& agent_path, | |
786 const AdapterCallback& callback) OVERRIDE { | |
787 VLOG(1) << "UnregisterAgent: " << object_path.value() | |
788 << " " << agent_path.value(); | |
789 callback.Run(object_path, false); | |
790 } | |
791 }; | |
792 | |
793 BluetoothAdapterClient::BluetoothAdapterClient() { | |
794 } | |
795 | |
796 BluetoothAdapterClient::~BluetoothAdapterClient() { | |
797 } | |
798 | |
799 BluetoothAdapterClient* BluetoothAdapterClient::Create( | |
800 DBusClientImplementationType type, | |
801 dbus::Bus* bus, | |
802 BluetoothManagerClient* manager_client) { | |
803 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) | |
804 return new BluetoothAdapterClientImpl(bus, manager_client); | |
805 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); | |
806 return new BluetoothAdapterClientStubImpl(); | |
807 } | |
808 | |
809 } // namespace chromeos | |
OLD | NEW |