Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / chromeos / net / network_portal_detector.h
blob8bc55c0a18adc6f2ea9d011869f6f2376ccf0a7c
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 #ifndef CHROME_BROWSER_CHROMEOS_NET_NETWORK_PORTAL_DETECTOR_H_
6 #define CHROME_BROWSER_CHROMEOS_NET_NETWORK_PORTAL_DETECTOR_H_
8 #include "base/basictypes.h"
9 #include "net/url_request/url_fetcher.h"
11 namespace chromeos {
13 class NetworkState;
15 // This class handles all notifications about network changes from
16 // NetworkStateHandler and delegates portal detection for the active
17 // network to CaptivePortalService.
18 class NetworkPortalDetector {
19 public:
20 enum CaptivePortalStatus {
21 CAPTIVE_PORTAL_STATUS_UNKNOWN = 0,
22 CAPTIVE_PORTAL_STATUS_OFFLINE = 1,
23 CAPTIVE_PORTAL_STATUS_ONLINE = 2,
24 CAPTIVE_PORTAL_STATUS_PORTAL = 3,
25 CAPTIVE_PORTAL_STATUS_PROXY_AUTH_REQUIRED = 4,
26 CAPTIVE_PORTAL_STATUS_COUNT
29 struct CaptivePortalState {
30 CaptivePortalState()
31 : status(CAPTIVE_PORTAL_STATUS_UNKNOWN),
32 response_code(net::URLFetcher::RESPONSE_CODE_INVALID) {
35 bool operator==(const CaptivePortalState& o) const {
36 return status == o.status && response_code == o.response_code;
39 CaptivePortalStatus status;
40 int response_code;
43 class Observer {
44 public:
45 // Called when portal detection is completed for |network|, or
46 // when observers add themselves via AddAndFireObserver(). In the
47 // second case, |network| is the active network and |state| is a
48 // current portal state for the active network, which can be
49 // currently in the unknown state, for instance, if portal
50 // detection is in process for the active network. Note, that
51 // |network| may be NULL.
52 virtual void OnPortalDetectionCompleted(
53 const NetworkState* network,
54 const CaptivePortalState& state) = 0;
56 protected:
57 virtual ~Observer() {}
60 // Adds |observer| to the observers list.
61 virtual void AddObserver(Observer* observer) = 0;
63 // Adds |observer| to the observers list and immediately calls
64 // OnPortalDetectionCompleted() with the active network (which may
65 // be NULL) and captive portal state for the active network (which
66 // may be unknown, if, for instance, portal detection is in process
67 // for the active network).
69 // WARNING: don't call this method from the Observer's ctors or
70 // dtors, as it implicitly calls OnPortalDetectionCompleted(), which
71 // is virtual.
72 // TODO (ygorshenin@): find a way to avoid this restriction.
73 virtual void AddAndFireObserver(Observer* observer) = 0;
75 // Removes |observer| from the observers list.
76 virtual void RemoveObserver(Observer* observer) = 0;
78 // Returns Captive Portal state for a given |network|.
79 virtual CaptivePortalState GetCaptivePortalState(
80 const chromeos::NetworkState* network) = 0;
82 // Returns true if portal detection is enabled.
83 virtual bool IsEnabled() = 0;
85 // Enable portal detection. This method is needed because we can't
86 // check current network for portal state unless user accepts EULA.
87 // If |start_detection| is true and NetworkPortalDetector was
88 // disabled previously, portal detection for the active network is
89 // initiated by this method.
90 virtual void Enable(bool start_detection) = 0;
92 // Restarts portal detection for the default network if currently in
93 // the idle state. Returns true if new portal detection attempt was
94 // started.
95 virtual bool StartDetectionIfIdle() = 0;
97 // Enables lazy detection mode. In this mode portal detection after
98 // first 3 consecutive attemps will be performed once in 5 seconds.
99 virtual void EnableLazyDetection() = 0;
101 // Dizables lazy detection mode.
102 virtual void DisableLazyDetection() = 0;
104 // Initializes network portal detector for testing. The
105 // |network_portal_detector| will be owned by the internal pointer
106 // and deleted by Shutdown().
107 static void InitializeForTesting(
108 NetworkPortalDetector* network_portal_detector);
110 // Creates an instance of the NetworkPortalDetector.
111 static void Initialize();
113 // Deletes the instance of the NetworkPortalDetector.
114 static void Shutdown();
116 // Gets the instance of the NetworkPortalDetector.
117 static NetworkPortalDetector* Get();
119 // Returns non-localized string representation of |status|.
120 static std::string CaptivePortalStatusString(CaptivePortalStatus status);
122 protected:
123 NetworkPortalDetector() {}
124 virtual ~NetworkPortalDetector() {}
126 private:
127 DISALLOW_COPY_AND_ASSIGN(NetworkPortalDetector);
130 } // namespace chromeos
132 #endif // CHROME_BROWSER_CHROMEOS_NET_NETWORK_PORTAL_DETECTOR_H_