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 "chrome/browser/chromeos/net/network_portal_detector_strategy.h" 10 #include "net/url_request/url_fetcher.h" 11 12 namespace chromeos { 13 14 class NetworkState; 15 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 { 20 public: 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 28 }; 29 30 struct CaptivePortalState { 31 CaptivePortalState() 32 : status(CAPTIVE_PORTAL_STATUS_UNKNOWN), 33 response_code(net::URLFetcher::RESPONSE_CODE_INVALID) { 34 } 35 36 bool operator==(const CaptivePortalState& o) const { 37 return status == o.status && response_code == o.response_code; 38 } 39 40 CaptivePortalStatus status; 41 int response_code; 42 base::TimeTicks time; 43 }; 44 45 class Observer { 46 public: 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; 57 58 protected: 59 virtual ~Observer() {} 60 }; 61 62 // Adds |observer| to the observers list. 63 virtual void AddObserver(Observer* observer) = 0; 64 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). 70 // 71 // WARNING: don't call this method from the Observer's ctors or 72 // dtors, as it implicitly calls OnPortalDetectionCompleted(), which 73 // is virtual. 74 // TODO (ygorshenin@): find a way to avoid this restriction. 75 virtual void AddAndFireObserver(Observer* observer) = 0; 76 77 // Removes |observer| from the observers list. 78 virtual void RemoveObserver(Observer* observer) = 0; 79 80 // Returns Captive Portal state for the network specified by |service_path|. 81 virtual CaptivePortalState GetCaptivePortalState( 82 const std::string& service_path) = 0; 83 84 // Returns true if portal detection is enabled. 85 virtual bool IsEnabled() = 0; 86 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; 93 94 // Restarts portal detection for the default network if currently in 95 // the idle state. Returns true if new portal detection attempt was 96 // started. 97 virtual bool StartDetectionIfIdle() = 0; 98 99 // Sets current strategy according to |id|. If current detection id 100 // doesn't equal to |id|, detection is restarted. 101 virtual void SetStrategy(PortalDetectorStrategy::StrategyId id) = 0; 102 103 // Initializes network portal detector for testing. The 104 // |network_portal_detector| will be owned by the internal pointer 105 // and deleted by Shutdown(). 106 static void InitializeForTesting( 107 NetworkPortalDetector* network_portal_detector); 108 109 // Creates an instance of the NetworkPortalDetector. 110 static void Initialize(); 111 112 // Deletes the instance of the NetworkPortalDetector. 113 static void Shutdown(); 114 115 // Gets the instance of the NetworkPortalDetector. Return value should 116 // be used carefully in tests, because it can be changed "on the fly" 117 // by calls to InitializeForTesting(). 118 static NetworkPortalDetector* Get(); 119 120 // Returns non-localized string representation of |status|. 121 static std::string CaptivePortalStatusString(CaptivePortalStatus status); 122 123 // Returns |true| if NetworkPortalDetector was Initialized and it is safe to 124 // call Get. 125 static bool IsInitialized(); 126 127 protected: 128 NetworkPortalDetector() {} 129 virtual ~NetworkPortalDetector() {} 130 131 private: 132 DISALLOW_COPY_AND_ASSIGN(NetworkPortalDetector); 133 }; 134 135 } // namespace chromeos 136 137 #endif // CHROME_BROWSER_CHROMEOS_NET_NETWORK_PORTAL_DETECTOR_H_ 138