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