Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / net / network_portal_detector.cc
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[] =
24     "Proxy authentication required";
25 const char kCaptivePortalStatusUnrecognized[] = "Unrecognized";
26
27 NetworkPortalDetector* g_network_portal_detector = NULL;
28 bool g_network_portal_detector_set_for_testing = false;
29
30 bool IsTestMode() {
31   return CommandLine::ForCurrentProcess()->HasSwitch(::switches::kTestType);
32 }
33
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());
42   }
43   virtual void RemoveObserver(Observer* /* observer */) OVERRIDE {}
44   virtual CaptivePortalState GetCaptivePortalState(
45       const std::string& /* service_path */) OVERRIDE {
46     return CaptivePortalState();
47   }
48   virtual bool IsEnabled() OVERRIDE { return false; }
49   virtual void Enable(bool /* start_detection */) OVERRIDE {}
50   virtual bool StartDetectionIfIdle() OVERRIDE { return false; }
51 };
52
53 }  // namespace
54
55 void NetworkPortalDetector::InitializeForTesting(
56     NetworkPortalDetector* network_portal_detector) {
57   if (network_portal_detector) {
58     CHECK(!g_network_portal_detector_set_for_testing)
59         << "NetworkPortalDetector::InitializeForTesting is called twice";
60     CHECK(network_portal_detector);
61     delete g_network_portal_detector;
62     g_network_portal_detector = network_portal_detector;
63     g_network_portal_detector_set_for_testing = true;
64   } else {
65     g_network_portal_detector = NULL;
66     g_network_portal_detector_set_for_testing = false;
67   }
68 }
69
70 // static
71 void NetworkPortalDetector::Initialize() {
72   if (g_network_portal_detector_set_for_testing)
73     return;
74   CHECK(!g_network_portal_detector)
75       << "NetworkPortalDetector::Initialize() is called twice";
76   if (IsTestMode()) {
77     g_network_portal_detector = new NetworkPortalDetectorStubImpl();
78   } else {
79     CHECK(g_browser_process);
80     CHECK(g_browser_process->system_request_context());
81     g_network_portal_detector = new NetworkPortalDetectorImpl(
82         g_browser_process->system_request_context());
83   }
84 }
85
86 // static
87 void NetworkPortalDetector::Shutdown() {
88   CHECK(g_network_portal_detector || g_network_portal_detector_set_for_testing)
89       << "NetworkPortalDetectorImpl::Shutdown() is called "
90       << "without previous call to Initialize()";
91   delete g_network_portal_detector;
92   g_network_portal_detector = NULL;
93 }
94
95 // static
96 NetworkPortalDetector* NetworkPortalDetector::Get() {
97   CHECK(g_network_portal_detector)
98       << "NetworkPortalDetector::Get() called before Initialize()";
99   return g_network_portal_detector;
100 }
101
102 // static
103 std::string NetworkPortalDetector::CaptivePortalStatusString(
104     CaptivePortalStatus status) {
105   switch (status) {
106     case NetworkPortalDetectorImpl::CAPTIVE_PORTAL_STATUS_UNKNOWN:
107       return kCaptivePortalStatusUnknown;
108     case NetworkPortalDetectorImpl::CAPTIVE_PORTAL_STATUS_OFFLINE:
109       return kCaptivePortalStatusOffline;
110     case NetworkPortalDetectorImpl::CAPTIVE_PORTAL_STATUS_ONLINE:
111       return kCaptivePortalStatusOnline;
112     case NetworkPortalDetectorImpl::CAPTIVE_PORTAL_STATUS_PORTAL:
113       return kCaptivePortalStatusPortal;
114     case NetworkPortalDetectorImpl::CAPTIVE_PORTAL_STATUS_PROXY_AUTH_REQUIRED:
115       return kCaptivePortalStatusProxyAuthRequired;
116     case NetworkPortalDetectorImpl::CAPTIVE_PORTAL_STATUS_COUNT:
117       NOTREACHED();
118   }
119   return kCaptivePortalStatusUnrecognized;
120 }
121
122 // static
123 bool NetworkPortalDetector::IsInitialized() {
124   return g_network_portal_detector;
125 }
126
127 }  // namespace chromeos