1 // Copyright (c) 2011 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 CHROME_BROWSER_CHROMEOS_CROS_NETWORK_LIBRARY_H_ 6 #define CHROME_BROWSER_CHROMEOS_CROS_NETWORK_LIBRARY_H_ 7 #pragma once 8 9 #include <string> 10 #include <vector> 11 12 #include "base/gtest_prod_util.h" 13 #include "base/memory/scoped_vector.h" 14 #include "base/memory/singleton.h" 15 #include "base/observer_list.h" 16 #include "base/string16.h" 17 #include "base/timer.h" 18 #include "third_party/cros/chromeos_network.h" 19 20 class DictionaryValue; 21 class Value; 22 23 namespace chromeos { 24 25 // Connection enums (see flimflam/include/service.h) 26 enum 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, 33 TYPE_VPN = 6, 34 }; 35 36 enum ConnectionMode { 37 MODE_UNKNOWN = 0, 38 MODE_MANAGED = 1, 39 MODE_ADHOC = 2, 40 }; 41 42 enum 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 SECURITY_PSK = 6, 50 }; 51 52 enum ConnectionState { 53 STATE_UNKNOWN = 0, 54 STATE_IDLE = 1, 55 STATE_CARRIER = 2, 56 STATE_ASSOCIATION = 3, 57 STATE_CONFIGURATION = 4, 58 STATE_READY = 5, 59 STATE_DISCONNECT = 6, 60 STATE_FAILURE = 7, 61 STATE_ACTIVATION_FAILURE = 8 62 }; 63 64 enum ConnectivityState { 65 CONN_STATE_UNKNOWN = 0, 66 CONN_STATE_UNRESTRICTED = 1, 67 CONN_STATE_RESTRICTED = 2, 68 CONN_STATE_NONE = 3 69 }; 70 71 // Network enums (see flimflam/include/network.h) 72 enum NetworkTechnology { 73 NETWORK_TECHNOLOGY_UNKNOWN = 0, 74 NETWORK_TECHNOLOGY_1XRTT = 1, 75 NETWORK_TECHNOLOGY_EVDO = 2, 76 NETWORK_TECHNOLOGY_GPRS = 3, 77 NETWORK_TECHNOLOGY_EDGE = 4, 78 NETWORK_TECHNOLOGY_UMTS = 5, 79 NETWORK_TECHNOLOGY_HSPA = 6, 80 NETWORK_TECHNOLOGY_HSPA_PLUS = 7, 81 NETWORK_TECHNOLOGY_LTE = 8, 82 NETWORK_TECHNOLOGY_LTE_ADVANCED = 9, 83 }; 84 85 enum ActivationState { 86 ACTIVATION_STATE_UNKNOWN = 0, 87 ACTIVATION_STATE_ACTIVATED = 1, 88 ACTIVATION_STATE_ACTIVATING = 2, 89 ACTIVATION_STATE_NOT_ACTIVATED = 3, 90 ACTIVATION_STATE_PARTIALLY_ACTIVATED = 4, 91 }; 92 93 enum NetworkRoamingState { 94 ROAMING_STATE_UNKNOWN = 0, 95 ROAMING_STATE_HOME = 1, 96 ROAMING_STATE_ROAMING = 2, 97 }; 98 99 // SIMLock states (see gobi-cromo-plugin/gobi_gsm_modem.cc) 100 enum SIMLockState { 101 SIM_UNKNOWN = 0, 102 SIM_UNLOCKED = 1, 103 SIM_LOCKED_PIN = 2, 104 SIM_LOCKED_PUK = 3, // also when SIM is blocked, then retries = 0. 105 }; 106 107 // SIM PinRequire states. Since PinRequire current state is not exposed as a 108 // cellular property, we initialize it's value based on the SIMLockState 109 // initial value. 110 // SIM_PIN_REQUIRE_UNKNOWN - SIM card is absent or SIMLockState initial value 111 // hasn't been received yet. 112 // SIM_PIN_REQUIRED - SIM card is locked when booted/wake from sleep and 113 // requires user to enter PIN. 114 // SIM_PIN_NOT_REQUIRED - SIM card is unlocked all the time and requires PIN 115 // only on certain operations, such as ChangeRequirePin, ChangePin, EnterPin. 116 enum SIMPinRequire { 117 SIM_PIN_REQUIRE_UNKNOWN = 0, 118 SIM_PIN_NOT_REQUIRED = 1, 119 SIM_PIN_REQUIRED = 2, 120 }; 121 122 // Any PIN operation result (EnterPin, UnblockPin etc.). 123 enum PinOperationError { 124 PIN_ERROR_NONE = 0, 125 PIN_ERROR_UNKNOWN = 1, 126 PIN_ERROR_INCORRECT_CODE = 2, // Either PIN/PUK specified is incorrect. 127 PIN_ERROR_BLOCKED = 3, // No more PIN retries left, SIM is blocked. 128 }; 129 130 // connection errors (see flimflam/include/service.h) 131 enum ConnectionError { 132 ERROR_NO_ERROR = 0, 133 ERROR_OUT_OF_RANGE = 1, 134 ERROR_PIN_MISSING = 2, 135 ERROR_DHCP_FAILED = 3, 136 ERROR_CONNECT_FAILED = 4, 137 ERROR_BAD_PASSPHRASE = 5, 138 ERROR_BAD_WEPKEY = 6, 139 ERROR_ACTIVATION_FAILED = 7, 140 ERROR_NEED_EVDO = 8, 141 ERROR_NEED_HOME_NETWORK = 9, 142 ERROR_OTASP_FAILED = 10, 143 ERROR_AAA_FAILED = 11, 144 }; 145 146 // We are currently only supporting setting a single EAP Method. 147 enum EAPMethod { 148 EAP_METHOD_UNKNOWN = 0, 149 EAP_METHOD_PEAP = 1, 150 EAP_METHOD_TLS = 2, 151 EAP_METHOD_TTLS = 3, 152 EAP_METHOD_LEAP = 4 153 }; 154 155 // We are currently only supporting setting a single EAP phase 2 authentication. 156 enum EAPPhase2Auth { 157 EAP_PHASE_2_AUTH_AUTO = 0, 158 EAP_PHASE_2_AUTH_MD5 = 1, 159 EAP_PHASE_2_AUTH_MSCHAPV2 = 2, 160 EAP_PHASE_2_AUTH_MSCHAP = 3, 161 EAP_PHASE_2_AUTH_PAP = 4, 162 EAP_PHASE_2_AUTH_CHAP = 5 163 }; 164 165 // Simple wrapper for property Cellular.FoundNetworks. 166 struct FoundCellularNetwork { 167 std::string status; 168 std::string network_id; 169 std::string short_name; 170 std::string long_name; 171 std::string technology; 172 }; 173 174 typedef std::vector<FoundCellularNetwork> CellularNetworkList; 175 176 // Cellular network is considered low data when less than 60 minues. 177 static const int kCellularDataLowSecs = 60 * 60; 178 179 // Cellular network is considered low data when less than 30 minues. 180 static const int kCellularDataVeryLowSecs = 30 * 60; 181 182 // Cellular network is considered low data when less than 100MB. 183 static const int kCellularDataLowBytes = 100 * 1024 * 1024; 184 185 // Cellular network is considered very low data when less than 50MB. 186 static const int kCellularDataVeryLowBytes = 50 * 1024 * 1024; 187 188 // Contains data related to the flimflam.Device interface, 189 // e.g. ethernet, wifi, cellular. 190 // TODO(dpolukhin): refactor to make base class and device specific derivatives. 191 class NetworkDevice { 192 public: 193 explicit NetworkDevice(const std::string& device_path); 194 195 // Device info. 196 const std::string& device_path() const { return device_path_; } 197 const std::string& name() const { return name_; } 198 ConnectionType type() const { return type_; } 199 bool scanning() const { return scanning_; } 200 const std::string& meid() const { return MEID_; } 201 const std::string& imei() const { return IMEI_; } 202 const std::string& imsi() const { return IMSI_; } 203 const std::string& esn() const { return ESN_; } 204 const std::string& mdn() const { return MDN_; } 205 const std::string& min() const { return MIN_; } 206 const std::string& model_id() const { return model_id_; } 207 const std::string& manufacturer() const { return manufacturer_; } 208 SIMLockState sim_lock_state() const { return sim_lock_state_; } 209 bool is_sim_locked() const { 210 return sim_lock_state_ == SIM_LOCKED_PIN || 211 sim_lock_state_ == SIM_LOCKED_PUK; 212 } 213 const int sim_retries_left() const { return sim_retries_left_; } 214 SIMPinRequire sim_pin_required() const { return sim_pin_required_; } 215 const std::string& firmware_revision() const { return firmware_revision_; } 216 const std::string& hardware_revision() const { return hardware_revision_; } 217 const unsigned int prl_version() const { return PRL_version_; } 218 const std::string& home_provider() const { return home_provider_; } 219 const std::string& home_provider_code() const { return home_provider_code_; } 220 const std::string& home_provider_country() const { 221 return home_provider_country_; 222 } 223 const std::string& home_provider_id() const { return home_provider_id_; } 224 const std::string& home_provider_name() const { return home_provider_name_; } 225 const std::string& selected_cellular_network() const { 226 return selected_cellular_network_; 227 } 228 const CellularNetworkList& found_cellular_networks() const { 229 return found_cellular_networks_; 230 } 231 bool data_roaming_allowed() const { return data_roaming_allowed_; } 232 bool support_network_scan() const { return support_network_scan_; } 233 234 private: 235 bool ParseValue(int index, const Value* value); 236 void ParseInfo(const DictionaryValue* info); 237 238 // General device info. 239 std::string device_path_; 240 std::string name_; 241 ConnectionType type_; 242 bool scanning_; 243 // Cellular specific device info. 244 std::string carrier_; 245 std::string home_provider_; 246 std::string home_provider_code_; 247 std::string home_provider_country_; 248 std::string home_provider_id_; 249 std::string home_provider_name_; 250 std::string MEID_; 251 std::string IMEI_; 252 std::string IMSI_; 253 std::string ESN_; 254 std::string MDN_; 255 std::string MIN_; 256 std::string model_id_; 257 std::string manufacturer_; 258 SIMLockState sim_lock_state_; 259 int sim_retries_left_; 260 SIMPinRequire sim_pin_required_; 261 std::string firmware_revision_; 262 std::string hardware_revision_; 263 int PRL_version_; 264 std::string selected_cellular_network_; 265 CellularNetworkList found_cellular_networks_; 266 bool data_roaming_allowed_; 267 bool support_network_scan_; 268 269 friend class NetworkLibraryImpl; 270 DISALLOW_COPY_AND_ASSIGN(NetworkDevice); 271 }; 272 273 // Contains data common to all network service types. 274 class Network { 275 public: 276 virtual ~Network() {} 277 278 const std::string& service_path() const { return service_path_; } 279 const std::string& name() const { return name_; } 280 const std::string& device_path() const { return device_path_; } 281 const std::string& ip_address() const { return ip_address_; } 282 ConnectionType type() const { return type_; } 283 ConnectionMode mode() const { return mode_; } 284 ConnectionState connection_state() const { return state_; } 285 bool connecting() const { return IsConnectingState(state_); } 286 bool configuring() const { return state_ == STATE_CONFIGURATION; } 287 bool connected() const { return state_ == STATE_READY; } 288 bool connecting_or_connected() const { return connecting() || connected(); } 289 bool failed() const { return state_ == STATE_FAILURE; } 290 bool failed_or_disconnected() const { 291 return failed() || state_ == STATE_IDLE; 292 } 293 ConnectionError error() const { return error_; } 294 ConnectionState state() const { return state_; } 295 // Is this network connectable. Currently, this is mainly used by 802.1x 296 // networks to specify that the network is not configured yet. 297 bool connectable() const { return connectable_; } 298 // Is this the active network, i.e, the one through which 299 // network traffic is being routed? A network can be connected, 300 // but not be carrying traffic. 301 bool is_active() const { return is_active_; } 302 bool favorite() const { return favorite_; } 303 bool auto_connect() const { return auto_connect_; } 304 ConnectivityState connectivity_state() const { return connectivity_state_; } 305 bool added() const { return added_; } 306 307 const std::string& unique_id() const { return unique_id_; } 308 309 // We don't have a setter for |favorite_| because to unfavorite a network is 310 // equivalent to forget a network, so we call forget network on cros for 311 // that. See ForgetWifiNetwork(). 312 void SetAutoConnect(bool auto_connect); 313 314 // Sets network name. 315 void SetName(const std::string& name); 316 317 // Return a string representation of the state code. 318 std::string GetStateString() const; 319 320 // Return a string representation of the error code. 321 std::string GetErrorString() const; 322 323 static bool IsConnectingState(ConnectionState state) { 324 return (state == STATE_ASSOCIATION || 325 state == STATE_CONFIGURATION || 326 state == STATE_CARRIER); 327 } 328 329 protected: 330 Network(const std::string& service_path, ConnectionType type) 331 : state_(STATE_UNKNOWN), 332 error_(ERROR_NO_ERROR), 333 connectable_(true), 334 is_active_(false), 335 favorite_(false), 336 auto_connect_(false), 337 connectivity_state_(CONN_STATE_UNKNOWN), 338 priority_order_(0), 339 added_(false), 340 service_path_(service_path), 341 type_(type) {} 342 343 // Parse name/value pairs from libcros. 344 virtual bool ParseValue(int index, const Value* value); 345 virtual void ParseInfo(const DictionaryValue* info); 346 347 // Methods to asynchronously set network service properties 348 virtual void SetStringProperty(const char* prop, const std::string& str, 349 std::string* dest); 350 virtual void SetBooleanProperty(const char* prop, bool b, bool* dest); 351 virtual void SetIntegerProperty(const char* prop, int i, int* dest); 352 virtual void SetValueProperty(const char* prop, Value* val); 353 virtual void ClearProperty(const char* prop); 354 355 // This will clear the property if string is empty. Otherwise, it will set it. 356 virtual void SetOrClearStringProperty(const char* prop, 357 const std::string& str, 358 std::string* dest); 359 360 std::string device_path_; 361 std::string name_; 362 std::string ip_address_; 363 ConnectionMode mode_; 364 ConnectionState state_; 365 ConnectionError error_; 366 bool connectable_; 367 bool is_active_; 368 bool favorite_; 369 bool auto_connect_; 370 ConnectivityState connectivity_state_; 371 372 // Unique identifier, set the first time the network is parsed. 373 std::string unique_id_; 374 375 private: 376 void set_name(const std::string& name) { name_ = name; } 377 void set_connecting(bool connecting) { 378 state_ = (connecting ? STATE_ASSOCIATION : STATE_IDLE); 379 } 380 void set_connected(bool connected) { 381 state_ = (connected ? STATE_READY : STATE_IDLE); 382 } 383 void set_state(ConnectionState state) { state_ = state; } 384 void set_connectable(bool connectable) { connectable_ = connectable; } 385 void set_active(bool is_active) { is_active_ = is_active; } 386 void set_error(ConnectionError error) { error_ = error; } 387 void set_connectivity_state(ConnectivityState connectivity_state) { 388 connectivity_state_ = connectivity_state; 389 } 390 void set_added(bool added) { added_ = added; } 391 392 // Initialize the IP address field 393 void InitIPAddress(); 394 395 // Priority value, corresponds to index in list from flimflam (0 = first) 396 int priority_order_; 397 398 // Set to true if the UI requested this as a new network. 399 bool added_; 400 401 // These must not be modified after construction. 402 std::string service_path_; 403 ConnectionType type_; 404 405 friend class NetworkLibraryImpl; 406 friend class NetworkLibraryStubImpl; 407 DISALLOW_COPY_AND_ASSIGN(Network); 408 // ChangeAutoConnectSaveTest accesses |favorite_|. 409 FRIEND_TEST_ALL_PREFIXES(WifiConfigViewTest, ChangeAutoConnectSaveTest); 410 }; 411 412 // Class for networks of TYPE_ETHERNET. 413 class EthernetNetwork : public Network { 414 public: 415 explicit EthernetNetwork(const std::string& service_path) : 416 Network(service_path, TYPE_ETHERNET) { 417 } 418 private: 419 friend class NetworkLibraryImpl; 420 DISALLOW_COPY_AND_ASSIGN(EthernetNetwork); 421 }; 422 423 // Class for networks of TYPE_VPN. 424 class VirtualNetwork : public Network { 425 public: 426 enum ProviderType { 427 PROVIDER_TYPE_L2TP_IPSEC_PSK, 428 PROVIDER_TYPE_L2TP_IPSEC_USER_CERT, 429 PROVIDER_TYPE_OPEN_VPN, 430 // Add new provider types before PROVIDER_TYPE_MAX. 431 PROVIDER_TYPE_MAX, 432 }; 433 434 explicit VirtualNetwork(const std::string& service_path) : 435 Network(service_path, TYPE_VPN), 436 provider_type_(PROVIDER_TYPE_L2TP_IPSEC_PSK) { 437 } 438 439 const std::string& server_hostname() const { return server_hostname_; } 440 ProviderType provider_type() const { return provider_type_; } 441 const std::string& ca_cert() const { return ca_cert_; } 442 const std::string& psk_passphrase() const { return psk_passphrase_; } 443 const std::string& user_cert() const { return user_cert_; } 444 const std::string& user_cert_key() const { return user_cert_key_; } 445 const std::string& username() const { return username_; } 446 const std::string& user_passphrase() const { return user_passphrase_; } 447 448 bool NeedMoreInfoToConnect() const; 449 450 // Public setters. 451 void SetCACert(const std::string& ca_cert); 452 void SetPSKPassphrase(const std::string& psk_passphrase); 453 void SetUserCert(const std::string& user_cert); 454 void SetUserCertKey(const std::string& key); 455 void SetUsername(const std::string& username); 456 void SetUserPassphrase(const std::string& user_passphrase); 457 458 std::string GetProviderTypeString() const; 459 460 private: 461 // Network overrides. 462 virtual bool ParseValue(int index, const Value* value); 463 virtual void ParseInfo(const DictionaryValue* info); 464 465 // VirtualNetwork private methods. 466 bool ParseProviderValue(int index, const Value* value); 467 468 void set_server_hostname(const std::string& server_hostname) { 469 server_hostname_ = server_hostname; 470 } 471 void set_provider_type(ProviderType provider_type) { 472 provider_type_ = provider_type; 473 } 474 void set_ca_cert(const std::string& ca_cert) { 475 ca_cert_ = ca_cert; 476 } 477 void set_psk_passphrase(const std::string& psk_passphrase) { 478 psk_passphrase_ = psk_passphrase; 479 } 480 void set_user_cert(const std::string& user_cert) { 481 user_cert_ = user_cert; 482 } 483 void set_user_cert_key(const std::string& key) { 484 user_cert_key_ = key; 485 } 486 void set_username(const std::string& username) { 487 username_ = username; 488 } 489 void set_user_passphrase(const std::string& user_passphrase) { 490 user_passphrase_ = user_passphrase; 491 } 492 493 std::string server_hostname_; 494 ProviderType provider_type_; 495 std::string ca_cert_; 496 std::string psk_passphrase_; 497 std::string user_cert_; 498 std::string user_cert_key_; 499 std::string username_; 500 std::string user_passphrase_; 501 502 friend class NetworkLibraryImpl; 503 DISALLOW_COPY_AND_ASSIGN(VirtualNetwork); 504 }; 505 typedef std::vector<VirtualNetwork*> VirtualNetworkVector; 506 507 // Base class for networks of TYPE_WIFI or TYPE_CELLULAR. 508 class WirelessNetwork : public Network { 509 public: 510 int strength() const { return strength_; } 511 512 protected: 513 WirelessNetwork(const std::string& service_path, ConnectionType type) 514 : Network(service_path, type), 515 strength_(0) {} 516 int strength_; 517 518 // Network overrides. 519 virtual bool ParseValue(int index, const Value* value); 520 521 private: 522 void set_strength(int strength) { strength_ = strength; } 523 524 friend class NetworkLibraryImpl; 525 friend class NetworkLibraryStubImpl; 526 DISALLOW_COPY_AND_ASSIGN(WirelessNetwork); 527 }; 528 529 // Class for networks of TYPE_CELLULAR. 530 class CellularDataPlan; 531 532 class CellularNetwork : public WirelessNetwork { 533 public: 534 enum DataLeft { 535 DATA_UNKNOWN, 536 DATA_NORMAL, 537 DATA_LOW, 538 DATA_VERY_LOW, 539 DATA_NONE 540 }; 541 542 struct Apn { 543 std::string apn; 544 std::string network_id; 545 std::string username; 546 std::string password; 547 548 Apn() {} 549 Apn(const std::string& apn, const std::string& network_id, 550 const std::string& username, const std::string& password) 551 : apn(apn), network_id(network_id), 552 username(username), password(password) { 553 } 554 void Set(const DictionaryValue& dict); 555 }; 556 557 virtual ~CellularNetwork(); 558 559 explicit CellularNetwork(const std::string& service_path) 560 : WirelessNetwork(service_path, TYPE_CELLULAR), 561 activation_state_(ACTIVATION_STATE_UNKNOWN), 562 network_technology_(NETWORK_TECHNOLOGY_UNKNOWN), 563 roaming_state_(ROAMING_STATE_UNKNOWN), 564 data_left_(DATA_UNKNOWN) { 565 } 566 // Starts device activation process. Returns false if the device state does 567 // not permit activation. 568 bool StartActivation() const; 569 // Requests data plans if the network is conencted and activated. 570 // Plan data will be passed through Network::Observer::CellularDataPlanChanged 571 // callback. 572 void RefreshDataPlansIfNeeded() const; 573 574 const ActivationState activation_state() const { return activation_state_; } 575 bool activated() const { 576 return activation_state() == ACTIVATION_STATE_ACTIVATED; 577 } 578 const NetworkTechnology network_technology() const { 579 return network_technology_; 580 } 581 const NetworkRoamingState roaming_state() const { return roaming_state_; } 582 bool restricted_pool() const { 583 return connectivity_state() == CONN_STATE_RESTRICTED; 584 } 585 bool needs_new_plan() const { 586 return restricted_pool() && connected() && activated(); 587 } 588 const std::string& operator_name() const { return operator_name_; } 589 const std::string& operator_code() const { return operator_code_; } 590 const std::string& operator_country() const { return operator_country_; } 591 const std::string& payment_url() const { return payment_url_; } 592 const std::string& usage_url() const { return usage_url_; } 593 DataLeft data_left() const { return data_left_; } 594 const Apn& apn() const { return apn_; } 595 const Apn& last_good_apn() const { return last_good_apn_; } 596 void SetApn(const Apn& apn); 597 bool SupportsDataPlan() const; 598 599 // Misc. 600 bool is_gsm() const { 601 return network_technology_ != NETWORK_TECHNOLOGY_EVDO && 602 network_technology_ != NETWORK_TECHNOLOGY_1XRTT && 603 network_technology_ != NETWORK_TECHNOLOGY_UNKNOWN; 604 } 605 606 // Return a string representation of network technology. 607 std::string GetNetworkTechnologyString() const; 608 // Return a string representation of connectivity state. 609 std::string GetConnectivityStateString() const; 610 // Return a string representation of activation state. 611 std::string GetActivationStateString() const; 612 // Return a string representation of roaming state. 613 std::string GetRoamingStateString() const; 614 615 // Return a string representation of |activation_state|. 616 static std::string ActivationStateToString(ActivationState activation_state); 617 618 protected: 619 // WirelessNetwork overrides. 620 virtual bool ParseValue(int index, const Value* value); 621 622 ActivationState activation_state_; 623 NetworkTechnology network_technology_; 624 NetworkRoamingState roaming_state_; 625 // Carrier Info 626 std::string operator_name_; 627 std::string operator_code_; 628 std::string operator_country_; 629 std::string payment_url_; 630 std::string usage_url_; 631 // Cached values 632 DataLeft data_left_; // Updated when data plans are updated. 633 Apn apn_; 634 Apn last_good_apn_; 635 636 private: 637 void set_activation_state(ActivationState state) { 638 activation_state_ = state; 639 } 640 void set_payment_url(const std::string& url) { payment_url_ = url; } 641 void set_usage_url(const std::string& url) { usage_url_ = url; } 642 void set_network_technology(NetworkTechnology technology) { 643 network_technology_ = technology; 644 } 645 void set_roaming_state(NetworkRoamingState state) { roaming_state_ = state; } 646 void set_data_left(DataLeft data_left) { data_left_ = data_left; } 647 void set_apn(const Apn& apn) { apn_ = apn; } 648 void set_last_good_apn(const Apn& apn) { last_good_apn_ = apn; } 649 650 friend class NetworkLibraryImpl; 651 friend class NetworkLibraryStubImpl; 652 DISALLOW_COPY_AND_ASSIGN(CellularNetwork); 653 }; 654 typedef std::vector<CellularNetwork*> CellularNetworkVector; 655 656 // Class for networks of TYPE_WIFI. 657 class WifiNetwork : public WirelessNetwork { 658 public: 659 explicit WifiNetwork(const std::string& service_path) 660 : WirelessNetwork(service_path, TYPE_WIFI), 661 encryption_(SECURITY_NONE), 662 passphrase_required_(false), 663 eap_method_(EAP_METHOD_UNKNOWN), 664 eap_phase_2_auth_(EAP_PHASE_2_AUTH_AUTO), 665 eap_use_system_cas_(true), 666 save_credentials_(false) { 667 } 668 669 bool encrypted() const { return encryption_ != SECURITY_NONE; } 670 ConnectionSecurity encryption() const { return encryption_; } 671 const std::string& passphrase() const { return passphrase_; } 672 const std::string& identity() const { return identity_; } 673 const std::string& cert_path() const { return cert_path_; } 674 bool passphrase_required() const { return passphrase_required_; } 675 676 EAPMethod eap_method() const { return eap_method_; } 677 EAPPhase2Auth eap_phase_2_auth() const { return eap_phase_2_auth_; } 678 const std::string& eap_server_ca_cert_nss_nickname() const { 679 return eap_server_ca_cert_nss_nickname_; } 680 const std::string& eap_client_cert_pkcs11_id() const { 681 return eap_client_cert_pkcs11_id_; } 682 const bool eap_use_system_cas() const { return eap_use_system_cas_; } 683 const std::string& eap_identity() const { return eap_identity_; } 684 const std::string& eap_anonymous_identity() const { 685 return eap_anonymous_identity_; } 686 const std::string& eap_passphrase() const { return eap_passphrase_; } 687 bool save_credentials() const { return save_credentials_; } 688 689 const std::string& GetPassphrase() const; 690 691 bool SetSsid(const std::string& ssid); 692 bool SetHexSsid(const std::string& ssid_hex); 693 void SetPassphrase(const std::string& passphrase); 694 void SetIdentity(const std::string& identity); 695 void SetCertPath(const std::string& cert_path); 696 697 // 802.1x properties 698 void SetEAPMethod(EAPMethod method); 699 void SetEAPPhase2Auth(EAPPhase2Auth auth); 700 void SetEAPServerCaCertNssNickname(const std::string& nss_nickname); 701 void SetEAPClientCertPkcs11Id(const std::string& pkcs11_id); 702 void SetEAPUseSystemCAs(bool use_system_cas); 703 void SetEAPIdentity(const std::string& identity); 704 void SetEAPAnonymousIdentity(const std::string& identity); 705 void SetEAPPassphrase(const std::string& passphrase); 706 void SetSaveCredentials(bool save_credentials); 707 708 // Erase cached credentials, used when "Save password" is unchecked. 709 void EraseCredentials(); 710 711 // Return a string representation of the encryption code. 712 // This not translated and should be only used for debugging purposes. 713 std::string GetEncryptionString() const; 714 715 // Return true if a passphrase or other input is required to connect. 716 bool IsPassphraseRequired() const; 717 718 // Return true if cert_path_ indicates that we have loaded the certificate. 719 bool IsCertificateLoaded() const; 720 721 private: 722 // WirelessNetwork overrides. 723 virtual bool ParseValue(int index, const Value* value); 724 virtual void ParseInfo(const DictionaryValue* info); 725 726 void CalculateUniqueId(); 727 728 void set_encryption(ConnectionSecurity encryption) { 729 encryption_ = encryption; 730 } 731 void set_passphrase(const std::string& passphrase) { 732 passphrase_ = passphrase; 733 } 734 void set_passphrase_required(bool passphrase_required) { 735 passphrase_required_ = passphrase_required; 736 } 737 void set_identity(const std::string& identity) { 738 identity_ = identity; 739 } 740 void set_cert_path(const std::string& cert_path) { 741 cert_path_ = cert_path; 742 } 743 744 ConnectionSecurity encryption_; 745 std::string passphrase_; 746 bool passphrase_required_; 747 std::string identity_; 748 std::string cert_path_; 749 750 EAPMethod eap_method_; 751 EAPPhase2Auth eap_phase_2_auth_; 752 std::string eap_server_ca_cert_nss_nickname_; 753 std::string eap_client_cert_pkcs11_id_; 754 bool eap_use_system_cas_; 755 std::string eap_identity_; 756 std::string eap_anonymous_identity_; 757 std::string eap_passphrase_; 758 // Tells flimflam to save passphrase and EAP credentials to disk. 759 bool save_credentials_; 760 761 // Internal state (not stored in flimflam). 762 // Passphrase set by user (stored for UI). 763 std::string user_passphrase_; 764 765 friend class NetworkLibraryImpl; 766 DISALLOW_COPY_AND_ASSIGN(WifiNetwork); 767 }; 768 typedef std::vector<WifiNetwork*> WifiNetworkVector; 769 770 // Cellular Data Plan management. 771 class CellularDataPlan { 772 public: 773 CellularDataPlan() 774 : plan_name("Unknown"), 775 plan_type(CELLULAR_DATA_PLAN_UNLIMITED), 776 plan_data_bytes(0), 777 data_bytes_used(0) { } 778 explicit CellularDataPlan(const CellularDataPlanInfo &plan) 779 : plan_name(plan.plan_name ? plan.plan_name : ""), 780 plan_type(plan.plan_type), 781 update_time(base::Time::FromInternalValue(plan.update_time)), 782 plan_start_time(base::Time::FromInternalValue(plan.plan_start_time)), 783 plan_end_time(base::Time::FromInternalValue(plan.plan_end_time)), 784 plan_data_bytes(plan.plan_data_bytes), 785 data_bytes_used(plan.data_bytes_used) { } 786 // Formats cellular plan description. 787 string16 GetPlanDesciption() const; 788 // Evaluates cellular plans status and returns warning string if it is near 789 // expiration. 790 string16 GetRemainingWarning() const; 791 // Formats remaining plan data description. 792 string16 GetDataRemainingDesciption() const; 793 // Formats plan expiration description. 794 string16 GetPlanExpiration() const; 795 // Formats plan usage info. 796 string16 GetUsageInfo() const; 797 // Returns a unique string for this plan that can be used for comparisons. 798 std::string GetUniqueIdentifier() const; 799 base::TimeDelta remaining_time() const; 800 int64 remaining_minutes() const; 801 // Returns plan data remaining in bytes. 802 int64 remaining_data() const; 803 // TODO(stevenjb): Make these private with accessors and properly named. 804 std::string plan_name; 805 CellularDataPlanType plan_type; 806 base::Time update_time; 807 base::Time plan_start_time; 808 base::Time plan_end_time; 809 int64 plan_data_bytes; 810 int64 data_bytes_used; 811 }; 812 typedef ScopedVector<CellularDataPlan> CellularDataPlanVector; 813 814 // Geolocation data. 815 struct CellTower { 816 enum RadioType { 817 RADIOTYPE_GSM, 818 RADIOTYPE_CDMA, 819 RADIOTYPE_WCDMA, 820 } radio_type; // GSM/WCDMA CDMA 821 int mobile_country_code; // MCC MCC 822 int mobile_network_code; // MNC SID 823 int location_area_code; // LAC NID 824 int cell_id; // CID BID 825 base::Time timestamp; // Timestamp when this cell was primary 826 int signal_strength; // Radio signal strength measured in dBm. 827 int timing_advance; // Represents the distance from the cell tower. 828 // Each unit is roughly 550 meters. 829 }; 830 831 struct WifiAccessPoint { 832 std::string mac_address; // The mac address of the WiFi node. 833 std::string name; // The SSID of the WiFi node. 834 base::Time timestamp; // Timestamp when this AP was detected. 835 int signal_strength; // Radio signal strength measured in dBm. 836 int signal_to_noise; // Current signal to noise ratio measured in dB. 837 int channel; // Wifi channel number. 838 }; 839 840 typedef std::vector<CellTower> CellTowerVector; 841 typedef std::vector<WifiAccessPoint> WifiAccessPointVector; 842 843 // IP Configuration. 844 struct NetworkIPConfig { 845 NetworkIPConfig(const std::string& device_path, IPConfigType type, 846 const std::string& address, const std::string& netmask, 847 const std::string& gateway, const std::string& name_servers) 848 : device_path(device_path), 849 type(type), 850 address(address), 851 netmask(netmask), 852 gateway(gateway), 853 name_servers(name_servers) {} 854 855 // NetworkIPConfigs are sorted by tyoe. 856 bool operator< (const NetworkIPConfig& other) const { 857 return type < other.type; 858 } 859 860 std::string device_path; 861 IPConfigType type; 862 std::string address; // This looks like "/device/0011aa22bb33" 863 std::string netmask; 864 std::string gateway; 865 std::string name_servers; 866 }; 867 typedef std::vector<NetworkIPConfig> NetworkIPConfigVector; 868 869 // This class handles the interaction with the ChromeOS network library APIs. 870 // Classes can add themselves as observers. Users can get an instance of the 871 // library like this: chromeos::CrosLibrary::Get()->GetNetworkLibrary() 872 class NetworkLibrary { 873 public: 874 enum HardwareAddressFormat { 875 FORMAT_RAW_HEX, 876 FORMAT_COLON_SEPARATED_HEX 877 }; 878 879 class NetworkManagerObserver { 880 public: 881 // Called when the state of the network manager has changed, 882 // for example, networks have appeared or disappeared. 883 virtual void OnNetworkManagerChanged(NetworkLibrary* obj) = 0; 884 }; 885 886 class NetworkObserver { 887 public: 888 // Called when the state of a single network has changed, 889 // for example signal strength or connection state. 890 virtual void OnNetworkChanged(NetworkLibrary* cros, 891 const Network* network) = 0; 892 }; 893 894 class NetworkDeviceObserver { 895 public: 896 // Called when the state of a single device has changed, 897 // for example SIMLock state for cellular. 898 virtual void OnNetworkDeviceChanged(NetworkLibrary* cros, 899 const NetworkDevice* device) = 0; 900 }; 901 902 class CellularDataPlanObserver { 903 public: 904 // Called when the cellular data plan has changed. 905 virtual void OnCellularDataPlanChanged(NetworkLibrary* obj) = 0; 906 }; 907 908 class PinOperationObserver { 909 public: 910 // Called when pin async operation has completed. 911 // Network is NULL when we don't have an associated Network object. 912 virtual void OnPinOperationCompleted(NetworkLibrary* cros, 913 PinOperationError error) = 0; 914 }; 915 916 class UserActionObserver { 917 public: 918 // Called when user initiates a new connection. 919 // Network is NULL when we don't have an associated Network object. 920 virtual void OnConnectionInitiated(NetworkLibrary* cros, 921 const Network* network) = 0; 922 }; 923 924 virtual ~NetworkLibrary() {} 925 926 virtual void AddNetworkManagerObserver(NetworkManagerObserver* observer) = 0; 927 virtual void RemoveNetworkManagerObserver( 928 NetworkManagerObserver* observer) = 0; 929 930 // An attempt to add an observer that has already been added for a 931 // give service path will be ignored. 932 virtual void AddNetworkObserver(const std::string& service_path, 933 NetworkObserver* observer) = 0; 934 // Remove an observer of a single network 935 virtual void RemoveNetworkObserver(const std::string& service_path, 936 NetworkObserver* observer) = 0; 937 // Stop |observer| from observing any networks 938 virtual void RemoveObserverForAllNetworks(NetworkObserver* observer) = 0; 939 940 // Add an observer for a single network device. 941 virtual void AddNetworkDeviceObserver(const std::string& device_path, 942 NetworkDeviceObserver* observer) = 0; 943 // Remove an observer for a single network device. 944 virtual void RemoveNetworkDeviceObserver(const std::string& device_path, 945 NetworkDeviceObserver* observer) = 0; 946 947 // Temporarily locks down certain functionality in network library to prevent 948 // unplanned side effects. During the lock down, Enable*Device() calls cannot 949 // be made. 950 virtual void Lock() = 0; 951 // Removes temporarily lock of network library. 952 virtual void Unlock() = 0; 953 // Checks if access to network library is locked. 954 virtual bool IsLocked() = 0; 955 956 virtual void AddCellularDataPlanObserver( 957 CellularDataPlanObserver* observer) = 0; 958 virtual void RemoveCellularDataPlanObserver( 959 CellularDataPlanObserver* observer) = 0; 960 961 virtual void AddPinOperationObserver(PinOperationObserver* observer) = 0; 962 virtual void RemovePinOperationObserver(PinOperationObserver* observer) = 0; 963 964 virtual void AddUserActionObserver(UserActionObserver* observer) = 0; 965 virtual void RemoveUserActionObserver(UserActionObserver* observer) = 0; 966 967 // Return the active or default Ethernet network (or NULL if none). 968 virtual const EthernetNetwork* ethernet_network() const = 0; 969 virtual bool ethernet_connecting() const = 0; 970 virtual bool ethernet_connected() const = 0; 971 972 // Return the active Wifi network (or NULL if none active). 973 virtual const WifiNetwork* wifi_network() const = 0; 974 virtual bool wifi_connecting() const = 0; 975 virtual bool wifi_connected() const = 0; 976 977 // Return the active Cellular network (or NULL if none active). 978 virtual const CellularNetwork* cellular_network() const = 0; 979 virtual bool cellular_connecting() const = 0; 980 virtual bool cellular_connected() const = 0; 981 982 // Return the active virtual network (or NULL if none active). 983 virtual const VirtualNetwork* virtual_network() const = 0; 984 virtual bool virtual_network_connecting() const = 0; 985 virtual bool virtual_network_connected() const = 0; 986 987 // Return true if any network is currently connected. 988 virtual bool Connected() const = 0; 989 990 // Return true if any network is currently connecting. 991 virtual bool Connecting() const = 0; 992 993 // Returns the current IP address if connected. If not, returns empty string. 994 virtual const std::string& IPAddress() const = 0; 995 996 // Returns the current list of wifi networks. 997 virtual const WifiNetworkVector& wifi_networks() const = 0; 998 999 // Returns the list of remembered wifi networks. 1000 virtual const WifiNetworkVector& remembered_wifi_networks() const = 0; 1001 1002 // Returns the current list of cellular networks. 1003 virtual const CellularNetworkVector& cellular_networks() const = 0; 1004 1005 // Returns the current list of virtual networks. 1006 virtual const VirtualNetworkVector& virtual_networks() const = 0; 1007 1008 // Return a pointer to the device, if it exists, or NULL. 1009 virtual const NetworkDevice* FindNetworkDeviceByPath( 1010 const std::string& path) const = 0; 1011 1012 // Returns device with TYPE_CELLULAR. Returns NULL if none exists. 1013 virtual const NetworkDevice* FindCellularDevice() const = 0; 1014 1015 // Returns device with TYPE_ETHERNET. Returns NULL if none exists. 1016 virtual const NetworkDevice* FindEthernetDevice() const = 0; 1017 1018 // Returns device with TYPE_WIFI. Returns NULL if none exists. 1019 virtual const NetworkDevice* FindWifiDevice() const = 0; 1020 1021 // Return a pointer to the network, if it exists, or NULL. 1022 // NOTE: Never store these results, store service paths instead. 1023 // The pattern for doing an operation on a Network is: 1024 // Network* network = cros->FindNetworkByPath(service_path); 1025 // network->SetFoo(); 1026 // network->Connect(); 1027 // As long as this is done in sequence on the UI thread it will be safe; 1028 // the network list only gets updated on the UI thread. 1029 virtual Network* FindNetworkByPath(const std::string& path) const = 0; 1030 virtual WifiNetwork* FindWifiNetworkByPath(const std::string& path) const = 0; 1031 virtual CellularNetwork* FindCellularNetworkByPath( 1032 const std::string& path) const = 0; 1033 virtual VirtualNetwork* FindVirtualNetworkByPath( 1034 const std::string& path) const = 0; 1035 1036 // Returns the visible wifi network corresponding to the remembered 1037 // wifi network, or NULL if the remembered network is not visible. 1038 virtual Network* FindNetworkFromRemembered( 1039 const Network* remembered) const = 0; 1040 1041 // Retrieves the data plans associated with |path|, NULL if there are no 1042 // associated plans. 1043 virtual const CellularDataPlanVector* GetDataPlans( 1044 const std::string& path) const = 0; 1045 1046 // This returns the significant data plan. If the user only has the 1047 // base data plan, then return that. If there is a base and a paid data plan, 1048 // then the significant one is the paid one. So return the paid plan. 1049 // If there are no data plans, then this method returns NULL. 1050 // This returns a pointer to a member of data_plans_, so if SetDataPlans() 1051 // gets called, the result becomes invalid. 1052 virtual const CellularDataPlan* GetSignificantDataPlan( 1053 const std::string& path) const = 0; 1054 1055 // Passes |old_pin|, |new_pin| to change SIM card PIM. 1056 virtual void ChangePin(const std::string& old_pin, 1057 const std::string& new_pin) = 0; 1058 1059 // Passes |pin|, |require_pin| value to change SIM card RequirePin setting. 1060 virtual void ChangeRequirePin(bool require_pin, 1061 const std::string& pin) = 0; 1062 1063 // Passes |pin| to unlock SIM card. 1064 virtual void EnterPin(const std::string& pin) = 0; 1065 1066 // Passes |puk|, |new_pin| to unblock SIM card. 1067 virtual void UnblockPin(const std::string& puk, 1068 const std::string& new_pin) = 0; 1069 1070 // Request a scan for available cellular networks. 1071 virtual void RequestCellularScan() = 0; 1072 1073 // Request a register in cellular network with |network_id|. 1074 virtual void RequestCellularRegister(const std::string& network_id) = 0; 1075 1076 // Change data roaming restriction for current cellular device. 1077 virtual void SetCellularDataRoamingAllowed(bool new_value) = 0; 1078 1079 // Request a scan for new wifi networks. 1080 virtual void RequestNetworkScan() = 0; 1081 1082 // Reads out the results of the last wifi scan. These results are not 1083 // pre-cached in the library, so the call may block whilst the results are 1084 // read over IPC. 1085 // Returns false if an error occurred in reading the results. Note that 1086 // a true return code only indicates the result set was successfully read, 1087 // it does not imply a scan has successfully completed yet. 1088 virtual bool GetWifiAccessPoints(WifiAccessPointVector* result) = 0; 1089 1090 // TODO(joth): Add GetCellTowers to retrieve a CellTowerVector. 1091 1092 // Connect to the specified wireless network. 1093 virtual void ConnectToWifiNetwork(WifiNetwork* network) = 0; 1094 1095 // Same as above but searches for an existing network by name. 1096 virtual void ConnectToWifiNetwork(const std::string& service_path) = 0; 1097 1098 // Connect to the specified network with security, ssid, passphrase, identity, 1099 // and (optionally) certpath. 1100 virtual void ConnectToWifiNetwork(ConnectionSecurity security, 1101 const std::string& ssid, 1102 const std::string& passphrase, 1103 const std::string& identity, 1104 const std::string& certpath) = 0; 1105 1106 // Connect to the specified cellular network. 1107 virtual void ConnectToCellularNetwork(CellularNetwork* network) = 0; 1108 1109 // Records information that cellular play payment had happened. 1110 virtual void SignalCellularPlanPayment() = 0; 1111 1112 // Returns true if cellular plan payment had been recorded recently. 1113 virtual bool HasRecentCellularPlanPayment() = 0; 1114 1115 // Connect to the specified virtual network. 1116 virtual void ConnectToVirtualNetwork(VirtualNetwork* network) = 0; 1117 1118 // Connect to the specified virtual network with service name, 1119 // server hostname, provider_type, PSK passphrase, username and passphrase. 1120 virtual void ConnectToVirtualNetworkPSK( 1121 const std::string& service_name, 1122 const std::string& server_hostname, 1123 const std::string& psk, 1124 const std::string& username, 1125 const std::string& user_passphrase) = 0; 1126 1127 // Disconnect from the specified network. 1128 virtual void DisconnectFromNetwork(const Network* network) = 0; 1129 1130 // Forget the wifi network corresponding to service_path. 1131 virtual void ForgetWifiNetwork(const std::string& service_path) = 0; 1132 1133 // Returns home carrier ID if available, otherwise empty string is returned. 1134 // Carrier ID format: <carrier name> (country). Ex.: "Verizon (us)". 1135 virtual std::string GetCellularHomeCarrierId() const = 0; 1136 1137 virtual bool ethernet_available() const = 0; 1138 virtual bool wifi_available() const = 0; 1139 virtual bool cellular_available() const = 0; 1140 1141 virtual bool ethernet_enabled() const = 0; 1142 virtual bool wifi_enabled() const = 0; 1143 virtual bool cellular_enabled() const = 0; 1144 1145 virtual bool wifi_scanning() const = 0; 1146 1147 virtual const Network* active_network() const = 0; 1148 virtual const Network* connected_network() const = 0; 1149 1150 virtual bool offline_mode() const = 0; 1151 1152 // Enables/disables the ethernet network device. 1153 virtual void EnableEthernetNetworkDevice(bool enable) = 0; 1154 1155 // Enables/disables the wifi network device. 1156 virtual void EnableWifiNetworkDevice(bool enable) = 0; 1157 1158 // Enables/disables the cellular network device. 1159 virtual void EnableCellularNetworkDevice(bool enable) = 0; 1160 1161 // Enables/disables offline mode. 1162 virtual void EnableOfflineMode(bool enable) = 0; 1163 1164 // Fetches IP configs and hardware address for a given device_path. 1165 // The hardware address is usually a MAC address like "0011AA22BB33". 1166 // |hardware_address| will be an empty string, if no hardware address is 1167 // found. 1168 virtual NetworkIPConfigVector GetIPConfigs( 1169 const std::string& device_path, 1170 std::string* hardware_address, 1171 HardwareAddressFormat) = 0; 1172 1173 // Factory function, creates a new instance and returns ownership. 1174 // For normal usage, access the singleton via CrosLibrary::Get(). 1175 static NetworkLibrary* GetImpl(bool stub); 1176 }; 1177 1178 } // namespace chromeos 1179 1180 #endif // CHROME_BROWSER_CHROMEOS_CROS_NETWORK_LIBRARY_H_ 1181