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