Chromium Code Reviews| 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_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 Loading... | |
| 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() { | |
|
hashimoto
2012/11/01 04:54:34
nit: OVERRIDE
stevenjb
2012/11/01 18:45:59
Done.
| |
| 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 std::string ssid = service_path.value(); | |
|
hashimoto
2012/11/01 04:54:34
Since this method is used not only for wifi networ
stevenjb
2012/11/01 18:45:59
Done.
| |
| 227 if (!stub_services_.GetDictionaryWithoutPathExpansion(ssid, &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 std::string ssid = service_path.value(); | |
| 245 if (!stub_services_.GetDictionaryWithoutPathExpansion(ssid, &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 Loading... | |
| 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() { | |
|
hashimoto
2012/11/01 04:54:34
nit: OVERRIDE
stevenjb
2012/11/01 18:45:59
Done.
| |
| 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 SetServiceProperty("stub_cellular1", | |
| 349 flimflam::kNetworkTechnologyProperty, | |
| 350 base::StringValue(flimflam::kNetworkTechnologyGsm)); | |
| 258 } | 351 } |
| 259 | 352 |
| 353 void PassStubDictionaryValue(const dbus::ObjectPath& service_path, | |
| 354 const DictionaryValueCallback& callback) { | |
| 355 base::DictionaryValue* dict = NULL; | |
| 356 std::string ssid = service_path.value(); | |
| 357 if (!stub_services_.GetDictionaryWithoutPathExpansion(ssid, &dict)) { | |
| 358 callback.Run(DBUS_METHOD_CALL_FAILURE, base::DictionaryValue()); | |
| 359 return; | |
| 360 } | |
| 361 callback.Run(DBUS_METHOD_CALL_SUCCESS, *dict); | |
| 362 } | |
| 363 | |
| 364 void NotifyObserversPropertyChanged(const dbus::ObjectPath& service_path, | |
| 365 const std::string& property) { | |
| 366 base::DictionaryValue* dict = NULL; | |
| 367 std::string ssid = service_path.value(); | |
| 368 if (!stub_services_.GetDictionaryWithoutPathExpansion(ssid, &dict)) { | |
| 369 LOG(ERROR) << "Notify for unknown service: " << ssid; | |
| 370 return; | |
| 371 } | |
| 372 base::Value* value; | |
|
hashimoto
2012/11/01 04:54:34
nit: = NULL;
stevenjb
2012/11/01 18:45:59
Done.
| |
| 373 if (!dict->GetWithoutPathExpansion(property, &value)) { | |
| 374 LOG(ERROR) << "Notify for unknown property: " | |
| 375 << ssid << " : " << property; | |
| 376 return; | |
| 377 } | |
| 378 FOR_EACH_OBSERVER(ShillPropertyChangedObserver, | |
| 379 observer_list_, | |
| 380 OnPropertyChanged(property, *value)); | |
| 381 } | |
| 382 | |
| 383 base::DictionaryValue* GetServiceProperties(const std::string& service_path) { | |
| 384 base::DictionaryValue* properties; | |
|
hashimoto
2012/11/01 04:54:34
nit: = NULL;
stevenjb
2012/11/01 18:45:59
Done.
| |
| 385 if (!stub_services_.GetDictionaryWithoutPathExpansion( | |
| 386 service_path, &properties)) { | |
| 387 properties = new base::DictionaryValue; | |
| 388 stub_services_.Set(service_path, properties); | |
| 389 } | |
| 390 return properties; | |
| 391 } | |
| 392 | |
| 393 | |
| 394 base::DictionaryValue stub_services_; | |
| 395 ObserverList<ShillPropertyChangedObserver> observer_list_; | |
| 396 | |
| 260 // Note: This should remain the last member so it'll be destroyed and | 397 // Note: This should remain the last member so it'll be destroyed and |
| 261 // invalidate its weak pointers before any other members are destroyed. | 398 // invalidate its weak pointers before any other members are destroyed. |
| 262 base::WeakPtrFactory<ShillServiceClientStubImpl> weak_ptr_factory_; | 399 base::WeakPtrFactory<ShillServiceClientStubImpl> weak_ptr_factory_; |
| 263 | 400 |
| 264 DISALLOW_COPY_AND_ASSIGN(ShillServiceClientStubImpl); | 401 DISALLOW_COPY_AND_ASSIGN(ShillServiceClientStubImpl); |
| 265 }; | 402 }; |
| 266 | 403 |
| 267 } // namespace | 404 } // namespace |
| 268 | 405 |
| 269 ShillServiceClient::ShillServiceClient() {} | 406 ShillServiceClient::ShillServiceClient() {} |
| 270 | 407 |
| 271 ShillServiceClient::~ShillServiceClient() {} | 408 ShillServiceClient::~ShillServiceClient() {} |
| 272 | 409 |
| 273 // static | 410 // static |
| 274 ShillServiceClient* ShillServiceClient::Create( | 411 ShillServiceClient* ShillServiceClient::Create( |
| 275 DBusClientImplementationType type, | 412 DBusClientImplementationType type, |
| 276 dbus::Bus* bus) { | 413 dbus::Bus* bus) { |
| 277 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) | 414 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) |
| 278 return new ShillServiceClientImpl(bus); | 415 return new ShillServiceClientImpl(bus); |
| 279 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); | 416 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); |
| 280 return new ShillServiceClientStubImpl(); | 417 return new ShillServiceClientStubImpl(); |
| 281 } | 418 } |
| 282 | 419 |
| 283 } // namespace chromeos | 420 } // namespace chromeos |
| OLD | NEW |