Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chromeos / dbus / fake_shill_manager_client.cc
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.
4
5 #include "chromeos/dbus/fake_shill_manager_client.h"
6
7 #include "base/bind.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"
21 #include "dbus/bus.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"
26
27 namespace chromeos {
28
29 namespace {
30
31 // Used to compare values for finding entries to erase in a ListValue.
32 // (ListValue only implements a const_iterator version of Find).
33 struct ValueEquals {
34   explicit ValueEquals(const base::Value* first) : first_(first) {}
35   bool operator()(const base::Value* second) const {
36     return first_->Equals(second);
37   }
38   const base::Value* first_;
39 };
40
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))
55       continue;
56     const base::DictionaryValue* properties =
57         service_client->GetServiceProperties(service_path);
58     if (!properties) {
59       LOG(ERROR) << "Properties not found for service: " << service_path;
60       continue;
61     }
62     std::string type;
63     properties->GetString(shill::kTypeProperty, &type);
64     if (type != match_type)
65       continue;
66     std::string state;
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);
74     } else {
75       inactive_service_list_out->push_back(service_path);
76     }
77   }
78 }
79
80 void LogErrorCallback(const std::string& error_name,
81                       const std::string& error_message) {
82   LOG(ERROR) << error_name << ": " << error_message;
83 }
84
85 bool IsConnectedState(const std::string& state) {
86   return state == shill::kStateOnline || state == shill::kStatePortal ||
87          state == shill::kStateReady;
88 }
89
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));
95 }
96
97 const char* kTechnologyUnavailable = "unavailable";
98 const char* kNetworkActivated = "activated";
99 const char* kNetworkDisabled = "disabled";
100
101 }  // namespace
102
103 FakeShillManagerClient::FakeShillManagerClient()
104     : interactive_delay_(0),
105       weak_ptr_factory_(this) {
106   ParseCommandLineSwitch();
107 }
108
109 FakeShillManagerClient::~FakeShillManagerClient() {}
110
111 // ShillManagerClient overrides.
112
113 void FakeShillManagerClient::Init(dbus::Bus* bus) {}
114
115 void FakeShillManagerClient::AddPropertyChangedObserver(
116     ShillPropertyChangedObserver* observer) {
117   observer_list_.AddObserver(observer);
118 }
119
120 void FakeShillManagerClient::RemovePropertyChangedObserver(
121     ShillPropertyChangedObserver* observer) {
122   observer_list_.RemoveObserver(observer);
123 }
124
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(),
131           callback));
132 }
133
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(),
140           callback));
141 }
142
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);
150 }
151
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;
157   if (!type.empty())
158     device_type = type;
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));
165   }
166   base::MessageLoop::current()->PostDelayedTask(
167       FROM_HERE,
168       base::Bind(&FakeShillManagerClient::ScanCompleted,
169                  weak_ptr_factory_.GetWeakPtr(),
170                  device_path,
171                  callback),
172       base::TimeDelta::FromSeconds(interactive_delay_));
173 }
174
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(
184         FROM_HERE,
185         base::Bind(error_callback, "StubError", "Property not found"));
186     return;
187   }
188   base::MessageLoop::current()->PostDelayedTask(
189       FROM_HERE,
190       base::Bind(&FakeShillManagerClient::SetTechnologyEnabled,
191                  weak_ptr_factory_.GetWeakPtr(),
192                  type,
193                  callback,
194                  true),
195       base::TimeDelta::FromSeconds(interactive_delay_));
196 }
197
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(
206         FROM_HERE,
207         base::Bind(error_callback, "StubError", "Property not found"));
208     return;
209   }
210   base::MessageLoop::current()->PostDelayedTask(
211       FROM_HERE,
212       base::Bind(&FakeShillManagerClient::SetTechnologyEnabled,
213                  weak_ptr_factory_.GetWeakPtr(),
214                  type,
215                  callback,
216                  false),
217       base::TimeDelta::FromSeconds(interactive_delay_));
218 }
219
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();
226
227   std::string guid;
228   std::string type;
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
233     // object path.
234     base::MessageLoop::current()->PostTask(
235         FROM_HERE, base::Bind(callback, dbus::ObjectPath()));
236     return;
237   }
238
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;
243
244   std::string ipconfig_path;
245   properties.GetString(shill::kIPConfigProperty, &ipconfig_path);
246
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,
255                                            true /* visible */,
256                                            true /* watch */);
257     existing_properties = service_client->GetServiceProperties(service_path);
258   }
259
260   scoped_ptr<base::DictionaryValue> merged_properties(
261       existing_properties->DeepCopy());
262   merged_properties->MergeDictionary(&properties);
263
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());
268   }
269
270   // If the Profile property is set, add it to ProfileClient.
271   std::string profile_path;
272   merged_properties->GetStringWithoutPathExpansion(shill::kProfileProperty,
273                                                    &profile_path);
274   if (!profile_path.empty()) {
275     DBusThreadManager::Get()->GetShillProfileClient()->GetTestInterface()->
276         AddService(profile_path, service_path);
277   }
278
279   base::MessageLoop::current()->PostTask(
280       FROM_HERE, base::Bind(callback, dbus::ObjectPath(service_path)));
281 }
282
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,
290                                            &profile_property);
291   CHECK(profile_property == profile_path.value());
292   ConfigureService(properties, callback, error_callback);
293 }
294
295
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()));
302 }
303
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));
309 }
310
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"));
318 }
319
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"));
327 }
328
329 void FakeShillManagerClient::ConnectToBestServices(
330     const base::Closure& callback,
331     const ErrorCallback& error_callback) {
332 }
333
334 ShillManagerClient::TestInterface* FakeShillManagerClient::GetTestInterface() {
335   return this;
336 }
337
338 // ShillManagerClient::TestInterface overrides.
339
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);
344   }
345 }
346
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);
352   }
353 }
354
355 void FakeShillManagerClient::ClearDevices() {
356   GetListProperty(shill::kDevicesProperty)->Clear();
357   CallNotifyObserversPropertyChanged(shill::kDevicesProperty);
358 }
359
360 void FakeShillManagerClient::AddTechnology(const std::string& type,
361                                            bool enabled) {
362   if (GetListProperty(shill::kAvailableTechnologiesProperty)->
363       AppendIfNotPresent(base::Value::CreateStringValue(type))) {
364     CallNotifyObserversPropertyChanged(
365         shill::kAvailableTechnologiesProperty);
366   }
367   if (enabled &&
368       GetListProperty(shill::kEnabledTechnologiesProperty)->
369       AppendIfNotPresent(base::Value::CreateStringValue(type))) {
370     CallNotifyObserversPropertyChanged(
371         shill::kEnabledTechnologiesProperty);
372   }
373 }
374
375 void FakeShillManagerClient::RemoveTechnology(const std::string& type) {
376   base::StringValue type_value(type);
377   if (GetListProperty(shill::kAvailableTechnologiesProperty)->Remove(
378       type_value, NULL)) {
379     CallNotifyObserversPropertyChanged(
380         shill::kAvailableTechnologiesProperty);
381   }
382   if (GetListProperty(shill::kEnabledTechnologiesProperty)->Remove(
383       type_value, NULL)) {
384     CallNotifyObserversPropertyChanged(
385         shill::kEnabledTechnologiesProperty);
386   }
387 }
388
389 void FakeShillManagerClient::SetTechnologyInitializing(const std::string& type,
390                                                        bool initializing) {
391   if (initializing) {
392     if (GetListProperty(shill::kUninitializedTechnologiesProperty)->
393         AppendIfNotPresent(base::Value::CreateStringValue(type))) {
394       CallNotifyObserversPropertyChanged(
395           shill::kUninitializedTechnologiesProperty);
396     }
397   } else {
398     if (GetListProperty(shill::kUninitializedTechnologiesProperty)->Remove(
399             base::StringValue(type), NULL)) {
400       CallNotifyObserversPropertyChanged(
401           shill::kUninitializedTechnologiesProperty);
402     }
403   }
404 }
405
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,
411                                                       &list_value)) {
412     list_value = new base::ListValue;
413     stub_geo_networks_.SetWithoutPathExpansion(technology, list_value);
414   }
415   list_value->Append(network.DeepCopy());
416 }
417
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);
423   }
424 }
425
426 void FakeShillManagerClient::ClearProperties() {
427   stub_properties_.Clear();
428 }
429
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));
434 }
435
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);
447   }
448   if (add_to_watch_list)
449     AddServiceToWatchList(service_path);
450 }
451
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);
458   }
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);
465   }
466 }
467
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);
474 }
475
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);
484   }
485 }
486
487 void FakeShillManagerClient::SortManagerServices() {
488   static const char* ordered_types[] = {
489     shill::kTypeEthernet,
490     shill::kTypeWifi,
491     shill::kTypeCellular,
492     shill::kTypeWimax,
493     shill::kTypeVPN
494   };
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);
501     }
502     return;
503   }
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);
509   }
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]);
515
516   CallNotifyObserversPropertyChanged(shill::kServicesProperty);
517
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);
526     if (!properties) {
527       LOG(ERROR) << "Properties not found for service: " << service_path;
528     } else {
529       std::string state;
530       properties->GetString(shill::kStateProperty, &state);
531       if (IsConnectedState(state))
532         new_default_service = service_path;
533     }
534   }
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);
539   }
540 }
541
542
543 int FakeShillManagerClient::GetInteractiveDelay() const {
544   return interactive_delay_;
545 }
546
547 void FakeShillManagerClient::SetupDefaultEnvironment() {
548   DBusThreadManager* dbus_manager = DBusThreadManager::Get();
549   ShillServiceClient::TestInterface* services =
550       dbus_manager->GetShillServiceClient()->GetTestInterface();
551   DCHECK(services);
552   ShillProfileClient::TestInterface* profiles =
553       dbus_manager->GetShillProfileClient()->GetTestInterface();
554   DCHECK(profiles);
555   ShillDeviceClient::TestInterface* devices =
556       dbus_manager->GetShillDeviceClient()->GetTestInterface();
557   DCHECK(devices);
558   ShillIPConfigClient::TestInterface* ip_configs =
559       dbus_manager->GetShillIPConfigClient()->GetTestInterface();
560   DCHECK(ip_configs);
561
562   const std::string shared_profile = ShillProfileClient::GetSharedProfilePath();
563   profiles->AddProfile(shared_profile, std::string());
564
565   const bool add_to_visible = true;
566   const bool add_to_watchlist = true;
567
568   bool enabled;
569   std::string state;
570
571   // IPConfigs
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);
588
589   // Ethernet
590   state = GetInitialStateForType(shill::kTypeEthernet, &enabled);
591   if (state == shill::kStateOnline) {
592     AddTechnology(shill::kTypeEthernet, enabled);
593     devices->AddDevice(
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,
600                                eth_ip_configs);
601     services->AddService("eth1", "eth1",
602                          shill::kTypeEthernet,
603                          state,
604                          add_to_visible, add_to_watchlist);
605     profiles->AddService(shared_profile, "eth1");
606   }
607
608   // Wifi
609   state = GetInitialStateForType(shill::kTypeWifi, &enabled);
610   if (state != kTechnologyUnavailable) {
611     bool portaled = false;
612     if (state == shill::kStatePortal) {
613       portaled = true;
614       state = shill::kStateIdle;
615     }
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,
623                                wifi_ip_configs);
624
625     services->AddService("wifi1",
626                          "wifi1",
627                          shill::kTypeWifi,
628                          state,
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");
634
635     services->AddService("wifi2",
636                          "wifi2_PSK",
637                          shill::kTypeWifi,
638                          shill::kStateIdle,
639                          add_to_visible, add_to_watchlist);
640     services->SetServiceProperty("wifi2",
641                                  shill::kSecurityProperty,
642                                  base::StringValue(shill::kSecurityPsk));
643
644     base::FundamentalValue strength_value(80);
645     services->SetServiceProperty(
646         "wifi2", shill::kSignalStrengthProperty, strength_value);
647     profiles->AddService(shared_profile, "wifi2");
648
649     if (portaled) {
650       const std::string kPortaledWifiPath = "portaled_wifi";
651       services->AddService(kPortaledWifiPath,
652                            "Portaled Wifi",
653                            shill::kTypeWifi,
654                            shill::kStatePortal,
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,
661                                               "portaled_wifi"));
662       services->SetServiceProperty(kPortaledWifiPath,
663                                    shill::kConnectableProperty,
664                                    base::FundamentalValue(true));
665       profiles->AddService(shared_profile, kPortaledWifiPath);
666     }
667   }
668
669   // Wimax
670   state = GetInitialStateForType(shill::kTypeWimax, &enabled);
671   if (state != kTechnologyUnavailable) {
672     AddTechnology(shill::kTypeWimax, enabled);
673     devices->AddDevice(
674         "/device/wimax1", shill::kTypeWimax, "stub_wimax_device1");
675
676     services->AddService("wimax1",
677                          "wimax1",
678                          shill::kTypeWimax,
679                          state,
680                          add_to_visible, add_to_watchlist);
681     services->SetServiceProperty(
682         "wimax1", shill::kConnectableProperty, base::FundamentalValue(true));
683   }
684
685   // Cellular
686   state = GetInitialStateForType(shill::kTypeCellular, &enabled);
687   if (state != kTechnologyUnavailable) {
688     bool activated = false;
689     if (state == kNetworkActivated) {
690       activated = true;
691       state = shill::kStateIdle;
692     }
693     AddTechnology(shill::kTypeCellular, enabled);
694     devices->AddDevice(
695         "/device/cellular1", shill::kTypeCellular, "stub_cellular_device1");
696     devices->SetDeviceProperty("/device/cellular1",
697                                shill::kCarrierProperty,
698                                base::StringValue(shill::kCarrierSprint));
699
700     services->AddService("cellular1",
701                          "cellular1",
702                          shill::kTypeCellular,
703                          state,
704                          add_to_visible, add_to_watchlist);
705     base::StringValue technology_value(shill::kNetworkTechnologyGsm);
706     services->SetServiceProperty(
707         "cellular1", shill::kNetworkTechnologyProperty, technology_value);
708
709     if (activated) {
710       services->SetServiceProperty(
711           "cellular1",
712           shill::kActivationStateProperty,
713           base::StringValue(shill::kActivationStateActivated));
714       services->SetServiceProperty("cellular1",
715                                    shill::kConnectableProperty,
716                                    base::FundamentalValue(true));
717     } else {
718       services->SetServiceProperty(
719           "cellular1",
720           shill::kActivationStateProperty,
721           base::StringValue(shill::kActivationStateNotActivated));
722     }
723
724     services->SetServiceProperty("cellular1",
725                                  shill::kRoamingStateProperty,
726                                  base::StringValue(shill::kRoamingStateHome));
727   }
728
729   // VPN
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");
740
741     services->AddService("vpn1",
742                          "vpn1",
743                          shill::kTypeVPN,
744                          state,
745                          add_to_visible, add_to_watchlist);
746     services->SetServiceProperty(
747         "vpn1", shill::kProviderProperty, provider_properties);
748     profiles->AddService(shared_profile, "vpn1");
749
750     services->AddService("vpn2",
751                          "vpn2",
752                          shill::kTypeVPN,
753                          shill::kStateIdle,
754                          add_to_visible, add_to_watchlist);
755     services->SetServiceProperty(
756         "vpn2", shill::kProviderProperty, provider_properties);
757   }
758
759   SortManagerServices();
760 }
761
762 // Private methods
763
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);
773 }
774
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);
787 }
788
789 void FakeShillManagerClient::PassStubGeoNetworks(
790     const DictionaryValueCallback& callback) const {
791   callback.Run(DBUS_METHOD_CALL_SUCCESS, stub_geo_networks_);
792 }
793
794 void FakeShillManagerClient::CallNotifyObserversPropertyChanged(
795     const std::string& property) {
796   // Avoid unnecessary delayed task if we have no observers (e.g. during
797   // initial setup).
798   if (!observer_list_.might_have_observers())
799     return;
800   base::MessageLoop::current()->PostTask(
801       FROM_HERE,
802       base::Bind(&FakeShillManagerClient::NotifyObserversPropertyChanged,
803                  weak_ptr_factory_.GetWeakPtr(),
804                  property));
805 }
806
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,
813                       observer_list_,
814                       OnPropertyChanged(property, *(services.get())));
815     return;
816   }
817   if (property == shill::kDevicesProperty) {
818     base::ListValue* devices = NULL;
819     if (stub_properties_.GetListWithoutPathExpansion(
820             shill::kDevicesProperty, &devices)) {
821       FOR_EACH_OBSERVER(ShillPropertyChangedObserver,
822                         observer_list_,
823                         OnPropertyChanged(property, *devices));
824     }
825     return;
826   }
827   base::Value* value = NULL;
828   if (!stub_properties_.GetWithoutPathExpansion(property, &value)) {
829     LOG(ERROR) << "Notify for unknown property: " << property;
830     return;
831   }
832   FOR_EACH_OBSERVER(ShillPropertyChangedObserver,
833                     observer_list_,
834                     OnPropertyChanged(property, *value));
835 }
836
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);
844   }
845   return list_property;
846 }
847
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())
857       enabled = true;
858   }
859   return enabled;
860 }
861
862 void FakeShillManagerClient::SetTechnologyEnabled(
863     const std::string& type,
864     const base::Closure& callback,
865     bool enabled) {
866   base::ListValue* enabled_list =
867       GetListProperty(shill::kEnabledTechnologiesProperty);
868   if (enabled)
869     enabled_list->AppendIfNotPresent(new base::StringValue(type));
870   else
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);
878 }
879
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))
891         continue;
892       const base::DictionaryValue* properties =
893           service_client->GetServiceProperties(service_path);
894       if (!properties) {
895         LOG(ERROR) << "Properties not found for service: " << service_path;
896         continue;
897       }
898       std::string name;
899       properties->GetString(shill::kNameProperty, &name);
900       std::string type;
901       properties->GetString(shill::kTypeProperty, &type);
902       if (TechnologyEnabled(type))
903         new_service_list->Append((*iter)->DeepCopy());
904     }
905   }
906   return new_service_list;
907 }
908
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));
916   }
917   CallNotifyObserversPropertyChanged(shill::kServicesProperty);
918   CallNotifyObserversPropertyChanged(shill::kServiceWatchListProperty);
919   base::MessageLoop::current()->PostTask(FROM_HERE, callback);
920 }
921
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);
932     }
933     return;
934   }
935   // Default setup
936   SetInitialNetworkState(shill::kTypeEthernet, shill::kStateOnline);
937   SetInitialNetworkState(shill::kTypeWifi, shill::kStateOnline);
938   SetInitialNetworkState(shill::kTypeCellular, shill::kStateIdle);
939   SetInitialNetworkState(shill::kTypeVPN, shill::kStateIdle);
940 }
941
942 bool FakeShillManagerClient::ParseOption(const std::string& arg0,
943                                          const std::string& arg1) {
944   if (arg0 == "interactive") {
945     int seconds = 3;
946     if (!arg1.empty())
947       base::StringToInt(arg1, &seconds);
948     interactive_delay_ = seconds;
949     return true;
950   }
951   return SetInitialNetworkState(arg0, arg1);
952 }
953
954 bool FakeShillManagerClient::SetInitialNetworkState(std::string type_arg,
955                                                     std::string state_arg) {
956   std::string state;
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;
979   } else {
980     LOG(ERROR) << "Unrecognized initial state: " << state_arg;
981     return false;
982   }
983
984   type_arg = StringToLowerASCII(type_arg);
985   // Special cases
986   if (type_arg == "wireless") {
987     shill_initial_state_map_[shill::kTypeWifi] = state;
988     shill_initial_state_map_[shill::kTypeCellular] = state;
989     return true;
990   }
991   // Convenience synonyms.
992   if (type_arg == "eth")
993     type_arg = shill::kTypeEthernet;
994
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;
1001     return false;
1002   }
1003
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;
1008   }
1009
1010   shill_initial_state_map_[type_arg] = state;
1011   return true;
1012 }
1013
1014 std::string FakeShillManagerClient::GetInitialStateForType(
1015     const std::string& type,
1016     bool* enabled) {
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()) {
1020     *enabled = false;
1021     return kTechnologyUnavailable;
1022   }
1023   std::string state = iter->second;
1024   if (state == kNetworkDisabled) {
1025     *enabled = false;
1026     return shill::kStateIdle;
1027   }
1028   *enabled = true;
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;
1033   }
1034   return state;
1035 }
1036
1037 }  // namespace chromeos