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_manager_client.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/logging.h" | |
9 #include "chrome/browser/chromeos/dbus/bluetooth_property.h" | |
10 #include "dbus/bus.h" | |
11 #include "dbus/message.h" | |
12 #include "dbus/object_path.h" | |
13 #include "dbus/object_proxy.h" | |
14 #include "third_party/cros_system_api/dbus/service_constants.h" | |
15 | |
16 namespace chromeos { | |
17 | |
18 BluetoothManagerClient::Properties::Properties(dbus::ObjectProxy* object_proxy, | |
19 PropertyChangedCallback callback) | |
20 : BluetoothPropertySet(object_proxy, | |
21 bluetooth_manager::kBluetoothManagerInterface, | |
22 callback) { | |
23 RegisterProperty(bluetooth_manager::kAdaptersProperty, &adapters); | |
24 } | |
25 | |
26 BluetoothManagerClient::Properties::~Properties() { | |
27 } | |
28 | |
29 | |
30 // The BluetoothManagerClient implementation used in production. | |
31 class BluetoothManagerClientImpl : public BluetoothManagerClient { | |
32 public: | |
33 explicit BluetoothManagerClientImpl(dbus::Bus* bus) | |
34 : weak_ptr_factory_(this), | |
35 object_proxy_(NULL) { | |
36 DVLOG(1) << "Creating BluetoothManagerClientImpl"; | |
37 | |
38 // Create the object proxy. | |
39 DCHECK(bus); | |
40 object_proxy_ = bus->GetObjectProxy( | |
41 bluetooth_manager::kBluetoothManagerServiceName, | |
42 dbus::ObjectPath(bluetooth_manager::kBluetoothManagerServicePath)); | |
43 | |
44 object_proxy_->ConnectToSignal( | |
45 bluetooth_manager::kBluetoothManagerInterface, | |
46 bluetooth_manager::kAdapterAddedSignal, | |
47 base::Bind(&BluetoothManagerClientImpl::AdapterAddedReceived, | |
48 weak_ptr_factory_.GetWeakPtr()), | |
49 base::Bind(&BluetoothManagerClientImpl::AdapterAddedConnected, | |
50 weak_ptr_factory_.GetWeakPtr())); | |
51 | |
52 object_proxy_->ConnectToSignal( | |
53 bluetooth_manager::kBluetoothManagerInterface, | |
54 bluetooth_manager::kAdapterRemovedSignal, | |
55 base::Bind(&BluetoothManagerClientImpl::AdapterRemovedReceived, | |
56 weak_ptr_factory_.GetWeakPtr()), | |
57 base::Bind(&BluetoothManagerClientImpl::AdapterRemovedConnected, | |
58 weak_ptr_factory_.GetWeakPtr())); | |
59 | |
60 object_proxy_->ConnectToSignal( | |
61 bluetooth_manager::kBluetoothManagerInterface, | |
62 bluetooth_manager::kDefaultAdapterChangedSignal, | |
63 base::Bind(&BluetoothManagerClientImpl::DefaultAdapterChangedReceived, | |
64 weak_ptr_factory_.GetWeakPtr()), | |
65 base::Bind(&BluetoothManagerClientImpl::DefaultAdapterChangedConnected, | |
66 weak_ptr_factory_.GetWeakPtr())); | |
67 | |
68 // Create the properties structure. | |
69 properties_ = new Properties( | |
70 object_proxy_, | |
71 base::Bind(&BluetoothManagerClientImpl::OnPropertyChanged, | |
72 weak_ptr_factory_.GetWeakPtr())); | |
73 | |
74 properties_->ConnectSignals(); | |
75 properties_->GetAll(); | |
76 } | |
77 | |
78 virtual ~BluetoothManagerClientImpl() { | |
79 // Clean up the Properties structure. | |
80 delete properties_; | |
81 } | |
82 | |
83 // BluetoothManagerClient override. | |
84 virtual void AddObserver(Observer* observer) OVERRIDE { | |
85 DCHECK(observer); | |
86 observers_.AddObserver(observer); | |
87 } | |
88 | |
89 // BluetoothManagerClient override. | |
90 virtual void RemoveObserver(Observer* observer) OVERRIDE { | |
91 DCHECK(observer); | |
92 observers_.RemoveObserver(observer); | |
93 } | |
94 | |
95 // BluetoothManagerClient override. | |
96 virtual Properties* GetProperties() OVERRIDE { | |
97 return properties_; | |
98 } | |
99 | |
100 // BluetoothManagerClient override. | |
101 virtual void DefaultAdapter(const AdapterCallback& callback) OVERRIDE { | |
102 dbus::MethodCall method_call( | |
103 bluetooth_manager::kBluetoothManagerInterface, | |
104 bluetooth_manager::kDefaultAdapter); | |
105 | |
106 DCHECK(object_proxy_); | |
107 object_proxy_->CallMethod( | |
108 &method_call, | |
109 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
110 base::Bind(&BluetoothManagerClientImpl::OnDefaultAdapter, | |
111 weak_ptr_factory_.GetWeakPtr(), callback)); | |
112 } | |
113 | |
114 // BluetoothManagerClient override. | |
115 virtual void FindAdapter(const std::string& address, | |
116 const AdapterCallback& callback) { | |
117 dbus::MethodCall method_call( | |
118 bluetooth_manager::kBluetoothManagerInterface, | |
119 bluetooth_manager::kFindAdapter); | |
120 | |
121 dbus::MessageWriter writer(&method_call); | |
122 writer.AppendString(address); | |
123 | |
124 DCHECK(object_proxy_); | |
125 object_proxy_->CallMethod( | |
126 &method_call, | |
127 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
128 base::Bind(&BluetoothManagerClientImpl::OnFindAdapter, | |
129 weak_ptr_factory_.GetWeakPtr(), callback)); | |
130 } | |
131 | |
132 private: | |
133 // Called by BluetoothPropertySet when a property value is changed, | |
134 // either by result of a signal or response to a GetAll() or Get() | |
135 // call. Informs observers. | |
136 void OnPropertyChanged(const std::string& property_name) { | |
137 FOR_EACH_OBSERVER(BluetoothManagerClient::Observer, observers_, | |
138 ManagerPropertyChanged(property_name)); | |
139 } | |
140 | |
141 // Called by dbus:: when an AdapterAdded signal is received. | |
142 void AdapterAddedReceived(dbus::Signal* signal) { | |
143 DCHECK(signal); | |
144 dbus::MessageReader reader(signal); | |
145 dbus::ObjectPath object_path; | |
146 if (!reader.PopObjectPath(&object_path)) { | |
147 LOG(WARNING) << "AdapterAdded signal has incorrect parameters: " | |
148 << signal->ToString(); | |
149 return; | |
150 } | |
151 | |
152 DVLOG(1) << "Adapter added: " << object_path.value(); | |
153 FOR_EACH_OBSERVER(Observer, observers_, AdapterAdded(object_path)); | |
154 } | |
155 | |
156 // Called by dbus:: when the AdapterAdded signal is initially connected. | |
157 void AdapterAddedConnected(const std::string& interface_name, | |
158 const std::string& signal_name, | |
159 bool success) { | |
160 LOG_IF(WARNING, !success) << "Failed to connect to AdapterAdded signal."; | |
161 } | |
162 | |
163 // Called by dbus:: when an AdapterRemoved signal is received. | |
164 void AdapterRemovedReceived(dbus::Signal* signal) { | |
165 DCHECK(signal); | |
166 dbus::MessageReader reader(signal); | |
167 dbus::ObjectPath object_path; | |
168 if (!reader.PopObjectPath(&object_path)) { | |
169 LOG(WARNING) << "AdapterRemoved signal has incorrect parameters: " | |
170 << signal->ToString(); | |
171 return; | |
172 } | |
173 | |
174 DVLOG(1) << "Adapter removed: " << object_path.value(); | |
175 FOR_EACH_OBSERVER(Observer, observers_, AdapterRemoved(object_path)); | |
176 } | |
177 | |
178 // Called by dbus:: when the AdapterRemoved signal is initially connected. | |
179 void AdapterRemovedConnected(const std::string& interface_name, | |
180 const std::string& signal_name, | |
181 bool success) { | |
182 LOG_IF(WARNING, !success) << "Failed to connect to AdapterRemoved signal."; | |
183 } | |
184 | |
185 // Called by dbus:: when a DefaultAdapterChanged signal is received. | |
186 void DefaultAdapterChangedReceived(dbus::Signal* signal) { | |
187 DCHECK(signal); | |
188 dbus::MessageReader reader(signal); | |
189 dbus::ObjectPath object_path; | |
190 if (!reader.PopObjectPath(&object_path)) { | |
191 LOG(WARNING) << "DefaultAdapterChanged signal has incorrect parameters: " | |
192 << signal->ToString(); | |
193 return; | |
194 } | |
195 | |
196 DVLOG(1) << "Default adapter changed: " << object_path.value(); | |
197 FOR_EACH_OBSERVER(Observer, observers_, DefaultAdapterChanged(object_path)); | |
198 } | |
199 | |
200 // Called by dbus:: when the DefaultAdapterChanged signal is initially | |
201 // connected. | |
202 void DefaultAdapterChangedConnected(const std::string& interface_name, | |
203 const std::string& signal_name, | |
204 bool success) { | |
205 LOG_IF(WARNING, !success) | |
206 << "Failed to connect to DefaultAdapterChanged signal."; | |
207 } | |
208 | |
209 // Called when a response for DefaultAdapter() is received. | |
210 void OnDefaultAdapter(const AdapterCallback& callback, | |
211 dbus::Response* response) { | |
212 // Parse response. | |
213 bool success = false; | |
214 dbus::ObjectPath object_path; | |
215 if (response != NULL) { | |
216 dbus::MessageReader reader(response); | |
217 if (!reader.PopObjectPath(&object_path)) { | |
218 LOG(WARNING) << "DefaultAdapter response has incorrect parameters: " | |
219 << response->ToString(); | |
220 } else { | |
221 success = true; | |
222 } | |
223 } else { | |
224 LOG(WARNING) << "Failed to get default adapter."; | |
225 } | |
226 | |
227 // Notify client. | |
228 callback.Run(object_path, success); | |
229 } | |
230 | |
231 // Called when a response for FindAdapter() is received. | |
232 void OnFindAdapter(const AdapterCallback& callback, | |
233 dbus::Response* response) { | |
234 // Parse response. | |
235 bool success = false; | |
236 dbus::ObjectPath object_path; | |
237 if (response != NULL) { | |
238 dbus::MessageReader reader(response); | |
239 if (!reader.PopObjectPath(&object_path)) { | |
240 LOG(WARNING) << "FindAdapter response has incorrect parameters: " | |
241 << response->ToString(); | |
242 } else { | |
243 success = true; | |
244 } | |
245 } else { | |
246 LOG(WARNING) << "Failed to find adapter."; | |
247 } | |
248 | |
249 // Notify client. | |
250 callback.Run(object_path, success); | |
251 } | |
252 | |
253 // Weak pointer factory for generating 'this' pointers that might live longer | |
254 // than we do. | |
255 base::WeakPtrFactory<BluetoothManagerClientImpl> weak_ptr_factory_; | |
256 | |
257 // D-Bus proxy for BlueZ Manager interface. | |
258 dbus::ObjectProxy* object_proxy_; | |
259 | |
260 // Properties for BlueZ Manager interface. | |
261 Properties* properties_; | |
262 | |
263 // List of observers interested in event notifications from us. | |
264 ObserverList<Observer> observers_; | |
265 | |
266 DISALLOW_COPY_AND_ASSIGN(BluetoothManagerClientImpl); | |
267 }; | |
268 | |
269 // The BluetoothManagerClient implementation used on Linux desktop, which does | |
270 // nothing. | |
271 class BluetoothManagerClientStubImpl : public BluetoothManagerClient { | |
272 public: | |
273 // BluetoothManagerClient override. | |
274 virtual void AddObserver(Observer* observer) OVERRIDE { | |
275 } | |
276 | |
277 // BluetoothManagerClient override. | |
278 virtual void RemoveObserver(Observer* observer) OVERRIDE { | |
279 } | |
280 | |
281 // BluetoothManagerClient override. | |
282 virtual Properties* GetProperties() OVERRIDE { | |
283 VLOG(1) << "GetProperties"; | |
284 return NULL; | |
285 } | |
286 | |
287 // BluetoothManagerClient override. | |
288 virtual void DefaultAdapter(const AdapterCallback& callback) OVERRIDE { | |
289 VLOG(1) << "DefaultAdapter."; | |
290 callback.Run(dbus::ObjectPath(), false); | |
291 } | |
292 | |
293 // BluetoothManagerClient override. | |
294 virtual void FindAdapter(const std::string& address, | |
295 const AdapterCallback& callback) { | |
296 VLOG(1) << "FindAdapter: " << address; | |
297 callback.Run(dbus::ObjectPath(), false); | |
298 } | |
299 }; | |
300 | |
301 BluetoothManagerClient::BluetoothManagerClient() { | |
302 } | |
303 | |
304 BluetoothManagerClient::~BluetoothManagerClient() { | |
305 } | |
306 | |
307 BluetoothManagerClient* BluetoothManagerClient::Create( | |
308 DBusClientImplementationType type, | |
309 dbus::Bus* bus) { | |
310 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) | |
311 return new BluetoothManagerClientImpl(bus); | |
312 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); | |
313 return new BluetoothManagerClientStubImpl(); | |
314 } | |
315 | |
316 } // namespace chromeos | |
OLD | NEW |