Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chromeos/dbus/shill_stub_helper.h" | 5 #include "chromeos/dbus/shill_stub_helper.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/command_line.h" | |
| 9 #include "base/strings/string_tokenizer.h" | 8 #include "base/strings/string_tokenizer.h" |
| 10 #include "chromeos/chromeos_switches.h" | |
| 11 #include "chromeos/dbus/dbus_thread_manager.h" | 9 #include "chromeos/dbus/dbus_thread_manager.h" |
| 12 #include "chromeos/dbus/shill_device_client.h" | 10 #include "chromeos/dbus/shill_device_client.h" |
| 13 #include "chromeos/dbus/shill_manager_client.h" | 11 #include "chromeos/dbus/shill_manager_client.h" |
| 14 #include "chromeos/dbus/shill_profile_client.h" | 12 #include "chromeos/dbus/shill_profile_client.h" |
| 15 #include "chromeos/dbus/shill_service_client.h" | 13 #include "chromeos/dbus/shill_service_client.h" |
| 16 #include "third_party/cros_system_api/dbus/service_constants.h" | 14 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 17 | 15 |
| 18 namespace chromeos { | 16 namespace chromeos { |
| 19 namespace shill_stub_helper { | 17 namespace shill_stub_helper { |
| 20 | 18 |
| 21 namespace { | 19 namespace { |
| 22 | 20 |
| 23 const char kDevicePathEthernet[] = "/device/eth1"; | 21 const char kDevicePathEthernet[] = "/device/eth1"; |
| 24 const char kDevicePathWifi[] = "/device/wifi1"; | 22 const char kDevicePathWifi[] = "/device/wifi1"; |
| 25 const char kDevicePathCellular[] = "/device/cellular1"; | 23 const char kDevicePathCellular[] = "/device/cellular1"; |
| 26 const char kDevicePathWimax[] = "/device/wimax1"; | 24 const char kDevicePathWimax[] = "/device/wimax1"; |
| 27 | 25 |
| 28 const char kStubPortalledWifiName[] = "Portalled Wifi"; | 26 const char kStubPortalledWifiName[] = "Portalled Wifi"; |
| 29 const char kStubPortalledWifiPath[] = "portalled_wifi"; | 27 const char kStubPortalledWifiPath[] = "portalled_wifi"; |
| 30 | 28 |
| 31 void UpdatePortalledWifiState() { | 29 void UpdatePortalledWifiState() { |
| 32 ShillServiceClient::TestInterface* services = | 30 ShillServiceClient::TestInterface* services = |
| 33 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface(); | 31 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface(); |
| 34 | 32 |
| 35 services->SetServiceProperty(kStubPortalledWifiPath, | 33 services->SetServiceProperty(kStubPortalledWifiPath, |
| 36 shill::kStateProperty, | 34 shill::kStateProperty, |
| 37 base::StringValue(shill::kStatePortal)); | 35 base::StringValue(shill::kStatePortal)); |
| 38 } | 36 } |
| 39 | 37 |
| 40 // Returns true if |network_type| is found in the comma separated list given | 38 std::string GetShillInitialState(const std::string& type, |
| 41 // with kEnabledStubNetworkTypes switch. | 39 bool* enabled, |
| 42 bool IsStubNetworkTypeEnabled(const std::string& network_type) { | 40 bool* portaled) { |
| 43 CommandLine* command_line = CommandLine::ForCurrentProcess(); | 41 std::string state = DBusThreadManager::Get()->GetShillInitialState(type); |
| 44 // If the switch is not present, enabled by default. | 42 if (state == shill::kStateDisconnect) { |
| 45 if (!command_line->HasSwitch(switches::kEnabledStubNetworkTypes)) | 43 *enabled = false; |
| 46 return true; | 44 *portaled = false; |
| 47 | 45 return shill::kStateIdle; |
| 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 } | 46 } |
| 55 return false; | 47 if (state == shill::kStatePortal) { |
| 48 if (type != shill::kTypeWifi) | |
| 49 LOG(WARNING) << "Invalid state: " << state << " for " << type; | |
| 50 *enabled = true; | |
| 51 *portaled = true; | |
| 52 return shill::kStateIdle; | |
| 53 } | |
| 54 if (state == shill::kActivationStateActivated && | |
| 55 type != shill::kTypeCellular) { | |
| 56 LOG(WARNING) << "Invalid state: " << state << " for " << type; | |
| 57 state = shill::kStateIdle; | |
| 58 } | |
| 59 *enabled = true; | |
| 60 *portaled = false; | |
| 61 return state; | |
| 56 } | 62 } |
| 57 | 63 |
| 58 } // namespace | 64 } // namespace |
| 59 | 65 |
| 60 const char kSharedProfilePath[] = "/profile/default"; | 66 const char kSharedProfilePath[] = "/profile/default"; |
| 61 | 67 |
| 62 bool IsStubPortalledWifiEnabled(const std::string& path) { | 68 bool IsStubPortalledWifiEnabled(const std::string& path) { |
|
pneubeck (no reviews)
2014/02/27 09:04:37
this super hacky function could move for example t
stevenjb
2014/02/28 02:44:06
Looking at the code that calls this, this shouldn'
| |
| 63 if (!CommandLine::ForCurrentProcess()->HasSwitch( | 69 std::string state = |
| 64 chromeos::switches::kEnableStubPortalledWifi)) { | 70 DBusThreadManager::Get()->GetShillInitialState(shill::kTypeWifi); |
| 65 return false; | 71 return state == shill::kStatePortal && path == kStubPortalledWifiPath; |
| 66 } | |
| 67 return path == kStubPortalledWifiPath; | |
| 68 } | 72 } |
| 69 | 73 |
| 70 void SetupDefaultEnvironment() { | 74 void SetupDefaultEnvironment() { |
|
pneubeck (no reviews)
2014/02/27 09:04:37
please pass FakeDBusThreadManager as an argument
stevenjb
2014/02/28 02:44:06
I ended up eliminating this class entirely. After
| |
| 75 DBusThreadManager* dbus_manager = DBusThreadManager::Get(); | |
| 71 ShillServiceClient::TestInterface* services = | 76 ShillServiceClient::TestInterface* services = |
| 72 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface(); | 77 dbus_manager->GetShillServiceClient()->GetTestInterface(); |
| 73 ShillProfileClient::TestInterface* profiles = | 78 ShillProfileClient::TestInterface* profiles = |
| 74 DBusThreadManager::Get()->GetShillProfileClient()->GetTestInterface(); | 79 dbus_manager->GetShillProfileClient()->GetTestInterface(); |
| 75 ShillManagerClient::TestInterface* manager = | 80 ShillManagerClient::TestInterface* manager = |
| 76 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface(); | 81 dbus_manager->GetShillManagerClient()->GetTestInterface(); |
| 77 ShillDeviceClient::TestInterface* devices = | 82 ShillDeviceClient::TestInterface* devices = |
| 78 DBusThreadManager::Get()->GetShillDeviceClient()->GetTestInterface(); | 83 dbus_manager->GetShillDeviceClient()->GetTestInterface(); |
| 79 if (!services || !profiles || !manager | !devices) | 84 if (!services || !profiles || !manager | !devices) |
| 80 return; | 85 return; |
| 81 | 86 |
| 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()); | 87 profiles->AddProfile(kSharedProfilePath, std::string()); |
| 89 | 88 |
| 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; | 89 const bool add_to_visible = true; |
| 103 const bool add_to_watchlist = true; | 90 const bool add_to_watchlist = true; |
| 104 | 91 |
| 105 // On real devices, service is not added for ethernet if cable is disconneted. | 92 bool enabled, portaled; |
| 106 if (IsStubNetworkTypeEnabled(shill::kTypeEthernet)) { | 93 std::string state; |
| 94 | |
| 95 // Ethernet. No service is not added unless connected. | |
| 96 state = GetShillInitialState(shill::kTypeEthernet, &enabled, &portaled); | |
| 97 if (state == shill::kStateOnline) { | |
| 98 manager->AddTechnology(shill::kTypeEthernet, enabled); | |
| 99 devices->AddDevice( | |
| 100 kDevicePathEthernet, shill::kTypeEthernet, "stub_eth_device1"); | |
| 107 services->AddService("eth1", "eth1", | 101 services->AddService("eth1", "eth1", |
| 108 shill::kTypeEthernet, | 102 shill::kTypeEthernet, |
| 109 shill::kStateOnline, | 103 state, |
| 110 add_to_visible, add_to_watchlist); | 104 add_to_visible, add_to_watchlist); |
| 111 profiles->AddService(kSharedProfilePath, "eth1"); | 105 profiles->AddService(kSharedProfilePath, "eth1"); |
| 112 } | 106 } |
| 113 | 107 |
| 114 // Wifi | 108 // Wifi |
| 109 state = GetShillInitialState(shill::kTypeWifi, &enabled, &portaled); | |
| 110 if (state != shill::kStateOffline) { | |
| 111 manager->AddTechnology(shill::kTypeWifi, enabled); | |
| 112 devices->AddDevice(kDevicePathWifi, shill::kTypeWifi, "stub_wifi_device1"); | |
| 115 | 113 |
| 116 services->AddService("wifi1", | 114 services->AddService("wifi1", |
| 117 "wifi1", | 115 "wifi1", |
| 118 shill::kTypeWifi, | 116 shill::kTypeWifi, |
| 119 IsStubNetworkTypeEnabled(shill::kTypeWifi) ? | 117 state, |
| 120 shill::kStateOnline : shill::kStateIdle, | 118 add_to_visible, add_to_watchlist); |
| 121 add_to_visible, add_to_watchlist); | 119 services->SetServiceProperty("wifi1", |
| 122 services->SetServiceProperty("wifi1", | 120 shill::kSecurityProperty, |
| 123 shill::kSecurityProperty, | 121 base::StringValue(shill::kSecurityWep)); |
| 124 base::StringValue(shill::kSecurityWep)); | 122 profiles->AddService(kSharedProfilePath, "wifi1"); |
| 125 profiles->AddService(kSharedProfilePath, "wifi1"); | |
| 126 | 123 |
| 127 services->AddService("wifi2", | 124 services->AddService("wifi2", |
| 128 "wifi2_PSK", | 125 "wifi2_PSK", |
| 129 shill::kTypeWifi, | 126 shill::kTypeWifi, |
| 130 shill::kStateIdle, | 127 shill::kStateIdle, |
| 131 add_to_visible, add_to_watchlist); | 128 add_to_visible, add_to_watchlist); |
| 132 services->SetServiceProperty("wifi2", | 129 services->SetServiceProperty("wifi2", |
| 133 shill::kSecurityProperty, | 130 shill::kSecurityProperty, |
| 134 base::StringValue(shill::kSecurityPsk)); | 131 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 | 132 |
| 140 if (CommandLine::ForCurrentProcess()->HasSwitch( | 133 base::FundamentalValue strength_value(80); |
| 141 chromeos::switches::kEnableStubPortalledWifi)) { | 134 services->SetServiceProperty( |
| 142 services->AddService(kStubPortalledWifiPath, | 135 "wifi2", shill::kSignalStrengthProperty, strength_value); |
| 143 kStubPortalledWifiName, | 136 profiles->AddService(kSharedProfilePath, "wifi2"); |
| 144 shill::kTypeWifi, | 137 |
| 145 shill::kStatePortal, | 138 if (portaled) { |
| 146 add_to_visible, add_to_watchlist); | 139 services->AddService(kStubPortalledWifiPath, |
| 147 services->SetServiceProperty(kStubPortalledWifiPath, | 140 kStubPortalledWifiName, |
| 148 shill::kSecurityProperty, | 141 shill::kTypeWifi, |
| 149 base::StringValue(shill::kSecurityNone)); | 142 shill::kStatePortal, |
| 150 services->SetConnectBehavior(kStubPortalledWifiPath, | 143 add_to_visible, add_to_watchlist); |
| 151 base::Bind(&UpdatePortalledWifiState)); | 144 services->SetServiceProperty(kStubPortalledWifiPath, |
| 152 services->SetServiceProperty(kStubPortalledWifiPath, | 145 shill::kSecurityProperty, |
| 153 shill::kConnectableProperty, | 146 base::StringValue(shill::kSecurityNone)); |
| 154 base::FundamentalValue(true)); | 147 services->SetConnectBehavior(kStubPortalledWifiPath, |
| 148 base::Bind(&UpdatePortalledWifiState)); | |
| 149 services->SetServiceProperty(kStubPortalledWifiPath, | |
| 150 shill::kConnectableProperty, | |
| 151 base::FundamentalValue(true)); | |
| 152 profiles->AddService(kSharedProfilePath, kStubPortalledWifiPath); | |
| 153 } | |
| 155 } | 154 } |
| 156 | 155 |
| 157 // Wimax | 156 // Wimax |
| 157 state = GetShillInitialState(shill::kTypeWimax, &enabled, &portaled); | |
| 158 if (state != shill::kStateOffline) { | |
| 159 manager->AddTechnology(shill::kTypeWimax, enabled); | |
| 160 devices->AddDevice( | |
| 161 kDevicePathWimax, shill::kTypeWimax, "stub_wimax_device1"); | |
| 158 | 162 |
| 159 services->AddService("wimax1", | 163 services->AddService("wimax1", |
| 160 "wimax1", | 164 "wimax1", |
| 161 shill::kTypeWimax, | 165 shill::kTypeWimax, |
| 162 shill::kStateIdle, | 166 state, |
| 163 add_to_visible, add_to_watchlist); | 167 add_to_visible, add_to_watchlist); |
| 164 services->SetServiceProperty( | 168 services->SetServiceProperty( |
| 165 "wimax1", shill::kConnectableProperty, base::FundamentalValue(true)); | 169 "wimax1", shill::kConnectableProperty, base::FundamentalValue(true)); |
| 170 } | |
| 166 | 171 |
| 167 // Cellular | 172 // Cellular |
| 173 state = GetShillInitialState(shill::kTypeCellular, &enabled, &portaled); | |
| 174 if (state != shill::kStateOffline) { | |
| 175 bool activated = false; | |
| 176 if (state == shill::kActivationStateActivated) { | |
| 177 activated = true; | |
| 178 state = shill::kStateIdle; | |
| 179 } | |
| 180 manager->AddTechnology(shill::kTypeCellular, enabled); | |
| 181 devices->AddDevice( | |
| 182 kDevicePathCellular, shill::kTypeCellular, "stub_cellular_device1"); | |
| 183 devices->SetDeviceProperty(kDevicePathCellular, | |
| 184 shill::kCarrierProperty, | |
| 185 base::StringValue(shill::kCarrierSprint)); | |
| 168 | 186 |
| 169 services->AddService("cellular1", | 187 LOG(ERROR) << "Cellular state: " << state; |
| 170 "cellular1", | 188 services->AddService("cellular1", |
| 171 shill::kTypeCellular, | 189 "cellular1", |
| 172 shill::kStateIdle, | 190 shill::kTypeCellular, |
| 173 add_to_visible, add_to_watchlist); | 191 state, |
| 174 base::StringValue technology_value(shill::kNetworkTechnologyGsm); | 192 add_to_visible, add_to_watchlist); |
| 175 services->SetServiceProperty( | 193 base::StringValue technology_value(shill::kNetworkTechnologyGsm); |
| 176 "cellular1", shill::kNetworkTechnologyProperty, technology_value); | 194 services->SetServiceProperty( |
| 177 services->SetServiceProperty( | 195 "cellular1", shill::kNetworkTechnologyProperty, technology_value); |
| 178 "cellular1", | 196 |
| 179 shill::kActivationStateProperty, | 197 if (activated) { |
| 180 base::StringValue(shill::kActivationStateNotActivated)); | 198 services->SetServiceProperty( |
| 181 services->SetServiceProperty("cellular1", | 199 "cellular1", |
| 182 shill::kRoamingStateProperty, | 200 shill::kActivationStateProperty, |
| 183 base::StringValue(shill::kRoamingStateHome)); | 201 base::StringValue(shill::kActivationStateActivated)); |
| 202 services->SetServiceProperty("cellular1", | |
| 203 shill::kConnectableProperty, | |
| 204 base::FundamentalValue(true)); | |
| 205 } else { | |
| 206 services->SetServiceProperty( | |
| 207 "cellular1", | |
| 208 shill::kActivationStateProperty, | |
| 209 base::StringValue(shill::kActivationStateNotActivated)); | |
| 210 } | |
| 211 | |
| 212 services->SetServiceProperty("cellular1", | |
| 213 shill::kRoamingStateProperty, | |
| 214 base::StringValue(shill::kRoamingStateHome)); | |
| 215 } | |
| 184 | 216 |
| 185 // VPN | 217 // VPN |
| 186 | 218 |
| 187 // Set the "Provider" dictionary properties. Note: when setting these in | 219 state = GetShillInitialState(shill::kTypeVPN, &enabled, &portaled); |
| 188 // Shill, "Provider.Type", etc keys are used, but when reading the values | 220 if (state != shill::kStateOffline) { |
| 189 // "Provider" . "Type", etc keys are used. Here we are setting the values | 221 // Set the "Provider" dictionary properties. Note: when setting these in |
| 190 // that will be read (by the UI, tests, etc). | 222 // Shill, "Provider.Type", etc keys are used, but when reading the values |
| 191 base::DictionaryValue provider_properties; | 223 // "Provider" . "Type", etc keys are used. Here we are setting the values |
| 192 provider_properties.SetString(shill::kTypeProperty, shill::kProviderOpenVpn); | 224 // that will be read (by the UI, tests, etc). |
| 193 provider_properties.SetString(shill::kHostProperty, "vpn_host"); | 225 base::DictionaryValue provider_properties; |
| 226 provider_properties.SetString(shill::kTypeProperty, | |
| 227 shill::kProviderOpenVpn); | |
| 228 provider_properties.SetString(shill::kHostProperty, "vpn_host"); | |
| 194 | 229 |
| 195 services->AddService("vpn1", | 230 services->AddService("vpn1", |
| 196 "vpn1", | 231 "vpn1", |
| 197 shill::kTypeVPN, | 232 shill::kTypeVPN, |
| 198 IsStubNetworkTypeEnabled(shill::kTypeVPN) ? | 233 state, |
| 199 shill::kStateOnline : shill::kStateIdle, | 234 add_to_visible, add_to_watchlist); |
| 200 add_to_visible, add_to_watchlist); | 235 services->SetServiceProperty( |
| 201 services->SetServiceProperty( | 236 "vpn1", shill::kProviderProperty, provider_properties); |
| 202 "vpn1", shill::kProviderProperty, provider_properties); | 237 profiles->AddService(kSharedProfilePath, "vpn1"); |
| 203 profiles->AddService(kSharedProfilePath, "vpn1"); | |
| 204 | 238 |
| 205 services->AddService("vpn2", | 239 services->AddService("vpn2", |
| 206 "vpn2", | 240 "vpn2", |
| 207 shill::kTypeVPN, | 241 shill::kTypeVPN, |
| 208 shill::kStateIdle, | 242 shill::kStateIdle, |
| 209 add_to_visible, add_to_watchlist); | 243 add_to_visible, add_to_watchlist); |
| 210 services->SetServiceProperty( | 244 services->SetServiceProperty( |
| 211 "vpn2", shill::kProviderProperty, provider_properties); | 245 "vpn2", shill::kProviderProperty, provider_properties); |
| 246 } | |
| 212 | 247 |
| 213 manager->SortManagerServices(); | 248 manager->SortManagerServices(); |
| 214 } | 249 } |
| 215 | 250 |
| 216 std::string DevicePathForType(const std::string& type) { | 251 std::string DevicePathForType(const std::string& type) { |
| 217 if (type == shill::kTypeEthernet) | 252 if (type == shill::kTypeEthernet) |
| 218 return kDevicePathEthernet; | 253 return kDevicePathEthernet; |
| 219 if (type == shill::kTypeWifi) | 254 if (type == shill::kTypeWifi) |
| 220 return kDevicePathWifi; | 255 return kDevicePathWifi; |
| 221 if (type == shill::kTypeCellular) | 256 if (type == shill::kTypeCellular) |
| 222 return kDevicePathCellular; | 257 return kDevicePathCellular; |
| 223 if (type == shill::kTypeWimax) | 258 if (type == shill::kTypeWimax) |
| 224 return kDevicePathWimax; | 259 return kDevicePathWimax; |
| 225 return ""; | 260 return ""; |
| 226 } | 261 } |
| 227 | 262 |
| 228 } // namespace shill_stub_helper | 263 } // namespace shill_stub_helper |
| 229 } // namespace chromeos | 264 } // namespace chromeos |
| OLD | NEW |