network_state.h revision a36e5920737c6adbddd3e43b760e5de8431db6e0
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  // Returns true if |connection_state_| is a connected/connecting state.
78  bool IsConnectedState() const;
79  bool IsConnectingState() const;
80
81  // Returns true if the ONC source is a device or user policy.
82  bool IsManaged() const;
83
84  // Returns true if the network properties are stored in a user profile.
85  bool IsPrivate() const;
86
87  // Returns a comma separated string of name servers.
88  std::string GetDnsServersAsString() const;
89
90  // Converts the prefix length to a netmaks string.
91  std::string GetNetmask() const;
92
93  // Helpers (used e.g. when a state is cached)
94  static bool StateIsConnected(const std::string& connection_state);
95  static bool StateIsConnecting(const std::string& connection_state);
96
97  // Helper to return a full prefixed version of an IPConfig property
98  // key.
99  static std::string IPConfigProperty(const char* key);
100
101  // Sets |out| to the ONCSource specified by the UIData property |value|.
102  // Returns true if the source was successfully parsed.
103  static bool GetOncSource(const base::Value& value, onc::ONCSource* out);
104
105  // Generates a name from properties."Wifi.HexSSID" if present, otherwise
106  // validates properties.Name and returns a valid utf8 version.
107  static std::string GetNameFromProperties(
108      const base::DictionaryValue& properties);
109
110 private:
111  friend class NetworkStateHandler;
112  friend class NetworkChangeNotifierChromeosUpdateTest;
113
114  // Updates |name_| from WiFi.HexSSID if provided, and validates |name_|.
115  // Returns true if |name_| changes.
116  bool UpdateName(const base::DictionaryValue& properties);
117
118  // TODO(gauravsh): Audit the list of properties that we are caching. We should
119  // only be doing this for commonly accessed properties. crbug.com/252553
120  // Common Network Service properties
121  std::string security_;
122  std::string device_path_;
123  std::string guid_;
124  std::string connection_state_;
125  std::string profile_path_;
126  std::string error_;
127  std::string error_details_;
128  bool auto_connect_;
129  bool favorite_;
130  int priority_;
131  // TODO(pneubeck): Remove ProxyConfig once NetworkConfigurationHandler
132  // provides proxy configuration. crbug/241775
133  base::DictionaryValue proxy_config_;
134  onc::ONCSource onc_source_;
135  // IPConfig properties.
136  // Note: These do not correspond to actual Shill.Service properties
137  // but are derived from the service's corresponding IPConfig object.
138  std::string ip_address_;
139  std::string gateway_;
140  std::vector<std::string> dns_servers_;
141  int prefix_length_;
142  // Wireless properties
143  int signal_strength_;
144  bool connectable_;
145  // Cellular properties
146  std::string network_technology_;
147  std::string activation_state_;
148  std::string roaming_;
149  bool activate_over_non_cellular_networks_;
150  bool cellular_out_of_credits_;
151  // Cellular payment portal properties.
152  std::string usage_url_;
153  std::string payment_url_;
154  std::string post_method_;
155  std::string post_data_;
156
157  DISALLOW_COPY_AND_ASSIGN(NetworkState);
158};
159
160}  // namespace chromeos
161
162#endif  // CHROMEOS_NETWORK_NETWORK_STATE_H_
163