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

Side by Side Diff: chromeos/dbus/flimflam_service_client.cc

Issue 10915106: Renaming instances of "flimflam" with "shill", now that we're only (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Upload after merge Created 8 years, 3 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
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 "chromeos/dbus/flimflam_service_client.h"
6
7 #include "base/bind.h"
8 #include "base/chromeos/chromeos_version.h"
9 #include "base/message_loop.h"
10 #include "base/stl_util.h"
11 #include "base/values.h"
12 #include "dbus/bus.h"
13 #include "dbus/message.h"
14 #include "dbus/object_proxy.h"
15 #include "third_party/cros_system_api/dbus/service_constants.h"
16
17 namespace chromeos {
18
19 namespace {
20
21 // Error callback for GetProperties.
22 void OnGetPropertiesError(
23 const dbus::ObjectPath& service_path,
24 const FlimflamServiceClient::DictionaryValueCallback& callback,
25 const std::string& error_name,
26 const std::string& error_message) {
27 const std::string log_string =
28 "Failed to call org.chromium.flimflam.Service.GetProperties for: " +
29 service_path.value() + ": " + error_name + ": " + error_message;
30
31 // Suppress ERROR log if error name is
32 // "org.freedesktop.DBus.Error.UnknownMethod". crbug.com/130660
33 if (error_name == DBUS_ERROR_UNKNOWN_METHOD)
34 VLOG(1) << log_string;
35 else
36 LOG(ERROR) << log_string;
37
38 base::DictionaryValue empty_dictionary;
39 callback.Run(DBUS_METHOD_CALL_FAILURE, empty_dictionary);
40 }
41
42 // The FlimflamServiceClient implementation.
43 class FlimflamServiceClientImpl : public FlimflamServiceClient {
44 public:
45 explicit FlimflamServiceClientImpl(dbus::Bus* bus)
46 : bus_(bus),
47 helpers_deleter_(&helpers_) {
48 }
49
50 // FlimflamServiceClient override.
51 virtual void SetPropertyChangedHandler(
52 const dbus::ObjectPath& service_path,
53 const PropertyChangedHandler& handler) OVERRIDE {
54 GetHelper(service_path)->SetPropertyChangedHandler(handler);
55 }
56
57 // FlimflamServiceClient override.
58 virtual void ResetPropertyChangedHandler(
59 const dbus::ObjectPath& service_path) OVERRIDE {
60 GetHelper(service_path)->ResetPropertyChangedHandler();
61 }
62
63 // FlimflamServiceClient override.
64 virtual void GetProperties(const dbus::ObjectPath& service_path,
65 const DictionaryValueCallback& callback) OVERRIDE {
66 dbus::MethodCall method_call(flimflam::kFlimflamServiceInterface,
67 flimflam::kGetPropertiesFunction);
68 GetHelper(service_path)->CallDictionaryValueMethodWithErrorCallback(
69 &method_call,
70 base::Bind(callback, DBUS_METHOD_CALL_SUCCESS),
71 base::Bind(&OnGetPropertiesError, service_path, callback));
72 }
73
74 // FlimflamServiceClient override.
75 virtual void SetProperty(const dbus::ObjectPath& service_path,
76 const std::string& name,
77 const base::Value& value,
78 const VoidDBusMethodCallback& callback) OVERRIDE {
79 dbus::MethodCall method_call(flimflam::kFlimflamServiceInterface,
80 flimflam::kSetPropertyFunction);
81 dbus::MessageWriter writer(&method_call);
82 writer.AppendString(name);
83 FlimflamClientHelper::AppendValueDataAsVariant(&writer, value);
84 GetHelper(service_path)->CallVoidMethod(&method_call, callback);
85 }
86
87 // FlimflamServiceClient override.
88 virtual void ClearProperty(const dbus::ObjectPath& service_path,
89 const std::string& name,
90 const VoidDBusMethodCallback& callback) OVERRIDE {
91 dbus::MethodCall method_call(flimflam::kFlimflamServiceInterface,
92 flimflam::kClearPropertyFunction);
93 dbus::MessageWriter writer(&method_call);
94 writer.AppendString(name);
95 GetHelper(service_path)->CallVoidMethod(&method_call, callback);
96 }
97
98 // FlimflamServiceClient override.
99 virtual void Connect(const dbus::ObjectPath& service_path,
100 const base::Closure& callback,
101 const ErrorCallback& error_callback) OVERRIDE {
102 dbus::MethodCall method_call(flimflam::kFlimflamServiceInterface,
103 flimflam::kConnectFunction);
104 GetHelper(service_path)->CallVoidMethodWithErrorCallback(
105 &method_call, callback, error_callback);
106 }
107
108 // FlimflamServiceClient override.
109 virtual void Disconnect(const dbus::ObjectPath& service_path,
110 const VoidDBusMethodCallback& callback) OVERRIDE {
111 dbus::MethodCall method_call(flimflam::kFlimflamServiceInterface,
112 flimflam::kDisconnectFunction);
113 GetHelper(service_path)->CallVoidMethod(&method_call, callback);
114 }
115
116 // FlimflamServiceClient override.
117 virtual void Remove(const dbus::ObjectPath& service_path,
118 const VoidDBusMethodCallback& callback) OVERRIDE {
119 dbus::MethodCall method_call(flimflam::kFlimflamServiceInterface,
120 flimflam::kRemoveServiceFunction);
121 GetHelper(service_path)->CallVoidMethod(&method_call, callback);
122 }
123
124 // FlimflamServiceClient override.
125 virtual void ActivateCellularModem(
126 const dbus::ObjectPath& service_path,
127 const std::string& carrier,
128 const VoidDBusMethodCallback& callback) OVERRIDE {
129 dbus::MethodCall method_call(flimflam::kFlimflamServiceInterface,
130 flimflam::kActivateCellularModemFunction);
131 dbus::MessageWriter writer(&method_call);
132 writer.AppendString(carrier);
133 GetHelper(service_path)->CallVoidMethod(&method_call, callback);
134 }
135
136 // FlimflamServiceClient override.
137 virtual bool CallActivateCellularModemAndBlock(
138 const dbus::ObjectPath& service_path,
139 const std::string& carrier) OVERRIDE {
140 dbus::MethodCall method_call(flimflam::kFlimflamServiceInterface,
141 flimflam::kActivateCellularModemFunction);
142 dbus::MessageWriter writer(&method_call);
143 writer.AppendString(carrier);
144 return GetHelper(service_path)->CallVoidMethodAndBlock(&method_call);
145 }
146
147 private:
148 typedef std::map<std::string, FlimflamClientHelper*> HelperMap;
149
150 // Returns the corresponding FlimflamClientHelper for the profile.
151 FlimflamClientHelper* GetHelper(const dbus::ObjectPath& service_path) {
152 HelperMap::iterator it = helpers_.find(service_path.value());
153 if (it != helpers_.end())
154 return it->second;
155
156 // There is no helper for the profile, create it.
157 dbus::ObjectProxy* object_proxy =
158 bus_->GetObjectProxy(flimflam::kFlimflamServiceName, service_path);
159 FlimflamClientHelper* helper = new FlimflamClientHelper(bus_, object_proxy);
160 helper->MonitorPropertyChanged(flimflam::kFlimflamServiceInterface);
161 helpers_.insert(HelperMap::value_type(service_path.value(), helper));
162 return helper;
163 }
164
165 dbus::Bus* bus_;
166 HelperMap helpers_;
167 STLValueDeleter<HelperMap> helpers_deleter_;
168
169 DISALLOW_COPY_AND_ASSIGN(FlimflamServiceClientImpl);
170 };
171
172 // A stub implementation of FlimflamServiceClient.
173 class FlimflamServiceClientStubImpl : public FlimflamServiceClient {
174 public:
175 FlimflamServiceClientStubImpl() : weak_ptr_factory_(this) {}
176
177 virtual ~FlimflamServiceClientStubImpl() {}
178
179 // FlimflamServiceClient override.
180 virtual void SetPropertyChangedHandler(
181 const dbus::ObjectPath& service_path,
182 const PropertyChangedHandler& handler) OVERRIDE {}
183
184 // FlimflamServiceClient override.
185 virtual void ResetPropertyChangedHandler(
186 const dbus::ObjectPath& service_path) OVERRIDE {}
187
188 // FlimflamServiceClient override.
189 virtual void GetProperties(const dbus::ObjectPath& service_path,
190 const DictionaryValueCallback& callback) OVERRIDE {
191 MessageLoop::current()->PostTask(
192 FROM_HERE,
193 base::Bind(&FlimflamServiceClientStubImpl::PassEmptyDictionaryValue,
194 weak_ptr_factory_.GetWeakPtr(),
195 callback));
196 }
197
198 // FlimflamServiceClient override.
199 virtual void SetProperty(const dbus::ObjectPath& service_path,
200 const std::string& name,
201 const base::Value& value,
202 const VoidDBusMethodCallback& callback) OVERRIDE {
203 PostSuccessVoidCallback(callback);
204 }
205
206 // FlimflamServiceClient override.
207 virtual void ClearProperty(const dbus::ObjectPath& service_path,
208 const std::string& name,
209 const VoidDBusMethodCallback& callback) OVERRIDE {
210 PostSuccessVoidCallback(callback);
211 }
212
213 // FlimflamServiceClient override.
214 virtual void Connect(const dbus::ObjectPath& service_path,
215 const base::Closure& callback,
216 const ErrorCallback& error_callback) OVERRIDE {
217 MessageLoop::current()->PostTask(FROM_HERE, callback);
218 }
219
220 // FlimflamServiceClient override.
221 virtual void Disconnect(const dbus::ObjectPath& service_path,
222 const VoidDBusMethodCallback& callback) OVERRIDE {
223 PostSuccessVoidCallback(callback);
224 }
225
226 // FlimflamServiceClient override.
227 virtual void Remove(const dbus::ObjectPath& service_path,
228 const VoidDBusMethodCallback& callback) OVERRIDE {
229 PostSuccessVoidCallback(callback);
230 }
231
232 // FlimflamServiceClient override.
233 virtual void ActivateCellularModem(
234 const dbus::ObjectPath& service_path,
235 const std::string& carrier,
236 const VoidDBusMethodCallback& callback) OVERRIDE {
237 PostSuccessVoidCallback(callback);
238 }
239
240 // FlimflamServiceClient override.
241 virtual bool CallActivateCellularModemAndBlock(
242 const dbus::ObjectPath& service_path,
243 const std::string& carrier) OVERRIDE {
244 return true;
245 }
246
247 private:
248 void PassEmptyDictionaryValue(const DictionaryValueCallback& callback) const {
249 base::DictionaryValue dictionary;
250 callback.Run(DBUS_METHOD_CALL_SUCCESS, dictionary);
251 }
252
253 // Posts a task to run a void callback with success status code.
254 void PostSuccessVoidCallback(const VoidDBusMethodCallback& callback) {
255 MessageLoop::current()->PostTask(FROM_HERE,
256 base::Bind(callback,
257 DBUS_METHOD_CALL_SUCCESS));
258 }
259
260 // Note: This should remain the last member so it'll be destroyed and
261 // invalidate its weak pointers before any other members are destroyed.
262 base::WeakPtrFactory<FlimflamServiceClientStubImpl> weak_ptr_factory_;
263
264 DISALLOW_COPY_AND_ASSIGN(FlimflamServiceClientStubImpl);
265 };
266
267 } // namespace
268
269 FlimflamServiceClient::FlimflamServiceClient() {}
270
271 FlimflamServiceClient::~FlimflamServiceClient() {}
272
273 // static
274 FlimflamServiceClient* FlimflamServiceClient::Create(
275 DBusClientImplementationType type,
276 dbus::Bus* bus) {
277 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION)
278 return new FlimflamServiceClientImpl(bus);
279 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type);
280 return new FlimflamServiceClientStubImpl();
281 }
282
283 } // namespace chromeos
OLDNEW
« no previous file with comments | « chromeos/dbus/flimflam_service_client.h ('k') | chromeos/dbus/flimflam_service_client_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698