| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2009 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 #include "chrome/browser/chromeos/cros_network_library.h" |
| 6 |
| 7 #include <algorithm> |
| 8 |
| 9 #include "base/string_util.h" |
| 10 #include "chrome/browser/chromeos/cros_library.h" |
| 11 |
| 12 CrosNetworkLibrary::CrosNetworkLibrary() |
| 13 : ethernet_connected_(false), |
| 14 wifi_connecting_(false), |
| 15 wifi_strength_(0) { |
| 16 if (CrosLibrary::loaded()) { |
| 17 InitNetworkStatus(); |
| 18 chromeos::MonitorNetworkStatus(&NetworkStatusChangedHandler, this); |
| 19 } |
| 20 } |
| 21 |
| 22 // static |
| 23 CrosNetworkLibrary* CrosNetworkLibrary::Get() { |
| 24 return Singleton<CrosNetworkLibrary>::get(); |
| 25 } |
| 26 |
| 27 // static |
| 28 bool CrosNetworkLibrary::loaded() { |
| 29 return CrosLibrary::loaded(); |
| 30 } |
| 31 |
| 32 void CrosNetworkLibrary::AddObserver(Observer* observer) { |
| 33 observers_.AddObserver(observer); |
| 34 } |
| 35 |
| 36 void CrosNetworkLibrary::RemoveObserver(Observer* observer) { |
| 37 observers_.RemoveObserver(observer); |
| 38 } |
| 39 |
| 40 WifiNetworkVector CrosNetworkLibrary::GetWifiNetworks() { |
| 41 WifiNetworkVector networks; |
| 42 if (CrosLibrary::loaded()) { |
| 43 chromeos::ServiceStatus* service_status = chromeos::GetAvailableNetworks(); |
| 44 for (int i = 0; i < service_status->size; i++) { |
| 45 const chromeos::ServiceInfo& service = service_status->services[i]; |
| 46 if (service.type == chromeos::TYPE_WIFI) { |
| 47 // Found a wifi network, add it to the list. |
| 48 networks.push_back(WifiNetwork(service.ssid, service.needs_passphrase, |
| 49 service.encryption, |
| 50 service.signal_strength)); |
| 51 } |
| 52 } |
| 53 chromeos::FreeServiceStatus(service_status); |
| 54 } |
| 55 |
| 56 // Sort the list of wifi networks by ssid. |
| 57 std::sort(networks.begin(), networks.end()); |
| 58 |
| 59 return networks; |
| 60 } |
| 61 |
| 62 static const char* GetEncryptionString(chromeos::EncryptionType encryption) { |
| 63 switch (encryption) { |
| 64 case chromeos::NONE: |
| 65 return "none"; |
| 66 case chromeos::RSN: |
| 67 return "rsn"; |
| 68 case chromeos::WEP: |
| 69 return "wep"; |
| 70 case chromeos::WPA: |
| 71 return "wpa"; |
| 72 } |
| 73 return "none"; |
| 74 } |
| 75 |
| 76 void CrosNetworkLibrary::ConnectToWifiNetwork(WifiNetwork network, |
| 77 const string16& password) { |
| 78 bool ok = true; |
| 79 if (CrosLibrary::loaded()) { |
| 80 ok = chromeos::ConnectToWifiNetwork(network.ssid.c_str(), |
| 81 password.empty() ? NULL : UTF16ToUTF8(password).c_str(), |
| 82 GetEncryptionString(network.encryption)); |
| 83 } |
| 84 if (ok) { |
| 85 // Notify all observers that connection has started. |
| 86 wifi_ssid_ = network.ssid; |
| 87 wifi_connecting_ = true; |
| 88 wifi_strength_ = network.strength; |
| 89 FOR_EACH_OBSERVER(Observer, observers_, NetworkChanged(this)); |
| 90 } |
| 91 } |
| 92 |
| 93 // static |
| 94 void CrosNetworkLibrary::NetworkStatusChangedHandler(void* object, |
| 95 const chromeos::ServiceInfo& service) { |
| 96 CrosNetworkLibrary* network = static_cast<CrosNetworkLibrary*>(object); |
| 97 network->ParseNetworkServiceInfo(service); |
| 98 FOR_EACH_OBSERVER(Observer, network->observers_, NetworkChanged(network)); |
| 99 } |
| 100 |
| 101 void CrosNetworkLibrary::ParseNetworkServiceInfo( |
| 102 const chromeos::ServiceInfo& service) { |
| 103 DLOG(INFO) << "Parse " << service.ssid << |
| 104 " typ=" << service.type << |
| 105 " sta=" << service.state << |
| 106 " pas=" << service.needs_passphrase << |
| 107 " enc=" << service.encryption << |
| 108 " sig=" << service.signal_strength; |
| 109 if (service.type == chromeos::TYPE_ETHERNET) { |
| 110 // Get the ethernet status. |
| 111 ethernet_connected_ = service.state == chromeos::STATE_READY; |
| 112 } else if (service.type == chromeos::TYPE_WIFI) { |
| 113 if (service.state == chromeos::STATE_READY) { |
| 114 // Record the wifi network that is connected. |
| 115 wifi_ssid_ = service.ssid; |
| 116 wifi_connecting_ = false; |
| 117 wifi_strength_ = service.signal_strength; |
| 118 } else if (service.state == chromeos::STATE_ASSOCIATION || |
| 119 service.state == chromeos::STATE_CONFIGURATION) { |
| 120 // Record the wifi network that is connecting. |
| 121 wifi_ssid_ = service.ssid; |
| 122 wifi_connecting_ = true; |
| 123 wifi_strength_ = service.signal_strength; |
| 124 } else { |
| 125 // A wifi network is disconnected. |
| 126 // If it is the current connected (or connecting) wifi network, then |
| 127 // we are currently disconnected from any wifi network. |
| 128 if (service.ssid == wifi_ssid_) { |
| 129 wifi_ssid_.clear(); |
| 130 wifi_connecting_ = false; |
| 131 wifi_strength_ = 0; |
| 132 } |
| 133 } |
| 134 } |
| 135 } |
| 136 |
| 137 void CrosNetworkLibrary::InitNetworkStatus() { |
| 138 chromeos::ServiceStatus* service_status = chromeos::GetAvailableNetworks(); |
| 139 for (int i = 0; i < service_status->size; i++) |
| 140 ParseNetworkServiceInfo(service_status->services[i]); |
| 141 chromeos::FreeServiceStatus(service_status); |
| 142 } |
| OLD | NEW |