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 <stdint.h>
9 
10 #include <memory>
11 #include <string>
12 #include <vector>
13 
14 #include "base/gtest_prod_util.h"
15 #include "base/macros.h"
16 #include "base/values.h"
17 #include "chromeos/network/managed_state.h"
18 #include "chromeos/services/network_config/public/mojom/cros_network_config.mojom-forward.h"
19 #include "components/onc/onc_constants.h"
20 #include "url/gurl.h"
21 
22 namespace base {
23 class Value;
24 }
25 
26 namespace chromeos {
27 
28 // Simple class to provide network state information about a network service.
29 // This class should always be passed as a const* and should never be held
30 // on to. Store network_state->path() (defined in ManagedState) instead and
31 // call NetworkStateHandler::GetNetworkState(path) to retrieve the state for
32 // the network.
33 //
34 // Note: NetworkStateHandler will store an entry for each member of
35 // Manager.ServiceCompleteList. The visible() method indicates whether the
36 // network is visible, and the IsInProfile() method indicates whether the
37 // network is saved in a profile.
COMPONENT_EXPORT(CHROMEOS_NETWORK)38 class COMPONENT_EXPORT(CHROMEOS_NETWORK) NetworkState : public ManagedState {
39  public:
40   explicit NetworkState(const std::string& path);
41   ~NetworkState() override;
42 
43   struct VpnProviderInfo {
44     // The id used by chrome to identify the provider (i.e. an extension id).
45     std::string id;
46     // The VPN type, provided by the VPN provider/extension.
47     std::string type;
48   };
49 
50   enum class PortalState {
51     // The network is not connected or the portal state is not available.
52     kUnknown,
53     // The network is connected and no portal is detected.
54     kOnline,
55     // A portal is suspected but no redirect was provided.
56     kPortalSuspected,
57     // The network is in a portal state with a redirect URL.
58     kPortal,
59     // A proxy requiring authentication is detected.
60     kProxyAuthRequired,
61     // The network is connected but no internet is available and no proxy was
62     // detected.
63     kNoInternet,
64     kMaxValue = kNoInternet  // For UMA_HISTOGRAM_ENUMERATION
65   };
66 
67   // ManagedState overrides
68   // If you change this method, update GetProperties too.
69   bool PropertyChanged(const std::string& key,
70                        const base::Value& value) override;
71   bool InitialPropertiesReceived(const base::Value& properties) override;
72   void GetStateProperties(base::Value* dictionary) const override;
73   bool IsActive() const override;
74 
75   // Called when the IPConfig properties may have changed. |properties| is
76   // expected to be of type DICTIONARY.
77   void IPConfigPropertiesChanged(const base::Value& properties);
78 
79   // Returns true if the network requires a service activation.
80   bool RequiresActivation() const;
81 
82   // Returns true if the network security type requires a passphrase only.
83   bool SecurityRequiresPassphraseOnly() const;
84 
85   // Accessors
86   bool visible() const { return visible_; }
87   void set_visible(bool visible) { visible_ = visible; }
88   const std::string& security_class() const { return security_class_; }
89   const std::string& device_path() const { return device_path_; }
90   const std::string& guid() const { return guid_; }
91   const std::string& profile_path() const { return profile_path_; }
92   const GURL& probe_url() const { return probe_url_; }
93   ::onc::ONCSource onc_source() const { return onc_source_; }
94 
95   // Provides the error for the last attempt to connect/configure the network
96   // (an empty string signifies no error at all). Note that this value can be
97   // cleared - see ClearError() below.
98   const std::string& GetError() const;
99 
100   // Clears the error associated with this network. Should be called whenever
101   // a connection to this network is initiated or the associated configuration
102   // is updated/removed.
103   void ClearError();
104 
105   // Returns |connection_state_| if visible, kStateIdle otherwise.
106   std::string connection_state() const;
107 
108   // Updates the connection state and saves the previous connection state.
109   void SetConnectionState(const std::string& connection_state);
110 
111   int priority() const { return priority_; }
112 
113   const base::Value& proxy_config() const { return proxy_config_; }
114   const base::Value& ipv4_config() const { return ipv4_config_; }
115   std::string GetIpAddress() const;
116   std::string GetGateway() const;
117   GURL GetWebProxyAutoDiscoveryUrl() const;
118 
119   // Wireless property accessors
120   bool connectable() const { return connectable_; }
121   void set_connectable(bool connectable) { connectable_ = connectable; }
122   int signal_strength() const { return signal_strength_; }
123   void set_signal_strength(int signal_strength) {
124     signal_strength_ = signal_strength;
125   }
126   const std::string& bssid() const { return bssid_; }
127   int frequency() const { return frequency_; }
128   bool blocked_by_policy() const { return blocked_by_policy_; }
129   void set_blocked_by_policy(bool blocked_by_policy) {
130     blocked_by_policy_ = blocked_by_policy;
131   }
132 
133   // Wifi property accessors
134   const std::string& eap_method() const { return eap_method_; }
135   const std::vector<uint8_t>& raw_ssid() const { return raw_ssid_; }
136 
137   // Cellular property accessors
138   const std::string& network_technology() const { return network_technology_; }
139   const std::string& activation_type() const { return activation_type_; }
140   const std::string& activation_state() const { return activation_state_; }
141   const std::string& payment_url() const { return payment_url_; }
142   const std::string& payment_post_data() const { return payment_post_data_; }
143   bool cellular_out_of_credits() const { return cellular_out_of_credits_; }
144   const std::string& tethering_state() const { return tethering_state_; }
145 
146   // VPN property accessors
147   const VpnProviderInfo* vpn_provider() const { return vpn_provider_.get(); }
148   std::string GetVpnProviderType() const;
149 
150   // Tether accessors and setters.
151   int battery_percentage() const { return battery_percentage_; }
152   void set_battery_percentage(int battery_percentage) {
153     battery_percentage_ = battery_percentage;
154   }
155   const std::string& tether_carrier() const { return tether_carrier_; }
156   void set_tether_carrier(const std::string& tether_carrier) {
157     tether_carrier_ = tether_carrier;
158   }
159   bool tether_has_connected_to_host() const {
160     return tether_has_connected_to_host_;
161   }
162   void set_tether_has_connected_to_host(bool tether_has_connected_to_host) {
163     tether_has_connected_to_host_ = tether_has_connected_to_host;
164   }
165   const std::string& tether_guid() const { return tether_guid_; }
166   void set_tether_guid(const std::string& guid) { tether_guid_ = guid; }
167 
168   bool connect_requested() const { return connect_requested_; }
169 
170   PortalState portal_state() const { return portal_state_; }
171 
172   // Returns true if the network is managed by policy (determined by
173   // |onc_source_|).
174   bool IsManagedByPolicy() const;
175 
176   // Returns true if the network is romaing and the provider does not require
177   // roaming.
178   bool IndicateRoaming() const;
179 
180   // Returns true if the network securty is WEP_8021x (Dynamic WEP)
181   bool IsDynamicWep() const;
182 
183   // Returns true if |connection_state_| is a connected/connecting state.
184   bool IsConnectedState() const;
185   bool IsConnectingState() const;
186   bool IsConnectingOrConnected() const;
187 
188   // Returns true if |connection_state_| is online.
189   bool IsOnline() const;
190 
191   // Returns true if this is a network stored in a profile.
192   bool IsInProfile() const;
193 
194   // Returns true if the network is never stored in a profile (e.g. Tether and
195   // default Cellular).
196   bool IsNonProfileType() const;
197 
198   // Returns true if the network properties are stored in a user profile.
199   bool IsPrivate() const;
200 
201   // Returns true if the network is a default Cellular network (see
202   // NetworkStateHandler::EnsureCellularNetwork()).
203   bool IsDefaultCellular() const;
204 
205   // Returns true if Shill has detected a captive portal state.
206   bool IsShillCaptivePortal() const;
207 
208   // Returns true if Shill or Chrome have detected a captive portal state.
209   bool IsCaptivePortal() const;
210 
211   // Returns true if the security type is non-empty and not 'none'.
212   bool IsSecure() const;
213 
214   // Returns the |raw_ssid| as a hex-encoded string
215   std::string GetHexSsid() const;
216 
217   // Returns a comma separated string of name servers.
218   std::string GetDnsServersAsString() const;
219 
220   // Converts the prefix length to a netmask string.
221   std::string GetNetmask() const;
222 
223   // Returns a specifier for identifying this network in the absence of a GUID.
224   // This should only be used by NetworkStateHandler for keeping track of
225   // GUIDs assigned to unsaved networks.
226   std::string GetSpecifier() const;
227 
228   // Set the GUID. Called exclusively by NetworkStateHandler.
229   void SetGuid(const std::string& guid);
230 
231   // Helpers for returning mojo types.
232   network_config::mojom::ActivationStateType GetMojoActivationState() const;
233   network_config::mojom::SecurityType GetMojoSecurity() const;
234 
235   // Helper for UMA stats. Corresponds to NetworkTechnology in enums.xml
236   // which is also used by Shill metrics.
237   enum class NetworkTechnologyType {
238     kCellular = 0,
239     kEthernet = 1,
240     kEthernetEap = 2,
241     kWiFi = 3,
242     kTether = 4,
243     kVPN = 5,
244     kUnknown = 6,
245     kMaxValue = kUnknown,
246   };
247   NetworkTechnologyType GetNetworkTechnologyType() const;
248 
249   // Setters for testing.
250   void set_connection_state_for_testing(const std::string& connection_state) {
251     connection_state_ = connection_state;
252   }
253   void set_connect_requested_for_testing(bool connect_requested) {
254     connect_requested_ = connect_requested;
255   }
256   void set_network_technology_for_testing(const std::string& technology) {
257     network_technology_ = technology;
258   }
259 
260   // Helpers (used e.g. when a state, error, or shill dictionary is cached)
261   static bool StateIsConnected(const std::string& connection_state);
262   static bool StateIsConnecting(const std::string& connection_state);
263   static bool StateIsPortalled(const std::string& connection_state);
264   static bool ErrorIsValid(const std::string& error);
265   static std::unique_ptr<NetworkState> CreateDefaultCellular(
266       const std::string& device_path);
267 
268   // Ignore changes to signal strength less than this value.
269   constexpr static const int kSignalStrengthChangeThreshold = 5;
270 
271  private:
272   friend class MobileActivatorTest;
273   friend class NetworkStateHandler;
274 
275   // Updates |name_| from the 'WiFi.HexSSID' entry in |properties|, which must
276   // be of type DICTIONARY, if the key exists, and validates |name_|. Returns
277   // true if |name_| changes.
278   bool UpdateName(const base::Value& properties);
279 
280   void UpdateCaptivePortalState(const base::Value& properties);
281 
282   void SetVpnProvider(const std::string& id, const std::string& type);
283 
284   // Set to true if the network is a member of Manager.Services.
285   bool visible_ = false;
286 
287   // Network Service properties. Avoid adding any additional properties here.
288   // Instead use NetworkConfigurationHandler::GetProperties() to asynchronously
289   // request properties from Shill.
290   std::string security_class_;
291   std::string eap_method_;    // Needed for WiFi EAP networks
292   std::string eap_key_mgmt_;  // Needed for identifying Dynamic WEP networks
293   std::string device_path_;
294   std::string guid_;
295   std::string tether_guid_;  // Used to double link a Tether and Wi-Fi network.
296   std::string connection_state_;
297   std::string last_connection_state_;
298   std::string profile_path_;
299   GURL probe_url_;
300   std::vector<uint8_t> raw_ssid_;  // Unknown encoding. Not necessarily UTF-8.
301   int priority_ = 0;  // kPriority, used for organizing known networks.
302   ::onc::ONCSource onc_source_ = ::onc::ONC_SOURCE_UNKNOWN;
303 
304   // Last non empty Service.Error property. Expected to be cleared via
305   // ClearError() when a connection attempt is initiated and when an associated
306   // configuration is updated/removed.
307   std::string last_error_;
308 
309   // Cached copy of the Shill Service IPConfig object. For ipv6 properties use
310   // the ip_configs_ property in the corresponding DeviceState.
311   base::Value ipv4_config_;
312 
313   // Wireless properties, used for icons and Connect logic.
314   bool connectable_ = false;
315   int signal_strength_ = 0;
316   std::string bssid_;
317   int frequency_ = 0;
318   bool blocked_by_policy_ = false;
319 
320   // Cellular properties, used for icons, Connect, and Activation.
321   std::string network_technology_;
322   std::string activation_type_;
323   std::string activation_state_;
324   std::string roaming_;
325   bool provider_requires_roaming_ = false;
326   std::string payment_url_;
327   std::string payment_post_data_;
328   bool cellular_out_of_credits_ = false;
329   std::string tethering_state_;
330 
331   // VPN properties, used to construct the display name and to show the correct
332   // configuration dialog. The id is the Extension ID or Arc package name for
333   // extension or Arc provider VPNs.
334   std::unique_ptr<VpnProviderInfo> vpn_provider_;
335 
336   // Tether properties.
337   std::string tether_carrier_;
338   int battery_percentage_ = 0;
339 
340   // Portal state is derived from connection_state_ and Shill portal properties.
341   PortalState portal_state_ = PortalState::kUnknown;
342   int portal_status_code_ = 0;
343 
344   // Whether the current device has already connected to the tether host device
345   // providing the hotspot corresponding to this NetworkState.
346   // Note: this means that the current device has already connected to the
347   // tether host, but it does not necessarily mean that the current device has
348   // connected to the Tether network corresponding to this NetworkState.
349   bool tether_has_connected_to_host_ = false;
350 
351   // TODO(pneubeck): Remove this once (Managed)NetworkConfigurationHandler
352   // provides proxy configuration. crbug.com/241775
353   base::Value proxy_config_;
354 
355   // Set while a network connect request is queued. Cleared on connect or
356   // if the request is aborted.
357   bool connect_requested_ = false;
358 
359   // Set by NetworkStateHandler if Chrome detects a captive portal state.
360   // See IsCaptivePortal() for details.
361   bool is_chrome_captive_portal_ = false;
362 
363   DISALLOW_COPY_AND_ASSIGN(NetworkState);
364 };
365 
366 }  // namespace chromeos
367 
368 #endif  // CHROMEOS_NETWORK_NETWORK_STATE_H_
369