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