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"
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 {
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 {
31 : status(CAPTIVE_PORTAL_STATUS_UNKNOWN),
32 response_code(net::URLFetcher::RESPONSE_CODE_INVALID) {
35 CaptivePortalStatus status;
41 // Called when portal detection is completed for |network|, or
42 // when observers add themselves via AddAndFireObserver(). In the
43 // second case, |network| is the active network and |state| is a
44 // current portal state for the active network, which can be
45 // currently in the unknown state, for instance, if portal
46 // detection is in process for the active network. Note, that
47 // |network| may be NULL.
48 virtual void OnPortalDetectionCompleted(
49 const NetworkState* network,
50 const CaptivePortalState& state) = 0;
53 virtual ~Observer() {}
56 // Adds |observer| to the observers list.
57 virtual void AddObserver(Observer* observer) = 0;
59 // Adds |observer| to the observers list and immediately calls
60 // OnPortalDetectionCompleted() with the active network (which may
61 // be NULL) and captive portal state for the active network (which
62 // may be unknown, if, for instance, portal detection is in process
63 // for the active network).
65 // WARNING: don't call this method from the Observer's ctors or
66 // dtors, as it implicitly calls OnPortalDetectionCompleted(), which
68 // TODO (ygorshenin@): find a way to avoid this restriction.
69 virtual void AddAndFireObserver(Observer* observer) = 0;
71 // Removes |observer| from the observers list.
72 virtual void RemoveObserver(Observer* observer) = 0;
74 // Returns Captive Portal state for a given |network|.
75 virtual CaptivePortalState GetCaptivePortalState(
76 const chromeos::NetworkState* network) = 0;
78 // Returns true if portal detection is enabled.
79 virtual bool IsEnabled() = 0;
81 // Enable portal detection. This method is needed because we can't
82 // check current network for portal state unless user accepts EULA.
83 // If |start_detection| is true and NetworkPortalDetector was
84 // disabled previously, portal detection for the active network is
85 // initiated by this method.
86 virtual void Enable(bool start_detection) = 0;
88 // Restarts portal detection for the default network if currently in
89 // the idle state. Returns true if new portal detection attempt was
91 virtual bool StartDetectionIfIdle() = 0;
93 // Enables lazy detection mode. In this mode portal detection after
94 // first 3 consecutive attemps will be performed once in 5 seconds.
95 virtual void EnableLazyDetection() = 0;
97 // Dizables lazy detection mode.
98 virtual void DisableLazyDetection() = 0;
100 // Initializes network portal detector for testing. The
101 // |network_portal_detector| will be owned by the internal pointer
102 // and deleted by Shutdown().
103 static void InitializeForTesting(
104 NetworkPortalDetector* network_portal_detector);
106 // Creates an instance of the NetworkPortalDetector.
107 static void Initialize();
109 // Deletes the instance of the NetworkPortalDetector.
110 static void Shutdown();
112 // Gets the instance of the NetworkPortalDetector.
113 static NetworkPortalDetector* Get();
116 NetworkPortalDetector() {}
117 virtual ~NetworkPortalDetector() {}
120 DISALLOW_COPY_AND_ASSIGN(NetworkPortalDetector);
123 } // namespace chromeos
125 #endif // CHROME_BROWSER_CHROMEOS_NET_NETWORK_PORTAL_DETECTOR_H_