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.
5 #include "chromeos/dbus/ap_manager_client.h"
8 #include "base/logging.h"
9 #include "base/memory/weak_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/observer_list.h"
13 #include "dbus/message.h"
14 #include "dbus/object_manager.h"
15 #include "dbus/object_proxy.h"
16 #include "dbus/values_util.h"
20 // TODO(benchan): Move these constants to system_api.
22 const char kApManagerServiceName
[] = "org.chromium.apmanager";
23 const char kApManagerServicePath
[] = "/org/chromium/apmanager";
24 const char kApManagerManagerPath
[] = "/org/chromium/apmanager/Manager";
25 const char kManagerInterfaceName
[] = "org.chromium.apmanager.Manager";
26 const char kConfigInterfaceName
[] = "org.chromium.apmanager.Config";
27 const char kDeviceInterfaceName
[] = "org.chromium.apmanager.Device";
28 const char kServiceInterfaceName
[] = "org.chromium.apmanager.Service";
29 const char kCreateServiceMethod
[] = "CreateService";
30 const char kRemoveServiceMethod
[] = "RemoveService";
31 const char kStartMethod
[] = "Start";
32 const char kStopMethod
[] = "Stop";
33 const char kSsidProperty
[] = "Ssid";
34 const char kInterfaceNameProperty
[] = "InterfaceName";
35 const char kSecurityModeProperty
[] = "SecurityMode";
36 const char kPassphraseProperty
[] = "Passphrase";
37 const char kHwModeProperty
[] = "HwMode";
38 const char kOperationModeProperty
[] = "OperationMode";
39 const char kChannelProperty
[] = "Channel";
40 const char kHiddenNetworkProperty
[] = "HiddenNetwork";
41 const char kBridgeInterfaceProperty
[] = "BridgeInterface";
42 const char kServiceAddressIndexProperty
[] = "ServerAddressIndex";
43 const char kDeviceNameProperty
[] = "DeviceName";
44 const char kInUsedProperty
[] = "InUsed";
45 const char kPreferredApInterfaceProperty
[] = "PreferredApInterface";
46 const char kConfigName
[] = "Config";
47 const char kStateName
[] = "State";
49 } // namespace apmanager
53 // Since there is no property associated with Manager objects, an empty callback
55 void ManagerPropertyChanged(const std::string
& property_name
) {
58 // The ApManagerClient implementation used in production.
59 class ApManagerClientImpl
: public ApManagerClient
,
60 public dbus::ObjectManager::Interface
{
62 ApManagerClientImpl();
63 ~ApManagerClientImpl() override
;
65 // ApManagerClient overrides.
66 void AddObserver(Observer
* observer
) override
;
67 void RemoveObserver(Observer
* observer
) override
;
68 void CreateService(const ObjectPathDBusMethodCallback
& callback
) override
;
69 void RemoveService(const dbus::ObjectPath
& object_path
,
70 const VoidDBusMethodCallback
& callback
) override
;
71 void StartService(const dbus::ObjectPath
& object_path
,
72 const VoidDBusMethodCallback
& callback
) override
;
73 void StopService(const dbus::ObjectPath
& object_path
,
74 const VoidDBusMethodCallback
& callback
) override
;
75 ConfigProperties
* GetConfigProperties(
76 const dbus::ObjectPath
& object_path
) override
;
77 const DeviceProperties
* GetDeviceProperties(
78 const dbus::ObjectPath
& object_path
) override
;
79 const ServiceProperties
* GetServiceProperties(
80 const dbus::ObjectPath
& object_path
) override
;
82 // DBusClient overrides.
83 void Init(dbus::Bus
* bus
) override
;
85 // dbus::ObjectManager::Interface overrides.
86 dbus::PropertySet
* CreateProperties(
87 dbus::ObjectProxy
* object_proxy
,
88 const dbus::ObjectPath
& object_path
,
89 const std::string
& interface_name
) override
;
90 void ObjectAdded(const dbus::ObjectPath
& object_path
,
91 const std::string
& interface_name
) override
;
92 void ObjectRemoved(const dbus::ObjectPath
& object_path
,
93 const std::string
& interface_name
) override
;
96 // Called by dbus::PropertySet when a property value is changed,
97 // either by result of a signal or response to a GetAll() or Get()
98 // call. Informs observers.
99 void OnConfigPropertyChanged(const dbus::ObjectPath
& object_path
,
100 const std::string
& property_name
);
101 void OnDevicePropertyChanged(const dbus::ObjectPath
& object_path
,
102 const std::string
& property_name
);
103 void OnServicePropertyChanged(const dbus::ObjectPath
& object_path
,
104 const std::string
& property_name
);
106 void OnObjectPathDBusMethod(const ObjectPathDBusMethodCallback
& callback
,
107 dbus::Response
* response
);
108 void OnStringDBusMethod(const StringDBusMethodCallback
& callback
,
109 dbus::Response
* response
);
110 void OnVoidDBusMethod(const VoidDBusMethodCallback
& callback
,
111 dbus::Response
* response
);
113 // List of observers interested in event notifications from us.
114 ObserverList
<Observer
> observers_
;
115 dbus::ObjectManager
* object_manager_
;
116 base::WeakPtrFactory
<ApManagerClientImpl
> weak_ptr_factory_
;
118 DISALLOW_COPY_AND_ASSIGN(ApManagerClientImpl
);
121 ApManagerClientImpl::ApManagerClientImpl()
122 : object_manager_(nullptr), weak_ptr_factory_(this) {
125 ApManagerClientImpl::~ApManagerClientImpl() {
126 if (object_manager_
) {
127 object_manager_
->UnregisterInterface(apmanager::kManagerInterfaceName
);
128 object_manager_
->UnregisterInterface(apmanager::kConfigInterfaceName
);
129 object_manager_
->UnregisterInterface(apmanager::kDeviceInterfaceName
);
130 object_manager_
->UnregisterInterface(apmanager::kServiceInterfaceName
);
134 void ApManagerClientImpl::AddObserver(Observer
* observer
) {
136 observers_
.AddObserver(observer
);
139 void ApManagerClientImpl::RemoveObserver(Observer
* observer
) {
141 observers_
.RemoveObserver(observer
);
144 void ApManagerClientImpl::CreateService(
145 const ObjectPathDBusMethodCallback
& callback
) {
146 dbus::ObjectProxy
* object_proxy
= object_manager_
->GetObjectProxy(
147 dbus::ObjectPath(apmanager::kApManagerManagerPath
));
149 base::MessageLoop::current()->PostTask(
151 base::Bind(&ApManagerClientImpl::OnObjectPathDBusMethod
,
152 weak_ptr_factory_
.GetWeakPtr(), callback
, nullptr));
156 dbus::MethodCall
method_call(apmanager::kManagerInterfaceName
,
157 apmanager::kCreateServiceMethod
);
158 dbus::MessageWriter
writer(&method_call
);
159 object_proxy
->CallMethod(
160 &method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
161 base::Bind(&ApManagerClientImpl::OnObjectPathDBusMethod
,
162 weak_ptr_factory_
.GetWeakPtr(), callback
));
165 void ApManagerClientImpl::RemoveService(
166 const dbus::ObjectPath
& object_path
,
167 const VoidDBusMethodCallback
& callback
) {
168 dbus::ObjectProxy
* object_proxy
= object_manager_
->GetObjectProxy(
169 dbus::ObjectPath(apmanager::kApManagerManagerPath
));
171 base::MessageLoop::current()->PostTask(
173 base::Bind(&ApManagerClientImpl::OnVoidDBusMethod
,
174 weak_ptr_factory_
.GetWeakPtr(), callback
, nullptr));
178 dbus::MethodCall
method_call(apmanager::kManagerInterfaceName
,
179 apmanager::kRemoveServiceMethod
);
180 dbus::MessageWriter
writer(&method_call
);
181 writer
.AppendObjectPath(object_path
);
182 object_proxy
->CallMethod(
183 &method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
184 base::Bind(&ApManagerClientImpl::OnVoidDBusMethod
,
185 weak_ptr_factory_
.GetWeakPtr(), callback
));
188 void ApManagerClientImpl::StartService(const dbus::ObjectPath
& object_path
,
189 const VoidDBusMethodCallback
& callback
) {
190 dbus::ObjectProxy
* object_proxy
=
191 object_manager_
->GetObjectProxy(object_path
);
193 base::MessageLoop::current()->PostTask(
195 base::Bind(&ApManagerClientImpl::OnVoidDBusMethod
,
196 weak_ptr_factory_
.GetWeakPtr(), callback
, nullptr));
200 dbus::MethodCall
method_call(apmanager::kServiceInterfaceName
,
201 apmanager::kStartMethod
);
202 dbus::MessageWriter
writer(&method_call
);
203 object_proxy
->CallMethod(
204 &method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
205 base::Bind(&ApManagerClientImpl::OnVoidDBusMethod
,
206 weak_ptr_factory_
.GetWeakPtr(), callback
));
209 void ApManagerClientImpl::StopService(const dbus::ObjectPath
& object_path
,
210 const VoidDBusMethodCallback
& callback
) {
211 dbus::ObjectProxy
* object_proxy
=
212 object_manager_
->GetObjectProxy(object_path
);
214 base::MessageLoop::current()->PostTask(
216 base::Bind(&ApManagerClientImpl::OnVoidDBusMethod
,
217 weak_ptr_factory_
.GetWeakPtr(), callback
, nullptr));
221 dbus::MethodCall
method_call(apmanager::kServiceInterfaceName
,
222 apmanager::kStopMethod
);
223 dbus::MessageWriter
writer(&method_call
);
224 object_proxy
->CallMethod(
225 &method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
226 base::Bind(&ApManagerClientImpl::OnVoidDBusMethod
,
227 weak_ptr_factory_
.GetWeakPtr(), callback
));
230 ApManagerClient::ConfigProperties
* ApManagerClientImpl::GetConfigProperties(
231 const dbus::ObjectPath
& object_path
) {
232 return static_cast<ConfigProperties
*>(object_manager_
->GetProperties(
233 object_path
, apmanager::kConfigInterfaceName
));
236 const ApManagerClient::DeviceProperties
*
237 ApManagerClientImpl::GetDeviceProperties(const dbus::ObjectPath
& object_path
) {
238 return static_cast<DeviceProperties
*>(object_manager_
->GetProperties(
239 object_path
, apmanager::kDeviceInterfaceName
));
242 const ApManagerClient::ServiceProperties
*
243 ApManagerClientImpl::GetServiceProperties(const dbus::ObjectPath
& object_path
) {
244 return static_cast<ServiceProperties
*>(object_manager_
->GetProperties(
245 object_path
, apmanager::kServiceInterfaceName
));
248 void ApManagerClientImpl::Init(dbus::Bus
* bus
) {
250 bus
->GetObjectManager(apmanager::kApManagerServiceName
,
251 dbus::ObjectPath(apmanager::kApManagerServicePath
));
252 object_manager_
->RegisterInterface(apmanager::kManagerInterfaceName
, this);
253 object_manager_
->RegisterInterface(apmanager::kConfigInterfaceName
, this);
254 object_manager_
->RegisterInterface(apmanager::kDeviceInterfaceName
, this);
255 object_manager_
->RegisterInterface(apmanager::kServiceInterfaceName
, this);
258 dbus::PropertySet
* ApManagerClientImpl::CreateProperties(
259 dbus::ObjectProxy
* object_proxy
,
260 const dbus::ObjectPath
& object_path
,
261 const std::string
& interface_name
) {
262 dbus::PropertySet
* properties
= nullptr;
263 if (interface_name
== apmanager::kManagerInterfaceName
) {
264 properties
= new dbus::PropertySet(object_proxy
, interface_name
,
265 base::Bind(&ManagerPropertyChanged
));
266 } else if (interface_name
== apmanager::kConfigInterfaceName
) {
267 properties
= new ConfigProperties(
268 object_proxy
, interface_name
,
269 base::Bind(&ApManagerClientImpl::OnConfigPropertyChanged
,
270 weak_ptr_factory_
.GetWeakPtr(), object_path
));
271 } else if (interface_name
== apmanager::kDeviceInterfaceName
) {
272 properties
= new DeviceProperties(
273 object_proxy
, interface_name
,
274 base::Bind(&ApManagerClientImpl::OnDevicePropertyChanged
,
275 weak_ptr_factory_
.GetWeakPtr(), object_path
));
276 } else if (interface_name
== apmanager::kServiceInterfaceName
) {
277 properties
= new ServiceProperties(
278 object_proxy
, interface_name
,
279 base::Bind(&ApManagerClientImpl::OnServicePropertyChanged
,
280 weak_ptr_factory_
.GetWeakPtr(), object_path
));
282 NOTREACHED() << "Unhandled interface name " << interface_name
;
287 void ApManagerClientImpl::ObjectAdded(const dbus::ObjectPath
& object_path
,
288 const std::string
& interface_name
) {
289 if (interface_name
== apmanager::kManagerInterfaceName
) {
290 FOR_EACH_OBSERVER(Observer
, observers_
, ManagerAdded());
291 } else if (interface_name
== apmanager::kConfigInterfaceName
) {
292 FOR_EACH_OBSERVER(Observer
, observers_
, ConfigAdded(object_path
));
293 } else if (interface_name
== apmanager::kDeviceInterfaceName
) {
294 FOR_EACH_OBSERVER(Observer
, observers_
, DeviceAdded(object_path
));
295 } else if (interface_name
== apmanager::kServiceInterfaceName
) {
296 FOR_EACH_OBSERVER(Observer
, observers_
, ServiceAdded(object_path
));
298 NOTREACHED() << "Unhandled interface name " << interface_name
;
302 void ApManagerClientImpl::ObjectRemoved(const dbus::ObjectPath
& object_path
,
303 const std::string
& interface_name
) {
304 if (interface_name
== apmanager::kManagerInterfaceName
) {
305 FOR_EACH_OBSERVER(Observer
, observers_
, ManagerRemoved());
306 } else if (interface_name
== apmanager::kConfigInterfaceName
) {
307 FOR_EACH_OBSERVER(Observer
, observers_
, ConfigRemoved(object_path
));
308 } else if (interface_name
== apmanager::kDeviceInterfaceName
) {
309 FOR_EACH_OBSERVER(Observer
, observers_
, DeviceRemoved(object_path
));
310 } else if (interface_name
== apmanager::kServiceInterfaceName
) {
311 FOR_EACH_OBSERVER(Observer
, observers_
, ServiceRemoved(object_path
));
313 NOTREACHED() << "Unhandled interface name " << interface_name
;
317 void ApManagerClientImpl::OnConfigPropertyChanged(
318 const dbus::ObjectPath
& object_path
,
319 const std::string
& property_name
) {
320 FOR_EACH_OBSERVER(Observer
, observers_
,
321 ConfigPropertyChanged(object_path
, property_name
));
324 void ApManagerClientImpl::OnDevicePropertyChanged(
325 const dbus::ObjectPath
& object_path
,
326 const std::string
& property_name
) {
327 FOR_EACH_OBSERVER(Observer
, observers_
,
328 ConfigPropertyChanged(object_path
, property_name
));
331 void ApManagerClientImpl::OnServicePropertyChanged(
332 const dbus::ObjectPath
& object_path
,
333 const std::string
& property_name
) {
334 FOR_EACH_OBSERVER(Observer
, observers_
,
335 ServicePropertyChanged(object_path
, property_name
));
338 void ApManagerClientImpl::OnObjectPathDBusMethod(
339 const ObjectPathDBusMethodCallback
& callback
,
340 dbus::Response
* response
) {
342 callback
.Run(DBUS_METHOD_CALL_FAILURE
, dbus::ObjectPath());
346 dbus::MessageReader
reader(response
);
347 dbus::ObjectPath result
;
348 if (!reader
.PopObjectPath(&result
)) {
349 callback
.Run(DBUS_METHOD_CALL_FAILURE
, result
);
353 callback
.Run(DBUS_METHOD_CALL_SUCCESS
, result
);
356 void ApManagerClientImpl::OnStringDBusMethod(
357 const StringDBusMethodCallback
& callback
,
358 dbus::Response
* response
) {
360 callback
.Run(DBUS_METHOD_CALL_FAILURE
, std::string());
364 dbus::MessageReader
reader(response
);
366 if (!reader
.PopString(&result
)) {
367 callback
.Run(DBUS_METHOD_CALL_FAILURE
, std::string());
371 callback
.Run(DBUS_METHOD_CALL_SUCCESS
, result
);
374 void ApManagerClientImpl::OnVoidDBusMethod(
375 const VoidDBusMethodCallback
& callback
,
376 dbus::Response
* response
) {
377 callback
.Run(response
? DBUS_METHOD_CALL_SUCCESS
: DBUS_METHOD_CALL_FAILURE
);
382 ApManagerClient::ConfigProperties::ConfigProperties(
383 dbus::ObjectProxy
* object_proxy
,
384 const std::string
& dbus_interface_name
,
385 const PropertyChangedCallback
& callback
)
386 : dbus::PropertySet(object_proxy
, dbus_interface_name
, callback
) {
387 RegisterProperty(apmanager::kSsidProperty
, &ssid_
);
388 RegisterProperty(apmanager::kInterfaceNameProperty
, &interface_name_
);
389 RegisterProperty(apmanager::kSecurityModeProperty
, &security_mode_
);
390 RegisterProperty(apmanager::kPassphraseProperty
, &passphrase_
);
391 RegisterProperty(apmanager::kHwModeProperty
, &hw_mode_
);
392 RegisterProperty(apmanager::kOperationModeProperty
, &operation_mode_
);
393 RegisterProperty(apmanager::kChannelProperty
, &channel_
);
394 RegisterProperty(apmanager::kHiddenNetworkProperty
, &hidden_network_
);
395 RegisterProperty(apmanager::kBridgeInterfaceProperty
, &bridge_interface_
);
396 RegisterProperty(apmanager::kServiceAddressIndexProperty
,
397 &server_address_index_
);
400 ApManagerClient::ConfigProperties::~ConfigProperties() {
403 ApManagerClient::DeviceProperties::DeviceProperties(
404 dbus::ObjectProxy
* object_proxy
,
405 const std::string
& interface_name
,
406 const PropertyChangedCallback
& callback
)
407 : dbus::PropertySet(object_proxy
, interface_name
, callback
) {
408 RegisterProperty(apmanager::kDeviceNameProperty
, &device_name_
);
409 RegisterProperty(apmanager::kInUsedProperty
, &in_used_
);
410 RegisterProperty(apmanager::kPreferredApInterfaceProperty
,
411 &preferred_ap_interface_
);
414 ApManagerClient::DeviceProperties::~DeviceProperties() {
417 ApManagerClient::ServiceProperties::ServiceProperties(
418 dbus::ObjectProxy
* object_proxy
,
419 const std::string
& interface_name
,
420 const PropertyChangedCallback
& callback
)
421 : dbus::PropertySet(object_proxy
, interface_name
, callback
) {
422 RegisterProperty(apmanager::kConfigName
, &config_
);
423 RegisterProperty(apmanager::kStateName
, &state_
);
426 ApManagerClient::ServiceProperties::~ServiceProperties() {
429 ApManagerClient::Observer::~Observer() {
432 void ApManagerClient::Observer::ManagerAdded() {
435 void ApManagerClient::Observer::ManagerRemoved() {
438 void ApManagerClient::Observer::ConfigAdded(
439 const dbus::ObjectPath
& object_path
) {
442 void ApManagerClient::Observer::ConfigRemoved(
443 const dbus::ObjectPath
& object_path
) {
446 void ApManagerClient::Observer::DeviceAdded(
447 const dbus::ObjectPath
& object_path
) {
450 void ApManagerClient::Observer::DeviceRemoved(
451 const dbus::ObjectPath
& object_path
) {
454 void ApManagerClient::Observer::ServiceAdded(
455 const dbus::ObjectPath
& object_path
) {
458 void ApManagerClient::Observer::ServiceRemoved(
459 const dbus::ObjectPath
& object_path
) {
462 void ApManagerClient::Observer::ConfigPropertyChanged(
463 const dbus::ObjectPath
& object_path
,
464 const std::string
& property_name
) {
467 void ApManagerClient::Observer::DevicePropertyChanged(
468 const dbus::ObjectPath
& object_path
,
469 const std::string
& property_name
) {
472 void ApManagerClient::Observer::ServicePropertyChanged(
473 const dbus::ObjectPath
& object_path
,
474 const std::string
& property_name
) {
477 ApManagerClient::ApManagerClient() {
480 ApManagerClient::~ApManagerClient() {
484 ApManagerClient
* ApManagerClient::Create() {
485 return new ApManagerClientImpl();
488 } // namespace chromeos