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

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

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 #include "chrome/browser/chromeos/net/network_portal_detector_impl.h" 5 #include "chrome/browser/chromeos/net/network_portal_detector_impl.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/command_line.h" 10 #include "base/command_line.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/message_loop/message_loop.h" 12 #include "base/message_loop/message_loop.h"
13 #include "base/metrics/histogram.h" 13 #include "base/metrics/histogram.h"
14 #include "base/strings/stringprintf.h" 14 #include "base/strings/stringprintf.h"
15 #include "chrome/browser/chrome_notification_types.h" 15 #include "chrome/browser/chrome_notification_types.h"
16 #include "chrome/browser/chromeos/net/network_portal_notification_controller.h"
16 #include "chromeos/dbus/dbus_thread_manager.h" 17 #include "chromeos/dbus/dbus_thread_manager.h"
17 #include "chromeos/dbus/shill_profile_client.h" 18 #include "chromeos/dbus/shill_profile_client.h"
18 #include "chromeos/login/login_state.h" 19 #include "chromeos/login/login_state.h"
19 #include "chromeos/network/network_state.h" 20 #include "chromeos/network/network_state.h"
20 #include "chromeos/network/network_state_handler.h" 21 #include "chromeos/network/network_state_handler.h"
21 #include "components/device_event_log/device_event_log.h" 22 #include "components/device_event_log/device_event_log.h"
22 #include "content/public/browser/notification_service.h" 23 #include "content/public/browser/notification_service.h"
23 #include "content/public/common/content_switches.h" 24 #include "content/public/common/content_switches.h"
24 #include "net/http/http_status_code.h" 25 #include "net/http/http_status_code.h"
25 #include "third_party/cros_system_api/dbus/service_constants.h" 26 #include "third_party/cros_system_api/dbus/service_constants.h"
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
132 UMA_HISTOGRAM_LONG_TIMES( 133 UMA_HISTOGRAM_LONG_TIMES(
133 NetworkPortalDetectorImpl::kSessionPortalToOnlineHistogram, 134 NetworkPortalDetectorImpl::kSessionPortalToOnlineHistogram,
134 duration); 135 duration);
135 } else { 136 } else {
136 UMA_HISTOGRAM_LONG_TIMES( 137 UMA_HISTOGRAM_LONG_TIMES(
137 NetworkPortalDetectorImpl::kOobePortalToOnlineHistogram, 138 NetworkPortalDetectorImpl::kOobePortalToOnlineHistogram,
138 duration); 139 duration);
139 } 140 }
140 } 141 }
141 142
143 class NetworkPortalDetectorStubImpl : public NetworkPortalDetector {
144 public:
145 NetworkPortalDetectorStubImpl() {}
146 ~NetworkPortalDetectorStubImpl() override {}
147
148 private:
149 // NetworkPortalDetector:
150 void AddObserver(Observer* observer) override {}
151 void AddAndFireObserver(Observer* observer) override {
152 if (observer)
153 observer->OnPortalDetectionCompleted(nullptr, CaptivePortalState());
154 }
155 void RemoveObserver(Observer* observer) override {}
156 CaptivePortalState GetCaptivePortalState(
157 const std::string& service_path) override {
158 return CaptivePortalState();
159 }
160 bool IsEnabled() override { return false; }
161 void Enable(bool start_detection) override {}
162 bool StartDetectionIfIdle() override { return false; }
163 void SetStrategy(PortalDetectorStrategy::StrategyId id) override {}
164 void OnLockScreenRequest() override {}
165
166 DISALLOW_COPY_AND_ASSIGN(NetworkPortalDetectorStubImpl);
167 };
168
142 } // namespace 169 } // namespace
143 170
144 //////////////////////////////////////////////////////////////////////////////// 171 ////////////////////////////////////////////////////////////////////////////////
145 // NetworkPortalDetectorImpl::DetectionAttemptCompletedLogState 172 // NetworkPortalDetectorImpl::DetectionAttemptCompletedLogState
146 173
147 NetworkPortalDetectorImpl::DetectionAttemptCompletedReport:: 174 NetworkPortalDetectorImpl::DetectionAttemptCompletedReport::
148 DetectionAttemptCompletedReport() 175 DetectionAttemptCompletedReport()
149 : result(captive_portal::RESULT_COUNT), response_code(-1) { 176 : result(captive_portal::RESULT_COUNT), response_code(-1) {
150 } 177 }
151 178
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
200 const char NetworkPortalDetectorImpl::kSessionShillOnlineHistogram[] = 227 const char NetworkPortalDetectorImpl::kSessionShillOnlineHistogram[] =
201 "CaptivePortal.Session.DiscrepancyWithShill_Online"; 228 "CaptivePortal.Session.DiscrepancyWithShill_Online";
202 const char NetworkPortalDetectorImpl::kSessionShillPortalHistogram[] = 229 const char NetworkPortalDetectorImpl::kSessionShillPortalHistogram[] =
203 "CaptivePortal.Session.DiscrepancyWithShill_RestrictedPool"; 230 "CaptivePortal.Session.DiscrepancyWithShill_RestrictedPool";
204 const char NetworkPortalDetectorImpl::kSessionShillOfflineHistogram[] = 231 const char NetworkPortalDetectorImpl::kSessionShillOfflineHistogram[] =
205 "CaptivePortal.Session.DiscrepancyWithShill_Offline"; 232 "CaptivePortal.Session.DiscrepancyWithShill_Offline";
206 const char NetworkPortalDetectorImpl::kSessionPortalToOnlineHistogram[] = 233 const char NetworkPortalDetectorImpl::kSessionPortalToOnlineHistogram[] =
207 "CaptivePortal.Session.PortalToOnlineTransition"; 234 "CaptivePortal.Session.PortalToOnlineTransition";
208 235
209 // static 236 // static
210 void NetworkPortalDetectorImpl::Initialize( 237 void NetworkPortalDetector::Initialize(
stevenjb 2015/09/17 20:45:52 I don't feel strongly about this, but I think this
achuithb 2015/09/17 22:03:32 I can move the stub impl to it's own file, but it'
stevenjb 2015/09/18 22:04:09 We can't implement a NetworkPortalDetector method
achuithb 2015/09/22 17:37:23 ok, I've created the stub files and reverted these
211 net::URLRequestContextGetter* url_context) { 238 net::URLRequestContextGetter* url_context) {
212 if (NetworkPortalDetector::set_for_testing()) 239 if (set_for_testing_)
213 return; 240 return;
214 CHECK(!NetworkPortalDetector::network_portal_detector()) 241 CHECK(!network_portal_detector_)
215 << "NetworkPortalDetector was initialized twice."; 242 << "NetworkPortalDetector was initialized twice.";
216 NET_LOG(EVENT) << "NetworkPortalDetectorImpl::Initialize()"; 243 NET_LOG(EVENT) << "NetworkPortalDetectorImpl::Initialize()";
217 if (base::CommandLine::ForCurrentProcess()->HasSwitch(::switches::kTestType)) 244 if (base::CommandLine::ForCurrentProcess()->HasSwitch(::switches::kTestType))
218 set_network_portal_detector(new NetworkPortalDetectorStubImpl()); 245 network_portal_detector_ = new NetworkPortalDetectorStubImpl();
219 else 246 else
220 set_network_portal_detector(new NetworkPortalDetectorImpl(url_context)); 247 network_portal_detector_ = new NetworkPortalDetectorImpl(url_context, true);
221 } 248 }
222 249
223 NetworkPortalDetectorImpl::NetworkPortalDetectorImpl( 250 NetworkPortalDetectorImpl::NetworkPortalDetectorImpl(
224 const scoped_refptr<net::URLRequestContextGetter>& request_context) 251 const scoped_refptr<net::URLRequestContextGetter>& request_context,
225 : state_(STATE_IDLE), 252 bool create_notification_controller)
226 test_url_(CaptivePortalDetector::kDefaultURL), 253 : test_url_(CaptivePortalDetector::kDefaultURL),
227 enabled_(false),
228 strategy_(PortalDetectorStrategy::CreateById( 254 strategy_(PortalDetectorStrategy::CreateById(
229 PortalDetectorStrategy::STRATEGY_ID_LOGIN_SCREEN, 255 PortalDetectorStrategy::STRATEGY_ID_LOGIN_SCREEN,
230 this)), 256 this)),
231 last_detection_result_(CAPTIVE_PORTAL_STATUS_UNKNOWN),
232 same_detection_result_count_(0),
233 no_response_result_count_(0),
234 weak_factory_(this) { 257 weak_factory_(this) {
235 NET_LOG(EVENT) << "NetworkPortalDetectorImpl::NetworkPortalDetectorImpl()"; 258 NET_LOG(EVENT) << "NetworkPortalDetectorImpl::NetworkPortalDetectorImpl()";
236 captive_portal_detector_.reset(new CaptivePortalDetector(request_context)); 259 captive_portal_detector_.reset(new CaptivePortalDetector(request_context));
237 260
238 notification_controller_.set_retry_detection_callback(base::Bind( 261 if (create_notification_controller) {
239 &NetworkPortalDetectorImpl::RetryDetection, base::Unretained(this))); 262 notification_controller_.reset(
263 new NetworkPortalNotificationController(this));
264 notification_controller_->set_retry_detection_callback(
265 base::Bind(&NetworkPortalDetectorImpl::RetryDetection,
266 weak_factory_.GetWeakPtr()));
267 }
240 268
241 registrar_.Add(this, 269 registrar_.Add(this,
242 chrome::NOTIFICATION_LOGIN_PROXY_CHANGED, 270 chrome::NOTIFICATION_LOGIN_PROXY_CHANGED,
243 content::NotificationService::AllSources()); 271 content::NotificationService::AllSources());
244 registrar_.Add(this, 272 registrar_.Add(this,
245 chrome::NOTIFICATION_AUTH_SUPPLIED, 273 chrome::NOTIFICATION_AUTH_SUPPLIED,
246 content::NotificationService::AllSources()); 274 content::NotificationService::AllSources());
247 registrar_.Add(this, 275 registrar_.Add(this,
248 chrome::NOTIFICATION_AUTH_CANCELLED, 276 chrome::NOTIFICATION_AUTH_CANCELLED,
249 content::NotificationService::AllSources()); 277 content::NotificationService::AllSources());
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
330 void NetworkPortalDetectorImpl::SetStrategy( 358 void NetworkPortalDetectorImpl::SetStrategy(
331 PortalDetectorStrategy::StrategyId id) { 359 PortalDetectorStrategy::StrategyId id) {
332 if (id == strategy_->Id()) 360 if (id == strategy_->Id())
333 return; 361 return;
334 strategy_ = PortalDetectorStrategy::CreateById(id, this).Pass(); 362 strategy_ = PortalDetectorStrategy::CreateById(id, this).Pass();
335 StopDetection(); 363 StopDetection();
336 StartDetectionIfIdle(); 364 StartDetectionIfIdle();
337 } 365 }
338 366
339 void NetworkPortalDetectorImpl::OnLockScreenRequest() { 367 void NetworkPortalDetectorImpl::OnLockScreenRequest() {
340 notification_controller_.CloseDialog(); 368 if (notification_controller_.get())
stevenjb 2015/09/17 20:45:52 nit: if (notification_controller_)
achuithb 2015/09/17 22:03:32 Done.
369 notification_controller_->CloseDialog();
341 } 370 }
342 371
343 void NetworkPortalDetectorImpl::DefaultNetworkChanged( 372 void NetworkPortalDetectorImpl::DefaultNetworkChanged(
344 const NetworkState* default_network) { 373 const NetworkState* default_network) {
345 DCHECK(CalledOnValidThread()); 374 DCHECK(CalledOnValidThread());
346 375
347 notification_controller_.DefaultNetworkChanged(default_network);
348 if (!default_network) { 376 if (!default_network) {
349 NET_LOG(EVENT) << "Default network changed: None"; 377 NET_LOG(EVENT) << "Default network changed: None";
350 378
351 default_network_name_.clear(); 379 default_network_name_.clear();
352 380
353 StopDetection(); 381 StopDetection();
354 382
355 CaptivePortalState state; 383 CaptivePortalState state;
356 state.status = CAPTIVE_PORTAL_STATUS_OFFLINE; 384 state.status = CAPTIVE_PORTAL_STATUS_OFFLINE;
357 OnDetectionCompleted(NULL, state); 385 OnDetectionCompleted(nullptr, state);
358 return; 386 return;
359 } 387 }
360 388
361 default_network_name_ = default_network->name(); 389 default_network_name_ = default_network->name();
362 390
363 bool network_changed = (default_network_id_ != default_network->guid()); 391 bool network_changed = (default_network_id_ != default_network->guid());
364 default_network_id_ = default_network->guid(); 392 default_network_id_ = default_network->guid();
365 393
366 bool connection_state_changed = 394 bool connection_state_changed =
367 (default_connection_state_ != default_network->connection_state()); 395 (default_connection_state_ != default_network->connection_state());
(...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after
601 portal_state_map_[network->guid()] = state; 629 portal_state_map_[network->guid()] = state;
602 } 630 }
603 NotifyDetectionCompleted(network, state); 631 NotifyDetectionCompleted(network, state);
604 } 632 }
605 633
606 void NetworkPortalDetectorImpl::NotifyDetectionCompleted( 634 void NetworkPortalDetectorImpl::NotifyDetectionCompleted(
607 const NetworkState* network, 635 const NetworkState* network,
608 const CaptivePortalState& state) { 636 const CaptivePortalState& state) {
609 FOR_EACH_OBSERVER( 637 FOR_EACH_OBSERVER(
610 Observer, observers_, OnPortalDetectionCompleted(network, state)); 638 Observer, observers_, OnPortalDetectionCompleted(network, state));
611 notification_controller_.OnPortalDetectionCompleted(network, state);
612 } 639 }
613 640
614 bool NetworkPortalDetectorImpl::AttemptTimeoutIsCancelledForTesting() const { 641 bool NetworkPortalDetectorImpl::AttemptTimeoutIsCancelledForTesting() const {
615 return attempt_timeout_.IsCancelled(); 642 return attempt_timeout_.IsCancelled();
616 } 643 }
617 644
618 void NetworkPortalDetectorImpl::RecordDetectionStats( 645 void NetworkPortalDetectorImpl::RecordDetectionStats(
619 const NetworkState* network, 646 const NetworkState* network,
620 CaptivePortalStatus status) { 647 CaptivePortalStatus status) {
621 // Don't record stats for offline state. 648 // Don't record stats for offline state.
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
653 } 680 }
654 681
655 void NetworkPortalDetectorImpl::ResetStrategyAndCounters() { 682 void NetworkPortalDetectorImpl::ResetStrategyAndCounters() {
656 last_detection_result_ = CAPTIVE_PORTAL_STATUS_UNKNOWN; 683 last_detection_result_ = CAPTIVE_PORTAL_STATUS_UNKNOWN;
657 same_detection_result_count_ = 0; 684 same_detection_result_count_ = 0;
658 no_response_result_count_ = 0; 685 no_response_result_count_ = 0;
659 strategy_->Reset(); 686 strategy_->Reset();
660 } 687 }
661 688
662 } // namespace chromeos 689 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698