| 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() 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 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() 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) OVERRIDE { |
| 295 base::DictionaryValue* properties = GetServiceProperties(service_path); |
| 296 properties->SetWithoutPathExpansion( |
| 297 flimflam::kSSIDProperty, |
| 298 base::Value::CreateStringValue(service_path)); |
| 299 properties->SetWithoutPathExpansion( |
| 300 flimflam::kNameProperty, |
| 301 base::Value::CreateStringValue(name)); |
| 302 properties->SetWithoutPathExpansion( |
| 303 flimflam::kTypeProperty, |
| 304 base::Value::CreateStringValue(type)); |
| 305 properties->SetWithoutPathExpansion( |
| 306 flimflam::kStateProperty, |
| 307 base::Value::CreateStringValue(state)); |
| 308 } |
| 309 |
| 310 virtual void RemoveService(const std::string& service_path) { |
| 311 stub_services_.RemoveWithoutPathExpansion(service_path, NULL); |
| 312 } |
| 313 |
| 314 virtual void SetServiceProperty(const std::string& service_path, |
| 315 const std::string& property, |
| 316 const base::Value& value) OVERRIDE { |
| 317 SetProperty(dbus::ObjectPath(service_path), property, value, |
| 318 base::Bind(&base::DoNothing), |
| 319 base::Bind(&ShillServiceClientStubImpl::ErrorFunction)); |
| 320 } |
| 321 |
| 322 virtual void ClearServices() OVERRIDE { |
| 323 stub_services_.Clear(); |
| 324 } |
| 325 |
| 254 private: | 326 private: |
| 255 void PassEmptyDictionaryValue(const DictionaryValueCallback& callback) const { | 327 void SetDefaultProperties() { |
| 256 base::DictionaryValue dictionary; | 328 // Add stub services. Note: names match Manager stub impl. |
| 257 callback.Run(DBUS_METHOD_CALL_SUCCESS, dictionary); | 329 AddService("stub_ethernet", "eth0", |
| 330 flimflam::kTypeEthernet, |
| 331 flimflam::kStateOnline); |
| 332 |
| 333 AddService("stub_wifi1", "wifi1", |
| 334 flimflam::kTypeWifi, |
| 335 flimflam::kStateOnline); |
| 336 |
| 337 AddService("stub_wifi2", "wifi2_PSK", |
| 338 flimflam::kTypeWifi, |
| 339 flimflam::kStateIdle); |
| 340 base::StringValue psk_value(flimflam::kSecurityPsk); |
| 341 SetServiceProperty("stub_wifi2", |
| 342 flimflam::kSecurityProperty, |
| 343 psk_value); |
| 344 |
| 345 AddService("stub_cellular1", "cellular1", |
| 346 flimflam::kTypeCellular, |
| 347 flimflam::kStateIdle); |
| 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 static void ErrorFunction(const std::string& error_name, |
| 396 const std::string& error_message) { |
| 397 LOG(ERROR) << "Shill Error: " << error_name << " : " << error_message; |
| 398 } |
| 399 |
| 400 base::DictionaryValue stub_services_; |
| 401 ObserverList<ShillPropertyChangedObserver> observer_list_; |
| 402 |
| 260 // Note: This should remain the last member so it'll be destroyed and | 403 // Note: This should remain the last member so it'll be destroyed and |
| 261 // invalidate its weak pointers before any other members are destroyed. | 404 // invalidate its weak pointers before any other members are destroyed. |
| 262 base::WeakPtrFactory<ShillServiceClientStubImpl> weak_ptr_factory_; | 405 base::WeakPtrFactory<ShillServiceClientStubImpl> weak_ptr_factory_; |
| 263 | 406 |
| 264 DISALLOW_COPY_AND_ASSIGN(ShillServiceClientStubImpl); | 407 DISALLOW_COPY_AND_ASSIGN(ShillServiceClientStubImpl); |
| 265 }; | 408 }; |
| 266 | 409 |
| 267 } // namespace | 410 } // namespace |
| 268 | 411 |
| 269 ShillServiceClient::ShillServiceClient() {} | 412 ShillServiceClient::ShillServiceClient() {} |
| 270 | 413 |
| 271 ShillServiceClient::~ShillServiceClient() {} | 414 ShillServiceClient::~ShillServiceClient() {} |
| 272 | 415 |
| 273 // static | 416 // static |
| 274 ShillServiceClient* ShillServiceClient::Create( | 417 ShillServiceClient* ShillServiceClient::Create( |
| 275 DBusClientImplementationType type, | 418 DBusClientImplementationType type, |
| 276 dbus::Bus* bus) { | 419 dbus::Bus* bus) { |
| 277 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) | 420 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) |
| 278 return new ShillServiceClientImpl(bus); | 421 return new ShillServiceClientImpl(bus); |
| 279 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); | 422 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); |
| 280 return new ShillServiceClientStubImpl(); | 423 return new ShillServiceClientStubImpl(); |
| 281 } | 424 } |
| 282 | 425 |
| 283 } // namespace chromeos | 426 } // namespace chromeos |
| OLD | NEW |