1 // Copyright 2014 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 CHROMEOS_NETWORK_PORTAL_DETECTOR_NETWORK_PORTAL_DETECTOR_H_
6 #define CHROMEOS_NETWORK_PORTAL_DETECTOR_NETWORK_PORTAL_DETECTOR_H_
7 
8 #include "base/component_export.h"
9 #include "base/macros.h"
10 #include "base/notreached.h"
11 #include "chromeos/network/portal_detector/network_portal_detector_strategy.h"
12 
13 namespace chromeos {
14 
15 class NetworkState;
16 
17 // This is an interface for a chromeos portal detector that allows for
18 // observation of captive portal state. It supports retries based on a portal
19 // detector strategy.
COMPONENT_EXPORT(CHROMEOS_NETWORK)20 class COMPONENT_EXPORT(CHROMEOS_NETWORK) NetworkPortalDetector {
21  public:
22   enum CaptivePortalStatus {
23     CAPTIVE_PORTAL_STATUS_UNKNOWN = 0,
24     CAPTIVE_PORTAL_STATUS_OFFLINE = 1,
25     CAPTIVE_PORTAL_STATUS_ONLINE = 2,
26     CAPTIVE_PORTAL_STATUS_PORTAL = 3,
27     CAPTIVE_PORTAL_STATUS_PROXY_AUTH_REQUIRED = 4,
28     CAPTIVE_PORTAL_STATUS_COUNT,
29     kMaxValue = CAPTIVE_PORTAL_STATUS_COUNT  // For UMA_HISTOGRAM_ENUMERATION
30   };
31 
32   class Observer {
33    public:
34     // Called when portal detection is completed for |network|, or
35     // when observers add themselves via AddAndFireObserver(). In the
36     // second case, |network| is the active network and |state| is a
37     // current portal state for the active network, which can be
38     // currently in the unknown state, for instance, if portal
39     // detection is in process for the active network. Note, that
40     // |network| may be null.
41     virtual void OnPortalDetectionCompleted(
42         const NetworkState* network,
43         const CaptivePortalStatus status) = 0;
44 
45     // Called on Shutdown, allows removal of observers. Primarly used in tests.
46     virtual void OnShutdown() {}
47 
48    protected:
49     virtual ~Observer() {}
50   };
51 
52   virtual ~NetworkPortalDetector() {}
53 
54   // Adds |observer| to the observers list.
55   virtual void AddObserver(Observer* observer) = 0;
56 
57   // Adds |observer| to the observers list and immediately calls
58   // OnPortalDetectionCompleted() with the active network (which may
59   // be null) and captive portal state for the active network (which
60   // may be unknown, if, for instance, portal detection is in process
61   // for the active network).
62   //
63   // WARNING: don't call this method from the Observer's ctors or
64   // dtors, as it implicitly calls OnPortalDetectionCompleted(), which
65   // is virtual.
66   // TODO (ygorshenin@): find a way to avoid this restriction.
67   virtual void AddAndFireObserver(Observer* observer) = 0;
68 
69   // Removes |observer| from the observers list.
70   virtual void RemoveObserver(Observer* observer) = 0;
71 
72   // Returns CaptivePortalStatus for the the default network or UNKNOWN.
73   virtual CaptivePortalStatus GetCaptivePortalStatus() = 0;
74 
75   // Returns true if portal detection is enabled.
76   virtual bool IsEnabled() = 0;
77 
78   // Enable portal detection. This method is needed because we can't
79   // check current network for portal state unless user accepts EULA.
80   // If |start_detection| is true and NetworkPortalDetector was
81   // disabled previously, portal detection for the active network is
82   // initiated by this method.
83   virtual void Enable(bool start_detection) = 0;
84 
85   // Starts or restarts portal detection for the default network. If not
86   // currently in the idle state, does nothing. Returns true if a new portal
87   // detection attempt was started.
88   virtual void StartPortalDetection() = 0;
89 
90   // Sets current strategy according to |id|. If current detection id
91   // doesn't equal to |id|, detection is restarted.
92   virtual void SetStrategy(PortalDetectorStrategy::StrategyId id) = 0;
93 
94   // Returns non-localized string representation of |status|.
95   static std::string CaptivePortalStatusString(CaptivePortalStatus status);
96 
97  protected:
98   NetworkPortalDetector() {}
99 
100  private:
101   DISALLOW_COPY_AND_ASSIGN(NetworkPortalDetector);
102 };
103 
104 // Manages a global NetworkPortalDetector instance that can be accessed across
105 // all ChromeOS components.
106 namespace network_portal_detector {
107 // Gets the instance of the NetworkPortalDetector. Return value should
108 // be used carefully in tests, because it can be changed "on the fly"
109 // by calls to InitializeForTesting().
110 COMPONENT_EXPORT(CHROMEOS_NETWORK) NetworkPortalDetector* GetInstance();
111 
112 // Returns |true| if NetworkPortalDetector was Initialized and it is safe to
113 // call GetInstance.
114 COMPONENT_EXPORT(CHROMEOS_NETWORK) bool IsInitialized();
115 
116 // Deletes the instance of the NetworkPortalDetector.
117 COMPONENT_EXPORT(CHROMEOS_NETWORK) void Shutdown();
118 
119 COMPONENT_EXPORT(CHROMEOS_NETWORK)
120 void SetNetworkPortalDetector(NetworkPortalDetector* network_portal_detector);
121 
122 // Initializes network portal detector for testing. The
123 // |network_portal_detector| will be owned by the internal pointer
124 // and deleted by Shutdown().
125 COMPONENT_EXPORT(CHROMEOS_NETWORK)
126 void InitializeForTesting(NetworkPortalDetector* network_portal_detector);
127 
128 // Returns true if the network portal detector has been set for testing.
129 COMPONENT_EXPORT(CHROMEOS_NETWORK) bool SetForTesting();
130 
131 }  // namespace network_portal_detector
132 
133 }  // namespace chromeos
134 
135 #endif  // CHROMEOS_NETWORK_PORTAL_DETECTOR_NETWORK_PORTAL_DETECTOR_H_
136