OLD | NEW |
---|---|
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 "chrome/browser/captive_portal/captive_portal_detector.h" | 5 #include "components/captive_portal/captive_portal_detector.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
9 #include "chrome/browser/profiles/profile.h" | |
10 #include "net/base/load_flags.h" | 9 #include "net/base/load_flags.h" |
11 #include "net/http/http_response_headers.h" | 10 #include "net/http/http_response_headers.h" |
12 #include "net/url_request/url_request_status.h" | 11 #include "net/url_request/url_request_status.h" |
13 | 12 |
14 namespace captive_portal { | 13 namespace captive_portal { |
15 | 14 |
16 namespace { | |
17 | |
18 const char* const kCaptivePortalResultNames[] = { | |
19 "InternetConnected", | |
20 "NoResponse", | |
21 "BehindCaptivePortal", | |
22 "NumCaptivePortalResults", | |
23 }; | |
24 COMPILE_ASSERT(arraysize(kCaptivePortalResultNames) == RESULT_COUNT + 1, | |
25 captive_portal_result_name_count_mismatch); | |
26 | |
27 } // namespace | |
28 | |
29 const char CaptivePortalDetector::kDefaultURL[] = | 15 const char CaptivePortalDetector::kDefaultURL[] = |
30 "http://www.gstatic.com/generate_204"; | 16 "http://www.gstatic.com/generate_204"; |
31 | 17 |
32 CaptivePortalDetector::CaptivePortalDetector( | 18 CaptivePortalDetector::CaptivePortalDetector( |
33 const scoped_refptr<net::URLRequestContextGetter>& request_context) | 19 const scoped_refptr<net::URLRequestContextGetter>& request_context) |
34 : request_context_(request_context) { | 20 : request_context_(request_context) { |
35 } | 21 } |
36 | 22 |
37 CaptivePortalDetector::~CaptivePortalDetector() { | 23 CaptivePortalDetector::~CaptivePortalDetector() { |
38 } | 24 } |
39 | 25 |
40 // static | |
41 std::string CaptivePortalDetector::CaptivePortalResultToString(Result result) { | |
42 DCHECK_GE(result, 0); | |
43 DCHECK_LT(static_cast<unsigned int>(result), | |
44 arraysize(kCaptivePortalResultNames)); | |
45 return kCaptivePortalResultNames[result]; | |
46 } | |
47 | |
48 void CaptivePortalDetector::DetectCaptivePortal( | 26 void CaptivePortalDetector::DetectCaptivePortal( |
49 const GURL& url, | 27 const GURL& url, |
50 const DetectionCallback& detection_callback) { | 28 const DetectionCallback& detection_callback) { |
51 DCHECK(CalledOnValidThread()); | 29 DCHECK(CalledOnValidThread()); |
52 DCHECK(!FetchingURL()); | 30 DCHECK(!FetchingURL()); |
53 DCHECK(detection_callback_.is_null()); | 31 DCHECK(detection_callback_.is_null()); |
54 | 32 |
55 detection_callback_ = detection_callback; | 33 detection_callback_ = detection_callback; |
56 | 34 |
57 // The first 0 means this can use a TestURLFetcherFactory in unit tests. | 35 // The first 0 means this can use a TestURLFetcherFactory in unit tests. |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
93 } | 71 } |
94 | 72 |
95 // Takes a net::URLFetcher that has finished trying to retrieve the test | 73 // Takes a net::URLFetcher that has finished trying to retrieve the test |
96 // URL, and returns a CaptivePortalService::Result based on its result. | 74 // URL, and returns a CaptivePortalService::Result based on its result. |
97 void CaptivePortalDetector::GetCaptivePortalResultFromResponse( | 75 void CaptivePortalDetector::GetCaptivePortalResultFromResponse( |
98 const net::URLFetcher* url_fetcher, | 76 const net::URLFetcher* url_fetcher, |
99 Results* results) const { | 77 Results* results) const { |
100 DCHECK(results); | 78 DCHECK(results); |
101 DCHECK(!url_fetcher->GetStatus().is_io_pending()); | 79 DCHECK(!url_fetcher->GetStatus().is_io_pending()); |
102 | 80 |
103 results->result = RESULT_NO_RESPONSE; | 81 results->result = captive_portal::RESULT_NO_RESPONSE; |
ygorshenin1
2014/04/21 13:36:45
Why did you explicitly specify namespace here?
stevenjb
2014/04/21 16:42:27
In this case, I prefer explicitly namespaced enums
| |
104 results->response_code = url_fetcher->GetResponseCode(); | 82 results->response_code = url_fetcher->GetResponseCode(); |
105 results->retry_after_delta = base::TimeDelta(); | 83 results->retry_after_delta = base::TimeDelta(); |
106 results->landing_url = url_fetcher->GetURL(); | 84 results->landing_url = url_fetcher->GetURL(); |
107 | 85 |
108 // If there's a network error of some sort when fetching a file via HTTP, | 86 // If there's a network error of some sort when fetching a file via HTTP, |
109 // there may be a networking problem, rather than a captive portal. | 87 // there may be a networking problem, rather than a captive portal. |
110 // TODO(mmenke): Consider special handling for redirects that end up at | 88 // TODO(mmenke): Consider special handling for redirects that end up at |
111 // errors, especially SSL certificate errors. | 89 // errors, especially SSL certificate errors. |
112 if (url_fetcher->GetStatus().status() != net::URLRequestStatus::SUCCESS) | 90 if (url_fetcher->GetStatus().status() != net::URLRequestStatus::SUCCESS) |
113 return; | 91 return; |
(...skipping 17 matching lines...) Expand all Loading... | |
131 if (full_date > now) | 109 if (full_date > now) |
132 results->retry_after_delta = full_date - now; | 110 results->retry_after_delta = full_date - now; |
133 } | 111 } |
134 return; | 112 return; |
135 } | 113 } |
136 | 114 |
137 // A 511 response (Network Authentication Required) means that the user needs | 115 // A 511 response (Network Authentication Required) means that the user needs |
138 // to login to whatever server issued the response. | 116 // to login to whatever server issued the response. |
139 // See: http://tools.ietf.org/html/rfc6585 | 117 // See: http://tools.ietf.org/html/rfc6585 |
140 if (results->response_code == 511) { | 118 if (results->response_code == 511) { |
141 results->result = RESULT_BEHIND_CAPTIVE_PORTAL; | 119 results->result = captive_portal::RESULT_BEHIND_CAPTIVE_PORTAL; |
142 return; | 120 return; |
143 } | 121 } |
144 | 122 |
145 // Other non-2xx/3xx HTTP responses may indicate server errors. | 123 // Other non-2xx/3xx HTTP responses may indicate server errors. |
146 if (results->response_code >= 400 || results->response_code < 200) | 124 if (results->response_code >= 400 || results->response_code < 200) |
147 return; | 125 return; |
148 | 126 |
149 // A 204 response code indicates there's no captive portal. | 127 // A 204 response code indicates there's no captive portal. |
150 if (results->response_code == 204) { | 128 if (results->response_code == 204) { |
151 results->result = RESULT_INTERNET_CONNECTED; | 129 results->result = captive_portal::RESULT_INTERNET_CONNECTED; |
152 return; | 130 return; |
153 } | 131 } |
154 | 132 |
155 // Otherwise, assume it's a captive portal. | 133 // Otherwise, assume it's a captive portal. |
156 results->result = RESULT_BEHIND_CAPTIVE_PORTAL; | 134 results->result = captive_portal::RESULT_BEHIND_CAPTIVE_PORTAL; |
157 } | 135 } |
158 | 136 |
159 base::Time CaptivePortalDetector::GetCurrentTime() const { | 137 base::Time CaptivePortalDetector::GetCurrentTime() const { |
160 if (time_for_testing_.is_null()) | 138 if (time_for_testing_.is_null()) |
161 return base::Time::Now(); | 139 return base::Time::Now(); |
162 else | 140 else |
163 return time_for_testing_; | 141 return time_for_testing_; |
164 } | 142 } |
165 | 143 |
166 bool CaptivePortalDetector::FetchingURL() const { | 144 bool CaptivePortalDetector::FetchingURL() const { |
167 return url_fetcher_.get() != NULL; | 145 return url_fetcher_.get() != NULL; |
168 } | 146 } |
169 | 147 |
170 } // namespace captive_portal | 148 } // namespace captive_portal |
OLD | NEW |