network_state.h revision 424c4d7b64af9d0d8fd9624f381f469654d5e3d2
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 17namespace 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. 24class 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 bool auto_connect() const { return auto_connect_; } 51 bool favorite() const { return favorite_; } 52 int priority() const { return priority_; } 53 const base::DictionaryValue& proxy_config() const { return proxy_config_; } 54 const NetworkUIData& ui_data() const { return ui_data_; } 55 // IPConfig Properties 56 const std::string& ip_address() const { return ip_address_; } 57 const std::string& gateway() const { return gateway_; } 58 const std::vector<std::string>& dns_servers() const { return dns_servers_; } 59 const int prefix_length() const { return prefix_length_; } 60 const GURL& web_proxy_auto_discovery_url() const { 61 return web_proxy_auto_discovery_url_; 62 } 63 // Wireless property accessors 64 int signal_strength() const { return signal_strength_; } 65 bool connectable() const { return connectable_; } 66 // Cellular property accessors 67 const std::string& network_technology() const { 68 return network_technology_; 69 } 70 const std::string& activation_state() const { return activation_state_; } 71 const std::string& roaming() const { return roaming_; } 72 bool activate_over_non_cellular_networks() const { 73 return activate_over_non_cellular_networks_; 74 } 75 bool cellular_out_of_credits() const { return cellular_out_of_credits_; } 76 const std::string& usage_url() const { return usage_url_; } 77 const std::string& payment_url() const { return payment_url_; } 78 const std::string& post_method() const { return post_method_; } 79 const std::string& post_data() const { return post_data_; } 80 81 // Whether this network has a CACertNSS nickname set. 82 bool HasCACertNSS() const { return has_ca_cert_nss_; } 83 84 // Returns true if |connection_state_| is a connected/connecting state. 85 bool IsConnectedState() const; 86 bool IsConnectingState() const; 87 88 // Returns true if the ONC source is a device or user policy. 89 bool IsManaged() const; 90 91 // Returns true if the network properties are stored in a user profile. 92 bool IsPrivate() const; 93 94 // Returns a comma separated string of name servers. 95 std::string GetDnsServersAsString() const; 96 97 // Converts the prefix length to a netmask string. 98 std::string GetNetmask() const; 99 100 // Helpers (used e.g. when a state is cached) 101 static bool StateIsConnected(const std::string& connection_state); 102 static bool StateIsConnecting(const std::string& connection_state); 103 104 // Helper to return a full prefixed version of an IPConfig property 105 // key. 106 static std::string IPConfigProperty(const char* key); 107 108 // Sets |out| to the UIData specified by |value|. Returns true if successfully 109 // parsed. 110 static bool GetUIDataFromValue(const base::Value& value, NetworkUIData* out); 111 112 // Generates a name from properties."Wifi.HexSSID" if present, otherwise 113 // validates properties.Name and returns a valid utf8 version. 114 static std::string GetNameFromProperties( 115 const std::string& service_path, 116 const base::DictionaryValue& properties); 117 118 private: 119 friend class MobileActivatorTest; 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 bool auto_connect_; 137 bool favorite_; 138 int priority_; 139 // TODO(pneubeck): Remove ProxyConfig once NetworkConfigurationHandler 140 // provides proxy configuration. crbug/241775 141 base::DictionaryValue proxy_config_; 142 NetworkUIData ui_data_; 143 // IPConfig properties. 144 // Note: These do not correspond to actual Shill.Service properties 145 // but are derived from the service's corresponding IPConfig object. 146 std::string ip_address_; 147 std::string gateway_; 148 std::vector<std::string> dns_servers_; 149 int prefix_length_; 150 GURL web_proxy_auto_discovery_url_; 151 // Wireless properties 152 int signal_strength_; 153 bool connectable_; 154 // Cellular properties 155 std::string network_technology_; 156 std::string activation_state_; 157 std::string roaming_; 158 bool activate_over_non_cellular_networks_; 159 bool cellular_out_of_credits_; 160 // Cellular payment portal properties. 161 std::string usage_url_; 162 std::string payment_url_; 163 std::string post_method_; 164 std::string post_data_; 165 166 // Whether a deprecated CaCertNSS property of this network is set. Required 167 // for migration to PEM. 168 bool has_ca_cert_nss_; 169 170 DISALLOW_COPY_AND_ASSIGN(NetworkState); 171}; 172 173} // namespace chromeos 174 175#endif // CHROMEOS_NETWORK_NETWORK_STATE_H_ 176