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 "chromeos/dbus/shill_manager_client.h"
6
7#include "base/bind.h"
8#include "base/logging.h"
9#include "base/message_loop/message_loop.h"
10#include "base/values.h"
11#include "chromeos/dbus/shill_property_changed_observer.h"
12#include "dbus/bus.h"
13#include "dbus/message.h"
14#include "dbus/object_path.h"
15#include "dbus/object_proxy.h"
16#include "dbus/values_util.h"
17#include "net/base/ip_endpoint.h"
18#include "third_party/cros_system_api/dbus/service_constants.h"
19
20namespace chromeos {
21
22namespace {
23
24// The ShillManagerClient implementation.
25class ShillManagerClientImpl : public ShillManagerClient {
26 public:
27  ShillManagerClientImpl() : proxy_(NULL) {}
28
29  ////////////////////////////////////
30  // ShillManagerClient overrides.
31  virtual void AddPropertyChangedObserver(
32      ShillPropertyChangedObserver* observer) OVERRIDE {
33    helper_->AddPropertyChangedObserver(observer);
34  }
35
36  virtual void RemovePropertyChangedObserver(
37      ShillPropertyChangedObserver* observer) OVERRIDE {
38    helper_->RemovePropertyChangedObserver(observer);
39  }
40
41  virtual void GetProperties(const DictionaryValueCallback& callback) OVERRIDE {
42    dbus::MethodCall method_call(shill::kFlimflamManagerInterface,
43                                 shill::kGetPropertiesFunction);
44    helper_->CallDictionaryValueMethod(&method_call, callback);
45  }
46
47  virtual void GetNetworksForGeolocation(
48      const DictionaryValueCallback& callback) OVERRIDE {
49    dbus::MethodCall method_call(shill::kFlimflamManagerInterface,
50                                 shill::kGetNetworksForGeolocation);
51    helper_->CallDictionaryValueMethod(&method_call, callback);
52  }
53
54  virtual void SetProperty(const std::string& name,
55                           const base::Value& value,
56                           const base::Closure& callback,
57                           const ErrorCallback& error_callback) OVERRIDE {
58    dbus::MethodCall method_call(shill::kFlimflamManagerInterface,
59                                 shill::kSetPropertyFunction);
60    dbus::MessageWriter writer(&method_call);
61    writer.AppendString(name);
62    ShillClientHelper::AppendValueDataAsVariant(&writer, value);
63    helper_->CallVoidMethodWithErrorCallback(&method_call,
64                                            callback,
65                                            error_callback);
66  }
67
68  virtual void RequestScan(const std::string& type,
69                           const base::Closure& callback,
70                           const ErrorCallback& error_callback) OVERRIDE {
71    dbus::MethodCall method_call(shill::kFlimflamManagerInterface,
72                                 shill::kRequestScanFunction);
73    dbus::MessageWriter writer(&method_call);
74    writer.AppendString(type);
75    helper_->CallVoidMethodWithErrorCallback(&method_call,
76                                            callback,
77                                            error_callback);
78  }
79
80  virtual void EnableTechnology(
81      const std::string& type,
82      const base::Closure& callback,
83      const ErrorCallback& error_callback) OVERRIDE {
84    dbus::MethodCall method_call(shill::kFlimflamManagerInterface,
85                                 shill::kEnableTechnologyFunction);
86    dbus::MessageWriter writer(&method_call);
87    writer.AppendString(type);
88    helper_->CallVoidMethodWithErrorCallback(&method_call,
89                                            callback,
90                                            error_callback);
91  }
92
93  virtual void DisableTechnology(
94      const std::string& type,
95      const base::Closure& callback,
96      const ErrorCallback& error_callback) OVERRIDE {
97    dbus::MethodCall method_call(shill::kFlimflamManagerInterface,
98                                 shill::kDisableTechnologyFunction);
99    dbus::MessageWriter writer(&method_call);
100    writer.AppendString(type);
101    helper_->CallVoidMethodWithErrorCallback(&method_call,
102                                            callback,
103                                            error_callback);
104  }
105
106  virtual void ConfigureService(
107      const base::DictionaryValue& properties,
108      const ObjectPathCallback& callback,
109      const ErrorCallback& error_callback) OVERRIDE {
110    dbus::MethodCall method_call(shill::kFlimflamManagerInterface,
111                                 shill::kConfigureServiceFunction);
112    dbus::MessageWriter writer(&method_call);
113    ShillClientHelper::AppendServicePropertiesDictionary(&writer, properties);
114    helper_->CallObjectPathMethodWithErrorCallback(&method_call,
115                                                  callback,
116                                                  error_callback);
117  }
118
119  virtual void ConfigureServiceForProfile(
120      const dbus::ObjectPath& profile_path,
121      const base::DictionaryValue& properties,
122      const ObjectPathCallback& callback,
123      const ErrorCallback& error_callback) OVERRIDE {
124    dbus::MethodCall method_call(shill::kFlimflamManagerInterface,
125                                 shill::kConfigureServiceForProfileFunction);
126    dbus::MessageWriter writer(&method_call);
127    writer.AppendObjectPath(dbus::ObjectPath(profile_path));
128    ShillClientHelper::AppendServicePropertiesDictionary(&writer, properties);
129    helper_->CallObjectPathMethodWithErrorCallback(&method_call,
130                                                  callback,
131                                                  error_callback);
132  }
133
134  virtual void GetService(
135      const base::DictionaryValue& properties,
136      const ObjectPathCallback& callback,
137      const ErrorCallback& error_callback) OVERRIDE {
138    dbus::MethodCall method_call(shill::kFlimflamManagerInterface,
139                                 shill::kGetServiceFunction);
140    dbus::MessageWriter writer(&method_call);
141    ShillClientHelper::AppendServicePropertiesDictionary(&writer, properties);
142    helper_->CallObjectPathMethodWithErrorCallback(&method_call,
143                                                  callback,
144                                                  error_callback);
145  }
146
147  virtual void VerifyDestination(const VerificationProperties& properties,
148                                 const BooleanCallback& callback,
149                                 const ErrorCallback& error_callback) OVERRIDE {
150    dbus::MethodCall method_call(shill::kFlimflamManagerInterface,
151                                 shill::kVerifyDestinationFunction);
152    dbus::MessageWriter writer(&method_call);
153    writer.AppendString(properties.certificate);
154    writer.AppendString(properties.public_key);
155    writer.AppendString(properties.nonce);
156    writer.AppendString(properties.signed_data);
157    writer.AppendString(properties.device_serial);
158    writer.AppendString(properties.device_ssid);
159    writer.AppendString(properties.device_bssid);
160    helper_->CallBooleanMethodWithErrorCallback(
161        &method_call, callback, error_callback);
162  }
163
164  virtual void VerifyAndEncryptCredentials(
165      const VerificationProperties& properties,
166      const std::string& service_path,
167      const StringCallback& callback,
168      const ErrorCallback& error_callback) OVERRIDE {
169    dbus::MethodCall method_call(shill::kFlimflamManagerInterface,
170                                 shill::kVerifyAndEncryptCredentialsFunction);
171    dbus::MessageWriter writer(&method_call);
172    writer.AppendString(properties.certificate);
173    writer.AppendString(properties.public_key);
174    writer.AppendString(properties.nonce);
175    writer.AppendString(properties.signed_data);
176    writer.AppendString(properties.device_serial);
177    writer.AppendString(properties.device_ssid);
178    writer.AppendString(properties.device_bssid);
179    writer.AppendObjectPath(dbus::ObjectPath(service_path));
180    helper_->CallStringMethodWithErrorCallback(
181        &method_call, callback, error_callback);
182  }
183
184  virtual void VerifyAndEncryptData(
185      const VerificationProperties& properties,
186      const std::string& data,
187      const StringCallback& callback,
188      const ErrorCallback& error_callback) OVERRIDE {
189    dbus::MethodCall method_call(shill::kFlimflamManagerInterface,
190                                 shill::kVerifyAndEncryptDataFunction);
191    dbus::MessageWriter writer(&method_call);
192    writer.AppendString(properties.certificate);
193    writer.AppendString(properties.public_key);
194    writer.AppendString(properties.nonce);
195    writer.AppendString(properties.signed_data);
196    writer.AppendString(properties.device_serial);
197    writer.AppendString(properties.device_ssid);
198    writer.AppendString(properties.device_bssid);
199    writer.AppendString(data);
200    helper_->CallStringMethodWithErrorCallback(
201        &method_call, callback, error_callback);
202  }
203
204  virtual void ConnectToBestServices(
205      const base::Closure& callback,
206      const ErrorCallback& error_callback) OVERRIDE {
207    dbus::MethodCall method_call(shill::kFlimflamManagerInterface,
208                                 shill::kConnectToBestServicesFunction);
209    helper_->CallVoidMethodWithErrorCallback(&method_call,
210                                            callback,
211                                            error_callback);
212  }
213
214  virtual void AddWakeOnPacketConnection(
215      const net::IPEndPoint& ip_endpoint,
216      const base::Closure& callback,
217      const ErrorCallback& error_callback) OVERRIDE {
218    if (ip_endpoint.address().empty()) {
219      LOG(ERROR) << "AddWakeOnPacketConnection: null address";
220      return;
221    }
222    dbus::MethodCall method_call(shill::kFlimflamManagerInterface,
223                                 shill::kAddWakeOnPacketConnectionFunction);
224    dbus::MessageWriter writer(&method_call);
225    writer.AppendString(net::IPAddressToString(ip_endpoint.address()));
226    helper_->CallVoidMethodWithErrorCallback(&method_call,
227                                             callback,
228                                             error_callback);
229  }
230
231  virtual void RemoveWakeOnPacketConnection(
232      const net::IPEndPoint& ip_endpoint,
233      const base::Closure& callback,
234      const ErrorCallback& error_callback) OVERRIDE {
235    if (ip_endpoint.address().empty()) {
236      LOG(ERROR) << "RemoveWakeOnPacketConnection: null address";
237      return;
238    }
239    dbus::MethodCall method_call(shill::kFlimflamManagerInterface,
240                                 shill::kRemoveWakeOnPacketConnectionFunction);
241    dbus::MessageWriter writer(&method_call);
242    writer.AppendString(net::IPAddressToString(ip_endpoint.address()));
243    helper_->CallVoidMethodWithErrorCallback(&method_call,
244                                             callback,
245                                             error_callback);
246  }
247
248  virtual void RemoveAllWakeOnPacketConnections(
249      const base::Closure& callback,
250      const ErrorCallback& error_callback) OVERRIDE {
251    dbus::MethodCall method_call(
252        shill::kFlimflamManagerInterface,
253        shill::kRemoveAllWakeOnPacketConnectionsFunction);
254    helper_->CallVoidMethodWithErrorCallback(&method_call,
255                                             callback,
256                                             error_callback);
257  }
258
259  virtual TestInterface* GetTestInterface() OVERRIDE {
260    return NULL;
261  }
262
263 protected:
264  virtual void Init(dbus::Bus* bus) OVERRIDE {
265    proxy_ = bus->GetObjectProxy(shill::kFlimflamServiceName,
266                                 dbus::ObjectPath(shill::kFlimflamServicePath));
267    helper_.reset(new ShillClientHelper(proxy_));
268    helper_->MonitorPropertyChanged(shill::kFlimflamManagerInterface);
269  }
270
271 private:
272  dbus::ObjectProxy* proxy_;
273  scoped_ptr<ShillClientHelper> helper_;
274
275  DISALLOW_COPY_AND_ASSIGN(ShillManagerClientImpl);
276};
277
278}  // namespace
279
280ShillManagerClient::ShillManagerClient() {}
281
282ShillManagerClient::~ShillManagerClient() {}
283
284// static
285ShillManagerClient* ShillManagerClient::Create() {
286  return new ShillManagerClientImpl();
287}
288
289// ShillManagerClient::VerificationProperties implementation.
290ShillManagerClient::VerificationProperties::VerificationProperties() {
291}
292
293ShillManagerClient::VerificationProperties::~VerificationProperties() {
294}
295
296}  // namespace chromeos
297