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