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 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
165 return NULL; | 165 return NULL; |
166 } | 166 } |
167 | 167 |
168 private: | 168 private: |
169 dbus::ObjectProxy* proxy_; | 169 dbus::ObjectProxy* proxy_; |
170 ShillClientHelper helper_; | 170 ShillClientHelper helper_; |
171 | 171 |
172 DISALLOW_COPY_AND_ASSIGN(ShillManagerClientImpl); | 172 DISALLOW_COPY_AND_ASSIGN(ShillManagerClientImpl); |
173 }; | 173 }; |
174 | 174 |
175 namespace { | |
hashimoto
2012/11/05 04:03:03
We are already in the unnamed namespace, no need t
stevenjb
2012/11/05 22:42:35
Oops. Done.
| |
176 | |
177 struct ValueEquals { | |
hashimoto
2012/11/05 04:03:03
Could you add some comment about how this struct i
stevenjb
2012/11/05 22:42:35
Done.
| |
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 | |
175 // A stub implementation of ShillManagerClient. | 187 // A stub implementation of ShillManagerClient. |
176 // Implemented: Stub devices and services for NetworkStateManager tests. | 188 // Implemented: Stub devices and services for NetworkStateManager tests. |
177 // Implemented: Stub cellular device entry for SMS tests. | 189 // Implemented: Stub cellular device entry for SMS tests. |
178 class ShillManagerClientStubImpl : public ShillManagerClient, | 190 class ShillManagerClientStubImpl : public ShillManagerClient, |
179 public ShillManagerClient::TestInterface { | 191 public ShillManagerClient::TestInterface { |
180 public: | 192 public: |
181 ShillManagerClientStubImpl() : weak_ptr_factory_(this) { | 193 ShillManagerClientStubImpl() |
194 : initialized_(false), | |
195 weak_ptr_factory_(this) { | |
182 SetDefaultProperties(); | 196 SetDefaultProperties(); |
197 initialized_ = true; | |
183 } | 198 } |
184 | 199 |
185 virtual ~ShillManagerClientStubImpl() {} | 200 virtual ~ShillManagerClientStubImpl() {} |
186 | 201 |
187 // ShillManagerClient overrides. | 202 // ShillManagerClient overrides. |
188 | 203 |
189 virtual void AddPropertyChangedObserver( | 204 virtual void AddPropertyChangedObserver( |
190 ShillPropertyChangedObserver* observer) OVERRIDE { | 205 ShillPropertyChangedObserver* observer) OVERRIDE { |
191 observer_list_.AddObserver(observer); | 206 observer_list_.AddObserver(observer); |
192 } | 207 } |
(...skipping 21 matching lines...) Expand all Loading... | |
214 const ErrorCallback& error_callback) OVERRIDE { | 229 const ErrorCallback& error_callback) OVERRIDE { |
215 stub_properties_.Set(name, value.DeepCopy()); | 230 stub_properties_.Set(name, value.DeepCopy()); |
216 MessageLoop::current()->PostTask(FROM_HERE, callback); | 231 MessageLoop::current()->PostTask(FROM_HERE, callback); |
217 } | 232 } |
218 | 233 |
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); |
223 const int kScanDelaySeconds = 3; | 238 const int kScanDelaySeconds = 3; |
224 MessageLoop::current()->PostDelayedTask( | 239 CallNotifyObserversPropertyChanged( |
225 FROM_HERE, | 240 flimflam::kServicesProperty, kScanDelaySeconds); |
226 base::Bind(&ShillManagerClientStubImpl::NotifyObserversPropertyChanged, | |
227 weak_ptr_factory_.GetWeakPtr(), | |
228 flimflam::kServicesProperty), | |
229 base::TimeDelta::FromSeconds(kScanDelaySeconds)); | |
230 } | 241 } |
231 | 242 |
232 virtual void EnableTechnology( | 243 virtual void EnableTechnology( |
233 const std::string& type, | 244 const std::string& type, |
234 const base::Closure& callback, | 245 const base::Closure& callback, |
235 const ErrorCallback& error_callback) OVERRIDE { | 246 const ErrorCallback& error_callback) OVERRIDE { |
236 base::ListValue* enabled_list = NULL; | 247 base::ListValue* enabled_list = NULL; |
237 if (!stub_properties_.GetListWithoutPathExpansion( | 248 if (!stub_properties_.GetListWithoutPathExpansion( |
238 flimflam::kEnabledTechnologiesProperty, &enabled_list)) { | 249 flimflam::kEnabledTechnologiesProperty, &enabled_list)) { |
239 MessageLoop::current()->PostTask( | 250 MessageLoop::current()->PostTask( |
240 FROM_HERE, | 251 FROM_HERE, |
241 base::Bind(error_callback, "StubError", "Property not found")); | 252 base::Bind(error_callback, "StubError", "Property not found")); |
242 return; | 253 return; |
243 } | 254 } |
244 MessageLoop::current()->PostTask(FROM_HERE, callback); | 255 MessageLoop::current()->PostTask(FROM_HERE, callback); |
245 enabled_list->AppendIfNotPresent(new base::StringValue(type)); | 256 enabled_list->AppendIfNotPresent(new base::StringValue(type)); |
246 MessageLoop::current()->PostTask( | 257 CallNotifyObserversPropertyChanged( |
247 FROM_HERE, | 258 flimflam::kEnabledTechnologiesProperty, 0); |
248 base::Bind(&ShillManagerClientStubImpl::NotifyObserversPropertyChanged, | |
249 weak_ptr_factory_.GetWeakPtr(), | |
250 flimflam::kEnabledTechnologiesProperty)); | |
251 } | 259 } |
252 | 260 |
253 virtual void DisableTechnology( | 261 virtual void DisableTechnology( |
254 const std::string& type, | 262 const std::string& type, |
255 const base::Closure& callback, | 263 const base::Closure& callback, |
256 const ErrorCallback& error_callback) OVERRIDE { | 264 const ErrorCallback& error_callback) OVERRIDE { |
257 base::ListValue* enabled_list = NULL; | 265 base::ListValue* enabled_list = NULL; |
258 if (!stub_properties_.GetListWithoutPathExpansion( | 266 if (!stub_properties_.GetListWithoutPathExpansion( |
259 flimflam::kEnabledTechnologiesProperty, &enabled_list)) { | 267 flimflam::kEnabledTechnologiesProperty, &enabled_list)) { |
260 MessageLoop::current()->PostTask( | 268 MessageLoop::current()->PostTask( |
261 FROM_HERE, | 269 FROM_HERE, |
262 base::Bind(error_callback, "StubError", "Property not found")); | 270 base::Bind(error_callback, "StubError", "Property not found")); |
263 return; | 271 return; |
264 } | 272 } |
265 MessageLoop::current()->PostTask(FROM_HERE, callback); | 273 MessageLoop::current()->PostTask(FROM_HERE, callback); |
266 base::StringValue type_value(type); | 274 base::StringValue type_value(type); |
267 enabled_list->Remove(type_value, NULL); | 275 enabled_list->Remove(type_value, NULL); |
268 MessageLoop::current()->PostTask( | 276 CallNotifyObserversPropertyChanged( |
269 FROM_HERE, | 277 flimflam::kEnabledTechnologiesProperty, 0); |
270 base::Bind(&ShillManagerClientStubImpl::NotifyObserversPropertyChanged, | |
271 weak_ptr_factory_.GetWeakPtr(), | |
272 flimflam::kEnabledTechnologiesProperty)); | |
273 } | 278 } |
274 | 279 |
275 virtual void ConfigureService( | 280 virtual void ConfigureService( |
276 const base::DictionaryValue& properties, | 281 const base::DictionaryValue& properties, |
277 const base::Closure& callback, | 282 const base::Closure& callback, |
278 const ErrorCallback& error_callback) OVERRIDE { | 283 const ErrorCallback& error_callback) OVERRIDE { |
279 MessageLoop::current()->PostTask(FROM_HERE, callback); | 284 MessageLoop::current()->PostTask(FROM_HERE, callback); |
280 } | 285 } |
281 | 286 |
282 virtual void GetService( | 287 virtual void GetService( |
283 const base::DictionaryValue& properties, | 288 const base::DictionaryValue& properties, |
284 const ObjectPathCallback& callback, | 289 const ObjectPathCallback& callback, |
285 const ErrorCallback& error_callback) OVERRIDE { | 290 const ErrorCallback& error_callback) OVERRIDE { |
286 MessageLoop::current()->PostTask( | 291 MessageLoop::current()->PostTask( |
287 FROM_HERE, base::Bind(callback, dbus::ObjectPath())); | 292 FROM_HERE, base::Bind(callback, dbus::ObjectPath())); |
288 } | 293 } |
289 | 294 |
290 virtual ShillManagerClient::TestInterface* GetTestInterface() OVERRIDE { | 295 virtual ShillManagerClient::TestInterface* GetTestInterface() OVERRIDE { |
291 return this; | 296 return this; |
292 } | 297 } |
293 | 298 |
294 // ShillManagerClient::TestInterface overrides. | 299 // ShillManagerClient::TestInterface overrides. |
295 | 300 |
296 virtual void AddDevice(const std::string& device_path) OVERRIDE { | 301 virtual void AddDevice(const std::string& device_path) OVERRIDE { |
297 GetListProperty(flimflam::kDevicesProperty)->Append( | 302 if (GetListProperty(flimflam::kDevicesProperty)->AppendIfNotPresent( |
298 base::Value::CreateStringValue(device_path)); | 303 base::Value::CreateStringValue(device_path))) { |
304 CallNotifyObserversPropertyChanged(flimflam::kDevicesProperty, 0); | |
305 } | |
299 } | 306 } |
300 | 307 |
301 virtual void RemoveDevice(const std::string& device_path) OVERRIDE { | 308 virtual void RemoveDevice(const std::string& device_path) OVERRIDE { |
302 base::StringValue device_path_value(device_path); | 309 base::StringValue device_path_value(device_path); |
303 GetListProperty(flimflam::kDevicesProperty)->Remove( | 310 if (GetListProperty(flimflam::kDevicesProperty)->Remove( |
304 device_path_value, NULL); | 311 device_path_value, NULL)) { |
312 CallNotifyObserversPropertyChanged(flimflam::kDevicesProperty, 0); | |
313 } | |
305 } | 314 } |
306 | 315 |
307 virtual void AddService(const std::string& service_path, | 316 virtual void AddService(const std::string& service_path, |
308 bool add_to_watch_list) OVERRIDE { | 317 bool add_to_watch_list) OVERRIDE { |
309 GetListProperty(flimflam::kServicesProperty)->Append( | 318 if (GetListProperty(flimflam::kServicesProperty)->AppendIfNotPresent( |
310 base::Value::CreateStringValue(service_path)); | 319 base::Value::CreateStringValue(service_path))) { |
311 if (add_to_watch_list) { | 320 CallNotifyObserversPropertyChanged(flimflam::kServicesProperty, 0); |
312 GetListProperty(flimflam::kServiceWatchListProperty)->Append( | |
313 base::Value::CreateStringValue(service_path)); | |
314 } | 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); | |
315 } | 344 } |
316 | 345 |
317 virtual void RemoveService(const std::string& service_path) OVERRIDE { | 346 virtual void RemoveService(const std::string& service_path) OVERRIDE { |
318 base::StringValue service_path_value(service_path); | 347 base::StringValue service_path_value(service_path); |
319 GetListProperty(flimflam::kServicesProperty)->Remove( | 348 if (GetListProperty(flimflam::kServicesProperty)->Remove( |
320 service_path_value, NULL); | 349 service_path_value, NULL)) { |
321 GetListProperty(flimflam::kServiceWatchListProperty)->Remove( | 350 CallNotifyObserversPropertyChanged(flimflam::kServicesProperty, 0); |
322 service_path_value, NULL); | 351 } |
352 if (GetListProperty(flimflam::kServiceWatchListProperty)->Remove( | |
353 service_path_value, NULL)) { | |
354 CallNotifyObserversPropertyChanged( | |
355 flimflam::kServiceWatchListProperty, 0); | |
356 } | |
323 } | 357 } |
324 | 358 |
325 virtual void AddTechnology(const std::string& type, bool enabled) OVERRIDE { | 359 virtual void AddTechnology(const std::string& type, bool enabled) OVERRIDE { |
326 GetListProperty(flimflam::kAvailableTechnologiesProperty)->Append( | 360 if (GetListProperty(flimflam::kAvailableTechnologiesProperty)-> |
327 base::Value::CreateStringValue(type)); | 361 AppendIfNotPresent(base::Value::CreateStringValue(type))) { |
328 if (enabled) { | 362 CallNotifyObserversPropertyChanged( |
329 GetListProperty(flimflam::kEnabledTechnologiesProperty)->Append( | 363 flimflam::kAvailableTechnologiesProperty, 0); |
330 base::Value::CreateStringValue(type)); | 364 } |
365 if (enabled && | |
366 GetListProperty(flimflam::kEnabledTechnologiesProperty)-> | |
367 AppendIfNotPresent(base::Value::CreateStringValue(type))) { | |
368 CallNotifyObserversPropertyChanged( | |
369 flimflam::kEnabledTechnologiesProperty, 0); | |
331 } | 370 } |
332 } | 371 } |
333 | 372 |
334 virtual void RemoveTechnology(const std::string& type) OVERRIDE { | 373 virtual void RemoveTechnology(const std::string& type) OVERRIDE { |
335 base::StringValue type_value(type); | 374 base::StringValue type_value(type); |
336 GetListProperty(flimflam::kAvailableTechnologiesProperty)->Remove( | 375 if (GetListProperty(flimflam::kAvailableTechnologiesProperty)->Remove( |
337 type_value, NULL); | 376 type_value, NULL)) { |
338 GetListProperty(flimflam::kEnabledTechnologiesProperty)->Remove( | 377 CallNotifyObserversPropertyChanged( |
339 type_value, NULL); | 378 flimflam::kAvailableTechnologiesProperty, 0); |
379 } | |
380 if (GetListProperty(flimflam::kEnabledTechnologiesProperty)->Remove( | |
381 type_value, NULL)) { | |
382 CallNotifyObserversPropertyChanged( | |
383 flimflam::kEnabledTechnologiesProperty, 0); | |
384 } | |
340 } | 385 } |
341 | 386 |
342 virtual void ClearProperties() OVERRIDE { | 387 virtual void ClearProperties() OVERRIDE { |
343 stub_properties_.Clear(); | 388 stub_properties_.Clear(); |
hashimoto
2012/11/05 04:03:03
No need to notify observers here? If so, please ad
stevenjb
2012/11/05 22:42:35
Comment added to header.
| |
344 } | 389 } |
345 | 390 |
346 private: | 391 private: |
347 void SetDefaultProperties() { | 392 void SetDefaultProperties() { |
348 // Stub Devices, Note: names match Device stub map. | 393 // Stub Devices, Note: names match Device stub map. |
349 AddDevice("stub_wifi_device1"); | 394 AddDevice("stub_wifi_device1"); |
350 AddDevice("stub_cellular_device1"); | 395 AddDevice("stub_cellular_device1"); |
351 | 396 |
352 // Stub Services, Note: names match Service stub map. | 397 // Stub Services, Note: names match Service stub map. |
353 AddService("stub_ethernet", true); | 398 AddService("stub_ethernet", true); |
354 AddService("stub_wifi1", true); | 399 AddService("stub_wifi1", true); |
355 AddService("stub_wifi2", true); | 400 AddService("stub_wifi2", true); |
356 AddService("stub_cellular1", true); | 401 AddService("stub_cellular1", true); |
357 | 402 |
358 // Stub Technologies | 403 // Stub Technologies |
359 AddTechnology(flimflam::kTypeEthernet, true); | 404 AddTechnology(flimflam::kTypeEthernet, true); |
360 AddTechnology(flimflam::kTypeWifi, true); | 405 AddTechnology(flimflam::kTypeWifi, true); |
361 AddTechnology(flimflam::kTypeCellular, true); | 406 AddTechnology(flimflam::kTypeCellular, true); |
362 } | 407 } |
363 | 408 |
364 void PassStubProperties(const DictionaryValueCallback& callback) const { | 409 void PassStubProperties(const DictionaryValueCallback& callback) const { |
365 callback.Run(DBUS_METHOD_CALL_SUCCESS, stub_properties_); | 410 callback.Run(DBUS_METHOD_CALL_SUCCESS, stub_properties_); |
366 } | 411 } |
367 | 412 |
413 void CallNotifyObserversPropertyChanged(const std::string& property, | |
414 int delay_seconds) { | |
Greg Spencer (Chromium)
2012/11/05 22:07:23
Is seconds enough granularity? Seems like millise
stevenjb
2012/11/05 22:42:35
Done.
| |
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 | |
368 void NotifyObserversPropertyChanged(const std::string& property) { | 425 void NotifyObserversPropertyChanged(const std::string& property) { |
369 base::Value* value = NULL; | 426 base::Value* value = NULL; |
370 if (!stub_properties_.GetWithoutPathExpansion(property, &value)) { | 427 if (!stub_properties_.GetWithoutPathExpansion(property, &value)) { |
371 LOG(ERROR) << "Notify for unknown property: " << property; | 428 LOG(ERROR) << "Notify for unknown property: " << property; |
372 return; | 429 return; |
373 } | 430 } |
374 FOR_EACH_OBSERVER(ShillPropertyChangedObserver, | 431 FOR_EACH_OBSERVER(ShillPropertyChangedObserver, |
375 observer_list_, | 432 observer_list_, |
376 OnPropertyChanged(property, *value)); | 433 OnPropertyChanged(property, *value)); |
377 } | 434 } |
378 | 435 |
379 base::ListValue* GetListProperty(const std::string& property) { | 436 base::ListValue* GetListProperty(const std::string& property) { |
380 base::ListValue* list_property = NULL; | 437 base::ListValue* list_property = NULL; |
381 if (!stub_properties_.GetListWithoutPathExpansion( | 438 if (!stub_properties_.GetListWithoutPathExpansion( |
382 property, &list_property)) { | 439 property, &list_property)) { |
383 list_property = new base::ListValue; | 440 list_property = new base::ListValue; |
384 stub_properties_.Set(property, list_property); | 441 stub_properties_.Set(property, list_property); |
385 } | 442 } |
386 return list_property; | 443 return list_property; |
387 } | 444 } |
388 | 445 |
446 bool initialized_; | |
hashimoto
2012/11/05 04:03:03
Please add comments to describe that we need this
stevenjb
2012/11/05 22:42:35
Good point. Removed this.
| |
389 base::DictionaryValue stub_properties_; | 447 base::DictionaryValue stub_properties_; |
390 ObserverList<ShillPropertyChangedObserver> observer_list_; | 448 ObserverList<ShillPropertyChangedObserver> observer_list_; |
391 | 449 |
392 // 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 |
393 // invalidate its weak pointers before any other members are destroyed. | 451 // invalidate its weak pointers before any other members are destroyed. |
394 base::WeakPtrFactory<ShillManagerClientStubImpl> weak_ptr_factory_; | 452 base::WeakPtrFactory<ShillManagerClientStubImpl> weak_ptr_factory_; |
395 | 453 |
396 DISALLOW_COPY_AND_ASSIGN(ShillManagerClientStubImpl); | 454 DISALLOW_COPY_AND_ASSIGN(ShillManagerClientStubImpl); |
397 }; | 455 }; |
398 | 456 |
399 } // namespace | 457 } // namespace |
400 | 458 |
401 ShillManagerClient::ShillManagerClient() {} | 459 ShillManagerClient::ShillManagerClient() {} |
402 | 460 |
403 ShillManagerClient::~ShillManagerClient() {} | 461 ShillManagerClient::~ShillManagerClient() {} |
404 | 462 |
405 // static | 463 // static |
406 ShillManagerClient* ShillManagerClient::Create( | 464 ShillManagerClient* ShillManagerClient::Create( |
407 DBusClientImplementationType type, | 465 DBusClientImplementationType type, |
408 dbus::Bus* bus) { | 466 dbus::Bus* bus) { |
409 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) | 467 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) |
410 return new ShillManagerClientImpl(bus); | 468 return new ShillManagerClientImpl(bus); |
411 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); | 469 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); |
412 return new ShillManagerClientStubImpl(); | 470 return new ShillManagerClientStubImpl(); |
413 } | 471 } |
414 | 472 |
415 } // namespace chromeos | 473 } // namespace chromeos |
OLD | NEW |