Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / chromeos / net / network_portal_detector_impl.h
bloba933ca6a303aee1637473a696f216193aad9baa8
1 // Copyright (c) 2013 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.
5 #ifndef CHROME_BROWSER_CHROMEOS_NET_NETWORK_PORTAL_DETECTOR_IMPL_H_
6 #define CHROME_BROWSER_CHROMEOS_NET_NETWORK_PORTAL_DETECTOR_IMPL_H_
8 #include <string>
10 #include "base/basictypes.h"
11 #include "base/cancelable_callback.h"
12 #include "base/compiler_specific.h"
13 #include "base/containers/hash_tables.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/observer_list.h"
18 #include "base/threading/non_thread_safe.h"
19 #include "base/time/time.h"
20 #include "chrome/browser/chromeos/net/network_portal_notification_controller.h"
21 #include "chromeos/network/network_state_handler_observer.h"
22 #include "chromeos/network/portal_detector/network_portal_detector.h"
23 #include "chromeos/network/portal_detector/network_portal_detector_strategy.h"
24 #include "components/captive_portal/captive_portal_detector.h"
25 #include "components/captive_portal/captive_portal_types.h"
26 #include "content/public/browser/notification_observer.h"
27 #include "content/public/browser/notification_registrar.h"
28 #include "net/url_request/url_fetcher.h"
29 #include "url/gurl.h"
31 class NetworkingConfigTest;
33 namespace net {
34 class URLRequestContextGetter;
37 namespace chromeos {
39 class NetworkState;
41 // This class handles all notifications about network changes from
42 // NetworkStateHandler and delegates portal detection for the default
43 // network to CaptivePortalService.
44 class NetworkPortalDetectorImpl
45 : public NetworkPortalDetector,
46 public base::NonThreadSafe,
47 public chromeos::NetworkStateHandlerObserver,
48 public content::NotificationObserver,
49 public PortalDetectorStrategy::Delegate {
50 public:
51 static const char kOobeDetectionResultHistogram[];
52 static const char kOobeDetectionDurationHistogram[];
53 static const char kOobeShillOnlineHistogram[];
54 static const char kOobeShillPortalHistogram[];
55 static const char kOobeShillOfflineHistogram[];
56 static const char kOobePortalToOnlineHistogram[];
58 static const char kSessionDetectionResultHistogram[];
59 static const char kSessionDetectionDurationHistogram[];
60 static const char kSessionShillOnlineHistogram[];
61 static const char kSessionShillPortalHistogram[];
62 static const char kSessionShillOfflineHistogram[];
63 static const char kSessionPortalToOnlineHistogram[];
65 // Creates an instance of NetworkPortalDetectorImpl.
66 static void Initialize(net::URLRequestContextGetter* url_context);
68 explicit NetworkPortalDetectorImpl(
69 const scoped_refptr<net::URLRequestContextGetter>& request_context);
70 ~NetworkPortalDetectorImpl() override;
72 // NetworkPortalDetector implementation:
73 void AddObserver(Observer* observer) override;
74 void AddAndFireObserver(Observer* observer) override;
75 void RemoveObserver(Observer* observer) override;
76 CaptivePortalState GetCaptivePortalState(const std::string& guid) override;
77 bool IsEnabled() override;
78 void Enable(bool start_detection) override;
79 bool StartDetectionIfIdle() override;
80 void SetStrategy(PortalDetectorStrategy::StrategyId id) override;
81 void OnLockScreenRequest() override;
83 // NetworkStateHandlerObserver implementation:
84 void DefaultNetworkChanged(const NetworkState* network) override;
86 // PortalDetectorStrategy::Delegate implementation:
87 int NoResponseResultCount() override;
88 base::TimeTicks AttemptStartTime() override;
89 base::TimeTicks NowTicks() override;
91 private:
92 friend class ::NetworkingConfigTest;
93 friend class NetworkPortalDetectorImplTest;
94 friend class NetworkPortalDetectorImplBrowserTest;
96 struct DetectionAttemptCompletedReport {
97 DetectionAttemptCompletedReport();
99 DetectionAttemptCompletedReport(const std::string network_name,
100 const std::string network_id,
101 captive_portal::CaptivePortalResult result,
102 int response_code);
104 void Report() const;
106 bool Equals(const DetectionAttemptCompletedReport& o) const;
108 std::string network_name;
109 std::string network_id;
110 captive_portal::CaptivePortalResult result;
111 int response_code;
114 typedef std::string NetworkId;
115 typedef base::hash_map<NetworkId, CaptivePortalState> CaptivePortalStateMap;
117 enum State {
118 // No portal check is running.
119 STATE_IDLE = 0,
120 // Waiting for portal check.
121 STATE_PORTAL_CHECK_PENDING,
122 // Portal check is in progress.
123 STATE_CHECKING_FOR_PORTAL,
126 // Starts detection process.
127 void StartDetection();
129 // Stops whole detection process.
130 void StopDetection();
132 // Stops and restarts the detection process.
133 void RetryDetection();
135 // Initiates Captive Portal detection attempt after |delay|.
136 void ScheduleAttempt(const base::TimeDelta& delay);
138 // Starts detection attempt.
139 void StartAttempt();
141 // Called when portal check is timed out. Cancels portal check and calls
142 // OnPortalDetectionCompleted() with RESULT_NO_RESPONSE as a result.
143 void OnAttemptTimeout();
145 // Called by CaptivePortalDetector when detection attempt completes.
146 void OnAttemptCompleted(
147 const captive_portal::CaptivePortalDetector::Results& results);
149 // content::NotificationObserver implementation:
150 void Observe(int type,
151 const content::NotificationSource& source,
152 const content::NotificationDetails& details) override;
154 // Stores captive portal state for a |network| and notifies observers.
155 void OnDetectionCompleted(const NetworkState* network,
156 const CaptivePortalState& results);
158 // Notifies observers that portal detection is completed for a |network|.
159 void NotifyDetectionCompleted(const NetworkState* network,
160 const CaptivePortalState& state);
162 State state() const { return state_; }
164 bool is_idle() const {
165 return state_ == STATE_IDLE;
167 bool is_portal_check_pending() const {
168 return state_ == STATE_PORTAL_CHECK_PENDING;
170 bool is_checking_for_portal() const {
171 return state_ == STATE_CHECKING_FOR_PORTAL;
174 int same_detection_result_count_for_testing() const {
175 return same_detection_result_count_;
178 int no_response_result_count_for_testing() const {
179 return no_response_result_count_;
182 void set_no_response_result_count_for_testing(int count) {
183 no_response_result_count_ = count;
186 // Returns delay before next portal check. Used by unit tests.
187 const base::TimeDelta& next_attempt_delay_for_testing() const {
188 return next_attempt_delay_;
191 // Returns true if attempt timeout callback isn't fired or
192 // cancelled.
193 bool AttemptTimeoutIsCancelledForTesting() const;
195 // Record detection stats such as detection duration and detection
196 // result in UMA.
197 void RecordDetectionStats(const NetworkState* network,
198 CaptivePortalStatus status);
200 // Resets strategy and all counters used in computations of
201 // timeouts.
202 void ResetStrategyAndCounters();
204 // Sets current test time ticks. Used by unit tests.
205 void set_time_ticks_for_testing(const base::TimeTicks& time_ticks) {
206 time_ticks_for_testing_ = time_ticks;
209 // Advances current test time ticks. Used by unit tests.
210 void advance_time_ticks_for_testing(const base::TimeDelta& delta) {
211 time_ticks_for_testing_ += delta;
214 // Name of the default network.
215 std::string default_network_name_;
217 // Unique identifier of the default network.
218 std::string default_network_id_;
220 // Connection state of the default network.
221 std::string default_connection_state_;
223 State state_;
224 CaptivePortalStateMap portal_state_map_;
225 base::ObserverList<Observer> observers_;
227 base::CancelableClosure attempt_task_;
228 base::CancelableClosure attempt_timeout_;
230 // URL that returns a 204 response code when connected to the Internet.
231 GURL test_url_;
233 // Detector for checking default network for a portal state.
234 scoped_ptr<captive_portal::CaptivePortalDetector> captive_portal_detector_;
236 // True if the NetworkPortalDetector is enabled.
237 bool enabled_;
239 // Start time of portal detection.
240 base::TimeTicks detection_start_time_;
242 // Start time of detection attempt.
243 base::TimeTicks attempt_start_time_;
245 // Delay before next portal detection.
246 base::TimeDelta next_attempt_delay_;
248 // Current detection strategy.
249 scoped_ptr<PortalDetectorStrategy> strategy_;
251 // Last received result from captive portal detector.
252 CaptivePortalStatus last_detection_result_;
254 // Number of detection attempts with same result in a row.
255 int same_detection_result_count_;
257 // Number of detection attempts in a row with NO RESPONSE result.
258 int no_response_result_count_;
260 // UI notification controller about captive portal state.
261 NetworkPortalNotificationController notification_controller_;
263 content::NotificationRegistrar registrar_;
265 // Test time ticks used by unit tests.
266 base::TimeTicks time_ticks_for_testing_;
268 // Contents of a last log message about completed detection attempt.
269 DetectionAttemptCompletedReport attempt_completed_report_;
271 base::WeakPtrFactory<NetworkPortalDetectorImpl> weak_factory_;
273 DISALLOW_COPY_AND_ASSIGN(NetworkPortalDetectorImpl);
276 } // namespace chromeos
278 #endif // CHROME_BROWSER_CHROMEOS_NET_NETWORK_PORTAL_DETECTOR_IMPL_H_