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"
8 #include "base/command_line.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/string_split.h"
12 #include "base/strings/string_util.h"
13 #include "base/values.h"
14 #include "chromeos/chromeos_switches.h"
15 #include "chromeos/dbus/dbus_thread_manager.h"
16 #include "chromeos/dbus/shill_device_client.h"
17 #include "chromeos/dbus/shill_ipconfig_client.h"
18 #include "chromeos/dbus/shill_profile_client.h"
19 #include "chromeos/dbus/shill_property_changed_observer.h"
20 #include "chromeos/dbus/shill_service_client.h"
22 #include "dbus/message.h"
23 #include "dbus/object_path.h"
24 #include "dbus/values_util.h"
25 #include "third_party/cros_system_api/dbus/service_constants.h"
31 // Used to compare values for finding entries to erase in a ListValue.
32 // (ListValue only implements a const_iterator version of Find).
34 explicit ValueEquals(const base::Value* first) : first_(first) {}
35 bool operator()(const base::Value* second) const {
36 return first_->Equals(second);
38 const base::Value* first_;
41 // Appends string entries from |service_list_in| whose entries in ServiceClient
42 // have Type |match_type| to either an active list or an inactive list
43 // based on the entry's State.
44 void AppendServicesForType(
45 const base::ListValue* service_list_in,
46 const char* match_type,
47 std::vector<std::string>* active_service_list_out,
48 std::vector<std::string>* inactive_service_list_out) {
49 ShillServiceClient::TestInterface* service_client =
50 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface();
51 for (base::ListValue::const_iterator iter = service_list_in->begin();
52 iter != service_list_in->end(); ++iter) {
53 std::string service_path;
54 if (!(*iter)->GetAsString(&service_path))
56 const base::DictionaryValue* properties =
57 service_client->GetServiceProperties(service_path);
59 LOG(ERROR) << "Properties not found for service: " << service_path;
63 properties->GetString(shill::kTypeProperty, &type);
64 if (type != match_type)
67 properties->GetString(shill::kStateProperty, &state);
68 if (state == shill::kStateOnline ||
69 state == shill::kStateAssociation ||
70 state == shill::kStateConfiguration ||
71 state == shill::kStatePortal ||
72 state == shill::kStateReady) {
73 active_service_list_out->push_back(service_path);
75 inactive_service_list_out->push_back(service_path);
80 void LogErrorCallback(const std::string& error_name,
81 const std::string& error_message) {
82 LOG(ERROR) << error_name << ": " << error_message;
85 bool IsConnectedState(const std::string& state) {
86 return state == shill::kStateOnline || state == shill::kStatePortal ||
87 state == shill::kStateReady;
90 void UpdatePortaledWifiState(const std::string& service_path) {
91 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface()
92 ->SetServiceProperty(service_path,
93 shill::kStateProperty,
94 base::StringValue(shill::kStatePortal));
97 const char* kTechnologyUnavailable = "unavailable";
98 const char* kNetworkActivated = "activated";
99 const char* kNetworkDisabled = "disabled";
103 FakeShillManagerClient::FakeShillManagerClient()
104 : interactive_delay_(0),
105 weak_ptr_factory_(this) {
106 ParseCommandLineSwitch();
109 FakeShillManagerClient::~FakeShillManagerClient() {}
111 // ShillManagerClient overrides.
113 void FakeShillManagerClient::Init(dbus::Bus* bus) {}
115 void FakeShillManagerClient::AddPropertyChangedObserver(
116 ShillPropertyChangedObserver* observer) {
117 observer_list_.AddObserver(observer);
120 void FakeShillManagerClient::RemovePropertyChangedObserver(
121 ShillPropertyChangedObserver* observer) {
122 observer_list_.RemoveObserver(observer);
125 void FakeShillManagerClient::GetProperties(
126 const DictionaryValueCallback& callback) {
127 base::MessageLoop::current()->PostTask(
128 FROM_HERE, base::Bind(
129 &FakeShillManagerClient::PassStubProperties,
130 weak_ptr_factory_.GetWeakPtr(),
134 void FakeShillManagerClient::GetNetworksForGeolocation(
135 const DictionaryValueCallback& callback) {
136 base::MessageLoop::current()->PostTask(
137 FROM_HERE, base::Bind(
138 &FakeShillManagerClient::PassStubGeoNetworks,
139 weak_ptr_factory_.GetWeakPtr(),
143 void FakeShillManagerClient::SetProperty(const std::string& name,
144 const base::Value& value,
145 const base::Closure& callback,
146 const ErrorCallback& error_callback) {
147 stub_properties_.SetWithoutPathExpansion(name, value.DeepCopy());
148 CallNotifyObserversPropertyChanged(name);
149 base::MessageLoop::current()->PostTask(FROM_HERE, callback);
152 void FakeShillManagerClient::RequestScan(const std::string& type,
153 const base::Closure& callback,
154 const ErrorCallback& error_callback) {
155 // For Stub purposes, default to a Wifi scan.
156 std::string device_type = shill::kTypeWifi;
159 ShillDeviceClient::TestInterface* device_client =
160 DBusThreadManager::Get()->GetShillDeviceClient()->GetTestInterface();
161 std::string device_path = device_client->GetDevicePathForType(device_type);
162 if (!device_path.empty()) {
163 device_client->SetDeviceProperty(
164 device_path, shill::kScanningProperty, base::FundamentalValue(true));
166 base::MessageLoop::current()->PostDelayedTask(
168 base::Bind(&FakeShillManagerClient::ScanCompleted,
169 weak_ptr_factory_.GetWeakPtr(),
172 base::TimeDelta::FromSeconds(interactive_delay_));
175 void FakeShillManagerClient::EnableTechnology(
176 const std::string& type,
177 const base::Closure& callback,
178 const ErrorCallback& error_callback) {
179 base::ListValue* enabled_list = NULL;
180 if (!stub_properties_.GetListWithoutPathExpansion(
181 shill::kAvailableTechnologiesProperty, &enabled_list)) {
182 base::MessageLoop::current()->PostTask(FROM_HERE, callback);
183 base::MessageLoop::current()->PostTask(
185 base::Bind(error_callback, "StubError", "Property not found"));
188 base::MessageLoop::current()->PostDelayedTask(
190 base::Bind(&FakeShillManagerClient::SetTechnologyEnabled,
191 weak_ptr_factory_.GetWeakPtr(),
195 base::TimeDelta::FromSeconds(interactive_delay_));
198 void FakeShillManagerClient::DisableTechnology(
199 const std::string& type,
200 const base::Closure& callback,
201 const ErrorCallback& error_callback) {
202 base::ListValue* enabled_list = NULL;
203 if (!stub_properties_.GetListWithoutPathExpansion(
204 shill::kAvailableTechnologiesProperty, &enabled_list)) {
205 base::MessageLoop::current()->PostTask(
207 base::Bind(error_callback, "StubError", "Property not found"));
210 base::MessageLoop::current()->PostDelayedTask(
212 base::Bind(&FakeShillManagerClient::SetTechnologyEnabled,
213 weak_ptr_factory_.GetWeakPtr(),
217 base::TimeDelta::FromSeconds(interactive_delay_));
220 void FakeShillManagerClient::ConfigureService(
221 const base::DictionaryValue& properties,
222 const ObjectPathCallback& callback,
223 const ErrorCallback& error_callback) {
224 ShillServiceClient::TestInterface* service_client =
225 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface();
229 if (!properties.GetString(shill::kGuidProperty, &guid) ||
230 !properties.GetString(shill::kTypeProperty, &type)) {
231 LOG(ERROR) << "ConfigureService requires GUID and Type to be defined";
232 // If the properties aren't filled out completely, then just return an empty
234 base::MessageLoop::current()->PostTask(
235 FROM_HERE, base::Bind(callback, dbus::ObjectPath()));
239 // For the purposes of this stub, we're going to assume that the GUID property
240 // is set to the service path because we don't want to re-implement Shill's
241 // property matching magic here.
242 std::string service_path = guid;
244 std::string ipconfig_path;
245 properties.GetString(shill::kIPConfigProperty, &ipconfig_path);
247 // Merge the new properties with existing properties, if any.
248 const base::DictionaryValue* existing_properties =
249 service_client->GetServiceProperties(service_path);
250 if (!existing_properties) {
251 // Add a new service to the service client stub because none exists, yet.
252 // This calls AddManagerService.
253 service_client->AddServiceWithIPConfig(service_path, guid, type,
254 shill::kStateIdle, ipconfig_path,
257 existing_properties = service_client->GetServiceProperties(service_path);
260 scoped_ptr<base::DictionaryValue> merged_properties(
261 existing_properties->DeepCopy());
262 merged_properties->MergeDictionary(&properties);
264 // Now set all the properties.
265 for (base::DictionaryValue::Iterator iter(*merged_properties);
266 !iter.IsAtEnd(); iter.Advance()) {
267 service_client->SetServiceProperty(service_path, iter.key(), iter.value());
270 // If the Profile property is set, add it to ProfileClient.
271 std::string profile_path;
272 merged_properties->GetStringWithoutPathExpansion(shill::kProfileProperty,
274 if (!profile_path.empty()) {
275 DBusThreadManager::Get()->GetShillProfileClient()->GetTestInterface()->
276 AddService(profile_path, service_path);
279 base::MessageLoop::current()->PostTask(
280 FROM_HERE, base::Bind(callback, dbus::ObjectPath(service_path)));
283 void FakeShillManagerClient::ConfigureServiceForProfile(
284 const dbus::ObjectPath& profile_path,
285 const base::DictionaryValue& properties,
286 const ObjectPathCallback& callback,
287 const ErrorCallback& error_callback) {
288 std::string profile_property;
289 properties.GetStringWithoutPathExpansion(shill::kProfileProperty,
291 CHECK(profile_property == profile_path.value());
292 ConfigureService(properties, callback, error_callback);
296 void FakeShillManagerClient::GetService(
297 const base::DictionaryValue& properties,
298 const ObjectPathCallback& callback,
299 const ErrorCallback& error_callback) {
300 base::MessageLoop::current()->PostTask(
301 FROM_HERE, base::Bind(callback, dbus::ObjectPath()));
304 void FakeShillManagerClient::VerifyDestination(
305 const VerificationProperties& properties,
306 const BooleanCallback& callback,
307 const ErrorCallback& error_callback) {
308 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback, true));
311 void FakeShillManagerClient::VerifyAndEncryptCredentials(
312 const VerificationProperties& properties,
313 const std::string& service_path,
314 const StringCallback& callback,
315 const ErrorCallback& error_callback) {
316 base::MessageLoop::current()->PostTask(
317 FROM_HERE, base::Bind(callback, "encrypted_credentials"));
320 void FakeShillManagerClient::VerifyAndEncryptData(
321 const VerificationProperties& properties,
322 const std::string& data,
323 const StringCallback& callback,
324 const ErrorCallback& error_callback) {
325 base::MessageLoop::current()->PostTask(
326 FROM_HERE, base::Bind(callback, "encrypted_data"));
329 void FakeShillManagerClient::ConnectToBestServices(
330 const base::Closure& callback,
331 const ErrorCallback& error_callback) {
334 ShillManagerClient::TestInterface* FakeShillManagerClient::GetTestInterface() {
338 // ShillManagerClient::TestInterface overrides.
340 void FakeShillManagerClient::AddDevice(const std::string& device_path) {
341 if (GetListProperty(shill::kDevicesProperty)->AppendIfNotPresent(
342 base::Value::CreateStringValue(device_path))) {
343 CallNotifyObserversPropertyChanged(shill::kDevicesProperty);
347 void FakeShillManagerClient::RemoveDevice(const std::string& device_path) {
348 base::StringValue device_path_value(device_path);
349 if (GetListProperty(shill::kDevicesProperty)->Remove(
350 device_path_value, NULL)) {
351 CallNotifyObserversPropertyChanged(shill::kDevicesProperty);
355 void FakeShillManagerClient::ClearDevices() {
356 GetListProperty(shill::kDevicesProperty)->Clear();
357 CallNotifyObserversPropertyChanged(shill::kDevicesProperty);
360 void FakeShillManagerClient::AddTechnology(const std::string& type,
362 if (GetListProperty(shill::kAvailableTechnologiesProperty)->
363 AppendIfNotPresent(base::Value::CreateStringValue(type))) {
364 CallNotifyObserversPropertyChanged(
365 shill::kAvailableTechnologiesProperty);
368 GetListProperty(shill::kEnabledTechnologiesProperty)->
369 AppendIfNotPresent(base::Value::CreateStringValue(type))) {
370 CallNotifyObserversPropertyChanged(
371 shill::kEnabledTechnologiesProperty);
375 void FakeShillManagerClient::RemoveTechnology(const std::string& type) {
376 base::StringValue type_value(type);
377 if (GetListProperty(shill::kAvailableTechnologiesProperty)->Remove(
379 CallNotifyObserversPropertyChanged(
380 shill::kAvailableTechnologiesProperty);
382 if (GetListProperty(shill::kEnabledTechnologiesProperty)->Remove(
384 CallNotifyObserversPropertyChanged(
385 shill::kEnabledTechnologiesProperty);
389 void FakeShillManagerClient::SetTechnologyInitializing(const std::string& type,
392 if (GetListProperty(shill::kUninitializedTechnologiesProperty)->
393 AppendIfNotPresent(base::Value::CreateStringValue(type))) {
394 CallNotifyObserversPropertyChanged(
395 shill::kUninitializedTechnologiesProperty);
398 if (GetListProperty(shill::kUninitializedTechnologiesProperty)->Remove(
399 base::StringValue(type), NULL)) {
400 CallNotifyObserversPropertyChanged(
401 shill::kUninitializedTechnologiesProperty);
406 void FakeShillManagerClient::AddGeoNetwork(
407 const std::string& technology,
408 const base::DictionaryValue& network) {
409 base::ListValue* list_value = NULL;
410 if (!stub_geo_networks_.GetListWithoutPathExpansion(technology,
412 list_value = new base::ListValue;
413 stub_geo_networks_.SetWithoutPathExpansion(technology, list_value);
415 list_value->Append(network.DeepCopy());
418 void FakeShillManagerClient::AddProfile(const std::string& profile_path) {
419 const char* key = shill::kProfilesProperty;
420 if (GetListProperty(key)
421 ->AppendIfNotPresent(new base::StringValue(profile_path))) {
422 CallNotifyObserversPropertyChanged(key);
426 void FakeShillManagerClient::ClearProperties() {
427 stub_properties_.Clear();
430 void FakeShillManagerClient::SetManagerProperty(const std::string& key,
431 const base::Value& value) {
432 SetProperty(key, value,
433 base::Bind(&base::DoNothing), base::Bind(&LogErrorCallback));
436 void FakeShillManagerClient::AddManagerService(const std::string& service_path,
437 bool add_to_visible_list,
438 bool add_to_watch_list) {
439 // Always add to ServiceCompleteListProperty.
440 GetListProperty(shill::kServiceCompleteListProperty)->AppendIfNotPresent(
441 base::Value::CreateStringValue(service_path));
442 // If visible, add to Services and notify if new.
443 if (add_to_visible_list &&
444 GetListProperty(shill::kServicesProperty)->AppendIfNotPresent(
445 base::Value::CreateStringValue(service_path))) {
446 CallNotifyObserversPropertyChanged(shill::kServicesProperty);
448 if (add_to_watch_list)
449 AddServiceToWatchList(service_path);
452 void FakeShillManagerClient::RemoveManagerService(
453 const std::string& service_path) {
454 base::StringValue service_path_value(service_path);
455 if (GetListProperty(shill::kServicesProperty)->Remove(
456 service_path_value, NULL)) {
457 CallNotifyObserversPropertyChanged(shill::kServicesProperty);
459 GetListProperty(shill::kServiceCompleteListProperty)->Remove(
460 service_path_value, NULL);
461 if (GetListProperty(shill::kServiceWatchListProperty)->Remove(
462 service_path_value, NULL)) {
463 CallNotifyObserversPropertyChanged(
464 shill::kServiceWatchListProperty);
468 void FakeShillManagerClient::ClearManagerServices() {
469 GetListProperty(shill::kServicesProperty)->Clear();
470 GetListProperty(shill::kServiceCompleteListProperty)->Clear();
471 GetListProperty(shill::kServiceWatchListProperty)->Clear();
472 CallNotifyObserversPropertyChanged(shill::kServicesProperty);
473 CallNotifyObserversPropertyChanged(shill::kServiceWatchListProperty);
476 void FakeShillManagerClient::ServiceStateChanged(
477 const std::string& service_path,
478 const std::string& state) {
479 if (service_path == default_service_ && !IsConnectedState(state)) {
480 // Default service is no longer connected; clear.
481 default_service_.clear();
482 base::StringValue default_service_value(default_service_);
483 SetManagerProperty(shill::kDefaultServiceProperty, default_service_value);
487 void FakeShillManagerClient::SortManagerServices() {
488 static const char* ordered_types[] = {
489 shill::kTypeEthernet,
491 shill::kTypeCellular,
495 base::ListValue* service_list = GetListProperty(shill::kServicesProperty);
496 if (!service_list || service_list->empty()) {
497 if (!default_service_.empty()) {
498 default_service_.clear();
499 base::StringValue empty_value("");
500 SetManagerProperty(shill::kDefaultServiceProperty, empty_value);
504 std::vector<std::string> active_services;
505 std::vector<std::string> inactive_services;
506 for (size_t i = 0; i < arraysize(ordered_types); ++i) {
507 AppendServicesForType(service_list, ordered_types[i],
508 &active_services, &inactive_services);
510 service_list->Clear();
511 for (size_t i = 0; i < active_services.size(); ++i)
512 service_list->AppendString(active_services[i]);
513 for (size_t i = 0; i < inactive_services.size(); ++i)
514 service_list->AppendString(inactive_services[i]);
516 CallNotifyObserversPropertyChanged(shill::kServicesProperty);
518 // Set the first active service as the Default service.
519 std::string new_default_service;
520 if (!active_services.empty()) {
521 ShillServiceClient::TestInterface* service_client =
522 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface();
523 std::string service_path = active_services[0];
524 const base::DictionaryValue* properties =
525 service_client->GetServiceProperties(service_path);
527 LOG(ERROR) << "Properties not found for service: " << service_path;
530 properties->GetString(shill::kStateProperty, &state);
531 if (IsConnectedState(state))
532 new_default_service = service_path;
535 if (default_service_ != new_default_service) {
536 default_service_ = new_default_service;
537 base::StringValue default_service_value(default_service_);
538 SetManagerProperty(shill::kDefaultServiceProperty, default_service_value);
543 int FakeShillManagerClient::GetInteractiveDelay() const {
544 return interactive_delay_;
547 void FakeShillManagerClient::SetupDefaultEnvironment() {
548 DBusThreadManager* dbus_manager = DBusThreadManager::Get();
549 ShillServiceClient::TestInterface* services =
550 dbus_manager->GetShillServiceClient()->GetTestInterface();
552 ShillProfileClient::TestInterface* profiles =
553 dbus_manager->GetShillProfileClient()->GetTestInterface();
555 ShillDeviceClient::TestInterface* devices =
556 dbus_manager->GetShillDeviceClient()->GetTestInterface();
558 ShillIPConfigClient::TestInterface* ip_configs =
559 dbus_manager->GetShillIPConfigClient()->GetTestInterface();
562 const std::string shared_profile = ShillProfileClient::GetSharedProfilePath();
563 profiles->AddProfile(shared_profile, std::string());
565 const bool add_to_visible = true;
566 const bool add_to_watchlist = true;
572 base::DictionaryValue ipconfig_v4_dictionary;
573 ipconfig_v4_dictionary.SetStringWithoutPathExpansion(
574 shill::kAddressProperty, "0.0.0.0");
575 ipconfig_v4_dictionary.SetStringWithoutPathExpansion(
576 shill::kGatewayProperty, "0.0.0.1");
577 ipconfig_v4_dictionary.SetIntegerWithoutPathExpansion(
578 shill::kPrefixlenProperty, 0);
579 ipconfig_v4_dictionary.SetStringWithoutPathExpansion(
580 shill::kMethodProperty, shill::kTypeIPv4);
581 ip_configs->AddIPConfig("ipconfig_v4_path", ipconfig_v4_dictionary);
582 base::DictionaryValue ipconfig_v6_dictionary;
583 ipconfig_v6_dictionary.SetStringWithoutPathExpansion(
584 shill::kAddressProperty, "0:0:0:0:0:0:0:0");
585 ipconfig_v6_dictionary.SetStringWithoutPathExpansion(
586 shill::kMethodProperty, shill::kTypeIPv6);
587 ip_configs->AddIPConfig("ipconfig_v6_path", ipconfig_v6_dictionary);
590 state = GetInitialStateForType(shill::kTypeEthernet, &enabled);
591 if (state == shill::kStateOnline) {
592 AddTechnology(shill::kTypeEthernet, enabled);
594 "/device/eth1", shill::kTypeEthernet, "stub_eth_device1");
595 base::ListValue eth_ip_configs;
596 eth_ip_configs.AppendString("ipconfig_v4_path");
597 eth_ip_configs.AppendString("ipconfig_v6_path");
598 devices->SetDeviceProperty("/device/eth1",
599 shill::kIPConfigsProperty,
601 services->AddService("eth1", "eth1",
602 shill::kTypeEthernet,
604 add_to_visible, add_to_watchlist);
605 profiles->AddService(shared_profile, "eth1");
609 state = GetInitialStateForType(shill::kTypeWifi, &enabled);
610 if (state != kTechnologyUnavailable) {
611 bool portaled = false;
612 if (state == shill::kStatePortal) {
614 state = shill::kStateIdle;
616 AddTechnology(shill::kTypeWifi, enabled);
617 devices->AddDevice("/device/wifi1", shill::kTypeWifi, "stub_wifi_device1");
618 base::ListValue wifi_ip_configs;
619 wifi_ip_configs.AppendString("ipconfig_v4_path");
620 wifi_ip_configs.AppendString("ipconfig_v6_path");
621 devices->SetDeviceProperty("/device/wifi1",
622 shill::kIPConfigsProperty,
625 services->AddService("wifi1",
629 add_to_visible, add_to_watchlist);
630 services->SetServiceProperty("wifi1",
631 shill::kSecurityProperty,
632 base::StringValue(shill::kSecurityWep));
633 profiles->AddService(shared_profile, "wifi1");
635 services->AddService("wifi2",
639 add_to_visible, add_to_watchlist);
640 services->SetServiceProperty("wifi2",
641 shill::kSecurityProperty,
642 base::StringValue(shill::kSecurityPsk));
644 base::FundamentalValue strength_value(80);
645 services->SetServiceProperty(
646 "wifi2", shill::kSignalStrengthProperty, strength_value);
647 profiles->AddService(shared_profile, "wifi2");
650 const std::string kPortaledWifiPath = "portaled_wifi";
651 services->AddService(kPortaledWifiPath,
655 add_to_visible, add_to_watchlist);
656 services->SetServiceProperty(kPortaledWifiPath,
657 shill::kSecurityProperty,
658 base::StringValue(shill::kSecurityNone));
659 services->SetConnectBehavior(kPortaledWifiPath,
660 base::Bind(&UpdatePortaledWifiState,
662 services->SetServiceProperty(kPortaledWifiPath,
663 shill::kConnectableProperty,
664 base::FundamentalValue(true));
665 profiles->AddService(shared_profile, kPortaledWifiPath);
670 state = GetInitialStateForType(shill::kTypeWimax, &enabled);
671 if (state != kTechnologyUnavailable) {
672 AddTechnology(shill::kTypeWimax, enabled);
674 "/device/wimax1", shill::kTypeWimax, "stub_wimax_device1");
676 services->AddService("wimax1",
680 add_to_visible, add_to_watchlist);
681 services->SetServiceProperty(
682 "wimax1", shill::kConnectableProperty, base::FundamentalValue(true));
686 state = GetInitialStateForType(shill::kTypeCellular, &enabled);
687 if (state != kTechnologyUnavailable) {
688 bool activated = false;
689 if (state == kNetworkActivated) {
691 state = shill::kStateIdle;
693 AddTechnology(shill::kTypeCellular, enabled);
695 "/device/cellular1", shill::kTypeCellular, "stub_cellular_device1");
696 devices->SetDeviceProperty("/device/cellular1",
697 shill::kCarrierProperty,
698 base::StringValue(shill::kCarrierSprint));
700 services->AddService("cellular1",
702 shill::kTypeCellular,
704 add_to_visible, add_to_watchlist);
705 base::StringValue technology_value(shill::kNetworkTechnologyGsm);
706 services->SetServiceProperty(
707 "cellular1", shill::kNetworkTechnologyProperty, technology_value);
710 services->SetServiceProperty(
712 shill::kActivationStateProperty,
713 base::StringValue(shill::kActivationStateActivated));
714 services->SetServiceProperty("cellular1",
715 shill::kConnectableProperty,
716 base::FundamentalValue(true));
718 services->SetServiceProperty(
720 shill::kActivationStateProperty,
721 base::StringValue(shill::kActivationStateNotActivated));
724 services->SetServiceProperty("cellular1",
725 shill::kRoamingStateProperty,
726 base::StringValue(shill::kRoamingStateHome));
730 state = GetInitialStateForType(shill::kTypeVPN, &enabled);
731 if (state != kTechnologyUnavailable) {
732 // Set the "Provider" dictionary properties. Note: when setting these in
733 // Shill, "Provider.Type", etc keys are used, but when reading the values
734 // "Provider" . "Type", etc keys are used. Here we are setting the values
735 // that will be read (by the UI, tests, etc).
736 base::DictionaryValue provider_properties;
737 provider_properties.SetString(shill::kTypeProperty,
738 shill::kProviderOpenVpn);
739 provider_properties.SetString(shill::kHostProperty, "vpn_host");
741 services->AddService("vpn1",
745 add_to_visible, add_to_watchlist);
746 services->SetServiceProperty(
747 "vpn1", shill::kProviderProperty, provider_properties);
748 profiles->AddService(shared_profile, "vpn1");
750 services->AddService("vpn2",
754 add_to_visible, add_to_watchlist);
755 services->SetServiceProperty(
756 "vpn2", shill::kProviderProperty, provider_properties);
759 SortManagerServices();
764 void FakeShillManagerClient::AddServiceToWatchList(
765 const std::string& service_path) {
766 // Remove and insert the service, moving it to the front of the watch list.
767 GetListProperty(shill::kServiceWatchListProperty)->Remove(
768 base::StringValue(service_path), NULL);
769 GetListProperty(shill::kServiceWatchListProperty)->Insert(
770 0, base::Value::CreateStringValue(service_path));
771 CallNotifyObserversPropertyChanged(
772 shill::kServiceWatchListProperty);
775 void FakeShillManagerClient::PassStubProperties(
776 const DictionaryValueCallback& callback) const {
777 scoped_ptr<base::DictionaryValue> stub_properties(
778 stub_properties_.DeepCopy());
779 // Remove disabled services from the list.
780 stub_properties->SetWithoutPathExpansion(
781 shill::kServicesProperty,
782 GetEnabledServiceList(shill::kServicesProperty));
783 stub_properties->SetWithoutPathExpansion(
784 shill::kServiceWatchListProperty,
785 GetEnabledServiceList(shill::kServiceWatchListProperty));
786 callback.Run(DBUS_METHOD_CALL_SUCCESS, *stub_properties);
789 void FakeShillManagerClient::PassStubGeoNetworks(
790 const DictionaryValueCallback& callback) const {
791 callback.Run(DBUS_METHOD_CALL_SUCCESS, stub_geo_networks_);
794 void FakeShillManagerClient::CallNotifyObserversPropertyChanged(
795 const std::string& property) {
796 // Avoid unnecessary delayed task if we have no observers (e.g. during
798 if (!observer_list_.might_have_observers())
800 base::MessageLoop::current()->PostTask(
802 base::Bind(&FakeShillManagerClient::NotifyObserversPropertyChanged,
803 weak_ptr_factory_.GetWeakPtr(),
807 void FakeShillManagerClient::NotifyObserversPropertyChanged(
808 const std::string& property) {
809 if (property == shill::kServicesProperty ||
810 property == shill::kServiceWatchListProperty) {
811 scoped_ptr<base::ListValue> services(GetEnabledServiceList(property));
812 FOR_EACH_OBSERVER(ShillPropertyChangedObserver,
814 OnPropertyChanged(property, *(services.get())));
817 if (property == shill::kDevicesProperty) {
818 base::ListValue* devices = NULL;
819 if (stub_properties_.GetListWithoutPathExpansion(
820 shill::kDevicesProperty, &devices)) {
821 FOR_EACH_OBSERVER(ShillPropertyChangedObserver,
823 OnPropertyChanged(property, *devices));
827 base::Value* value = NULL;
828 if (!stub_properties_.GetWithoutPathExpansion(property, &value)) {
829 LOG(ERROR) << "Notify for unknown property: " << property;
832 FOR_EACH_OBSERVER(ShillPropertyChangedObserver,
834 OnPropertyChanged(property, *value));
837 base::ListValue* FakeShillManagerClient::GetListProperty(
838 const std::string& property) {
839 base::ListValue* list_property = NULL;
840 if (!stub_properties_.GetListWithoutPathExpansion(
841 property, &list_property)) {
842 list_property = new base::ListValue;
843 stub_properties_.SetWithoutPathExpansion(property, list_property);
845 return list_property;
848 bool FakeShillManagerClient::TechnologyEnabled(const std::string& type) const {
849 if (type == shill::kTypeVPN)
850 return true; // VPN is always "enabled" since there is no associated device
851 bool enabled = false;
852 const base::ListValue* technologies;
853 if (stub_properties_.GetListWithoutPathExpansion(
854 shill::kEnabledTechnologiesProperty, &technologies)) {
855 base::StringValue type_value(type);
856 if (technologies->Find(type_value) != technologies->end())
862 void FakeShillManagerClient::SetTechnologyEnabled(
863 const std::string& type,
864 const base::Closure& callback,
866 base::ListValue* enabled_list =
867 GetListProperty(shill::kEnabledTechnologiesProperty);
869 enabled_list->AppendIfNotPresent(new base::StringValue(type));
871 enabled_list->Remove(base::StringValue(type), NULL);
872 CallNotifyObserversPropertyChanged(
873 shill::kEnabledTechnologiesProperty);
874 base::MessageLoop::current()->PostTask(FROM_HERE, callback);
875 // May affect available services
876 CallNotifyObserversPropertyChanged(shill::kServicesProperty);
877 CallNotifyObserversPropertyChanged(shill::kServiceWatchListProperty);
880 base::ListValue* FakeShillManagerClient::GetEnabledServiceList(
881 const std::string& property) const {
882 base::ListValue* new_service_list = new base::ListValue;
883 const base::ListValue* service_list;
884 if (stub_properties_.GetListWithoutPathExpansion(property, &service_list)) {
885 ShillServiceClient::TestInterface* service_client =
886 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface();
887 for (base::ListValue::const_iterator iter = service_list->begin();
888 iter != service_list->end(); ++iter) {
889 std::string service_path;
890 if (!(*iter)->GetAsString(&service_path))
892 const base::DictionaryValue* properties =
893 service_client->GetServiceProperties(service_path);
895 LOG(ERROR) << "Properties not found for service: " << service_path;
899 properties->GetString(shill::kNameProperty, &name);
901 properties->GetString(shill::kTypeProperty, &type);
902 if (TechnologyEnabled(type))
903 new_service_list->Append((*iter)->DeepCopy());
906 return new_service_list;
909 void FakeShillManagerClient::ScanCompleted(const std::string& device_path,
910 const base::Closure& callback) {
911 if (!device_path.empty()) {
912 DBusThreadManager::Get()->GetShillDeviceClient()->GetTestInterface()->
913 SetDeviceProperty(device_path,
914 shill::kScanningProperty,
915 base::FundamentalValue(false));
917 CallNotifyObserversPropertyChanged(shill::kServicesProperty);
918 CallNotifyObserversPropertyChanged(shill::kServiceWatchListProperty);
919 base::MessageLoop::current()->PostTask(FROM_HERE, callback);
922 void FakeShillManagerClient::ParseCommandLineSwitch() {
923 CommandLine* command_line = CommandLine::ForCurrentProcess();
924 if (command_line->HasSwitch(switches::kShillStub)) {
925 std::string option_str =
926 command_line->GetSwitchValueASCII(switches::kShillStub);
927 base::StringPairs string_pairs;
928 base::SplitStringIntoKeyValuePairs(option_str, '=', ',', &string_pairs);
929 for (base::StringPairs::iterator iter = string_pairs.begin();
930 iter != string_pairs.end(); ++iter) {
931 ParseOption((*iter).first, (*iter).second);
936 SetInitialNetworkState(shill::kTypeEthernet, shill::kStateOnline);
937 SetInitialNetworkState(shill::kTypeWifi, shill::kStateOnline);
938 SetInitialNetworkState(shill::kTypeCellular, shill::kStateIdle);
939 SetInitialNetworkState(shill::kTypeVPN, shill::kStateIdle);
942 bool FakeShillManagerClient::ParseOption(const std::string& arg0,
943 const std::string& arg1) {
944 if (arg0 == "interactive") {
947 base::StringToInt(arg1, &seconds);
948 interactive_delay_ = seconds;
951 return SetInitialNetworkState(arg0, arg1);
954 bool FakeShillManagerClient::SetInitialNetworkState(std::string type_arg,
955 std::string state_arg) {
957 state_arg = StringToLowerASCII(state_arg);
958 if (state_arg.empty() || state_arg == "1" || state_arg == "on" ||
959 state_arg == "enabled" || state_arg == "connected" ||
960 state_arg == "online") {
961 // Enabled and connected (default value)
962 state = shill::kStateOnline;
963 } else if (state_arg == "0" || state_arg == "off" ||
964 state_arg == "inactive" || state_arg == shill::kStateIdle) {
965 // Technology enabled, services are created but are not connected.
966 state = shill::kStateIdle;
967 } else if (state_arg == "disabled" || state_arg == "disconnect") {
968 // Technology disabled but available, services created but not connected.
969 state = kNetworkDisabled;
970 } else if (state_arg == "none" || state_arg == "offline") {
971 // Technology not available, do not create services.
972 state = kTechnologyUnavailable;
973 } else if (state_arg == "portal") {
974 // Technology is enabled, a service is connected and in Portal state.
975 state = shill::kStatePortal;
976 } else if (state_arg == "active" || state_arg == "activated") {
977 // Technology is enabled, a service is connected and Activated.
978 state = kNetworkActivated;
980 LOG(ERROR) << "Unrecognized initial state: " << state_arg;
984 type_arg = StringToLowerASCII(type_arg);
986 if (type_arg == "wireless") {
987 shill_initial_state_map_[shill::kTypeWifi] = state;
988 shill_initial_state_map_[shill::kTypeCellular] = state;
991 // Convenience synonyms.
992 if (type_arg == "eth")
993 type_arg = shill::kTypeEthernet;
995 if (type_arg != shill::kTypeEthernet &&
996 type_arg != shill::kTypeWifi &&
997 type_arg != shill::kTypeCellular &&
998 type_arg != shill::kTypeWimax &&
999 type_arg != shill::kTypeVPN) {
1000 LOG(WARNING) << "Unrecognized Shill network type: " << type_arg;
1004 // Unconnected or disabled ethernet is the same as unavailable.
1005 if (type_arg == shill::kTypeEthernet &&
1006 (state == shill::kStateIdle || state == kNetworkDisabled)) {
1007 state = kTechnologyUnavailable;
1010 shill_initial_state_map_[type_arg] = state;
1014 std::string FakeShillManagerClient::GetInitialStateForType(
1015 const std::string& type,
1017 std::map<std::string, std::string>::const_iterator iter =
1018 shill_initial_state_map_.find(type);
1019 if (iter == shill_initial_state_map_.end()) {
1021 return kTechnologyUnavailable;
1023 std::string state = iter->second;
1024 if (state == kNetworkDisabled) {
1026 return shill::kStateIdle;
1029 if ((state == shill::kStatePortal && type != shill::kTypeWifi) ||
1030 (state == kNetworkActivated && type != shill::kTypeCellular)) {
1031 LOG(WARNING) << "Invalid state: " << state << " for " << type;
1032 return shill::kStateIdle;
1037 } // namespace chromeos