Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(276)

Side by Side Diff: chrome/browser/chromeos/dbus/flimflam_ipconfig_client.cc

Issue 9875013: Add FlimflamIPConfigClient (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed a nit Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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 "chrome/browser/chromeos/dbus/flimflam_ipconfig_client.h"
6
7 #include "base/bind.h"
8 #include "base/chromeos/chromeos_version.h"
9 #include "base/message_loop.h"
10 #include "dbus/bus.h"
11 #include "dbus/message.h"
12 #include "dbus/object_path.h"
13 #include "dbus/object_proxy.h"
14 #include "dbus/values_util.h"
15 #include "third_party/cros_system_api/dbus/service_constants.h"
16
17 namespace chromeos {
18
19 namespace {
20
21 // The FlimflamIPConfigClient implementation.
22 class FlimflamIPConfigClientImpl : public FlimflamIPConfigClient {
23 public:
24 explicit FlimflamIPConfigClientImpl(dbus::Bus* bus);
25
26 // FlimflamIPConfigClient override.
27 virtual void SetPropertyChangedHandler(
28 const PropertyChangedHandler& handler) OVERRIDE;
29
30 // FlimflamIPConfigClient override.
31 virtual void ResetPropertyChangedHandler() OVERRIDE;
32 // FlimflamIPConfigClient override.
33 virtual void GetProperties(const DictionaryValueCallback& callback) OVERRIDE;
34 // FlimflamIPConfigClient override.
35 virtual void SetProperty(const std::string& name,
36 const base::Value& value,
37 const VoidCallback& callback) OVERRIDE;
38 // FlimflamIPConfigClient override.
39 virtual void ClearProperty(const std::string& name,
40 const VoidCallback& callback) OVERRIDE;
41 // FlimflamIPConfigClient override.
42 virtual void Remove(const VoidCallback& callback) OVERRIDE;
43
44 private:
45 // Handles the result of signal connection setup.
46 void OnSignalConnected(const std::string& interface,
47 const std::string& signal,
48 bool success);
49 // Handles PropertyChanged signal.
50 void OnPropertyChanged(dbus::Signal* signal);
51 // Handles responses for methods without results.
52 void OnVoidMethod(const VoidCallback& callback, dbus::Response* response);
53 // Handles responses for methods with DictionaryValue results.
54 void OnDictionaryValueMethod(const DictionaryValueCallback& callback,
55 dbus::Response* response);
56 dbus::ObjectProxy* proxy_;
57 base::WeakPtrFactory<FlimflamIPConfigClientImpl> weak_ptr_factory_;
58 PropertyChangedHandler property_changed_handler_;
59
60 DISALLOW_COPY_AND_ASSIGN(FlimflamIPConfigClientImpl);
61 };
62
63 FlimflamIPConfigClientImpl::FlimflamIPConfigClientImpl(dbus::Bus* bus)
64 : proxy_(bus->GetObjectProxy(
65 flimflam::kFlimflamServiceName,
66 dbus::ObjectPath(flimflam::kFlimflamServicePath))),
67 weak_ptr_factory_(this) {
68 proxy_->ConnectToSignal(
69 flimflam::kFlimflamIPConfigInterface,
70 flimflam::kMonitorPropertyChanged,
71 base::Bind(&FlimflamIPConfigClientImpl::OnPropertyChanged,
72 weak_ptr_factory_.GetWeakPtr()),
73 base::Bind(&FlimflamIPConfigClientImpl::OnSignalConnected,
74 weak_ptr_factory_.GetWeakPtr()));
75 }
76
77 void FlimflamIPConfigClientImpl::SetPropertyChangedHandler(
78 const PropertyChangedHandler& handler) {
79 property_changed_handler_ = handler;
80 }
81
82 void FlimflamIPConfigClientImpl::ResetPropertyChangedHandler() {
83 property_changed_handler_.Reset();
84 }
85
86 // FlimflamIPConfigClient override.
87 void FlimflamIPConfigClientImpl::GetProperties(
88 const DictionaryValueCallback& callback) {
89 dbus::MethodCall method_call(flimflam::kFlimflamIPConfigInterface,
90 flimflam::kGetPropertiesFunction);
91 proxy_->CallMethod(
92 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
93 base::Bind(&FlimflamIPConfigClientImpl::OnDictionaryValueMethod,
94 weak_ptr_factory_.GetWeakPtr(),
95 callback));
96 }
97
98 // FlimflamIPConfigClient override.
99 void FlimflamIPConfigClientImpl::SetProperty(const std::string& name,
100 const base::Value& value,
101 const VoidCallback& callback) {
102 dbus::MethodCall method_call(flimflam::kFlimflamIPConfigInterface,
103 flimflam::kSetPropertyFunction);
104 dbus::MessageWriter writer(&method_call);
105 writer.AppendString(name);
106 // IPConfig supports writing basic type and string array properties.
107 switch (value.GetType()) {
108 case base::Value::TYPE_LIST: {
109 const base::ListValue* list_value = NULL;
110 value.GetAsList(&list_value);
111 dbus::MessageWriter sub_writer(NULL);
112 writer.OpenArray("s", &sub_writer);
113 for (base::ListValue::const_iterator it = list_value->begin();
114 it != list_value->end();
115 ++it) {
116 std::string str;
117 (*it)->GetAsString(&str);
118 sub_writer.AppendString(str);
119 }
120 writer.CloseContainer(&sub_writer);
121 }
122 case base::Value::TYPE_BOOLEAN:
123 case base::Value::TYPE_INTEGER:
124 case base::Value::TYPE_DOUBLE:
125 case base::Value::TYPE_STRING:
126 dbus::AppendBasicTypeValueData(&writer, value);
127 break;
128 default:
129 DLOG(ERROR) << "Unexpected type " << value.GetType();
130 }
131 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
132 base::Bind(&FlimflamIPConfigClientImpl::OnVoidMethod,
133 weak_ptr_factory_.GetWeakPtr(),
134 callback));
135 }
136
137 // FlimflamIPConfigClient override.
138 void FlimflamIPConfigClientImpl::ClearProperty(const std::string& name,
139 const VoidCallback& callback) {
140 dbus::MethodCall method_call(flimflam::kFlimflamIPConfigInterface,
141 flimflam::kClearPropertyFunction);
142 dbus::MessageWriter writer(&method_call);
143 writer.AppendString(name);
144 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
145 base::Bind(&FlimflamIPConfigClientImpl::OnVoidMethod,
146 weak_ptr_factory_.GetWeakPtr(),
147 callback));
148 }
149
150 // FlimflamIPConfigClient override.
151 void FlimflamIPConfigClientImpl::Remove(const VoidCallback& callback) {
152 dbus::MethodCall method_call(flimflam::kFlimflamIPConfigInterface,
153 flimflam::kRemoveConfigFunction);
154 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
155 base::Bind(&FlimflamIPConfigClientImpl::OnVoidMethod,
156 weak_ptr_factory_.GetWeakPtr(),
157 callback));
158 }
159
160 void FlimflamIPConfigClientImpl::OnSignalConnected(const std::string& interface,
161 const std::string& signal,
162 bool success) {
163 LOG_IF(ERROR, !success) << "Connect to " << interface << " "
164 << signal << " failed.";
165 }
166
167 void FlimflamIPConfigClientImpl::OnPropertyChanged(dbus::Signal* signal) {
168 dbus::MessageReader reader(signal);
169 std::string name;
170 if (!reader.PopString(&name))
171 return;
172 scoped_ptr<base::Value> value(dbus::PopDataAsValue(&reader));
173 if (!value.get())
174 return;
175
176 if (!property_changed_handler_.is_null())
177 property_changed_handler_.Run(name, *value);
178 }
179
180 void FlimflamIPConfigClientImpl::OnVoidMethod(const VoidCallback& callback,
181 dbus::Response* response) {
182 if (!response) {
183 callback.Run(FAILURE);
184 return;
185 }
186 callback.Run(SUCCESS);
187 }
188
189 void FlimflamIPConfigClientImpl::OnDictionaryValueMethod(
190 const DictionaryValueCallback& callback,
191 dbus::Response* response) {
192 if (!response) {
193 base::DictionaryValue result;
194 callback.Run(FAILURE, result);
195 return;
196 }
197 dbus::MessageReader reader(response);
198 scoped_ptr<base::Value> value(dbus::PopDataAsValue(&reader));
199 base::DictionaryValue* result = NULL;
200 if (!value.get() || !value->GetAsDictionary(&result)) {
201 base::DictionaryValue result;
202 callback.Run(FAILURE, result);
203 return;
204 }
205 callback.Run(SUCCESS, *result);
206 }
207
208 // A stub implementation of FlimflamIPConfigClient.
209 class FlimflamIPConfigClientStubImpl : public FlimflamIPConfigClient {
210 public:
211 FlimflamIPConfigClientStubImpl() : weak_ptr_factory_(this) {}
212
213 virtual ~FlimflamIPConfigClientStubImpl() {}
214
215 // FlimflamIPConfigClient override.
216 virtual void SetPropertyChangedHandler(
217 const PropertyChangedHandler& handler) OVERRIDE {}
218
219 // FlimflamIPConfigClient override.
220 virtual void ResetPropertyChangedHandler() OVERRIDE {}
221
222 // FlimflamIPConfigClient override.
223 virtual void GetProperties(const DictionaryValueCallback& callback) OVERRIDE {
224 MessageLoop::current()->PostTask(
225 FROM_HERE, base::Bind(&FlimflamIPConfigClientStubImpl::PassProperties,
226 weak_ptr_factory_.GetWeakPtr(),
227 callback));
228 }
229
230 // FlimflamIPConfigClient override.
231 virtual void SetProperty(const std::string& name,
232 const base::Value& value,
233 const VoidCallback& callback) OVERRIDE {
234 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback, SUCCESS));
235 }
236
237 // FlimflamIPConfigClient override.
238 virtual void ClearProperty(const std::string& name,
239 const VoidCallback& callback) OVERRIDE {
240 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback, SUCCESS));
241 }
242
243 // FlimflamIPConfigClient override.
244 virtual void Remove(const VoidCallback& callback) OVERRIDE {
245 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback, SUCCESS));
246 }
247
248 private:
249 // Runs callback with |properties_|.
250 void PassProperties(const DictionaryValueCallback& callback) const {
251 callback.Run(SUCCESS, properties_);
252 }
253
254 base::WeakPtrFactory<FlimflamIPConfigClientStubImpl> weak_ptr_factory_;
255 base::DictionaryValue properties_;
256
257 DISALLOW_COPY_AND_ASSIGN(FlimflamIPConfigClientStubImpl);
258 };
259
260 } // namespace
261
262 FlimflamIPConfigClient::FlimflamIPConfigClient() {}
263
264 FlimflamIPConfigClient::~FlimflamIPConfigClient() {}
265
266 // static
267 FlimflamIPConfigClient* FlimflamIPConfigClient::Create(dbus::Bus* bus) {
268 if (base::chromeos::IsRunningOnChromeOS())
269 return new FlimflamIPConfigClientImpl(bus);
270 else
271 return new FlimflamIPConfigClientStubImpl();
272 }
273
274 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698