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 // Used to compare values for finding entries to erase in a ListValue. |
| 176 // (ListValue only implements a const_iterator version of Find). |
| 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 |
175 // A stub implementation of ShillManagerClient. | 185 // A stub implementation of ShillManagerClient. |
176 // Implemented: Stub devices and services for NetworkStateManager tests. | 186 // Implemented: Stub devices and services for NetworkStateManager tests. |
177 // Implemented: Stub cellular device entry for SMS tests. | 187 // Implemented: Stub cellular device entry for SMS tests. |
178 class ShillManagerClientStubImpl : public ShillManagerClient, | 188 class ShillManagerClientStubImpl : public ShillManagerClient, |
179 public ShillManagerClient::TestInterface { | 189 public ShillManagerClient::TestInterface { |
180 public: | 190 public: |
181 ShillManagerClientStubImpl() : weak_ptr_factory_(this) { | 191 ShillManagerClientStubImpl() |
| 192 : weak_ptr_factory_(this) { |
182 SetDefaultProperties(); | 193 SetDefaultProperties(); |
183 } | 194 } |
184 | 195 |
185 virtual ~ShillManagerClientStubImpl() {} | 196 virtual ~ShillManagerClientStubImpl() {} |
186 | 197 |
187 // ShillManagerClient overrides. | 198 // ShillManagerClient overrides. |
188 | 199 |
189 virtual void AddPropertyChangedObserver( | 200 virtual void AddPropertyChangedObserver( |
190 ShillPropertyChangedObserver* observer) OVERRIDE { | 201 ShillPropertyChangedObserver* observer) OVERRIDE { |
191 observer_list_.AddObserver(observer); | 202 observer_list_.AddObserver(observer); |
(...skipping 21 matching lines...) Expand all Loading... |
213 const base::Closure& callback, | 224 const base::Closure& callback, |
214 const ErrorCallback& error_callback) OVERRIDE { | 225 const ErrorCallback& error_callback) OVERRIDE { |
215 stub_properties_.Set(name, value.DeepCopy()); | 226 stub_properties_.Set(name, value.DeepCopy()); |
216 MessageLoop::current()->PostTask(FROM_HERE, callback); | 227 MessageLoop::current()->PostTask(FROM_HERE, callback); |
217 } | 228 } |
218 | 229 |
219 virtual void RequestScan(const std::string& type, | 230 virtual void RequestScan(const std::string& type, |
220 const base::Closure& callback, | 231 const base::Closure& callback, |
221 const ErrorCallback& error_callback) OVERRIDE { | 232 const ErrorCallback& error_callback) OVERRIDE { |
222 MessageLoop::current()->PostTask(FROM_HERE, callback); | 233 MessageLoop::current()->PostTask(FROM_HERE, callback); |
223 const int kScanDelaySeconds = 3; | 234 const int kScanDelayMilliseconds = 3000; |
224 MessageLoop::current()->PostDelayedTask( | 235 CallNotifyObserversPropertyChanged( |
225 FROM_HERE, | 236 flimflam::kServicesProperty, kScanDelayMilliseconds); |
226 base::Bind(&ShillManagerClientStubImpl::NotifyObserversPropertyChanged, | |
227 weak_ptr_factory_.GetWeakPtr(), | |
228 flimflam::kServicesProperty), | |
229 base::TimeDelta::FromSeconds(kScanDelaySeconds)); | |
230 } | 237 } |
231 | 238 |
232 virtual void EnableTechnology( | 239 virtual void EnableTechnology( |
233 const std::string& type, | 240 const std::string& type, |
234 const base::Closure& callback, | 241 const base::Closure& callback, |
235 const ErrorCallback& error_callback) OVERRIDE { | 242 const ErrorCallback& error_callback) OVERRIDE { |
236 base::ListValue* enabled_list = NULL; | 243 base::ListValue* enabled_list = NULL; |
237 if (!stub_properties_.GetListWithoutPathExpansion( | 244 if (!stub_properties_.GetListWithoutPathExpansion( |
238 flimflam::kEnabledTechnologiesProperty, &enabled_list)) { | 245 flimflam::kEnabledTechnologiesProperty, &enabled_list)) { |
239 MessageLoop::current()->PostTask( | 246 MessageLoop::current()->PostTask( |
240 FROM_HERE, | 247 FROM_HERE, |
241 base::Bind(error_callback, "StubError", "Property not found")); | 248 base::Bind(error_callback, "StubError", "Property not found")); |
242 return; | 249 return; |
243 } | 250 } |
244 MessageLoop::current()->PostTask(FROM_HERE, callback); | 251 MessageLoop::current()->PostTask(FROM_HERE, callback); |
245 enabled_list->AppendIfNotPresent(new base::StringValue(type)); | 252 enabled_list->AppendIfNotPresent(new base::StringValue(type)); |
246 MessageLoop::current()->PostTask( | 253 CallNotifyObserversPropertyChanged( |
247 FROM_HERE, | 254 flimflam::kEnabledTechnologiesProperty, 0); |
248 base::Bind(&ShillManagerClientStubImpl::NotifyObserversPropertyChanged, | |
249 weak_ptr_factory_.GetWeakPtr(), | |
250 flimflam::kEnabledTechnologiesProperty)); | |
251 } | 255 } |
252 | 256 |
253 virtual void DisableTechnology( | 257 virtual void DisableTechnology( |
254 const std::string& type, | 258 const std::string& type, |
255 const base::Closure& callback, | 259 const base::Closure& callback, |
256 const ErrorCallback& error_callback) OVERRIDE { | 260 const ErrorCallback& error_callback) OVERRIDE { |
257 base::ListValue* enabled_list = NULL; | 261 base::ListValue* enabled_list = NULL; |
258 if (!stub_properties_.GetListWithoutPathExpansion( | 262 if (!stub_properties_.GetListWithoutPathExpansion( |
259 flimflam::kEnabledTechnologiesProperty, &enabled_list)) { | 263 flimflam::kEnabledTechnologiesProperty, &enabled_list)) { |
260 MessageLoop::current()->PostTask( | 264 MessageLoop::current()->PostTask( |
261 FROM_HERE, | 265 FROM_HERE, |
262 base::Bind(error_callback, "StubError", "Property not found")); | 266 base::Bind(error_callback, "StubError", "Property not found")); |
263 return; | 267 return; |
264 } | 268 } |
265 MessageLoop::current()->PostTask(FROM_HERE, callback); | 269 MessageLoop::current()->PostTask(FROM_HERE, callback); |
266 base::StringValue type_value(type); | 270 base::StringValue type_value(type); |
267 enabled_list->Remove(type_value, NULL); | 271 enabled_list->Remove(type_value, NULL); |
268 MessageLoop::current()->PostTask( | 272 CallNotifyObserversPropertyChanged( |
269 FROM_HERE, | 273 flimflam::kEnabledTechnologiesProperty, 0); |
270 base::Bind(&ShillManagerClientStubImpl::NotifyObserversPropertyChanged, | |
271 weak_ptr_factory_.GetWeakPtr(), | |
272 flimflam::kEnabledTechnologiesProperty)); | |
273 } | 274 } |
274 | 275 |
275 virtual void ConfigureService( | 276 virtual void ConfigureService( |
276 const base::DictionaryValue& properties, | 277 const base::DictionaryValue& properties, |
277 const base::Closure& callback, | 278 const base::Closure& callback, |
278 const ErrorCallback& error_callback) OVERRIDE { | 279 const ErrorCallback& error_callback) OVERRIDE { |
279 MessageLoop::current()->PostTask(FROM_HERE, callback); | 280 MessageLoop::current()->PostTask(FROM_HERE, callback); |
280 } | 281 } |
281 | 282 |
282 virtual void GetService( | 283 virtual void GetService( |
283 const base::DictionaryValue& properties, | 284 const base::DictionaryValue& properties, |
284 const ObjectPathCallback& callback, | 285 const ObjectPathCallback& callback, |
285 const ErrorCallback& error_callback) OVERRIDE { | 286 const ErrorCallback& error_callback) OVERRIDE { |
286 MessageLoop::current()->PostTask( | 287 MessageLoop::current()->PostTask( |
287 FROM_HERE, base::Bind(callback, dbus::ObjectPath())); | 288 FROM_HERE, base::Bind(callback, dbus::ObjectPath())); |
288 } | 289 } |
289 | 290 |
290 virtual ShillManagerClient::TestInterface* GetTestInterface() OVERRIDE { | 291 virtual ShillManagerClient::TestInterface* GetTestInterface() OVERRIDE { |
291 return this; | 292 return this; |
292 } | 293 } |
293 | 294 |
294 // ShillManagerClient::TestInterface overrides. | 295 // ShillManagerClient::TestInterface overrides. |
295 | 296 |
296 virtual void AddDevice(const std::string& device_path) OVERRIDE { | 297 virtual void AddDevice(const std::string& device_path) OVERRIDE { |
297 GetListProperty(flimflam::kDevicesProperty)->Append( | 298 if (GetListProperty(flimflam::kDevicesProperty)->AppendIfNotPresent( |
298 base::Value::CreateStringValue(device_path)); | 299 base::Value::CreateStringValue(device_path))) { |
| 300 CallNotifyObserversPropertyChanged(flimflam::kDevicesProperty, 0); |
| 301 } |
299 } | 302 } |
300 | 303 |
301 virtual void RemoveDevice(const std::string& device_path) OVERRIDE { | 304 virtual void RemoveDevice(const std::string& device_path) OVERRIDE { |
302 base::StringValue device_path_value(device_path); | 305 base::StringValue device_path_value(device_path); |
303 GetListProperty(flimflam::kDevicesProperty)->Remove( | 306 if (GetListProperty(flimflam::kDevicesProperty)->Remove( |
304 device_path_value, NULL); | 307 device_path_value, NULL)) { |
| 308 CallNotifyObserversPropertyChanged(flimflam::kDevicesProperty, 0); |
| 309 } |
305 } | 310 } |
306 | 311 |
307 virtual void AddService(const std::string& service_path, | 312 virtual void AddService(const std::string& service_path, |
308 bool add_to_watch_list) OVERRIDE { | 313 bool add_to_watch_list) OVERRIDE { |
309 GetListProperty(flimflam::kServicesProperty)->Append( | 314 if (GetListProperty(flimflam::kServicesProperty)->AppendIfNotPresent( |
310 base::Value::CreateStringValue(service_path)); | 315 base::Value::CreateStringValue(service_path))) { |
311 if (add_to_watch_list) { | 316 CallNotifyObserversPropertyChanged(flimflam::kServicesProperty, 0); |
312 GetListProperty(flimflam::kServiceWatchListProperty)->Append( | |
313 base::Value::CreateStringValue(service_path)); | |
314 } | 317 } |
| 318 if (add_to_watch_list) |
| 319 AddServiceToWatchList(service_path); |
| 320 } |
| 321 |
| 322 virtual void AddServiceAtIndex(const std::string& service_path, |
| 323 size_t index, |
| 324 bool add_to_watch_list) OVERRIDE { |
| 325 base::StringValue path_value(service_path); |
| 326 base::ListValue* service_list = |
| 327 GetListProperty(flimflam::kServicesProperty); |
| 328 base::ListValue::iterator iter = |
| 329 std::find_if(service_list->begin(), service_list->end(), |
| 330 ValueEquals(&path_value)); |
| 331 service_list->Find(path_value); |
| 332 if (iter != service_list->end()) |
| 333 service_list->Erase(iter, NULL); |
| 334 service_list->Insert(index, path_value.DeepCopy()); |
| 335 CallNotifyObserversPropertyChanged(flimflam::kServicesProperty, 0); |
| 336 if (add_to_watch_list) |
| 337 AddServiceToWatchList(service_path); |
315 } | 338 } |
316 | 339 |
317 virtual void RemoveService(const std::string& service_path) OVERRIDE { | 340 virtual void RemoveService(const std::string& service_path) OVERRIDE { |
318 base::StringValue service_path_value(service_path); | 341 base::StringValue service_path_value(service_path); |
319 GetListProperty(flimflam::kServicesProperty)->Remove( | 342 if (GetListProperty(flimflam::kServicesProperty)->Remove( |
320 service_path_value, NULL); | 343 service_path_value, NULL)) { |
321 GetListProperty(flimflam::kServiceWatchListProperty)->Remove( | 344 CallNotifyObserversPropertyChanged(flimflam::kServicesProperty, 0); |
322 service_path_value, NULL); | 345 } |
| 346 if (GetListProperty(flimflam::kServiceWatchListProperty)->Remove( |
| 347 service_path_value, NULL)) { |
| 348 CallNotifyObserversPropertyChanged( |
| 349 flimflam::kServiceWatchListProperty, 0); |
| 350 } |
323 } | 351 } |
324 | 352 |
325 virtual void AddTechnology(const std::string& type, bool enabled) OVERRIDE { | 353 virtual void AddTechnology(const std::string& type, bool enabled) OVERRIDE { |
326 GetListProperty(flimflam::kAvailableTechnologiesProperty)->Append( | 354 if (GetListProperty(flimflam::kAvailableTechnologiesProperty)-> |
327 base::Value::CreateStringValue(type)); | 355 AppendIfNotPresent(base::Value::CreateStringValue(type))) { |
328 if (enabled) { | 356 CallNotifyObserversPropertyChanged( |
329 GetListProperty(flimflam::kEnabledTechnologiesProperty)->Append( | 357 flimflam::kAvailableTechnologiesProperty, 0); |
330 base::Value::CreateStringValue(type)); | 358 } |
| 359 if (enabled && |
| 360 GetListProperty(flimflam::kEnabledTechnologiesProperty)-> |
| 361 AppendIfNotPresent(base::Value::CreateStringValue(type))) { |
| 362 CallNotifyObserversPropertyChanged( |
| 363 flimflam::kEnabledTechnologiesProperty, 0); |
331 } | 364 } |
332 } | 365 } |
333 | 366 |
334 virtual void RemoveTechnology(const std::string& type) OVERRIDE { | 367 virtual void RemoveTechnology(const std::string& type) OVERRIDE { |
335 base::StringValue type_value(type); | 368 base::StringValue type_value(type); |
336 GetListProperty(flimflam::kAvailableTechnologiesProperty)->Remove( | 369 if (GetListProperty(flimflam::kAvailableTechnologiesProperty)->Remove( |
337 type_value, NULL); | 370 type_value, NULL)) { |
338 GetListProperty(flimflam::kEnabledTechnologiesProperty)->Remove( | 371 CallNotifyObserversPropertyChanged( |
339 type_value, NULL); | 372 flimflam::kAvailableTechnologiesProperty, 0); |
| 373 } |
| 374 if (GetListProperty(flimflam::kEnabledTechnologiesProperty)->Remove( |
| 375 type_value, NULL)) { |
| 376 CallNotifyObserversPropertyChanged( |
| 377 flimflam::kEnabledTechnologiesProperty, 0); |
| 378 } |
340 } | 379 } |
341 | 380 |
342 virtual void ClearProperties() OVERRIDE { | 381 virtual void ClearProperties() OVERRIDE { |
343 stub_properties_.Clear(); | 382 stub_properties_.Clear(); |
344 } | 383 } |
345 | 384 |
346 private: | 385 private: |
| 386 void AddServiceToWatchList(const std::string& service_path) { |
| 387 if (GetListProperty( |
| 388 flimflam::kServiceWatchListProperty)->AppendIfNotPresent( |
| 389 base::Value::CreateStringValue(service_path))) { |
| 390 CallNotifyObserversPropertyChanged( |
| 391 flimflam::kServiceWatchListProperty, 0); |
| 392 } |
| 393 } |
| 394 |
347 void SetDefaultProperties() { | 395 void SetDefaultProperties() { |
348 // Stub Devices, Note: names match Device stub map. | 396 // Stub Devices, Note: names match Device stub map. |
349 AddDevice("stub_wifi_device1"); | 397 AddDevice("stub_wifi_device1"); |
350 AddDevice("stub_cellular_device1"); | 398 AddDevice("stub_cellular_device1"); |
351 | 399 |
352 // Stub Services, Note: names match Service stub map. | 400 // Stub Services, Note: names match Service stub map. |
353 AddService("stub_ethernet", true); | 401 AddService("stub_ethernet", true); |
354 AddService("stub_wifi1", true); | 402 AddService("stub_wifi1", true); |
355 AddService("stub_wifi2", true); | 403 AddService("stub_wifi2", true); |
356 AddService("stub_cellular1", true); | 404 AddService("stub_cellular1", true); |
357 | 405 |
358 // Stub Technologies | 406 // Stub Technologies |
359 AddTechnology(flimflam::kTypeEthernet, true); | 407 AddTechnology(flimflam::kTypeEthernet, true); |
360 AddTechnology(flimflam::kTypeWifi, true); | 408 AddTechnology(flimflam::kTypeWifi, true); |
361 AddTechnology(flimflam::kTypeCellular, true); | 409 AddTechnology(flimflam::kTypeCellular, true); |
362 } | 410 } |
363 | 411 |
364 void PassStubProperties(const DictionaryValueCallback& callback) const { | 412 void PassStubProperties(const DictionaryValueCallback& callback) const { |
365 callback.Run(DBUS_METHOD_CALL_SUCCESS, stub_properties_); | 413 callback.Run(DBUS_METHOD_CALL_SUCCESS, stub_properties_); |
366 } | 414 } |
367 | 415 |
| 416 void CallNotifyObserversPropertyChanged(const std::string& property, |
| 417 int delay_ms) { |
| 418 // Avoid unnecessary delayed task if we have no observers (e.g. during |
| 419 // initial setup). |
| 420 if (observer_list_.size() == 0) |
| 421 return; |
| 422 MessageLoop::current()->PostDelayedTask( |
| 423 FROM_HERE, |
| 424 base::Bind(&ShillManagerClientStubImpl::NotifyObserversPropertyChanged, |
| 425 weak_ptr_factory_.GetWeakPtr(), |
| 426 property), |
| 427 base::TimeDelta::FromMilliseconds(delay_ms)); |
| 428 } |
| 429 |
368 void NotifyObserversPropertyChanged(const std::string& property) { | 430 void NotifyObserversPropertyChanged(const std::string& property) { |
369 base::Value* value = NULL; | 431 base::Value* value = NULL; |
370 if (!stub_properties_.GetWithoutPathExpansion(property, &value)) { | 432 if (!stub_properties_.GetWithoutPathExpansion(property, &value)) { |
371 LOG(ERROR) << "Notify for unknown property: " << property; | 433 LOG(ERROR) << "Notify for unknown property: " << property; |
372 return; | 434 return; |
373 } | 435 } |
374 FOR_EACH_OBSERVER(ShillPropertyChangedObserver, | 436 FOR_EACH_OBSERVER(ShillPropertyChangedObserver, |
375 observer_list_, | 437 observer_list_, |
376 OnPropertyChanged(property, *value)); | 438 OnPropertyChanged(property, *value)); |
377 } | 439 } |
(...skipping 28 matching lines...) Expand all Loading... |
406 ShillManagerClient* ShillManagerClient::Create( | 468 ShillManagerClient* ShillManagerClient::Create( |
407 DBusClientImplementationType type, | 469 DBusClientImplementationType type, |
408 dbus::Bus* bus) { | 470 dbus::Bus* bus) { |
409 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) | 471 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) |
410 return new ShillManagerClientImpl(bus); | 472 return new ShillManagerClientImpl(bus); |
411 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); | 473 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); |
412 return new ShillManagerClientStubImpl(); | 474 return new ShillManagerClientStubImpl(); |
413 } | 475 } |
414 | 476 |
415 } // namespace chromeos | 477 } // namespace chromeos |
OLD | NEW |