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

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

Issue 11365022: Add TestInterface methods for adding shill stub devices and services (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Yet another clang fix Created 8 years, 1 month 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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chromeos/dbus/shill_service_client.h" 5 #include "chromeos/dbus/shill_service_client.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/chromeos/chromeos_version.h" 8 #include "base/chromeos/chromeos_version.h"
9 #include "base/message_loop.h" 9 #include "base/message_loop.h"
10 #include "base/stl_util.h" 10 #include "base/stl_util.h"
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 virtual bool CallActivateCellularModemAndBlock( 146 virtual bool CallActivateCellularModemAndBlock(
147 const dbus::ObjectPath& service_path, 147 const dbus::ObjectPath& service_path,
148 const std::string& carrier) OVERRIDE { 148 const std::string& carrier) OVERRIDE {
149 dbus::MethodCall method_call(flimflam::kFlimflamServiceInterface, 149 dbus::MethodCall method_call(flimflam::kFlimflamServiceInterface,
150 flimflam::kActivateCellularModemFunction); 150 flimflam::kActivateCellularModemFunction);
151 dbus::MessageWriter writer(&method_call); 151 dbus::MessageWriter writer(&method_call);
152 writer.AppendString(carrier); 152 writer.AppendString(carrier);
153 return GetHelper(service_path)->CallVoidMethodAndBlock(&method_call); 153 return GetHelper(service_path)->CallVoidMethodAndBlock(&method_call);
154 } 154 }
155 155
156 virtual ShillServiceClient::TestInterface* GetTestInterface() OVERRIDE {
157 return NULL;
158 }
159
156 private: 160 private:
157 typedef std::map<std::string, ShillClientHelper*> HelperMap; 161 typedef std::map<std::string, ShillClientHelper*> HelperMap;
158 162
159 // Returns the corresponding ShillClientHelper for the profile. 163 // Returns the corresponding ShillClientHelper for the profile.
160 ShillClientHelper* GetHelper(const dbus::ObjectPath& service_path) { 164 ShillClientHelper* GetHelper(const dbus::ObjectPath& service_path) {
161 HelperMap::iterator it = helpers_.find(service_path.value()); 165 HelperMap::iterator it = helpers_.find(service_path.value());
162 if (it != helpers_.end()) 166 if (it != helpers_.end())
163 return it->second; 167 return it->second;
164 168
165 // There is no helper for the profile, create it. 169 // There is no helper for the profile, create it.
166 dbus::ObjectProxy* object_proxy = 170 dbus::ObjectProxy* object_proxy =
167 bus_->GetObjectProxy(flimflam::kFlimflamServiceName, service_path); 171 bus_->GetObjectProxy(flimflam::kFlimflamServiceName, service_path);
168 ShillClientHelper* helper = new ShillClientHelper(bus_, object_proxy); 172 ShillClientHelper* helper = new ShillClientHelper(bus_, object_proxy);
169 helper->MonitorPropertyChanged(flimflam::kFlimflamServiceInterface); 173 helper->MonitorPropertyChanged(flimflam::kFlimflamServiceInterface);
170 helpers_.insert(HelperMap::value_type(service_path.value(), helper)); 174 helpers_.insert(HelperMap::value_type(service_path.value(), helper));
171 return helper; 175 return helper;
172 } 176 }
173 177
174 dbus::Bus* bus_; 178 dbus::Bus* bus_;
175 HelperMap helpers_; 179 HelperMap helpers_;
176 STLValueDeleter<HelperMap> helpers_deleter_; 180 STLValueDeleter<HelperMap> helpers_deleter_;
177 181
178 DISALLOW_COPY_AND_ASSIGN(ShillServiceClientImpl); 182 DISALLOW_COPY_AND_ASSIGN(ShillServiceClientImpl);
179 }; 183 };
180 184
181 // A stub implementation of ShillServiceClient. 185 // A stub implementation of ShillServiceClient.
182 class ShillServiceClientStubImpl : public ShillServiceClient { 186 class ShillServiceClientStubImpl : public ShillServiceClient,
187 public ShillServiceClient::TestInterface {
183 public: 188 public:
184 ShillServiceClientStubImpl() : weak_ptr_factory_(this) {} 189 ShillServiceClientStubImpl() : weak_ptr_factory_(this) {
190 SetDefaultProperties();
191 }
185 192
186 virtual ~ShillServiceClientStubImpl() {} 193 virtual ~ShillServiceClientStubImpl() {
194 }
187 195
188 ///////////////////////////////////
189 // ShillServiceClient overrides. 196 // ShillServiceClient overrides.
197
190 virtual void AddPropertyChangedObserver( 198 virtual void AddPropertyChangedObserver(
191 const dbus::ObjectPath& service_path, 199 const dbus::ObjectPath& service_path,
192 ShillPropertyChangedObserver* observer) OVERRIDE {} 200 ShillPropertyChangedObserver* observer) OVERRIDE {
201 observer_list_.AddObserver(observer);
202 }
193 203
194 virtual void RemovePropertyChangedObserver( 204 virtual void RemovePropertyChangedObserver(
195 const dbus::ObjectPath& service_path, 205 const dbus::ObjectPath& service_path,
196 ShillPropertyChangedObserver* observer) OVERRIDE {} 206 ShillPropertyChangedObserver* observer) OVERRIDE {
207 observer_list_.RemoveObserver(observer);
208 }
197 209
198 virtual void GetProperties(const dbus::ObjectPath& service_path, 210 virtual void GetProperties(const dbus::ObjectPath& service_path,
199 const DictionaryValueCallback& callback) OVERRIDE { 211 const DictionaryValueCallback& callback) OVERRIDE {
200 MessageLoop::current()->PostTask( 212 MessageLoop::current()->PostTask(
201 FROM_HERE, 213 FROM_HERE,
202 base::Bind(&ShillServiceClientStubImpl::PassEmptyDictionaryValue, 214 base::Bind(&ShillServiceClientStubImpl::PassStubDictionaryValue,
203 weak_ptr_factory_.GetWeakPtr(), 215 weak_ptr_factory_.GetWeakPtr(),
216 service_path,
204 callback)); 217 callback));
205 } 218 }
206 219
207 virtual void SetProperty(const dbus::ObjectPath& service_path, 220 virtual void SetProperty(const dbus::ObjectPath& service_path,
208 const std::string& name, 221 const std::string& name,
209 const base::Value& value, 222 const base::Value& value,
210 const base::Closure& callback, 223 const base::Closure& callback,
211 const ErrorCallback& error_callback) OVERRIDE { 224 const ErrorCallback& error_callback) OVERRIDE {
225 base::DictionaryValue* dict = NULL;
226 if (!stub_services_.GetDictionaryWithoutPathExpansion(
227 service_path.value(), &dict)) {
228 error_callback.Run("StubError", "Service not found");
229 return;
230 }
231 dict->SetWithoutPathExpansion(name, value.DeepCopy());
212 MessageLoop::current()->PostTask(FROM_HERE, callback); 232 MessageLoop::current()->PostTask(FROM_HERE, callback);
233 MessageLoop::current()->PostTask(
234 FROM_HERE,
235 base::Bind(&ShillServiceClientStubImpl::NotifyObserversPropertyChanged,
236 weak_ptr_factory_.GetWeakPtr(), service_path, name));
213 } 237 }
214 238
215 virtual void ClearProperty(const dbus::ObjectPath& service_path, 239 virtual void ClearProperty(const dbus::ObjectPath& service_path,
216 const std::string& name, 240 const std::string& name,
217 const base::Closure& callback, 241 const base::Closure& callback,
218 const ErrorCallback& error_callback) OVERRIDE { 242 const ErrorCallback& error_callback) OVERRIDE {
243 base::DictionaryValue* dict = NULL;
244 if (!stub_services_.GetDictionaryWithoutPathExpansion(
245 service_path.value(), &dict)) {
246 error_callback.Run("StubError", "Service not found");
247 return;
248 }
249 dict->Remove(name, NULL);
219 MessageLoop::current()->PostTask(FROM_HERE, callback); 250 MessageLoop::current()->PostTask(FROM_HERE, callback);
220 } 251 }
221 252
222 virtual void Connect(const dbus::ObjectPath& service_path, 253 virtual void Connect(const dbus::ObjectPath& service_path,
223 const base::Closure& callback, 254 const base::Closure& callback,
224 const ErrorCallback& error_callback) OVERRIDE { 255 const ErrorCallback& error_callback) OVERRIDE {
225 MessageLoop::current()->PostTask(FROM_HERE, callback); 256 MessageLoop::current()->PostTask(FROM_HERE, callback);
226 } 257 }
227 258
228 virtual void Disconnect(const dbus::ObjectPath& service_path, 259 virtual void Disconnect(const dbus::ObjectPath& service_path,
(...skipping 15 matching lines...) Expand all
244 const ErrorCallback& error_callback) OVERRIDE { 275 const ErrorCallback& error_callback) OVERRIDE {
245 MessageLoop::current()->PostTask(FROM_HERE, callback); 276 MessageLoop::current()->PostTask(FROM_HERE, callback);
246 } 277 }
247 278
248 virtual bool CallActivateCellularModemAndBlock( 279 virtual bool CallActivateCellularModemAndBlock(
249 const dbus::ObjectPath& service_path, 280 const dbus::ObjectPath& service_path,
250 const std::string& carrier) OVERRIDE { 281 const std::string& carrier) OVERRIDE {
251 return true; 282 return true;
252 } 283 }
253 284
285 virtual ShillServiceClient::TestInterface* GetTestInterface() OVERRIDE {
286 return this;
287 }
288
289 // ShillServiceClient::TestInterface overrides.
290
291 virtual void AddService(const std::string& service_path,
292 const std::string& name,
293 const std::string& type,
294 const std::string& state,
295 const std::string& security) OVERRIDE {
296 base::DictionaryValue* properties = GetServiceProperties(service_path);
297 properties->SetWithoutPathExpansion(
298 flimflam::kSSIDProperty,
299 base::Value::CreateStringValue(service_path));
300 properties->SetWithoutPathExpansion(
301 flimflam::kNameProperty,
302 base::Value::CreateStringValue(name));
303 properties->SetWithoutPathExpansion(
304 flimflam::kTypeProperty,
305 base::Value::CreateStringValue(type));
306 properties->SetWithoutPathExpansion(
307 flimflam::kStateProperty,
308 base::Value::CreateStringValue(state));
309 properties->SetWithoutPathExpansion(
310 flimflam::kSecurityProperty,
311 base::Value::CreateStringValue(security));
312 }
313
314 virtual void RemoveService(const std::string& service_path) {
315 stub_services_.RemoveWithoutPathExpansion(service_path, NULL);
316 }
317
318 virtual void SetServiceProperty(const std::string& service_path,
319 const std::string& property,
320 const base::Value& value) OVERRIDE {
321 SetProperty(dbus::ObjectPath(service_path), property, value,
322 base::Bind(&base::DoNothing), ErrorCallback());
323 }
324
325 virtual void ClearServices() OVERRIDE {
326 stub_services_.Clear();
327 }
328
254 private: 329 private:
255 void PassEmptyDictionaryValue(const DictionaryValueCallback& callback) const { 330 void SetDefaultProperties() {
256 base::DictionaryValue dictionary; 331 // Add stub services. Note: names match Manager stub impl.
257 callback.Run(DBUS_METHOD_CALL_SUCCESS, dictionary); 332 AddService("stub_ethernet", "eth0",
333 flimflam::kTypeEthernet,
334 flimflam::kStateOnline,
335 flimflam::kSecurityNone);
336 AddService("stub_wifi1", "wifi1",
337 flimflam::kTypeWifi,
338 flimflam::kStateOnline,
339 flimflam::kSecurityNone);
340 AddService("stub_wifi2", "wifi2_PSK",
341 flimflam::kTypeWifi,
342 flimflam::kStateIdle,
343 flimflam::kSecurityPsk);
344 AddService("stub_cellular1", "cellular1",
345 flimflam::kTypeCellular,
346 flimflam::kStateIdle,
347 flimflam::kSecurityNone);
348 base::StringValue technology_value(flimflam::kNetworkTechnologyGsm);
349 SetServiceProperty("stub_cellular1",
350 flimflam::kNetworkTechnologyProperty,
351 technology_value);
258 } 352 }
259 353
354 void PassStubDictionaryValue(const dbus::ObjectPath& service_path,
355 const DictionaryValueCallback& callback) {
356 base::DictionaryValue* dict = NULL;
357 if (!stub_services_.GetDictionaryWithoutPathExpansion(
358 service_path.value(), &dict)) {
359 base::DictionaryValue empty_dictionary;
360 callback.Run(DBUS_METHOD_CALL_FAILURE, empty_dictionary);
361 return;
362 }
363 callback.Run(DBUS_METHOD_CALL_SUCCESS, *dict);
364 }
365
366 void NotifyObserversPropertyChanged(const dbus::ObjectPath& service_path,
367 const std::string& property) {
368 base::DictionaryValue* dict = NULL;
369 std::string path = service_path.value();
370 if (!stub_services_.GetDictionaryWithoutPathExpansion(path, &dict)) {
371 LOG(ERROR) << "Notify for unknown service: " << path;
372 return;
373 }
374 base::Value* value = NULL;
375 if (!dict->GetWithoutPathExpansion(property, &value)) {
376 LOG(ERROR) << "Notify for unknown property: "
377 << path << " : " << property;
378 return;
379 }
380 FOR_EACH_OBSERVER(ShillPropertyChangedObserver,
381 observer_list_,
382 OnPropertyChanged(property, *value));
383 }
384
385 base::DictionaryValue* GetServiceProperties(const std::string& service_path) {
386 base::DictionaryValue* properties = NULL;
387 if (!stub_services_.GetDictionaryWithoutPathExpansion(
388 service_path, &properties)) {
389 properties = new base::DictionaryValue;
390 stub_services_.Set(service_path, properties);
391 }
392 return properties;
393 }
394
395
396 base::DictionaryValue stub_services_;
397 ObserverList<ShillPropertyChangedObserver> observer_list_;
398
260 // Note: This should remain the last member so it'll be destroyed and 399 // Note: This should remain the last member so it'll be destroyed and
261 // invalidate its weak pointers before any other members are destroyed. 400 // invalidate its weak pointers before any other members are destroyed.
262 base::WeakPtrFactory<ShillServiceClientStubImpl> weak_ptr_factory_; 401 base::WeakPtrFactory<ShillServiceClientStubImpl> weak_ptr_factory_;
263 402
264 DISALLOW_COPY_AND_ASSIGN(ShillServiceClientStubImpl); 403 DISALLOW_COPY_AND_ASSIGN(ShillServiceClientStubImpl);
265 }; 404 };
266 405
267 } // namespace 406 } // namespace
268 407
269 ShillServiceClient::ShillServiceClient() {} 408 ShillServiceClient::ShillServiceClient() {}
270 409
271 ShillServiceClient::~ShillServiceClient() {} 410 ShillServiceClient::~ShillServiceClient() {}
272 411
273 // static 412 // static
274 ShillServiceClient* ShillServiceClient::Create( 413 ShillServiceClient* ShillServiceClient::Create(
275 DBusClientImplementationType type, 414 DBusClientImplementationType type,
276 dbus::Bus* bus) { 415 dbus::Bus* bus) {
277 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) 416 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION)
278 return new ShillServiceClientImpl(bus); 417 return new ShillServiceClientImpl(bus);
279 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); 418 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type);
280 return new ShillServiceClientStubImpl(); 419 return new ShillServiceClientStubImpl();
281 } 420 }
282 421
283 } // namespace chromeos 422 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698