| 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 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 dbus::Bus* bus_; | 174 dbus::Bus* bus_; |
| 175 HelperMap helpers_; | 175 HelperMap helpers_; |
| 176 STLValueDeleter<HelperMap> helpers_deleter_; | 176 STLValueDeleter<HelperMap> helpers_deleter_; |
| 177 | 177 |
| 178 DISALLOW_COPY_AND_ASSIGN(ShillServiceClientImpl); | 178 DISALLOW_COPY_AND_ASSIGN(ShillServiceClientImpl); |
| 179 }; | 179 }; |
| 180 | 180 |
| 181 // A stub implementation of ShillServiceClient. | 181 // A stub implementation of ShillServiceClient. |
| 182 class ShillServiceClientStubImpl : public ShillServiceClient { | 182 class ShillServiceClientStubImpl : public ShillServiceClient { |
| 183 public: | 183 public: |
| 184 ShillServiceClientStubImpl() : weak_ptr_factory_(this) {} | 184 ShillServiceClientStubImpl() : weak_ptr_factory_(this) { |
| 185 // Add stub services. Note: names match Manager stub impl. |
| 186 PopulateNetworkDictionary("stub_ethernet", "eth0", |
| 187 flimflam::kTypeEthernet, |
| 188 flimflam::kStateOnline, |
| 189 flimflam::kSecurityNone); |
| 190 PopulateNetworkDictionary("stub_wifi1", "wifi1", |
| 191 flimflam::kTypeWifi, |
| 192 flimflam::kStateOnline, |
| 193 flimflam::kSecurityNone); |
| 194 PopulateNetworkDictionary("stub_wifi2", "wifi2_PSK", |
| 195 flimflam::kTypeWifi, |
| 196 flimflam::kStateIdle, |
| 197 flimflam::kSecurityPsk); |
| 198 base::DictionaryValue* cellular1 = |
| 199 PopulateNetworkDictionary("stub_cellular1", "cellular1", |
| 200 flimflam::kTypeCellular, |
| 201 flimflam::kStateIdle, |
| 202 flimflam::kSecurityNone); |
| 203 cellular1->SetWithoutPathExpansion( |
| 204 flimflam::kNetworkTechnologyProperty, |
| 205 base::Value::CreateStringValue(flimflam::kNetworkTechnologyGsm)); |
| 206 } |
| 185 | 207 |
| 186 virtual ~ShillServiceClientStubImpl() {} | 208 virtual ~ShillServiceClientStubImpl() {} |
| 187 | 209 |
| 188 /////////////////////////////////// | 210 /////////////////////////////////// |
| 189 // ShillServiceClient overrides. | 211 // ShillServiceClient overrides. |
| 190 virtual void AddPropertyChangedObserver( | 212 virtual void AddPropertyChangedObserver( |
| 191 const dbus::ObjectPath& service_path, | 213 const dbus::ObjectPath& service_path, |
| 192 ShillPropertyChangedObserver* observer) OVERRIDE {} | 214 ShillPropertyChangedObserver* observer) OVERRIDE { |
| 215 observer_list_.AddObserver(observer); |
| 216 } |
| 193 | 217 |
| 194 virtual void RemovePropertyChangedObserver( | 218 virtual void RemovePropertyChangedObserver( |
| 195 const dbus::ObjectPath& service_path, | 219 const dbus::ObjectPath& service_path, |
| 196 ShillPropertyChangedObserver* observer) OVERRIDE {} | 220 ShillPropertyChangedObserver* observer) OVERRIDE { |
| 221 observer_list_.RemoveObserver(observer); |
| 222 } |
| 197 | 223 |
| 198 virtual void GetProperties(const dbus::ObjectPath& service_path, | 224 virtual void GetProperties(const dbus::ObjectPath& service_path, |
| 199 const DictionaryValueCallback& callback) OVERRIDE { | 225 const DictionaryValueCallback& callback) OVERRIDE { |
| 200 MessageLoop::current()->PostTask( | 226 MessageLoop::current()->PostTask( |
| 201 FROM_HERE, | 227 FROM_HERE, |
| 202 base::Bind(&ShillServiceClientStubImpl::PassEmptyDictionaryValue, | 228 base::Bind(&ShillServiceClientStubImpl::PassStubDictionaryValue, |
| 203 weak_ptr_factory_.GetWeakPtr(), | 229 weak_ptr_factory_.GetWeakPtr(), |
| 230 service_path, |
| 204 callback)); | 231 callback)); |
| 205 } | 232 } |
| 206 | 233 |
| 207 virtual void SetProperty(const dbus::ObjectPath& service_path, | 234 virtual void SetProperty(const dbus::ObjectPath& service_path, |
| 208 const std::string& name, | 235 const std::string& name, |
| 209 const base::Value& value, | 236 const base::Value& value, |
| 210 const base::Closure& callback, | 237 const base::Closure& callback, |
| 211 const ErrorCallback& error_callback) OVERRIDE { | 238 const ErrorCallback& error_callback) OVERRIDE { |
| 239 base::DictionaryValue* dict = NULL; |
| 240 std::string ssid = service_path.value(); |
| 241 if (!stub_services_.GetDictionaryWithoutPathExpansion(ssid, &dict)) { |
| 242 error_callback.Run("StubError", "Service not found"); |
| 243 return; |
| 244 } |
| 245 dict->SetWithoutPathExpansion(name, value.DeepCopy()); |
| 212 MessageLoop::current()->PostTask(FROM_HERE, callback); | 246 MessageLoop::current()->PostTask(FROM_HERE, callback); |
| 247 MessageLoop::current()->PostTask( |
| 248 FROM_HERE, |
| 249 base::Bind(&ShillServiceClientStubImpl::NotifyObserversPropertyChanged, |
| 250 weak_ptr_factory_.GetWeakPtr(), service_path, name)); |
| 213 } | 251 } |
| 214 | 252 |
| 215 virtual void ClearProperty(const dbus::ObjectPath& service_path, | 253 virtual void ClearProperty(const dbus::ObjectPath& service_path, |
| 216 const std::string& name, | 254 const std::string& name, |
| 217 const base::Closure& callback, | 255 const base::Closure& callback, |
| 218 const ErrorCallback& error_callback) OVERRIDE { | 256 const ErrorCallback& error_callback) OVERRIDE { |
| 257 base::DictionaryValue* dict = NULL; |
| 258 std::string ssid = service_path.value(); |
| 259 if (!stub_services_.GetDictionaryWithoutPathExpansion(ssid, &dict)) { |
| 260 error_callback.Run("StubError", "Service not found"); |
| 261 return; |
| 262 } |
| 263 dict->Remove(name, NULL); |
| 219 MessageLoop::current()->PostTask(FROM_HERE, callback); | 264 MessageLoop::current()->PostTask(FROM_HERE, callback); |
| 220 } | 265 } |
| 221 | 266 |
| 222 virtual void Connect(const dbus::ObjectPath& service_path, | 267 virtual void Connect(const dbus::ObjectPath& service_path, |
| 223 const base::Closure& callback, | 268 const base::Closure& callback, |
| 224 const ErrorCallback& error_callback) OVERRIDE { | 269 const ErrorCallback& error_callback) OVERRIDE { |
| 225 MessageLoop::current()->PostTask(FROM_HERE, callback); | 270 MessageLoop::current()->PostTask(FROM_HERE, callback); |
| 226 } | 271 } |
| 227 | 272 |
| 228 virtual void Disconnect(const dbus::ObjectPath& service_path, | 273 virtual void Disconnect(const dbus::ObjectPath& service_path, |
| (...skipping 16 matching lines...) Expand all Loading... |
| 245 MessageLoop::current()->PostTask(FROM_HERE, callback); | 290 MessageLoop::current()->PostTask(FROM_HERE, callback); |
| 246 } | 291 } |
| 247 | 292 |
| 248 virtual bool CallActivateCellularModemAndBlock( | 293 virtual bool CallActivateCellularModemAndBlock( |
| 249 const dbus::ObjectPath& service_path, | 294 const dbus::ObjectPath& service_path, |
| 250 const std::string& carrier) OVERRIDE { | 295 const std::string& carrier) OVERRIDE { |
| 251 return true; | 296 return true; |
| 252 } | 297 } |
| 253 | 298 |
| 254 private: | 299 private: |
| 255 void PassEmptyDictionaryValue(const DictionaryValueCallback& callback) const { | 300 void PassStubDictionaryValue(const dbus::ObjectPath& service_path, |
| 256 base::DictionaryValue dictionary; | 301 const DictionaryValueCallback& callback) { |
| 257 callback.Run(DBUS_METHOD_CALL_SUCCESS, dictionary); | 302 base::DictionaryValue* dict = NULL; |
| 303 std::string ssid = service_path.value(); |
| 304 if (!stub_services_.GetDictionaryWithoutPathExpansion(ssid, &dict)) { |
| 305 callback.Run(DBUS_METHOD_CALL_FAILURE, base::DictionaryValue()); |
| 306 return; |
| 307 } |
| 308 callback.Run(DBUS_METHOD_CALL_SUCCESS, *dict); |
| 258 } | 309 } |
| 259 | 310 |
| 311 base::DictionaryValue* PopulateNetworkDictionary( |
| 312 const std::string& ssid, |
| 313 const std::string& name, |
| 314 const std::string& type, |
| 315 const std::string& state, |
| 316 const std::string& security) { |
| 317 base::DictionaryValue* dict = new base::DictionaryValue; |
| 318 dict->SetWithoutPathExpansion(flimflam::kSSIDProperty, |
| 319 base::Value::CreateStringValue(ssid)); |
| 320 dict->SetWithoutPathExpansion(flimflam::kNameProperty, |
| 321 base::Value::CreateStringValue(name)); |
| 322 dict->SetWithoutPathExpansion(flimflam::kTypeProperty, |
| 323 base::Value::CreateStringValue(type)); |
| 324 dict->SetWithoutPathExpansion(flimflam::kStateProperty, |
| 325 base::Value::CreateStringValue(state)); |
| 326 dict->SetWithoutPathExpansion(flimflam::kSecurityProperty, |
| 327 base::Value::CreateStringValue(security)); |
| 328 stub_services_.Set(ssid, dict); |
| 329 return dict; |
| 330 } |
| 331 |
| 332 void NotifyObserversPropertyChanged(const dbus::ObjectPath& service_path, |
| 333 const std::string& property) { |
| 334 base::DictionaryValue* dict = NULL; |
| 335 std::string ssid = service_path.value(); |
| 336 if (!stub_services_.GetDictionaryWithoutPathExpansion(ssid, &dict)) { |
| 337 LOG(ERROR) << "Notify for unknown service: " << ssid; |
| 338 return; |
| 339 } |
| 340 base::Value* value; |
| 341 if (!dict->GetWithoutPathExpansion(property, &value)) { |
| 342 LOG(ERROR) << "Notify for unknown property: " |
| 343 << ssid << " : " << property; |
| 344 return; |
| 345 } |
| 346 FOR_EACH_OBSERVER(ShillPropertyChangedObserver, |
| 347 observer_list_, |
| 348 OnPropertyChanged(property, *value)); |
| 349 } |
| 350 |
| 351 |
| 352 base::DictionaryValue stub_services_; |
| 353 ObserverList<ShillPropertyChangedObserver> observer_list_; |
| 354 |
| 260 // Note: This should remain the last member so it'll be destroyed and | 355 // Note: This should remain the last member so it'll be destroyed and |
| 261 // invalidate its weak pointers before any other members are destroyed. | 356 // invalidate its weak pointers before any other members are destroyed. |
| 262 base::WeakPtrFactory<ShillServiceClientStubImpl> weak_ptr_factory_; | 357 base::WeakPtrFactory<ShillServiceClientStubImpl> weak_ptr_factory_; |
| 263 | 358 |
| 264 DISALLOW_COPY_AND_ASSIGN(ShillServiceClientStubImpl); | 359 DISALLOW_COPY_AND_ASSIGN(ShillServiceClientStubImpl); |
| 265 }; | 360 }; |
| 266 | 361 |
| 267 } // namespace | 362 } // namespace |
| 268 | 363 |
| 269 ShillServiceClient::ShillServiceClient() {} | 364 ShillServiceClient::ShillServiceClient() {} |
| 270 | 365 |
| 271 ShillServiceClient::~ShillServiceClient() {} | 366 ShillServiceClient::~ShillServiceClient() {} |
| 272 | 367 |
| 273 // static | 368 // static |
| 274 ShillServiceClient* ShillServiceClient::Create( | 369 ShillServiceClient* ShillServiceClient::Create( |
| 275 DBusClientImplementationType type, | 370 DBusClientImplementationType type, |
| 276 dbus::Bus* bus) { | 371 dbus::Bus* bus) { |
| 277 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) | 372 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) |
| 278 return new ShillServiceClientImpl(bus); | 373 return new ShillServiceClientImpl(bus); |
| 279 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); | 374 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); |
| 280 return new ShillServiceClientStubImpl(); | 375 return new ShillServiceClientStubImpl(); |
| 281 } | 376 } |
| 282 | 377 |
| 283 } // namespace chromeos | 378 } // namespace chromeos |
| OLD | NEW |