Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(632)

Side by Side Diff: chrome/browser/chromeos/net/network_portal_detector_impl.h

Issue 1346843003: Refactor NetworkPortalDetector and NetworkPortalNotificationController. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 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 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 #ifndef CHROME_BROWSER_CHROMEOS_NET_NETWORK_PORTAL_DETECTOR_IMPL_H_ 5 #ifndef CHROME_BROWSER_CHROMEOS_NET_NETWORK_PORTAL_DETECTOR_IMPL_H_
6 #define CHROME_BROWSER_CHROMEOS_NET_NETWORK_PORTAL_DETECTOR_IMPL_H_ 6 #define CHROME_BROWSER_CHROMEOS_NET_NETWORK_PORTAL_DETECTOR_IMPL_H_
7 7
8 #include <string> 8 #include <string>
9 9
10 #include "base/basictypes.h" 10 #include "base/basictypes.h"
11 #include "base/cancelable_callback.h" 11 #include "base/cancelable_callback.h"
12 #include "base/compiler_specific.h" 12 #include "base/compiler_specific.h"
13 #include "base/containers/hash_tables.h" 13 #include "base/containers/hash_tables.h"
14 #include "base/memory/ref_counted.h" 14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h" 15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/weak_ptr.h" 16 #include "base/memory/weak_ptr.h"
17 #include "base/observer_list.h" 17 #include "base/observer_list.h"
18 #include "base/threading/non_thread_safe.h" 18 #include "base/threading/non_thread_safe.h"
19 #include "base/time/time.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" 20 #include "chromeos/network/network_state_handler_observer.h"
22 #include "chromeos/network/portal_detector/network_portal_detector.h" 21 #include "chromeos/network/portal_detector/network_portal_detector.h"
23 #include "chromeos/network/portal_detector/network_portal_detector_strategy.h" 22 #include "chromeos/network/portal_detector/network_portal_detector_strategy.h"
24 #include "components/captive_portal/captive_portal_detector.h" 23 #include "components/captive_portal/captive_portal_detector.h"
25 #include "components/captive_portal/captive_portal_types.h" 24 #include "components/captive_portal/captive_portal_types.h"
26 #include "content/public/browser/notification_observer.h" 25 #include "content/public/browser/notification_observer.h"
27 #include "content/public/browser/notification_registrar.h" 26 #include "content/public/browser/notification_registrar.h"
28 #include "net/url_request/url_fetcher.h" 27 #include "net/url_request/url_fetcher.h"
29 #include "url/gurl.h" 28 #include "url/gurl.h"
30 29
31 class NetworkingConfigTest; 30 class NetworkingConfigTest;
32 31
33 namespace net { 32 namespace net {
34 class URLRequestContextGetter; 33 class URLRequestContextGetter;
35 } 34 }
36 35
37 namespace chromeos { 36 namespace chromeos {
38 37
38 class NetworkPortalNotificationController;
39 class NetworkState; 39 class NetworkState;
40 40
41 // This class handles all notifications about network changes from 41 // This class handles all notifications about network changes from
42 // NetworkStateHandler and delegates portal detection for the default 42 // NetworkStateHandler and delegates portal detection for the default
43 // network to CaptivePortalService. 43 // network to CaptivePortalService.
44 class NetworkPortalDetectorImpl 44 class NetworkPortalDetectorImpl
45 : public NetworkPortalDetector, 45 : public NetworkPortalDetector,
46 public base::NonThreadSafe, 46 public base::NonThreadSafe,
47 public chromeos::NetworkStateHandlerObserver, 47 public chromeos::NetworkStateHandlerObserver,
48 public content::NotificationObserver, 48 public content::NotificationObserver,
49 public PortalDetectorStrategy::Delegate { 49 public PortalDetectorStrategy::Delegate {
50 public: 50 public:
51 static const char kOobeDetectionResultHistogram[]; 51 static const char kOobeDetectionResultHistogram[];
52 static const char kOobeDetectionDurationHistogram[]; 52 static const char kOobeDetectionDurationHistogram[];
53 static const char kOobeShillOnlineHistogram[]; 53 static const char kOobeShillOnlineHistogram[];
54 static const char kOobeShillPortalHistogram[]; 54 static const char kOobeShillPortalHistogram[];
55 static const char kOobeShillOfflineHistogram[]; 55 static const char kOobeShillOfflineHistogram[];
56 static const char kOobePortalToOnlineHistogram[]; 56 static const char kOobePortalToOnlineHistogram[];
57 57
58 static const char kSessionDetectionResultHistogram[]; 58 static const char kSessionDetectionResultHistogram[];
59 static const char kSessionDetectionDurationHistogram[]; 59 static const char kSessionDetectionDurationHistogram[];
60 static const char kSessionShillOnlineHistogram[]; 60 static const char kSessionShillOnlineHistogram[];
61 static const char kSessionShillPortalHistogram[]; 61 static const char kSessionShillPortalHistogram[];
62 static const char kSessionShillOfflineHistogram[]; 62 static const char kSessionShillOfflineHistogram[];
63 static const char kSessionPortalToOnlineHistogram[]; 63 static const char kSessionPortalToOnlineHistogram[];
64 64
65 // Creates an instance of NetworkPortalDetectorImpl. 65 NetworkPortalDetectorImpl(
66 static void Initialize(net::URLRequestContextGetter* url_context); 66 const scoped_refptr<net::URLRequestContextGetter>& request_context,
67 67 bool create_notification_controller);
68 explicit NetworkPortalDetectorImpl(
69 const scoped_refptr<net::URLRequestContextGetter>& request_context);
70 ~NetworkPortalDetectorImpl() override; 68 ~NetworkPortalDetectorImpl() override;
71 69
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;
82
83 // NetworkStateHandlerObserver implementation:
84 void DefaultNetworkChanged(const NetworkState* network) override;
85
86 // PortalDetectorStrategy::Delegate implementation:
87 int NoResponseResultCount() override;
88 base::TimeTicks AttemptStartTime() override;
89 base::TimeTicks NowTicks() override;
90
91 private: 70 private:
92 friend class ::NetworkingConfigTest; 71 friend class ::NetworkingConfigTest;
93 friend class NetworkPortalDetectorImplTest; 72 friend class NetworkPortalDetectorImplTest;
94 friend class NetworkPortalDetectorImplBrowserTest; 73 friend class NetworkPortalDetectorImplBrowserTest;
95 74
75 typedef base::hash_map<std::string, CaptivePortalState> CaptivePortalStateMap;
stevenjb 2015/09/17 20:45:52 nit: WS
achuithb 2015/09/17 22:03:32 What's WS? I imagine you mean using using? If so,
stevenjb 2015/09/18 22:04:09 "White Space" :) There should be a blank line here
achuithb 2015/09/22 17:37:23 Done.
76 enum State {
77 // No portal check is running.
78 STATE_IDLE = 0,
79 // Waiting for portal check.
80 STATE_PORTAL_CHECK_PENDING,
81 // Portal check is in progress.
82 STATE_CHECKING_FOR_PORTAL,
83 };
84
96 struct DetectionAttemptCompletedReport { 85 struct DetectionAttemptCompletedReport {
97 DetectionAttemptCompletedReport(); 86 DetectionAttemptCompletedReport();
98 87
99 DetectionAttemptCompletedReport(const std::string network_name, 88 DetectionAttemptCompletedReport(const std::string network_name,
100 const std::string network_id, 89 const std::string network_id,
101 captive_portal::CaptivePortalResult result, 90 captive_portal::CaptivePortalResult result,
102 int response_code); 91 int response_code);
103 92
104 void Report() const; 93 void Report() const;
105 94
106 bool Equals(const DetectionAttemptCompletedReport& o) const; 95 bool Equals(const DetectionAttemptCompletedReport& o) const;
107 96
108 std::string network_name; 97 std::string network_name;
109 std::string network_id; 98 std::string network_id;
110 captive_portal::CaptivePortalResult result; 99 captive_portal::CaptivePortalResult result;
111 int response_code; 100 int response_code;
112 }; 101 };
113 102
114 typedef std::string NetworkId;
115 typedef base::hash_map<NetworkId, CaptivePortalState> CaptivePortalStateMap;
116
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,
124 };
125
126 // Starts detection process. 103 // Starts detection process.
127 void StartDetection(); 104 void StartDetection();
128 105
129 // Stops whole detection process. 106 // Stops whole detection process.
130 void StopDetection(); 107 void StopDetection();
131 108
132 // Stops and restarts the detection process. 109 // Stops and restarts the detection process.
133 void RetryDetection(); 110 void RetryDetection();
134 111
135 // Initiates Captive Portal detection attempt after |delay|. 112 // Initiates Captive Portal detection attempt after |delay|.
136 void ScheduleAttempt(const base::TimeDelta& delay); 113 void ScheduleAttempt(const base::TimeDelta& delay);
137 114
138 // Starts detection attempt. 115 // Starts detection attempt.
139 void StartAttempt(); 116 void StartAttempt();
140 117
141 // Called when portal check is timed out. Cancels portal check and calls 118 // Called when portal check is timed out. Cancels portal check and calls
142 // OnPortalDetectionCompleted() with RESULT_NO_RESPONSE as a result. 119 // OnPortalDetectionCompleted() with RESULT_NO_RESPONSE as a result.
143 void OnAttemptTimeout(); 120 void OnAttemptTimeout();
144 121
145 // Called by CaptivePortalDetector when detection attempt completes. 122 // Called by CaptivePortalDetector when detection attempt completes.
146 void OnAttemptCompleted( 123 void OnAttemptCompleted(
147 const captive_portal::CaptivePortalDetector::Results& results); 124 const captive_portal::CaptivePortalDetector::Results& results);
148 125
126 // NetworkPortalDetector implementation:
127 void AddObserver(Observer* observer) override;
128 void AddAndFireObserver(Observer* observer) override;
129 void RemoveObserver(Observer* observer) override;
130 CaptivePortalState GetCaptivePortalState(const std::string& guid) override;
131 bool IsEnabled() override;
132 void Enable(bool start_detection) override;
133 bool StartDetectionIfIdle() override;
134 void SetStrategy(PortalDetectorStrategy::StrategyId id) override;
135 void OnLockScreenRequest() override;
136
137 // NetworkStateHandlerObserver implementation:
138 void DefaultNetworkChanged(const NetworkState* network) override;
139
140 // PortalDetectorStrategy::Delegate implementation:
141 int NoResponseResultCount() override;
142 base::TimeTicks AttemptStartTime() override;
143 base::TimeTicks NowTicks() override;
144
149 // content::NotificationObserver implementation: 145 // content::NotificationObserver implementation:
150 void Observe(int type, 146 void Observe(int type,
151 const content::NotificationSource& source, 147 const content::NotificationSource& source,
152 const content::NotificationDetails& details) override; 148 const content::NotificationDetails& details) override;
153 149
154 // Stores captive portal state for a |network| and notifies observers. 150 // Stores captive portal state for a |network| and notifies observers.
155 void OnDetectionCompleted(const NetworkState* network, 151 void OnDetectionCompleted(const NetworkState* network,
156 const CaptivePortalState& results); 152 const CaptivePortalState& results);
157 153
158 // Notifies observers that portal detection is completed for a |network|. 154 // Notifies observers that portal detection is completed for a |network|.
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 209
214 // Name of the default network. 210 // Name of the default network.
215 std::string default_network_name_; 211 std::string default_network_name_;
216 212
217 // Unique identifier of the default network. 213 // Unique identifier of the default network.
218 std::string default_network_id_; 214 std::string default_network_id_;
219 215
220 // Connection state of the default network. 216 // Connection state of the default network.
221 std::string default_connection_state_; 217 std::string default_connection_state_;
222 218
223 State state_; 219 State state_ = STATE_IDLE;
224 CaptivePortalStateMap portal_state_map_; 220 CaptivePortalStateMap portal_state_map_;
225 base::ObserverList<Observer> observers_; 221 base::ObserverList<Observer> observers_;
226 222
227 base::CancelableClosure attempt_task_; 223 base::CancelableClosure attempt_task_;
228 base::CancelableClosure attempt_timeout_; 224 base::CancelableClosure attempt_timeout_;
229 225
230 // URL that returns a 204 response code when connected to the Internet. 226 // URL that returns a 204 response code when connected to the Internet.
231 GURL test_url_; 227 GURL test_url_;
232 228
233 // Detector for checking default network for a portal state. 229 // Detector for checking default network for a portal state.
234 scoped_ptr<captive_portal::CaptivePortalDetector> captive_portal_detector_; 230 scoped_ptr<captive_portal::CaptivePortalDetector> captive_portal_detector_;
235 231
236 // True if the NetworkPortalDetector is enabled. 232 // True if the NetworkPortalDetector is enabled.
237 bool enabled_; 233 bool enabled_ = false;
238 234
239 // Start time of portal detection. 235 // Start time of portal detection.
240 base::TimeTicks detection_start_time_; 236 base::TimeTicks detection_start_time_;
241 237
242 // Start time of detection attempt. 238 // Start time of detection attempt.
243 base::TimeTicks attempt_start_time_; 239 base::TimeTicks attempt_start_time_;
244 240
245 // Delay before next portal detection. 241 // Delay before next portal detection.
246 base::TimeDelta next_attempt_delay_; 242 base::TimeDelta next_attempt_delay_;
247 243
248 // Current detection strategy. 244 // Current detection strategy.
249 scoped_ptr<PortalDetectorStrategy> strategy_; 245 scoped_ptr<PortalDetectorStrategy> strategy_;
250 246
251 // Last received result from captive portal detector. 247 // Last received result from captive portal detector.
252 CaptivePortalStatus last_detection_result_; 248 CaptivePortalStatus last_detection_result_ = CAPTIVE_PORTAL_STATUS_UNKNOWN;
253 249
254 // Number of detection attempts with same result in a row. 250 // Number of detection attempts with same result in a row.
255 int same_detection_result_count_; 251 int same_detection_result_count_ = 0;
256 252
257 // Number of detection attempts in a row with NO RESPONSE result. 253 // Number of detection attempts in a row with NO RESPONSE result.
258 int no_response_result_count_; 254 int no_response_result_count_ = 0;
259 255
260 // UI notification controller about captive portal state. 256 // UI notification controller about captive portal state.
261 NetworkPortalNotificationController notification_controller_; 257 scoped_ptr<NetworkPortalNotificationController> notification_controller_;
262 258
263 content::NotificationRegistrar registrar_; 259 content::NotificationRegistrar registrar_;
264 260
265 // Test time ticks used by unit tests. 261 // Test time ticks used by unit tests.
266 base::TimeTicks time_ticks_for_testing_; 262 base::TimeTicks time_ticks_for_testing_;
267 263
268 // Contents of a last log message about completed detection attempt. 264 // Contents of a last log message about completed detection attempt.
269 DetectionAttemptCompletedReport attempt_completed_report_; 265 DetectionAttemptCompletedReport attempt_completed_report_;
270 266
271 base::WeakPtrFactory<NetworkPortalDetectorImpl> weak_factory_; 267 base::WeakPtrFactory<NetworkPortalDetectorImpl> weak_factory_;
272 268
273 DISALLOW_COPY_AND_ASSIGN(NetworkPortalDetectorImpl); 269 DISALLOW_COPY_AND_ASSIGN(NetworkPortalDetectorImpl);
274 }; 270 };
275 271
276 } // namespace chromeos 272 } // namespace chromeos
277 273
278 #endif // CHROME_BROWSER_CHROMEOS_NET_NETWORK_PORTAL_DETECTOR_IMPL_H_ 274 #endif // CHROME_BROWSER_CHROMEOS_NET_NETWORK_PORTAL_DETECTOR_IMPL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698