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

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: stub 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 // Creates an instance of the implementation or a stub.
66 static void Initialize(net::URLRequestContextGetter* url_context); 66 static void Initialize(net::URLRequestContextGetter* url_context);
achuithb 2015/09/22 17:37:23 The problem is I need to make this go away since i
stevenjb 2015/09/22 17:41:25 I'm not sure I understand the problem. It doesn't
achuithb 2015/09/22 17:43:15 Acknowledged.
67 67
68 explicit NetworkPortalDetectorImpl( 68 NetworkPortalDetectorImpl(
69 const scoped_refptr<net::URLRequestContextGetter>& request_context); 69 const scoped_refptr<net::URLRequestContextGetter>& request_context,
70 bool create_notification_controller);
70 ~NetworkPortalDetectorImpl() override; 71 ~NetworkPortalDetectorImpl() override;
71 72
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: 73 private:
92 friend class ::NetworkingConfigTest; 74 friend class ::NetworkingConfigTest;
93 friend class NetworkPortalDetectorImplTest; 75 friend class NetworkPortalDetectorImplTest;
94 friend class NetworkPortalDetectorImplBrowserTest; 76 friend class NetworkPortalDetectorImplBrowserTest;
95 77
78 using CaptivePortalStateMap = base::hash_map<std::string, CaptivePortalState>;
79
80 enum State {
81 // No portal check is running.
82 STATE_IDLE = 0,
83 // Waiting for portal check.
84 STATE_PORTAL_CHECK_PENDING,
85 // Portal check is in progress.
86 STATE_CHECKING_FOR_PORTAL,
87 };
88
96 struct DetectionAttemptCompletedReport { 89 struct DetectionAttemptCompletedReport {
97 DetectionAttemptCompletedReport(); 90 DetectionAttemptCompletedReport();
98 91
99 DetectionAttemptCompletedReport(const std::string network_name, 92 DetectionAttemptCompletedReport(const std::string network_name,
100 const std::string network_id, 93 const std::string network_id,
101 captive_portal::CaptivePortalResult result, 94 captive_portal::CaptivePortalResult result,
102 int response_code); 95 int response_code);
103 96
104 void Report() const; 97 void Report() const;
105 98
106 bool Equals(const DetectionAttemptCompletedReport& o) const; 99 bool Equals(const DetectionAttemptCompletedReport& o) const;
107 100
108 std::string network_name; 101 std::string network_name;
109 std::string network_id; 102 std::string network_id;
110 captive_portal::CaptivePortalResult result; 103 captive_portal::CaptivePortalResult result;
111 int response_code; 104 int response_code;
112 }; 105 };
113 106
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. 107 // Starts detection process.
127 void StartDetection(); 108 void StartDetection();
128 109
129 // Stops whole detection process. 110 // Stops whole detection process.
130 void StopDetection(); 111 void StopDetection();
131 112
132 // Stops and restarts the detection process. 113 // Stops and restarts the detection process.
133 void RetryDetection(); 114 void RetryDetection();
134 115
135 // Initiates Captive Portal detection attempt after |delay|. 116 // Initiates Captive Portal detection attempt after |delay|.
136 void ScheduleAttempt(const base::TimeDelta& delay); 117 void ScheduleAttempt(const base::TimeDelta& delay);
137 118
138 // Starts detection attempt. 119 // Starts detection attempt.
139 void StartAttempt(); 120 void StartAttempt();
140 121
141 // Called when portal check is timed out. Cancels portal check and calls 122 // Called when portal check is timed out. Cancels portal check and calls
142 // OnPortalDetectionCompleted() with RESULT_NO_RESPONSE as a result. 123 // OnPortalDetectionCompleted() with RESULT_NO_RESPONSE as a result.
143 void OnAttemptTimeout(); 124 void OnAttemptTimeout();
144 125
145 // Called by CaptivePortalDetector when detection attempt completes. 126 // Called by CaptivePortalDetector when detection attempt completes.
146 void OnAttemptCompleted( 127 void OnAttemptCompleted(
147 const captive_portal::CaptivePortalDetector::Results& results); 128 const captive_portal::CaptivePortalDetector::Results& results);
148 129
130 // NetworkPortalDetector implementation:
131 void AddObserver(Observer* observer) override;
132 void AddAndFireObserver(Observer* observer) override;
133 void RemoveObserver(Observer* observer) override;
134 CaptivePortalState GetCaptivePortalState(const std::string& guid) override;
135 bool IsEnabled() override;
136 void Enable(bool start_detection) override;
137 bool StartDetectionIfIdle() override;
138 void SetStrategy(PortalDetectorStrategy::StrategyId id) override;
139 void OnLockScreenRequest() override;
140
141 // NetworkStateHandlerObserver implementation:
142 void DefaultNetworkChanged(const NetworkState* network) override;
143
144 // PortalDetectorStrategy::Delegate implementation:
145 int NoResponseResultCount() override;
146 base::TimeTicks AttemptStartTime() override;
147 base::TimeTicks NowTicks() override;
148
149 // content::NotificationObserver implementation: 149 // content::NotificationObserver implementation:
150 void Observe(int type, 150 void Observe(int type,
151 const content::NotificationSource& source, 151 const content::NotificationSource& source,
152 const content::NotificationDetails& details) override; 152 const content::NotificationDetails& details) override;
153 153
154 // Stores captive portal state for a |network| and notifies observers. 154 // Stores captive portal state for a |network| and notifies observers.
155 void OnDetectionCompleted(const NetworkState* network, 155 void OnDetectionCompleted(const NetworkState* network,
156 const CaptivePortalState& results); 156 const CaptivePortalState& results);
157 157
158 // Notifies observers that portal detection is completed for a |network|. 158 // Notifies observers that portal detection is completed for a |network|.
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 213
214 // Name of the default network. 214 // Name of the default network.
215 std::string default_network_name_; 215 std::string default_network_name_;
216 216
217 // Unique identifier of the default network. 217 // Unique identifier of the default network.
218 std::string default_network_id_; 218 std::string default_network_id_;
219 219
220 // Connection state of the default network. 220 // Connection state of the default network.
221 std::string default_connection_state_; 221 std::string default_connection_state_;
222 222
223 State state_; 223 State state_ = STATE_IDLE;
224 CaptivePortalStateMap portal_state_map_; 224 CaptivePortalStateMap portal_state_map_;
225 base::ObserverList<Observer> observers_; 225 base::ObserverList<Observer> observers_;
226 226
227 base::CancelableClosure attempt_task_; 227 base::CancelableClosure attempt_task_;
228 base::CancelableClosure attempt_timeout_; 228 base::CancelableClosure attempt_timeout_;
229 229
230 // URL that returns a 204 response code when connected to the Internet. 230 // URL that returns a 204 response code when connected to the Internet.
231 GURL test_url_; 231 GURL test_url_;
232 232
233 // Detector for checking default network for a portal state. 233 // Detector for checking default network for a portal state.
234 scoped_ptr<captive_portal::CaptivePortalDetector> captive_portal_detector_; 234 scoped_ptr<captive_portal::CaptivePortalDetector> captive_portal_detector_;
235 235
236 // True if the NetworkPortalDetector is enabled. 236 // True if the NetworkPortalDetector is enabled.
237 bool enabled_; 237 bool enabled_ = false;
238 238
239 // Start time of portal detection. 239 // Start time of portal detection.
240 base::TimeTicks detection_start_time_; 240 base::TimeTicks detection_start_time_;
241 241
242 // Start time of detection attempt. 242 // Start time of detection attempt.
243 base::TimeTicks attempt_start_time_; 243 base::TimeTicks attempt_start_time_;
244 244
245 // Delay before next portal detection. 245 // Delay before next portal detection.
246 base::TimeDelta next_attempt_delay_; 246 base::TimeDelta next_attempt_delay_;
247 247
248 // Current detection strategy. 248 // Current detection strategy.
249 scoped_ptr<PortalDetectorStrategy> strategy_; 249 scoped_ptr<PortalDetectorStrategy> strategy_;
250 250
251 // Last received result from captive portal detector. 251 // Last received result from captive portal detector.
252 CaptivePortalStatus last_detection_result_; 252 CaptivePortalStatus last_detection_result_ = CAPTIVE_PORTAL_STATUS_UNKNOWN;
253 253
254 // Number of detection attempts with same result in a row. 254 // Number of detection attempts with same result in a row.
255 int same_detection_result_count_; 255 int same_detection_result_count_ = 0;
256 256
257 // Number of detection attempts in a row with NO RESPONSE result. 257 // Number of detection attempts in a row with NO RESPONSE result.
258 int no_response_result_count_; 258 int no_response_result_count_ = 0;
259 259
260 // UI notification controller about captive portal state. 260 // UI notification controller about captive portal state.
261 NetworkPortalNotificationController notification_controller_; 261 scoped_ptr<NetworkPortalNotificationController> notification_controller_;
262 262
263 content::NotificationRegistrar registrar_; 263 content::NotificationRegistrar registrar_;
264 264
265 // Test time ticks used by unit tests. 265 // Test time ticks used by unit tests.
266 base::TimeTicks time_ticks_for_testing_; 266 base::TimeTicks time_ticks_for_testing_;
267 267
268 // Contents of a last log message about completed detection attempt. 268 // Contents of a last log message about completed detection attempt.
269 DetectionAttemptCompletedReport attempt_completed_report_; 269 DetectionAttemptCompletedReport attempt_completed_report_;
270 270
271 base::WeakPtrFactory<NetworkPortalDetectorImpl> weak_factory_; 271 base::WeakPtrFactory<NetworkPortalDetectorImpl> weak_factory_;
272 272
273 DISALLOW_COPY_AND_ASSIGN(NetworkPortalDetectorImpl); 273 DISALLOW_COPY_AND_ASSIGN(NetworkPortalDetectorImpl);
274 }; 274 };
275 275
276 } // namespace chromeos 276 } // namespace chromeos
277 277
278 #endif // CHROME_BROWSER_CHROMEOS_NET_NETWORK_PORTAL_DETECTOR_IMPL_H_ 278 #endif // CHROME_BROWSER_CHROMEOS_NET_NETWORK_PORTAL_DETECTOR_IMPL_H_
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/chrome_browser_main_chromeos.cc ('k') | chrome/browser/chromeos/net/network_portal_detector_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698