| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef CHROME_BROWSER_CAPTIVE_PORTAL_CAPTIVE_PORTAL_DETECTOR_H_ | |
| 6 #define CHROME_BROWSER_CAPTIVE_PORTAL_CAPTIVE_PORTAL_DETECTOR_H_ | |
| 7 | |
| 8 #include "base/basictypes.h" | |
| 9 #include "base/callback.h" | |
| 10 #include "base/compiler_specific.h" | |
| 11 #include "base/memory/ref_counted.h" | |
| 12 #include "base/memory/scoped_ptr.h" | |
| 13 #include "base/threading/non_thread_safe.h" | |
| 14 #include "base/time/time.h" | |
| 15 #include "net/url_request/url_fetcher.h" | |
| 16 #include "net/url_request/url_fetcher_delegate.h" | |
| 17 #include "net/url_request/url_request_context_getter.h" | |
| 18 | |
| 19 class GURL; | |
| 20 | |
| 21 namespace captive_portal { | |
| 22 | |
| 23 // Possible results of an attempt to detect a captive portal. | |
| 24 enum Result { | |
| 25 // There's a confirmed connection to the Internet. | |
| 26 RESULT_INTERNET_CONNECTED, | |
| 27 // The URL request received a network or HTTP error, or a non-HTTP response. | |
| 28 RESULT_NO_RESPONSE, | |
| 29 // The URL request apparently encountered a captive portal. It received a | |
| 30 // a valid HTTP response with a 2xx other than 204, 3xx, or 511 status code. | |
| 31 RESULT_BEHIND_CAPTIVE_PORTAL, | |
| 32 RESULT_COUNT | |
| 33 }; | |
| 34 | |
| 35 class CaptivePortalDetector : public net::URLFetcherDelegate, | |
| 36 public base::NonThreadSafe { | |
| 37 public: | |
| 38 struct Results { | |
| 39 Results() | |
| 40 : result(RESULT_NO_RESPONSE), | |
| 41 response_code(net::URLFetcher::RESPONSE_CODE_INVALID) { | |
| 42 } | |
| 43 | |
| 44 Result result; | |
| 45 int response_code; | |
| 46 base::TimeDelta retry_after_delta; | |
| 47 GURL landing_url; | |
| 48 }; | |
| 49 | |
| 50 typedef base::Callback<void(const Results& results)> DetectionCallback; | |
| 51 | |
| 52 // The test URL. When connected to the Internet, it should return a | |
| 53 // blank page with a 204 status code. When behind a captive portal, | |
| 54 // requests for this URL should get an HTTP redirect or a login | |
| 55 // page. When neither is true, no server should respond to requests | |
| 56 // for this URL. | |
| 57 static const char kDefaultURL[]; | |
| 58 | |
| 59 explicit CaptivePortalDetector( | |
| 60 const scoped_refptr<net::URLRequestContextGetter>& request_context); | |
| 61 virtual ~CaptivePortalDetector(); | |
| 62 | |
| 63 static std::string CaptivePortalResultToString(Result result); | |
| 64 | |
| 65 // Triggers a check for a captive portal. After completion, runs the | |
| 66 // |callback|. | |
| 67 void DetectCaptivePortal(const GURL& url, const DetectionCallback& callback); | |
| 68 | |
| 69 // Cancels captive portal check. | |
| 70 void Cancel(); | |
| 71 | |
| 72 private: | |
| 73 friend class CaptivePortalDetectorTestBase; | |
| 74 | |
| 75 // net::URLFetcherDelegate: | |
| 76 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | |
| 77 | |
| 78 // Takes a net::URLFetcher that has finished trying to retrieve the | |
| 79 // test URL, and fills a Results struct based on its result. If the | |
| 80 // response is a 503 with a Retry-After header, |retry_after| field | |
| 81 // of |results| is populated accordingly. Otherwise, it's set to | |
| 82 // base::TimeDelta(). | |
| 83 void GetCaptivePortalResultFromResponse(const net::URLFetcher* url_fetcher, | |
| 84 Results* results) const; | |
| 85 | |
| 86 // Returns the current time. Used only when determining time until a | |
| 87 // Retry-After date. | |
| 88 base::Time GetCurrentTime() const; | |
| 89 | |
| 90 // Returns true if a captive portal check is currently running. | |
| 91 bool FetchingURL() const; | |
| 92 | |
| 93 // Sets current test time. Used by unit tests. | |
| 94 void set_time_for_testing(const base::Time& time) { | |
| 95 time_for_testing_ = time; | |
| 96 } | |
| 97 | |
| 98 // Advances current test time. Used by unit tests. | |
| 99 void advance_time_for_testing(const base::TimeDelta& delta) { | |
| 100 time_for_testing_ += delta; | |
| 101 } | |
| 102 | |
| 103 // URL request context. | |
| 104 scoped_refptr<net::URLRequestContextGetter> request_context_; | |
| 105 | |
| 106 DetectionCallback detection_callback_; | |
| 107 | |
| 108 scoped_ptr<net::URLFetcher> url_fetcher_; | |
| 109 | |
| 110 // Test time used by unit tests. | |
| 111 base::Time time_for_testing_; | |
| 112 | |
| 113 DISALLOW_COPY_AND_ASSIGN(CaptivePortalDetector); | |
| 114 }; | |
| 115 | |
| 116 } // namespace captive_portal | |
| 117 | |
| 118 #endif // CHROME_BROWSER_CAPTIVE_PORTAL_CAPTIVE_PORTAL_DETECTOR_H_ | |
| OLD | NEW |