Home | History | Annotate | Download | only in network
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef CHROMEOS_NETWORK_NETWORK_STATE_H_
      6 #define CHROMEOS_NETWORK_NETWORK_STATE_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/values.h"
     12 #include "chromeos/network/managed_state.h"
     13 #include "chromeos/network/network_ui_data.h"
     14 #include "chromeos/network/onc/onc_constants.h"
     15 #include "url/gurl.h"
     16 
     17 namespace chromeos {
     18 
     19 // Simple class to provide network state information about a network service.
     20 // This class should always be passed as a const* and should never be held
     21 // on to. Store network_state->path() (defined in ManagedState) instead and
     22 // call NetworkStateHandler::GetNetworkState(path) to retrieve the state for
     23 // the network.
     24 class CHROMEOS_EXPORT NetworkState : public ManagedState {
     25  public:
     26   typedef std::vector<int> FrequencyList;
     27 
     28   explicit NetworkState(const std::string& path);
     29   virtual ~NetworkState();
     30 
     31   // ManagedState overrides
     32   // If you change this method, update GetProperties too.
     33   virtual bool PropertyChanged(const std::string& key,
     34                                const base::Value& value) OVERRIDE;
     35   virtual bool InitialPropertiesReceived(
     36       const base::DictionaryValue& properties) OVERRIDE;
     37 
     38   // Fills |dictionary| with the state properties. All the properties that are
     39   // accepted by PropertyChanged are stored in |dictionary|, no other values are
     40   // stored.
     41   void GetProperties(base::DictionaryValue* dictionary) const;
     42 
     43   // Accessors
     44   const std::string& security() const { return security_; }
     45   const std::string& device_path() const { return device_path_; }
     46   const std::string& guid() const { return guid_; }
     47   const std::string& connection_state() const { return connection_state_; }
     48   const std::string& profile_path() const { return profile_path_; }
     49   const std::string& error() const { return error_; }
     50   const std::string& error_details() const { return error_details_; }
     51   bool auto_connect() const { return auto_connect_; }
     52   bool favorite() const { return favorite_; }
     53   int priority() const { return priority_; }
     54   const base::DictionaryValue& proxy_config() const { return proxy_config_; }
     55   const NetworkUIData& ui_data() const { return ui_data_; }
     56   // IPConfig Properties
     57   const std::string& ip_address() const { return ip_address_; }
     58   const std::string& gateway() const { return gateway_; }
     59   const std::vector<std::string>& dns_servers() const { return dns_servers_; }
     60   const int prefix_length() const { return prefix_length_; }
     61   const GURL& web_proxy_auto_discovery_url() const {
     62     return web_proxy_auto_discovery_url_;
     63   }
     64   // Wireless property accessors
     65   int signal_strength() const { return signal_strength_; }
     66   bool connectable() const { return connectable_; }
     67   // Cellular property accessors
     68   const std::string& network_technology() const {
     69     return network_technology_;
     70   }
     71   const std::string& activation_state() const { return activation_state_; }
     72   const std::string& roaming() const { return roaming_; }
     73   bool activate_over_non_cellular_networks() const {
     74     return activate_over_non_cellular_networks_;
     75   }
     76   bool cellular_out_of_credits() const { return cellular_out_of_credits_; }
     77   const std::string& usage_url() const { return usage_url_; }
     78   const std::string& payment_url() const { return payment_url_; }
     79   const std::string& post_method() const { return post_method_; }
     80   const std::string& post_data() const { return post_data_; }
     81 
     82   // Whether this network has a CACertNSS nickname set.
     83   bool HasCACertNSS() const { return has_ca_cert_nss_; }
     84 
     85   // Returns true if |connection_state_| is a connected/connecting state.
     86   bool IsConnectedState() const;
     87   bool IsConnectingState() const;
     88 
     89   // Returns true if the ONC source is a device or user policy.
     90   bool IsManaged() const;
     91 
     92   // Returns true if the network properties are stored in a user profile.
     93   bool IsPrivate() const;
     94 
     95   // Returns a comma separated string of name servers.
     96   std::string GetDnsServersAsString() const;
     97 
     98   // Converts the prefix length to a netmask string.
     99   std::string GetNetmask() const;
    100 
    101   // Helpers (used e.g. when a state is cached)
    102   static bool StateIsConnected(const std::string& connection_state);
    103   static bool StateIsConnecting(const std::string& connection_state);
    104 
    105   // Helper to return a full prefixed version of an IPConfig property
    106   // key.
    107   static std::string IPConfigProperty(const char* key);
    108 
    109   // Sets |out| to the UIData specified by |value|. Returns true if successfully
    110   // parsed.
    111   static bool GetUIDataFromValue(const base::Value& value, NetworkUIData* out);
    112 
    113   // Generates a name from properties."Wifi.HexSSID" if present, otherwise
    114   // validates properties.Name and returns a valid utf8 version.
    115   static std::string GetNameFromProperties(
    116       const std::string& service_path,
    117       const base::DictionaryValue& properties);
    118 
    119  private:
    120   friend class NetworkStateHandler;
    121   friend class NetworkChangeNotifierChromeosUpdateTest;
    122 
    123   // Updates |name_| from WiFi.HexSSID if provided, and validates |name_|.
    124   // Returns true if |name_| changes.
    125   bool UpdateName(const base::DictionaryValue& properties);
    126 
    127   // TODO(gauravsh): Audit the list of properties that we are caching. We should
    128   // only be doing this for commonly accessed properties. crbug.com/252553
    129   // Common Network Service properties
    130   std::string security_;
    131   std::string device_path_;
    132   std::string guid_;
    133   std::string connection_state_;
    134   std::string profile_path_;
    135   std::string error_;
    136   std::string error_details_;
    137   bool auto_connect_;
    138   bool favorite_;
    139   int priority_;
    140   // TODO(pneubeck): Remove ProxyConfig once NetworkConfigurationHandler
    141   // provides proxy configuration. crbug/241775
    142   base::DictionaryValue proxy_config_;
    143   NetworkUIData ui_data_;
    144   // IPConfig properties.
    145   // Note: These do not correspond to actual Shill.Service properties
    146   // but are derived from the service's corresponding IPConfig object.
    147   std::string ip_address_;
    148   std::string gateway_;
    149   std::vector<std::string> dns_servers_;
    150   int prefix_length_;
    151   GURL web_proxy_auto_discovery_url_;
    152   // Wireless properties
    153   int signal_strength_;
    154   bool connectable_;
    155   // Cellular properties
    156   std::string network_technology_;
    157   std::string activation_state_;
    158   std::string roaming_;
    159   bool activate_over_non_cellular_networks_;
    160   bool cellular_out_of_credits_;
    161   // Cellular payment portal properties.
    162   std::string usage_url_;
    163   std::string payment_url_;
    164   std::string post_method_;
    165   std::string post_data_;
    166 
    167   // Whether a deprecated CaCertNSS property of this network is set. Required
    168   // for migration to PEM.
    169   bool has_ca_cert_nss_;
    170 
    171   DISALLOW_COPY_AND_ASSIGN(NetworkState);
    172 };
    173 
    174 }  // namespace chromeos
    175 
    176 #endif  // CHROMEOS_NETWORK_NETWORK_STATE_H_
    177