Don't preload rarely seen large images
[chromium-blink-merge.git] / chromeos / dbus / fake_shill_manager_client.cc
blob6a6cc3e90044049ca3f46ee84782c38be3b133f2
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chromeos/dbus/fake_shill_manager_client.h"
7 #include "base/bind.h"
8 #include "base/command_line.h"
9 #include "base/location.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/single_thread_task_runner.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/string_split.h"
14 #include "base/strings/string_util.h"
15 #include "base/strings/stringprintf.h"
16 #include "base/thread_task_runner_handle.h"
17 #include "base/values.h"
18 #include "chromeos/chromeos_switches.h"
19 #include "chromeos/dbus/dbus_thread_manager.h"
20 #include "chromeos/dbus/shill_device_client.h"
21 #include "chromeos/dbus/shill_ipconfig_client.h"
22 #include "chromeos/dbus/shill_profile_client.h"
23 #include "chromeos/dbus/shill_property_changed_observer.h"
24 #include "chromeos/dbus/shill_service_client.h"
25 #include "dbus/bus.h"
26 #include "dbus/message.h"
27 #include "dbus/object_path.h"
28 #include "dbus/values_util.h"
29 #include "third_party/cros_system_api/dbus/service_constants.h"
31 namespace chromeos {
33 namespace {
35 // Allow parsed command line option 'tdls_busy' to set the fake busy count.
36 int s_tdls_busy_count = 0;
37 int s_extra_wifi_networks = 0;
39 // Used to compare values for finding entries to erase in a ListValue.
40 // (ListValue only implements a const_iterator version of Find).
41 struct ValueEquals {
42 explicit ValueEquals(const base::Value* first) : first_(first) {}
43 bool operator()(const base::Value* second) const {
44 return first_->Equals(second);
46 const base::Value* first_;
49 // Appends string entries from |service_list_in| whose entries in ServiceClient
50 // have Type |match_type| to one of the output lists based on the entry's State.
51 void AppendServicesForType(
52 const base::ListValue* service_list_in,
53 const char* match_type,
54 bool technology_enabled,
55 std::vector<std::string>* active_service_list_out,
56 std::vector<std::string>* inactive_service_list_out,
57 std::vector<std::string>* disabled_service_list_out) {
58 ShillServiceClient::TestInterface* service_client =
59 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface();
60 for (base::ListValue::const_iterator iter = service_list_in->begin();
61 iter != service_list_in->end(); ++iter) {
62 std::string service_path;
63 if (!(*iter)->GetAsString(&service_path))
64 continue;
65 const base::DictionaryValue* properties =
66 service_client->GetServiceProperties(service_path);
67 if (!properties) {
68 LOG(ERROR) << "Properties not found for service: " << service_path;
69 continue;
71 std::string type;
72 properties->GetString(shill::kTypeProperty, &type);
73 if (type != match_type)
74 continue;
75 bool visible = false;
76 if (technology_enabled)
77 properties->GetBoolean(shill::kVisibleProperty, &visible);
78 if (!visible) {
79 disabled_service_list_out->push_back(service_path);
80 continue;
82 std::string state;
83 properties->GetString(shill::kStateProperty, &state);
84 if (state == shill::kStateOnline ||
85 state == shill::kStateAssociation ||
86 state == shill::kStateConfiguration ||
87 state == shill::kStatePortal ||
88 state == shill::kStateReady) {
89 active_service_list_out->push_back(service_path);
90 } else {
91 inactive_service_list_out->push_back(service_path);
96 void LogErrorCallback(const std::string& error_name,
97 const std::string& error_message) {
98 LOG(ERROR) << error_name << ": " << error_message;
101 bool IsConnectedState(const std::string& state) {
102 return state == shill::kStateOnline || state == shill::kStatePortal ||
103 state == shill::kStateReady;
106 void UpdatePortaledWifiState(const std::string& service_path) {
107 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface()
108 ->SetServiceProperty(service_path,
109 shill::kStateProperty,
110 base::StringValue(shill::kStatePortal));
113 bool IsCellularTechnology(const std::string& type) {
114 return (type == shill::kNetworkTechnology1Xrtt ||
115 type == shill::kNetworkTechnologyEvdo ||
116 type == shill::kNetworkTechnologyGsm ||
117 type == shill::kNetworkTechnologyGprs ||
118 type == shill::kNetworkTechnologyEdge ||
119 type == shill::kNetworkTechnologyUmts ||
120 type == shill::kNetworkTechnologyHspa ||
121 type == shill::kNetworkTechnologyHspaPlus ||
122 type == shill::kNetworkTechnologyLte ||
123 type == shill::kNetworkTechnologyLteAdvanced);
126 const char* kTechnologyUnavailable = "unavailable";
127 const char* kNetworkActivated = "activated";
128 const char* kNetworkDisabled = "disabled";
129 const char* kCellularServicePath = "/service/cellular1";
130 const char* kRoamingRequired = "required";
132 } // namespace
134 // static
135 const char FakeShillManagerClient::kFakeEthernetNetworkGuid[] = "eth1_guid";
137 FakeShillManagerClient::FakeShillManagerClient()
138 : interactive_delay_(0),
139 cellular_technology_(shill::kNetworkTechnologyGsm),
140 weak_ptr_factory_(this) {
141 ParseCommandLineSwitch();
144 FakeShillManagerClient::~FakeShillManagerClient() {}
146 // ShillManagerClient overrides.
148 void FakeShillManagerClient::Init(dbus::Bus* bus) {}
150 void FakeShillManagerClient::AddPropertyChangedObserver(
151 ShillPropertyChangedObserver* observer) {
152 observer_list_.AddObserver(observer);
155 void FakeShillManagerClient::RemovePropertyChangedObserver(
156 ShillPropertyChangedObserver* observer) {
157 observer_list_.RemoveObserver(observer);
160 void FakeShillManagerClient::GetProperties(
161 const DictionaryValueCallback& callback) {
162 VLOG(1) << "Manager.GetProperties";
163 base::ThreadTaskRunnerHandle::Get()->PostTask(
164 FROM_HERE, base::Bind(&FakeShillManagerClient::PassStubProperties,
165 weak_ptr_factory_.GetWeakPtr(), callback));
168 void FakeShillManagerClient::GetNetworksForGeolocation(
169 const DictionaryValueCallback& callback) {
170 base::ThreadTaskRunnerHandle::Get()->PostTask(
171 FROM_HERE, base::Bind(&FakeShillManagerClient::PassStubGeoNetworks,
172 weak_ptr_factory_.GetWeakPtr(), callback));
175 void FakeShillManagerClient::SetProperty(const std::string& name,
176 const base::Value& value,
177 const base::Closure& callback,
178 const ErrorCallback& error_callback) {
179 VLOG(2) << "SetProperty: " << name;
180 stub_properties_.SetWithoutPathExpansion(name, value.DeepCopy());
181 CallNotifyObserversPropertyChanged(name);
182 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback);
185 void FakeShillManagerClient::RequestScan(const std::string& type,
186 const base::Closure& callback,
187 const ErrorCallback& error_callback) {
188 // For Stub purposes, default to a Wifi scan.
189 std::string device_type = shill::kTypeWifi;
190 if (!type.empty())
191 device_type = type;
192 ShillDeviceClient::TestInterface* device_client =
193 DBusThreadManager::Get()->GetShillDeviceClient()->GetTestInterface();
194 std::string device_path = device_client->GetDevicePathForType(device_type);
195 if (!device_path.empty()) {
196 device_client->SetDeviceProperty(
197 device_path, shill::kScanningProperty, base::FundamentalValue(true));
199 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
200 FROM_HERE,
201 base::Bind(&FakeShillManagerClient::ScanCompleted,
202 weak_ptr_factory_.GetWeakPtr(), device_path, callback),
203 base::TimeDelta::FromSeconds(interactive_delay_));
206 void FakeShillManagerClient::EnableTechnology(
207 const std::string& type,
208 const base::Closure& callback,
209 const ErrorCallback& error_callback) {
210 base::ListValue* enabled_list = NULL;
211 if (!stub_properties_.GetListWithoutPathExpansion(
212 shill::kAvailableTechnologiesProperty, &enabled_list)) {
213 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback);
214 base::ThreadTaskRunnerHandle::Get()->PostTask(
215 FROM_HERE,
216 base::Bind(error_callback, "StubError", "Property not found"));
217 return;
219 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
220 FROM_HERE,
221 base::Bind(&FakeShillManagerClient::SetTechnologyEnabled,
222 weak_ptr_factory_.GetWeakPtr(), type, callback, true),
223 base::TimeDelta::FromSeconds(interactive_delay_));
226 void FakeShillManagerClient::DisableTechnology(
227 const std::string& type,
228 const base::Closure& callback,
229 const ErrorCallback& error_callback) {
230 base::ListValue* enabled_list = NULL;
231 if (!stub_properties_.GetListWithoutPathExpansion(
232 shill::kAvailableTechnologiesProperty, &enabled_list)) {
233 base::ThreadTaskRunnerHandle::Get()->PostTask(
234 FROM_HERE,
235 base::Bind(error_callback, "StubError", "Property not found"));
236 return;
238 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
239 FROM_HERE,
240 base::Bind(&FakeShillManagerClient::SetTechnologyEnabled,
241 weak_ptr_factory_.GetWeakPtr(), type, callback, false),
242 base::TimeDelta::FromSeconds(interactive_delay_));
245 void FakeShillManagerClient::ConfigureService(
246 const base::DictionaryValue& properties,
247 const ObjectPathCallback& callback,
248 const ErrorCallback& error_callback) {
249 ShillServiceClient::TestInterface* service_client =
250 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface();
252 std::string guid;
253 std::string type;
254 if (!properties.GetString(shill::kGuidProperty, &guid) ||
255 !properties.GetString(shill::kTypeProperty, &type)) {
256 LOG(ERROR) << "ConfigureService requires GUID and Type to be defined";
257 // If the properties aren't filled out completely, then just return an empty
258 // object path.
259 base::ThreadTaskRunnerHandle::Get()->PostTask(
260 FROM_HERE, base::Bind(callback, dbus::ObjectPath()));
261 return;
264 // For the purposes of this stub, we're going to assume that the GUID property
265 // is set to the service path because we don't want to re-implement Shill's
266 // property matching magic here.
267 std::string service_path = guid;
269 std::string ipconfig_path;
270 properties.GetString(shill::kIPConfigProperty, &ipconfig_path);
272 // Merge the new properties with existing properties, if any.
273 const base::DictionaryValue* existing_properties =
274 service_client->GetServiceProperties(service_path);
275 if (!existing_properties) {
276 // Add a new service to the service client stub because none exists, yet.
277 // This calls AddManagerService.
278 service_client->AddServiceWithIPConfig(service_path,
279 guid /* guid */,
280 guid /* name */,
281 type,
282 shill::kStateIdle,
283 ipconfig_path,
284 true /* visible */);
285 existing_properties = service_client->GetServiceProperties(service_path);
288 scoped_ptr<base::DictionaryValue> merged_properties(
289 existing_properties->DeepCopy());
290 merged_properties->MergeDictionary(&properties);
292 // Now set all the properties.
293 for (base::DictionaryValue::Iterator iter(*merged_properties);
294 !iter.IsAtEnd(); iter.Advance()) {
295 service_client->SetServiceProperty(service_path, iter.key(), iter.value());
298 // If the Profile property is set, add it to ProfileClient.
299 std::string profile_path;
300 merged_properties->GetStringWithoutPathExpansion(shill::kProfileProperty,
301 &profile_path);
302 if (!profile_path.empty()) {
303 DBusThreadManager::Get()->GetShillProfileClient()->GetTestInterface()->
304 AddService(profile_path, service_path);
307 base::ThreadTaskRunnerHandle::Get()->PostTask(
308 FROM_HERE, base::Bind(callback, dbus::ObjectPath(service_path)));
311 void FakeShillManagerClient::ConfigureServiceForProfile(
312 const dbus::ObjectPath& profile_path,
313 const base::DictionaryValue& properties,
314 const ObjectPathCallback& callback,
315 const ErrorCallback& error_callback) {
316 std::string profile_property;
317 properties.GetStringWithoutPathExpansion(shill::kProfileProperty,
318 &profile_property);
319 CHECK(profile_property == profile_path.value());
320 ConfigureService(properties, callback, error_callback);
324 void FakeShillManagerClient::GetService(
325 const base::DictionaryValue& properties,
326 const ObjectPathCallback& callback,
327 const ErrorCallback& error_callback) {
328 base::ThreadTaskRunnerHandle::Get()->PostTask(
329 FROM_HERE, base::Bind(callback, dbus::ObjectPath()));
332 void FakeShillManagerClient::VerifyDestination(
333 const VerificationProperties& properties,
334 const BooleanCallback& callback,
335 const ErrorCallback& error_callback) {
336 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
337 base::Bind(callback, true));
340 void FakeShillManagerClient::VerifyAndEncryptCredentials(
341 const VerificationProperties& properties,
342 const std::string& service_path,
343 const StringCallback& callback,
344 const ErrorCallback& error_callback) {
345 base::ThreadTaskRunnerHandle::Get()->PostTask(
346 FROM_HERE, base::Bind(callback, "encrypted_credentials"));
349 void FakeShillManagerClient::VerifyAndEncryptData(
350 const VerificationProperties& properties,
351 const std::string& data,
352 const StringCallback& callback,
353 const ErrorCallback& error_callback) {
354 base::ThreadTaskRunnerHandle::Get()->PostTask(
355 FROM_HERE, base::Bind(callback, "encrypted_data"));
358 void FakeShillManagerClient::ConnectToBestServices(
359 const base::Closure& callback,
360 const ErrorCallback& error_callback) {
361 if (best_service_.empty()) {
362 VLOG(1) << "No 'best' service set.";
363 return;
366 DBusThreadManager::Get()->GetShillServiceClient()->Connect(
367 dbus::ObjectPath(best_service_), callback, error_callback);
370 ShillManagerClient::TestInterface* FakeShillManagerClient::GetTestInterface() {
371 return this;
374 // ShillManagerClient::TestInterface overrides.
376 void FakeShillManagerClient::AddDevice(const std::string& device_path) {
377 if (GetListProperty(shill::kDevicesProperty)
378 ->AppendIfNotPresent(new base::StringValue(device_path))) {
379 CallNotifyObserversPropertyChanged(shill::kDevicesProperty);
383 void FakeShillManagerClient::RemoveDevice(const std::string& device_path) {
384 base::StringValue device_path_value(device_path);
385 if (GetListProperty(shill::kDevicesProperty)->Remove(
386 device_path_value, NULL)) {
387 CallNotifyObserversPropertyChanged(shill::kDevicesProperty);
391 void FakeShillManagerClient::ClearDevices() {
392 GetListProperty(shill::kDevicesProperty)->Clear();
393 CallNotifyObserversPropertyChanged(shill::kDevicesProperty);
396 void FakeShillManagerClient::AddTechnology(const std::string& type,
397 bool enabled) {
398 if (GetListProperty(shill::kAvailableTechnologiesProperty)
399 ->AppendIfNotPresent(new base::StringValue(type))) {
400 CallNotifyObserversPropertyChanged(
401 shill::kAvailableTechnologiesProperty);
403 if (enabled &&
404 GetListProperty(shill::kEnabledTechnologiesProperty)
405 ->AppendIfNotPresent(new base::StringValue(type))) {
406 CallNotifyObserversPropertyChanged(
407 shill::kEnabledTechnologiesProperty);
411 void FakeShillManagerClient::RemoveTechnology(const std::string& type) {
412 base::StringValue type_value(type);
413 if (GetListProperty(shill::kAvailableTechnologiesProperty)->Remove(
414 type_value, NULL)) {
415 CallNotifyObserversPropertyChanged(
416 shill::kAvailableTechnologiesProperty);
418 if (GetListProperty(shill::kEnabledTechnologiesProperty)->Remove(
419 type_value, NULL)) {
420 CallNotifyObserversPropertyChanged(
421 shill::kEnabledTechnologiesProperty);
425 void FakeShillManagerClient::SetTechnologyInitializing(const std::string& type,
426 bool initializing) {
427 if (initializing) {
428 if (GetListProperty(shill::kUninitializedTechnologiesProperty)
429 ->AppendIfNotPresent(new base::StringValue(type))) {
430 CallNotifyObserversPropertyChanged(
431 shill::kUninitializedTechnologiesProperty);
433 } else {
434 if (GetListProperty(shill::kUninitializedTechnologiesProperty)->Remove(
435 base::StringValue(type), NULL)) {
436 CallNotifyObserversPropertyChanged(
437 shill::kUninitializedTechnologiesProperty);
442 void FakeShillManagerClient::AddGeoNetwork(
443 const std::string& technology,
444 const base::DictionaryValue& network) {
445 base::ListValue* list_value = NULL;
446 if (!stub_geo_networks_.GetListWithoutPathExpansion(technology,
447 &list_value)) {
448 list_value = new base::ListValue;
449 stub_geo_networks_.SetWithoutPathExpansion(technology, list_value);
451 list_value->Append(network.DeepCopy());
454 void FakeShillManagerClient::AddProfile(const std::string& profile_path) {
455 const char* key = shill::kProfilesProperty;
456 if (GetListProperty(key)
457 ->AppendIfNotPresent(new base::StringValue(profile_path))) {
458 CallNotifyObserversPropertyChanged(key);
462 void FakeShillManagerClient::ClearProperties() {
463 stub_properties_.Clear();
466 void FakeShillManagerClient::SetManagerProperty(const std::string& key,
467 const base::Value& value) {
468 SetProperty(key, value,
469 base::Bind(&base::DoNothing), base::Bind(&LogErrorCallback));
472 void FakeShillManagerClient::AddManagerService(
473 const std::string& service_path,
474 bool notify_observers) {
475 VLOG(2) << "AddManagerService: " << service_path;
476 GetListProperty(shill::kServiceCompleteListProperty)
477 ->AppendIfNotPresent(new base::StringValue(service_path));
478 SortManagerServices(false);
479 if (notify_observers)
480 CallNotifyObserversPropertyChanged(shill::kServiceCompleteListProperty);
483 void FakeShillManagerClient::RemoveManagerService(
484 const std::string& service_path) {
485 VLOG(2) << "RemoveManagerService: " << service_path;
486 base::StringValue service_path_value(service_path);
487 GetListProperty(shill::kServiceCompleteListProperty)->Remove(
488 service_path_value, NULL);
489 CallNotifyObserversPropertyChanged(shill::kServiceCompleteListProperty);
492 void FakeShillManagerClient::ClearManagerServices() {
493 VLOG(1) << "ClearManagerServices";
494 GetListProperty(shill::kServiceCompleteListProperty)->Clear();
495 CallNotifyObserversPropertyChanged(shill::kServiceCompleteListProperty);
498 void FakeShillManagerClient::ServiceStateChanged(
499 const std::string& service_path,
500 const std::string& state) {
501 if (service_path == default_service_ && !IsConnectedState(state)) {
502 // Default service is no longer connected; clear.
503 default_service_.clear();
504 base::StringValue default_service_value(default_service_);
505 SetManagerProperty(shill::kDefaultServiceProperty, default_service_value);
509 void FakeShillManagerClient::SortManagerServices(bool notify) {
510 VLOG(1) << "SortManagerServices";
511 static const char* ordered_types[] = {shill::kTypeEthernet,
512 shill::kTypeEthernetEap,
513 shill::kTypeWifi,
514 shill::kTypeCellular,
515 shill::kTypeWimax,
516 shill::kTypeVPN};
518 base::ListValue* complete_list =
519 GetListProperty(shill::kServiceCompleteListProperty);
520 if (complete_list->empty())
521 return;
522 scoped_ptr<base::ListValue> prev_complete_list(complete_list->DeepCopy());
524 std::vector<std::string> active_services;
525 std::vector<std::string> inactive_services;
526 std::vector<std::string> disabled_services;
527 for (size_t i = 0; i < arraysize(ordered_types); ++i) {
528 AppendServicesForType(complete_list,
529 ordered_types[i],
530 TechnologyEnabled(ordered_types[i]),
531 &active_services,
532 &inactive_services,
533 &disabled_services);
535 complete_list->Clear();
536 for (size_t i = 0; i < active_services.size(); ++i)
537 complete_list->AppendString(active_services[i]);
538 for (size_t i = 0; i < inactive_services.size(); ++i)
539 complete_list->AppendString(inactive_services[i]);
540 for (size_t i = 0; i < disabled_services.size(); ++i)
541 complete_list->AppendString(disabled_services[i]);
543 if (notify && !complete_list->Equals(prev_complete_list.get()))
544 CallNotifyObserversPropertyChanged(shill::kServiceCompleteListProperty);
546 // Set the first active service as the Default service.
547 std::string new_default_service;
548 if (!active_services.empty()) {
549 ShillServiceClient::TestInterface* service_client =
550 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface();
551 std::string service_path = active_services[0];
552 const base::DictionaryValue* properties =
553 service_client->GetServiceProperties(service_path);
554 if (!properties) {
555 LOG(ERROR) << "Properties not found for service: " << service_path;
556 } else {
557 std::string state;
558 properties->GetString(shill::kStateProperty, &state);
559 if (IsConnectedState(state))
560 new_default_service = service_path;
563 if (default_service_ != new_default_service) {
564 default_service_ = new_default_service;
565 base::StringValue default_service_value(default_service_);
566 SetManagerProperty(shill::kDefaultServiceProperty, default_service_value);
570 int FakeShillManagerClient::GetInteractiveDelay() const {
571 return interactive_delay_;
574 void FakeShillManagerClient::SetBestServiceToConnect(
575 const std::string& service_path) {
576 best_service_ = service_path;
579 void FakeShillManagerClient::SetupDefaultEnvironment() {
580 // Bail out from setup if there is no message loop. This will be the common
581 // case for tests that are not testing Shill.
582 if (!base::ThreadTaskRunnerHandle::IsSet())
583 return;
585 DBusThreadManager* dbus_manager = DBusThreadManager::Get();
586 ShillServiceClient::TestInterface* services =
587 dbus_manager->GetShillServiceClient()->GetTestInterface();
588 DCHECK(services);
589 ShillProfileClient::TestInterface* profiles =
590 dbus_manager->GetShillProfileClient()->GetTestInterface();
591 DCHECK(profiles);
592 ShillDeviceClient::TestInterface* devices =
593 dbus_manager->GetShillDeviceClient()->GetTestInterface();
594 DCHECK(devices);
595 ShillIPConfigClient::TestInterface* ip_configs =
596 dbus_manager->GetShillIPConfigClient()->GetTestInterface();
597 DCHECK(ip_configs);
599 const std::string shared_profile = ShillProfileClient::GetSharedProfilePath();
600 profiles->AddProfile(shared_profile, std::string());
602 const bool add_to_visible = true;
604 // IPConfigs
605 base::DictionaryValue ipconfig_v4_dictionary;
606 ipconfig_v4_dictionary.SetStringWithoutPathExpansion(
607 shill::kAddressProperty, "100.0.0.1");
608 ipconfig_v4_dictionary.SetStringWithoutPathExpansion(
609 shill::kGatewayProperty, "100.0.0.2");
610 ipconfig_v4_dictionary.SetIntegerWithoutPathExpansion(
611 shill::kPrefixlenProperty, 1);
612 ipconfig_v4_dictionary.SetStringWithoutPathExpansion(
613 shill::kMethodProperty, shill::kTypeIPv4);
614 ip_configs->AddIPConfig("ipconfig_v4_path", ipconfig_v4_dictionary);
615 base::DictionaryValue ipconfig_v6_dictionary;
616 ipconfig_v6_dictionary.SetStringWithoutPathExpansion(
617 shill::kAddressProperty, "0:0:0:0:100:0:0:1");
618 ipconfig_v6_dictionary.SetStringWithoutPathExpansion(
619 shill::kMethodProperty, shill::kTypeIPv6);
620 ip_configs->AddIPConfig("ipconfig_v6_path", ipconfig_v6_dictionary);
622 bool enabled;
623 std::string state;
625 // Ethernet
626 state = GetInitialStateForType(shill::kTypeEthernet, &enabled);
627 if (state == shill::kStateOnline) {
628 AddTechnology(shill::kTypeEthernet, enabled);
629 devices->AddDevice(
630 "/device/eth1", shill::kTypeEthernet, "stub_eth_device1");
631 devices->SetDeviceProperty("/device/eth1",
632 shill::kAddressProperty,
633 base::StringValue("0123456789ab"));
634 base::ListValue eth_ip_configs;
635 eth_ip_configs.AppendString("ipconfig_v4_path");
636 eth_ip_configs.AppendString("ipconfig_v6_path");
637 devices->SetDeviceProperty("/device/eth1",
638 shill::kIPConfigsProperty,
639 eth_ip_configs);
640 const std::string kFakeEthernetNetworkPath = "/service/eth1";
641 services->AddService(kFakeEthernetNetworkPath,
642 kFakeEthernetNetworkGuid,
643 "eth1" /* name */,
644 shill::kTypeEthernet,
645 state,
646 add_to_visible);
647 profiles->AddService(shared_profile, kFakeEthernetNetworkPath);
650 // Wifi
651 if (s_tdls_busy_count != 0) {
652 DBusThreadManager::Get()
653 ->GetShillDeviceClient()
654 ->GetTestInterface()
655 ->SetTDLSBusyCount(s_tdls_busy_count);
658 state = GetInitialStateForType(shill::kTypeWifi, &enabled);
659 if (state != kTechnologyUnavailable) {
660 bool portaled = false;
661 if (state == shill::kStatePortal) {
662 portaled = true;
663 state = shill::kStateIdle;
665 AddTechnology(shill::kTypeWifi, enabled);
666 devices->AddDevice("/device/wifi1", shill::kTypeWifi, "stub_wifi_device1");
667 devices->SetDeviceProperty("/device/wifi1",
668 shill::kAddressProperty,
669 base::StringValue("23456789abcd"));
670 base::ListValue wifi_ip_configs;
671 wifi_ip_configs.AppendString("ipconfig_v4_path");
672 wifi_ip_configs.AppendString("ipconfig_v6_path");
673 devices->SetDeviceProperty("/device/wifi1",
674 shill::kIPConfigsProperty,
675 wifi_ip_configs);
677 const std::string kWifi1Path = "/service/wifi1";
678 services->AddService(kWifi1Path,
679 "wifi1_guid",
680 "wifi1" /* name */,
681 shill::kTypeWifi,
682 state,
683 add_to_visible);
684 services->SetServiceProperty(kWifi1Path,
685 shill::kSecurityClassProperty,
686 base::StringValue(shill::kSecurityWep));
687 services->SetServiceProperty(kWifi1Path,
688 shill::kConnectableProperty,
689 base::FundamentalValue(true));
690 profiles->AddService(shared_profile, kWifi1Path);
692 const std::string kWifi2Path = "/service/wifi2";
693 services->AddService(kWifi2Path,
694 "wifi2_PSK_guid",
695 "wifi2_PSK" /* name */,
696 shill::kTypeWifi,
697 shill::kStateIdle,
698 add_to_visible);
699 services->SetServiceProperty(kWifi2Path,
700 shill::kSecurityClassProperty,
701 base::StringValue(shill::kSecurityPsk));
702 services->SetServiceProperty(
703 kWifi2Path, shill::kSignalStrengthProperty, base::FundamentalValue(80));
704 profiles->AddService(shared_profile, kWifi2Path);
706 const std::string kWifi3Path = "/service/wifi3";
707 services->AddService(kWifi3Path,
708 "", /* empty GUID */
709 "wifi3" /* name */,
710 shill::kTypeWifi,
711 shill::kStateIdle,
712 add_to_visible);
713 services->SetServiceProperty(
714 kWifi3Path, shill::kSignalStrengthProperty, base::FundamentalValue(40));
716 if (portaled) {
717 const std::string kPortaledWifiPath = "/service/portaled_wifi";
718 services->AddService(kPortaledWifiPath, "portaled_wifi_guid",
719 "Portaled Wifi" /* name */, shill::kTypeWifi,
720 shill::kStateIdle, add_to_visible);
721 services->SetServiceProperty(kPortaledWifiPath,
722 shill::kSecurityClassProperty,
723 base::StringValue(shill::kSecurityNone));
724 services->SetConnectBehavior(
725 kPortaledWifiPath,
726 base::Bind(&UpdatePortaledWifiState, kPortaledWifiPath));
727 services->SetServiceProperty(kPortaledWifiPath,
728 shill::kConnectableProperty,
729 base::FundamentalValue(true));
730 profiles->AddService(shared_profile, kPortaledWifiPath);
733 for (int i = 0; i < s_extra_wifi_networks; ++i) {
734 int id = 4 + i;
735 std::string path = base::StringPrintf("/service/wifi%d", id);
736 std::string guid = base::StringPrintf("wifi%d_guid", id);
737 std::string name = base::StringPrintf("wifi%d", id);
738 services->AddService(path, guid, name, shill::kTypeWifi,
739 shill::kStateIdle, add_to_visible);
743 // Wimax
744 const std::string kWimaxPath = "/service/wimax1";
745 state = GetInitialStateForType(shill::kTypeWimax, &enabled);
746 if (state != kTechnologyUnavailable) {
747 AddTechnology(shill::kTypeWimax, enabled);
748 devices->AddDevice(
749 "/device/wimax1", shill::kTypeWimax, "stub_wimax_device1");
751 services->AddService(kWimaxPath, "wimax1_guid", "wimax1" /* name */,
752 shill::kTypeWimax, state, add_to_visible);
753 services->SetServiceProperty(kWimaxPath, shill::kConnectableProperty,
754 base::FundamentalValue(true));
755 base::FundamentalValue strength_value(80);
756 services->SetServiceProperty(kWimaxPath, shill::kSignalStrengthProperty,
757 strength_value);
758 profiles->AddService(shared_profile, kWimaxPath);
761 // Cellular
762 state = GetInitialStateForType(shill::kTypeCellular, &enabled);
763 if (state != kTechnologyUnavailable) {
764 bool activated = false;
765 if (state == kNetworkActivated) {
766 activated = true;
767 state = shill::kStateIdle;
769 AddTechnology(shill::kTypeCellular, enabled);
770 devices->AddDevice(
771 "/device/cellular1", shill::kTypeCellular, "stub_cellular_device1");
772 devices->SetDeviceProperty("/device/cellular1",
773 shill::kCarrierProperty,
774 base::StringValue(shill::kCarrierSprint));
775 base::ListValue carrier_list;
776 carrier_list.AppendString(shill::kCarrierSprint);
777 carrier_list.AppendString(shill::kCarrierGenericUMTS);
778 devices->SetDeviceProperty("/device/cellular1",
779 shill::kSupportedCarriersProperty,
780 carrier_list);
781 devices->SetDeviceProperty("/device/cellular1",
782 shill::kSupportNetworkScanProperty,
783 base::FundamentalValue(true));
784 if (roaming_state_ == kRoamingRequired) {
785 devices->SetDeviceProperty("/device/cellular1",
786 shill::kProviderRequiresRoamingProperty,
787 base::FundamentalValue(true));
790 services->AddService(kCellularServicePath,
791 "cellular1_guid",
792 "cellular1" /* name */,
793 shill::kTypeCellular,
794 state,
795 add_to_visible);
796 base::StringValue technology_value(cellular_technology_);
797 devices->SetDeviceProperty("/device/cellular1",
798 shill::kTechnologyFamilyProperty,
799 technology_value);
800 services->SetServiceProperty(kCellularServicePath,
801 shill::kNetworkTechnologyProperty,
802 technology_value);
804 if (activated) {
805 services->SetServiceProperty(
806 kCellularServicePath,
807 shill::kActivationStateProperty,
808 base::StringValue(shill::kActivationStateActivated));
809 services->SetServiceProperty(kCellularServicePath,
810 shill::kConnectableProperty,
811 base::FundamentalValue(true));
812 } else {
813 services->SetServiceProperty(
814 kCellularServicePath,
815 shill::kActivationStateProperty,
816 base::StringValue(shill::kActivationStateNotActivated));
819 std::string shill_roaming_state;
820 if (roaming_state_ == kRoamingRequired)
821 shill_roaming_state = shill::kRoamingStateRoaming;
822 else if (roaming_state_.empty())
823 shill_roaming_state = shill::kRoamingStateHome;
824 else // |roaming_state_| is expected to be a valid Shill state.
825 shill_roaming_state = roaming_state_;
826 services->SetServiceProperty(kCellularServicePath,
827 shill::kRoamingStateProperty,
828 base::StringValue(shill_roaming_state));
829 profiles->AddService(shared_profile, kCellularServicePath);
832 // VPN
833 state = GetInitialStateForType(shill::kTypeVPN, &enabled);
834 if (state != kTechnologyUnavailable) {
835 // Set the "Provider" dictionary properties. Note: when setting these in
836 // Shill, "Provider.Type", etc keys are used, but when reading the values
837 // "Provider" . "Type", etc keys are used. Here we are setting the values
838 // that will be read (by the UI, tests, etc).
839 base::DictionaryValue provider_properties_openvpn;
840 provider_properties_openvpn.SetString(shill::kTypeProperty,
841 shill::kProviderOpenVpn);
842 provider_properties_openvpn.SetString(shill::kHostProperty, "vpn_host");
844 services->AddService("/service/vpn1",
845 "vpn1_guid",
846 "vpn1" /* name */,
847 shill::kTypeVPN,
848 state,
849 add_to_visible);
850 services->SetServiceProperty(
851 "/service/vpn1", shill::kProviderProperty, provider_properties_openvpn);
852 profiles->AddService(shared_profile, "/service/vpn1");
854 base::DictionaryValue provider_properties_l2tp;
855 provider_properties_l2tp.SetString(shill::kTypeProperty,
856 shill::kProviderL2tpIpsec);
857 provider_properties_l2tp.SetString(shill::kHostProperty, "vpn_host2");
859 services->AddService("/service/vpn2",
860 "vpn2_guid",
861 "vpn2" /* name */,
862 shill::kTypeVPN,
863 shill::kStateIdle,
864 add_to_visible);
865 services->SetServiceProperty(
866 "/service/vpn2", shill::kProviderProperty, provider_properties_l2tp);
869 // Additional device states
870 for (DevicePropertyMap::iterator iter1 = shill_device_property_map_.begin();
871 iter1 != shill_device_property_map_.end(); ++iter1) {
872 std::string device_type = iter1->first;
873 std::string device_path = devices->GetDevicePathForType(device_type);
874 for (ShillPropertyMap::iterator iter2 = iter1->second.begin();
875 iter2 != iter1->second.end(); ++iter2) {
876 devices->SetDeviceProperty(device_path, iter2->first, *(iter2->second));
877 delete iter2->second;
881 SortManagerServices(true);
884 // Private methods
886 void FakeShillManagerClient::PassStubProperties(
887 const DictionaryValueCallback& callback) const {
888 scoped_ptr<base::DictionaryValue> stub_properties(
889 stub_properties_.DeepCopy());
890 stub_properties->SetWithoutPathExpansion(
891 shill::kServiceCompleteListProperty,
892 GetEnabledServiceList(shill::kServiceCompleteListProperty));
893 callback.Run(DBUS_METHOD_CALL_SUCCESS, *stub_properties);
896 void FakeShillManagerClient::PassStubGeoNetworks(
897 const DictionaryValueCallback& callback) const {
898 callback.Run(DBUS_METHOD_CALL_SUCCESS, stub_geo_networks_);
901 void FakeShillManagerClient::CallNotifyObserversPropertyChanged(
902 const std::string& property) {
903 // Avoid unnecessary delayed task if we have no observers (e.g. during
904 // initial setup).
905 if (!observer_list_.might_have_observers())
906 return;
907 base::ThreadTaskRunnerHandle::Get()->PostTask(
908 FROM_HERE,
909 base::Bind(&FakeShillManagerClient::NotifyObserversPropertyChanged,
910 weak_ptr_factory_.GetWeakPtr(), property));
913 void FakeShillManagerClient::NotifyObserversPropertyChanged(
914 const std::string& property) {
915 VLOG(1) << "NotifyObserversPropertyChanged: " << property;
916 base::Value* value = NULL;
917 if (!stub_properties_.GetWithoutPathExpansion(property, &value)) {
918 LOG(ERROR) << "Notify for unknown property: " << property;
919 return;
921 if (property == shill::kServiceCompleteListProperty) {
922 scoped_ptr<base::ListValue> services(GetEnabledServiceList(property));
923 FOR_EACH_OBSERVER(ShillPropertyChangedObserver,
924 observer_list_,
925 OnPropertyChanged(property, *(services.get())));
926 return;
928 FOR_EACH_OBSERVER(ShillPropertyChangedObserver,
929 observer_list_,
930 OnPropertyChanged(property, *value));
933 base::ListValue* FakeShillManagerClient::GetListProperty(
934 const std::string& property) {
935 base::ListValue* list_property = NULL;
936 if (!stub_properties_.GetListWithoutPathExpansion(
937 property, &list_property)) {
938 list_property = new base::ListValue;
939 stub_properties_.SetWithoutPathExpansion(property, list_property);
941 return list_property;
944 bool FakeShillManagerClient::TechnologyEnabled(const std::string& type) const {
945 if (type == shill::kTypeVPN)
946 return true; // VPN is always "enabled" since there is no associated device
947 if (type == shill::kTypeEthernetEap)
948 return true;
949 bool enabled = false;
950 const base::ListValue* technologies;
951 if (stub_properties_.GetListWithoutPathExpansion(
952 shill::kEnabledTechnologiesProperty, &technologies)) {
953 base::StringValue type_value(type);
954 if (technologies->Find(type_value) != technologies->end())
955 enabled = true;
957 return enabled;
960 void FakeShillManagerClient::SetTechnologyEnabled(
961 const std::string& type,
962 const base::Closure& callback,
963 bool enabled) {
964 base::ListValue* enabled_list =
965 GetListProperty(shill::kEnabledTechnologiesProperty);
966 if (enabled)
967 enabled_list->AppendIfNotPresent(new base::StringValue(type));
968 else
969 enabled_list->Remove(base::StringValue(type), NULL);
970 CallNotifyObserversPropertyChanged(
971 shill::kEnabledTechnologiesProperty);
972 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback);
973 // May affect available services.
974 SortManagerServices(true);
977 base::ListValue* FakeShillManagerClient::GetEnabledServiceList(
978 const std::string& property) const {
979 base::ListValue* new_service_list = new base::ListValue;
980 const base::ListValue* service_list;
981 if (stub_properties_.GetListWithoutPathExpansion(property, &service_list)) {
982 ShillServiceClient::TestInterface* service_client =
983 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface();
984 for (base::ListValue::const_iterator iter = service_list->begin();
985 iter != service_list->end(); ++iter) {
986 std::string service_path;
987 if (!(*iter)->GetAsString(&service_path))
988 continue;
989 const base::DictionaryValue* properties =
990 service_client->GetServiceProperties(service_path);
991 if (!properties) {
992 LOG(ERROR) << "Properties not found for service: " << service_path;
993 continue;
995 std::string type;
996 properties->GetString(shill::kTypeProperty, &type);
997 if (TechnologyEnabled(type))
998 new_service_list->Append((*iter)->DeepCopy());
1001 return new_service_list;
1004 void FakeShillManagerClient::ScanCompleted(const std::string& device_path,
1005 const base::Closure& callback) {
1006 if (!device_path.empty()) {
1007 DBusThreadManager::Get()->GetShillDeviceClient()->GetTestInterface()->
1008 SetDeviceProperty(device_path,
1009 shill::kScanningProperty,
1010 base::FundamentalValue(false));
1012 VLOG(2) << "ScanCompleted";
1013 CallNotifyObserversPropertyChanged(shill::kServiceCompleteListProperty);
1014 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback);
1017 void FakeShillManagerClient::ParseCommandLineSwitch() {
1018 // Default setup
1019 SetInitialNetworkState(shill::kTypeEthernet, shill::kStateOnline);
1020 SetInitialNetworkState(shill::kTypeWifi, shill::kStateOnline);
1021 SetInitialNetworkState(shill::kTypeCellular, shill::kStateIdle);
1022 SetInitialNetworkState(shill::kTypeVPN, shill::kStateIdle);
1024 // Parse additional options
1025 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
1026 if (!command_line->HasSwitch(switches::kShillStub))
1027 return;
1029 std::string option_str =
1030 command_line->GetSwitchValueASCII(switches::kShillStub);
1031 VLOG(1) << "Parsing command line:" << option_str;
1032 base::StringPairs string_pairs;
1033 base::SplitStringIntoKeyValuePairs(option_str, '=', ',', &string_pairs);
1034 for (base::StringPairs::iterator iter = string_pairs.begin();
1035 iter != string_pairs.end(); ++iter) {
1036 ParseOption((*iter).first, (*iter).second);
1040 bool FakeShillManagerClient::ParseOption(const std::string& arg0,
1041 const std::string& arg1) {
1042 VLOG(1) << "Parsing command line option: '" << arg0 << "=" << arg1 << "'";
1043 if ((arg0 == "clear" || arg0 == "reset") && arg1 == "1") {
1044 shill_initial_state_map_.clear();
1045 return true;
1046 } else if (arg0 == "interactive") {
1047 int seconds = 3;
1048 if (!arg1.empty())
1049 base::StringToInt(arg1, &seconds);
1050 interactive_delay_ = seconds;
1051 return true;
1052 } else if (arg0 == "sim_lock") {
1053 bool locked = (arg1 == "1") ? true : false;
1054 base::DictionaryValue* simlock_dict = new base::DictionaryValue;
1055 simlock_dict->Set(shill::kSIMLockEnabledProperty,
1056 new base::FundamentalValue(locked));
1057 std::string lock_type = shill::kSIMLockPin;
1058 simlock_dict->SetString(shill::kSIMLockTypeProperty, lock_type);
1059 simlock_dict->SetInteger(shill::kSIMLockRetriesLeftProperty, 5);
1061 shill_device_property_map_[shill::kTypeCellular]
1062 [shill::kSIMLockStatusProperty] = simlock_dict;
1063 shill_device_property_map_
1064 [shill::kTypeCellular][shill::kTechnologyFamilyProperty] =
1065 new base::StringValue(shill::kNetworkTechnologyGsm);
1066 return true;
1067 } else if (arg0 == "tdls_busy") {
1068 if (!arg1.empty())
1069 base::StringToInt(arg1, &s_tdls_busy_count);
1070 else
1071 s_tdls_busy_count = 1;
1072 return true;
1073 } else if (arg0 == "roaming") {
1074 // "home", "roaming", or "required"
1075 roaming_state_ = arg1;
1076 return true;
1078 return SetInitialNetworkState(arg0, arg1);
1081 bool FakeShillManagerClient::SetInitialNetworkState(std::string type_arg,
1082 std::string state_arg) {
1083 int state_arg_as_int = -1;
1084 base::StringToInt(state_arg, &state_arg_as_int);
1086 std::string state;
1087 if (state_arg.empty() || state_arg == "1" || state_arg == "on" ||
1088 state_arg == "enabled" || state_arg == "connected" ||
1089 state_arg == "online") {
1090 // Enabled and connected (default value)
1091 state = shill::kStateOnline;
1092 } else if (state_arg == "0" || state_arg == "off" ||
1093 state_arg == "inactive" || state_arg == shill::kStateIdle) {
1094 // Technology enabled, services are created but are not connected.
1095 state = shill::kStateIdle;
1096 } else if (type_arg == shill::kTypeWifi && state_arg_as_int > 1) {
1097 // Enabled and connected, add extra wifi networks.
1098 state = shill::kStateOnline;
1099 s_extra_wifi_networks = state_arg_as_int - 1;
1100 } else if (state_arg == "disabled" || state_arg == "disconnect") {
1101 // Technology disabled but available, services created but not connected.
1102 state = kNetworkDisabled;
1103 } else if (state_arg == "none" || state_arg == "offline") {
1104 // Technology not available, do not create services.
1105 state = kTechnologyUnavailable;
1106 } else if (state_arg == "portal") {
1107 // Technology is enabled, a service is connected and in Portal state.
1108 state = shill::kStatePortal;
1109 } else if (state_arg == "active" || state_arg == "activated") {
1110 // Technology is enabled, a service is connected and Activated.
1111 state = kNetworkActivated;
1112 } else if (type_arg == shill::kTypeCellular &&
1113 IsCellularTechnology(state_arg)) {
1114 state = shill::kStateOnline;
1115 cellular_technology_ = state_arg;
1116 } else if (type_arg == shill::kTypeCellular && state_arg == "LTEAdvanced") {
1117 // Special case, Shill name contains a ' '.
1118 state = shill::kStateOnline;
1119 cellular_technology_ = shill::kNetworkTechnologyLteAdvanced;
1120 } else {
1121 LOG(ERROR) << "Unrecognized initial state: " << type_arg << "="
1122 << state_arg;
1123 return false;
1126 // Special cases
1127 if (type_arg == "wireless") {
1128 shill_initial_state_map_[shill::kTypeWifi] = state;
1129 shill_initial_state_map_[shill::kTypeCellular] = state;
1130 return true;
1132 // Convenience synonyms.
1133 if (type_arg == "eth")
1134 type_arg = shill::kTypeEthernet;
1136 if (type_arg != shill::kTypeEthernet &&
1137 type_arg != shill::kTypeWifi &&
1138 type_arg != shill::kTypeCellular &&
1139 type_arg != shill::kTypeWimax &&
1140 type_arg != shill::kTypeVPN) {
1141 LOG(WARNING) << "Unrecognized Shill network type: " << type_arg;
1142 return false;
1145 // Unconnected or disabled ethernet is the same as unavailable.
1146 if (type_arg == shill::kTypeEthernet &&
1147 (state == shill::kStateIdle || state == kNetworkDisabled)) {
1148 state = kTechnologyUnavailable;
1151 shill_initial_state_map_[type_arg] = state;
1152 return true;
1155 std::string FakeShillManagerClient::GetInitialStateForType(
1156 const std::string& type,
1157 bool* enabled) {
1158 std::map<std::string, std::string>::const_iterator iter =
1159 shill_initial_state_map_.find(type);
1160 if (iter == shill_initial_state_map_.end()) {
1161 *enabled = false;
1162 return kTechnologyUnavailable;
1164 std::string state = iter->second;
1165 if (state == kNetworkDisabled) {
1166 *enabled = false;
1167 return shill::kStateIdle;
1169 *enabled = true;
1170 if ((state == shill::kStatePortal && type != shill::kTypeWifi) ||
1171 (state == kNetworkActivated && type != shill::kTypeCellular)) {
1172 LOG(WARNING) << "Invalid state: " << state << " for " << type;
1173 return shill::kStateIdle;
1175 return state;
1178 } // namespace chromeos