Home | History | Annotate | Download | only in net
      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 // NetworkLibrary 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   virtual void Init() = 0;
     57   virtual void Shutdown() = 0;
     58 
     59   // Adds |observer| to the observers list.
     60   virtual void AddObserver(Observer* observer) = 0;
     61 
     62   // Adds |observer| to the observers list and immediately calls
     63   // OnPortalDetectionCompleted() with the active network (which may
     64   // be NULL) and captive portal state for the active network (which
     65   // may be unknown, if, for instance, portal detection is in process
     66   // for the active network).
     67   //
     68   // WARNING: don't call this method from the Observer's ctors or
     69   // dtors, as it implicitly calls OnPortalDetectionCompleted(), which
     70   // is virtual.
     71   // TODO (ygorshenin@): find a way to avoid this restriction.
     72   virtual void AddAndFireObserver(Observer* observer) = 0;
     73 
     74   // Removes |observer| from the observers list.
     75   virtual void RemoveObserver(Observer* observer) = 0;
     76 
     77   // Returns Captive Portal state for a given |network|.
     78   virtual CaptivePortalState GetCaptivePortalState(
     79       const chromeos::NetworkState* network) = 0;
     80 
     81   // Returns true if portal detection is enabled.
     82   virtual bool IsEnabled() = 0;
     83 
     84   // Enable portal detection. This method is needed because we can't
     85   // check current network for portal state unless user accepts EULA.
     86   // If |start_detection| is true and NetworkPortalDetector was
     87   // disabled previously, portal detection for the active network is
     88   // initiated by this method.
     89   virtual void Enable(bool start_detection) = 0;
     90 
     91   // Restarts portal detection for the default network if currently in
     92   // the idle state. Returns true if new portal detection attempt was
     93   // started.
     94   virtual bool StartDetectionIfIdle() = 0;
     95 
     96   // Enables lazy detection mode. In this mode portal detection after
     97   // first 3 consecutive attemps will be performed once in 5 seconds.
     98   virtual void EnableLazyDetection() = 0;
     99 
    100   // Dizables lazy detection mode.
    101   virtual void DisableLazyDetection() = 0;
    102 
    103   // Creates an instance of the NetworkPortalDetector.
    104   static NetworkPortalDetector* CreateInstance();
    105 
    106   // Gets the instance of the NetworkPortalDetector.
    107   static NetworkPortalDetector* GetInstance();
    108 
    109   // Returns true is NetworkPortalDetector service is enabled in command line.
    110   static bool IsEnabledInCommandLine();
    111 
    112  protected:
    113   NetworkPortalDetector();
    114   virtual ~NetworkPortalDetector();
    115 
    116  private:
    117   DISALLOW_COPY_AND_ASSIGN(NetworkPortalDetector);
    118 };
    119 
    120 }  // namespace chromeos
    121 
    122 #endif  // CHROME_BROWSER_CHROMEOS_NET_NETWORK_PORTAL_DETECTOR_H_
    123