Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3)

Side by Side Diff: chromeos/network/network_state.cc

Issue 873713004: Introduce NetworkState::is_captive_portal() (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Feedback, unit test Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chromeos/network/network_state.h ('k') | chromeos/network/network_state_handler.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/network/network_state.h" 5 #include "chromeos/network/network_state.h"
6 6
7 #include "base/strings/string_number_conversions.h" 7 #include "base/strings/string_number_conversions.h"
8 #include "base/strings/stringprintf.h" 8 #include "base/strings/stringprintf.h"
9 #include "chromeos/device_event_log.h" 9 #include "chromeos/device_event_log.h"
10 #include "chromeos/network/network_profile_handler.h" 10 #include "chromeos/network/network_profile_handler.h"
(...skipping 11 matching lines...) Expand all
22 std::vector<std::string>* result) { 22 std::vector<std::string>* result) {
23 for (size_t i = 0; i < string_list.GetSize(); ++i) { 23 for (size_t i = 0; i < string_list.GetSize(); ++i) {
24 std::string str; 24 std::string str;
25 if (!string_list.GetString(i, &str)) 25 if (!string_list.GetString(i, &str))
26 return false; 26 return false;
27 result->push_back(str); 27 result->push_back(str);
28 } 28 }
29 return true; 29 return true;
30 } 30 }
31 31
32 bool IsCaptivePortalState(const base::DictionaryValue& properties, bool log) {
33 std::string state;
34 properties.GetStringWithoutPathExpansion(shill::kStateProperty, &state);
35 if (state != shill::kStatePortal)
36 return false;
37 std::string portal_detection_phase, portal_detection_status;
38 if (!properties.GetStringWithoutPathExpansion(
39 shill::kPortalDetectionFailedPhaseProperty,
40 &portal_detection_phase) ||
41 !properties.GetStringWithoutPathExpansion(
42 shill::kPortalDetectionFailedStatusProperty,
43 &portal_detection_status)) {
44 // If Shill (or a stub) has not set PortalDetectionFailedStatus
45 // or PortalDetectionFailedPhase, assume we are in captive portal state.
46 return true;
47 }
48
49 // Shill reports the phase in which it determined that the device is behind a
50 // captive portal. We only want to rely only on incorrect content being
51 // returned and ignore other reasons.
52 bool is_captive_portal =
53 portal_detection_phase == shill::kPortalDetectionPhaseContent &&
54 portal_detection_status == shill::kPortalDetectionStatusFailure;
55
56 if (log) {
57 std::string name;
58 properties.GetStringWithoutPathExpansion(shill::kNameProperty, &name);
59 if (name.empty())
60 properties.GetStringWithoutPathExpansion(shill::kSSIDProperty, &name);
61 if (!is_captive_portal) {
62 NET_LOG(EVENT) << "State is 'portal' but not in captive portal state:"
63 << " name=" << name << " phase=" << portal_detection_phase
64 << " status=" << portal_detection_status;
65 } else {
66 NET_LOG(EVENT) << "Network is in captive portal state: " << name;
67 }
68 }
69
70 return is_captive_portal;
71 }
72
32 } // namespace 73 } // namespace
33 74
34 namespace chromeos { 75 namespace chromeos {
35 76
36 NetworkState::NetworkState(const std::string& path) 77 NetworkState::NetworkState(const std::string& path)
37 : ManagedState(MANAGED_TYPE_NETWORK, path), 78 : ManagedState(MANAGED_TYPE_NETWORK, path),
38 visible_(false), 79 visible_(false),
80 prefix_length_(0),
39 connectable_(false), 81 connectable_(false),
40 prefix_length_(0), 82 is_captive_portal_(false),
41 signal_strength_(0), 83 signal_strength_(0),
42 cellular_out_of_credits_(false) { 84 cellular_out_of_credits_(false) {
43 } 85 }
44 86
45 NetworkState::~NetworkState() { 87 NetworkState::~NetworkState() {
46 } 88 }
47 89
48 bool NetworkState::PropertyChanged(const std::string& key, 90 bool NetworkState::PropertyChanged(const std::string& key,
49 const base::Value& value) { 91 const base::Value& value) {
50 // Keep care that these properties are the same as in |GetProperties|. 92 // Keep care that these properties are the same as in |GetProperties|.
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
134 NET_LOG(ERROR) << "NetworkState has no type: " 176 NET_LOG(ERROR) << "NetworkState has no type: "
135 << shill_property_util::GetNetworkIdFromProperties( 177 << shill_property_util::GetNetworkIdFromProperties(
136 properties); 178 properties);
137 return false; 179 return false;
138 } 180 }
139 181
140 // By convention, all visible WiFi and WiMAX networks have a 182 // By convention, all visible WiFi and WiMAX networks have a
141 // SignalStrength > 0. 183 // SignalStrength > 0.
142 if ((type() == shill::kTypeWifi || type() == shill::kTypeWimax) && 184 if ((type() == shill::kTypeWifi || type() == shill::kTypeWimax) &&
143 visible() && signal_strength_ <= 0) { 185 visible() && signal_strength_ <= 0) {
144 signal_strength_ = 1; 186 signal_strength_ = 1;
145 } 187 }
146 188
189 // Any change to connection state will trigger a complete property update,
190 // so we update is_captive_portal_ here.
191 is_captive_portal_ = IsCaptivePortalState(properties, true /* log */);
192
147 // Ensure that the network has a valid name. 193 // Ensure that the network has a valid name.
148 return UpdateName(properties); 194 return UpdateName(properties);
149 } 195 }
150 196
151 void NetworkState::GetStateProperties(base::DictionaryValue* dictionary) const { 197 void NetworkState::GetStateProperties(base::DictionaryValue* dictionary) const {
152 ManagedState::GetStateProperties(dictionary); 198 ManagedState::GetStateProperties(dictionary);
153 199
154 // Properties shared by all types. 200 // Properties shared by all types.
155 dictionary->SetStringWithoutPathExpansion(shill::kGuidProperty, guid()); 201 dictionary->SetStringWithoutPathExpansion(shill::kGuidProperty, guid());
156 dictionary->SetStringWithoutPathExpansion(shill::kSecurityClassProperty, 202 dictionary->SetStringWithoutPathExpansion(shill::kSecurityClassProperty,
(...skipping 18 matching lines...) Expand all
175 } 221 }
176 222
177 // Wifi properties 223 // Wifi properties
178 if (NetworkTypePattern::WiFi().MatchesType(type())) { 224 if (NetworkTypePattern::WiFi().MatchesType(type())) {
179 dictionary->SetStringWithoutPathExpansion(shill::kEapMethodProperty, 225 dictionary->SetStringWithoutPathExpansion(shill::kEapMethodProperty,
180 eap_method()); 226 eap_method());
181 } 227 }
182 228
183 // Mobile properties 229 // Mobile properties
184 if (NetworkTypePattern::Mobile().MatchesType(type())) { 230 if (NetworkTypePattern::Mobile().MatchesType(type())) {
185 dictionary->SetStringWithoutPathExpansion( 231 dictionary->SetStringWithoutPathExpansion(shill::kNetworkTechnologyProperty,
186 shill::kNetworkTechnologyProperty, 232 network_technology());
187 network_technology());
188 dictionary->SetStringWithoutPathExpansion(shill::kActivationStateProperty, 233 dictionary->SetStringWithoutPathExpansion(shill::kActivationStateProperty,
189 activation_state()); 234 activation_state());
190 dictionary->SetStringWithoutPathExpansion(shill::kRoamingStateProperty, 235 dictionary->SetStringWithoutPathExpansion(shill::kRoamingStateProperty,
191 roaming()); 236 roaming());
192 dictionary->SetBooleanWithoutPathExpansion(shill::kOutOfCreditsProperty, 237 dictionary->SetBooleanWithoutPathExpansion(shill::kOutOfCreditsProperty,
193 cellular_out_of_credits()); 238 cellular_out_of_credits());
194 } 239 }
195 } 240 }
196 241
197 void NetworkState::IPConfigPropertiesChanged( 242 void NetworkState::IPConfigPropertiesChanged(
198 const base::DictionaryValue& properties) { 243 const base::DictionaryValue& properties) {
199 for (base::DictionaryValue::Iterator iter(properties); 244 for (base::DictionaryValue::Iterator iter(properties); !iter.IsAtEnd();
200 !iter.IsAtEnd(); iter.Advance()) { 245 iter.Advance()) {
201 std::string key = iter.key(); 246 std::string key = iter.key();
202 const base::Value& value = iter.value(); 247 const base::Value& value = iter.value();
203 248
204 if (key == shill::kAddressProperty) { 249 if (key == shill::kAddressProperty) {
205 GetStringValue(key, value, &ip_address_); 250 GetStringValue(key, value, &ip_address_);
206 } else if (key == shill::kGatewayProperty) { 251 } else if (key == shill::kGatewayProperty) {
207 GetStringValue(key, value, &gateway_); 252 GetStringValue(key, value, &gateway_);
208 } else if (key == shill::kNameServersProperty) { 253 } else if (key == shill::kNameServersProperty) {
209 const base::ListValue* dns_servers; 254 const base::ListValue* dns_servers;
210 if (value.GetAsList(&dns_servers)) { 255 if (value.GetAsList(&dns_servers)) {
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
255 300
256 bool NetworkState::IsInProfile() const { 301 bool NetworkState::IsInProfile() const {
257 // kTypeEthernetEap is always saved. We need this check because it does 302 // kTypeEthernetEap is always saved. We need this check because it does
258 // not show up in the visible list, but its properties may not be available 303 // not show up in the visible list, but its properties may not be available
259 // when it first shows up in ServiceCompleteList. See crbug.com/355117. 304 // when it first shows up in ServiceCompleteList. See crbug.com/355117.
260 return !profile_path_.empty() || type() == shill::kTypeEthernetEap; 305 return !profile_path_.empty() || type() == shill::kTypeEthernetEap;
261 } 306 }
262 307
263 bool NetworkState::IsPrivate() const { 308 bool NetworkState::IsPrivate() const {
264 return !profile_path_.empty() && 309 return !profile_path_.empty() &&
265 profile_path_ != NetworkProfileHandler::GetSharedProfilePath(); 310 profile_path_ != NetworkProfileHandler::GetSharedProfilePath();
266 } 311 }
267 312
268 std::string NetworkState::GetDnsServersAsString() const { 313 std::string NetworkState::GetDnsServersAsString() const {
269 std::string result; 314 std::string result;
270 for (size_t i = 0; i < dns_servers_.size(); ++i) { 315 for (size_t i = 0; i < dns_servers_.size(); ++i) {
271 if (i != 0) 316 if (i != 0)
272 result += ","; 317 result += ",";
273 result += dns_servers_[i]; 318 result += dns_servers_[i];
274 } 319 }
275 return result; 320 return result;
276 } 321 }
277 322
278 std::string NetworkState::GetNetmask() const { 323 std::string NetworkState::GetNetmask() const {
279 return network_util::PrefixLengthToNetmask(prefix_length_); 324 return network_util::PrefixLengthToNetmask(prefix_length_);
280 } 325 }
281 326
282 std::string NetworkState::GetSpecifier() const { 327 std::string NetworkState::GetSpecifier() const {
283 if (!update_received()) { 328 if (!update_received()) {
284 NET_LOG(ERROR) << "GetSpecifier called before update: " << path(); 329 NET_LOG(ERROR) << "GetSpecifier called before update: " << path();
285 return std::string(); 330 return std::string();
286 } 331 }
287 if (type() == shill::kTypeWifi) 332 if (type() == shill::kTypeWifi)
288 return name() + "_" + security_class_; 333 return name() + "_" + security_class_;
289 if (!name().empty()) 334 if (!name().empty())
290 return name(); 335 return name();
291 return type(); // For unnamed networks such as ethernet. 336 return type(); // For unnamed networks such as ethernet.
292 } 337 }
293 338
294 void NetworkState::SetGuid(const std::string& guid) { 339 void NetworkState::SetGuid(const std::string& guid) {
(...skipping 18 matching lines...) Expand all
313 } 358 }
314 359
315 // static 360 // static
316 bool NetworkState::StateIsConnecting(const std::string& connection_state) { 361 bool NetworkState::StateIsConnecting(const std::string& connection_state) {
317 return (connection_state == shill::kStateAssociation || 362 return (connection_state == shill::kStateAssociation ||
318 connection_state == shill::kStateConfiguration || 363 connection_state == shill::kStateConfiguration ||
319 connection_state == shill::kStateCarrier); 364 connection_state == shill::kStateCarrier);
320 } 365 }
321 366
322 // static 367 // static
368 bool NetworkState::NetworkStateIsCaptivePortal(
369 const base::DictionaryValue& shill_properties) {
370 return IsCaptivePortalState(shill_properties, false /* log */);
371 }
372
373 // static
323 bool NetworkState::ErrorIsValid(const std::string& error) { 374 bool NetworkState::ErrorIsValid(const std::string& error) {
324 // Shill uses "Unknown" to indicate an unset or cleared error state. 375 // Shill uses "Unknown" to indicate an unset or cleared error state.
325 return !error.empty() && error != kErrorUnknown; 376 return !error.empty() && error != kErrorUnknown;
326 } 377 }
327 378
328 } // namespace chromeos 379 } // namespace chromeos
OLDNEW
« no previous file with comments | « chromeos/network/network_state.h ('k') | chromeos/network/network_state_handler.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698