Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS 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 SHILL_PORTAL_DETECTOR_ |
| 6 | #define SHILL_PORTAL_DETECTOR_ |
| 7 | |
| 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 11 | #include <base/callback.h> |
Paul Stewart | f582b50 | 2012-04-04 21:39:22 -0700 | [diff] [blame] | 12 | #include <base/cancelable_callback.h> |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 13 | #include <base/memory/ref_counted.h> |
| 14 | #include <base/memory/scoped_ptr.h> |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 15 | #include <base/memory/weak_ptr.h> |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 16 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
| 17 | |
| 18 | #include "shill/http_request.h" |
| 19 | #include "shill/http_url.h" |
| 20 | #include "shill/refptr_types.h" |
| 21 | #include "shill/shill_time.h" |
| 22 | #include "shill/sockets.h" |
| 23 | |
| 24 | namespace shill { |
| 25 | |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 26 | class ByteString; |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 27 | class EventDispatcher; |
| 28 | class PortalDetector; |
| 29 | class Time; |
| 30 | |
| 31 | // The PortalDetector class implements the portal detection |
| 32 | // facility in shill, which is responsible for checking to see |
| 33 | // if a connection has "general internet connectivity". |
| 34 | // |
| 35 | // This information can be used for ranking one connection |
| 36 | // against another, or for informing UI and other components |
| 37 | // outside the connection manager whether the connection seems |
| 38 | // available for "general use" or if further user action may be |
| 39 | // necessary (e.g, click through of a WiFi Hotspot's splash |
| 40 | // page). |
| 41 | // |
| 42 | // This is achieved by trying to access a URL and expecting a |
| 43 | // specific response. Any result that deviates from this result |
| 44 | // (DNS or HTTP errors, as well as deviations from the expected |
| 45 | // content) are considered failures. |
| 46 | class PortalDetector { |
| 47 | public: |
| 48 | enum Phase { |
| 49 | kPhaseConnection, |
| 50 | kPhaseDNS, |
| 51 | kPhaseHTTP, |
| 52 | kPhaseContent, |
| 53 | kPhaseUnknown |
| 54 | }; |
| 55 | |
| 56 | enum Status { |
| 57 | kStatusFailure, |
| 58 | kStatusSuccess, |
| 59 | kStatusTimeout |
| 60 | }; |
| 61 | |
| 62 | struct Result { |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 63 | Result() |
| 64 | : phase(kPhaseUnknown), status(kStatusFailure), |
| 65 | num_attempts(0), final(false) {} |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 66 | Result(Phase phase_in, Status status_in) |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 67 | : phase(phase_in), status(status_in), |
| 68 | num_attempts(0), final(false) {} |
| 69 | Result(Phase phase_in, Status status_in, int num_attempts_in, bool final_in) |
| 70 | : phase(phase_in), status(status_in), |
| 71 | num_attempts(num_attempts_in), final(final_in) {} |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 72 | Phase phase; |
| 73 | Status status; |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 74 | // Total number of portal detections attempted. |
| 75 | // This includes failure, timeout and successful attempts. |
| 76 | // This only valid when |final| is true. |
| 77 | int num_attempts; |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 78 | bool final; |
| 79 | }; |
| 80 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 81 | static const int kDefaultCheckIntervalSeconds; |
Paul Stewart | f555cf8 | 2012-03-15 14:42:43 -0700 | [diff] [blame] | 82 | static const char kDefaultCheckPortalList[]; |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 83 | static const char kDefaultURL[]; |
| 84 | static const char kResponseExpected[]; |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 85 | // Maximum number of times the PortalDetector will attempt a connection. |
| 86 | static const int kMaxRequestAttempts; |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 87 | |
| 88 | PortalDetector(ConnectionRefPtr connection, |
| 89 | EventDispatcher *dispatcher, |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 90 | const base::Callback<void(const Result&)> &callback); |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 91 | virtual ~PortalDetector(); |
| 92 | |
| 93 | // Start a portal detection test. Returns true if |url_string| correctly |
| 94 | // parses as a URL. Returns false (and does not start) if the |url_string| |
| 95 | // fails to parse. |
| 96 | // |
| 97 | // As each attempt completes the callback handed to the constructor will |
| 98 | // be called. The PortalDetector will try up to kMaxRequestAttempts times |
| 99 | // to successfully retrieve the URL. If the attempt is successful or |
| 100 | // this is the last attempt, the "final" flag in the Result structure will |
| 101 | // be true, otherwise it will be false, and the PortalDetector will |
| 102 | // schedule the next attempt. |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 103 | virtual bool Start(const std::string &url_string); |
| 104 | virtual bool StartAfterDelay(const std::string &url_string, |
| 105 | int delay_seconds); |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 106 | |
| 107 | // End the current portal detection process if one exists, and do not call |
| 108 | // the callback. |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 109 | virtual void Stop(); |
| 110 | |
| 111 | // Returns whether portal request is "in progress": whether the portal |
| 112 | // detector is in the progress of making attempts. Returns true if |
| 113 | // attempts are in progress, false otherwise. Notably, this function |
| 114 | // returns false during the period of time between calling "Start" or |
| 115 | // "StartAfterDelay" and the actual start of the first attempt. |
| 116 | virtual bool IsInProgress(); |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 117 | |
| 118 | static const std::string PhaseToString(Phase phase); |
| 119 | static const std::string StatusToString(Status status); |
| 120 | static Result GetPortalResultForRequestResult(HTTPRequest::Result result); |
| 121 | |
| 122 | private: |
| 123 | friend class PortalDetectorTest; |
| 124 | FRIEND_TEST(PortalDetectorTest, StartAttemptFailed); |
| 125 | FRIEND_TEST(PortalDetectorTest, StartAttemptRepeated); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 126 | FRIEND_TEST(PortalDetectorTest, StartAttemptAfterDelay); |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 127 | FRIEND_TEST(PortalDetectorTest, AttemptCount); |
Christopher Wiley | 6e1dc0f | 2012-10-17 15:38:56 -0700 | [diff] [blame] | 128 | FRIEND_TEST(PortalDetectorTest, ReadBadHeadersRetry); |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 129 | |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 130 | // Minimum time between attempts to connect to server. |
| 131 | static const int kMinTimeBetweenAttemptsSeconds; |
| 132 | // Time to wait for request to complete. |
| 133 | static const int kRequestTimeoutSeconds; |
Christopher Wiley | 6e1dc0f | 2012-10-17 15:38:56 -0700 | [diff] [blame] | 134 | // Maximum number of failures in content phase before we stop attempting |
| 135 | // connections. |
| 136 | static const int kMaxFailuresInContentPhase; |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 137 | |
| 138 | static const char kPhaseConnectionString[]; |
| 139 | static const char kPhaseDNSString[]; |
| 140 | static const char kPhaseHTTPString[]; |
| 141 | static const char kPhaseContentString[]; |
| 142 | static const char kPhaseUnknownString[]; |
| 143 | |
| 144 | static const char kStatusFailureString[]; |
| 145 | static const char kStatusSuccessString[]; |
| 146 | static const char kStatusTimeoutString[]; |
| 147 | |
| 148 | void CompleteAttempt(Result result); |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 149 | void RequestReadCallback(const ByteString &response_data); |
| 150 | void RequestResultCallback(HTTPRequest::Result result, |
| 151 | const ByteString &response_data); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 152 | void StartAttempt(int init_delay_seconds); |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 153 | void StartAttemptTask(); |
| 154 | void StopAttempt(); |
| 155 | void TimeoutAttemptTask(); |
| 156 | |
| 157 | int attempt_count_; |
| 158 | struct timeval attempt_start_time_; |
| 159 | ConnectionRefPtr connection_; |
| 160 | EventDispatcher *dispatcher_; |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 161 | base::WeakPtrFactory<PortalDetector> weak_ptr_factory_; |
Paul Stewart | f582b50 | 2012-04-04 21:39:22 -0700 | [diff] [blame] | 162 | base::CancelableClosure attempt_timeout_; |
| 163 | base::CancelableClosure start_attempt_; |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 164 | base::Callback<void(const Result &)> portal_result_callback_; |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 165 | scoped_ptr<HTTPRequest> request_; |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 166 | base::Callback<void(const ByteString &)> request_read_callback_; |
| 167 | base::Callback<void(HTTPRequest::Result, const ByteString &)> |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 168 | request_result_callback_; |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 169 | Sockets sockets_; |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 170 | Time *time_; |
| 171 | HTTPURL url_; |
Christopher Wiley | 6e1dc0f | 2012-10-17 15:38:56 -0700 | [diff] [blame] | 172 | int failures_in_content_phase_; |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 173 | |
| 174 | DISALLOW_COPY_AND_ASSIGN(PortalDetector); |
| 175 | }; |
| 176 | |
| 177 | } // namespace shill |
| 178 | |
| 179 | #endif // SHILL_PORTAL_DETECTOR_ |