network_state.h revision 868fa2fe829687343ffae624259930155e16dbd8
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  explicit NetworkState(const std::string& path);
25  virtual ~NetworkState();
26
27  // ManagedState overrides
28  // If you change this method, update GetProperties too.
29  virtual bool PropertyChanged(const std::string& key,
30                               const base::Value& value) OVERRIDE;
31  virtual void InitialPropertiesReceived() OVERRIDE;
32
33  // Fills |dictionary| with the state properties. All the properties that are
34  // accepted by PropertyChanged are stored in |dictionary|, no other values are
35  // stored.
36  void GetProperties(base::DictionaryValue* dictionary) const;
37
38  // Accessors
39  const std::string& security() const { return security_; }
40  const std::string& ip_address() const { return ip_address_; }
41  const std::vector<std::string>& dns_servers() const { return dns_servers_; }
42  const std::string& device_path() const { return device_path_; }
43  const std::string& guid() const { return guid_; }
44  const std::string& connection_state() const { return connection_state_; }
45  const std::string& profile_path() const { return profile_path_; }
46  const std::string& error() const { return error_; }
47  const std::string& error_details() const { return error_details_; }
48  bool auto_connect() const { return auto_connect_; }
49  bool favorite() const { return favorite_; }
50  int priority() const { return priority_; }
51  const base::DictionaryValue& proxy_config() const { return proxy_config_; }
52  onc::ONCSource onc_source() const { return onc_source_; }
53  // Wireless property accessors
54  int signal_strength() const { return signal_strength_; }
55  bool connectable() const { return connectable_; }
56  // Wifi property accessors
57  bool passphrase_required() const { return passphrase_required_; }
58  // Cellular property accessors
59  const std::string& technology() const { return technology_; }
60  const std::string& activation_state() const { return activation_state_; }
61  const std::string& roaming() const { return roaming_; }
62  bool activate_over_non_cellular_networks() const {
63    return activate_over_non_cellular_networks_;
64  }
65  bool cellular_out_of_credits() const { return cellular_out_of_credits_; }
66
67  bool IsConnectedState() const;
68  bool IsConnectingState() const;
69
70  // Returns true if |error_| contains an authentication error.
71  bool HasAuthenticationError() const;
72
73  // Helpers (used e.g. when a state is cached)
74  static bool StateIsConnected(const std::string& connection_state);
75  static bool StateIsConnecting(const std::string& connection_state);
76
77  // Helper to return a full prefixed version of an IPConfig property
78  // key.
79  static std::string IPConfigProperty(const char* key);
80
81 private:
82  friend class NetworkStateHandler;
83  friend class NetworkChangeNotifierChromeosUpdateTest;
84
85  // Updates the name from hex_ssid_ if provided, and validates name_.
86  void UpdateName();
87
88  // Called by NetworkStateHandler when the ip config changes.
89  void set_ip_address(const std::string& ip_address) {
90    ip_address_ = ip_address;
91  }
92  void set_dns_servers(const std::vector<std::string>& dns_servers) {
93    dns_servers_ = dns_servers;
94  }
95
96  // Common Network Service properties
97  std::string security_;
98  std::string device_path_;
99  std::string guid_;
100  std::string connection_state_;
101  std::string profile_path_;
102  std::string error_;
103  std::string error_details_;
104  bool auto_connect_;
105  bool favorite_;
106  int priority_;
107  // TODO(pneubeck): Remove ProxyConfig and ONCSource once
108  // NetworkConfigurationHandler provides proxy configuration. crbug/241775
109  base::DictionaryValue proxy_config_;
110  onc::ONCSource onc_source_;
111  // IPConfig properties.
112  // Note: These do not correspond to actual Shill.Service properties
113  // but are derived from the service's corresponding IPConfig object.
114  std::string ip_address_;
115  std::vector<std::string> dns_servers_;
116  // Wireless properties
117  int signal_strength_;
118  bool connectable_;
119  // Wifi properties
120  std::string hex_ssid_;
121  std::string country_code_;
122  bool passphrase_required_;
123  // Cellular properties
124  std::string technology_;
125  std::string activation_state_;
126  std::string roaming_;
127  bool activate_over_non_cellular_networks_;
128  bool cellular_out_of_credits_;
129
130  DISALLOW_COPY_AND_ASSIGN(NetworkState);
131};
132
133}  // namespace chromeos
134
135#endif  // CHROMEOS_NETWORK_NETWORK_STATE_H_
136