network_state.h revision 6e8cce623b6e4fe0c9e4af605d675dd9d0338c38
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 "components/onc/onc_constants.h" 14#include "url/gurl.h" 15 16namespace base { 17class DictionaryValue; 18class Value; 19} 20 21namespace chromeos { 22 23// Simple class to provide network state information about a network service. 24// This class should always be passed as a const* and should never be held 25// on to. Store network_state->path() (defined in ManagedState) instead and 26// call NetworkStateHandler::GetNetworkState(path) to retrieve the state for 27// the network. 28// 29// Note: NetworkStateHandler will store an entry for each member of 30// Manager.ServiceCompleteList. The visible() method indicates whether the 31// network is visible, and the IsInProfile() method indicates whether the 32// network is saved in a profile. 33class CHROMEOS_EXPORT NetworkState : public ManagedState { 34 public: 35 explicit NetworkState(const std::string& path); 36 virtual ~NetworkState(); 37 38 // ManagedState overrides 39 // If you change this method, update GetProperties too. 40 virtual bool PropertyChanged(const std::string& key, 41 const base::Value& value) OVERRIDE; 42 virtual bool InitialPropertiesReceived( 43 const base::DictionaryValue& properties) OVERRIDE; 44 virtual void GetStateProperties( 45 base::DictionaryValue* dictionary) const OVERRIDE; 46 47 void IPConfigPropertiesChanged(const base::DictionaryValue& properties); 48 49 // Returns true, if the network requires a service activation. 50 bool RequiresActivation() const; 51 52 // Accessors 53 bool visible() const { return visible_; } 54 const std::string& security() const { return security_; } 55 const std::string& device_path() const { return device_path_; } 56 const std::string& guid() const { return guid_; } 57 const std::string& profile_path() const { return profile_path_; } 58 const std::string& error() const { return error_; } 59 const std::string& last_error() const { return last_error_; } 60 void clear_last_error() { last_error_.clear(); } 61 62 // Returns |connection_state_| if visible, kStateDisconnect otherwise. 63 std::string connection_state() const; 64 65 const base::DictionaryValue& proxy_config() const { return proxy_config_; } 66 67 // IPConfig Properties. These require an extra call to ShillIPConfigClient, 68 // so cache them to avoid excessively complex client code. 69 const std::string& ip_address() const { return ip_address_; } 70 const std::string& gateway() const { return gateway_; } 71 const std::vector<std::string>& dns_servers() const { return dns_servers_; } 72 const GURL& web_proxy_auto_discovery_url() const { 73 return web_proxy_auto_discovery_url_; 74 } 75 76 // Wireless property accessors 77 bool connectable() const { return connectable_; } 78 int signal_strength() const { return signal_strength_; } 79 80 // Wifi property accessors 81 const std::string& eap_method() const { return eap_method_; } 82 83 // Cellular property accessors 84 const std::string& network_technology() const { 85 return network_technology_; 86 } 87 const std::string& activation_type() const { return activation_type_; } 88 const std::string& activation_state() const { return activation_state_; } 89 const std::string& roaming() const { return roaming_; } 90 bool cellular_out_of_credits() const { return cellular_out_of_credits_; } 91 92 // Whether this network has a CACertNSS nickname set. 93 bool HasCACertNSS() const { return has_ca_cert_nss_; } 94 95 // Returns true if |connection_state_| is a connected/connecting state. 96 bool IsConnectedState() const; 97 bool IsConnectingState() const; 98 99 // Returns true if this is a network stored in a profile. 100 bool IsInProfile() const; 101 102 // Returns true if the network properties are stored in a user profile. 103 bool IsPrivate() const; 104 105 // Returns a comma separated string of name servers. 106 std::string GetDnsServersAsString() const; 107 108 // Converts the prefix length to a netmask string. 109 std::string GetNetmask() const; 110 111 // Returns a specifier for identifying this network in the absence of a GUID. 112 // This should only be used by NetworkStateHandler for keeping track of 113 // GUIDs assigned to unsaved networks. 114 std::string GetSpecifier() const; 115 116 // Set the GUID. Called exclusively by NetworkStateHandler. 117 void SetGuid(const std::string& guid); 118 119 // Helpers (used e.g. when a state or error is cached) 120 static bool StateIsConnected(const std::string& connection_state); 121 static bool StateIsConnecting(const std::string& connection_state); 122 static bool ErrorIsValid(const std::string& error); 123 124 private: 125 friend class MobileActivatorTest; 126 friend class NetworkStateHandler; 127 friend class NetworkChangeNotifierChromeosUpdateTest; 128 129 // Updates |name_| from WiFi.HexSSID if provided, and validates |name_|. 130 // Returns true if |name_| changes. 131 bool UpdateName(const base::DictionaryValue& properties); 132 133 // Set to true if the network is a member of Manager.Services. 134 bool visible_; 135 136 // Network Service properties. Avoid adding any additional properties here. 137 // Instead use NetworkConfigurationHandler::GetProperties() to asynchronously 138 // request properties from Shill. 139 std::string security_; 140 std::string eap_method_; // Needed for WiFi EAP networks 141 std::string device_path_; 142 std::string guid_; 143 std::string connection_state_; 144 std::string profile_path_; 145 bool connectable_; 146 147 // Reflects the current Shill Service.Error property. This might get cleared 148 // by Shill shortly after a failure. 149 std::string error_; 150 151 // Last non empty Service.Error property. Cleared by NetworkConnectionHandler 152 // when a connection attempt is initiated. 153 std::string last_error_; 154 155 // IPConfig properties. 156 // Note: These do not correspond to actual Shill.Service properties 157 // but are derived from the service's corresponding IPConfig object. 158 std::string ip_address_; 159 std::string gateway_; 160 std::vector<std::string> dns_servers_; 161 int prefix_length_; // Used by GetNetmask() 162 GURL web_proxy_auto_discovery_url_; 163 164 // Wireless properties, used for icons and Connect logic. 165 int signal_strength_; 166 167 // Cellular properties, used for icons, Connect, and Activation. 168 std::string network_technology_; 169 std::string activation_type_; 170 std::string activation_state_; 171 std::string roaming_; 172 bool cellular_out_of_credits_; 173 174 // Whether a deprecated CaCertNSS property of this network is set. Required 175 // for migration to PEM. 176 bool has_ca_cert_nss_; 177 178 // TODO(pneubeck): Remove this once (Managed)NetworkConfigurationHandler 179 // provides proxy configuration. crbug.com/241775 180 base::DictionaryValue proxy_config_; 181 182 DISALLOW_COPY_AND_ASSIGN(NetworkState); 183}; 184 185} // namespace chromeos 186 187#endif // CHROMEOS_NETWORK_NETWORK_STATE_H_ 188