Home | History | Annotate | Download | only in cros
      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 CHROME_BROWSER_CHROMEOS_CROS_NETWORK_LIBRARY_IMPL_BASE_H_
      6 #define CHROME_BROWSER_CHROMEOS_CROS_NETWORK_LIBRARY_IMPL_BASE_H_
      7 
      8 #include <list>
      9 #include <set>
     10 
     11 #include "chrome/browser/chromeos/cros/network_library.h"
     12 #include "chromeos/network/onc/onc_constants.h"
     13 
     14 namespace chromeos {
     15 
     16 class NetworkLibraryImplBase : public NetworkLibrary {
     17  public:
     18   NetworkLibraryImplBase();
     19   virtual ~NetworkLibraryImplBase();
     20 
     21   //////////////////////////////////////////////////////////////////////////////
     22   // NetworkLibraryImplBase virtual functions.
     23 
     24   // Functions for monitoring networks & devices.
     25   virtual void MonitorNetworkStart(const std::string& service_path) = 0;
     26   virtual void MonitorNetworkStop(const std::string& service_path) = 0;
     27   virtual void MonitorNetworkDeviceStart(const std::string& device_path) = 0;
     28   virtual void MonitorNetworkDeviceStop(const std::string& device_path) = 0;
     29 
     30   // Called from ConnectToWifiNetwork.
     31   // Calls ConnectToWifiNetworkUsingConnectData if network request succeeds.
     32   virtual void CallRequestWifiNetworkAndConnect(
     33       const std::string& ssid, ConnectionSecurity security) = 0;
     34   // Called from ConnectToVirtualNetwork*.
     35   // Calls ConnectToVirtualNetworkUsingConnectData if network request succeeds.
     36   virtual void CallRequestVirtualNetworkAndConnect(
     37       const std::string& service_name,
     38       const std::string& server_hostname,
     39       ProviderType provider_type) = 0;
     40   // Call to configure a wifi service. The identifier is either a service_path
     41   // or a GUID. |info| is a dictionary of property values.
     42   virtual void CallConfigureService(const std::string& identifier,
     43                                     const DictionaryValue* info) = 0;
     44   // Called from NetworkConnectStart.
     45   // Calls NetworkConnectCompleted when the connection attempt completes.
     46   virtual void CallConnectToNetwork(Network* network) = 0;
     47   // Called from DeleteRememberedNetwork.
     48   virtual void CallDeleteRememberedNetwork(
     49       const std::string& profile_path, const std::string& service_path) = 0;
     50 
     51   // Called from Enable*NetworkDevice.
     52   // Asynchronously enables or disables the specified device type.
     53   virtual void CallEnableNetworkDeviceType(
     54       ConnectionType device, bool enable) = 0;
     55 
     56   // Called from DeleteRememberedNetwork for VPN services.
     57   // Asynchronously disconnects and removes the service.
     58   virtual void CallRemoveNetwork(const Network* network) = 0;
     59 
     60   //////////////////////////////////////////////////////////////////////////////
     61   // NetworkLibrary implementation.
     62 
     63   // virtual Init implemented in derived classes.
     64   // virtual IsCros implemented in derived classes.
     65 
     66   virtual void AddNetworkProfileObserver(
     67       NetworkProfileObserver* observer) OVERRIDE;
     68   virtual void RemoveNetworkProfileObserver(
     69       NetworkProfileObserver* observer) OVERRIDE;
     70   virtual void AddNetworkManagerObserver(
     71       NetworkManagerObserver* observer) OVERRIDE;
     72   virtual void RemoveNetworkManagerObserver(
     73       NetworkManagerObserver* observer) OVERRIDE;
     74   virtual void AddNetworkObserver(const std::string& service_path,
     75                                   NetworkObserver* observer) OVERRIDE;
     76   virtual void RemoveNetworkObserver(const std::string& service_path,
     77                                      NetworkObserver* observer) OVERRIDE;
     78   virtual void RemoveObserverForAllNetworks(
     79       NetworkObserver* observer) OVERRIDE;
     80   virtual void AddNetworkDeviceObserver(
     81       const std::string& device_path,
     82       NetworkDeviceObserver* observer) OVERRIDE;
     83   virtual void RemoveNetworkDeviceObserver(
     84       const std::string& device_path,
     85       NetworkDeviceObserver* observer) OVERRIDE;
     86 
     87   virtual void AddPinOperationObserver(
     88       PinOperationObserver* observer) OVERRIDE;
     89   virtual void RemovePinOperationObserver(
     90       PinOperationObserver* observer) OVERRIDE;
     91 
     92   virtual const EthernetNetwork* ethernet_network() const OVERRIDE;
     93   virtual bool ethernet_connecting() const OVERRIDE;
     94   virtual bool ethernet_connected() const OVERRIDE;
     95   virtual const WifiNetwork* wifi_network() const OVERRIDE;
     96   virtual bool wifi_connecting() const OVERRIDE;
     97   virtual bool wifi_connected() const OVERRIDE;
     98   virtual const CellularNetwork* cellular_network() const OVERRIDE;
     99   virtual bool cellular_connecting() const OVERRIDE;
    100   virtual bool cellular_connected() const OVERRIDE;
    101   virtual const WimaxNetwork* wimax_network() const OVERRIDE;
    102   virtual bool wimax_connecting() const OVERRIDE;
    103   virtual bool wimax_connected() const OVERRIDE;
    104   virtual const VirtualNetwork* virtual_network() const OVERRIDE;
    105   virtual bool virtual_network_connecting() const OVERRIDE;
    106   virtual bool virtual_network_connected() const OVERRIDE;
    107   virtual bool Connected() const OVERRIDE;
    108   virtual bool Connecting() const OVERRIDE;
    109   virtual const WifiNetworkVector& wifi_networks() const OVERRIDE;
    110   virtual const WifiNetworkVector& remembered_wifi_networks() const OVERRIDE;
    111   virtual const CellularNetworkVector& cellular_networks() const OVERRIDE;
    112   virtual const WimaxNetworkVector& wimax_networks() const OVERRIDE;
    113   virtual const VirtualNetworkVector& virtual_networks() const OVERRIDE;
    114   virtual const VirtualNetworkVector&
    115       remembered_virtual_networks() const OVERRIDE;
    116   virtual const Network* active_network() const OVERRIDE;
    117   virtual const Network* active_nonvirtual_network() const OVERRIDE;
    118   virtual const Network* connected_network() const OVERRIDE;
    119   virtual const Network* connecting_network() const OVERRIDE;
    120   virtual bool ethernet_available() const OVERRIDE;
    121   virtual bool wifi_available() const OVERRIDE;
    122   virtual bool wimax_available() const OVERRIDE;
    123   virtual bool cellular_available() const OVERRIDE;
    124   virtual bool ethernet_enabled() const OVERRIDE;
    125   virtual bool wifi_enabled() const OVERRIDE;
    126   virtual bool wimax_enabled() const OVERRIDE;
    127   virtual bool cellular_enabled() const OVERRIDE;
    128   virtual bool wifi_scanning() const OVERRIDE;
    129   virtual bool cellular_initializing() const OVERRIDE;
    130 
    131   virtual const NetworkDevice* FindNetworkDeviceByPath(
    132       const std::string& path) const OVERRIDE;
    133   NetworkDevice* FindNetworkDeviceByPath(const std::string& path);
    134   virtual const NetworkDevice* FindMobileDevice() const OVERRIDE;
    135   virtual const NetworkDevice* FindCellularDevice() const OVERRIDE;
    136   virtual Network* FindNetworkByPath(const std::string& path) const OVERRIDE;
    137   virtual Network* FindNetworkByUniqueId(
    138       const std::string& unique_id) const OVERRIDE;
    139   WirelessNetwork* FindWirelessNetworkByPath(const std::string& path) const;
    140   virtual WifiNetwork* FindWifiNetworkByPath(
    141       const std::string& path) const OVERRIDE;
    142   virtual WimaxNetwork* FindWimaxNetworkByPath(
    143       const std::string& path) const OVERRIDE;
    144   virtual CellularNetwork* FindCellularNetworkByPath(
    145       const std::string& path) const OVERRIDE;
    146   virtual VirtualNetwork* FindVirtualNetworkByPath(
    147       const std::string& path) const OVERRIDE;
    148   Network* FindRememberedFromNetwork(const Network* network) const;
    149   virtual Network* FindRememberedNetworkByPath(
    150       const std::string& path) const OVERRIDE;
    151 
    152   virtual const base::DictionaryValue* FindOncForNetwork(
    153       const std::string& unique_id) const OVERRIDE;
    154 
    155   virtual void SignalCellularPlanPayment() OVERRIDE;
    156   virtual bool HasRecentCellularPlanPayment() OVERRIDE;
    157   virtual const std::string& GetCellularHomeCarrierId() const OVERRIDE;
    158   virtual bool CellularDeviceUsesDirectActivation() const OVERRIDE;
    159 
    160   // virtual ChangePin implemented in derived classes.
    161   // virtual ChangeRequiredPin implemented in derived classes.
    162   // virtual EnterPin implemented in derived classes.
    163   // virtual UnblockPin implemented in derived classes.
    164 
    165   // virtual RequestCellularScan implemented in derived classes.
    166   // virtual RequestCellularRegister implemented in derived classes.
    167   // virtual SetCellularDataRoamingAllowed implemented in derived classes.
    168   // virtual SetCarrier implemented in derived classes.
    169   // virtual IsCellularAlwaysInRoaming implemented in derived classes.
    170   // virtual RequestNetworkScan implemented in derived classes.
    171 
    172   virtual bool CanConnectToNetwork(const Network* network) const OVERRIDE;
    173 
    174   // Connect to an existing network.
    175   virtual void ConnectToWifiNetwork(WifiNetwork* wifi) OVERRIDE;
    176   virtual void ConnectToWifiNetwork(WifiNetwork* wifi, bool shared) OVERRIDE;
    177   virtual void ConnectToWimaxNetwork(WimaxNetwork* wimax) OVERRIDE;
    178   virtual void ConnectToWimaxNetwork(WimaxNetwork* wimax, bool shared) OVERRIDE;
    179   virtual void ConnectToCellularNetwork(CellularNetwork* cellular) OVERRIDE;
    180   virtual void ConnectToVirtualNetwork(VirtualNetwork* vpn) OVERRIDE;
    181 
    182   // Request a network and connect to it.
    183   virtual void ConnectToUnconfiguredWifiNetwork(
    184       const std::string& ssid,
    185       ConnectionSecurity security,
    186       const std::string& passphrase,
    187       const EAPConfigData* eap_config,
    188       bool save_credentials,
    189       bool shared) OVERRIDE;
    190 
    191   virtual void ConnectToUnconfiguredVirtualNetwork(
    192       const std::string& service_name,
    193       const std::string& server_hostname,
    194       ProviderType provider_type,
    195       const VPNConfigData& config) OVERRIDE;
    196 
    197   // virtual DisconnectFromNetwork implemented in derived classes.
    198   virtual void ForgetNetwork(const std::string& service_path) OVERRIDE;
    199   virtual void EnableEthernetNetworkDevice(bool enable) OVERRIDE;
    200   virtual void EnableWifiNetworkDevice(bool enable) OVERRIDE;
    201   virtual void EnableWimaxNetworkDevice(bool enable) OVERRIDE;
    202   virtual void EnableCellularNetworkDevice(bool enable) OVERRIDE;
    203   // virtual GetIPConfigs implemented in derived classes.
    204   // virtual SetIPConfig implemented in derived classes.
    205   virtual void LoadOncNetworks(const base::ListValue& network_configs,
    206                                onc::ONCSource source) OVERRIDE;
    207   virtual bool SetActiveNetwork(ConnectionType type,
    208                                 const std::string& service_path) OVERRIDE;
    209 
    210  protected:
    211   typedef ObserverList<NetworkObserver> NetworkObserverList;
    212   typedef std::map<std::string, NetworkObserverList*> NetworkObserverMap;
    213 
    214   typedef ObserverList<NetworkDeviceObserver> NetworkDeviceObserverList;
    215   typedef std::map<std::string, NetworkDeviceObserverList*>
    216       NetworkDeviceObserverMap;
    217 
    218   typedef std::map<std::string, Network*> NetworkMap;
    219   typedef std::map<std::string, int> PriorityMap;
    220   typedef std::map<std::string, NetworkDevice*> NetworkDeviceMap;
    221   typedef std::map<std::string, const base::DictionaryValue*> NetworkOncMap;
    222   typedef std::map<onc::ONCSource,
    223                    std::set<std::string> > NetworkSourceMap;
    224 
    225   struct NetworkProfile {
    226     NetworkProfile(const std::string& p, NetworkProfileType t);
    227     ~NetworkProfile();
    228     std::string path;
    229     NetworkProfileType type;
    230     typedef std::set<std::string> ServiceList;
    231     ServiceList services;
    232   };
    233   typedef std::list<NetworkProfile> NetworkProfileList;
    234 
    235   struct ConnectData {
    236     ConnectData();
    237     ~ConnectData();
    238     ConnectionSecurity security;
    239     std::string service_name;  // For example, SSID.
    240     std::string username;
    241     std::string passphrase;
    242     std::string otp;
    243     std::string group_name;
    244     std::string server_hostname;
    245     std::string server_ca_cert_pem;
    246     std::string client_cert_pkcs11_id;
    247     EAPMethod eap_method;
    248     EAPPhase2Auth eap_auth;
    249     bool eap_use_system_cas;
    250     std::string eap_identity;
    251     std::string eap_anonymous_identity;
    252     std::string psk_key;
    253     bool save_credentials;
    254     NetworkProfileType profile_type;
    255   };
    256 
    257   enum NetworkConnectStatus {
    258     CONNECT_SUCCESS,
    259     CONNECT_BAD_PASSPHRASE,
    260     CONNECT_FAILED
    261   };
    262 
    263   // Return true if a profile matching |type| is loaded.
    264   bool HasProfileType(NetworkProfileType type) const;
    265 
    266   // This will connect to a preferred network if the currently connected
    267   // network is not preferred. This should be called when the active profile
    268   // changes.
    269   void SwitchToPreferredNetwork();
    270 
    271   // Finds device by connection type.
    272   const NetworkDevice* FindDeviceByType(ConnectionType type) const;
    273   // Called from ConnectTo*Network.
    274   void NetworkConnectStartWifi(
    275       WifiNetwork* network, NetworkProfileType profile_type);
    276   void NetworkConnectStartVPN(VirtualNetwork* network);
    277   void NetworkConnectStart(Network* network, NetworkProfileType profile_type);
    278   // Called from CallConnectToNetwork.
    279   void NetworkConnectCompleted(Network* network,
    280                                NetworkConnectStatus status);
    281   // Called from CallRequestWifiNetworkAndConnect.
    282   void ConnectToWifiNetworkUsingConnectData(WifiNetwork* wifi);
    283   // Called from CallRequestVirtualNetworkAndConnect.
    284   void ConnectToVirtualNetworkUsingConnectData(VirtualNetwork* vpn);
    285 
    286   // Network list management functions.
    287   void ClearActiveNetwork(ConnectionType type);
    288   void UpdateActiveNetwork(Network* network);
    289   void AddNetwork(Network* network);
    290   void DeleteNetwork(Network* network);
    291 
    292   // Calls ForgetNetwork for remembered wifi and virtual networks based on id.
    293   // When |if_found| is true, then it forgets networks that appear in |ids|.
    294   // When |if_found| is false, it removes networks that do NOT appear in |ids|.
    295   // |source| is the import source of the data.
    296   void ForgetNetworksById(onc::ONCSource source,
    297                           std::set<std::string> ids,
    298                           bool if_found);
    299 
    300   // Checks whether |network| has meanwhile been pruned by ONC policy. If so,
    301   // instructs shill to remove the network, deletes |network| and returns
    302   // false.
    303   bool ValidateRememberedNetwork(Network* network);
    304 
    305   // Adds |network| to the remembered networks data structures and returns true
    306   // if ValidateRememberedNetwork(network) returns true. Returns false
    307   // otherwise.
    308   bool ValidateAndAddRememberedNetwork(Network* network);
    309 
    310   void DeleteRememberedNetwork(const std::string& service_path);
    311   void ClearNetworks();
    312   void DeleteRememberedNetworks();
    313   void DeleteDevice(const std::string& device_path);
    314   void DeleteDeviceFromDeviceObserversMap(const std::string& device_path);
    315 
    316   // Profile management functions.
    317   void AddProfile(const std::string& profile_path,
    318                   NetworkProfileType profile_type);
    319   NetworkProfile* GetProfileForType(NetworkProfileType type);
    320   void SetProfileType(Network* network, NetworkProfileType type);
    321   void SetProfileTypeFromPath(Network* network);
    322   std::string GetProfilePath(NetworkProfileType type);
    323 
    324   // Notifications.
    325   void NotifyNetworkProfileObservers();
    326   void NotifyNetworkManagerChanged(bool force_update);
    327   void SignalNetworkManagerObservers();
    328   void NotifyNetworkChanged(const Network* network);
    329   void NotifyNetworkDeviceChanged(NetworkDevice* device, PropertyIndex index);
    330   void NotifyPinOperationCompleted(PinOperationError error);
    331 
    332   // TPM related functions.
    333   void GetTpmInfo();
    334   const std::string& GetTpmSlot();
    335   const std::string& GetTpmPin();
    336 
    337   // Network profile observer list.
    338   ObserverList<NetworkProfileObserver> network_profile_observers_;
    339 
    340   // Network manager observer list.
    341   ObserverList<NetworkManagerObserver> network_manager_observers_;
    342 
    343   // PIN operation observer list.
    344   ObserverList<PinOperationObserver> pin_operation_observers_;
    345 
    346   // Network observer map.
    347   NetworkObserverMap network_observers_;
    348 
    349   // Network device observer map.
    350   NetworkDeviceObserverMap network_device_observers_;
    351 
    352   // List of profiles.
    353   NetworkProfileList profile_list_;
    354 
    355   // A service path based map of all visible Networks.
    356   NetworkMap network_map_;
    357 
    358   // A unique_id based map of all visible Networks.
    359   NetworkMap network_unique_id_map_;
    360 
    361   // A service path based map of all remembered Networks.
    362   NetworkMap remembered_network_map_;
    363 
    364   // A list of services that we are awaiting updates for.
    365   PriorityMap network_update_requests_;
    366 
    367   // A device path based map of all NetworkDevices.
    368   NetworkDeviceMap device_map_;
    369 
    370   // The ethernet network.
    371   EthernetNetwork* ethernet_;
    372 
    373   // The list of available wifi networks.
    374   WifiNetworkVector wifi_networks_;
    375 
    376   // The current connected (or connecting) wifi network.
    377   WifiNetwork* active_wifi_;
    378 
    379   // The remembered wifi networks.
    380   WifiNetworkVector remembered_wifi_networks_;
    381 
    382   // The list of available cellular networks.
    383   CellularNetworkVector cellular_networks_;
    384 
    385   // The list of available wimax networks.
    386   WimaxNetworkVector wimax_networks_;
    387 
    388   // The current connected (or connecting) cellular network.
    389   CellularNetwork* active_cellular_;
    390 
    391   // The current connected (or connecting) Wimax network.
    392   WimaxNetwork* active_wimax_;
    393 
    394   // The list of available virtual networks.
    395   VirtualNetworkVector virtual_networks_;
    396 
    397   // The current connected (or connecting) virtual network.
    398   VirtualNetwork* active_virtual_;
    399 
    400   // The remembered virtual networks.
    401   VirtualNetworkVector remembered_virtual_networks_;
    402 
    403   // The path of the active profile (for retrieving remembered services).
    404   std::string active_profile_path_;
    405 
    406   // The current available network devices. Bitwise flag of ConnectionTypes.
    407   int available_devices_;
    408 
    409   // The current uninitialized network devices. Bitwise flag of ConnectionTypes.
    410   int uninitialized_devices_;
    411 
    412   // The current enabled network devices. Bitwise flag of ConnectionTypes.
    413   int enabled_devices_;
    414 
    415   // The current busy network devices. Bitwise flag of ConnectionTypes.
    416   // Busy means device is switching from enable/disable state.
    417   int busy_devices_;
    418 
    419   // True if we are currently scanning for wifi networks.
    420   bool wifi_scanning_;
    421 
    422   // List of interfaces for which portal check is enabled.
    423   std::string check_portal_list_;
    424 
    425   // True if access network library is locked.
    426   bool is_locked_;
    427 
    428   // TPM module user slot and PIN, needed by shill to access certificates.
    429   std::string tpm_slot_;
    430   std::string tpm_pin_;
    431 
    432   // Type of pending SIM operation, SIM_OPERATION_NONE otherwise.
    433   SimOperationType sim_operation_;
    434 
    435  private:
    436   // List of networks to move to the user profile once logged in.
    437   std::list<std::string> user_networks_;
    438 
    439   // Weak pointer factory for canceling a network change callback.
    440   base::WeakPtrFactory<NetworkLibraryImplBase> notify_manager_weak_factory_;
    441 
    442   // Cellular plan payment time.
    443   base::Time cellular_plan_payment_time_;
    444 
    445   // Temporary connection data for async connect calls.
    446   ConnectData connect_data_;
    447 
    448   // Holds unique id to ONC mapping.
    449   NetworkOncMap network_onc_map_;
    450 
    451   // Keeps track of what networks ONC has configured. This is used to weed out
    452   // stray networks that shill still has on file, but are not known on the
    453   // Chrome side.
    454   NetworkSourceMap network_source_map_;
    455 
    456   DISALLOW_COPY_AND_ASSIGN(NetworkLibraryImplBase);
    457 };
    458 
    459 }  // namespace chromeos
    460 
    461 #endif  // CHROME_BROWSER_CHROMEOS_CROS_NETWORK_LIBRARY_IMPL_BASE_H_
    462