• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "chrome/browser/chromeos/net/network_portal_detector.h"
6 
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"
14 
15 namespace chromeos {
16 
17 namespace {
18 
19 const char kCaptivePortalStatusUnknown[] = "Unknown";
20 const char kCaptivePortalStatusOffline[] = "Offline";
21 const char kCaptivePortalStatusOnline[]  = "Online";
22 const char kCaptivePortalStatusPortal[]  = "Portal";
23 const char kCaptivePortalStatusProxyAuthRequired[] = "ProxyAuthRequired";
24 const char kCaptivePortalStatusUnrecognized[] = "Unrecognized";
25 
26 NetworkPortalDetector* g_network_portal_detector = NULL;
27 bool g_network_portal_detector_set_for_testing = false;
28 
IsTestMode()29 bool IsTestMode() {
30   return CommandLine::ForCurrentProcess()->HasSwitch(::switches::kTestType);
31 }
32 
33 // Stub implementation of NetworkPortalDetector.
34 class NetworkPortalDetectorStubImpl : public NetworkPortalDetector {
35  protected:
36   // NetworkPortalDetector implementation:
AddObserver(Observer *)37   virtual void AddObserver(Observer* /* observer */) OVERRIDE {}
AddAndFireObserver(Observer * observer)38   virtual void AddAndFireObserver(Observer* observer) OVERRIDE {
39     if (observer)
40       observer->OnPortalDetectionCompleted(NULL, CaptivePortalState());
41   }
RemoveObserver(Observer *)42   virtual void RemoveObserver(Observer* /* observer */) OVERRIDE {}
GetCaptivePortalState(const std::string &)43   virtual CaptivePortalState GetCaptivePortalState(
44       const std::string& /* service_path */) OVERRIDE {
45     return CaptivePortalState();
46   }
IsEnabled()47   virtual bool IsEnabled() OVERRIDE { return false; }
Enable(bool)48   virtual void Enable(bool /* start_detection */) OVERRIDE {}
StartDetectionIfIdle()49   virtual bool StartDetectionIfIdle() OVERRIDE { return false; }
SetStrategy(PortalDetectorStrategy::StrategyId)50   virtual void SetStrategy(
51       PortalDetectorStrategy::StrategyId /* id */) OVERRIDE {}
52 };
53 
54 }  // namespace
55 
InitializeForTesting(NetworkPortalDetector * network_portal_detector)56 void NetworkPortalDetector::InitializeForTesting(
57     NetworkPortalDetector* network_portal_detector) {
58   if (network_portal_detector) {
59     CHECK(!g_network_portal_detector_set_for_testing)
60         << "NetworkPortalDetector::InitializeForTesting is called twice";
61     CHECK(network_portal_detector);
62     delete g_network_portal_detector;
63     g_network_portal_detector = network_portal_detector;
64     g_network_portal_detector_set_for_testing = true;
65   } else {
66     g_network_portal_detector = NULL;
67     g_network_portal_detector_set_for_testing = false;
68   }
69 }
70 
71 // static
Initialize()72 void NetworkPortalDetector::Initialize() {
73   if (g_network_portal_detector_set_for_testing)
74     return;
75   CHECK(!g_network_portal_detector)
76       << "NetworkPortalDetector::Initialize() is called twice";
77   if (IsTestMode()) {
78     g_network_portal_detector = new NetworkPortalDetectorStubImpl();
79   } else {
80     CHECK(g_browser_process);
81     CHECK(g_browser_process->system_request_context());
82     g_network_portal_detector = new NetworkPortalDetectorImpl(
83         g_browser_process->system_request_context());
84   }
85 }
86 
87 // static
Shutdown()88 void NetworkPortalDetector::Shutdown() {
89   CHECK(g_network_portal_detector || g_network_portal_detector_set_for_testing)
90       << "NetworkPortalDetectorImpl::Shutdown() is called "
91       << "without previous call to Initialize()";
92   delete g_network_portal_detector;
93   g_network_portal_detector = NULL;
94 }
95 
96 // static
Get()97 NetworkPortalDetector* NetworkPortalDetector::Get() {
98   CHECK(g_network_portal_detector)
99       << "NetworkPortalDetector::Get() called before Initialize()";
100   return g_network_portal_detector;
101 }
102 
103 // static
CaptivePortalStatusString(CaptivePortalStatus status)104 std::string NetworkPortalDetector::CaptivePortalStatusString(
105     CaptivePortalStatus status) {
106   switch (status) {
107     case NetworkPortalDetectorImpl::CAPTIVE_PORTAL_STATUS_UNKNOWN:
108       return kCaptivePortalStatusUnknown;
109     case NetworkPortalDetectorImpl::CAPTIVE_PORTAL_STATUS_OFFLINE:
110       return kCaptivePortalStatusOffline;
111     case NetworkPortalDetectorImpl::CAPTIVE_PORTAL_STATUS_ONLINE:
112       return kCaptivePortalStatusOnline;
113     case NetworkPortalDetectorImpl::CAPTIVE_PORTAL_STATUS_PORTAL:
114       return kCaptivePortalStatusPortal;
115     case NetworkPortalDetectorImpl::CAPTIVE_PORTAL_STATUS_PROXY_AUTH_REQUIRED:
116       return kCaptivePortalStatusProxyAuthRequired;
117     case NetworkPortalDetectorImpl::CAPTIVE_PORTAL_STATUS_COUNT:
118       NOTREACHED();
119   }
120   return kCaptivePortalStatusUnrecognized;
121 }
122 
123 // static
IsInitialized()124 bool NetworkPortalDetector::IsInitialized() {
125   return g_network_portal_detector;
126 }
127 
128 }  // namespace chromeos
129