Home | History | Annotate | Download | only in network
      1 // Copyright (c) 2012 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_NETWORK_STATE_HANDLER_H_
      6 #define CHROMEOS_NETWORK_NETWORK_STATE_HANDLER_H_
      7 
      8 #include <map>
      9 #include <set>
     10 #include <string>
     11 #include <vector>
     12 
     13 #include "base/callback_forward.h"
     14 #include "base/gtest_prod_util.h"
     15 #include "base/memory/scoped_ptr.h"
     16 #include "base/observer_list.h"
     17 #include "chromeos/chromeos_export.h"
     18 #include "chromeos/network/managed_state.h"
     19 #include "chromeos/network/network_handler.h"
     20 #include "chromeos/network/network_handler_callbacks.h"
     21 #include "chromeos/network/shill_property_handler.h"
     22 
     23 namespace base {
     24 class DictionaryValue;
     25 class ListValue;
     26 class Value;
     27 }
     28 
     29 namespace tracked_objects {
     30 class Location;
     31 }
     32 
     33 namespace chromeos {
     34 
     35 class DeviceState;
     36 class NetworkState;
     37 class NetworkStateHandlerObserver;
     38 class NetworkStateHandlerTest;
     39 
     40 // Class for tracking the list of visible networks and their properties.
     41 //
     42 // This class maps essential properties from the connection manager (Shill) for
     43 // each visible network. It is not used to change the properties of services or
     44 // devices, only global (manager) properties.
     45 //
     46 // All getters return the currently cached properties. This class is expected to
     47 // keep properties up to date by managing the appropriate Shill observers.
     48 // It will invoke its own more specific observer methods when the specified
     49 // changes occur.
     50 //
     51 // Most *ByType or *ForType methods will accept any of the following  for
     52 // |type|. See individual methods for specific notes.
     53 // * Any type defined in service_constants.h (e.g. flimflam::kTypeWifi)
     54 // * kMatchTypeDefault returns the default (active) network
     55 // * kMatchTypeNonVirtual returns the primary non virtual network
     56 // * kMatchTypeWireless returns the primary wireless network
     57 // * kMatchTypeMobile returns the primary cellular or wimax network
     58 
     59 class CHROMEOS_EXPORT NetworkStateHandler
     60     : public internal::ShillPropertyHandler::Listener {
     61  public:
     62   typedef std::vector<ManagedState*> ManagedStateList;
     63   typedef std::vector<const NetworkState*> NetworkStateList;
     64   typedef std::vector<const DeviceState*> DeviceStateList;
     65   typedef std::vector<const FavoriteState*> FavoriteStateList;
     66 
     67   enum TechnologyState {
     68     TECHNOLOGY_UNAVAILABLE,
     69     TECHNOLOGY_AVAILABLE,
     70     TECHNOLOGY_UNINITIALIZED,
     71     TECHNOLOGY_ENABLING,
     72     TECHNOLOGY_ENABLED
     73   };
     74 
     75   virtual ~NetworkStateHandler();
     76 
     77   // Add/remove observers.
     78   void AddObserver(NetworkStateHandlerObserver* observer,
     79                    const tracked_objects::Location& from_here);
     80   void RemoveObserver(NetworkStateHandlerObserver* observer,
     81                       const tracked_objects::Location& from_here);
     82 
     83   // Requests all Manager properties, specifically to update the complete
     84   // list of services which determines the list of Favorites. This should be
     85   // called any time a new service is configured or a Profile is loaded.
     86   void UpdateManagerProperties();
     87 
     88   // Returns the state for technology |type|. kMatchTypeMobile (only) is
     89   // also supported.
     90   TechnologyState GetTechnologyState(const std::string& type) const;
     91   bool IsTechnologyAvailable(const std::string& type) const {
     92     return GetTechnologyState(type) != TECHNOLOGY_UNAVAILABLE;
     93   }
     94   bool IsTechnologyEnabled(const std::string& type) const {
     95     return GetTechnologyState(type) == TECHNOLOGY_ENABLED;
     96   }
     97 
     98   // Asynchronously sets the technology enabled property for |type|.
     99   // kMatchTypeMobile (only) is also supported.
    100   // Note: Modifies Manager state. Calls |error_callback| on failure.
    101   void SetTechnologyEnabled(
    102       const std::string& type,
    103       bool enabled,
    104       const network_handler::ErrorCallback& error_callback);
    105 
    106   // Finds and returns a device state by |device_path| or NULL if not found.
    107   const DeviceState* GetDeviceState(const std::string& device_path) const;
    108 
    109   // Finds and returns a device state by |type|. Returns NULL if not found.
    110   // See note above for valid types.
    111   const DeviceState* GetDeviceStateByType(const std::string& type) const;
    112 
    113   // Returns true if any device of |type| is scanning.
    114   // See note above for valid types.
    115   bool GetScanningByType(const std::string& type) const;
    116 
    117   // Finds and returns a network state by |service_path| or NULL if not found.
    118   // Note: NetworkState is frequently updated asynchronously, i.e. properties
    119   // are not always updated all at once. This will contain the most recent
    120   // value for each property. To receive notifications when a property changes,
    121   // observe this class and implement NetworkPropertyChanged().
    122   const NetworkState* GetNetworkState(const std::string& service_path) const;
    123 
    124   // Returns the default connected network (which includes VPNs) or NULL.
    125   // This is equivalent to ConnectedNetworkByType(kMatchTypeDefault).
    126   const NetworkState* DefaultNetwork() const;
    127 
    128   // Returns the primary connected network of matching |type|, otherwise NULL.
    129   // See note above for valid types.
    130   const NetworkState* ConnectedNetworkByType(const std::string& type) const;
    131 
    132   // Like ConnectedNetworkByType() but returns a connecting network or NULL.
    133   const NetworkState* ConnectingNetworkByType(const std::string& type) const;
    134 
    135   // Like ConnectedNetworkByType() but returns any matching network or NULL.
    136   // Mostly useful for mobile networks where there is generally only one
    137   // network. Note: O(N).
    138   const NetworkState* FirstNetworkByType(const std::string& type) const;
    139 
    140   // Returns the hardware (MAC) address for the first connected network
    141   // matching |type|, or an empty string if none.
    142   // See note above for valid types.
    143   std::string HardwareAddressForType(const std::string& type) const;
    144   // Same as above but in aa:bb format.
    145   std::string FormattedHardwareAddressForType(const std::string& type) const;
    146 
    147   // Sets |list| to contain the list of networks.  The returned list contains
    148   // a copy of NetworkState pointers which should not be stored or used beyond
    149   // the scope of the calling function (i.e. they may later become invalid, but
    150   // only on the UI thread).
    151   void GetNetworkList(NetworkStateList* list) const;
    152 
    153   // Sets |list| to contain the list of devices.  The returned list contains
    154   // a copy of DeviceState pointers which should not be stored or used beyond
    155   // the scope of the calling function (i.e. they may later become invalid, but
    156   // only on the UI thread).
    157   void GetDeviceList(DeviceStateList* list) const;
    158 
    159   // Sets |list| to contain the list of favorite (aka "preferred") networks.
    160   // See GetNetworkList() for usage, and notes for |favorite_list_|.
    161   // Favorites that are visible have the same path() as the entries in
    162   // GetNetworkList(), so GetNetworkState() can be used to determine if a
    163   // favorite is visible and retrieve the complete properties (and vice-versa).
    164   void GetFavoriteList(FavoriteStateList* list) const;
    165 
    166   // Finds and returns a favorite state by |service_path| or NULL if not found.
    167   const FavoriteState* GetFavoriteState(const std::string& service_path) const;
    168 
    169   // Requests a network scan. This may trigger updates to the network
    170   // list, which will trigger the appropriate observer calls.
    171   void RequestScan() const;
    172 
    173   // Request a scan if not scanning and run |callback| when the Scanning state
    174   // for any Device matching |type| completes.
    175   void WaitForScan(const std::string& type, const base::Closure& callback);
    176 
    177   // Request a network scan then signal Shill to connect to the best available
    178   // networks when completed.
    179   void ConnectToBestWifiNetwork();
    180 
    181   // Request an update for an existing NetworkState, e.g. after configuring
    182   // a network. This is a no-op if an update request is already pending.
    183   // Returns true if the network exists and an update is requested or pending.
    184   // When the properties are received, NetworkPropertiesUpdated will be
    185   // signaled for each member of |observers_|, regardless of whether any
    186   // properties actually changed.
    187   bool RequestUpdateForNetwork(const std::string& service_path);
    188 
    189   // Request an update for all existing NetworkState entries, e.g. after
    190   // loading an ONC configuration file that may have updated one or more
    191   // existing networks.
    192   void RequestUpdateForAllNetworks();
    193 
    194   // Set the list of devices on which portal check is enabled.
    195   void SetCheckPortalList(const std::string& check_portal_list);
    196 
    197   const std::string& check_portal_list() const { return check_portal_list_; }
    198 
    199   // Generates a DictionaryValue of all NetworkState properties. Currently
    200   // provided for debugging purposes only.
    201   void GetNetworkStatePropertiesForTest(
    202       base::DictionaryValue* dictionary) const;
    203 
    204   // Construct and initialize an instance for testing.
    205   static NetworkStateHandler* InitializeForTest();
    206 
    207   static const char kMatchTypeDefault[];
    208   static const char kMatchTypeWireless[];
    209   static const char kMatchTypeMobile[];
    210   static const char kMatchTypeNonVirtual[];
    211 
    212   // Default set of comma separated interfaces on which to enable
    213   // portal checking.
    214   static const char kDefaultCheckPortalList[];
    215 
    216  protected:
    217   friend class NetworkHandler;
    218   NetworkStateHandler();
    219 
    220   // ShillPropertyHandler::Listener overrides.
    221 
    222   // This adds new entries to the managed list specified by |type| and deletes
    223   // any entries that are no longer in the list.
    224   virtual void UpdateManagedList(ManagedState::ManagedType type,
    225                                  const base::ListValue& entries) OVERRIDE;
    226 
    227   // The list of profiles changed (i.e. a user has logged in). Re-request
    228   // properties for all services since they may have changed.
    229   virtual void ProfileListChanged() OVERRIDE;
    230 
    231   // Parses the properties for the network service or device. Mostly calls
    232   // managed->PropertyChanged(key, value) for each dictionary entry.
    233   virtual void UpdateManagedStateProperties(
    234       ManagedState::ManagedType type,
    235       const std::string& path,
    236       const base::DictionaryValue& properties) OVERRIDE;
    237 
    238   // Called by ShillPropertyHandler when a watched service property changes.
    239   virtual void UpdateNetworkServiceProperty(
    240       const std::string& service_path,
    241       const std::string& key,
    242       const base::Value& value) OVERRIDE;
    243 
    244   // Called by ShillPropertyHandler when a watched device property changes.
    245   virtual void UpdateDeviceProperty(
    246       const std::string& device_path,
    247       const std::string& key,
    248       const base::Value& value) OVERRIDE;
    249 
    250   // Called by ShillPropertyHandler when the portal check list manager property
    251   // changes.
    252   virtual void CheckPortalListChanged(
    253       const std::string& check_portal_list) OVERRIDE;
    254 
    255   // Sends NetworkManagerChanged() to observers and logs an event.
    256   virtual void NotifyManagerPropertyChanged() OVERRIDE;
    257 
    258   // Called by |shill_property_handler_| when the service or device list has
    259   // changed and all entries have been updated. This updates the list and
    260   // notifies observers. If |type| == TYPE_NETWORK this also calls
    261   // CheckDefaultNetworkChanged().
    262   virtual void ManagedStateListChanged(
    263       ManagedState::ManagedType type) OVERRIDE;
    264 
    265   // Called after construction. Called explicitly by tests after adding
    266   // test observers.
    267   void InitShillPropertyHandler();
    268 
    269  private:
    270   typedef std::list<base::Closure> ScanCallbackList;
    271   typedef std::map<std::string, ScanCallbackList> ScanCompleteCallbackMap;
    272   friend class NetworkStateHandlerTest;
    273   FRIEND_TEST_ALL_PREFIXES(NetworkStateHandlerTest, NetworkStateHandlerStub);
    274 
    275   // NetworkState specific method for UpdateManagedStateProperties which
    276   // notifies observers.
    277   void UpdateNetworkStateProperties(NetworkState* network,
    278                                     const base::DictionaryValue& properties);
    279 
    280   // Non-const getters for managed entries. These are const so that they can
    281   // be called by Get[Network|Device]State, even though they return non-const
    282   // pointers.
    283   DeviceState* GetModifiableDeviceState(const std::string& device_path) const;
    284   NetworkState* GetModifiableNetworkState(
    285       const std::string& service_path) const;
    286   ManagedState* GetModifiableManagedState(const ManagedStateList* managed_list,
    287                                           const std::string& path) const;
    288 
    289   // Gets the list specified by |type|.
    290   ManagedStateList* GetManagedList(ManagedState::ManagedType type);
    291 
    292   // Helper function to notify observers. Calls CheckDefaultNetworkChanged().
    293   void OnNetworkConnectionStateChanged(NetworkState* network);
    294 
    295   // If the default network changed returns true and sets
    296   // |default_network_path_|.
    297   bool CheckDefaultNetworkChanged();
    298 
    299   // Logs an event and notifies observers.
    300   void OnDefaultNetworkChanged();
    301 
    302   // Notifies observers and updates connecting_network_.
    303   void NetworkPropertiesUpdated(const NetworkState* network);
    304 
    305   // Called whenever Device.Scanning state transitions to false.
    306   void ScanCompleted(const std::string& type);
    307 
    308   // Returns the technology type for |type|.
    309   std::string GetTechnologyForType(const std::string& type) const;
    310 
    311   // Shill property handler instance, owned by this class.
    312   scoped_ptr<internal::ShillPropertyHandler> shill_property_handler_;
    313 
    314   // Observer list
    315   ObserverList<NetworkStateHandlerObserver> observers_;
    316 
    317   // List of managed network states
    318   ManagedStateList network_list_;
    319 
    320   // List of managed favorite states; this list includes all entries in
    321   // Manager.ServiceCompleteList, but only entries with a non-empty Profile
    322   // property are returned in GetFavoriteList().
    323   ManagedStateList favorite_list_;
    324 
    325   // List of managed device states
    326   ManagedStateList device_list_;
    327 
    328   // Keeps track of the default network for notifying observers when it changes.
    329   std::string default_network_path_;
    330 
    331   // List of interfaces on which portal check is enabled.
    332   std::string check_portal_list_;
    333 
    334   // Callbacks to run when a scan for the technology type completes.
    335   ScanCompleteCallbackMap scan_complete_callbacks_;
    336 
    337   DISALLOW_COPY_AND_ASSIGN(NetworkStateHandler);
    338 };
    339 
    340 }  // namespace chromeos
    341 
    342 #endif  // CHROMEOS_NETWORK_NETWORK_STATE_HANDLER_H_
    343