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