• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/shill_stub_helper.h"
6 
7 #include "base/bind.h"
8 #include "base/command_line.h"
9 #include "chromeos/chromeos_switches.h"
10 #include "chromeos/dbus/dbus_thread_manager.h"
11 #include "chromeos/dbus/shill_device_client.h"
12 #include "chromeos/dbus/shill_manager_client.h"
13 #include "chromeos/dbus/shill_profile_client.h"
14 #include "chromeos/dbus/shill_service_client.h"
15 #include "third_party/cros_system_api/dbus/service_constants.h"
16 
17 namespace chromeos {
18 namespace shill_stub_helper {
19 
20 namespace {
21 
22 const char kDevicePathEthernet[] = "/device/eth1";
23 const char kDevicePathWifi[] = "/device/wifi1";
24 const char kDevicePathCellular[] = "/device/cellular1";
25 const char kDevicePathWimax[] = "/device/wimax1";
26 
27 const char kStubPortalledWifiName[] = "Portalled Wifi";
28 const char kStubPortalledWifiPath[] = "portalled_wifi";
29 
UpdatePortalledWifiState()30 void UpdatePortalledWifiState() {
31   ShillServiceClient::TestInterface* services =
32       DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface();
33 
34   services->SetServiceProperty(kStubPortalledWifiPath,
35                                shill::kStateProperty,
36                                base::StringValue(shill::kStatePortal));
37 }
38 
39 }  // namespace
40 
41 const char kSharedProfilePath[] = "/profile/default";
42 
IsStubPortalledWifiEnabled(const std::string & path)43 bool IsStubPortalledWifiEnabled(const std::string& path) {
44   if (!CommandLine::ForCurrentProcess()->HasSwitch(
45            chromeos::switches::kEnableStubPortalledWifi)) {
46     return false;
47   }
48   return path == kStubPortalledWifiPath;
49 }
50 
SetupDefaultEnvironment()51 void SetupDefaultEnvironment() {
52   ShillServiceClient::TestInterface* services =
53       DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface();
54   ShillProfileClient::TestInterface* profiles =
55       DBusThreadManager::Get()->GetShillProfileClient()->GetTestInterface();
56   ShillManagerClient::TestInterface* manager =
57       DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface();
58   ShillDeviceClient::TestInterface* devices =
59       DBusThreadManager::Get()->GetShillDeviceClient()->GetTestInterface();
60   if (!services || !profiles || !manager | !devices)
61     return;
62 
63   // Stub Technologies.
64   if (!CommandLine::ForCurrentProcess()->HasSwitch(
65            chromeos::switches::kDisableStubEthernet)) {
66     manager->AddTechnology(shill::kTypeEthernet, true);
67   }
68   manager->AddTechnology(shill::kTypeWifi, true);
69   manager->AddTechnology(shill::kTypeCellular, true);
70   manager->AddTechnology(shill::kTypeWimax, true);
71 
72   profiles->AddProfile(kSharedProfilePath, std::string());
73 
74   devices->AddDevice(
75       kDevicePathEthernet, shill::kTypeEthernet, "stub_eth_device1");
76   devices->AddDevice(kDevicePathWifi, shill::kTypeWifi, "stub_wifi_device1");
77 
78   devices->AddDevice(
79       kDevicePathCellular, shill::kTypeCellular, "stub_cellular_device1");
80   devices->SetDeviceProperty(kDevicePathCellular,
81                              shill::kCarrierProperty,
82                              base::StringValue(shill::kCarrierSprint));
83 
84   devices->AddDevice(kDevicePathWimax, shill::kTypeWimax, "stub_wimax_device1");
85 
86   const bool add_to_visible = true;
87   const bool add_to_watchlist = true;
88 
89   if (!CommandLine::ForCurrentProcess()->HasSwitch(
90            chromeos::switches::kDisableStubEthernet)) {
91     services->AddService("eth1", "eth1",
92                          shill::kTypeEthernet,
93                          shill::kStateOnline,
94                          add_to_visible, add_to_watchlist);
95     profiles->AddService(kSharedProfilePath, "eth1");
96   }
97 
98   // Wifi
99 
100   services->AddService("wifi1",
101                        "wifi1",
102                        shill::kTypeWifi,
103                        shill::kStateOnline,
104                        add_to_visible, add_to_watchlist);
105   services->SetServiceProperty("wifi1",
106                                shill::kSecurityProperty,
107                                base::StringValue(shill::kSecurityWep));
108   profiles->AddService(kSharedProfilePath, "wifi1");
109 
110   services->AddService("wifi2",
111                        "wifi2_PSK",
112                        shill::kTypeWifi,
113                        shill::kStateIdle,
114                        add_to_visible, add_to_watchlist);
115   services->SetServiceProperty("wifi2",
116                                shill::kSecurityProperty,
117                                base::StringValue(shill::kSecurityPsk));
118   base::FundamentalValue strength_value(80);
119   services->SetServiceProperty(
120       "wifi2", shill::kSignalStrengthProperty, strength_value);
121   profiles->AddService(kSharedProfilePath, "wifi2");
122 
123   if (CommandLine::ForCurrentProcess()->HasSwitch(
124           chromeos::switches::kEnableStubPortalledWifi)) {
125     services->AddService(kStubPortalledWifiPath,
126                          kStubPortalledWifiName,
127                          shill::kTypeWifi,
128                          shill::kStatePortal,
129                          add_to_visible, add_to_watchlist);
130     services->SetServiceProperty(kStubPortalledWifiPath,
131                                  shill::kSecurityProperty,
132                                  base::StringValue(shill::kSecurityNone));
133     services->SetConnectBehavior(kStubPortalledWifiPath,
134                                  base::Bind(&UpdatePortalledWifiState));
135     services->SetServiceProperty(kStubPortalledWifiPath,
136                                  shill::kConnectableProperty,
137                                  base::FundamentalValue(true));
138   }
139 
140   // Wimax
141 
142   services->AddService("wimax1",
143                        "wimax1",
144                        shill::kTypeWimax,
145                        shill::kStateIdle,
146                        add_to_visible, add_to_watchlist);
147   services->SetServiceProperty(
148       "wimax1", shill::kConnectableProperty, base::FundamentalValue(true));
149 
150   // Cellular
151 
152   services->AddService("cellular1",
153                        "cellular1",
154                        shill::kTypeCellular,
155                        shill::kStateIdle,
156                        add_to_visible, add_to_watchlist);
157   base::StringValue technology_value(shill::kNetworkTechnologyGsm);
158   services->SetServiceProperty(
159       "cellular1", shill::kNetworkTechnologyProperty, technology_value);
160   services->SetServiceProperty(
161       "cellular1",
162       shill::kActivationStateProperty,
163       base::StringValue(shill::kActivationStateNotActivated));
164   services->SetServiceProperty("cellular1",
165                                shill::kRoamingStateProperty,
166                                base::StringValue(shill::kRoamingStateHome));
167 
168   // VPN
169 
170   // Set the "Provider" dictionary properties. Note: when setting these in
171   // Shill, "Provider.Type", etc keys are used, but when reading the values
172   // "Provider" . "Type", etc keys are used. Here we are setting the values
173   // that will be read (by the UI, tests, etc).
174   base::DictionaryValue provider_properties;
175   provider_properties.SetString(shill::kTypeProperty, shill::kProviderOpenVpn);
176   provider_properties.SetString(shill::kHostProperty, "vpn_host");
177 
178   services->AddService("vpn1",
179                        "vpn1",
180                        shill::kTypeVPN,
181                        shill::kStateOnline,
182                        add_to_visible, add_to_watchlist);
183   services->SetServiceProperty(
184       "vpn1", shill::kProviderProperty, provider_properties);
185   profiles->AddService(kSharedProfilePath, "vpn1");
186 
187   services->AddService("vpn2",
188                        "vpn2",
189                        shill::kTypeVPN,
190                        shill::kStateOffline,
191                        add_to_visible, add_to_watchlist);
192   services->SetServiceProperty(
193       "vpn2", shill::kProviderProperty, provider_properties);
194 
195   manager->SortManagerServices();
196 }
197 
DevicePathForType(const std::string & type)198 std::string DevicePathForType(const std::string& type) {
199   if (type == shill::kTypeEthernet)
200     return kDevicePathEthernet;
201   if (type == shill::kTypeWifi)
202     return kDevicePathWifi;
203   if (type == shill::kTypeCellular)
204     return kDevicePathCellular;
205   if (type == shill::kTypeWimax)
206     return kDevicePathWimax;
207   return "";
208 }
209 
210 }  // namespace shill_stub_helper
211 }  // namespace chromeos
212