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