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_H_
      6 #define CHROME_BROWSER_CHROMEOS_CROS_NETWORK_LIBRARY_H_
      7 
      8 #include <map>
      9 #include <string>
     10 #include <vector>
     11 
     12 #include "base/callback.h"
     13 #include "base/compiler_specific.h"
     14 #include "base/gtest_prod_util.h"
     15 #include "base/memory/ref_counted.h"
     16 #include "base/memory/scoped_ptr.h"
     17 #include "base/memory/singleton.h"
     18 #include "base/memory/weak_ptr.h"
     19 #include "base/observer_list.h"
     20 #include "base/timer/timer.h"
     21 #include "base/values.h"
     22 #include "chrome/browser/chromeos/cros/network_constants.h"
     23 #include "chromeos/network/network_ip_config.h"
     24 #include "chromeos/network/network_ui_data.h"
     25 #include "chromeos/network/network_util.h"
     26 #include "chromeos/network/onc/onc_constants.h"
     27 
     28 namespace chromeos {
     29 
     30 class CertificatePattern;
     31 class EnrollmentDelegate;
     32 class NetworkDeviceParser;
     33 class NetworkParser;
     34 
     35 // This is the list of all implementation classes that are allowed
     36 // access to the internals of the network library classes.
     37 #define NETWORK_LIBRARY_IMPL_FRIENDS            \
     38   friend class NetworkLibraryImplBase;          \
     39   friend class NetworkLibraryImplCros;          \
     40   friend class NetworkLibraryImplStub;
     41 
     42 
     43 // Simple wrapper for property Cellular.FoundNetworks.
     44 struct FoundCellularNetwork {
     45   FoundCellularNetwork();
     46   ~FoundCellularNetwork();
     47 
     48   std::string status;
     49   std::string network_id;
     50   std::string short_name;
     51   std::string long_name;
     52   std::string technology;
     53 };
     54 typedef std::vector<FoundCellularNetwork> CellularNetworkList;
     55 
     56 struct CellularApn {
     57   std::string apn;
     58   std::string network_id;
     59   std::string username;
     60   std::string password;
     61   std::string name;
     62   std::string localized_name;
     63   std::string language;
     64 
     65   CellularApn();
     66   CellularApn(const std::string& apn, const std::string& network_id,
     67       const std::string& username, const std::string& password);
     68   ~CellularApn();
     69   void Set(const base::DictionaryValue& dict);
     70 };
     71 typedef std::vector<CellularApn> CellularApnList;
     72 
     73 // The value of priority if it is not set.
     74 const int kPriorityNotSet = 0;
     75 // The value of priority if network is preferred.
     76 const int kPriorityPreferred = 1;
     77 
     78 // Contains data related to the shill.Device interface,
     79 // e.g. ethernet, wifi, cellular.
     80 // TODO(dpolukhin): refactor to make base class and device specific derivatives.
     81 class NetworkDevice {
     82  public:
     83   explicit NetworkDevice(const std::string& device_path);
     84   ~NetworkDevice();
     85 
     86   NetworkDeviceParser* device_parser() { return device_parser_.get(); }
     87   void SetNetworkDeviceParser(NetworkDeviceParser* parser);
     88 
     89   // Device info.
     90   const std::string& device_path() const { return device_path_; }
     91   const std::string& name() const { return name_; }
     92   const std::string& unique_id() const { return unique_id_; }
     93   ConnectionType type() const { return type_; }
     94   bool scanning() const { return scanning_; }
     95   const std::string& meid() const { return meid_; }
     96   const std::string& iccid() const { return iccid_; }
     97   const std::string& imei() const { return imei_; }
     98   const std::string& imsi() const { return imsi_; }
     99   const std::string& esn() const { return esn_; }
    100   const std::string& mdn() const { return mdn_; }
    101   const std::string& min() const { return min_; }
    102   const std::string& model_id() const { return model_id_; }
    103   const std::string& manufacturer() const { return manufacturer_; }
    104   SimLockState sim_lock_state() const { return sim_lock_state_; }
    105   bool is_sim_locked() const {
    106     return sim_lock_state_ == SIM_LOCKED_PIN ||
    107         sim_lock_state_ == SIM_LOCKED_PUK;
    108   }
    109   // Returns true if GSM modem and SIM as absent, otherwise
    110   // returns false: GSM modem and SIM card is present or CDMA modem.
    111   bool is_sim_absent() const {
    112     return technology_family() == TECHNOLOGY_FAMILY_GSM &&
    113            !is_sim_locked() && !sim_present_;
    114   }
    115   int sim_retries_left() const { return sim_retries_left_; }
    116   SimPinRequire sim_pin_required() const { return sim_pin_required_; }
    117   bool powered() const { return powered_; }
    118   const std::string& firmware_revision() const { return firmware_revision_; }
    119   const std::string& hardware_revision() const { return hardware_revision_; }
    120   const unsigned int prl_version() const { return prl_version_; }
    121   const std::string& home_provider_code() const { return home_provider_code_; }
    122   const std::string& home_provider_country() const {
    123     return home_provider_country_;
    124   }
    125   const std::string& home_provider_id() const { return home_provider_id_; }
    126   const std::string& home_provider_name() const { return home_provider_name_; }
    127   const bool provider_requires_roaming() const {
    128     return provider_requires_roaming_;
    129   }
    130   const std::string& selected_cellular_network() const {
    131     return selected_cellular_network_;
    132   }
    133   const CellularNetworkList& found_cellular_networks() const {
    134     return found_cellular_networks_;
    135   }
    136   bool data_roaming_allowed() const { return data_roaming_allowed_; }
    137   bool support_network_scan() const { return support_network_scan_; }
    138   std::string carrier() const { return carrier_; }
    139   base::ListValue* supported_carriers() const {
    140     return supported_carriers_.get();
    141   }
    142   enum TechnologyFamily technology_family() const { return technology_family_; }
    143   const CellularApnList& provider_apn_list() const {
    144     return provider_apn_list_;
    145   }
    146 
    147   // Updates the property specified by |key| with the contents of
    148   // |value|.  Returns false on failure.  Upon success, returns the
    149   // PropertyIndex that was updated in |index|.  |index| may be NULL
    150   // if not needed.
    151   bool UpdateStatus(const std::string& key,
    152                     const base::Value& value,
    153                     PropertyIndex* index);
    154 
    155  protected:
    156   void set_unique_id(const std::string& unique_id) { unique_id_ = unique_id; }
    157 
    158  private:
    159   // This allows NetworkDeviceParser and its subclasses access to
    160   // device privates so that they can be reconstituted during parsing.
    161   // The parsers only access things through the private set_ functions
    162   // so that this class can evolve without having to change all the
    163   // parsers.
    164   friend class NativeNetworkDeviceParser;
    165 
    166   // This allows the implementation classes access to privates.
    167   NETWORK_LIBRARY_IMPL_FRIENDS;
    168 
    169   // Use these functions at your peril.  They are used by the various
    170   // parsers to set state, and really shouldn't be used by anyone
    171   // else.
    172   void set_device_path(const std::string& device_path) {
    173     device_path_ = device_path;
    174   }
    175   void set_name(const std::string& name) { name_ = name; }
    176   void set_type(ConnectionType type) { type_ = type; }
    177   void set_scanning(bool scanning) { scanning_ = scanning; }
    178   void set_meid(const std::string& meid) { meid_ = meid; }
    179   void set_iccid(const std::string& iccid) { iccid_ = iccid; }
    180   void set_imei(const std::string& imei) { imei_ = imei; }
    181   void set_imsi(const std::string& imsi) { imsi_ = imsi; }
    182   void set_esn(const std::string& esn) { esn_ = esn; }
    183   void set_mdn(const std::string& mdn) { mdn_ = mdn; }
    184   void set_min(const std::string& min) { min_ = min; }
    185   void set_technology_family(TechnologyFamily technology_family) {
    186     technology_family_ = technology_family;
    187   }
    188   void set_carrier(const std::string& carrier) { carrier_ = carrier; }
    189   void set_supported_carriers(const base::ListValue& supported_carriers) {
    190     supported_carriers_.reset(supported_carriers.DeepCopy());
    191   }
    192   void set_home_provider_code(const std::string& home_provider_code) {
    193     home_provider_code_ = home_provider_code;
    194   }
    195   void set_home_provider_country(const std::string& home_provider_country) {
    196     home_provider_country_ = home_provider_country;
    197   }
    198   void set_home_provider_id(const std::string& home_provider_id) {
    199     home_provider_id_ = home_provider_id;
    200   }
    201   void set_home_provider_name(const std::string& home_provider_name) {
    202     home_provider_name_ = home_provider_name;
    203   }
    204   void set_provider_requires_roaming(bool provider_requires_roaming) {
    205     provider_requires_roaming_ = provider_requires_roaming;
    206   }
    207   void set_model_id(const std::string& model_id) { model_id_ = model_id; }
    208   void set_manufacturer(const std::string& manufacturer) {
    209     manufacturer_ = manufacturer;
    210   }
    211   void set_prl_version(int prl_version) {
    212     prl_version_ = prl_version;
    213   }
    214   void set_sim_lock_state(SimLockState sim_lock_state) {
    215     sim_lock_state_ = sim_lock_state;
    216   }
    217   void set_sim_retries_left(int sim_retries_left) {
    218     sim_retries_left_ = sim_retries_left;
    219   }
    220   void set_sim_pin_required(SimPinRequire sim_pin_required) {
    221     sim_pin_required_ = sim_pin_required;
    222   }
    223   void set_sim_present(bool sim_present) {
    224     sim_present_ = sim_present;
    225   }
    226   void set_powered(bool powered) {
    227     powered_ = powered;
    228   }
    229   void set_firmware_revision(const std::string& firmware_revision) {
    230     firmware_revision_ = firmware_revision;
    231   }
    232   void set_hardware_revision(const std::string& hardware_revision) {
    233     hardware_revision_ = hardware_revision;
    234   }
    235   void set_selected_cellular_network(
    236       const std::string& selected_cellular_network) {
    237     selected_cellular_network_ = selected_cellular_network;
    238   }
    239   void set_found_cellular_networks(
    240       const CellularNetworkList& found_cellular_networks) {
    241     found_cellular_networks_ = found_cellular_networks;
    242   }
    243   void set_data_roaming_allowed(bool data_roaming_allowed) {
    244     data_roaming_allowed_ = data_roaming_allowed;
    245   }
    246   void set_support_network_scan(bool support_network_scan) {
    247     support_network_scan_ = support_network_scan;
    248   }
    249   void set_provider_apn_list(const CellularApnList& provider_apn_list) {
    250     provider_apn_list_ = provider_apn_list;
    251   }
    252 
    253   void ParseInfo(const base::DictionaryValue& info);
    254 
    255   // General device info.
    256   std::string device_path_;
    257   std::string name_;
    258   std::string unique_id_;
    259   ConnectionType type_;
    260   bool scanning_;
    261   // Cellular specific device info.
    262   TechnologyFamily technology_family_;
    263   std::string carrier_;
    264   scoped_ptr<base::ListValue> supported_carriers_;
    265   std::string home_provider_code_;
    266   std::string home_provider_country_;
    267   std::string home_provider_id_;
    268   std::string home_provider_name_;
    269   bool provider_requires_roaming_;
    270   std::string meid_;
    271   std::string imei_;
    272   std::string iccid_;
    273   std::string imsi_;
    274   std::string esn_;
    275   std::string mdn_;
    276   std::string min_;
    277   std::string model_id_;
    278   std::string manufacturer_;
    279   SimLockState sim_lock_state_;
    280   int sim_retries_left_;
    281   SimPinRequire sim_pin_required_;
    282   bool sim_present_;
    283   bool powered_;
    284   std::string firmware_revision_;
    285   std::string hardware_revision_;
    286   int prl_version_;
    287   std::string selected_cellular_network_;
    288   CellularNetworkList found_cellular_networks_;
    289   bool data_roaming_allowed_;
    290   bool support_network_scan_;
    291   CellularApnList provider_apn_list_;
    292 
    293   // This is the parser we use to parse messages from the native
    294   // network layer.
    295   scoped_ptr<NetworkDeviceParser> device_parser_;
    296 
    297   DISALLOW_COPY_AND_ASSIGN(NetworkDevice);
    298 };
    299 
    300 // Contains data common to all network service types.
    301 class Network {
    302  public:
    303   virtual ~Network();
    304 
    305   // Test API for accessing setters in tests.
    306   class TestApi {
    307    public:
    308     explicit TestApi(Network* network) : network_(network) {}
    309     void SetConnected() {
    310       network_->set_connected();
    311     }
    312     void SetConnecting() {
    313       network_->set_connecting();
    314     }
    315     void SetDisconnected() {
    316       network_->set_disconnected();
    317     }
    318     void SetUserConnectState(UserConnectState user_connect_state) {
    319       network_->set_user_connect_state(user_connect_state);
    320     }
    321 
    322    private:
    323     Network* network_;
    324   };
    325   friend class TestApi;
    326 
    327   const std::string& service_path() const { return service_path_; }
    328   const std::string& name() const { return name_; }
    329   const std::string& device_path() const { return device_path_; }
    330   const std::string& ip_address() const { return ip_address_; }
    331   ConnectionType type() const { return type_; }
    332   ConnectionState connection_state() const { return state_; }
    333   bool connecting() const { return IsConnectingState(state_); }
    334   bool configuring() const { return state_ == STATE_CONFIGURATION; }
    335   bool connected() const { return IsConnectedState(state_); }
    336   bool connecting_or_connected() const { return connecting() || connected(); }
    337   // True when a user-initiated connection attempt is in progress
    338   bool connection_started() const {
    339     return user_connect_state_ == USER_CONNECT_STARTED;
    340   }
    341   UserConnectState user_connect_state() const { return user_connect_state_; }
    342   bool failed() const { return state_ == STATE_FAILURE; }
    343   bool disconnected() const { return IsDisconnectedState(state_); }
    344   bool online() const { return state_ == STATE_ONLINE; }
    345   bool restricted_pool() const { return state_ == STATE_PORTAL; }
    346   ConnectionError error() const { return error_; }
    347   ConnectionState state() const { return state_; }
    348   // Is this network connectable. Currently, this is mainly used by 802.1x
    349   // networks to specify that the network is not configured yet.
    350   bool connectable() const { return connectable_; }
    351   // Is this the active network, i.e, the one through which
    352   // network traffic is being routed? A network can be connected,
    353   // but not be carrying traffic.
    354   bool is_active() const { return is_active_; }
    355   bool preferred() const { return priority_ != kPriorityNotSet; }
    356   bool auto_connect() const { return auto_connect_; }
    357   bool save_credentials() const { return save_credentials_; }
    358 
    359   bool added() const { return added_; }
    360   bool notify_failure() const { return notify_failure_; }
    361   const std::string& profile_path() const { return profile_path_; }
    362   NetworkProfileType profile_type() const { return profile_type_; }
    363 
    364   const std::string& unique_id() const { return unique_id_; }
    365   int priority_order() const { return priority_order_; }
    366 
    367   const NetworkUIData& ui_data() const { return ui_data_; }
    368 
    369   void set_notify_failure(bool state) { notify_failure_ = state; }
    370 
    371   void SetPreferred(bool preferred);
    372 
    373   void SetAutoConnect(bool auto_connect);
    374 
    375   void SetName(const std::string& name);
    376 
    377   void SetSaveCredentials(bool save_credentials);
    378 
    379   void ClearUIData();
    380 
    381   // This will resolve any automatic configuration that has to occur
    382   // (provisioning certificates, etc.) before attempting to connect to the
    383   // network.  When configuration is complete, calls the closure to finish the
    384   // connection or show the config dialog to collect user-supplied info.
    385   virtual void AttemptConnection(const base::Closure& connect);
    386 
    387   // Return a string representation of the state code.
    388   std::string GetStateString() const;
    389 
    390   // Return a string representation of the error code.
    391   std::string GetErrorString() const;
    392 
    393   // Return true if the network must be in the user profile (e.g. has certs).
    394   virtual bool RequiresUserProfile() const;
    395 
    396   // Copy any credentials from a remembered network that are unset in |this|.
    397   virtual void CopyCredentialsFromRemembered(Network* remembered);
    398 
    399   // Static helper functions.
    400   static bool IsConnectedState(ConnectionState state) {
    401     return (state == STATE_READY ||
    402             state == STATE_ONLINE ||
    403             state == STATE_PORTAL);
    404   }
    405   static bool IsConnectingState(ConnectionState state) {
    406     return (state == STATE_CONNECT_REQUESTED ||
    407             state == STATE_ASSOCIATION ||
    408             state == STATE_CONFIGURATION ||
    409             state == STATE_CARRIER);
    410   }
    411   static bool IsDisconnectedState(ConnectionState state) {
    412     return (state == STATE_UNKNOWN ||
    413             state == STATE_IDLE ||
    414             state == STATE_DISCONNECT ||
    415             state == STATE_FAILURE ||
    416             state == STATE_ACTIVATION_FAILURE);
    417   }
    418 
    419   // Adopts the given enrollment handler to handle any certificate enrollment
    420   // URIs encountered during network connection.
    421   void SetEnrollmentDelegate(EnrollmentDelegate* delegate);
    422 
    423   virtual bool UpdateStatus(const std::string& key,
    424                             const base::Value& value,
    425                             PropertyIndex* index);
    426 
    427   // Creates a Network object for the given type for testing.
    428   static Network* CreateForTesting(ConnectionType type);
    429 
    430  protected:
    431   Network(const std::string& service_path,
    432           ConnectionType type);
    433 
    434   NetworkParser* network_parser() { return network_parser_.get(); }
    435   void SetNetworkParser(NetworkParser* parser);
    436 
    437   // Set the state and update flags if necessary.
    438   void SetState(ConnectionState state);
    439 
    440   // Set the error state and update notify_failure_
    441   void SetError(ConnectionError error);
    442 
    443   // Parse name/value pairs from libcros.
    444   virtual void ParseInfo(const base::DictionaryValue& info);
    445 
    446   // Erase cached credentials, used when "Save password" is unchecked.
    447   virtual void EraseCredentials();
    448 
    449   // Calculate a unique identifier for the network.
    450   virtual void CalculateUniqueId();
    451 
    452   // Methods to asynchronously set network service properties
    453   virtual void SetStringProperty(const char* prop, const std::string& str,
    454                                  std::string* dest);
    455   virtual void SetBooleanProperty(const char* prop, bool b, bool* dest);
    456   virtual void SetIntegerProperty(const char* prop, int i, int* dest);
    457   virtual void SetValueProperty(const char* prop, const base::Value& val);
    458   virtual void ClearProperty(const char* prop);
    459 
    460   // This will clear the property if string is empty. Otherwise, it will set it.
    461   virtual void SetOrClearStringProperty(const char* prop,
    462                                         const std::string& str,
    463                                         std::string* dest);
    464 
    465   void set_unique_id(const std::string& unique_id) { unique_id_ = unique_id; }
    466   const CertificatePattern& client_cert_pattern() const {
    467     return ui_data_.certificate_pattern();
    468   }
    469 
    470   ClientCertType client_cert_type() const {
    471     return ui_data_.certificate_type();
    472   }
    473 
    474   EnrollmentDelegate* enrollment_delegate() const {
    475     return enrollment_delegate_.get();
    476   }
    477 
    478  private:
    479   // This allows NetworkParser and its subclasses access to device
    480   // privates so that they can be reconstituted during parsing.  The
    481   // parsers only access things through the private set_ functions so
    482   // that this class can evolve without having to change all the
    483   // parsers.
    484   friend class NetworkParser;
    485   friend class NativeNetworkParser;
    486   friend class NativeVirtualNetworkParser;
    487   // We reach directly into the network for testing purposes.
    488   friend class MobileActivatorTest;
    489   // This allows the implementation classes access to privates.
    490   NETWORK_LIBRARY_IMPL_FRIENDS;
    491 
    492   // Use these functions at your peril.  They are used by the various
    493   // parsers to set state, and really shouldn't be used by anything else
    494   // because they don't do the error checking and sending to the
    495   // network layer that the other setters do.
    496   void set_device_path(const std::string& device_path) {
    497     device_path_ = device_path;
    498   }
    499   void set_name(const std::string& name) { name_ = name; }
    500   void set_connecting();
    501   void set_behind_portal() {
    502     state_ = STATE_PORTAL;
    503   }
    504   void set_connected() {
    505     state_ = STATE_ONLINE;
    506   }
    507   void set_disconnected() {
    508     state_ = STATE_IDLE;
    509   }
    510   void set_connectable(bool connectable) { connectable_ = connectable; }
    511   void set_user_connect_state(UserConnectState user_connect_state) {
    512     user_connect_state_ = user_connect_state;
    513   }
    514   void set_is_active(bool is_active) { is_active_ = is_active; }
    515   void set_added(bool added) { added_ = added; }
    516   void set_auto_connect(bool auto_connect) { auto_connect_ = auto_connect; }
    517   void set_save_credentials(bool save_credentials) {
    518     save_credentials_ = save_credentials;
    519   }
    520   void set_profile_path(const std::string& path) { profile_path_ = path; }
    521   void set_profile_type(NetworkProfileType type) { profile_type_ = type; }
    522   void set_ui_data(const NetworkUIData& ui_data) {
    523     ui_data_ = ui_data;
    524   }
    525   void set_client_cert_pattern(const CertificatePattern& pattern) {
    526     ui_data_.set_certificate_pattern(pattern);
    527   }
    528   void set_client_cert_type(ClientCertType type) {
    529     ui_data_.set_certificate_type(type);
    530   }
    531 
    532   // Set the profile path and update the flimflam property.
    533   void SetProfilePath(const std::string& profile_path);
    534 
    535   // Trigger an asynchronous initialization the IP address field.
    536   void InitIPAddress();
    537 
    538   // Initialize the IP address field.
    539   static void InitIPAddressCallback(
    540       const std::string& service_path,
    541       const NetworkIPConfigVector& ip_configs,
    542       const std::string& hardware_address);
    543 
    544   std::string device_path_;
    545   std::string name_;
    546   std::string ip_address_;
    547   ConnectionState state_;
    548   ConnectionError error_;
    549   bool connectable_;
    550   UserConnectState user_connect_state_;
    551   bool is_active_;
    552   int priority_;  // determines order in network list.
    553   bool auto_connect_;
    554   bool save_credentials_;  // save passphrase and EAP credentials to disk.
    555   scoped_ptr<EnrollmentDelegate> enrollment_delegate_;
    556 
    557   // Unique identifier, set the first time the network is parsed.
    558   std::string unique_id_;
    559 
    560   // Priority value, corresponds to index in list from shill (0 = first)
    561   int priority_order_;
    562 
    563   // Set to true if the UI requested this as a new network.
    564   bool added_;
    565 
    566   // Set to true when a new connection failure occurs; cleared when observers
    567   // are notified.
    568   bool notify_failure_;
    569 
    570   // Profile path for networks.
    571   std::string profile_path_;
    572 
    573   // Set to profile type based on profile_path_.
    574   NetworkProfileType profile_type_;
    575 
    576   // These must not be modified after construction.
    577   std::string service_path_;
    578   ConnectionType type_;
    579 
    580   // UI-level state that is opaque to the connection manager. The value is
    581   // stored in JSON-serialized from in the connection manager.
    582   NetworkUIData ui_data_;
    583 
    584   // This is the parser we use to parse messages from the native
    585   // network layer.
    586   scoped_ptr<NetworkParser> network_parser_;
    587 
    588   DISALLOW_COPY_AND_ASSIGN(Network);
    589 };
    590 
    591 // Class for networks of TYPE_ETHERNET.
    592 class EthernetNetwork : public Network {
    593  public:
    594   explicit EthernetNetwork(const std::string& service_path);
    595  private:
    596   // This allows the implementation classes access to privates.
    597   NETWORK_LIBRARY_IMPL_FRIENDS;
    598 
    599   DISALLOW_COPY_AND_ASSIGN(EthernetNetwork);
    600 };
    601 
    602 // Class for networks of TYPE_VPN.
    603 class VirtualNetwork : public Network {
    604  public:
    605   explicit VirtualNetwork(const std::string& service_path);
    606   virtual ~VirtualNetwork();
    607 
    608   const std::string& server_hostname() const { return server_hostname_; }
    609   ProviderType provider_type() const { return provider_type_; }
    610   const std::string& ca_cert_pem() const { return ca_cert_pem_; }
    611   const std::string& psk_passphrase() const { return psk_passphrase_; }
    612   const std::string& client_cert_id() const { return client_cert_id_; }
    613   const std::string& username() const { return username_; }
    614   const std::string& user_passphrase() const { return user_passphrase_; }
    615   const std::string& group_name() const { return group_name_; }
    616 
    617   // Sets the well-known PKCS#11 slot and PIN for accessing certificates.
    618   void SetCertificateSlotAndPin(
    619       const std::string& slot, const std::string& pin);
    620 
    621   // Network overrides.
    622   virtual bool RequiresUserProfile() const OVERRIDE;
    623   virtual void CopyCredentialsFromRemembered(Network* remembered) OVERRIDE;
    624   virtual void AttemptConnection(const base::Closure& connect) OVERRIDE;
    625 
    626   // Public getters.
    627   bool NeedMoreInfoToConnect() const;
    628   std::string GetProviderTypeString() const;
    629   // Returns true if a PSK passphrase is required to connect.
    630   bool IsPSKPassphraseRequired() const;
    631   // Returns true if a user passphrase is required to connect.
    632   bool IsUserPassphraseRequired() const;
    633 
    634   // Public setters.
    635   void SetCACertPEM(const std::string& ca_cert_pem);
    636   void SetL2TPIPsecPSKCredentials(const std::string& psk_passphrase,
    637                                   const std::string& username,
    638                                   const std::string& user_passphrase,
    639                                   const std::string& group_name);
    640   void SetL2TPIPsecCertCredentials(const std::string& client_cert_id,
    641                                    const std::string& username,
    642                                    const std::string& user_passphrase,
    643                                    const std::string& group_name);
    644   void SetOpenVPNCredentials(const std::string& client_cert_id,
    645                              const std::string& username,
    646                              const std::string& user_passphrase,
    647                              const std::string& otp);
    648   void SetServerHostname(const std::string& server_hostname);
    649 
    650  private:
    651   // This allows NetworkParser and its subclasses access to
    652   // device privates so that they can be reconstituted during parsing.
    653   // The parsers only access things through the private set_ functions
    654   // so that this class can evolve without having to change all the
    655   // parsers.
    656   friend class NativeNetworkParser;
    657   friend class NativeVirtualNetworkParser;
    658 
    659   // This allows the implementation classes access to privates.
    660   NETWORK_LIBRARY_IMPL_FRIENDS;
    661 
    662   // Use these functions at your peril.  They are used by the various
    663   // parsers to set state, and really shouldn't be used by anything else
    664   // because they don't do the error checking and sending to the
    665   // network layer that the other setters do.
    666   void set_server_hostname(const std::string& server_hostname) {
    667     server_hostname_ = server_hostname;
    668   }
    669   void set_provider_type(ProviderType provider_type) {
    670     provider_type_ = provider_type;
    671   }
    672   void set_ca_cert_pem(const std::string& ca_cert_pem) {
    673     ca_cert_pem_ = ca_cert_pem;
    674   }
    675   void set_psk_passphrase(const std::string& psk_passphrase) {
    676     psk_passphrase_ = psk_passphrase;
    677   }
    678   void set_psk_passphrase_required(bool psk_passphrase_required) {
    679     psk_passphrase_required_ = psk_passphrase_required;
    680   }
    681   void set_client_cert_id(const std::string& client_cert_id) {
    682     client_cert_id_ = client_cert_id;
    683   }
    684   void set_username(const std::string& username) { username_ = username; }
    685   void set_user_passphrase(const std::string& user_passphrase) {
    686     user_passphrase_ = user_passphrase;
    687   }
    688   void set_user_passphrase_required(bool user_passphrase_required) {
    689     user_passphrase_required_ = user_passphrase_required;
    690   }
    691   void set_group_name(const std::string& group_name) {
    692     group_name_ = group_name;
    693   }
    694 
    695   // Matches the client certificate pattern by checking to see if a certificate
    696   // exists that meets the pattern criteria. If it finds one, it sets the
    697   // appropriate network property. If not, it passes |connect| to the
    698   // EnrollmentDelegate to do something with the enrollment URI (e.g. launch a
    699   // dialog) to install the certificate, and then invoke |connect|. If
    700   // |allow_enroll| is false, then the enrollment handler will not be invoked in
    701   // the case of a missing certificate.
    702   void MatchCertificatePattern(bool allow_enroll, const base::Closure& connect);
    703 
    704  // Network overrides.
    705   virtual void EraseCredentials() OVERRIDE;
    706   virtual void CalculateUniqueId() OVERRIDE;
    707 
    708   std::string server_hostname_;
    709   ProviderType provider_type_;
    710   std::string ca_cert_pem_;
    711   std::string psk_passphrase_;
    712   bool psk_passphrase_required_;
    713   // PKCS#11 ID for client certificate.
    714   std::string client_cert_id_;
    715   std::string username_;
    716   std::string user_passphrase_;
    717   bool user_passphrase_required_;
    718   std::string group_name_;
    719 
    720   // Weak pointer factory for wrapping pointers to this network in callbacks.
    721   base::WeakPtrFactory<VirtualNetwork> weak_pointer_factory_;
    722 
    723   DISALLOW_COPY_AND_ASSIGN(VirtualNetwork);
    724 };
    725 typedef std::vector<VirtualNetwork*> VirtualNetworkVector;
    726 
    727 // Base class for networks of TYPE_WIFI or TYPE_CELLULAR.
    728 class WirelessNetwork : public Network {
    729  public:
    730   // Test API for accessing setters in tests.
    731   class TestApi {
    732    public:
    733     explicit TestApi(WirelessNetwork* network) : network_(network) {}
    734     void SetStrength(int strength) { network_->set_strength(strength); }
    735    private:
    736     WirelessNetwork* network_;
    737   };
    738   friend class TestApi;
    739 
    740   int strength() const { return strength_; }
    741 
    742  protected:
    743   WirelessNetwork(const std::string& service_path,
    744                   ConnectionType type)
    745       : Network(service_path, type), strength_(0) {}
    746 
    747  private:
    748   // This allows NativeWirelessNetworkParser access to device privates
    749   // so that they can be reconstituted during parsing.  The parsers
    750   // only access things through the private set_ functions so that
    751   // this class can evolve without having to change all the parsers.
    752   friend class NativeWirelessNetworkParser;
    753 
    754   // This allows the implementation classes access to privates.
    755   NETWORK_LIBRARY_IMPL_FRIENDS;
    756 
    757   // The friend parsers use this.
    758   void set_strength(int strength) { strength_ = strength; }
    759 
    760   int strength_;  // 0-100
    761 
    762   DISALLOW_COPY_AND_ASSIGN(WirelessNetwork);
    763 };
    764 
    765 // Class for networks of TYPE_CELLULAR.
    766 class CellularNetwork : public WirelessNetwork {
    767  public:
    768   // Test API for accessing setters in tests.
    769   class TestApi {
    770    public:
    771     explicit TestApi(CellularNetwork* network) : network_(network) {}
    772     void SetRoamingState(NetworkRoamingState roaming_state) {
    773       network_->set_roaming_state(roaming_state);
    774     }
    775    private:
    776     CellularNetwork* network_;
    777   };
    778   friend class TestApi;
    779 
    780   explicit CellularNetwork(const std::string& service_path);
    781   virtual ~CellularNetwork();
    782 
    783   // Starts device activation process. Returns false if the device state does
    784   // not permit activation.
    785   virtual bool StartActivation();
    786   virtual void CompleteActivation();
    787 
    788   bool activate_over_non_cellular_network() const {
    789     return activate_over_non_cellular_network_;
    790   }
    791   const ActivationState activation_state() const { return activation_state_; }
    792   bool activated() const {
    793     return activation_state() == ACTIVATION_STATE_ACTIVATED;
    794   }
    795   const NetworkTechnology network_technology() const {
    796     return network_technology_;
    797   }
    798   const NetworkRoamingState roaming_state() const { return roaming_state_; }
    799   const std::string& operator_name() const { return operator_name_; }
    800   const std::string& operator_code() const { return operator_code_; }
    801   const std::string& operator_country() const { return operator_country_; }
    802   bool out_of_credits() const { return out_of_credits_; }
    803   const std::string& payment_url() const { return payment_url_; }
    804   const std::string& usage_url() const { return usage_url_; }
    805   const std::string& post_data() const { return post_data_; }
    806   const bool using_post() const { return using_post_; }
    807   const CellularApn& apn() const { return apn_; }
    808   const CellularApn& last_good_apn() const { return last_good_apn_; }
    809 
    810   // Sets the APN to use in establishing data connections. Only
    811   // the fields of the APN that are needed for making connections
    812   // are passed to shill. The name, localized_name, and language
    813   // fields are ignored.
    814   void SetApn(const CellularApn& apn);
    815 
    816   // Returns true if network supports activation.
    817   bool SupportsActivation() const;
    818 
    819   // Returns whether the network needs to be activated.
    820   bool NeedsActivation() const;
    821 
    822   // Return a string representation of network technology.
    823   std::string GetNetworkTechnologyString() const;
    824   // Return a string representation of activation state.
    825   std::string GetActivationStateString() const;
    826   // Return a string representation of roaming state.
    827   std::string GetRoamingStateString() const;
    828 
    829   // Return a string representation of |activation_state|.
    830   static std::string ActivationStateToString(ActivationState activation_state);
    831 
    832  private:
    833   // This allows NativeCellularNetworkParser access to device privates
    834   // so that they can be reconstituted during parsing.  The parsers
    835   // only access things through the private set_ functions so that
    836   // this class can evolve without having to change all the parsers.
    837   friend class NativeCellularNetworkParser;
    838   // We reach directly into the network for testing purposes.
    839   friend class MobileActivatorTest;
    840 
    841   // This allows the implementation classes access to privates.
    842   NETWORK_LIBRARY_IMPL_FRIENDS;
    843 
    844   // Use these functions at your peril.  They are used by the various
    845   // parsers to set state, and really shouldn't be used by anything else
    846   // because they don't do the error checking and sending to the
    847   // network layer that the other setters do.
    848   void set_activate_over_non_cellular_network(bool value) {
    849     activate_over_non_cellular_network_ = value;
    850   }
    851   void set_activation_state(ActivationState activation_state) {
    852     activation_state_ = activation_state;
    853   }
    854   void set_network_technology(NetworkTechnology network_technology) {
    855     network_technology_ = network_technology;
    856   }
    857   void set_roaming_state(NetworkRoamingState roaming_state) {
    858     roaming_state_ = roaming_state;
    859   }
    860   void set_operator_name(const std::string& operator_name) {
    861     operator_name_ = operator_name;
    862   }
    863   void set_operator_code(const std::string& operator_code) {
    864     operator_code_ = operator_code;
    865   }
    866   void set_operator_country(const std::string& operator_country) {
    867     operator_country_ = operator_country;
    868   }
    869   void set_out_of_credits(bool out_of_credits) {
    870     out_of_credits_ = out_of_credits;
    871   }
    872   void set_payment_url(const std::string& payment_url) {
    873     payment_url_ = payment_url;
    874   }
    875   void set_post_data(const std::string& post_data) {
    876     post_data_ = post_data;
    877   }
    878   void set_using_post(bool using_post) {
    879     using_post_ = using_post;
    880   }
    881   void set_usage_url(const std::string& usage_url) { usage_url_ = usage_url; }
    882   void set_apn(const base::DictionaryValue& apn) { apn_.Set(apn); }
    883   void set_last_good_apn(const base::DictionaryValue& last_good_apn) {
    884     last_good_apn_.Set(last_good_apn);
    885   }
    886 
    887   bool activate_over_non_cellular_network_;
    888   bool out_of_credits_;
    889   ActivationState activation_state_;
    890   NetworkTechnology network_technology_;
    891   NetworkRoamingState roaming_state_;
    892   // Carrier Info
    893   std::string operator_name_;
    894   std::string operator_code_;
    895   std::string operator_country_;
    896   std::string payment_url_;
    897   std::string usage_url_;
    898   std::string post_data_;
    899   bool using_post_;
    900   // Cached values
    901   CellularApn apn_;
    902   CellularApn last_good_apn_;
    903 
    904   DISALLOW_COPY_AND_ASSIGN(CellularNetwork);
    905 };
    906 typedef std::vector<CellularNetwork*> CellularNetworkVector;
    907 
    908 // Class for networks of TYPE_WIFI.
    909 class WifiNetwork : public WirelessNetwork {
    910  public:
    911   // Test API for accessing setters in tests.
    912   class TestApi {
    913    public:
    914     explicit TestApi(WifiNetwork* network) : network_(network) {}
    915     void SetEncryption(ConnectionSecurity encryption) {
    916       network_->set_encryption(encryption);
    917     }
    918     void SetSsid(const std::string& ssid) {
    919       network_->SetSsid(ssid);
    920     }
    921     void SetHexSsid(const std::string& ssid_hex) {
    922       network_->SetHexSsid(ssid_hex);
    923     }
    924    private:
    925     WifiNetwork* network_;
    926   };
    927   friend class TestApi;
    928 
    929   explicit WifiNetwork(const std::string& service_path);
    930   virtual ~WifiNetwork();
    931 
    932   bool encrypted() const { return encryption_ != SECURITY_NONE; }
    933   ConnectionSecurity encryption() const { return encryption_; }
    934   const std::string& passphrase() const { return passphrase_; }
    935   const std::string& identity() const { return identity_; }
    936   bool passphrase_required() const { return passphrase_required_; }
    937   bool hidden_ssid() const { return hidden_ssid_; }
    938   const std::string& bssid() const { return bssid_; }
    939   int frequency() const { return frequency_; }
    940 
    941   EAPMethod eap_method() const { return eap_method_; }
    942   EAPPhase2Auth eap_phase_2_auth() const { return eap_phase_2_auth_; }
    943   const std::string& eap_server_ca_cert_pem() const {
    944     return eap_server_ca_cert_pem_; }
    945   const std::string& eap_client_cert_pkcs11_id() const {
    946     return eap_client_cert_pkcs11_id_; }
    947   const bool eap_use_system_cas() const { return eap_use_system_cas_; }
    948   const std::string& eap_identity() const { return eap_identity_; }
    949   const std::string& eap_anonymous_identity() const {
    950     return eap_anonymous_identity_;
    951   }
    952   const std::string& eap_passphrase() const { return eap_passphrase_; }
    953   const bool eap_save_credentials() const { return eap_save_credentials_; }
    954 
    955   const std::string& GetPassphrase() const;
    956 
    957   // Set property and call SetNetworkServiceProperty:
    958 
    959   void SetPassphrase(const std::string& passphrase);
    960 
    961   // 802.1x properties
    962   void SetEAPMethod(EAPMethod method);
    963   void SetEAPPhase2Auth(EAPPhase2Auth auth);
    964   void SetEAPServerCaCertPEM(const std::string& ca_cert_pem);
    965   void SetEAPClientCertPkcs11Id(const std::string& pkcs11_id);
    966   void SetEAPUseSystemCAs(bool use_system_cas);
    967   void SetEAPIdentity(const std::string& identity);
    968   void SetEAPAnonymousIdentity(const std::string& identity);
    969   void SetEAPPassphrase(const std::string& passphrase);
    970 
    971   // Sets the well-known PKCS#11 PIN for accessing certificates.
    972   void SetCertificatePin(const std::string& pin);
    973 
    974   // Network overrides.
    975   virtual bool RequiresUserProfile() const OVERRIDE;
    976   virtual void AttemptConnection(const base::Closure& connect) OVERRIDE;
    977 
    978   // Return a string representation of the encryption code.
    979   // This not translated and should be only used for debugging purposes.
    980   std::string GetEncryptionString() const;
    981 
    982   // Return true if a passphrase or other input is required to connect.
    983   bool IsPassphraseRequired() const;
    984 
    985  protected:
    986    // This allows NativeWifiNetworkParser access to device privates so
    987   // that they can be reconstituted during parsing.  The parsers only
    988   // access things through the private set_ functions so that this
    989   // class can evolve without having to change all the parsers.
    990   friend class NativeWifiNetworkParser;
    991 
    992   // This allows the implementation classes access to privates.
    993   NETWORK_LIBRARY_IMPL_FRIENDS;
    994 
    995   // Use these functions at your peril.  They are used by the various
    996   // parsers to set state, and really shouldn't be used by anything else
    997   // because they don't do the error checking and sending to the
    998   // network layer that the other setters do.
    999 
   1000   bool SetSsid(const std::string& ssid);
   1001   bool SetHexSsid(const std::string& ssid_hex);
   1002 
   1003   void set_encryption(ConnectionSecurity encryption) {
   1004     encryption_ = encryption;
   1005   }
   1006   void set_passphrase(const std::string& passphrase) {
   1007     passphrase_ = passphrase;
   1008     user_passphrase_ = passphrase;
   1009   }
   1010   void set_passphrase_required(bool passphrase_required) {
   1011     passphrase_required_ = passphrase_required;
   1012   }
   1013   void set_identity(const std::string& identity) {
   1014     identity_ = identity;
   1015   }
   1016   void set_hidden_ssid(bool hidden_ssid) {
   1017     hidden_ssid_ = hidden_ssid;
   1018   }
   1019   void set_bssid(const std::string& bssid) { bssid_ = bssid; }
   1020   void set_frequency(int frequency) { frequency_ = frequency; }
   1021   void set_eap_method(EAPMethod eap_method) { eap_method_ = eap_method; }
   1022   void set_eap_phase_2_auth(EAPPhase2Auth eap_phase_2_auth) {
   1023     eap_phase_2_auth_ = eap_phase_2_auth;
   1024   }
   1025   void set_eap_server_ca_cert_pem(const std::string& eap_server_ca_cert_pem) {
   1026     eap_server_ca_cert_pem_ = eap_server_ca_cert_pem;
   1027   }
   1028   void set_eap_client_cert_pkcs11_id(
   1029       const std::string& eap_client_cert_pkcs11_id) {
   1030     eap_client_cert_pkcs11_id_ = eap_client_cert_pkcs11_id;
   1031   }
   1032   void set_eap_use_system_cas(bool eap_use_system_cas) {
   1033     eap_use_system_cas_ = eap_use_system_cas;
   1034   }
   1035   void set_eap_identity(const std::string& eap_identity) {
   1036     eap_identity_ = eap_identity;
   1037   }
   1038   void set_eap_anonymous_identity(const std::string& eap_anonymous_identity) {
   1039     eap_anonymous_identity_ = eap_anonymous_identity;
   1040   }
   1041   void set_eap_passphrase(const std::string& eap_passphrase) {
   1042     eap_passphrase_ = eap_passphrase;
   1043   }
   1044   void set_eap_save_credentials(bool save_credentials) {
   1045     eap_save_credentials_ = save_credentials;
   1046   }
   1047 
   1048   // Matches the client certificate pattern by checking to see if a certificate
   1049   // exists that meets the pattern criteria. If it finds one, it sets the
   1050   // appropriate network property. If not, it passes |connect| to the
   1051   // EnrollmentDelegate to do something with the enrollment URI (e.g. launch a
   1052   // dialog) to install the certificate, and then invoke |connect|. If
   1053   // |allow_enroll| is false, then the enrollment handler will not be invoked in
   1054   // the case of a missing certificate.
   1055   void MatchCertificatePattern(bool allow_enroll, const base::Closure& connect);
   1056 
   1057   // Network overrides.
   1058   virtual void EraseCredentials() OVERRIDE;
   1059   virtual void CalculateUniqueId() OVERRIDE;
   1060 
   1061   ConnectionSecurity encryption_;
   1062   std::string passphrase_;
   1063   bool passphrase_required_;
   1064   std::string identity_;
   1065   bool hidden_ssid_;
   1066   std::string bssid_;
   1067   int frequency_;
   1068 
   1069   EAPMethod eap_method_;
   1070   EAPPhase2Auth eap_phase_2_auth_;
   1071   std::string eap_server_ca_cert_pem_;
   1072   std::string eap_client_cert_pkcs11_id_;
   1073   bool eap_use_system_cas_;
   1074   std::string eap_identity_;
   1075   std::string eap_anonymous_identity_;
   1076   std::string eap_passphrase_;
   1077   bool eap_save_credentials_;
   1078 
   1079   // Internal state (not stored in shill).
   1080   // Passphrase set by user (stored for UI).
   1081   std::string user_passphrase_;
   1082 
   1083   // Weak pointer factory for wrapping pointers to this network in callbacks.
   1084   base::WeakPtrFactory<WifiNetwork> weak_pointer_factory_;
   1085 
   1086   DISALLOW_COPY_AND_ASSIGN(WifiNetwork);
   1087 };
   1088 
   1089 typedef std::vector<WifiNetwork*> WifiNetworkVector;
   1090 
   1091 
   1092 // Class for networks of TYPE_WIMAX.
   1093 class WimaxNetwork : public WirelessNetwork {
   1094  public:
   1095   explicit WimaxNetwork(const std::string& service_path);
   1096   virtual ~WimaxNetwork();
   1097 
   1098   bool passphrase_required() const { return passphrase_required_; }
   1099   const std::string& eap_identity() const { return eap_identity_; }
   1100   const std::string& eap_passphrase() const { return eap_passphrase_; }
   1101 
   1102   void SetEAPIdentity(const std::string& identity);
   1103   void SetEAPPassphrase(const std::string& passphrase);
   1104 
   1105  protected:
   1106   // This allows NativeWimaxNetworkParser access to device privates so
   1107   // that they can be reconstituted during parsing.  The parsers only
   1108   // access things through the private set_ functions so that this
   1109   // class can evolve without having to change all the parsers.
   1110   friend class NativeWimaxNetworkParser;
   1111 
   1112   // This allows the implementation classes access to privates.
   1113   NETWORK_LIBRARY_IMPL_FRIENDS;
   1114 
   1115   void set_eap_identity(const std::string& identity) {
   1116     eap_identity_ = identity;
   1117   }
   1118   void set_eap_passphrase(const std::string& passphrase) {
   1119     eap_passphrase_ = passphrase;
   1120   }
   1121   void set_passphrase_required(bool passphrase_required) {
   1122     passphrase_required_ = passphrase_required;
   1123   }
   1124 
   1125   // Network overrides.
   1126   virtual void EraseCredentials() OVERRIDE;
   1127   virtual void CalculateUniqueId() OVERRIDE;
   1128 
   1129   bool passphrase_required_;
   1130   std::string eap_identity_;
   1131   std::string eap_passphrase_;
   1132 
   1133   DISALLOW_COPY_AND_ASSIGN(WimaxNetwork);
   1134 };
   1135 
   1136 typedef std::vector<WimaxNetwork*> WimaxNetworkVector;
   1137 
   1138 // Geolocation data.
   1139 struct CellTower {
   1140   CellTower();
   1141 
   1142   enum RadioType {
   1143     RADIOTYPE_GSM,
   1144     RADIOTYPE_CDMA,
   1145     RADIOTYPE_WCDMA,
   1146   } radio_type;                   // GSM/WCDMA     CDMA
   1147   int mobile_country_code;        //   MCC          MCC
   1148   int mobile_network_code;        //   MNC          SID
   1149   int location_area_code;         //   LAC          NID
   1150   int cell_id;                    //   CID          BID
   1151   base::Time timestamp;  // Timestamp when this cell was primary
   1152   int signal_strength;   // Radio signal strength measured in dBm.
   1153   int timing_advance;    // Represents the distance from the cell tower.
   1154                          // Each unit is roughly 550 meters.
   1155 };
   1156 
   1157 typedef std::vector<CellTower> CellTowerVector;
   1158 
   1159 // This class handles the interaction with the ChromeOS network library APIs.
   1160 // Classes can add themselves as observers. Users can get an instance of the
   1161 // library like this: chromeos::NetworkLibrary::Get()
   1162 class NetworkLibrary {
   1163  public:
   1164   enum HardwareAddressFormat {
   1165     FORMAT_RAW_HEX,
   1166     FORMAT_COLON_SEPARATED_HEX
   1167   };
   1168 
   1169   // Used to configure which IP parameters will be specified by DHCP and which
   1170   // will be set by the user.
   1171   enum UseDHCP {
   1172     USE_DHCP_ADDRESS      = 0x1,
   1173     USE_DHCP_NETMASK      = 0x1 << 1,
   1174     USE_DHCP_GATEWAY      = 0x1 << 2,
   1175     USE_DHCP_NAME_SERVERS = 0x1 << 3,
   1176     USE_DHCP_ALL_ROUTING_INFO =
   1177         (USE_DHCP_ADDRESS |
   1178          USE_DHCP_NETMASK |
   1179          USE_DHCP_GATEWAY),
   1180   };
   1181 
   1182   class NetworkProfileObserver {
   1183    public:
   1184     // Called when the list of network profiles was changed.
   1185     virtual void OnProfileListChanged() = 0;
   1186    protected:
   1187     virtual ~NetworkProfileObserver() {}
   1188   };
   1189 
   1190   class NetworkManagerObserver {
   1191    public:
   1192     // Called when the state of the network manager has changed,
   1193     // for example, networks have appeared or disappeared.
   1194     virtual void OnNetworkManagerChanged(NetworkLibrary* obj) = 0;
   1195    protected:
   1196     virtual ~NetworkManagerObserver() {}
   1197   };
   1198 
   1199   class NetworkObserver {
   1200    public:
   1201     // Called when the state of a single network has changed,
   1202     // for example signal strength or connection state.
   1203     virtual void OnNetworkChanged(NetworkLibrary* cros,
   1204                                   const Network* network) = 0;
   1205    protected:
   1206     virtual ~NetworkObserver() {}
   1207   };
   1208 
   1209   class NetworkDeviceObserver {
   1210    public:
   1211     // Called when |device| got notification about new networks available.
   1212     virtual void OnNetworkDeviceFoundNetworks(NetworkLibrary* cros,
   1213                                               const NetworkDevice* device) {}
   1214 
   1215     // Called when |device| got notification about SIM lock change.
   1216     virtual void OnNetworkDeviceSimLockChanged(NetworkLibrary* cros,
   1217                                                const NetworkDevice* device) {}
   1218    protected:
   1219     virtual ~NetworkDeviceObserver() {}
   1220   };
   1221 
   1222   class PinOperationObserver {
   1223    public:
   1224     // Called when pin async operation has completed.
   1225     // Network is NULL when we don't have an associated Network object.
   1226     virtual void OnPinOperationCompleted(NetworkLibrary* cros,
   1227                                          PinOperationError error) = 0;
   1228    protected:
   1229     virtual ~PinOperationObserver() {}
   1230   };
   1231 
   1232   virtual ~NetworkLibrary() {}
   1233 
   1234   virtual void Init() = 0;
   1235 
   1236   // Returns true if libcros was loaded instead of stubbed out.
   1237   virtual bool IsCros() const = 0;
   1238 
   1239   virtual void AddNetworkProfileObserver(NetworkProfileObserver* observer) = 0;
   1240   virtual void RemoveNetworkProfileObserver(
   1241       NetworkProfileObserver* observer) = 0;
   1242 
   1243   virtual void AddNetworkManagerObserver(NetworkManagerObserver* observer) = 0;
   1244   virtual void RemoveNetworkManagerObserver(
   1245       NetworkManagerObserver* observer) = 0;
   1246 
   1247   // An attempt to add an observer that has already been added for a
   1248   // give service path will be ignored.
   1249   virtual void AddNetworkObserver(const std::string& service_path,
   1250                                   NetworkObserver* observer) = 0;
   1251   // Remove an observer of a single network
   1252   virtual void RemoveNetworkObserver(const std::string& service_path,
   1253                                      NetworkObserver* observer) = 0;
   1254   // Stop |observer| from observing any networks
   1255   virtual void RemoveObserverForAllNetworks(NetworkObserver* observer) = 0;
   1256 
   1257   // Add an observer for a single network device.
   1258   virtual void AddNetworkDeviceObserver(const std::string& device_path,
   1259                                         NetworkDeviceObserver* observer) = 0;
   1260   // Remove an observer for a single network device.
   1261   virtual void RemoveNetworkDeviceObserver(const std::string& device_path,
   1262                                            NetworkDeviceObserver* observer) = 0;
   1263 
   1264   virtual void AddPinOperationObserver(PinOperationObserver* observer) = 0;
   1265   virtual void RemovePinOperationObserver(PinOperationObserver* observer) = 0;
   1266 
   1267   // Return the active or default Ethernet network (or NULL if none).
   1268   virtual const EthernetNetwork* ethernet_network() const = 0;
   1269   virtual bool ethernet_connecting() const = 0;
   1270   virtual bool ethernet_connected() const = 0;
   1271 
   1272   // Return the active Wifi network (or NULL if none active).
   1273   virtual const WifiNetwork* wifi_network() const = 0;
   1274   virtual bool wifi_connecting() const = 0;
   1275   virtual bool wifi_connected() const = 0;
   1276 
   1277   // Return the active Cellular network (or NULL if none active).
   1278   virtual const CellularNetwork* cellular_network() const = 0;
   1279   virtual bool cellular_connecting() const = 0;
   1280   virtual bool cellular_connected() const = 0;
   1281 
   1282   // Return the active Wimax network (or NULL if none active).
   1283   virtual const WimaxNetwork* wimax_network() const = 0;
   1284   virtual bool wimax_connecting() const = 0;
   1285   virtual bool wimax_connected() const = 0;
   1286 
   1287   // Return the active virtual network (or NULL if none active).
   1288   virtual const VirtualNetwork* virtual_network() const = 0;
   1289   virtual bool virtual_network_connecting() const = 0;
   1290   virtual bool virtual_network_connected() const = 0;
   1291 
   1292   // Return true if any network is currently connected.
   1293   virtual bool Connected() const = 0;
   1294 
   1295   // Return true if any network is currently connecting.
   1296   virtual bool Connecting() const = 0;
   1297 
   1298   // Returns the current list of wifi networks.
   1299   virtual const WifiNetworkVector& wifi_networks() const = 0;
   1300 
   1301   // Returns the list of remembered wifi networks.
   1302   virtual const WifiNetworkVector& remembered_wifi_networks() const = 0;
   1303 
   1304   // Returns the current list of cellular networks.
   1305   virtual const CellularNetworkVector& cellular_networks() const = 0;
   1306 
   1307   // Returns the current list of Wimax networks.
   1308   virtual const WimaxNetworkVector& wimax_networks() const = 0;
   1309 
   1310   // Returns the current list of virtual networks.
   1311   virtual const VirtualNetworkVector& virtual_networks() const = 0;
   1312 
   1313   // Returns the current list of virtual networks.
   1314   virtual const VirtualNetworkVector& remembered_virtual_networks() const = 0;
   1315 
   1316   virtual const Network* active_network() const = 0;
   1317   virtual const Network* active_nonvirtual_network() const = 0;
   1318   virtual const Network* connected_network() const = 0;
   1319   virtual const Network* connecting_network() const = 0;
   1320 
   1321   virtual bool ethernet_available() const = 0;
   1322   virtual bool wifi_available() const = 0;
   1323   virtual bool wimax_available() const = 0;
   1324   virtual bool cellular_available() const = 0;
   1325 
   1326   virtual bool ethernet_enabled() const = 0;
   1327   virtual bool wifi_enabled() const = 0;
   1328   virtual bool wimax_enabled() const = 0;
   1329   virtual bool cellular_enabled() const = 0;
   1330 
   1331   virtual bool wifi_scanning() const = 0;
   1332   virtual bool cellular_initializing() const = 0;
   1333 
   1334   // Return a pointer to the device, if it exists, or NULL.
   1335   virtual const NetworkDevice* FindNetworkDeviceByPath(
   1336       const std::string& path) const = 0;
   1337 
   1338   // Returns device with TYPE_CELLULAR or TYPE_WIMAX.
   1339   // Returns NULL if none exists.
   1340   virtual const NetworkDevice* FindMobileDevice() const = 0;
   1341 
   1342   // Returns device with TYPE_CELLULAR. Returns NULL if none exists.
   1343   virtual const NetworkDevice* FindCellularDevice() const = 0;
   1344 
   1345   // Return a pointer to the network, if it exists, or NULL.
   1346   // NOTE: Never store these results, store service paths instead.
   1347   // The pattern for doing an operation on a Network is:
   1348   // Network* network = cros->FindNetworkByPath(service_path);
   1349   // network->SetFoo();
   1350   // network->Connect();
   1351   // As long as this is done in sequence on the UI thread it will be safe;
   1352   // the network list only gets updated on the UI thread.
   1353   virtual Network* FindNetworkByPath(const std::string& path) const = 0;
   1354   virtual Network* FindNetworkByUniqueId(
   1355       const std::string& unique_id) const = 0;
   1356   virtual WifiNetwork* FindWifiNetworkByPath(const std::string& path) const = 0;
   1357   virtual CellularNetwork* FindCellularNetworkByPath(
   1358       const std::string& path) const = 0;
   1359   virtual WimaxNetwork* FindWimaxNetworkByPath(
   1360       const std::string& path) const = 0;
   1361   virtual VirtualNetwork* FindVirtualNetworkByPath(
   1362       const std::string& path) const = 0;
   1363 
   1364   // Return a pointer to the remembered network, if it exists, or NULL.
   1365   virtual Network* FindRememberedNetworkByPath(
   1366       const std::string& path) const = 0;
   1367 
   1368   // Return a pointer to the ONC dictionary for a network identified by unique
   1369   // ID. Returns NULL if there is no ONC dictionary available for that network.
   1370   // The ONC dictionary is usually only present for policy-configure networks
   1371   // which get reconfigured at startup.
   1372   virtual const base::DictionaryValue* FindOncForNetwork(
   1373       const std::string& unique_id) const = 0;
   1374 
   1375   // Records information that cellular plan payment has happened.
   1376   virtual void SignalCellularPlanPayment() = 0;
   1377 
   1378   // Returns true if cellular plan payment has been recorded recently.
   1379   virtual bool HasRecentCellularPlanPayment() = 0;
   1380 
   1381   // Returns home carrier ID if available, otherwise empty string is returned.
   1382   // Carrier ID format: <carrier name> (country). Ex.: "Verizon (us)".
   1383   virtual const std::string& GetCellularHomeCarrierId() const = 0;
   1384 
   1385   // Checks if the current cellular device should be activated by directly
   1386   // calling it's activate function instead of going through the activation
   1387   // process.
   1388   // Note: Currently Sprint is the only carrier using direct activation.
   1389   virtual bool CellularDeviceUsesDirectActivation() const = 0;
   1390 
   1391   // Passes |old_pin|, |new_pin| to change SIM card PIM.
   1392   virtual void ChangePin(const std::string& old_pin,
   1393                          const std::string& new_pin) = 0;
   1394 
   1395   // Passes |pin|, |require_pin| value to change SIM card RequirePin setting.
   1396   virtual void ChangeRequirePin(bool require_pin,
   1397                                 const std::string& pin) = 0;
   1398 
   1399   // Passes |pin| to unlock SIM card.
   1400   virtual void EnterPin(const std::string& pin) = 0;
   1401 
   1402   // Passes |puk|, |new_pin| to unblock SIM card.
   1403   virtual void UnblockPin(const std::string& puk,
   1404                           const std::string& new_pin) = 0;
   1405 
   1406   // Request a scan for available cellular networks.
   1407   virtual void RequestCellularScan() = 0;
   1408 
   1409   // Request a register in cellular network with |network_id|.
   1410   virtual void RequestCellularRegister(const std::string& network_id) = 0;
   1411 
   1412   // Change data roaming restriction for current cellular device.
   1413   virtual void SetCellularDataRoamingAllowed(bool new_value) = 0;
   1414 
   1415   // Changes the active cellular carrier to the one provided, calls the closure
   1416   // once the transition is complete.
   1417   virtual void SetCarrier(const std::string& carrier,
   1418                           const NetworkOperationCallback& completed) = 0;
   1419 
   1420   // Return true if GSM SIM card can work only with enabled roaming.
   1421   virtual bool IsCellularAlwaysInRoaming() = 0;
   1422 
   1423   // Request a scan for new wifi networks.
   1424   virtual void RequestNetworkScan() = 0;
   1425 
   1426   // TODO(joth): Add GetCellTowers to retrieve a CellTowerVector.
   1427 
   1428   // Returns false if there is no way to connect to this network, even with
   1429   // user input (e.g. it requires a user profile but none is available).
   1430   virtual bool CanConnectToNetwork(const Network* network) const = 0;
   1431 
   1432   // Connect to the specified wireless network.
   1433   virtual void ConnectToWifiNetwork(WifiNetwork* network) = 0;
   1434 
   1435   // Connect to the specified wireless network and set its profile
   1436   // to SHARED if |shared| is true, otherwise to USER.
   1437   virtual void ConnectToWifiNetwork(WifiNetwork* network, bool shared) = 0;
   1438 
   1439   // Connect to the specified cellular network.
   1440   virtual void ConnectToCellularNetwork(CellularNetwork* network) = 0;
   1441 
   1442   // Connect to the specified WiMAX network.
   1443   virtual void ConnectToWimaxNetwork(WimaxNetwork* network) = 0;
   1444 
   1445   // Connect to the specified WiMAX network and set its profile
   1446   // to SHARED if |shared| is true, otherwise to USER.
   1447   virtual void ConnectToWimaxNetwork(WimaxNetwork* network, bool shared) = 0;
   1448 
   1449   // Connect to the specified virtual network.
   1450   virtual void ConnectToVirtualNetwork(VirtualNetwork* network) = 0;
   1451 
   1452   // Connect to an unconfigured network with given SSID, security, passphrase,
   1453   // and optional EAP configuration. If |security| is SECURITY_8021X,
   1454   // |eap_config| must be provided.
   1455   struct EAPConfigData {
   1456     EAPConfigData();
   1457     ~EAPConfigData();
   1458     EAPMethod method;
   1459     EAPPhase2Auth auth;
   1460     std::string server_ca_cert_pem;
   1461     bool use_system_cas;
   1462     std::string client_cert_pkcs11_id;
   1463     std::string identity;
   1464     std::string anonymous_identity;
   1465   };
   1466   virtual void ConnectToUnconfiguredWifiNetwork(
   1467       const std::string& ssid,
   1468       ConnectionSecurity security,
   1469       const std::string& passphrase,
   1470       const EAPConfigData* eap_config,
   1471       bool save_credentials,
   1472       bool shared) = 0;
   1473 
   1474   // Connect to the specified virtual network with service name.
   1475   // VPNConfigData must be provided.
   1476   struct VPNConfigData {
   1477     VPNConfigData();
   1478     ~VPNConfigData();
   1479     std::string psk;
   1480     std::string server_ca_cert_pem;
   1481     std::string client_cert_pkcs11_id;
   1482     std::string username;
   1483     std::string user_passphrase;
   1484     std::string otp;
   1485     std::string group_name;
   1486     bool save_credentials;
   1487   };
   1488   virtual void ConnectToUnconfiguredVirtualNetwork(
   1489       const std::string& service_name,
   1490       const std::string& server_hostname,
   1491       ProviderType provider_type,
   1492       const VPNConfigData& config) = 0;
   1493 
   1494   // Disconnect from the specified network.
   1495   virtual void DisconnectFromNetwork(const Network* network) = 0;
   1496 
   1497   // Forget the network corresponding to service_path.
   1498   virtual void ForgetNetwork(const std::string& service_path) = 0;
   1499 
   1500   // Enables/disables the ethernet network device.
   1501   virtual void EnableEthernetNetworkDevice(bool enable) = 0;
   1502 
   1503   // Enables/disables the wifi network device.
   1504   virtual void EnableWifiNetworkDevice(bool enable) = 0;
   1505 
   1506   // Enables/disables the wimax network device.
   1507   virtual void EnableWimaxNetworkDevice(bool enable) = 0;
   1508 
   1509   // Enables/disables the cellular network device.
   1510   virtual void EnableCellularNetworkDevice(bool enable) = 0;
   1511 
   1512   // Fetches IP configs and hardware address for a given device_path and returns
   1513   // them via the given callback.
   1514   virtual void GetIPConfigs(const std::string& device_path,
   1515                             HardwareAddressFormat format,
   1516                             const NetworkGetIPConfigsCallback& callback) = 0;
   1517 
   1518   // Sets the configuration of the IP parameters. This is called when user
   1519   // changes IP settings from dhcp to static or vice versa or when user changes
   1520   // the ip config info. If nothing is changed, this method does nothing.
   1521   // |dhcp_usage_mask| is a bitmask composed of items from the UseDHCP enum, and
   1522   // indicates which of the supplied values are overridden by values given by
   1523   // the default IP acquisition technique for the service (DHCP, usually).
   1524   virtual void SetIPParameters(const std::string& service_path,
   1525                                const std::string& address,
   1526                                const std::string& netmask,
   1527                                const std::string& gateway,
   1528                                const std::string& name_servers,
   1529                                int dhcp_usage_mask) = 0;
   1530 
   1531   // Requests the service properties associated with |service_path|. Calls
   1532   // |callback| with the properties when competed.
   1533   typedef base::Callback<void(const std::string& service_path,
   1534                               const base::DictionaryValue* properties)>
   1535       NetworkServicePropertiesCallback;
   1536   virtual void RequestNetworkServiceProperties(
   1537       const std::string& service_path,
   1538       const NetworkServicePropertiesCallback& callback) = 0;
   1539 
   1540   // Load networks from a list of NetworkConfigurations of ONC.
   1541   virtual void LoadOncNetworks(const base::ListValue& network_configs,
   1542                                onc::ONCSource source) = 0;
   1543 
   1544   // This sets the active network for the network type. Note: priority order
   1545   // is unchanged (i.e. if a wifi network is set to active, but an ethernet
   1546   // network is still active, active_network() will still return the ethernet
   1547   // network). Other networks of the same type will become inactive.
   1548   // Used for testing.
   1549   virtual bool SetActiveNetwork(ConnectionType type,
   1550                                 const std::string& service_path) = 0;
   1551 
   1552   // Factory function, creates a new instance and returns ownership.
   1553   // For normal usage, access the singleton via NetworkLibrary::Get().
   1554   static NetworkLibrary* GetImpl(bool stub);
   1555 
   1556   // Initializes the global instance.
   1557   static void Initialize(bool use_stub);
   1558 
   1559   // Destroys the global instance. Must be called before AtExitManager is
   1560   // destroyed to ensure a clean shutdown.
   1561   static void Shutdown();
   1562 
   1563   // Gets the global instance. Returns NULL if Initialize() has not been
   1564   // called (or Shutdown() has been called).
   1565   static NetworkLibrary* Get();
   1566 
   1567   // Sets the network library to be returned from Get(). The existing network
   1568   // library will be deleted.
   1569   static void SetForTesting(NetworkLibrary* network_library);
   1570 };
   1571 
   1572 // The class is used for enabling the stub libcros, and cleaning it up at
   1573 // the end of the object lifetime. Useful for testing.
   1574 class ScopedStubNetworkLibraryEnabler {
   1575  public:
   1576   ScopedStubNetworkLibraryEnabler() {
   1577     NetworkLibrary::Initialize(true);
   1578   }
   1579 
   1580   ~ScopedStubNetworkLibraryEnabler() {
   1581     NetworkLibrary::Shutdown();
   1582   }
   1583 
   1584  private:
   1585   DISALLOW_COPY_AND_ASSIGN(ScopedStubNetworkLibraryEnabler);
   1586 };
   1587 
   1588 }  // namespace chromeos
   1589 
   1590 #endif  // CHROME_BROWSER_CHROMEOS_CROS_NETWORK_LIBRARY_H_
   1591