OLD | NEW |
| (Empty) |
1 // Copyright 2015 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 "chromeos/dbus/ap_manager_client.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/location.h" | |
9 #include "base/logging.h" | |
10 #include "base/macros.h" | |
11 #include "base/memory/weak_ptr.h" | |
12 #include "base/observer_list.h" | |
13 #include "base/single_thread_task_runner.h" | |
14 #include "base/threading/thread_task_runner_handle.h" | |
15 #include "dbus/bus.h" | |
16 #include "dbus/message.h" | |
17 #include "dbus/object_manager.h" | |
18 #include "dbus/object_proxy.h" | |
19 #include "dbus/values_util.h" | |
20 | |
21 namespace chromeos { | |
22 | |
23 // TODO(benchan): Move these constants to system_api. | |
24 namespace apmanager { | |
25 const char kApManagerServiceName[] = "org.chromium.apmanager"; | |
26 const char kApManagerServicePath[] = "/org/chromium/apmanager"; | |
27 const char kApManagerManagerPath[] = "/org/chromium/apmanager/Manager"; | |
28 const char kManagerInterfaceName[] = "org.chromium.apmanager.Manager"; | |
29 const char kConfigInterfaceName[] = "org.chromium.apmanager.Config"; | |
30 const char kDeviceInterfaceName[] = "org.chromium.apmanager.Device"; | |
31 const char kServiceInterfaceName[] = "org.chromium.apmanager.Service"; | |
32 const char kCreateServiceMethod[] = "CreateService"; | |
33 const char kRemoveServiceMethod[] = "RemoveService"; | |
34 const char kStartMethod[] = "Start"; | |
35 const char kStopMethod[] = "Stop"; | |
36 const char kSsidProperty[] = "Ssid"; | |
37 const char kInterfaceNameProperty[] = "InterfaceName"; | |
38 const char kSecurityModeProperty[] = "SecurityMode"; | |
39 const char kPassphraseProperty[] = "Passphrase"; | |
40 const char kHwModeProperty[] = "HwMode"; | |
41 const char kOperationModeProperty[] = "OperationMode"; | |
42 const char kChannelProperty[] = "Channel"; | |
43 const char kHiddenNetworkProperty[] = "HiddenNetwork"; | |
44 const char kBridgeInterfaceProperty[] = "BridgeInterface"; | |
45 const char kServiceAddressIndexProperty[] = "ServerAddressIndex"; | |
46 const char kDeviceNameProperty[] = "DeviceName"; | |
47 const char kInUsedProperty[] = "InUsed"; | |
48 const char kPreferredApInterfaceProperty[] = "PreferredApInterface"; | |
49 const char kConfigName[] = "Config"; | |
50 const char kStateName[] = "State"; | |
51 | |
52 } // namespace apmanager | |
53 | |
54 namespace { | |
55 | |
56 // Since there is no property associated with Manager objects, an empty callback | |
57 // is used. | |
58 void ManagerPropertyChanged(const std::string& property_name) { | |
59 } | |
60 | |
61 // The ApManagerClient implementation used in production. | |
62 class ApManagerClientImpl : public ApManagerClient, | |
63 public dbus::ObjectManager::Interface { | |
64 public: | |
65 ApManagerClientImpl(); | |
66 ~ApManagerClientImpl() override; | |
67 | |
68 // ApManagerClient overrides. | |
69 void AddObserver(Observer* observer) override; | |
70 void RemoveObserver(Observer* observer) override; | |
71 void CreateService(const ObjectPathDBusMethodCallback& callback) override; | |
72 void RemoveService(const dbus::ObjectPath& object_path, | |
73 const VoidDBusMethodCallback& callback) override; | |
74 void StartService(const dbus::ObjectPath& object_path, | |
75 const VoidDBusMethodCallback& callback) override; | |
76 void StopService(const dbus::ObjectPath& object_path, | |
77 const VoidDBusMethodCallback& callback) override; | |
78 ConfigProperties* GetConfigProperties( | |
79 const dbus::ObjectPath& object_path) override; | |
80 const DeviceProperties* GetDeviceProperties( | |
81 const dbus::ObjectPath& object_path) override; | |
82 const ServiceProperties* GetServiceProperties( | |
83 const dbus::ObjectPath& object_path) override; | |
84 | |
85 // DBusClient overrides. | |
86 void Init(dbus::Bus* bus) override; | |
87 | |
88 // dbus::ObjectManager::Interface overrides. | |
89 dbus::PropertySet* CreateProperties( | |
90 dbus::ObjectProxy* object_proxy, | |
91 const dbus::ObjectPath& object_path, | |
92 const std::string& interface_name) override; | |
93 void ObjectAdded(const dbus::ObjectPath& object_path, | |
94 const std::string& interface_name) override; | |
95 void ObjectRemoved(const dbus::ObjectPath& object_path, | |
96 const std::string& interface_name) override; | |
97 | |
98 private: | |
99 // Called by dbus::PropertySet when a property value is changed, | |
100 // either by result of a signal or response to a GetAll() or Get() | |
101 // call. Informs observers. | |
102 void OnConfigPropertyChanged(const dbus::ObjectPath& object_path, | |
103 const std::string& property_name); | |
104 void OnDevicePropertyChanged(const dbus::ObjectPath& object_path, | |
105 const std::string& property_name); | |
106 void OnServicePropertyChanged(const dbus::ObjectPath& object_path, | |
107 const std::string& property_name); | |
108 | |
109 void OnObjectPathDBusMethod(const ObjectPathDBusMethodCallback& callback, | |
110 dbus::Response* response); | |
111 void OnStringDBusMethod(const StringDBusMethodCallback& callback, | |
112 dbus::Response* response); | |
113 void OnVoidDBusMethod(const VoidDBusMethodCallback& callback, | |
114 dbus::Response* response); | |
115 | |
116 // List of observers interested in event notifications from us. | |
117 base::ObserverList<Observer> observers_; | |
118 dbus::ObjectManager* object_manager_; | |
119 base::WeakPtrFactory<ApManagerClientImpl> weak_ptr_factory_; | |
120 | |
121 DISALLOW_COPY_AND_ASSIGN(ApManagerClientImpl); | |
122 }; | |
123 | |
124 ApManagerClientImpl::ApManagerClientImpl() | |
125 : object_manager_(nullptr), weak_ptr_factory_(this) { | |
126 } | |
127 | |
128 ApManagerClientImpl::~ApManagerClientImpl() { | |
129 if (object_manager_) { | |
130 object_manager_->UnregisterInterface(apmanager::kManagerInterfaceName); | |
131 object_manager_->UnregisterInterface(apmanager::kConfigInterfaceName); | |
132 object_manager_->UnregisterInterface(apmanager::kDeviceInterfaceName); | |
133 object_manager_->UnregisterInterface(apmanager::kServiceInterfaceName); | |
134 } | |
135 } | |
136 | |
137 void ApManagerClientImpl::AddObserver(Observer* observer) { | |
138 DCHECK(observer); | |
139 observers_.AddObserver(observer); | |
140 } | |
141 | |
142 void ApManagerClientImpl::RemoveObserver(Observer* observer) { | |
143 DCHECK(observer); | |
144 observers_.RemoveObserver(observer); | |
145 } | |
146 | |
147 void ApManagerClientImpl::CreateService( | |
148 const ObjectPathDBusMethodCallback& callback) { | |
149 dbus::ObjectProxy* object_proxy = object_manager_->GetObjectProxy( | |
150 dbus::ObjectPath(apmanager::kApManagerManagerPath)); | |
151 if (!object_proxy) { | |
152 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
153 FROM_HERE, | |
154 base::Bind(&ApManagerClientImpl::OnObjectPathDBusMethod, | |
155 weak_ptr_factory_.GetWeakPtr(), callback, nullptr)); | |
156 return; | |
157 } | |
158 | |
159 dbus::MethodCall method_call(apmanager::kManagerInterfaceName, | |
160 apmanager::kCreateServiceMethod); | |
161 dbus::MessageWriter writer(&method_call); | |
162 object_proxy->CallMethod( | |
163 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
164 base::Bind(&ApManagerClientImpl::OnObjectPathDBusMethod, | |
165 weak_ptr_factory_.GetWeakPtr(), callback)); | |
166 } | |
167 | |
168 void ApManagerClientImpl::RemoveService( | |
169 const dbus::ObjectPath& object_path, | |
170 const VoidDBusMethodCallback& callback) { | |
171 dbus::ObjectProxy* object_proxy = object_manager_->GetObjectProxy( | |
172 dbus::ObjectPath(apmanager::kApManagerManagerPath)); | |
173 if (!object_proxy) { | |
174 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
175 FROM_HERE, | |
176 base::Bind(&ApManagerClientImpl::OnVoidDBusMethod, | |
177 weak_ptr_factory_.GetWeakPtr(), callback, nullptr)); | |
178 return; | |
179 } | |
180 | |
181 dbus::MethodCall method_call(apmanager::kManagerInterfaceName, | |
182 apmanager::kRemoveServiceMethod); | |
183 dbus::MessageWriter writer(&method_call); | |
184 writer.AppendObjectPath(object_path); | |
185 object_proxy->CallMethod( | |
186 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
187 base::Bind(&ApManagerClientImpl::OnVoidDBusMethod, | |
188 weak_ptr_factory_.GetWeakPtr(), callback)); | |
189 } | |
190 | |
191 void ApManagerClientImpl::StartService(const dbus::ObjectPath& object_path, | |
192 const VoidDBusMethodCallback& callback) { | |
193 dbus::ObjectProxy* object_proxy = | |
194 object_manager_->GetObjectProxy(object_path); | |
195 if (!object_proxy) { | |
196 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
197 FROM_HERE, | |
198 base::Bind(&ApManagerClientImpl::OnVoidDBusMethod, | |
199 weak_ptr_factory_.GetWeakPtr(), callback, nullptr)); | |
200 return; | |
201 } | |
202 | |
203 dbus::MethodCall method_call(apmanager::kServiceInterfaceName, | |
204 apmanager::kStartMethod); | |
205 dbus::MessageWriter writer(&method_call); | |
206 object_proxy->CallMethod( | |
207 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
208 base::Bind(&ApManagerClientImpl::OnVoidDBusMethod, | |
209 weak_ptr_factory_.GetWeakPtr(), callback)); | |
210 } | |
211 | |
212 void ApManagerClientImpl::StopService(const dbus::ObjectPath& object_path, | |
213 const VoidDBusMethodCallback& callback) { | |
214 dbus::ObjectProxy* object_proxy = | |
215 object_manager_->GetObjectProxy(object_path); | |
216 if (!object_proxy) { | |
217 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
218 FROM_HERE, | |
219 base::Bind(&ApManagerClientImpl::OnVoidDBusMethod, | |
220 weak_ptr_factory_.GetWeakPtr(), callback, nullptr)); | |
221 return; | |
222 } | |
223 | |
224 dbus::MethodCall method_call(apmanager::kServiceInterfaceName, | |
225 apmanager::kStopMethod); | |
226 dbus::MessageWriter writer(&method_call); | |
227 object_proxy->CallMethod( | |
228 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
229 base::Bind(&ApManagerClientImpl::OnVoidDBusMethod, | |
230 weak_ptr_factory_.GetWeakPtr(), callback)); | |
231 } | |
232 | |
233 ApManagerClient::ConfigProperties* ApManagerClientImpl::GetConfigProperties( | |
234 const dbus::ObjectPath& object_path) { | |
235 return static_cast<ConfigProperties*>(object_manager_->GetProperties( | |
236 object_path, apmanager::kConfigInterfaceName)); | |
237 } | |
238 | |
239 const ApManagerClient::DeviceProperties* | |
240 ApManagerClientImpl::GetDeviceProperties(const dbus::ObjectPath& object_path) { | |
241 return static_cast<DeviceProperties*>(object_manager_->GetProperties( | |
242 object_path, apmanager::kDeviceInterfaceName)); | |
243 } | |
244 | |
245 const ApManagerClient::ServiceProperties* | |
246 ApManagerClientImpl::GetServiceProperties(const dbus::ObjectPath& object_path) { | |
247 return static_cast<ServiceProperties*>(object_manager_->GetProperties( | |
248 object_path, apmanager::kServiceInterfaceName)); | |
249 } | |
250 | |
251 void ApManagerClientImpl::Init(dbus::Bus* bus) { | |
252 object_manager_ = | |
253 bus->GetObjectManager(apmanager::kApManagerServiceName, | |
254 dbus::ObjectPath(apmanager::kApManagerServicePath)); | |
255 object_manager_->RegisterInterface(apmanager::kManagerInterfaceName, this); | |
256 object_manager_->RegisterInterface(apmanager::kConfigInterfaceName, this); | |
257 object_manager_->RegisterInterface(apmanager::kDeviceInterfaceName, this); | |
258 object_manager_->RegisterInterface(apmanager::kServiceInterfaceName, this); | |
259 } | |
260 | |
261 dbus::PropertySet* ApManagerClientImpl::CreateProperties( | |
262 dbus::ObjectProxy* object_proxy, | |
263 const dbus::ObjectPath& object_path, | |
264 const std::string& interface_name) { | |
265 dbus::PropertySet* properties = nullptr; | |
266 if (interface_name == apmanager::kManagerInterfaceName) { | |
267 properties = new dbus::PropertySet(object_proxy, interface_name, | |
268 base::Bind(&ManagerPropertyChanged)); | |
269 } else if (interface_name == apmanager::kConfigInterfaceName) { | |
270 properties = new ConfigProperties( | |
271 object_proxy, interface_name, | |
272 base::Bind(&ApManagerClientImpl::OnConfigPropertyChanged, | |
273 weak_ptr_factory_.GetWeakPtr(), object_path)); | |
274 } else if (interface_name == apmanager::kDeviceInterfaceName) { | |
275 properties = new DeviceProperties( | |
276 object_proxy, interface_name, | |
277 base::Bind(&ApManagerClientImpl::OnDevicePropertyChanged, | |
278 weak_ptr_factory_.GetWeakPtr(), object_path)); | |
279 } else if (interface_name == apmanager::kServiceInterfaceName) { | |
280 properties = new ServiceProperties( | |
281 object_proxy, interface_name, | |
282 base::Bind(&ApManagerClientImpl::OnServicePropertyChanged, | |
283 weak_ptr_factory_.GetWeakPtr(), object_path)); | |
284 } else { | |
285 NOTREACHED() << "Unhandled interface name " << interface_name; | |
286 } | |
287 return properties; | |
288 } | |
289 | |
290 void ApManagerClientImpl::ObjectAdded(const dbus::ObjectPath& object_path, | |
291 const std::string& interface_name) { | |
292 if (interface_name == apmanager::kManagerInterfaceName) { | |
293 FOR_EACH_OBSERVER(Observer, observers_, ManagerAdded()); | |
294 } else if (interface_name == apmanager::kConfigInterfaceName) { | |
295 FOR_EACH_OBSERVER(Observer, observers_, ConfigAdded(object_path)); | |
296 } else if (interface_name == apmanager::kDeviceInterfaceName) { | |
297 FOR_EACH_OBSERVER(Observer, observers_, DeviceAdded(object_path)); | |
298 } else if (interface_name == apmanager::kServiceInterfaceName) { | |
299 FOR_EACH_OBSERVER(Observer, observers_, ServiceAdded(object_path)); | |
300 } else { | |
301 NOTREACHED() << "Unhandled interface name " << interface_name; | |
302 } | |
303 } | |
304 | |
305 void ApManagerClientImpl::ObjectRemoved(const dbus::ObjectPath& object_path, | |
306 const std::string& interface_name) { | |
307 if (interface_name == apmanager::kManagerInterfaceName) { | |
308 FOR_EACH_OBSERVER(Observer, observers_, ManagerRemoved()); | |
309 } else if (interface_name == apmanager::kConfigInterfaceName) { | |
310 FOR_EACH_OBSERVER(Observer, observers_, ConfigRemoved(object_path)); | |
311 } else if (interface_name == apmanager::kDeviceInterfaceName) { | |
312 FOR_EACH_OBSERVER(Observer, observers_, DeviceRemoved(object_path)); | |
313 } else if (interface_name == apmanager::kServiceInterfaceName) { | |
314 FOR_EACH_OBSERVER(Observer, observers_, ServiceRemoved(object_path)); | |
315 } else { | |
316 NOTREACHED() << "Unhandled interface name " << interface_name; | |
317 } | |
318 } | |
319 | |
320 void ApManagerClientImpl::OnConfigPropertyChanged( | |
321 const dbus::ObjectPath& object_path, | |
322 const std::string& property_name) { | |
323 FOR_EACH_OBSERVER(Observer, observers_, | |
324 ConfigPropertyChanged(object_path, property_name)); | |
325 } | |
326 | |
327 void ApManagerClientImpl::OnDevicePropertyChanged( | |
328 const dbus::ObjectPath& object_path, | |
329 const std::string& property_name) { | |
330 FOR_EACH_OBSERVER(Observer, observers_, | |
331 ConfigPropertyChanged(object_path, property_name)); | |
332 } | |
333 | |
334 void ApManagerClientImpl::OnServicePropertyChanged( | |
335 const dbus::ObjectPath& object_path, | |
336 const std::string& property_name) { | |
337 FOR_EACH_OBSERVER(Observer, observers_, | |
338 ServicePropertyChanged(object_path, property_name)); | |
339 } | |
340 | |
341 void ApManagerClientImpl::OnObjectPathDBusMethod( | |
342 const ObjectPathDBusMethodCallback& callback, | |
343 dbus::Response* response) { | |
344 if (!response) { | |
345 callback.Run(DBUS_METHOD_CALL_FAILURE, dbus::ObjectPath()); | |
346 return; | |
347 } | |
348 | |
349 dbus::MessageReader reader(response); | |
350 dbus::ObjectPath result; | |
351 if (!reader.PopObjectPath(&result)) { | |
352 callback.Run(DBUS_METHOD_CALL_FAILURE, result); | |
353 return; | |
354 } | |
355 | |
356 callback.Run(DBUS_METHOD_CALL_SUCCESS, result); | |
357 } | |
358 | |
359 void ApManagerClientImpl::OnStringDBusMethod( | |
360 const StringDBusMethodCallback& callback, | |
361 dbus::Response* response) { | |
362 if (!response) { | |
363 callback.Run(DBUS_METHOD_CALL_FAILURE, std::string()); | |
364 return; | |
365 } | |
366 | |
367 dbus::MessageReader reader(response); | |
368 std::string result; | |
369 if (!reader.PopString(&result)) { | |
370 callback.Run(DBUS_METHOD_CALL_FAILURE, std::string()); | |
371 return; | |
372 } | |
373 | |
374 callback.Run(DBUS_METHOD_CALL_SUCCESS, result); | |
375 } | |
376 | |
377 void ApManagerClientImpl::OnVoidDBusMethod( | |
378 const VoidDBusMethodCallback& callback, | |
379 dbus::Response* response) { | |
380 callback.Run(response ? DBUS_METHOD_CALL_SUCCESS : DBUS_METHOD_CALL_FAILURE); | |
381 } | |
382 | |
383 } // namespace | |
384 | |
385 ApManagerClient::ConfigProperties::ConfigProperties( | |
386 dbus::ObjectProxy* object_proxy, | |
387 const std::string& dbus_interface_name, | |
388 const PropertyChangedCallback& callback) | |
389 : dbus::PropertySet(object_proxy, dbus_interface_name, callback) { | |
390 RegisterProperty(apmanager::kSsidProperty, &ssid_); | |
391 RegisterProperty(apmanager::kInterfaceNameProperty, &interface_name_); | |
392 RegisterProperty(apmanager::kSecurityModeProperty, &security_mode_); | |
393 RegisterProperty(apmanager::kPassphraseProperty, &passphrase_); | |
394 RegisterProperty(apmanager::kHwModeProperty, &hw_mode_); | |
395 RegisterProperty(apmanager::kOperationModeProperty, &operation_mode_); | |
396 RegisterProperty(apmanager::kChannelProperty, &channel_); | |
397 RegisterProperty(apmanager::kHiddenNetworkProperty, &hidden_network_); | |
398 RegisterProperty(apmanager::kBridgeInterfaceProperty, &bridge_interface_); | |
399 RegisterProperty(apmanager::kServiceAddressIndexProperty, | |
400 &server_address_index_); | |
401 } | |
402 | |
403 ApManagerClient::ConfigProperties::~ConfigProperties() { | |
404 } | |
405 | |
406 ApManagerClient::DeviceProperties::DeviceProperties( | |
407 dbus::ObjectProxy* object_proxy, | |
408 const std::string& interface_name, | |
409 const PropertyChangedCallback& callback) | |
410 : dbus::PropertySet(object_proxy, interface_name, callback) { | |
411 RegisterProperty(apmanager::kDeviceNameProperty, &device_name_); | |
412 RegisterProperty(apmanager::kInUsedProperty, &in_used_); | |
413 RegisterProperty(apmanager::kPreferredApInterfaceProperty, | |
414 &preferred_ap_interface_); | |
415 } | |
416 | |
417 ApManagerClient::DeviceProperties::~DeviceProperties() { | |
418 } | |
419 | |
420 ApManagerClient::ServiceProperties::ServiceProperties( | |
421 dbus::ObjectProxy* object_proxy, | |
422 const std::string& interface_name, | |
423 const PropertyChangedCallback& callback) | |
424 : dbus::PropertySet(object_proxy, interface_name, callback) { | |
425 RegisterProperty(apmanager::kConfigName, &config_); | |
426 RegisterProperty(apmanager::kStateName, &state_); | |
427 } | |
428 | |
429 ApManagerClient::ServiceProperties::~ServiceProperties() { | |
430 } | |
431 | |
432 ApManagerClient::Observer::~Observer() { | |
433 } | |
434 | |
435 void ApManagerClient::Observer::ManagerAdded() { | |
436 } | |
437 | |
438 void ApManagerClient::Observer::ManagerRemoved() { | |
439 } | |
440 | |
441 void ApManagerClient::Observer::ConfigAdded( | |
442 const dbus::ObjectPath& object_path) { | |
443 } | |
444 | |
445 void ApManagerClient::Observer::ConfigRemoved( | |
446 const dbus::ObjectPath& object_path) { | |
447 } | |
448 | |
449 void ApManagerClient::Observer::DeviceAdded( | |
450 const dbus::ObjectPath& object_path) { | |
451 } | |
452 | |
453 void ApManagerClient::Observer::DeviceRemoved( | |
454 const dbus::ObjectPath& object_path) { | |
455 } | |
456 | |
457 void ApManagerClient::Observer::ServiceAdded( | |
458 const dbus::ObjectPath& object_path) { | |
459 } | |
460 | |
461 void ApManagerClient::Observer::ServiceRemoved( | |
462 const dbus::ObjectPath& object_path) { | |
463 } | |
464 | |
465 void ApManagerClient::Observer::ConfigPropertyChanged( | |
466 const dbus::ObjectPath& object_path, | |
467 const std::string& property_name) { | |
468 } | |
469 | |
470 void ApManagerClient::Observer::DevicePropertyChanged( | |
471 const dbus::ObjectPath& object_path, | |
472 const std::string& property_name) { | |
473 } | |
474 | |
475 void ApManagerClient::Observer::ServicePropertyChanged( | |
476 const dbus::ObjectPath& object_path, | |
477 const std::string& property_name) { | |
478 } | |
479 | |
480 ApManagerClient::ApManagerClient() { | |
481 } | |
482 | |
483 ApManagerClient::~ApManagerClient() { | |
484 } | |
485 | |
486 // static | |
487 ApManagerClient* ApManagerClient::Create() { | |
488 return new ApManagerClientImpl(); | |
489 } | |
490 | |
491 } // namespace chromeos | |
OLD | NEW |