network_state.h revision cedac228d2dd51db4b79ea1e72c7f249408ee061
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 "chromeos/network/managed_state.h" 12#include "chromeos/network/network_ui_data.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. 28class CHROMEOS_EXPORT NetworkState : public ManagedState { 29 public: 30 explicit NetworkState(const std::string& path); 31 virtual ~NetworkState(); 32 33 // ManagedState overrides 34 // If you change this method, update GetProperties too. 35 virtual bool PropertyChanged(const std::string& key, 36 const base::Value& value) OVERRIDE; 37 virtual bool InitialPropertiesReceived( 38 const base::DictionaryValue& properties) OVERRIDE; 39 virtual void GetStateProperties( 40 base::DictionaryValue* dictionary) const OVERRIDE; 41 42 void IPConfigPropertiesChanged(const base::DictionaryValue& properties); 43 44 // Returns true, if the network requires a service activation. 45 bool RequiresActivation() const; 46 47 // Accessors 48 const std::string& security() const { return security_; } 49 const std::string& device_path() const { return device_path_; } 50 const std::string& guid() const { return guid_; } 51 const std::string& connection_state() const { return connection_state_; } 52 const std::string& profile_path() const { return profile_path_; } 53 const std::string& error() const { return error_; } 54 const std::string& last_error() const { return last_error_; } 55 void clear_last_error() { last_error_.clear(); } 56 57 const NetworkUIData& ui_data() const { return ui_data_; } 58 59 // IPConfig Properties. These require an extra call to ShillIPConfigClient, 60 // so cache them to avoid excessively complex client code. 61 const std::string& ip_address() const { return ip_address_; } 62 const std::string& gateway() const { return gateway_; } 63 const std::vector<std::string>& dns_servers() const { return dns_servers_; } 64 const GURL& web_proxy_auto_discovery_url() const { 65 return web_proxy_auto_discovery_url_; 66 } 67 68 // Wireless property accessors 69 bool connectable() const { return connectable_; } 70 int signal_strength() const { return signal_strength_; } 71 72 // Wifi property accessors 73 const std::string& eap_method() const { return eap_method_; } 74 75 // Cellular property accessors 76 const std::string& network_technology() const { 77 return network_technology_; 78 } 79 const std::string& activation_state() const { return activation_state_; } 80 const std::string& roaming() const { return roaming_; } 81 bool activate_over_non_cellular_networks() const { 82 return activate_over_non_cellular_networks_; 83 } 84 bool cellular_out_of_credits() const { return cellular_out_of_credits_; } 85 86 // Whether this network has a CACertNSS nickname set. 87 bool HasCACertNSS() const { return has_ca_cert_nss_; } 88 89 // Returns true if |connection_state_| is a connected/connecting state. 90 bool IsConnectedState() const; 91 bool IsConnectingState() const; 92 93 // Returns true if the network properties are stored in a user profile. 94 bool IsPrivate() const; 95 96 // Returns a comma separated string of name servers. 97 std::string GetDnsServersAsString() const; 98 99 // Converts the prefix length to a netmask string. 100 std::string GetNetmask() const; 101 102 // Set the GUID. Called exclusively by NetworkStateHandler. 103 void SetGuid(const std::string& guid); 104 105 // Helpers (used e.g. when a state or error is cached) 106 static bool StateIsConnected(const std::string& connection_state); 107 static bool StateIsConnecting(const std::string& connection_state); 108 static bool ErrorIsValid(const std::string& error); 109 110 private: 111 friend class MobileActivatorTest; 112 friend class NetworkStateHandler; 113 friend class NetworkChangeNotifierChromeosUpdateTest; 114 115 // Updates |name_| from WiFi.HexSSID if provided, and validates |name_|. 116 // Returns true if |name_| changes. 117 bool UpdateName(const base::DictionaryValue& properties); 118 119 // Network Service properties. Avoid adding any additional properties here. 120 // Instead use NetworkConfigurationHandler::GetProperties() to asynchronously 121 // request properties from Shill. 122 std::string security_; 123 std::string eap_method_; // Needed for WiFi EAP networks 124 std::string device_path_; 125 std::string guid_; 126 std::string connection_state_; 127 std::string profile_path_; 128 bool connectable_; 129 130 // Reflects the current Shill Service.Error property. This might get cleared 131 // by Shill shortly after a failure. 132 std::string error_; 133 134 // Last non empty Service.Error property. Cleared by NetworkConnectionHandler 135 // when a connection attempt is initiated. 136 std::string last_error_; 137 138 // This is convenient to keep cached for now, but shouldn't be necessary; 139 // avoid using it if possible. 140 NetworkUIData ui_data_; 141 142 // IPConfig properties. 143 // Note: These do not correspond to actual Shill.Service properties 144 // but are derived from the service's corresponding IPConfig object. 145 std::string ip_address_; 146 std::string gateway_; 147 std::vector<std::string> dns_servers_; 148 int prefix_length_; // Used by GetNetmask() 149 GURL web_proxy_auto_discovery_url_; 150 151 // Wireless properties, used for icons and Connect logic. 152 int signal_strength_; 153 154 // Cellular properties, used for icons, Connect, and Activation. 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 161 // Whether a deprecated CaCertNSS property of this network is set. Required 162 // for migration to PEM. 163 bool has_ca_cert_nss_; 164 165 DISALLOW_COPY_AND_ASSIGN(NetworkState); 166}; 167 168} // namespace chromeos 169 170#endif // CHROMEOS_NETWORK_NETWORK_STATE_H_ 171