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 "chrome/browser/chromeos/login/screens/error_screen.h"
10 #include "net/url_request/url_fetcher.h"
16 // This class handles all notifications about network changes from
17 // NetworkStateHandler and delegates portal detection for the active
18 // network to CaptivePortalService.
19 class NetworkPortalDetector
: public ErrorScreen::Observer
{
21 enum CaptivePortalStatus
{
22 CAPTIVE_PORTAL_STATUS_UNKNOWN
= 0,
23 CAPTIVE_PORTAL_STATUS_OFFLINE
= 1,
24 CAPTIVE_PORTAL_STATUS_ONLINE
= 2,
25 CAPTIVE_PORTAL_STATUS_PORTAL
= 3,
26 CAPTIVE_PORTAL_STATUS_PROXY_AUTH_REQUIRED
= 4,
27 CAPTIVE_PORTAL_STATUS_COUNT
30 struct CaptivePortalState
{
32 : status(CAPTIVE_PORTAL_STATUS_UNKNOWN
),
33 response_code(net::URLFetcher::RESPONSE_CODE_INVALID
) {
36 bool operator==(const CaptivePortalState
& o
) const {
37 return status
== o
.status
&& response_code
== o
.response_code
;
40 CaptivePortalStatus status
;
47 // Called when portal detection is completed for |network|, or
48 // when observers add themselves via AddAndFireObserver(). In the
49 // second case, |network| is the active network and |state| is a
50 // current portal state for the active network, which can be
51 // currently in the unknown state, for instance, if portal
52 // detection is in process for the active network. Note, that
53 // |network| may be NULL.
54 virtual void OnPortalDetectionCompleted(
55 const NetworkState
* network
,
56 const CaptivePortalState
& state
) = 0;
59 virtual ~Observer() {}
62 // Adds |observer| to the observers list.
63 virtual void AddObserver(Observer
* observer
) = 0;
65 // Adds |observer| to the observers list and immediately calls
66 // OnPortalDetectionCompleted() with the active network (which may
67 // be NULL) and captive portal state for the active network (which
68 // may be unknown, if, for instance, portal detection is in process
69 // for the active network).
71 // WARNING: don't call this method from the Observer's ctors or
72 // dtors, as it implicitly calls OnPortalDetectionCompleted(), which
74 // TODO (ygorshenin@): find a way to avoid this restriction.
75 virtual void AddAndFireObserver(Observer
* observer
) = 0;
77 // Removes |observer| from the observers list.
78 virtual void RemoveObserver(Observer
* observer
) = 0;
80 // Returns Captive Portal state for the network specified by |service_path|.
81 virtual CaptivePortalState
GetCaptivePortalState(
82 const std::string
& service_path
) = 0;
84 // Returns true if portal detection is enabled.
85 virtual bool IsEnabled() = 0;
87 // Enable portal detection. This method is needed because we can't
88 // check current network for portal state unless user accepts EULA.
89 // If |start_detection| is true and NetworkPortalDetector was
90 // disabled previously, portal detection for the active network is
91 // initiated by this method.
92 virtual void Enable(bool start_detection
) = 0;
94 // Restarts portal detection for the default network if currently in
95 // the idle state. Returns true if new portal detection attempt was
97 virtual bool StartDetectionIfIdle() = 0;
99 virtual void OnErrorScreenShow() OVERRIDE
{}
100 virtual void OnErrorScreenHide() OVERRIDE
{}
102 // Initializes network portal detector for testing. The
103 // |network_portal_detector| will be owned by the internal pointer
104 // and deleted by Shutdown().
105 static void InitializeForTesting(
106 NetworkPortalDetector
* network_portal_detector
);
108 // Creates an instance of the NetworkPortalDetector.
109 static void Initialize();
111 // Deletes the instance of the NetworkPortalDetector.
112 static void Shutdown();
114 // Gets the instance of the NetworkPortalDetector. Return value should
115 // be used carefully in tests, because it can be changed "on the fly"
116 // by calls to InitializeForTesting().
117 static NetworkPortalDetector
* Get();
119 // Returns non-localized string representation of |status|.
120 static std::string
CaptivePortalStatusString(CaptivePortalStatus status
);
122 // Returns |true| if NetworkPortalDetector was Initialized and it is safe to
124 static bool IsInitialized();
127 NetworkPortalDetector() {}
128 virtual ~NetworkPortalDetector() {}
131 DISALLOW_COPY_AND_ASSIGN(NetworkPortalDetector
);
134 } // namespace chromeos
136 #endif // CHROME_BROWSER_CHROMEOS_NET_NETWORK_PORTAL_DETECTOR_H_