network_state.h revision d0247b1b59f9c528cb6df88b4f2b9afaf80d181e
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  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  // Returns true, if the network requires a service activation.
42  bool RequiresActivation() const;
43
44  // Accessors
45  const std::string& security() const { return security_; }
46  const std::string& device_path() const { return device_path_; }
47  const std::string& guid() const { return guid_; }
48  const std::string& connection_state() const { return connection_state_; }
49  const std::string& profile_path() const { return profile_path_; }
50  const std::string& error() const { return error_; }
51  bool connectable() const { return connectable_; }
52
53  const base::DictionaryValue& proxy_config() const { return proxy_config_; }
54  const NetworkUIData& ui_data() const { return ui_data_; }
55
56  // IPConfig Properties. These require an extra call to ShillIPConfigClient,
57  // so cache them to avoid excessively complex client code.
58  const std::string& ip_address() const { return ip_address_; }
59  const std::string& gateway() const { return gateway_; }
60  const std::vector<std::string>& dns_servers() const { return dns_servers_; }
61  const GURL& web_proxy_auto_discovery_url() const {
62    return web_proxy_auto_discovery_url_;
63  }
64
65  // Wireless property accessors
66  int signal_strength() const { return signal_strength_; }
67
68  // Cellular property accessors
69  const std::string& network_technology() const {
70    return network_technology_;
71  }
72  const std::string& activation_state() const { return activation_state_; }
73  const std::string& roaming() const { return roaming_; }
74  bool activate_over_non_cellular_networks() const {
75    return activate_over_non_cellular_networks_;
76  }
77  bool cellular_out_of_credits() const { return cellular_out_of_credits_; }
78
79  // Whether this network has a CACertNSS nickname set.
80  bool HasCACertNSS() const { return has_ca_cert_nss_; }
81
82  // Returns true if |connection_state_| is a connected/connecting state.
83  bool IsConnectedState() const;
84  bool IsConnectingState() const;
85
86  // Returns true if the ONC source is a device or user policy.
87  bool IsManaged() const;
88
89  // Returns true if the network properties are stored in a user profile.
90  bool IsPrivate() const;
91
92  // Returns a comma separated string of name servers.
93  std::string GetDnsServersAsString() const;
94
95  // Converts the prefix length to a netmask string.
96  std::string GetNetmask() const;
97
98  // Helpers (used e.g. when a state is cached)
99  static bool StateIsConnected(const std::string& connection_state);
100  static bool StateIsConnecting(const std::string& connection_state);
101
102  // Helper to return a full prefixed version of an IPConfig property key.
103  static std::string IPConfigProperty(const char* key);
104
105
106 private:
107  friend class MobileActivatorTest;
108  friend class NetworkStateHandler;
109  friend class NetworkChangeNotifierChromeosUpdateTest;
110
111  // Updates |name_| from WiFi.HexSSID if provided, and validates |name_|.
112  // Returns true if |name_| changes.
113  bool UpdateName(const base::DictionaryValue& properties);
114
115  // Network Service properties. Avoid adding any additional properties here.
116  // Instead use NetworkConfigurationHandler::GetProperties() to asynchronously
117  // request properties from Shill.
118  std::string security_;
119  std::string device_path_;
120  std::string guid_;
121  std::string connection_state_;
122  std::string profile_path_;
123  std::string error_;
124  bool connectable_;
125
126  // TODO(pneubeck): Remove ProxyConfig once NetworkConfigurationHandler
127  // provides proxy configuration. crbug.com/241775
128  base::DictionaryValue proxy_config_;
129
130  // This is convenient to keep cached for now, but shouldn't be necessary;
131  // avoid using it if possible.
132  NetworkUIData ui_data_;
133
134  // IPConfig properties.
135  // Note: These do not correspond to actual Shill.Service properties
136  // but are derived from the service's corresponding IPConfig object.
137  std::string ip_address_;
138  std::string gateway_;
139  std::vector<std::string> dns_servers_;
140  int prefix_length_;  // Used by GetNetmask()
141  GURL web_proxy_auto_discovery_url_;
142
143  // Wireless properties, used for icons and Connect logic.
144  int signal_strength_;
145
146  // Cellular properties, used for icons, Connect, and Activation.
147  std::string network_technology_;
148  std::string activation_state_;
149  std::string roaming_;
150  bool activate_over_non_cellular_networks_;
151  bool cellular_out_of_credits_;
152
153  // Whether a deprecated CaCertNSS property of this network is set. Required
154  // for migration to PEM.
155  bool has_ca_cert_nss_;
156
157  DISALLOW_COPY_AND_ASSIGN(NetworkState);
158};
159
160}  // namespace chromeos
161
162#endif  // CHROMEOS_NETWORK_NETWORK_STATE_H_
163