| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/cros_network_library.h" | 5 #include "chrome/browser/chromeos/cros_network_library.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include "chrome/browser/chrome_thread.h" | 11 #include "chrome/browser/chrome_thread.h" |
| 12 #include "chrome/browser/chromeos/cros_library.h" | 12 #include "chrome/browser/chromeos/cros_library.h" |
| 13 | 13 |
| 14 // Allows InvokeLater without adding refcounting. This class is a Singleton and | 14 // Allows InvokeLater without adding refcounting. This class is a Singleton and |
| 15 // won't be deleted until it's last InvokeLater is run. | 15 // won't be deleted until it's last InvokeLater is run. |
| 16 template <> | 16 template <> |
| 17 struct RunnableMethodTraits<CrosNetworkLibrary> { | 17 struct RunnableMethodTraits<CrosNetworkLibrary> { |
| 18 void RetainCallee(CrosNetworkLibrary* obj) {} | 18 void RetainCallee(CrosNetworkLibrary* obj) {} |
| 19 void ReleaseCallee(CrosNetworkLibrary* obj) {} | 19 void ReleaseCallee(CrosNetworkLibrary* obj) {} |
| 20 }; | 20 }; |
| 21 | 21 |
| 22 CrosNetworkLibrary::CrosNetworkLibrary() { | 22 CrosNetworkLibrary::CrosNetworkLibrary() { |
| 23 if (CrosLibrary::loaded()) { | 23 if (CrosLibrary::loaded()) { |
| 24 MessageLoop* loop = ChromeThread::GetMessageLoop(ChromeThread::FILE); | 24 Init(); |
| 25 if (loop) { | |
| 26 loop->PostTask(FROM_HERE, NewRunnableMethod(this, | |
| 27 &CrosNetworkLibrary::InitOnBackgroundThread)); | |
| 28 } | |
| 29 } | 25 } |
| 30 } | 26 } |
| 31 | 27 |
| 32 CrosNetworkLibrary::~CrosNetworkLibrary() { | 28 CrosNetworkLibrary::~CrosNetworkLibrary() { |
| 33 if (CrosLibrary::loaded()) { | 29 if (CrosLibrary::loaded()) { |
| 34 // FILE thread is already gone by the time we get to this destructor. | |
| 35 // So it's ok to just make the disconnect call on the main thread. | |
| 36 chromeos::DisconnectNetworkStatus(network_status_connection_); | 30 chromeos::DisconnectNetworkStatus(network_status_connection_); |
| 37 } | 31 } |
| 38 } | 32 } |
| 39 | 33 |
| 40 // static | 34 // static |
| 41 CrosNetworkLibrary* CrosNetworkLibrary::Get() { | 35 CrosNetworkLibrary* CrosNetworkLibrary::Get() { |
| 42 return Singleton<CrosNetworkLibrary>::get(); | 36 return Singleton<CrosNetworkLibrary>::get(); |
| 43 } | 37 } |
| 44 | 38 |
| 45 // static | 39 // static |
| (...skipping 19 matching lines...) Expand all Loading... |
| 65 return "wep"; | 59 return "wep"; |
| 66 case chromeos::WPA: | 60 case chromeos::WPA: |
| 67 return "wpa"; | 61 return "wpa"; |
| 68 } | 62 } |
| 69 return "none"; | 63 return "none"; |
| 70 } | 64 } |
| 71 | 65 |
| 72 void CrosNetworkLibrary::ConnectToWifiNetwork(WifiNetwork network, | 66 void CrosNetworkLibrary::ConnectToWifiNetwork(WifiNetwork network, |
| 73 const string16& password) { | 67 const string16& password) { |
| 74 if (CrosLibrary::loaded()) { | 68 if (CrosLibrary::loaded()) { |
| 75 MessageLoop* loop = ChromeThread::GetMessageLoop(ChromeThread::FILE); | 69 // This call kicks off a request to connect to this network, the results of |
| 76 if (loop) | 70 // which we'll hear about through the monitoring we've set up in Init(); |
| 77 loop->PostTask(FROM_HERE, NewRunnableFunction( | 71 chromeos::ConnectToWifiNetwork( |
| 78 &chromeos::ConnectToWifiNetwork, | 72 network.ssid.c_str(), |
| 79 network.ssid.c_str(), | 73 password.empty() ? NULL : UTF16ToUTF8(password).c_str(), |
| 80 password.empty() ? NULL : UTF16ToUTF8(password).c_str(), | 74 GetEncryptionString(network.encryption)); |
| 81 GetEncryptionString(network.encryption))); | |
| 82 } | 75 } |
| 83 } | 76 } |
| 84 | 77 |
| 85 // static | 78 // static |
| 86 void CrosNetworkLibrary::NetworkStatusChangedHandler(void* object, | 79 void CrosNetworkLibrary::NetworkStatusChangedHandler(void* object, |
| 87 const chromeos::ServiceStatus& service_status) { | 80 const chromeos::ServiceStatus& service_status) { |
| 88 CrosNetworkLibrary* network = static_cast<CrosNetworkLibrary*>(object); | 81 CrosNetworkLibrary* network = static_cast<CrosNetworkLibrary*>(object); |
| 89 WifiNetworkVector networks; | 82 WifiNetworkVector networks; |
| 90 bool ethernet_connected; | 83 bool ethernet_connected; |
| 91 ParseNetworks(service_status, &networks, ðernet_connected); | 84 ParseNetworks(service_status, &networks, ðernet_connected); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 115 networks->push_back(WifiNetwork(service.ssid, | 108 networks->push_back(WifiNetwork(service.ssid, |
| 116 service.needs_passphrase, | 109 service.needs_passphrase, |
| 117 service.encryption, | 110 service.encryption, |
| 118 service.signal_strength, | 111 service.signal_strength, |
| 119 connecting, | 112 connecting, |
| 120 connected)); | 113 connected)); |
| 121 } | 114 } |
| 122 } | 115 } |
| 123 } | 116 } |
| 124 | 117 |
| 125 void CrosNetworkLibrary::InitOnBackgroundThread() { | 118 void CrosNetworkLibrary::Init() { |
| 119 // First, get the currently available networks. This data is cached |
| 120 // on the connman side, so the call should be quick. |
| 126 chromeos::ServiceStatus* service_status = chromeos::GetAvailableNetworks(); | 121 chromeos::ServiceStatus* service_status = chromeos::GetAvailableNetworks(); |
| 127 if (service_status) { | 122 if (service_status) { |
| 123 LOG(INFO) << "Getting initial CrOS network info."; |
| 128 WifiNetworkVector networks; | 124 WifiNetworkVector networks; |
| 129 bool ethernet_connected; | 125 bool ethernet_connected; |
| 130 ParseNetworks(*service_status, &networks, ðernet_connected); | 126 ParseNetworks(*service_status, &networks, ðernet_connected); |
| 131 UpdateNetworkStatus(networks, ethernet_connected); | 127 UpdateNetworkStatus(networks, ethernet_connected); |
| 132 chromeos::FreeServiceStatus(service_status); | 128 chromeos::FreeServiceStatus(service_status); |
| 133 } | 129 } |
| 130 LOG(INFO) << "Registering for network status updates."; |
| 131 // Now, register to receive updates on network status. |
| 134 network_status_connection_ = chromeos::MonitorNetworkStatus( | 132 network_status_connection_ = chromeos::MonitorNetworkStatus( |
| 135 &NetworkStatusChangedHandler, this); | 133 &NetworkStatusChangedHandler, this); |
| 136 } | 134 } |
| 137 | 135 |
| 138 void CrosNetworkLibrary::UpdateNetworkStatus( | 136 void CrosNetworkLibrary::UpdateNetworkStatus( |
| 139 const WifiNetworkVector& networks, bool ethernet_connected) { | 137 const WifiNetworkVector& networks, bool ethernet_connected) { |
| 140 // Make sure we run on UI thread. | 138 // Make sure we run on UI thread. |
| 141 if (!ChromeThread::CurrentlyOn(ChromeThread::UI)) { | 139 if (!ChromeThread::CurrentlyOn(ChromeThread::UI)) { |
| 142 MessageLoop* loop = ChromeThread::GetMessageLoop(ChromeThread::UI); | 140 MessageLoop* loop = ChromeThread::GetMessageLoop(ChromeThread::UI); |
| 143 if (loop) | 141 if (loop) |
| 144 loop->PostTask(FROM_HERE, NewRunnableMethod(this, | 142 loop->PostTask(FROM_HERE, NewRunnableMethod(this, |
| 145 &CrosNetworkLibrary::UpdateNetworkStatus, networks, | 143 &CrosNetworkLibrary::UpdateNetworkStatus, networks, |
| 146 ethernet_connected)); | 144 ethernet_connected)); |
| 147 return; | 145 return; |
| 148 } | 146 } |
| 149 | 147 |
| 150 ethernet_connected_ = ethernet_connected; | 148 ethernet_connected_ = ethernet_connected; |
| 151 wifi_networks_ = networks; | 149 wifi_networks_ = networks; |
| 152 // Sort the list of wifi networks by ssid. | 150 // Sort the list of wifi networks by ssid. |
| 153 std::sort(wifi_networks_.begin(), wifi_networks_.end()); | 151 std::sort(wifi_networks_.begin(), wifi_networks_.end()); |
| 154 wifi_ = WifiNetwork(); | 152 wifi_ = WifiNetwork(); |
| 155 for (size_t i = 0; i < wifi_networks_.size(); i++) { | 153 for (size_t i = 0; i < wifi_networks_.size(); i++) { |
| 156 if (wifi_networks_[i].connecting || wifi_networks_[i].connected) { | 154 if (wifi_networks_[i].connecting || wifi_networks_[i].connected) { |
| 157 wifi_ = wifi_networks_[i]; | 155 wifi_ = wifi_networks_[i]; |
| 158 break; // There is only one connected or connecting wifi network. | 156 break; // There is only one connected or connecting wifi network. |
| 159 } | 157 } |
| 160 } | 158 } |
| 161 FOR_EACH_OBSERVER(Observer, observers_, NetworkChanged(this)); | 159 FOR_EACH_OBSERVER(Observer, observers_, NetworkChanged(this)); |
| 162 } | 160 } |
| OLD | NEW |