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

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

Issue 11192024: Add chromeos::NetworkStateManager (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address feedback 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 173 matching lines...) Expand 10 before | Expand all | Expand 10 after
184 184
185 // A stub implementation of ShillServiceClient. 185 // A stub implementation of ShillServiceClient.
186 class ShillServiceClientStubImpl : public ShillServiceClient, 186 class ShillServiceClientStubImpl : public ShillServiceClient,
187 public ShillServiceClient::TestInterface { 187 public ShillServiceClient::TestInterface {
188 public: 188 public:
189 ShillServiceClientStubImpl() : weak_ptr_factory_(this) { 189 ShillServiceClientStubImpl() : weak_ptr_factory_(this) {
190 SetDefaultProperties(); 190 SetDefaultProperties();
191 } 191 }
192 192
193 virtual ~ShillServiceClientStubImpl() { 193 virtual ~ShillServiceClientStubImpl() {
194 STLDeleteContainerPairSecondPointers(
195 observer_list_.begin(), observer_list_.end());
194 } 196 }
195 197
196 // ShillServiceClient overrides. 198 // ShillServiceClient overrides.
197 199
198 virtual void AddPropertyChangedObserver( 200 virtual void AddPropertyChangedObserver(
199 const dbus::ObjectPath& service_path, 201 const dbus::ObjectPath& service_path,
200 ShillPropertyChangedObserver* observer) OVERRIDE { 202 ShillPropertyChangedObserver* observer) OVERRIDE {
201 observer_list_.AddObserver(observer); 203 GetObserverList(service_path).AddObserver(observer);
202 } 204 }
203 205
204 virtual void RemovePropertyChangedObserver( 206 virtual void RemovePropertyChangedObserver(
205 const dbus::ObjectPath& service_path, 207 const dbus::ObjectPath& service_path,
206 ShillPropertyChangedObserver* observer) OVERRIDE { 208 ShillPropertyChangedObserver* observer) OVERRIDE {
207 observer_list_.RemoveObserver(observer); 209 GetObserverList(service_path).RemoveObserver(observer);
208 } 210 }
209 211
210 virtual void GetProperties(const dbus::ObjectPath& service_path, 212 virtual void GetProperties(const dbus::ObjectPath& service_path,
211 const DictionaryValueCallback& callback) OVERRIDE { 213 const DictionaryValueCallback& callback) OVERRIDE {
212 MessageLoop::current()->PostTask( 214 MessageLoop::current()->PostTask(
213 FROM_HERE, 215 FROM_HERE,
214 base::Bind(&ShillServiceClientStubImpl::PassStubDictionaryValue, 216 base::Bind(&ShillServiceClientStubImpl::PassStubDictionaryValue,
215 weak_ptr_factory_.GetWeakPtr(), 217 weak_ptr_factory_.GetWeakPtr(),
216 service_path, 218 service_path,
217 callback)); 219 callback));
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
284 286
285 virtual ShillServiceClient::TestInterface* GetTestInterface() OVERRIDE { 287 virtual ShillServiceClient::TestInterface* GetTestInterface() OVERRIDE {
286 return this; 288 return this;
287 } 289 }
288 290
289 // ShillServiceClient::TestInterface overrides. 291 // ShillServiceClient::TestInterface overrides.
290 292
291 virtual void AddService(const std::string& service_path, 293 virtual void AddService(const std::string& service_path,
292 const std::string& name, 294 const std::string& name,
293 const std::string& type, 295 const std::string& type,
294 const std::string& state, 296 const std::string& state) OVERRIDE {
295 const std::string& security) OVERRIDE {
296 base::DictionaryValue* properties = GetServiceProperties(service_path); 297 base::DictionaryValue* properties = GetServiceProperties(service_path);
297 properties->SetWithoutPathExpansion( 298 properties->SetWithoutPathExpansion(
298 flimflam::kSSIDProperty, 299 flimflam::kSSIDProperty,
299 base::Value::CreateStringValue(service_path)); 300 base::Value::CreateStringValue(service_path));
300 properties->SetWithoutPathExpansion( 301 properties->SetWithoutPathExpansion(
301 flimflam::kNameProperty, 302 flimflam::kNameProperty,
302 base::Value::CreateStringValue(name)); 303 base::Value::CreateStringValue(name));
303 properties->SetWithoutPathExpansion( 304 properties->SetWithoutPathExpansion(
304 flimflam::kTypeProperty, 305 flimflam::kTypeProperty,
305 base::Value::CreateStringValue(type)); 306 base::Value::CreateStringValue(type));
306 properties->SetWithoutPathExpansion( 307 properties->SetWithoutPathExpansion(
307 flimflam::kStateProperty, 308 flimflam::kStateProperty,
308 base::Value::CreateStringValue(state)); 309 base::Value::CreateStringValue(state));
309 properties->SetWithoutPathExpansion(
310 flimflam::kSecurityProperty,
311 base::Value::CreateStringValue(security));
312 } 310 }
313 311
314 virtual void RemoveService(const std::string& service_path) { 312 virtual void RemoveService(const std::string& service_path) {
315 stub_services_.RemoveWithoutPathExpansion(service_path, NULL); 313 stub_services_.RemoveWithoutPathExpansion(service_path, NULL);
316 } 314 }
317 315
318 virtual void SetServiceProperty(const std::string& service_path, 316 virtual void SetServiceProperty(const std::string& service_path,
319 const std::string& property, 317 const std::string& property,
320 const base::Value& value) OVERRIDE { 318 const base::Value& value) OVERRIDE {
321 SetProperty(dbus::ObjectPath(service_path), property, value, 319 SetProperty(dbus::ObjectPath(service_path), property, value,
322 base::Bind(&base::DoNothing), ErrorCallback()); 320 base::Bind(&base::DoNothing),
321 base::Bind(&ShillServiceClientStubImpl::ErrorFunction));
323 } 322 }
324 323
325 virtual void ClearServices() OVERRIDE { 324 virtual void ClearServices() OVERRIDE {
326 stub_services_.Clear(); 325 stub_services_.Clear();
327 } 326 }
328 327
329 private: 328 private:
329 typedef ObserverList<ShillPropertyChangedObserver> PropertyObserverList;
330
330 void SetDefaultProperties() { 331 void SetDefaultProperties() {
331 // Add stub services. Note: names match Manager stub impl. 332 // Add stub services. Note: names match Manager stub impl.
332 AddService("stub_ethernet", "eth0", 333 AddService("stub_ethernet", "eth0",
333 flimflam::kTypeEthernet, 334 flimflam::kTypeEthernet,
334 flimflam::kStateOnline, 335 flimflam::kStateOnline);
335 flimflam::kSecurityNone); 336
336 AddService("stub_wifi1", "wifi1", 337 AddService("stub_wifi1", "wifi1",
337 flimflam::kTypeWifi, 338 flimflam::kTypeWifi,
338 flimflam::kStateOnline, 339 flimflam::kStateOnline);
339 flimflam::kSecurityNone); 340
340 AddService("stub_wifi2", "wifi2_PSK", 341 AddService("stub_wifi2", "wifi2_PSK",
341 flimflam::kTypeWifi, 342 flimflam::kTypeWifi,
342 flimflam::kStateIdle, 343 flimflam::kStateIdle);
343 flimflam::kSecurityPsk); 344 base::StringValue psk_value(flimflam::kSecurityPsk);
345 SetServiceProperty("stub_wifi2",
346 flimflam::kSecurityProperty,
347 psk_value);
348
344 AddService("stub_cellular1", "cellular1", 349 AddService("stub_cellular1", "cellular1",
345 flimflam::kTypeCellular, 350 flimflam::kTypeCellular,
346 flimflam::kStateIdle, 351 flimflam::kStateIdle);
347 flimflam::kSecurityNone);
348 base::StringValue technology_value(flimflam::kNetworkTechnologyGsm); 352 base::StringValue technology_value(flimflam::kNetworkTechnologyGsm);
349 SetServiceProperty("stub_cellular1", 353 SetServiceProperty("stub_cellular1",
350 flimflam::kNetworkTechnologyProperty, 354 flimflam::kNetworkTechnologyProperty,
351 technology_value); 355 technology_value);
352 } 356 }
353 357
354 void PassStubDictionaryValue(const dbus::ObjectPath& service_path, 358 void PassStubDictionaryValue(const dbus::ObjectPath& service_path,
355 const DictionaryValueCallback& callback) { 359 const DictionaryValueCallback& callback) {
356 base::DictionaryValue* dict = NULL; 360 base::DictionaryValue* dict = NULL;
357 if (!stub_services_.GetDictionaryWithoutPathExpansion( 361 if (!stub_services_.GetDictionaryWithoutPathExpansion(
(...skipping 13 matching lines...) Expand all
371 LOG(ERROR) << "Notify for unknown service: " << path; 375 LOG(ERROR) << "Notify for unknown service: " << path;
372 return; 376 return;
373 } 377 }
374 base::Value* value = NULL; 378 base::Value* value = NULL;
375 if (!dict->GetWithoutPathExpansion(property, &value)) { 379 if (!dict->GetWithoutPathExpansion(property, &value)) {
376 LOG(ERROR) << "Notify for unknown property: " 380 LOG(ERROR) << "Notify for unknown property: "
377 << path << " : " << property; 381 << path << " : " << property;
378 return; 382 return;
379 } 383 }
380 FOR_EACH_OBSERVER(ShillPropertyChangedObserver, 384 FOR_EACH_OBSERVER(ShillPropertyChangedObserver,
381 observer_list_, 385 GetObserverList(service_path),
382 OnPropertyChanged(property, *value)); 386 OnPropertyChanged(property, *value));
383 } 387 }
384 388
385 base::DictionaryValue* GetServiceProperties(const std::string& service_path) { 389 base::DictionaryValue* GetServiceProperties(const std::string& service_path) {
386 base::DictionaryValue* properties = NULL; 390 base::DictionaryValue* properties = NULL;
387 if (!stub_services_.GetDictionaryWithoutPathExpansion( 391 if (!stub_services_.GetDictionaryWithoutPathExpansion(
388 service_path, &properties)) { 392 service_path, &properties)) {
389 properties = new base::DictionaryValue; 393 properties = new base::DictionaryValue;
390 stub_services_.Set(service_path, properties); 394 stub_services_.Set(service_path, properties);
391 } 395 }
392 return properties; 396 return properties;
393 } 397 }
394 398
399 PropertyObserverList& GetObserverList(const dbus::ObjectPath& device_path) {
hashimoto 2012/11/06 01:51:24 nit: Please use pointer instead of reference if th
stevenjb 2012/11/06 03:17:03 See other comment.
400 std::map<dbus::ObjectPath, PropertyObserverList*>::iterator iter =
401 observer_list_.find(device_path);
402 if (iter != observer_list_.end())
403 return *(iter->second);
404 PropertyObserverList* observer_list = new PropertyObserverList();
405 observer_list_[device_path] = observer_list;
406 return *observer_list;
407 }
408
409 static void ErrorFunction(const std::string& error_name,
410 const std::string& error_message) {
411 LOG(ERROR) << "Shill Error: " << error_name << " : " << error_message;
412 }
395 413
396 base::DictionaryValue stub_services_; 414 base::DictionaryValue stub_services_;
397 ObserverList<ShillPropertyChangedObserver> observer_list_; 415 // Observer list for each service.
416 std::map<dbus::ObjectPath, PropertyObserverList*> observer_list_;
398 417
399 // Note: This should remain the last member so it'll be destroyed and 418 // Note: This should remain the last member so it'll be destroyed and
400 // invalidate its weak pointers before any other members are destroyed. 419 // invalidate its weak pointers before any other members are destroyed.
401 base::WeakPtrFactory<ShillServiceClientStubImpl> weak_ptr_factory_; 420 base::WeakPtrFactory<ShillServiceClientStubImpl> weak_ptr_factory_;
402 421
403 DISALLOW_COPY_AND_ASSIGN(ShillServiceClientStubImpl); 422 DISALLOW_COPY_AND_ASSIGN(ShillServiceClientStubImpl);
404 }; 423 };
405 424
406 } // namespace 425 } // namespace
407 426
408 ShillServiceClient::ShillServiceClient() {} 427 ShillServiceClient::ShillServiceClient() {}
409 428
410 ShillServiceClient::~ShillServiceClient() {} 429 ShillServiceClient::~ShillServiceClient() {}
411 430
412 // static 431 // static
413 ShillServiceClient* ShillServiceClient::Create( 432 ShillServiceClient* ShillServiceClient::Create(
414 DBusClientImplementationType type, 433 DBusClientImplementationType type,
415 dbus::Bus* bus) { 434 dbus::Bus* bus) {
416 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) 435 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION)
417 return new ShillServiceClientImpl(bus); 436 return new ShillServiceClientImpl(bus);
418 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); 437 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type);
419 return new ShillServiceClientStubImpl(); 438 return new ShillServiceClientStubImpl();
420 } 439 }
421 440
422 } // namespace chromeos 441 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698