| 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 return; |
| 25 if (loop) { | 25 |
| 26 loop->PostTask(FROM_HERE, NewRunnableMethod(this, | 26 ChromeThread::PostTask( |
| 27 &CrosNetworkLibrary::InitOnBackgroundThread)); | 27 ChromeThread::FILE, FROM_HERE, |
| 28 } | 28 NewRunnableMethod(this, &CrosNetworkLibrary::InitOnBackgroundThread)); |
| 29 } | |
| 30 } | 29 } |
| 31 | 30 |
| 32 CrosNetworkLibrary::~CrosNetworkLibrary() { | 31 CrosNetworkLibrary::~CrosNetworkLibrary() { |
| 33 if (CrosLibrary::loaded()) { | 32 if (CrosLibrary::loaded()) { |
| 34 // FILE thread is already gone by the time we get to this destructor. | 33 // 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. | 34 // So it's ok to just make the disconnect call on the main thread. |
| 36 chromeos::DisconnectNetworkStatus(network_status_connection_); | 35 chromeos::DisconnectNetworkStatus(network_status_connection_); |
| 37 } | 36 } |
| 38 } | 37 } |
| 39 | 38 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 64 case chromeos::WEP: | 63 case chromeos::WEP: |
| 65 return "wep"; | 64 return "wep"; |
| 66 case chromeos::WPA: | 65 case chromeos::WPA: |
| 67 return "wpa"; | 66 return "wpa"; |
| 68 } | 67 } |
| 69 return "none"; | 68 return "none"; |
| 70 } | 69 } |
| 71 | 70 |
| 72 void CrosNetworkLibrary::ConnectToWifiNetwork(WifiNetwork network, | 71 void CrosNetworkLibrary::ConnectToWifiNetwork(WifiNetwork network, |
| 73 const string16& password) { | 72 const string16& password) { |
| 74 if (CrosLibrary::loaded()) { | 73 if (!CrosLibrary::loaded()) |
| 75 MessageLoop* loop = ChromeThread::GetMessageLoop(ChromeThread::FILE); | 74 return; |
| 76 if (loop) | 75 |
| 77 loop->PostTask(FROM_HERE, NewRunnableFunction( | 76 ChromeThread::PostTask( |
| 77 ChromeThread::FILE, FROM_HERE, |
| 78 NewRunnableFunction( |
| 78 &chromeos::ConnectToWifiNetwork, | 79 &chromeos::ConnectToWifiNetwork, |
| 79 network.ssid.c_str(), | 80 network.ssid.c_str(), |
| 80 password.empty() ? NULL : UTF16ToUTF8(password).c_str(), | 81 password.empty() ? NULL : UTF16ToUTF8(password).c_str(), |
| 81 GetEncryptionString(network.encryption))); | 82 GetEncryptionString(network.encryption))); |
| 82 } | |
| 83 } | 83 } |
| 84 | 84 |
| 85 // static | 85 // static |
| 86 void CrosNetworkLibrary::NetworkStatusChangedHandler(void* object, | 86 void CrosNetworkLibrary::NetworkStatusChangedHandler(void* object, |
| 87 const chromeos::ServiceStatus& service_status) { | 87 const chromeos::ServiceStatus& service_status) { |
| 88 CrosNetworkLibrary* network = static_cast<CrosNetworkLibrary*>(object); | 88 CrosNetworkLibrary* network = static_cast<CrosNetworkLibrary*>(object); |
| 89 WifiNetworkVector networks; | 89 WifiNetworkVector networks; |
| 90 bool ethernet_connected; | 90 bool ethernet_connected; |
| 91 ParseNetworks(service_status, &networks, ðernet_connected); | 91 ParseNetworks(service_status, &networks, ðernet_connected); |
| 92 network->UpdateNetworkStatus(networks, ethernet_connected); | 92 network->UpdateNetworkStatus(networks, ethernet_connected); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 chromeos::FreeServiceStatus(service_status); | 132 chromeos::FreeServiceStatus(service_status); |
| 133 } | 133 } |
| 134 network_status_connection_ = chromeos::MonitorNetworkStatus( | 134 network_status_connection_ = chromeos::MonitorNetworkStatus( |
| 135 &NetworkStatusChangedHandler, this); | 135 &NetworkStatusChangedHandler, this); |
| 136 } | 136 } |
| 137 | 137 |
| 138 void CrosNetworkLibrary::UpdateNetworkStatus( | 138 void CrosNetworkLibrary::UpdateNetworkStatus( |
| 139 const WifiNetworkVector& networks, bool ethernet_connected) { | 139 const WifiNetworkVector& networks, bool ethernet_connected) { |
| 140 // Make sure we run on UI thread. | 140 // Make sure we run on UI thread. |
| 141 if (!ChromeThread::CurrentlyOn(ChromeThread::UI)) { | 141 if (!ChromeThread::CurrentlyOn(ChromeThread::UI)) { |
| 142 MessageLoop* loop = ChromeThread::GetMessageLoop(ChromeThread::UI); | 142 ChromeThread::PostTask( |
| 143 if (loop) | 143 ChromeThread::UI, FROM_HERE, |
| 144 loop->PostTask(FROM_HERE, NewRunnableMethod(this, | 144 NewRunnableMethod(this, |
| 145 &CrosNetworkLibrary::UpdateNetworkStatus, networks, | 145 &CrosNetworkLibrary::UpdateNetworkStatus, networks, |
| 146 ethernet_connected)); | 146 ethernet_connected)); |
| 147 return; | 147 return; |
| 148 } | 148 } |
| 149 | 149 |
| 150 ethernet_connected_ = ethernet_connected; | 150 ethernet_connected_ = ethernet_connected; |
| 151 wifi_networks_ = networks; | 151 wifi_networks_ = networks; |
| 152 // Sort the list of wifi networks by ssid. | 152 // Sort the list of wifi networks by ssid. |
| 153 std::sort(wifi_networks_.begin(), wifi_networks_.end()); | 153 std::sort(wifi_networks_.begin(), wifi_networks_.end()); |
| 154 wifi_ = WifiNetwork(); | 154 wifi_ = WifiNetwork(); |
| 155 for (size_t i = 0; i < wifi_networks_.size(); i++) { | 155 for (size_t i = 0; i < wifi_networks_.size(); i++) { |
| 156 if (wifi_networks_[i].connecting || wifi_networks_[i].connected) { | 156 if (wifi_networks_[i].connecting || wifi_networks_[i].connected) { |
| 157 wifi_ = wifi_networks_[i]; | 157 wifi_ = wifi_networks_[i]; |
| 158 break; // There is only one connected or connecting wifi network. | 158 break; // There is only one connected or connecting wifi network. |
| 159 } | 159 } |
| 160 } | 160 } |
| 161 FOR_EACH_OBSERVER(Observer, observers_, NetworkChanged(this)); | 161 FOR_EACH_OBSERVER(Observer, observers_, NetworkChanged(this)); |
| 162 } | 162 } |
| OLD | NEW |