Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(232)

Side by Side Diff: chromeos/dbus/shill_manager_client_stub.cc

Issue 12381002: Improve network stub behavior (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: s/res/enabled Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chromeos/dbus/shill_manager_client_stub.h ('k') | chromeos/dbus/shill_service_client.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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_stub.h" 5 #include "chromeos/dbus/shill_manager_client_stub.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/command_line.h"
9 #include "base/message_loop.h" 10 #include "base/message_loop.h"
10 #include "base/values.h" 11 #include "base/values.h"
12 #include "chromeos/chromeos_switches.h"
13 #include "chromeos/dbus/dbus_thread_manager.h"
11 #include "chromeos/dbus/shill_property_changed_observer.h" 14 #include "chromeos/dbus/shill_property_changed_observer.h"
15 #include "chromeos/dbus/shill_service_client.h"
12 #include "dbus/bus.h" 16 #include "dbus/bus.h"
13 #include "dbus/message.h" 17 #include "dbus/message.h"
14 #include "dbus/object_path.h" 18 #include "dbus/object_path.h"
15 #include "dbus/object_proxy.h" 19 #include "dbus/object_proxy.h"
16 #include "dbus/values_util.h" 20 #include "dbus/values_util.h"
17 #include "third_party/cros_system_api/dbus/service_constants.h" 21 #include "third_party/cros_system_api/dbus/service_constants.h"
18 22
19 namespace chromeos { 23 namespace chromeos {
20 24
21 namespace { 25 namespace {
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
74 FROM_HERE, base::Bind( 78 FROM_HERE, base::Bind(
75 &ShillManagerClientStub::PassStubGeoNetworks, 79 &ShillManagerClientStub::PassStubGeoNetworks,
76 weak_ptr_factory_.GetWeakPtr(), 80 weak_ptr_factory_.GetWeakPtr(),
77 callback)); 81 callback));
78 } 82 }
79 83
80 void ShillManagerClientStub::SetProperty(const std::string& name, 84 void ShillManagerClientStub::SetProperty(const std::string& name,
81 const base::Value& value, 85 const base::Value& value,
82 const base::Closure& callback, 86 const base::Closure& callback,
83 const ErrorCallback& error_callback) { 87 const ErrorCallback& error_callback) {
84 stub_properties_.Set(name, value.DeepCopy()); 88 stub_properties_.SetWithoutPathExpansion(name, value.DeepCopy());
85 if (callback.is_null()) 89 if (callback.is_null())
86 return; 90 return;
87 MessageLoop::current()->PostTask(FROM_HERE, callback); 91 MessageLoop::current()->PostTask(FROM_HERE, callback);
88 } 92 }
89 93
90 void ShillManagerClientStub::RequestScan(const std::string& type, 94 void ShillManagerClientStub::RequestScan(const std::string& type,
91 const base::Closure& callback, 95 const base::Closure& callback,
92 const ErrorCallback& error_callback) { 96 const ErrorCallback& error_callback) {
93 const int kScanDelayMilliseconds = 3000; 97 const int kScanDelayMilliseconds = 3000;
94 CallNotifyObserversPropertyChanged( 98 CallNotifyObserversPropertyChanged(
(...skipping 14 matching lines...) Expand all
109 MessageLoop::current()->PostTask(FROM_HERE, callback); 113 MessageLoop::current()->PostTask(FROM_HERE, callback);
110 MessageLoop::current()->PostTask( 114 MessageLoop::current()->PostTask(
111 FROM_HERE, 115 FROM_HERE,
112 base::Bind(error_callback, "StubError", "Property not found")); 116 base::Bind(error_callback, "StubError", "Property not found"));
113 } 117 }
114 return; 118 return;
115 } 119 }
116 enabled_list->AppendIfNotPresent(new base::StringValue(type)); 120 enabled_list->AppendIfNotPresent(new base::StringValue(type));
117 CallNotifyObserversPropertyChanged( 121 CallNotifyObserversPropertyChanged(
118 flimflam::kEnabledTechnologiesProperty, 0); 122 flimflam::kEnabledTechnologiesProperty, 0);
119 if (callback.is_null()) 123 if (!callback.is_null())
120 return; 124 MessageLoop::current()->PostTask(FROM_HERE, callback);
121 MessageLoop::current()->PostTask(FROM_HERE, callback); 125 // May affect available services
126 CallNotifyObserversPropertyChanged(flimflam::kServicesProperty, 0);
127 CallNotifyObserversPropertyChanged(flimflam::kServiceWatchListProperty, 0);
122 } 128 }
123 129
124 void ShillManagerClientStub::DisableTechnology( 130 void ShillManagerClientStub::DisableTechnology(
125 const std::string& type, 131 const std::string& type,
126 const base::Closure& callback, 132 const base::Closure& callback,
127 const ErrorCallback& error_callback) { 133 const ErrorCallback& error_callback) {
128 base::ListValue* enabled_list = NULL; 134 base::ListValue* enabled_list = NULL;
129 if (!stub_properties_.GetListWithoutPathExpansion( 135 if (!stub_properties_.GetListWithoutPathExpansion(
130 flimflam::kEnabledTechnologiesProperty, &enabled_list)) { 136 flimflam::kEnabledTechnologiesProperty, &enabled_list)) {
131 if (!error_callback.is_null()) { 137 if (!error_callback.is_null()) {
132 MessageLoop::current()->PostTask( 138 MessageLoop::current()->PostTask(
133 FROM_HERE, 139 FROM_HERE,
134 base::Bind(error_callback, "StubError", "Property not found")); 140 base::Bind(error_callback, "StubError", "Property not found"));
135 } 141 }
136 return; 142 return;
137 } 143 }
138 base::StringValue type_value(type); 144 base::StringValue type_value(type);
139 enabled_list->Remove(type_value, NULL); 145 enabled_list->Remove(type_value, NULL);
140 CallNotifyObserversPropertyChanged( 146 CallNotifyObserversPropertyChanged(
141 flimflam::kEnabledTechnologiesProperty, 0); 147 flimflam::kEnabledTechnologiesProperty, 0);
142 if (callback.is_null()) 148 if (!callback.is_null())
143 return; 149 MessageLoop::current()->PostTask(FROM_HERE, callback);
144 MessageLoop::current()->PostTask(FROM_HERE, callback); 150 // May affect available services
145 } 151 CallNotifyObserversPropertyChanged(flimflam::kServicesProperty, 0);
152 CallNotifyObserversPropertyChanged(flimflam::kServiceWatchListProperty, 0);
153 }
146 154
147 void ShillManagerClientStub::ConfigureService( 155 void ShillManagerClientStub::ConfigureService(
148 const base::DictionaryValue& properties, 156 const base::DictionaryValue& properties,
149 const ObjectPathCallback& callback, 157 const ObjectPathCallback& callback,
150 const ErrorCallback& error_callback) { 158 const ErrorCallback& error_callback) {
151 if (callback.is_null()) 159 if (callback.is_null())
152 return; 160 return;
153 MessageLoop::current()->PostTask( 161 MessageLoop::current()->PostTask(
154 FROM_HERE, base::Bind(callback, dbus::ObjectPath())); 162 FROM_HERE, base::Bind(callback, dbus::ObjectPath()));
155 } 163 }
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
223 } 231 }
224 232
225 void ShillManagerClientStub::RemoveDevice(const std::string& device_path) { 233 void ShillManagerClientStub::RemoveDevice(const std::string& device_path) {
226 base::StringValue device_path_value(device_path); 234 base::StringValue device_path_value(device_path);
227 if (GetListProperty(flimflam::kDevicesProperty)->Remove( 235 if (GetListProperty(flimflam::kDevicesProperty)->Remove(
228 device_path_value, NULL)) { 236 device_path_value, NULL)) {
229 CallNotifyObserversPropertyChanged(flimflam::kDevicesProperty, 0); 237 CallNotifyObserversPropertyChanged(flimflam::kDevicesProperty, 0);
230 } 238 }
231 } 239 }
232 240
233 void ShillManagerClientStub::ResetDevices() { 241 void ShillManagerClientStub::ClearDevices() {
234 stub_properties_.Remove(flimflam::kDevicesProperty, NULL); 242 stub_properties_.Remove(flimflam::kDevicesProperty, NULL);
235 } 243 }
236 244
237 void ShillManagerClientStub::AddService(const std::string& service_path, 245 void ShillManagerClientStub::AddService(const std::string& service_path,
238 bool add_to_watch_list) { 246 bool add_to_watch_list) {
239 if (GetListProperty(flimflam::kServicesProperty)->AppendIfNotPresent( 247 if (GetListProperty(flimflam::kServicesProperty)->AppendIfNotPresent(
240 base::Value::CreateStringValue(service_path))) { 248 base::Value::CreateStringValue(service_path))) {
241 CallNotifyObserversPropertyChanged(flimflam::kServicesProperty, 0); 249 CallNotifyObserversPropertyChanged(flimflam::kServicesProperty, 0);
242 } 250 }
243 if (add_to_watch_list) 251 if (add_to_watch_list)
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
308 stub_properties_.Clear(); 316 stub_properties_.Clear();
309 } 317 }
310 318
311 void ShillManagerClientStub::AddGeoNetwork( 319 void ShillManagerClientStub::AddGeoNetwork(
312 const std::string& technology, 320 const std::string& technology,
313 const base::DictionaryValue& network) { 321 const base::DictionaryValue& network) {
314 base::ListValue* list_value = NULL; 322 base::ListValue* list_value = NULL;
315 if (!stub_geo_networks_.GetListWithoutPathExpansion( 323 if (!stub_geo_networks_.GetListWithoutPathExpansion(
316 technology, &list_value)) { 324 technology, &list_value)) {
317 list_value = new base::ListValue; 325 list_value = new base::ListValue;
318 stub_geo_networks_.Set(technology, list_value); 326 stub_geo_networks_.SetWithoutPathExpansion(technology, list_value);
319 } 327 }
320 list_value->Append(network.DeepCopy()); 328 list_value->Append(network.DeepCopy());
321 } 329 }
322 330
323 void ShillManagerClientStub::AddServiceToWatchList( 331 void ShillManagerClientStub::AddServiceToWatchList(
324 const std::string& service_path) { 332 const std::string& service_path) {
325 if (GetListProperty( 333 if (GetListProperty(
326 flimflam::kServiceWatchListProperty)->AppendIfNotPresent( 334 flimflam::kServiceWatchListProperty)->AppendIfNotPresent(
327 base::Value::CreateStringValue(service_path))) { 335 base::Value::CreateStringValue(service_path))) {
328 CallNotifyObserversPropertyChanged( 336 CallNotifyObserversPropertyChanged(
329 flimflam::kServiceWatchListProperty, 0); 337 flimflam::kServiceWatchListProperty, 0);
330 } 338 }
331 } 339 }
332 340
333 void ShillManagerClientStub::SetDefaultProperties() { 341 void ShillManagerClientStub::SetDefaultProperties() {
334 // Stub Devices, Note: names match Device stub map. 342 // Stub Technologies.
335 AddDevice("stub_wifi_device1"); 343 if (!CommandLine::ForCurrentProcess()->HasSwitch(
336 AddDevice("stub_cellular_device1"); 344 chromeos::switches::kDisableStubEthernet)) {
337 345 AddTechnology(flimflam::kTypeEthernet, true);
338 // Stub Services, Note: names match Service stub map. 346 }
339 AddService("stub_ethernet", true);
340 AddService("stub_wifi1", true);
341 AddService("stub_wifi2", true);
342 AddService("stub_cellular1", true);
343
344 // Stub Technologies
345 AddTechnology(flimflam::kTypeEthernet, true);
346 AddTechnology(flimflam::kTypeWifi, true); 347 AddTechnology(flimflam::kTypeWifi, true);
347 AddTechnology(flimflam::kTypeCellular, true); 348 AddTechnology(flimflam::kTypeCellular, true);
348 } 349 }
349 350
350 void ShillManagerClientStub::PassStubProperties( 351 void ShillManagerClientStub::PassStubProperties(
351 const DictionaryValueCallback& callback) const { 352 const DictionaryValueCallback& callback) const {
352 callback.Run(DBUS_METHOD_CALL_SUCCESS, stub_properties_); 353 scoped_ptr<base::DictionaryValue> stub_properties(
354 stub_properties_.DeepCopy());
355 // Remove disabled services from the list
356 stub_properties->SetWithoutPathExpansion(
357 flimflam::kServicesProperty,
358 GetEnabledServiceList(flimflam::kServicesProperty));
359 stub_properties->SetWithoutPathExpansion(
360 flimflam::kServiceWatchListProperty,
361 GetEnabledServiceList(flimflam::kServiceWatchListProperty));
362 callback.Run(DBUS_METHOD_CALL_SUCCESS, *stub_properties);
353 } 363 }
354 364
355 void ShillManagerClientStub::PassStubGeoNetworks( 365 void ShillManagerClientStub::PassStubGeoNetworks(
356 const DictionaryValueCallback& callback) const { 366 const DictionaryValueCallback& callback) const {
357 callback.Run(DBUS_METHOD_CALL_SUCCESS, stub_geo_networks_); 367 callback.Run(DBUS_METHOD_CALL_SUCCESS, stub_geo_networks_);
358 } 368 }
359 369
360 void ShillManagerClientStub::CallNotifyObserversPropertyChanged( 370 void ShillManagerClientStub::CallNotifyObserversPropertyChanged(
361 const std::string& property, 371 const std::string& property,
362 int delay_ms) { 372 int delay_ms) {
363 // Avoid unnecessary delayed task if we have no observers (e.g. during 373 // Avoid unnecessary delayed task if we have no observers (e.g. during
364 // initial setup). 374 // initial setup).
365 if (observer_list_.size() == 0) 375 if (observer_list_.size() == 0)
366 return; 376 return;
367 MessageLoop::current()->PostDelayedTask( 377 MessageLoop::current()->PostDelayedTask(
368 FROM_HERE, 378 FROM_HERE,
369 base::Bind(&ShillManagerClientStub::NotifyObserversPropertyChanged, 379 base::Bind(&ShillManagerClientStub::NotifyObserversPropertyChanged,
370 weak_ptr_factory_.GetWeakPtr(), 380 weak_ptr_factory_.GetWeakPtr(),
371 property), 381 property),
372 base::TimeDelta::FromMilliseconds(delay_ms)); 382 base::TimeDelta::FromMilliseconds(delay_ms));
373 } 383 }
374 384
375 void ShillManagerClientStub::NotifyObserversPropertyChanged( 385 void ShillManagerClientStub::NotifyObserversPropertyChanged(
376 const std::string& property) { 386 const std::string& property) {
387 if (property == flimflam::kServicesProperty ||
388 property == flimflam::kServiceWatchListProperty) {
389 scoped_ptr<base::ListValue> services(GetEnabledServiceList(property));
390 FOR_EACH_OBSERVER(ShillPropertyChangedObserver,
391 observer_list_,
392 OnPropertyChanged(property, *(services.get())));
393 return;
394 }
377 base::Value* value = NULL; 395 base::Value* value = NULL;
378 if (!stub_properties_.GetWithoutPathExpansion(property, &value)) { 396 if (!stub_properties_.GetWithoutPathExpansion(property, &value)) {
379 LOG(ERROR) << "Notify for unknown property: " << property; 397 LOG(ERROR) << "Notify for unknown property: " << property;
380 return; 398 return;
381 } 399 }
382 FOR_EACH_OBSERVER(ShillPropertyChangedObserver, 400 FOR_EACH_OBSERVER(ShillPropertyChangedObserver,
383 observer_list_, 401 observer_list_,
384 OnPropertyChanged(property, *value)); 402 OnPropertyChanged(property, *value));
385 } 403 }
386 404
387 base::ListValue* ShillManagerClientStub::GetListProperty( 405 base::ListValue* ShillManagerClientStub::GetListProperty(
388 const std::string& property) { 406 const std::string& property) {
389 base::ListValue* list_property = NULL; 407 base::ListValue* list_property = NULL;
390 if (!stub_properties_.GetListWithoutPathExpansion( 408 if (!stub_properties_.GetListWithoutPathExpansion(
391 property, &list_property)) { 409 property, &list_property)) {
392 list_property = new base::ListValue; 410 list_property = new base::ListValue;
393 stub_properties_.Set(property, list_property); 411 stub_properties_.SetWithoutPathExpansion(property, list_property);
394 } 412 }
395 return list_property; 413 return list_property;
396 } 414 }
397 415
416 bool ShillManagerClientStub::TechnologyEnabled(const std::string& type) const {
417 bool enabled = false;
418 const base::ListValue* technologies;
419 if (stub_properties_.GetListWithoutPathExpansion(
420 flimflam::kEnabledTechnologiesProperty, &technologies)) {
421 base::StringValue type_value(type);
422 if (technologies->Find(type_value) != technologies->end())
423 enabled = true;
424 }
425 return enabled;
426 }
427
428 base::ListValue* ShillManagerClientStub::GetEnabledServiceList(
429 const std::string& property) const {
430 base::ListValue* new_service_list = new base::ListValue;
431 const base::ListValue* service_list;
432 if (stub_properties_.GetListWithoutPathExpansion(property, &service_list)) {
433 ShillServiceClient::TestInterface* service_client =
434 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface();
435 for (base::ListValue::const_iterator iter = service_list->begin();
436 iter != service_list->end(); ++iter) {
437 std::string service_path;
438 if (!(*iter)->GetAsString(&service_path))
439 continue;
440 const base::DictionaryValue* properties =
441 service_client->GetServiceProperties(service_path);
442 std::string name;
443 properties->GetString(flimflam::kNameProperty, &name);
444 std::string type;
445 properties->GetString(flimflam::kTypeProperty, &type);
446 if (TechnologyEnabled(type))
447 new_service_list->Append((*iter)->DeepCopy());
448 }
449 }
450 return new_service_list;
451 }
452
398 } // namespace chromeos 453 } // namespace chromeos
OLDNEW
« no previous file with comments | « chromeos/dbus/shill_manager_client_stub.h ('k') | chromeos/dbus/shill_service_client.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698