- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / net / network_portal_detector.h
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 #ifndef CHROME_BROWSER_CHROMEOS_NET_NETWORK_PORTAL_DETECTOR_H_
6 #define CHROME_BROWSER_CHROMEOS_NET_NETWORK_PORTAL_DETECTOR_H_
7
8 #include "base/basictypes.h"
9 #include "net/url_request/url_fetcher.h"
10
11 namespace chromeos {
12
13 class NetworkState;
14
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
27   };
28
29   struct CaptivePortalState {
30     CaptivePortalState()
31         : status(CAPTIVE_PORTAL_STATUS_UNKNOWN),
32           response_code(net::URLFetcher::RESPONSE_CODE_INVALID) {
33     }
34
35     CaptivePortalStatus status;
36     int response_code;
37   };
38
39   class Observer {
40    public:
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;
51
52    protected:
53     virtual ~Observer() {}
54   };
55
56   // Adds |observer| to the observers list.
57   virtual void AddObserver(Observer* observer) = 0;
58
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).
64   //
65   // WARNING: don't call this method from the Observer's ctors or
66   // dtors, as it implicitly calls OnPortalDetectionCompleted(), which
67   // is virtual.
68   // TODO (ygorshenin@): find a way to avoid this restriction.
69   virtual void AddAndFireObserver(Observer* observer) = 0;
70
71   // Removes |observer| from the observers list.
72   virtual void RemoveObserver(Observer* observer) = 0;
73
74   // Returns Captive Portal state for a given |network|.
75   virtual CaptivePortalState GetCaptivePortalState(
76       const chromeos::NetworkState* network) = 0;
77
78   // Returns true if portal detection is enabled.
79   virtual bool IsEnabled() = 0;
80
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;
87
88   // Restarts portal detection for the default network if currently in
89   // the idle state. Returns true if new portal detection attempt was
90   // started.
91   virtual bool StartDetectionIfIdle() = 0;
92
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;
96
97   // Dizables lazy detection mode.
98   virtual void DisableLazyDetection() = 0;
99
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);
105
106   // Creates an instance of the NetworkPortalDetector.
107   static void Initialize();
108
109   // Deletes the instance of the NetworkPortalDetector.
110   static void Shutdown();
111
112   // Gets the instance of the NetworkPortalDetector.
113   static NetworkPortalDetector* Get();
114
115  protected:
116   NetworkPortalDetector() {}
117   virtual ~NetworkPortalDetector() {}
118
119  private:
120   DISALLOW_COPY_AND_ASSIGN(NetworkPortalDetector);
121 };
122
123 }  // namespace chromeos
124
125 #endif  // CHROME_BROWSER_CHROMEOS_NET_NETWORK_PORTAL_DETECTOR_H_