Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / chromeos / net / network_portal_detector.cc
blobf9e384dab816a36da1ff7349b77bc8b55ecc540c
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 #include "chrome/browser/chromeos/net/network_portal_detector.h"
7 #include "base/command_line.h"
8 #include "base/logging.h"
9 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/chromeos/net/network_portal_detector_impl.h"
11 #include "chrome/browser/chromeos/net/network_portal_detector_test_impl.h"
12 #include "chrome/common/chrome_switches.h"
13 #include "chromeos/chromeos_switches.h"
15 namespace chromeos {
17 namespace {
19 const char kCaptivePortalStatusUnknown[] = "Unknown";
20 const char kCaptivePortalStatusOffline[] = "Offline";
21 const char kCaptivePortalStatusOnline[] = "Online";
22 const char kCaptivePortalStatusPortal[] = "Portal";
23 const char kCaptivePortalStatusProxyAuthRequired[] =
24 "Proxy authentication required";
25 const char kCaptivePortalStatusUnrecognized[] = "Unrecognized";
27 NetworkPortalDetector* g_network_portal_detector = NULL;
28 bool g_network_portal_detector_set_for_testing = false;
30 bool IsTestMode() {
31 return CommandLine::ForCurrentProcess()->HasSwitch(::switches::kTestType);
34 // Stub implementation of NetworkPortalDetector.
35 class NetworkPortalDetectorStubImpl : public NetworkPortalDetector {
36 protected:
37 // NetworkPortalDetector implementation:
38 virtual void AddObserver(Observer* /* observer */) OVERRIDE {}
39 virtual void AddAndFireObserver(Observer* observer) OVERRIDE {
40 if (observer)
41 observer->OnPortalDetectionCompleted(NULL, CaptivePortalState());
43 virtual void RemoveObserver(Observer* /* observer */) OVERRIDE {}
44 virtual CaptivePortalState GetCaptivePortalState(
45 const NetworkState* /* network */) OVERRIDE {
46 return CaptivePortalState();
48 virtual bool IsEnabled() OVERRIDE { return false; }
49 virtual void Enable(bool /* start_detection */) OVERRIDE {}
50 virtual bool StartDetectionIfIdle() OVERRIDE { return false; }
51 virtual void EnableLazyDetection() OVERRIDE {}
52 virtual void DisableLazyDetection() OVERRIDE {}
55 } // namespace
57 void NetworkPortalDetector::InitializeForTesting(
58 NetworkPortalDetector* network_portal_detector) {
59 CHECK(!g_network_portal_detector)
60 << "NetworkPortalDetector::InitializeForTesting() is called after "
61 << "Initialize()";
62 CHECK(network_portal_detector);
63 g_network_portal_detector = network_portal_detector;
64 g_network_portal_detector_set_for_testing = true;
67 // static
68 void NetworkPortalDetector::Initialize() {
69 if (g_network_portal_detector_set_for_testing)
70 return;
71 CHECK(!g_network_portal_detector)
72 << "NetworkPortalDetector::Initialize() is called twice";
73 if (IsTestMode()) {
74 g_network_portal_detector = new NetworkPortalDetectorStubImpl();
75 } else {
76 CHECK(g_browser_process);
77 CHECK(g_browser_process->system_request_context());
78 g_network_portal_detector = new NetworkPortalDetectorImpl(
79 g_browser_process->system_request_context());
83 // static
84 void NetworkPortalDetector::Shutdown() {
85 CHECK(g_network_portal_detector || g_network_portal_detector_set_for_testing)
86 << "NetworkPortalDetectorImpl::Shutdown() is called "
87 << "without previous call to Initialize()";
88 if (g_network_portal_detector) {
89 delete g_network_portal_detector;
90 g_network_portal_detector = NULL;
94 // static
95 NetworkPortalDetector* NetworkPortalDetector::Get() {
96 CHECK(g_network_portal_detector)
97 << "NetworkPortalDetector::Get() called before Initialize()";
98 return g_network_portal_detector;
101 // static
102 std::string NetworkPortalDetector::CaptivePortalStatusString(
103 CaptivePortalStatus status) {
104 switch (status) {
105 case NetworkPortalDetectorImpl::CAPTIVE_PORTAL_STATUS_UNKNOWN:
106 return kCaptivePortalStatusUnknown;
107 case NetworkPortalDetectorImpl::CAPTIVE_PORTAL_STATUS_OFFLINE:
108 return kCaptivePortalStatusOffline;
109 case NetworkPortalDetectorImpl::CAPTIVE_PORTAL_STATUS_ONLINE:
110 return kCaptivePortalStatusOnline;
111 case NetworkPortalDetectorImpl::CAPTIVE_PORTAL_STATUS_PORTAL:
112 return kCaptivePortalStatusPortal;
113 case NetworkPortalDetectorImpl::CAPTIVE_PORTAL_STATUS_PROXY_AUTH_REQUIRED:
114 return kCaptivePortalStatusProxyAuthRequired;
115 case NetworkPortalDetectorImpl::CAPTIVE_PORTAL_STATUS_COUNT:
116 NOTREACHED();
118 return kCaptivePortalStatusUnrecognized;
121 } // namespace chromeos