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