| OLD | NEW | 
|---|
| 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_manager_client.h" | 5 #include "chromeos/dbus/shill_manager_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/values.h" | 10 #include "base/values.h" | 
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 154       const ErrorCallback& error_callback) OVERRIDE { | 154       const ErrorCallback& error_callback) OVERRIDE { | 
| 155     dbus::MethodCall method_call(flimflam::kFlimflamManagerInterface, | 155     dbus::MethodCall method_call(flimflam::kFlimflamManagerInterface, | 
| 156                                  flimflam::kGetServiceFunction); | 156                                  flimflam::kGetServiceFunction); | 
| 157     dbus::MessageWriter writer(&method_call); | 157     dbus::MessageWriter writer(&method_call); | 
| 158     AppendServicePropertiesDictionary(&writer, properties); | 158     AppendServicePropertiesDictionary(&writer, properties); | 
| 159     helper_.CallObjectPathMethodWithErrorCallback(&method_call, | 159     helper_.CallObjectPathMethodWithErrorCallback(&method_call, | 
| 160                                                   callback, | 160                                                   callback, | 
| 161                                                   error_callback); | 161                                                   error_callback); | 
| 162   } | 162   } | 
| 163 | 163 | 
|  | 164   virtual TestInterface* GetTestInterface() OVERRIDE { | 
|  | 165     return NULL; | 
|  | 166   } | 
|  | 167 | 
| 164  private: | 168  private: | 
| 165   dbus::ObjectProxy* proxy_; | 169   dbus::ObjectProxy* proxy_; | 
| 166   ShillClientHelper helper_; | 170   ShillClientHelper helper_; | 
| 167 | 171 | 
| 168   DISALLOW_COPY_AND_ASSIGN(ShillManagerClientImpl); | 172   DISALLOW_COPY_AND_ASSIGN(ShillManagerClientImpl); | 
| 169 }; | 173 }; | 
| 170 | 174 | 
|  | 175 namespace { | 
|  | 176 | 
|  | 177 struct ValueEquals { | 
|  | 178   ValueEquals(const Value* first) : first_(first) {} | 
|  | 179   bool operator ()(const Value* second) const { | 
|  | 180     return first_->Equals(second); | 
|  | 181   } | 
|  | 182   const Value* first_; | 
|  | 183 }; | 
|  | 184 | 
|  | 185 }  // namespace | 
|  | 186 | 
| 171 // A stub implementation of ShillManagerClient. | 187 // A stub implementation of ShillManagerClient. | 
| 172 // Implemented: Stub cellular DeviceList entry for SMS testing. | 188 // Implemented: Stub devices and services for NetworkStateManager tests. | 
| 173 class ShillManagerClientStubImpl : public ShillManagerClient { | 189 // Implemented: Stub cellular device entry for SMS tests. | 
|  | 190 class ShillManagerClientStubImpl : public ShillManagerClient, | 
|  | 191                                    public ShillManagerClient::TestInterface { | 
| 174  public: | 192  public: | 
| 175   ShillManagerClientStubImpl() : weak_ptr_factory_(this) { | 193   ShillManagerClientStubImpl() | 
| 176     base::ListValue* device_list = new base::ListValue; | 194       : initialized_(false), | 
| 177     // Note: names match Device stub map. | 195         weak_ptr_factory_(this) { | 
| 178     const char kStubCellular1[] = "stub_cellular1"; | 196     SetDefaultProperties(); | 
| 179     const char kStubCellular2[] = "stub_cellular2"; | 197     initialized_ = true; | 
| 180     device_list->Append(base::Value::CreateStringValue(kStubCellular1)); |  | 
| 181     device_list->Append(base::Value::CreateStringValue(kStubCellular2)); |  | 
| 182     stub_properties_.Set(flimflam::kDevicesProperty, device_list); |  | 
| 183   } | 198   } | 
| 184 | 199 | 
| 185   virtual ~ShillManagerClientStubImpl() {} | 200   virtual ~ShillManagerClientStubImpl() {} | 
| 186 | 201 | 
| 187   ////////////////////////////////// |  | 
| 188   // ShillManagerClient overrides. | 202   // ShillManagerClient overrides. | 
|  | 203 | 
| 189   virtual void AddPropertyChangedObserver( | 204   virtual void AddPropertyChangedObserver( | 
| 190       ShillPropertyChangedObserver* observer) OVERRIDE {} | 205       ShillPropertyChangedObserver* observer) OVERRIDE { | 
|  | 206     observer_list_.AddObserver(observer); | 
|  | 207   } | 
| 191 | 208 | 
| 192   virtual void RemovePropertyChangedObserver( | 209   virtual void RemovePropertyChangedObserver( | 
| 193       ShillPropertyChangedObserver* observer) OVERRIDE {} | 210       ShillPropertyChangedObserver* observer) OVERRIDE { | 
|  | 211     observer_list_.RemoveObserver(observer); | 
|  | 212   } | 
| 194 | 213 | 
| 195   // ShillManagerClient override. |  | 
| 196   virtual void GetProperties(const DictionaryValueCallback& callback) OVERRIDE { | 214   virtual void GetProperties(const DictionaryValueCallback& callback) OVERRIDE { | 
| 197     MessageLoop::current()->PostTask( | 215     MessageLoop::current()->PostTask( | 
| 198         FROM_HERE, base::Bind( | 216         FROM_HERE, base::Bind( | 
| 199             &ShillManagerClientStubImpl::PassStubProperties, | 217             &ShillManagerClientStubImpl::PassStubProperties, | 
| 200             weak_ptr_factory_.GetWeakPtr(), | 218             weak_ptr_factory_.GetWeakPtr(), | 
| 201             callback)); | 219             callback)); | 
| 202   } | 220   } | 
| 203 | 221 | 
| 204   // ShillManagerClient override. |  | 
| 205   virtual base::DictionaryValue* CallGetPropertiesAndBlock() OVERRIDE { | 222   virtual base::DictionaryValue* CallGetPropertiesAndBlock() OVERRIDE { | 
| 206     return new base::DictionaryValue; | 223     return stub_properties_.DeepCopy(); | 
| 207   } | 224   } | 
| 208 | 225 | 
| 209   // ShillManagerClient override. |  | 
| 210   virtual void SetProperty(const std::string& name, | 226   virtual void SetProperty(const std::string& name, | 
| 211                            const base::Value& value, | 227                            const base::Value& value, | 
| 212                            const base::Closure& callback, | 228                            const base::Closure& callback, | 
| 213                            const ErrorCallback& error_callback) OVERRIDE { | 229                            const ErrorCallback& error_callback) OVERRIDE { | 
| 214     stub_properties_.Set(name, value.DeepCopy()); | 230     stub_properties_.Set(name, value.DeepCopy()); | 
| 215     MessageLoop::current()->PostTask(FROM_HERE, callback); | 231     MessageLoop::current()->PostTask(FROM_HERE, callback); | 
| 216   } | 232   } | 
| 217 | 233 | 
| 218   // ShillManagerClient override. |  | 
| 219   virtual void RequestScan(const std::string& type, | 234   virtual void RequestScan(const std::string& type, | 
| 220                            const base::Closure& callback, | 235                            const base::Closure& callback, | 
| 221                            const ErrorCallback& error_callback) OVERRIDE { | 236                            const ErrorCallback& error_callback) OVERRIDE { | 
| 222     MessageLoop::current()->PostTask(FROM_HERE, callback); | 237     MessageLoop::current()->PostTask(FROM_HERE, callback); | 
|  | 238     const int kScanDelaySeconds = 3; | 
|  | 239     CallNotifyObserversPropertyChanged( | 
|  | 240         flimflam::kServicesProperty, kScanDelaySeconds); | 
| 223   } | 241   } | 
| 224 | 242 | 
| 225   // ShillManagerClient override. |  | 
| 226   virtual void EnableTechnology( | 243   virtual void EnableTechnology( | 
| 227       const std::string& type, | 244       const std::string& type, | 
| 228       const base::Closure& callback, | 245       const base::Closure& callback, | 
| 229       const ErrorCallback& error_callback) OVERRIDE { | 246       const ErrorCallback& error_callback) OVERRIDE { | 
|  | 247     base::ListValue* enabled_list = NULL; | 
|  | 248     if (!stub_properties_.GetListWithoutPathExpansion( | 
|  | 249             flimflam::kEnabledTechnologiesProperty, &enabled_list)) { | 
|  | 250       MessageLoop::current()->PostTask( | 
|  | 251           FROM_HERE, | 
|  | 252           base::Bind(error_callback, "StubError", "Property not found")); | 
|  | 253       return; | 
|  | 254     } | 
| 230     MessageLoop::current()->PostTask(FROM_HERE, callback); | 255     MessageLoop::current()->PostTask(FROM_HERE, callback); | 
|  | 256     enabled_list->AppendIfNotPresent(new base::StringValue(type)); | 
|  | 257     CallNotifyObserversPropertyChanged( | 
|  | 258         flimflam::kEnabledTechnologiesProperty, 0); | 
| 231   } | 259   } | 
| 232 | 260 | 
| 233   // ShillManagerClient override. |  | 
| 234   virtual void DisableTechnology( | 261   virtual void DisableTechnology( | 
| 235       const std::string& type, | 262       const std::string& type, | 
| 236       const base::Closure& callback, | 263       const base::Closure& callback, | 
| 237       const ErrorCallback& error_callback) OVERRIDE { | 264       const ErrorCallback& error_callback) OVERRIDE { | 
|  | 265     base::ListValue* enabled_list = NULL; | 
|  | 266     if (!stub_properties_.GetListWithoutPathExpansion( | 
|  | 267             flimflam::kEnabledTechnologiesProperty, &enabled_list)) { | 
|  | 268       MessageLoop::current()->PostTask( | 
|  | 269           FROM_HERE, | 
|  | 270           base::Bind(error_callback, "StubError", "Property not found")); | 
|  | 271       return; | 
|  | 272     } | 
| 238     MessageLoop::current()->PostTask(FROM_HERE, callback); | 273     MessageLoop::current()->PostTask(FROM_HERE, callback); | 
|  | 274     base::StringValue type_value(type); | 
|  | 275     enabled_list->Remove(type_value, NULL); | 
|  | 276     CallNotifyObserversPropertyChanged( | 
|  | 277         flimflam::kEnabledTechnologiesProperty, 0); | 
| 239   } | 278   } | 
| 240 | 279 | 
| 241   // ShillManagerClient override. |  | 
| 242   virtual void ConfigureService( | 280   virtual void ConfigureService( | 
| 243       const base::DictionaryValue& properties, | 281       const base::DictionaryValue& properties, | 
| 244       const base::Closure& callback, | 282       const base::Closure& callback, | 
| 245       const ErrorCallback& error_callback) OVERRIDE { | 283       const ErrorCallback& error_callback) OVERRIDE { | 
| 246     MessageLoop::current()->PostTask(FROM_HERE, callback); | 284     MessageLoop::current()->PostTask(FROM_HERE, callback); | 
| 247   } | 285   } | 
| 248 | 286 | 
| 249   // ShillManagerClient override. |  | 
| 250   virtual void GetService( | 287   virtual void GetService( | 
| 251       const base::DictionaryValue& properties, | 288       const base::DictionaryValue& properties, | 
| 252       const ObjectPathCallback& callback, | 289       const ObjectPathCallback& callback, | 
| 253       const ErrorCallback& error_callback) OVERRIDE { | 290       const ErrorCallback& error_callback) OVERRIDE { | 
| 254     MessageLoop::current()->PostTask(FROM_HERE, | 291     MessageLoop::current()->PostTask( | 
| 255                                      base::Bind(callback, | 292         FROM_HERE, base::Bind(callback, dbus::ObjectPath())); | 
| 256                                                 dbus::ObjectPath())); | 293   } | 
|  | 294 | 
|  | 295   virtual ShillManagerClient::TestInterface* GetTestInterface() OVERRIDE { | 
|  | 296     return this; | 
|  | 297   } | 
|  | 298 | 
|  | 299   // ShillManagerClient::TestInterface overrides. | 
|  | 300 | 
|  | 301   virtual void AddDevice(const std::string& device_path) OVERRIDE { | 
|  | 302     if (GetListProperty(flimflam::kDevicesProperty)->AppendIfNotPresent( | 
|  | 303             base::Value::CreateStringValue(device_path))) { | 
|  | 304       CallNotifyObserversPropertyChanged(flimflam::kDevicesProperty, 0); | 
|  | 305     } | 
|  | 306   } | 
|  | 307 | 
|  | 308   virtual void RemoveDevice(const std::string& device_path) OVERRIDE { | 
|  | 309     base::StringValue device_path_value(device_path); | 
|  | 310     if (GetListProperty(flimflam::kDevicesProperty)->Remove( | 
|  | 311             device_path_value, NULL)) { | 
|  | 312       CallNotifyObserversPropertyChanged(flimflam::kDevicesProperty, 0); | 
|  | 313     } | 
|  | 314   } | 
|  | 315 | 
|  | 316   virtual void AddService(const std::string& service_path, | 
|  | 317                           bool add_to_watch_list) OVERRIDE { | 
|  | 318     if (GetListProperty(flimflam::kServicesProperty)->AppendIfNotPresent( | 
|  | 319             base::Value::CreateStringValue(service_path))) { | 
|  | 320       CallNotifyObserversPropertyChanged(flimflam::kServicesProperty, 0); | 
|  | 321     } | 
|  | 322     if (add_to_watch_list && | 
|  | 323         GetListProperty( | 
|  | 324             flimflam::kServiceWatchListProperty)->AppendIfNotPresent( | 
|  | 325                 base::Value::CreateStringValue(service_path))) { | 
|  | 326       CallNotifyObserversPropertyChanged( | 
|  | 327           flimflam::kServiceWatchListProperty, 0); | 
|  | 328     } | 
|  | 329   } | 
|  | 330 | 
|  | 331   virtual void InsertService(const std::string& service_path, | 
|  | 332                              size_t index) OVERRIDE { | 
|  | 333     base::StringValue path_value(service_path); | 
|  | 334     base::ListValue* service_list = | 
|  | 335         GetListProperty(flimflam::kServicesProperty); | 
|  | 336     base::ListValue::iterator iter = | 
|  | 337         std::find_if(service_list->begin(), service_list->end(), | 
|  | 338                      ValueEquals(&path_value)); | 
|  | 339         service_list->Find(path_value); | 
|  | 340     if (iter != service_list->end()) | 
|  | 341       service_list->Erase(iter, NULL); | 
|  | 342     service_list->Insert(index, path_value.DeepCopy()); | 
|  | 343     CallNotifyObserversPropertyChanged(flimflam::kServicesProperty, 0); | 
|  | 344   } | 
|  | 345 | 
|  | 346   virtual void RemoveService(const std::string& service_path) OVERRIDE { | 
|  | 347     base::StringValue service_path_value(service_path); | 
|  | 348     if (GetListProperty(flimflam::kServicesProperty)->Remove( | 
|  | 349             service_path_value, NULL)) { | 
|  | 350       CallNotifyObserversPropertyChanged(flimflam::kServicesProperty, 0); | 
|  | 351     } | 
|  | 352     if (GetListProperty(flimflam::kServiceWatchListProperty)->Remove( | 
|  | 353             service_path_value, NULL)) { | 
|  | 354       CallNotifyObserversPropertyChanged( | 
|  | 355           flimflam::kServiceWatchListProperty, 0); | 
|  | 356     } | 
|  | 357   } | 
|  | 358 | 
|  | 359   virtual void AddTechnology(const std::string& type, bool enabled) OVERRIDE { | 
|  | 360     if (GetListProperty(flimflam::kAvailableTechnologiesProperty)-> | 
|  | 361         AppendIfNotPresent(base::Value::CreateStringValue(type))) { | 
|  | 362       CallNotifyObserversPropertyChanged( | 
|  | 363           flimflam::kAvailableTechnologiesProperty, 0); | 
|  | 364     } | 
|  | 365     if (enabled && | 
|  | 366         GetListProperty(flimflam::kEnabledTechnologiesProperty)-> | 
|  | 367         AppendIfNotPresent(base::Value::CreateStringValue(type))) { | 
|  | 368       CallNotifyObserversPropertyChanged( | 
|  | 369           flimflam::kEnabledTechnologiesProperty, 0); | 
|  | 370     } | 
|  | 371   } | 
|  | 372 | 
|  | 373   virtual void RemoveTechnology(const std::string& type) OVERRIDE { | 
|  | 374     base::StringValue type_value(type); | 
|  | 375     if (GetListProperty(flimflam::kAvailableTechnologiesProperty)->Remove( | 
|  | 376             type_value, NULL)) { | 
|  | 377       CallNotifyObserversPropertyChanged( | 
|  | 378           flimflam::kAvailableTechnologiesProperty, 0); | 
|  | 379     } | 
|  | 380     if (GetListProperty(flimflam::kEnabledTechnologiesProperty)->Remove( | 
|  | 381             type_value, NULL)) { | 
|  | 382       CallNotifyObserversPropertyChanged( | 
|  | 383           flimflam::kEnabledTechnologiesProperty, 0); | 
|  | 384     } | 
|  | 385   } | 
|  | 386 | 
|  | 387   virtual void ClearProperties() OVERRIDE { | 
|  | 388     stub_properties_.Clear(); | 
| 257   } | 389   } | 
| 258 | 390 | 
| 259  private: | 391  private: | 
|  | 392   void SetDefaultProperties() { | 
|  | 393     // Stub Devices, Note: names match Device stub map. | 
|  | 394     AddDevice("stub_wifi_device1"); | 
|  | 395     AddDevice("stub_cellular_device1"); | 
|  | 396 | 
|  | 397     // Stub Services, Note: names match Service stub map. | 
|  | 398     AddService("stub_ethernet", true); | 
|  | 399     AddService("stub_wifi1", true); | 
|  | 400     AddService("stub_wifi2", true); | 
|  | 401     AddService("stub_cellular1", true); | 
|  | 402 | 
|  | 403     // Stub Technologies | 
|  | 404     AddTechnology(flimflam::kTypeEthernet, true); | 
|  | 405     AddTechnology(flimflam::kTypeWifi, true); | 
|  | 406     AddTechnology(flimflam::kTypeCellular, true); | 
|  | 407   } | 
|  | 408 | 
| 260   void PassStubProperties(const DictionaryValueCallback& callback) const { | 409   void PassStubProperties(const DictionaryValueCallback& callback) const { | 
| 261     callback.Run(DBUS_METHOD_CALL_SUCCESS, stub_properties_); | 410     callback.Run(DBUS_METHOD_CALL_SUCCESS, stub_properties_); | 
| 262   } | 411   } | 
| 263 | 412 | 
|  | 413   void CallNotifyObserversPropertyChanged(const std::string& property, | 
|  | 414                                           int delay_seconds) { | 
|  | 415     if (!initialized_) | 
|  | 416       return; | 
|  | 417     MessageLoop::current()->PostDelayedTask( | 
|  | 418         FROM_HERE, | 
|  | 419         base::Bind(&ShillManagerClientStubImpl::NotifyObserversPropertyChanged, | 
|  | 420                    weak_ptr_factory_.GetWeakPtr(), | 
|  | 421                    property), | 
|  | 422         base::TimeDelta::FromSeconds(delay_seconds)); | 
|  | 423   } | 
|  | 424 | 
|  | 425   void NotifyObserversPropertyChanged(const std::string& property) { | 
|  | 426     base::Value* value = NULL; | 
|  | 427     if (!stub_properties_.GetWithoutPathExpansion(property, &value)) { | 
|  | 428       LOG(ERROR) << "Notify for unknown property: " << property; | 
|  | 429       return; | 
|  | 430     } | 
|  | 431     FOR_EACH_OBSERVER(ShillPropertyChangedObserver, | 
|  | 432                       observer_list_, | 
|  | 433                       OnPropertyChanged(property, *value)); | 
|  | 434   } | 
|  | 435 | 
|  | 436   base::ListValue* GetListProperty(const std::string& property) { | 
|  | 437     base::ListValue* list_property = NULL; | 
|  | 438     if (!stub_properties_.GetListWithoutPathExpansion( | 
|  | 439             property, &list_property)) { | 
|  | 440       list_property = new base::ListValue; | 
|  | 441       stub_properties_.Set(property, list_property); | 
|  | 442     } | 
|  | 443     return list_property; | 
|  | 444   } | 
|  | 445 | 
|  | 446   bool initialized_; | 
| 264   base::DictionaryValue stub_properties_; | 447   base::DictionaryValue stub_properties_; | 
|  | 448   ObserverList<ShillPropertyChangedObserver> observer_list_; | 
| 265 | 449 | 
| 266   // Note: This should remain the last member so it'll be destroyed and | 450   // Note: This should remain the last member so it'll be destroyed and | 
| 267   // invalidate its weak pointers before any other members are destroyed. | 451   // invalidate its weak pointers before any other members are destroyed. | 
| 268   base::WeakPtrFactory<ShillManagerClientStubImpl> weak_ptr_factory_; | 452   base::WeakPtrFactory<ShillManagerClientStubImpl> weak_ptr_factory_; | 
| 269 | 453 | 
| 270   DISALLOW_COPY_AND_ASSIGN(ShillManagerClientStubImpl); | 454   DISALLOW_COPY_AND_ASSIGN(ShillManagerClientStubImpl); | 
| 271 }; | 455 }; | 
| 272 | 456 | 
| 273 }  // namespace | 457 }  // namespace | 
| 274 | 458 | 
| 275 ShillManagerClient::ShillManagerClient() {} | 459 ShillManagerClient::ShillManagerClient() {} | 
| 276 | 460 | 
| 277 ShillManagerClient::~ShillManagerClient() {} | 461 ShillManagerClient::~ShillManagerClient() {} | 
| 278 | 462 | 
| 279 // static | 463 // static | 
| 280 ShillManagerClient* ShillManagerClient::Create( | 464 ShillManagerClient* ShillManagerClient::Create( | 
| 281     DBusClientImplementationType type, | 465     DBusClientImplementationType type, | 
| 282     dbus::Bus* bus) { | 466     dbus::Bus* bus) { | 
| 283   if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) | 467   if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) | 
| 284     return new ShillManagerClientImpl(bus); | 468     return new ShillManagerClientImpl(bus); | 
| 285   DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); | 469   DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); | 
| 286   return new ShillManagerClientStubImpl(); | 470   return new ShillManagerClientStubImpl(); | 
| 287 } | 471 } | 
| 288 | 472 | 
| 289 }  // namespace chromeos | 473 }  // namespace chromeos | 
| OLD | NEW | 
|---|