blob: ee172389314b680adc31251fa9941dd36bedf309 [file] [log] [blame]
[email protected]894ee392011-02-25 21:05:021// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]7598ab52009-10-08 01:33:212// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]268b02f2010-02-04 21:07:155#ifndef CHROME_BROWSER_CHROMEOS_CROS_NETWORK_LIBRARY_H_
6#define CHROME_BROWSER_CHROMEOS_CROS_NETWORK_LIBRARY_H_
[email protected]32b76ef2010-07-26 23:08:247#pragma once
[email protected]7598ab52009-10-08 01:33:218
9#include <string>
10#include <vector>
11
[email protected]0aadb272010-11-17 09:33:0812#include "base/gtest_prod_util.h"
[email protected]3b63f8f42011-03-28 01:54:1513#include "base/memory/scoped_vector.h"
14#include "base/memory/singleton.h"
[email protected]7598ab52009-10-08 01:33:2115#include "base/observer_list.h"
[email protected]f0053942010-11-13 02:33:5416#include "base/string16.h"
[email protected]1f410002009-10-28 04:11:0017#include "base/timer.h"
[email protected]583dca282010-12-17 01:12:0518#include "third_party/cros/chromeos_network.h"
[email protected]7598ab52009-10-08 01:33:2119
[email protected]894ee392011-02-25 21:05:0220class DictionaryValue;
[email protected]3c1139f2010-10-22 06:17:4221class Value;
22
[email protected]b22c21c2009-10-30 00:35:0023namespace chromeos {
24
[email protected]51f6caa2011-03-04 18:16:2525// Connection enums (see flimflam/include/service.h)
26enum ConnectionType {
27 TYPE_UNKNOWN = 0,
28 TYPE_ETHERNET = 1,
29 TYPE_WIFI = 2,
30 TYPE_WIMAX = 3,
31 TYPE_BLUETOOTH = 4,
32 TYPE_CELLULAR = 5,
[email protected]83c82652011-03-05 01:59:3433 TYPE_VPN = 6,
[email protected]51f6caa2011-03-04 18:16:2534};
35
36enum ConnectionMode {
37 MODE_UNKNOWN = 0,
38 MODE_MANAGED = 1,
39 MODE_ADHOC = 2,
40};
41
42enum ConnectionSecurity {
43 SECURITY_UNKNOWN = 0,
44 SECURITY_NONE = 1,
45 SECURITY_WEP = 2,
46 SECURITY_WPA = 3,
47 SECURITY_RSN = 4,
48 SECURITY_8021X = 5,
49};
50
51enum ConnectionState {
52 STATE_UNKNOWN = 0,
53 STATE_IDLE = 1,
54 STATE_CARRIER = 2,
55 STATE_ASSOCIATION = 3,
56 STATE_CONFIGURATION = 4,
57 STATE_READY = 5,
58 STATE_DISCONNECT = 6,
59 STATE_FAILURE = 7,
60 STATE_ACTIVATION_FAILURE = 8
61};
62
63enum ConnectivityState {
64 CONN_STATE_UNKNOWN = 0,
65 CONN_STATE_UNRESTRICTED = 1,
66 CONN_STATE_RESTRICTED = 2,
67 CONN_STATE_NONE = 3
68};
69
70// Network enums (see flimflam/include/network.h)
71enum NetworkTechnology {
72 NETWORK_TECHNOLOGY_UNKNOWN = 0,
73 NETWORK_TECHNOLOGY_1XRTT = 1,
74 NETWORK_TECHNOLOGY_EVDO = 2,
75 NETWORK_TECHNOLOGY_GPRS = 3,
76 NETWORK_TECHNOLOGY_EDGE = 4,
77 NETWORK_TECHNOLOGY_UMTS = 5,
78 NETWORK_TECHNOLOGY_HSPA = 6,
79 NETWORK_TECHNOLOGY_HSPA_PLUS = 7,
80 NETWORK_TECHNOLOGY_LTE = 8,
81 NETWORK_TECHNOLOGY_LTE_ADVANCED = 9,
82};
83
84enum ActivationState {
85 ACTIVATION_STATE_UNKNOWN = 0,
86 ACTIVATION_STATE_ACTIVATED = 1,
87 ACTIVATION_STATE_ACTIVATING = 2,
88 ACTIVATION_STATE_NOT_ACTIVATED = 3,
89 ACTIVATION_STATE_PARTIALLY_ACTIVATED = 4,
90};
91
92enum NetworkRoamingState {
93 ROAMING_STATE_UNKNOWN = 0,
94 ROAMING_STATE_HOME = 1,
95 ROAMING_STATE_ROAMING = 2,
96};
97
98// connection errors (see flimflam/include/service.h)
99enum ConnectionError {
100 ERROR_UNKNOWN = 0,
101 ERROR_OUT_OF_RANGE = 1,
102 ERROR_PIN_MISSING = 2,
103 ERROR_DHCP_FAILED = 3,
104 ERROR_CONNECT_FAILED = 4,
105 ERROR_BAD_PASSPHRASE = 5,
106 ERROR_BAD_WEPKEY = 6,
107 ERROR_ACTIVATION_FAILED = 7,
108 ERROR_NEED_EVDO = 8,
109 ERROR_NEED_HOME_NETWORK = 9,
110 ERROR_OTASP_FAILED = 10,
111 ERROR_AAA_FAILED = 11,
112};
113
[email protected]9100a362011-03-22 08:20:26114// We are currently only supporting setting a single EAP Method.
115enum EAPMethod {
116 EAP_METHOD_UNKNOWN = 0,
117 EAP_METHOD_PEAP = 1,
118 EAP_METHOD_TLS = 2,
119 EAP_METHOD_TTLS = 3,
120 EAP_METHOD_LEAP = 4
121};
122
123// We are currently only supporting setting a single EAP phase 2 authentication.
124enum EAPPhase2Auth {
125 EAP_PHASE_2_AUTH_AUTO = 0,
126 EAP_PHASE_2_AUTH_MD5 = 1,
127 EAP_PHASE_2_AUTH_MSCHAPV2 = 2,
128 EAP_PHASE_2_AUTH_MSCHAP = 3,
129 EAP_PHASE_2_AUTH_PAP = 4,
130 EAP_PHASE_2_AUTH_CHAP = 5
131};
132
[email protected]543a79a2010-10-20 01:14:58133// Cellular network is considered low data when less than 60 minues.
134static const int kCellularDataLowSecs = 60 * 60;
135
136// Cellular network is considered low data when less than 30 minues.
137static const int kCellularDataVeryLowSecs = 30 * 60;
138
139// Cellular network is considered low data when less than 100MB.
140static const int kCellularDataLowBytes = 100 * 1024 * 1024;
141
142// Cellular network is considered very low data when less than 50MB.
143static const int kCellularDataVeryLowBytes = 50 * 1024 * 1024;
144
[email protected]894ee392011-02-25 21:05:02145// Contains data related to the flimflam.Device interface,
146// e.g. ethernet, wifi, cellular.
147class NetworkDevice {
148 public:
149 explicit NetworkDevice(const std::string& device_path);
[email protected]894ee392011-02-25 21:05:02150
151 // Device info.
152 const std::string& device_path() const { return device_path_; }
153 const std::string& name() const { return name_; }
154 ConnectionType type() const { return type_; }
155 bool scanning() const { return scanning_; }
156 const std::string& meid() const { return MEID_; }
157 const std::string& imei() const { return IMEI_; }
158 const std::string& imsi() const { return IMSI_; }
159 const std::string& esn() const { return ESN_; }
160 const std::string& mdn() const { return MDN_; }
161 const std::string& min() const { return MIN_; }
162 const std::string& model_id() const { return model_id_; }
163 const std::string& manufacturer() const { return manufacturer_; }
164 const std::string& firmware_revision() const { return firmware_revision_; }
165 const std::string& hardware_revision() const { return hardware_revision_; }
166 const std::string& last_update() const { return last_update_; }
167 const unsigned int prl_version() const { return PRL_version_; }
168
[email protected]07ed5c082011-03-02 19:50:18169 private:
170 bool ParseValue(int index, const Value* value);
171 void ParseInfo(const DictionaryValue* info);
172
[email protected]894ee392011-02-25 21:05:02173 // General device info.
174 std::string device_path_;
175 std::string name_;
176 ConnectionType type_;
177 bool scanning_;
178 // Cellular specific device info.
179 std::string carrier_;
180 std::string MEID_;
181 std::string IMEI_;
182 std::string IMSI_;
183 std::string ESN_;
184 std::string MDN_;
185 std::string MIN_;
186 std::string model_id_;
187 std::string manufacturer_;
188 std::string firmware_revision_;
189 std::string hardware_revision_;
190 std::string last_update_;
191 int PRL_version_;
[email protected]07ed5c082011-03-02 19:50:18192
193 friend class NetworkLibraryImpl;
194 DISALLOW_COPY_AND_ASSIGN(NetworkDevice);
[email protected]894ee392011-02-25 21:05:02195};
196
197// Contains data common to all network service types.
[email protected]00e17562010-05-06 18:59:25198class Network {
199 public:
[email protected]894ee392011-02-25 21:05:02200 virtual ~Network() {}
201
[email protected]00e17562010-05-06 18:59:25202 const std::string& service_path() const { return service_path_; }
[email protected]894ee392011-02-25 21:05:02203 const std::string& name() const { return name_; }
[email protected]00e17562010-05-06 18:59:25204 const std::string& device_path() const { return device_path_; }
205 const std::string& ip_address() const { return ip_address_; }
[email protected]5a1c2dd2010-05-11 22:52:30206 ConnectionType type() const { return type_; }
[email protected]894ee392011-02-25 21:05:02207 ConnectionMode mode() const { return mode_; }
[email protected]9a24152e2010-10-08 23:15:31208 ConnectionState connection_state() const { return state_; }
[email protected]5a1c2dd2010-05-11 22:52:30209 bool connecting() const { return state_ == STATE_ASSOCIATION ||
[email protected]894ee392011-02-25 21:05:02210 state_ == STATE_CONFIGURATION ||
211 state_ == STATE_CARRIER; }
[email protected]8df038b2010-10-28 22:56:46212 bool configuring() const { return state_ == STATE_CONFIGURATION; }
[email protected]5a1c2dd2010-05-11 22:52:30213 bool connected() const { return state_ == STATE_READY; }
214 bool connecting_or_connected() const { return connecting() || connected(); }
215 bool failed() const { return state_ == STATE_FAILURE; }
[email protected]894ee392011-02-25 21:05:02216 bool failed_or_disconnected() const {
217 return failed() || state_ == STATE_IDLE;
218 }
[email protected]5a1c2dd2010-05-11 22:52:30219 ConnectionError error() const { return error_; }
[email protected]89ccd242010-10-12 08:58:27220 ConnectionState state() const { return state_; }
[email protected]9100a362011-03-22 08:20:26221 // Is this network connectable. Currently, this is mainly used by 802.1x
222 // networks to specify that the network is not configured yet.
[email protected]ca9f6702010-11-17 01:28:14223 bool connectable() const { return connectable_; }
[email protected]ceaf8f12010-11-04 20:37:32224 // Is this the active network, i.e, the one through which
225 // network traffic is being routed? A network can be connected,
226 // but not be carrying traffic.
227 bool is_active() const { return is_active_; }
[email protected]07ed5c082011-03-02 19:50:18228 bool favorite() const { return favorite_; }
229 bool auto_connect() const { return auto_connect_; }
230 ConnectivityState connectivity_state() const { return connectivity_state_; }
[email protected]00e17562010-05-06 18:59:25231
[email protected]07ed5c082011-03-02 19:50:18232 // We don't have a setter for |favorite_| because to unfavorite a network is
233 // equivalent to forget a network, so we call forget network on cros for
234 // that. See ForgetWifiNetwork().
[email protected]d63f4752011-03-07 21:37:18235 void SetAutoConnect(bool auto_connect);
[email protected]00e17562010-05-06 18:59:25236
[email protected]5a1c2dd2010-05-11 22:52:30237 // Return a string representation of the state code.
[email protected]caf063d2010-10-11 18:58:22238 std::string GetStateString() const;
[email protected]5a1c2dd2010-05-11 22:52:30239
240 // Return a string representation of the error code.
[email protected]caf063d2010-10-11 18:58:22241 std::string GetErrorString() const;
[email protected]5a1c2dd2010-05-11 22:52:30242
[email protected]00e17562010-05-06 18:59:25243 protected:
[email protected]894ee392011-02-25 21:05:02244 Network(const std::string& service_path, ConnectionType type)
245 : state_(STATE_UNKNOWN),
[email protected]ceaf8f12010-11-04 20:37:32246 error_(ERROR_UNKNOWN),
[email protected]ca9f6702010-11-17 01:28:14247 connectable_(true),
[email protected]894ee392011-02-25 21:05:02248 is_active_(false),
[email protected]07ed5c082011-03-02 19:50:18249 favorite_(false),
250 auto_connect_(false),
251 connectivity_state_(CONN_STATE_UNKNOWN),
[email protected]83c82652011-03-05 01:59:34252 priority_(0),
[email protected]894ee392011-02-25 21:05:02253 service_path_(service_path),
[email protected]07ed5c082011-03-02 19:50:18254 type_(type) {}
255
256 // Parse name/value pairs from libcros.
257 virtual bool ParseValue(int index, const Value* value);
258 void ParseInfo(const DictionaryValue* info);
[email protected]00e17562010-05-06 18:59:25259
[email protected]ebbdaaba2011-03-24 17:45:16260 // Methods to asynchronously set network service properties
[email protected]d63f4752011-03-07 21:37:18261 virtual void SetStringProperty(const char* prop, const std::string& str);
262 virtual void SetBooleanProperty(const char* prop, bool b);
263 virtual void SetIntegerProperty(const char* prop, int i);
[email protected]ebbdaaba2011-03-24 17:45:16264 virtual void ClearProperty(const char* prop);
265 // This will clear the property if string is empty. Otherwise, it will set it.
266 virtual void SetOrClearStringProperty(const char* prop,
267 const std::string& str);
[email protected]d63f4752011-03-07 21:37:18268
[email protected]00e17562010-05-06 18:59:25269 std::string device_path_;
[email protected]894ee392011-02-25 21:05:02270 std::string name_;
[email protected]00e17562010-05-06 18:59:25271 std::string ip_address_;
[email protected]894ee392011-02-25 21:05:02272 ConnectionMode mode_;
[email protected]9a24152e2010-10-08 23:15:31273 ConnectionState state_;
[email protected]5a1c2dd2010-05-11 22:52:30274 ConnectionError error_;
[email protected]ca9f6702010-11-17 01:28:14275 bool connectable_;
[email protected]ceaf8f12010-11-04 20:37:32276 bool is_active_;
[email protected]07ed5c082011-03-02 19:50:18277 bool favorite_;
278 bool auto_connect_;
279 ConnectivityState connectivity_state_;
[email protected]ceaf8f12010-11-04 20:37:32280
281 private:
[email protected]07ed5c082011-03-02 19:50:18282 void set_name(const std::string& name) { name_ = name; }
283 void set_connecting(bool connecting) {
284 state_ = (connecting ? STATE_ASSOCIATION : STATE_IDLE);
285 }
286 void set_connected(bool connected) {
287 state_ = (connected ? STATE_READY : STATE_IDLE);
288 }
[email protected]ceaf8f12010-11-04 20:37:32289 void set_state(ConnectionState state) { state_ = state; }
[email protected]ca9f6702010-11-17 01:28:14290 void set_connectable(bool connectable) { connectable_ = connectable; }
[email protected]ceaf8f12010-11-04 20:37:32291 void set_active(bool is_active) { is_active_ = is_active; }
[email protected]fe3a05092010-11-24 23:53:16292 void set_error(ConnectionError error) { error_ = error; }
[email protected]07ed5c082011-03-02 19:50:18293 void set_connectivity_state(ConnectivityState connectivity_state) {
294 connectivity_state_ = connectivity_state;
295 }
[email protected]ceaf8f12010-11-04 20:37:32296
[email protected]ebbdaaba2011-03-24 17:45:16297 virtual void SetValueProperty(const char* prop, Value* val);
298
[email protected]9e320292010-11-18 00:52:29299 // Initialize the IP address field
300 void InitIPAddress();
301
[email protected]83c82652011-03-05 01:59:34302 // Priority value, corresponds to index in list from flimflam (0 = highest)
303 int priority_;
304
[email protected]894ee392011-02-25 21:05:02305 // These must not be modified after construction.
306 std::string service_path_;
307 ConnectionType type_;
308
[email protected]ceaf8f12010-11-04 20:37:32309 friend class NetworkLibraryImpl;
[email protected]07ed5c082011-03-02 19:50:18310 // ChangeAutoConnectSaveTest accesses |favorite_|.
311 FRIEND_TEST_ALL_PREFIXES(WifiConfigViewTest, ChangeAutoConnectSaveTest);
[email protected]0fb499a2009-11-10 21:03:33312};
313
[email protected]894ee392011-02-25 21:05:02314// Class for networks of TYPE_ETHERNET.
[email protected]00e17562010-05-06 18:59:25315class EthernetNetwork : public Network {
316 public:
[email protected]894ee392011-02-25 21:05:02317 explicit EthernetNetwork(const std::string& service_path) :
318 Network(service_path, TYPE_ETHERNET) {
[email protected]45066d52010-10-29 17:24:57319 }
[email protected]00e17562010-05-06 18:59:25320};
321
[email protected]83c82652011-03-05 01:59:34322// Class for networks of TYPE_VPN.
323class VirtualNetwork : public Network {
324 public:
325 explicit VirtualNetwork(const std::string& service_path) :
326 Network(service_path, TYPE_VPN) {
327 }
328};
329typedef std::vector<VirtualNetwork*> VirtualNetworkVector;
330
[email protected]894ee392011-02-25 21:05:02331// Base class for networks of TYPE_WIFI or TYPE_CELLULAR.
[email protected]00e17562010-05-06 18:59:25332class WirelessNetwork : public Network {
333 public:
[email protected]00e17562010-05-06 18:59:25334 int strength() const { return strength_; }
[email protected]00e17562010-05-06 18:59:25335
336 protected:
[email protected]894ee392011-02-25 21:05:02337 WirelessNetwork(const std::string& service_path, ConnectionType type)
338 : Network(service_path, type),
[email protected]07ed5c082011-03-02 19:50:18339 strength_(0) {}
[email protected]00e17562010-05-06 18:59:25340 int strength_;
[email protected]07ed5c082011-03-02 19:50:18341
342 // Network overrides.
343 virtual bool ParseValue(int index, const Value* value);
[email protected]ceaf8f12010-11-04 20:37:32344
345 private:
[email protected]ceaf8f12010-11-04 20:37:32346 void set_strength(int strength) { strength_ = strength; }
347
348 friend class NetworkLibraryImpl;
[email protected]00e17562010-05-06 18:59:25349};
350
[email protected]894ee392011-02-25 21:05:02351// Class for networks of TYPE_CELLULAR.
352class CellularDataPlan;
[email protected]170d19c2011-03-03 20:42:08353
[email protected]00e17562010-05-06 18:59:25354class CellularNetwork : public WirelessNetwork {
355 public:
[email protected]543a79a2010-10-20 01:14:58356 enum DataLeft {
[email protected]eda4d0c2011-01-24 19:52:02357 DATA_UNKNOWN,
[email protected]543a79a2010-10-20 01:14:58358 DATA_NORMAL,
359 DATA_LOW,
360 DATA_VERY_LOW,
361 DATA_NONE
362 };
363
[email protected]05f06df2010-11-04 17:31:16364 virtual ~CellularNetwork();
[email protected]894ee392011-02-25 21:05:02365
366 explicit CellularNetwork(const std::string& service_path)
367 : WirelessNetwork(service_path, TYPE_CELLULAR),
368 activation_state_(ACTIVATION_STATE_UNKNOWN),
369 network_technology_(NETWORK_TECHNOLOGY_UNKNOWN),
370 roaming_state_(ROAMING_STATE_UNKNOWN),
[email protected]894ee392011-02-25 21:05:02371 data_left_(DATA_UNKNOWN) {
372 }
[email protected]9d3f7d5e2010-09-25 06:17:04373 // Starts device activation process. Returns false if the device state does
374 // not permit activation.
375 bool StartActivation() const;
[email protected]a4a89cf2011-03-16 18:17:54376 // Requests data plans if the network is conencted and activated.
377 // Plan data will be passed through Network::Observer::CellularDataPlanChanged
378 // callback.
379 void RefreshDataPlansIfNeeded() const;
380
[email protected]ad75cda2010-09-28 23:18:46381 const ActivationState activation_state() const { return activation_state_; }
[email protected]a4a89cf2011-03-16 18:17:54382 bool activated() const {
383 return activation_state() == ACTIVATION_STATE_ACTIVATED;
384 }
[email protected]17cc0522010-10-01 20:19:12385 const NetworkTechnology network_technology() const {
[email protected]caf063d2010-10-11 18:58:22386 return network_technology_;
387 }
[email protected]17cc0522010-10-01 20:19:12388 const NetworkRoamingState roaming_state() const { return roaming_state_; }
[email protected]353e0dd82010-11-22 08:39:31389 bool restricted_pool() const {
390 return connectivity_state() == CONN_STATE_RESTRICTED;
391 }
[email protected]f0053942010-11-13 02:33:54392 bool needs_new_plan() const {
[email protected]a4a89cf2011-03-16 18:17:54393 return restricted_pool() && connected() && activated();
[email protected]f0053942010-11-13 02:33:54394 }
[email protected]9a24152e2010-10-08 23:15:31395 const std::string& operator_name() const { return operator_name_; }
396 const std::string& operator_code() const { return operator_code_; }
[email protected]9d3f7d5e2010-09-25 06:17:04397 const std::string& payment_url() const { return payment_url_; }
[email protected]07ed5c082011-03-02 19:50:18398 const std::string& usage_url() const { return usage_url_; }
[email protected]894ee392011-02-25 21:05:02399 DataLeft data_left() const { return data_left_; }
400
401 // Misc.
[email protected]633e5c02010-11-19 22:11:35402 bool is_gsm() const {
403 return network_technology_ != NETWORK_TECHNOLOGY_EVDO &&
404 network_technology_ != NETWORK_TECHNOLOGY_1XRTT &&
405 network_technology_ != NETWORK_TECHNOLOGY_UNKNOWN;
406 }
[email protected]3cc2e592010-06-21 13:45:43407
[email protected]caf063d2010-10-11 18:58:22408 // Return a string representation of network technology.
409 std::string GetNetworkTechnologyString() const;
[email protected]353e0dd82010-11-22 08:39:31410 // Return a string representation of connectivity state.
411 std::string GetConnectivityStateString() const;
[email protected]caf063d2010-10-11 18:58:22412 // Return a string representation of activation state.
413 std::string GetActivationStateString() const;
414 // Return a string representation of roaming state.
415 std::string GetRoamingStateString() const;
416
[email protected]3c1139f2010-10-22 06:17:42417 // Return a string representation of |activation_state|.
418 static std::string ActivationStateToString(ActivationState activation_state);
419
[email protected]9d3f7d5e2010-09-25 06:17:04420 protected:
[email protected]07ed5c082011-03-02 19:50:18421 // WirelessNetwork overrides.
422 virtual bool ParseValue(int index, const Value* value);
[email protected]45066d52010-10-29 17:24:57423
[email protected]ad75cda2010-09-28 23:18:46424 ActivationState activation_state_;
[email protected]17cc0522010-10-01 20:19:12425 NetworkTechnology network_technology_;
426 NetworkRoamingState roaming_state_;
[email protected]7b947813da2010-10-07 23:47:42427 // Carrier Info
428 std::string operator_name_;
429 std::string operator_code_;
[email protected]9d3f7d5e2010-09-25 06:17:04430 std::string payment_url_;
[email protected]07ed5c082011-03-02 19:50:18431 std::string usage_url_;
[email protected]894ee392011-02-25 21:05:02432 // Cached values
433 DataLeft data_left_; // Updated when data plans are updated.
[email protected]ceaf8f12010-11-04 20:37:32434
435 private:
436 void set_activation_state(ActivationState state) {
437 activation_state_ = state;
438 }
[email protected]894ee392011-02-25 21:05:02439 void set_payment_url(const std::string& url) { payment_url_ = url; }
[email protected]07ed5c082011-03-02 19:50:18440 void set_usage_url(const std::string& url) { usage_url_ = url; }
[email protected]ceaf8f12010-11-04 20:37:32441 void set_network_technology(NetworkTechnology technology) {
442 network_technology_ = technology;
443 }
[email protected]894ee392011-02-25 21:05:02444 void set_roaming_state(NetworkRoamingState state) { roaming_state_ = state; }
[email protected]894ee392011-02-25 21:05:02445 void set_data_left(DataLeft data_left) { data_left_ = data_left; }
[email protected]ceaf8f12010-11-04 20:37:32446
447 friend class NetworkLibraryImpl;
[email protected]00e17562010-05-06 18:59:25448};
[email protected]894ee392011-02-25 21:05:02449typedef std::vector<CellularNetwork*> CellularNetworkVector;
[email protected]00e17562010-05-06 18:59:25450
[email protected]894ee392011-02-25 21:05:02451// Class for networks of TYPE_WIFI.
[email protected]00e17562010-05-06 18:59:25452class WifiNetwork : public WirelessNetwork {
453 public:
[email protected]894ee392011-02-25 21:05:02454 explicit WifiNetwork(const std::string& service_path)
455 : WirelessNetwork(service_path, TYPE_WIFI),
[email protected]d63f4752011-03-07 21:37:18456 encryption_(SECURITY_NONE),
[email protected]9100a362011-03-22 08:20:26457 passphrase_required_(false),
458 eap_method_(EAP_METHOD_UNKNOWN),
459 eap_phase_2_auth_(EAP_PHASE_2_AUTH_AUTO),
460 eap_use_system_cas_(true) {
[email protected]894ee392011-02-25 21:05:02461 }
[email protected]7598ab52009-10-08 01:33:21462
[email protected]00e17562010-05-06 18:59:25463 bool encrypted() const { return encryption_ != SECURITY_NONE; }
464 ConnectionSecurity encryption() const { return encryption_; }
465 const std::string& passphrase() const { return passphrase_; }
[email protected]d47313852010-05-11 23:01:04466 const std::string& identity() const { return identity_; }
467 const std::string& cert_path() const { return cert_path_; }
[email protected]00e17562010-05-06 18:59:25468
[email protected]9100a362011-03-22 08:20:26469 EAPMethod eap_method() const { return eap_method_; }
470 EAPPhase2Auth eap_phase_2_auth() const { return eap_phase_2_auth_; }
471 const std::string& eap_server_ca_cert() const {
472 return eap_server_ca_cert_path_; }
473 const std::string& eap_client_cert() const { return eap_client_cert_path_; }
474 const bool eap_use_system_cas() const { return eap_use_system_cas_; }
475 const std::string& eap_identity() const { return eap_identity_; }
476 const std::string& eap_anonymous_identity() const {
477 return eap_anonymous_identity_; }
478 const std::string& eap_passphrase() const { return eap_passphrase_; }
479
[email protected]2f5a9a852011-03-19 00:54:34480 const std::string& GetPassphrase() const;
[email protected]00e17562010-05-06 18:59:25481
[email protected]d63f4752011-03-07 21:37:18482 void SetPassphrase(const std::string& passphrase);
483 void SetIdentity(const std::string& identity);
484 void SetCertPath(const std::string& cert_path);
485
[email protected]9100a362011-03-22 08:20:26486 // 802.1x properties
487 void SetEAPMethod(EAPMethod method);
488 void SetEAPPhase2Auth(EAPPhase2Auth auth);
489 void SetEAPServerCACert(const std::string& cert_path);
490 void SetEAPClientCert(const std::string& cert_path);
491 void SetEAPUseSystemCAs(bool use_system_cas);
492 void SetEAPIdentity(const std::string& identity);
493 void SetEAPAnonymousIdentity(const std::string& identity);
494 void SetEAPPassphrase(const std::string& passphrase);
495
[email protected]5a1c2dd2010-05-11 22:52:30496 // Return a string representation of the encryption code.
497 // This not translated and should be only used for debugging purposes.
498 std::string GetEncryptionString();
499
[email protected]84161362010-12-15 16:50:33500 // Return true if a passphrase or other input is required to connect.
501 bool IsPassphraseRequired() const;
502
[email protected]c1ee0062010-10-14 18:37:22503 // Return true if cert_path_ indicates that we have loaded the certificate.
504 bool IsCertificateLoaded() const;
505
[email protected]5a1c2dd2010-05-11 22:52:30506 protected:
[email protected]07ed5c082011-03-02 19:50:18507 // WirelessNetwork overrides.
508 virtual bool ParseValue(int index, const Value* value);
509
[email protected]d63f4752011-03-07 21:37:18510 private:
[email protected]2f5a9a852011-03-19 00:54:34511 void set_encryption(ConnectionSecurity encryption) {
512 encryption_ = encryption;
513 }
514 void set_passphrase(const std::string& passphrase) {
515 passphrase_ = passphrase;
516 }
[email protected]d63f4752011-03-07 21:37:18517 void set_passphrase_required(bool passphrase_required) {
518 passphrase_required_ = passphrase_required;
519 }
[email protected]2f5a9a852011-03-19 00:54:34520 void set_identity(const std::string& identity) {
521 identity_ = identity;
522 }
523 void set_cert_path(const std::string& cert_path) {
524 cert_path_ = cert_path;
525 }
[email protected]d63f4752011-03-07 21:37:18526
[email protected]00e17562010-05-06 18:59:25527 ConnectionSecurity encryption_;
528 std::string passphrase_;
[email protected]e027da192010-11-08 21:35:08529 bool passphrase_required_;
[email protected]d47313852010-05-11 23:01:04530 std::string identity_;
531 std::string cert_path_;
[email protected]d63f4752011-03-07 21:37:18532
[email protected]9100a362011-03-22 08:20:26533 EAPMethod eap_method_;
534 EAPPhase2Auth eap_phase_2_auth_;
535 std::string eap_server_ca_cert_path_;
536 std::string eap_client_cert_path_;
537 bool eap_use_system_cas_;
538 std::string eap_identity_;
539 std::string eap_anonymous_identity_;
540 std::string eap_passphrase_;
541
[email protected]2f5a9a852011-03-19 00:54:34542 // Internal state (not stored in flimflam).
543 // Passphrase set by user (stored for UI).
544 std::string user_passphrase_;
545
[email protected]d63f4752011-03-07 21:37:18546 friend class NetworkLibraryImpl;
[email protected]7598ab52009-10-08 01:33:21547};
[email protected]894ee392011-02-25 21:05:02548typedef std::vector<WifiNetwork*> WifiNetworkVector;
[email protected]00e17562010-05-06 18:59:25549
[email protected]894ee392011-02-25 21:05:02550// Cellular Data Plan management.
551class CellularDataPlan {
552 public:
553 CellularDataPlan()
554 : plan_name("Unknown"),
555 plan_type(CELLULAR_DATA_PLAN_UNLIMITED),
556 plan_data_bytes(0),
557 data_bytes_used(0) { }
558 explicit CellularDataPlan(const CellularDataPlanInfo &plan)
559 : plan_name(plan.plan_name ? plan.plan_name : ""),
560 plan_type(plan.plan_type),
561 update_time(base::Time::FromInternalValue(plan.update_time)),
562 plan_start_time(base::Time::FromInternalValue(plan.plan_start_time)),
563 plan_end_time(base::Time::FromInternalValue(plan.plan_end_time)),
564 plan_data_bytes(plan.plan_data_bytes),
565 data_bytes_used(plan.data_bytes_used) { }
566 // Formats cellular plan description.
567 string16 GetPlanDesciption() const;
568 // Evaluates cellular plans status and returns warning string if it is near
569 // expiration.
570 string16 GetRemainingWarning() const;
571 // Formats remaining plan data description.
572 string16 GetDataRemainingDesciption() const;
573 // Formats plan expiration description.
574 string16 GetPlanExpiration() const;
575 // Formats plan usage info.
576 string16 GetUsageInfo() const;
577 // Returns a unique string for this plan that can be used for comparisons.
578 std::string GetUniqueIdentifier() const;
579 base::TimeDelta remaining_time() const;
580 int64 remaining_minutes() const;
[email protected]170d19c2011-03-03 20:42:08581 // Returns plan data remaining in bytes.
[email protected]894ee392011-02-25 21:05:02582 int64 remaining_data() const;
[email protected]894ee392011-02-25 21:05:02583 // TODO(stevenjb): Make these private with accessors and properly named.
584 std::string plan_name;
585 CellularDataPlanType plan_type;
586 base::Time update_time;
587 base::Time plan_start_time;
588 base::Time plan_end_time;
589 int64 plan_data_bytes;
590 int64 data_bytes_used;
591};
592typedef ScopedVector<CellularDataPlan> CellularDataPlanVector;
[email protected]38d387cd2010-01-14 01:00:39593
[email protected]894ee392011-02-25 21:05:02594// Geolocation data.
[email protected]63259a52010-07-15 10:55:38595struct CellTower {
596 enum RadioType {
597 RADIOTYPE_GSM,
598 RADIOTYPE_CDMA,
599 RADIOTYPE_WCDMA,
600 } radio_type; // GSM/WCDMA CDMA
601 int mobile_country_code; // MCC MCC
602 int mobile_network_code; // MNC SID
603 int location_area_code; // LAC NID
604 int cell_id; // CID BID
[email protected]17f12b52010-10-02 02:08:16605 base::Time timestamp; // Timestamp when this cell was primary
606 int signal_strength; // Radio signal strength measured in dBm.
607 int timing_advance; // Represents the distance from the cell tower.
608 // Each unit is roughly 550 meters.
[email protected]63259a52010-07-15 10:55:38609};
610
611struct WifiAccessPoint {
612 std::string mac_address; // The mac address of the WiFi node.
613 std::string name; // The SSID of the WiFi node.
614 base::Time timestamp; // Timestamp when this AP was detected.
615 int signal_strength; // Radio signal strength measured in dBm.
616 int signal_to_noise; // Current signal to noise ratio measured in dB.
617 int channel; // Wifi channel number.
618};
619
620typedef std::vector<CellTower> CellTowerVector;
621typedef std::vector<WifiAccessPoint> WifiAccessPointVector;
622
[email protected]894ee392011-02-25 21:05:02623// IP Configuration.
[email protected]3fb19f5c2010-02-17 00:49:50624struct NetworkIPConfig {
625 NetworkIPConfig(const std::string& device_path, IPConfigType type,
626 const std::string& address, const std::string& netmask,
627 const std::string& gateway, const std::string& name_servers)
628 : device_path(device_path),
629 type(type),
630 address(address),
631 netmask(netmask),
632 gateway(gateway),
633 name_servers(name_servers) {}
634
635 // NetworkIPConfigs are sorted by tyoe.
636 bool operator< (const NetworkIPConfig& other) const {
637 return type < other.type;
638 }
639
640 std::string device_path;
641 IPConfigType type;
[email protected]ea21f9c2010-10-21 08:26:54642 std::string address; // This looks like "/device/0011aa22bb33"
[email protected]3fb19f5c2010-02-17 00:49:50643 std::string netmask;
644 std::string gateway;
645 std::string name_servers;
646};
647typedef std::vector<NetworkIPConfig> NetworkIPConfigVector;
648
[email protected]819a43862010-08-12 10:09:21649// This class handles the interaction with the ChromeOS network library APIs.
650// Classes can add themselves as observers. Users can get an instance of the
651// library like this: chromeos::CrosLibrary::Get()->GetNetworkLibrary()
[email protected]62c7ef32010-03-23 23:44:24652class NetworkLibrary {
[email protected]7598ab52009-10-08 01:33:21653 public:
[email protected]ceaf8f12010-11-04 20:37:32654 class NetworkManagerObserver {
[email protected]7598ab52009-10-08 01:33:21655 public:
[email protected]ceaf8f12010-11-04 20:37:32656 // Called when the state of the network manager has changed,
657 // for example, networks have appeared or disappeared.
658 virtual void OnNetworkManagerChanged(NetworkLibrary* obj) = 0;
[email protected]7c3cc8a32010-11-03 06:58:14659 };
660
[email protected]ceaf8f12010-11-04 20:37:32661 class NetworkObserver {
[email protected]7c3cc8a32010-11-03 06:58:14662 public:
[email protected]ceaf8f12010-11-04 20:37:32663 // Called when the state of a single network has changed,
664 // for example signal strength or connection state.
665 virtual void OnNetworkChanged(NetworkLibrary* cros,
666 const Network* network) = 0;
667 };
668
669 class CellularDataPlanObserver {
670 public:
671 // Called when the cellular data plan has changed.
672 virtual void OnCellularDataPlanChanged(NetworkLibrary* obj) = 0;
[email protected]3c1139f2010-10-22 06:17:42673 };
674
[email protected]bd57d932011-02-15 19:40:26675 class UserActionObserver {
676 public:
677 // Called when user initiates a new connection.
678 // Network is NULL when we don't have an associated Network object.
679 virtual void OnConnectionInitiated(NetworkLibrary* cros,
680 const Network* network) = 0;
681 };
682
[email protected]62c7ef32010-03-23 23:44:24683 virtual ~NetworkLibrary() {}
[email protected]ceaf8f12010-11-04 20:37:32684
685 virtual void AddNetworkManagerObserver(NetworkManagerObserver* observer) = 0;
686 virtual void RemoveNetworkManagerObserver(
687 NetworkManagerObserver* observer) = 0;
688
689 // An attempt to add an observer that has already been added for a
690 // give service path will be ignored.
691 virtual void AddNetworkObserver(const std::string& service_path,
692 NetworkObserver* observer) = 0;
693 // Remove an observer of a single network
694 virtual void RemoveNetworkObserver(const std::string& service_path,
695 NetworkObserver* observer) = 0;
696 // Stop |observer| from observing any networks
697 virtual void RemoveObserverForAllNetworks(NetworkObserver* observer) = 0;
698
[email protected]1b57b4c2011-01-21 21:43:11699 // Temporarily locks down certain functionality in network library to prevent
700 // unplanned side effects. During the lock down, Enable*Device() calls cannot
701 // be made.
702 virtual void Lock() = 0;
703 // Removes temporarily lock of network library.
704 virtual void Unlock() = 0;
705 // Checks if access to network library is locked.
706 virtual bool IsLocked() = 0;
707
[email protected]ceaf8f12010-11-04 20:37:32708 virtual void AddCellularDataPlanObserver(
709 CellularDataPlanObserver* observer) = 0;
710 virtual void RemoveCellularDataPlanObserver(
711 CellularDataPlanObserver* observer) = 0;
[email protected]62c7ef32010-03-23 23:44:24712
[email protected]bd57d932011-02-15 19:40:26713 virtual void AddUserActionObserver(UserActionObserver* observer) = 0;
714 virtual void RemoveUserActionObserver(UserActionObserver* observer) = 0;
715
[email protected]83c82652011-03-05 01:59:34716 // Return the active or default Ethernet network (or NULL if none).
[email protected]37eec142010-12-02 18:47:09717 virtual const EthernetNetwork* ethernet_network() const = 0;
[email protected]62c7ef32010-03-23 23:44:24718 virtual bool ethernet_connecting() const = 0;
719 virtual bool ethernet_connected() const = 0;
720
[email protected]83c82652011-03-05 01:59:34721 // Return the active Wifi network (or NULL if none active).
[email protected]37eec142010-12-02 18:47:09722 virtual const WifiNetwork* wifi_network() const = 0;
[email protected]62c7ef32010-03-23 23:44:24723 virtual bool wifi_connecting() const = 0;
724 virtual bool wifi_connected() const = 0;
[email protected]62c7ef32010-03-23 23:44:24725
[email protected]83c82652011-03-05 01:59:34726 // Return the active Cellular network (or NULL if none active).
[email protected]37eec142010-12-02 18:47:09727 virtual const CellularNetwork* cellular_network() const = 0;
[email protected]62c7ef32010-03-23 23:44:24728 virtual bool cellular_connecting() const = 0;
729 virtual bool cellular_connected() const = 0;
[email protected]62c7ef32010-03-23 23:44:24730
[email protected]83c82652011-03-05 01:59:34731 // Return the active virtual network (or NULL if none active).
732 virtual const VirtualNetwork* virtual_network() const = 0;
733 virtual bool virtual_network_connecting() const = 0;
734 virtual bool virtual_network_connected() const = 0;
735
[email protected]62c7ef32010-03-23 23:44:24736 // Return true if any network is currently connected.
737 virtual bool Connected() const = 0;
738
739 // Return true if any network is currently connecting.
740 virtual bool Connecting() const = 0;
741
742 // Returns the current IP address if connected. If not, returns empty string.
[email protected]460c3f12010-11-08 18:20:58743 virtual const std::string& IPAddress() const = 0;
[email protected]62c7ef32010-03-23 23:44:24744
745 // Returns the current list of wifi networks.
746 virtual const WifiNetworkVector& wifi_networks() const = 0;
747
[email protected]665d7d62010-04-20 23:32:49748 // Returns the list of remembered wifi networks.
749 virtual const WifiNetworkVector& remembered_wifi_networks() const = 0;
750
[email protected]62c7ef32010-03-23 23:44:24751 // Returns the current list of cellular networks.
752 virtual const CellularNetworkVector& cellular_networks() const = 0;
753
[email protected]83c82652011-03-05 01:59:34754 // Returns the current list of virtual networks.
755 virtual const VirtualNetworkVector& virtual_networks() const = 0;
756
[email protected]894ee392011-02-25 21:05:02757 // Return a pointer to the device, if it exists, or NULL.
758 virtual const NetworkDevice* FindNetworkDeviceByPath(
759 const std::string& path) const = 0;
[email protected]45066d52010-10-29 17:24:57760
[email protected]894ee392011-02-25 21:05:02761 // Return a pointer to the network, if it exists, or NULL.
[email protected]2f5a9a852011-03-19 00:54:34762 // NOTE: Never store these results, store service paths instead.
763 // The pattern for doing an operation on a Network is:
764 // Network* network = cros->FindNetworkByPath(service_path);
765 // network->SetFoo();
766 // network->Connect();
767 // As long as this is done in sequence on the UI thread it will be safe;
768 // the network list only gets updated on the UI thread.
769 virtual Network* FindNetworkByPath(const std::string& path) const = 0;
770 virtual WifiNetwork* FindWifiNetworkByPath(const std::string& path) const = 0;
[email protected]d63f4752011-03-07 21:37:18771 virtual CellularNetwork* FindCellularNetworkByPath(
[email protected]894ee392011-02-25 21:05:02772 const std::string& path) const = 0;
773
774 // Retrieves the data plans associated with |path|, NULL if there are no
775 // associated plans.
776 virtual const CellularDataPlanVector* GetDataPlans(
777 const std::string& path) const = 0;
778
779 // This returns the significant data plan. If the user only has the
780 // base data plan, then return that. If there is a base and a paid data plan,
781 // then the significant one is the paid one. So return the paid plan.
782 // If there are no data plans, then this method returns NULL.
783 // This returns a pointer to a member of data_plans_, so if SetDataPlans()
784 // gets called, the result becomes invalid.
785 virtual const CellularDataPlan* GetSignificantDataPlan(
786 const std::string& path) const = 0;
[email protected]f9384b552010-06-28 23:02:26787
[email protected]62c7ef32010-03-23 23:44:24788 // Request a scan for new wifi networks.
[email protected]a4a89cf2011-03-16 18:17:54789 virtual void RequestNetworkScan() = 0;
[email protected]62c7ef32010-03-23 23:44:24790
[email protected]63259a52010-07-15 10:55:38791 // Reads out the results of the last wifi scan. These results are not
792 // pre-cached in the library, so the call may block whilst the results are
793 // read over IPC.
794 // Returns false if an error occurred in reading the results. Note that
795 // a true return code only indicates the result set was successfully read,
796 // it does not imply a scan has successfully completed yet.
797 virtual bool GetWifiAccessPoints(WifiAccessPointVector* result) = 0;
798
799 // TODO(joth): Add GetCellTowers to retrieve a CellTowerVector.
800
[email protected]2f5a9a852011-03-19 00:54:34801 // Connect to the specified wireless network.
[email protected]8ceaf0c2011-03-14 22:26:52802 virtual void ConnectToWifiNetwork(WifiNetwork* network) = 0;
[email protected]62c7ef32010-03-23 23:44:24803
[email protected]894ee392011-02-25 21:05:02804 // Same as above but searches for an existing network by name.
[email protected]8ceaf0c2011-03-14 22:26:52805 virtual void ConnectToWifiNetwork(const std::string& service_path) = 0;
[email protected]894ee392011-02-25 21:05:02806
[email protected]2f5a9a852011-03-19 00:54:34807 // Connect to the specified network with security, ssid, and passphrase.
[email protected]8ceaf0c2011-03-14 22:26:52808 virtual void ConnectToWifiNetwork(ConnectionSecurity security,
[email protected]0673a282010-11-02 20:41:44809 const std::string& ssid,
[email protected]2f5a9a852011-03-19 00:54:34810 const std::string& passphrase,
[email protected]4a5c59c82010-06-30 21:03:34811 const std::string& identity,
812 const std::string& certpath,
[email protected]88908d62010-04-30 21:50:45813 bool auto_connect) = 0;
[email protected]62c7ef32010-03-23 23:44:24814
815 // Connect to the specified cellular network.
[email protected]2f5a9a852011-03-19 00:54:34816 virtual void ConnectToCellularNetwork(CellularNetwork* network) = 0;
[email protected]62c7ef32010-03-23 23:44:24817
[email protected]c3a54e4b2011-01-18 04:37:05818 // Records information that cellular play payment had happened.
819 virtual void SignalCellularPlanPayment() = 0;
820
821 // Returns true if cellular plan payment had been recorded recently.
822 virtual bool HasRecentCellularPlanPayment() = 0;
823
[email protected]00e17562010-05-06 18:59:25824 // Disconnect from the specified wireless (either cellular or wifi) network.
825 virtual void DisconnectFromWirelessNetwork(
[email protected]45066d52010-10-29 17:24:57826 const WirelessNetwork* network) = 0;
[email protected]d1958fe2010-04-30 22:29:42827
[email protected]45066d52010-10-29 17:24:57828 // Forget the wifi network corresponding to service_path.
829 virtual void ForgetWifiNetwork(const std::string& service_path) = 0;
[email protected]665d7d62010-04-20 23:32:49830
[email protected]62c7ef32010-03-23 23:44:24831 virtual bool ethernet_available() const = 0;
832 virtual bool wifi_available() const = 0;
833 virtual bool cellular_available() const = 0;
834
835 virtual bool ethernet_enabled() const = 0;
836 virtual bool wifi_enabled() const = 0;
837 virtual bool cellular_enabled() const = 0;
838
[email protected]d3f9b882010-11-18 22:30:40839 virtual bool wifi_scanning() const = 0;
840
[email protected]ceaf8f12010-11-04 20:37:32841 virtual const Network* active_network() const = 0;
842
[email protected]62c7ef32010-03-23 23:44:24843 virtual bool offline_mode() const = 0;
844
845 // Enables/disables the ethernet network device.
846 virtual void EnableEthernetNetworkDevice(bool enable) = 0;
847
848 // Enables/disables the wifi network device.
849 virtual void EnableWifiNetworkDevice(bool enable) = 0;
850
851 // Enables/disables the cellular network device.
852 virtual void EnableCellularNetworkDevice(bool enable) = 0;
853
854 // Enables/disables offline mode.
855 virtual void EnableOfflineMode(bool enable) = 0;
856
[email protected]ea21f9c2010-10-21 08:26:54857 // Fetches IP configs and hardware address for a given device_path.
858 // The hardware address is usually a MAC address like "0011AA22BB33".
859 // |hardware_address| will be an empty string, if no hardware address is
860 // found.
[email protected]62c7ef32010-03-23 23:44:24861 virtual NetworkIPConfigVector GetIPConfigs(
[email protected]ea21f9c2010-10-21 08:26:54862 const std::string& device_path,
863 std::string* hardware_address) = 0;
[email protected]5a1c2dd2010-05-11 22:52:30864
865 // Fetches debug network info for display in about:network.
866 // The page will have a meta refresh of |refresh| seconds if |refresh| > 0.
867 virtual std::string GetHtmlInfo(int refresh) = 0;
[email protected]62c7ef32010-03-23 23:44:24868
[email protected]819a43862010-08-12 10:09:21869 // Factory function, creates a new instance and returns ownership.
870 // For normal usage, access the singleton via CrosLibrary::Get().
[email protected]617fd0d2010-08-04 20:05:17871 static NetworkLibrary* GetImpl(bool stub);
[email protected]7598ab52009-10-08 01:33:21872};
873
[email protected]b22c21c2009-10-30 00:35:00874} // namespace chromeos
875
[email protected]268b02f2010-02-04 21:07:15876#endif // CHROME_BROWSER_CHROMEOS_CROS_NETWORK_LIBRARY_H_