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

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: Review fix Created 8 years, 9 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 }
satorux1 2012/03/29 17:58:44 nit: add blank line.
hashimoto 2012/03/29 18:12:16 Done.
76 void FlimflamIPConfigClientImpl::SetPropertyChangedHandler(
77 const PropertyChangedHandler& handler) {
78 property_changed_handler_ = handler;
79 }
80
81 void FlimflamIPConfigClientImpl::ResetPropertyChangedHandler() {
82 property_changed_handler_.Reset();
83 }
84
85 // FlimflamIPConfigClient override.
86 void FlimflamIPConfigClientImpl::GetProperties(
87 const DictionaryValueCallback& callback) {
88 dbus::MethodCall method_call(flimflam::kFlimflamIPConfigInterface,
89 flimflam::kGetPropertiesFunction);
90 proxy_->CallMethod(
91 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
92 base::Bind(&FlimflamIPConfigClientImpl::OnDictionaryValueMethod,
93 weak_ptr_factory_.GetWeakPtr(),
94 callback));
95 }
96
97 // FlimflamIPConfigClient override.
98 void FlimflamIPConfigClientImpl::SetProperty(const std::string& name,
99 const base::Value& value,
100 const VoidCallback& callback) {
101 dbus::MethodCall method_call(flimflam::kFlimflamIPConfigInterface,
102 flimflam::kSetPropertyFunction);
103 dbus::MessageWriter writer(&method_call);
104 writer.AppendString(name);
105 // IPConfig supports writing basic type and string array properties.
106 switch (value.GetType()) {
107 case base::Value::TYPE_LIST: {
108 const base::ListValue* list_value = NULL;
109 value.GetAsList(&list_value);
110 dbus::MessageWriter sub_writer(NULL);
111 writer.OpenArray("s", &sub_writer);
112 for (base::ListValue::const_iterator it = list_value->begin();
113 it != list_value->end();
114 ++it) {
115 std::string str;
116 (*it)->GetAsString(&str);
117 sub_writer.AppendString(str);
118 }
119 writer.CloseContainer(&sub_writer);
120 }
121 case base::Value::TYPE_BOOLEAN:
122 case base::Value::TYPE_INTEGER:
123 case base::Value::TYPE_DOUBLE:
124 case base::Value::TYPE_STRING:
125 dbus::AppendBasicTypeValueData(&writer, value);
126 break;
127 default:
128 DLOG(ERROR) << "Unexpected type " << value.GetType();
129 }
130 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
131 base::Bind(&FlimflamIPConfigClientImpl::OnVoidMethod,
132 weak_ptr_factory_.GetWeakPtr(),
133 callback));
134 }
135
136 // FlimflamIPConfigClient override.
137 void FlimflamIPConfigClientImpl::ClearProperty(const std::string& name,
138 const VoidCallback& callback) {
139 dbus::MethodCall method_call(flimflam::kFlimflamIPConfigInterface,
140 flimflam::kClearPropertyFunction);
141 dbus::MessageWriter writer(&method_call);
142 writer.AppendString(name);
143 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
144 base::Bind(&FlimflamIPConfigClientImpl::OnVoidMethod,
145 weak_ptr_factory_.GetWeakPtr(),
146 callback));
147 }
148
149 // FlimflamIPConfigClient override.
150 void FlimflamIPConfigClientImpl::Remove(const VoidCallback& callback) {
151 dbus::MethodCall method_call(flimflam::kFlimflamIPConfigInterface,
152 flimflam::kRemoveConfigFunction);
153 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
154 base::Bind(&FlimflamIPConfigClientImpl::OnVoidMethod,
155 weak_ptr_factory_.GetWeakPtr(),
156 callback));
157 }
158
159 void FlimflamIPConfigClientImpl::OnSignalConnected(const std::string& interface,
160 const std::string& signal,
161 bool success) {
162 LOG_IF(ERROR, !success) << "Connect to " << interface << " "
163 << signal << " failed.";
164 }
165
166 void FlimflamIPConfigClientImpl::OnPropertyChanged(dbus::Signal* signal) {
167 dbus::MessageReader reader(signal);
168 std::string name;
169 if (!reader.PopString(&name))
170 return;
171 scoped_ptr<base::Value> value(dbus::PopDataAsValue(&reader));
172 if (!value.get())
173 return;
174
175 if (!property_changed_handler_.is_null())
176 property_changed_handler_.Run(name, *value);
177 }
178
179 void FlimflamIPConfigClientImpl::OnVoidMethod(const VoidCallback& callback,
180 dbus::Response* response) {
181 if (!response) {
182 callback.Run(FAILURE);
183 return;
184 }
185 callback.Run(SUCCESS);
186 }
187
188 void FlimflamIPConfigClientImpl::OnDictionaryValueMethod(
189 const DictionaryValueCallback& callback,
190 dbus::Response* response) {
191 if (!response) {
192 base::DictionaryValue result;
193 callback.Run(FAILURE, result);
194 return;
195 }
196 dbus::MessageReader reader(response);
197 scoped_ptr<base::Value> value(dbus::PopDataAsValue(&reader));
198 base::DictionaryValue* result = NULL;
199 if (!value.get() || !value->GetAsDictionary(&result)) {
200 base::DictionaryValue result;
201 callback.Run(FAILURE, result);
202 return;
203 }
204 callback.Run(SUCCESS, *result);
205 }
206
207 // A stub implementation of FlimflamIPConfigClient.
208 class FlimflamIPConfigClientStubImpl : public FlimflamIPConfigClient {
209 public:
210 FlimflamIPConfigClientStubImpl() : weak_ptr_factory_(this) {}
211
212 virtual ~FlimflamIPConfigClientStubImpl() {}
213
214 // FlimflamIPConfigClient override.
215 virtual void SetPropertyChangedHandler(
216 const PropertyChangedHandler& handler) OVERRIDE {}
217
218 // FlimflamIPConfigClient override.
219 virtual void ResetPropertyChangedHandler() OVERRIDE {}
220
221 // FlimflamIPConfigClient override.
222 virtual void GetProperties(const DictionaryValueCallback& callback) OVERRIDE {
223 MessageLoop::current()->PostTask(
224 FROM_HERE, base::Bind(&FlimflamIPConfigClientStubImpl::PassProperties,
225 weak_ptr_factory_.GetWeakPtr(),
226 callback));
227 }
228
229 // FlimflamIPConfigClient override.
230 virtual void SetProperty(const std::string& name,
231 const base::Value& value,
232 const VoidCallback& callback) OVERRIDE {
233 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback, SUCCESS));
234 }
235
236 // FlimflamIPConfigClient override.
237 virtual void ClearProperty(const std::string& name,
238 const VoidCallback& callback) OVERRIDE {
239 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback, SUCCESS));
240 }
241
242 // FlimflamIPConfigClient override.
243 virtual void Remove(const VoidCallback& callback) OVERRIDE {
244 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback, SUCCESS));
245 }
246
247 private:
248 // Runs callback with |properties_|.
249 void PassProperties(const DictionaryValueCallback& callback) const {
250 callback.Run(SUCCESS, properties_);
251 }
252
253 base::WeakPtrFactory<FlimflamIPConfigClientStubImpl> weak_ptr_factory_;
254 base::DictionaryValue properties_;
255
256 DISALLOW_COPY_AND_ASSIGN(FlimflamIPConfigClientStubImpl);
257 };
258
259 } // namespace
260
261 FlimflamIPConfigClient::FlimflamIPConfigClient() {}
262
263 FlimflamIPConfigClient::~FlimflamIPConfigClient() {}
264
265 // static
266 FlimflamIPConfigClient* FlimflamIPConfigClient::Create(dbus::Bus* bus) {
267 if (base::chromeos::IsRunningOnChromeOS())
268 return new FlimflamIPConfigClientImpl(bus);
269 else
270 return new FlimflamIPConfigClientStubImpl();
271 }
272
273 } // namespace chromeos
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/dbus/flimflam_ipconfig_client.h ('k') | chrome/browser/chromeos/dbus/mock_dbus_thread_manager.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698