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/network_library.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
10 #include "chrome/browser/chrome_thread.h" | 10 #include "chrome/browser/chrome_thread.h" |
11 #include "chrome/browser/chromeos/cros_library.h" | 11 #include "chrome/browser/chromeos/cros_library.h" |
12 #include "net/url_request/url_request_job.h" | 12 #include "net/url_request/url_request_job.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<chromeos::NetworkLibrary> { |
18 void RetainCallee(CrosNetworkLibrary* obj) {} | 18 void RetainCallee(chromeos::NetworkLibrary* obj) {} |
19 void ReleaseCallee(CrosNetworkLibrary* obj) {} | 19 void ReleaseCallee(chromeos::NetworkLibrary* obj) {} |
20 }; | 20 }; |
21 | 21 |
| 22 namespace chromeos { |
| 23 |
22 //////////////////////////////////////////////////////////////////////////////// | 24 //////////////////////////////////////////////////////////////////////////////// |
23 // CrosNetworkLibrary | 25 // NetworkLibrary |
24 | 26 |
25 // static | 27 // static |
26 const int CrosNetworkLibrary::kNetworkTrafficeTimerSecs = 1; | 28 const int NetworkLibrary::kNetworkTrafficeTimerSecs = 1; |
27 | 29 |
28 CrosNetworkLibrary::CrosNetworkLibrary() | 30 NetworkLibrary::NetworkLibrary() |
29 : traffic_type_(0), | 31 : traffic_type_(0), |
30 ethernet_connected_(false) { | 32 ethernet_connected_(false) { |
31 if (CrosLibrary::loaded()) { | 33 if (CrosLibrary::loaded()) { |
32 Init(); | 34 Init(); |
33 } | 35 } |
34 g_url_request_job_tracker.AddObserver(this); | 36 g_url_request_job_tracker.AddObserver(this); |
35 } | 37 } |
36 | 38 |
37 CrosNetworkLibrary::~CrosNetworkLibrary() { | 39 NetworkLibrary::~NetworkLibrary() { |
38 if (CrosLibrary::loaded()) { | 40 if (CrosLibrary::loaded()) { |
39 chromeos::DisconnectNetworkStatus(network_status_connection_); | 41 chromeos::DisconnectNetworkStatus(network_status_connection_); |
40 } | 42 } |
41 g_url_request_job_tracker.RemoveObserver(this); | 43 g_url_request_job_tracker.RemoveObserver(this); |
42 } | 44 } |
43 | 45 |
44 // static | 46 // static |
45 CrosNetworkLibrary* CrosNetworkLibrary::Get() { | 47 NetworkLibrary* NetworkLibrary::Get() { |
46 return Singleton<CrosNetworkLibrary>::get(); | 48 return Singleton<NetworkLibrary>::get(); |
47 } | 49 } |
48 | 50 |
49 // static | 51 // static |
50 bool CrosNetworkLibrary::loaded() { | 52 bool NetworkLibrary::loaded() { |
51 return CrosLibrary::loaded(); | 53 return CrosLibrary::loaded(); |
52 } | 54 } |
53 | 55 |
54 //////////////////////////////////////////////////////////////////////////////// | 56 //////////////////////////////////////////////////////////////////////////////// |
55 // CrosNetworkLibrary, URLRequestJobTracker::JobObserver implementation: | 57 // NetworkLibrary, URLRequestJobTracker::JobObserver implementation: |
56 | 58 |
57 void CrosNetworkLibrary::OnJobAdded(URLRequestJob* job) { | 59 void NetworkLibrary::OnJobAdded(URLRequestJob* job) { |
58 CheckNetworkTraffic(false); | 60 CheckNetworkTraffic(false); |
59 } | 61 } |
60 | 62 |
61 void CrosNetworkLibrary::OnJobRemoved(URLRequestJob* job) { | 63 void NetworkLibrary::OnJobRemoved(URLRequestJob* job) { |
62 CheckNetworkTraffic(false); | 64 CheckNetworkTraffic(false); |
63 } | 65 } |
64 | 66 |
65 void CrosNetworkLibrary::OnJobDone(URLRequestJob* job, | 67 void NetworkLibrary::OnJobDone(URLRequestJob* job, |
66 const URLRequestStatus& status) { | 68 const URLRequestStatus& status) { |
67 CheckNetworkTraffic(false); | 69 CheckNetworkTraffic(false); |
68 } | 70 } |
69 | 71 |
70 void CrosNetworkLibrary::OnJobRedirect(URLRequestJob* job, const GURL& location, | 72 void NetworkLibrary::OnJobRedirect(URLRequestJob* job, const GURL& location, |
71 int status_code) { | 73 int status_code) { |
72 CheckNetworkTraffic(false); | 74 CheckNetworkTraffic(false); |
73 } | 75 } |
74 | 76 |
75 void CrosNetworkLibrary::OnBytesRead(URLRequestJob* job, int byte_count) { | 77 void NetworkLibrary::OnBytesRead(URLRequestJob* job, int byte_count) { |
76 CheckNetworkTraffic(true); | 78 CheckNetworkTraffic(true); |
77 } | 79 } |
78 | 80 |
79 void CrosNetworkLibrary::AddObserver(Observer* observer) { | 81 void NetworkLibrary::AddObserver(Observer* observer) { |
80 observers_.AddObserver(observer); | 82 observers_.AddObserver(observer); |
81 } | 83 } |
82 | 84 |
83 void CrosNetworkLibrary::RemoveObserver(Observer* observer) { | 85 void NetworkLibrary::RemoveObserver(Observer* observer) { |
84 observers_.RemoveObserver(observer); | 86 observers_.RemoveObserver(observer); |
85 } | 87 } |
86 | 88 |
87 static const char* GetEncryptionString(chromeos::EncryptionType encryption) { | 89 static const char* GetEncryptionString(chromeos::EncryptionType encryption) { |
88 switch (encryption) { | 90 switch (encryption) { |
89 case chromeos::NONE: | 91 case chromeos::NONE: |
90 return "none"; | 92 return "none"; |
91 case chromeos::RSN: | 93 case chromeos::RSN: |
92 return "rsn"; | 94 return "rsn"; |
93 case chromeos::WEP: | 95 case chromeos::WEP: |
94 return "wep"; | 96 return "wep"; |
95 case chromeos::WPA: | 97 case chromeos::WPA: |
96 return "wpa"; | 98 return "wpa"; |
97 } | 99 } |
98 return "none"; | 100 return "none"; |
99 } | 101 } |
100 | 102 |
101 void CrosNetworkLibrary::ConnectToWifiNetwork(WifiNetwork network, | 103 void NetworkLibrary::ConnectToWifiNetwork(WifiNetwork network, |
102 const string16& password) { | 104 const string16& password) { |
103 if (CrosLibrary::loaded()) { | 105 if (CrosLibrary::loaded()) { |
104 // This call kicks off a request to connect to this network, the results of | 106 // This call kicks off a request to connect to this network, the results of |
105 // which we'll hear about through the monitoring we've set up in Init(); | 107 // which we'll hear about through the monitoring we've set up in Init(); |
106 chromeos::ConnectToWifiNetwork( | 108 chromeos::ConnectToWifiNetwork( |
107 network.ssid.c_str(), | 109 network.ssid.c_str(), |
108 password.empty() ? NULL : UTF16ToUTF8(password).c_str(), | 110 password.empty() ? NULL : UTF16ToUTF8(password).c_str(), |
109 GetEncryptionString(network.encryption)); | 111 GetEncryptionString(network.encryption)); |
110 } | 112 } |
111 } | 113 } |
112 | 114 |
113 // static | 115 // static |
114 void CrosNetworkLibrary::NetworkStatusChangedHandler(void* object, | 116 void NetworkLibrary::NetworkStatusChangedHandler(void* object, |
115 const chromeos::ServiceStatus& service_status) { | 117 const chromeos::ServiceStatus& service_status) { |
116 CrosNetworkLibrary* network = static_cast<CrosNetworkLibrary*>(object); | 118 NetworkLibrary* network = static_cast<NetworkLibrary*>(object); |
117 WifiNetworkVector networks; | 119 WifiNetworkVector networks; |
118 bool ethernet_connected; | 120 bool ethernet_connected; |
119 ParseNetworks(service_status, &networks, ðernet_connected); | 121 ParseNetworks(service_status, &networks, ðernet_connected); |
120 network->UpdateNetworkStatus(networks, ethernet_connected); | 122 network->UpdateNetworkStatus(networks, ethernet_connected); |
121 } | 123 } |
122 | 124 |
123 // static | 125 // static |
124 void CrosNetworkLibrary::ParseNetworks( | 126 void NetworkLibrary::ParseNetworks( |
125 const chromeos::ServiceStatus& service_status, WifiNetworkVector* networks, | 127 const chromeos::ServiceStatus& service_status, WifiNetworkVector* networks, |
126 bool* ethernet_connected) { | 128 bool* ethernet_connected) { |
127 *ethernet_connected = false; | 129 *ethernet_connected = false; |
128 for (int i = 0; i < service_status.size; i++) { | 130 for (int i = 0; i < service_status.size; i++) { |
129 const chromeos::ServiceInfo& service = service_status.services[i]; | 131 const chromeos::ServiceInfo& service = service_status.services[i]; |
130 DLOG(INFO) << "Parse " << service.ssid << | 132 DLOG(INFO) << "Parse " << service.ssid << |
131 " typ=" << service.type << | 133 " typ=" << service.type << |
132 " sta=" << service.state << | 134 " sta=" << service.state << |
133 " pas=" << service.needs_passphrase << | 135 " pas=" << service.needs_passphrase << |
134 " enc=" << service.encryption << | 136 " enc=" << service.encryption << |
135 " sig=" << service.signal_strength; | 137 " sig=" << service.signal_strength; |
136 if (service.type == chromeos::TYPE_ETHERNET) { | 138 if (service.type == chromeos::TYPE_ETHERNET) { |
137 // Get the ethernet status. | 139 // Get the ethernet status. |
138 *ethernet_connected = service.state == chromeos::STATE_READY; | 140 *ethernet_connected = service.state == chromeos::STATE_READY; |
139 } else if (service.type == chromeos::TYPE_WIFI) { | 141 } else if (service.type == chromeos::TYPE_WIFI) { |
140 bool connecting = service.state == chromeos::STATE_ASSOCIATION || | 142 bool connecting = service.state == chromeos::STATE_ASSOCIATION || |
141 service.state == chromeos::STATE_CONFIGURATION; | 143 service.state == chromeos::STATE_CONFIGURATION; |
142 bool connected = service.state == chromeos::STATE_READY; | 144 bool connected = service.state == chromeos::STATE_READY; |
143 networks->push_back(WifiNetwork(service.ssid, | 145 networks->push_back(WifiNetwork(service.ssid, |
144 service.needs_passphrase, | 146 service.needs_passphrase, |
145 service.encryption, | 147 service.encryption, |
146 service.signal_strength, | 148 service.signal_strength, |
147 connecting, | 149 connecting, |
148 connected)); | 150 connected)); |
149 } | 151 } |
150 } | 152 } |
151 } | 153 } |
152 | 154 |
153 void CrosNetworkLibrary::Init() { | 155 void NetworkLibrary::Init() { |
154 // First, get the currently available networks. This data is cached | 156 // First, get the currently available networks. This data is cached |
155 // on the connman side, so the call should be quick. | 157 // on the connman side, so the call should be quick. |
156 chromeos::ServiceStatus* service_status = chromeos::GetAvailableNetworks(); | 158 chromeos::ServiceStatus* service_status = chromeos::GetAvailableNetworks(); |
157 if (service_status) { | 159 if (service_status) { |
158 LOG(INFO) << "Getting initial CrOS network info."; | 160 LOG(INFO) << "Getting initial CrOS network info."; |
159 WifiNetworkVector networks; | 161 WifiNetworkVector networks; |
160 bool ethernet_connected; | 162 bool ethernet_connected; |
161 ParseNetworks(*service_status, &networks, ðernet_connected); | 163 ParseNetworks(*service_status, &networks, ðernet_connected); |
162 UpdateNetworkStatus(networks, ethernet_connected); | 164 UpdateNetworkStatus(networks, ethernet_connected); |
163 chromeos::FreeServiceStatus(service_status); | 165 chromeos::FreeServiceStatus(service_status); |
164 } | 166 } |
165 LOG(INFO) << "Registering for network status updates."; | 167 LOG(INFO) << "Registering for network status updates."; |
166 // Now, register to receive updates on network status. | 168 // Now, register to receive updates on network status. |
167 network_status_connection_ = chromeos::MonitorNetworkStatus( | 169 network_status_connection_ = chromeos::MonitorNetworkStatus( |
168 &NetworkStatusChangedHandler, this); | 170 &NetworkStatusChangedHandler, this); |
169 } | 171 } |
170 | 172 |
171 void CrosNetworkLibrary::UpdateNetworkStatus( | 173 void NetworkLibrary::UpdateNetworkStatus( |
172 const WifiNetworkVector& networks, bool ethernet_connected) { | 174 const WifiNetworkVector& networks, bool ethernet_connected) { |
173 // Make sure we run on UI thread. | 175 // Make sure we run on UI thread. |
174 if (!ChromeThread::CurrentlyOn(ChromeThread::UI)) { | 176 if (!ChromeThread::CurrentlyOn(ChromeThread::UI)) { |
175 ChromeThread::PostTask( | 177 ChromeThread::PostTask( |
176 ChromeThread::UI, FROM_HERE, | 178 ChromeThread::UI, FROM_HERE, |
177 NewRunnableMethod(this, | 179 NewRunnableMethod(this, |
178 &CrosNetworkLibrary::UpdateNetworkStatus, networks, | 180 &NetworkLibrary::UpdateNetworkStatus, networks, |
179 ethernet_connected)); | 181 ethernet_connected)); |
180 return; | 182 return; |
181 } | 183 } |
182 | 184 |
183 ethernet_connected_ = ethernet_connected; | 185 ethernet_connected_ = ethernet_connected; |
184 wifi_networks_ = networks; | 186 wifi_networks_ = networks; |
185 // Sort the list of wifi networks by ssid. | 187 // Sort the list of wifi networks by ssid. |
186 std::sort(wifi_networks_.begin(), wifi_networks_.end()); | 188 std::sort(wifi_networks_.begin(), wifi_networks_.end()); |
187 wifi_ = WifiNetwork(); | 189 wifi_ = WifiNetwork(); |
188 for (size_t i = 0; i < wifi_networks_.size(); i++) { | 190 for (size_t i = 0; i < wifi_networks_.size(); i++) { |
189 if (wifi_networks_[i].connecting || wifi_networks_[i].connected) { | 191 if (wifi_networks_[i].connecting || wifi_networks_[i].connected) { |
190 wifi_ = wifi_networks_[i]; | 192 wifi_ = wifi_networks_[i]; |
191 break; // There is only one connected or connecting wifi network. | 193 break; // There is only one connected or connecting wifi network. |
192 } | 194 } |
193 } | 195 } |
194 FOR_EACH_OBSERVER(Observer, observers_, NetworkChanged(this)); | 196 FOR_EACH_OBSERVER(Observer, observers_, NetworkChanged(this)); |
195 } | 197 } |
196 | 198 |
197 void CrosNetworkLibrary::CheckNetworkTraffic(bool download) { | 199 void NetworkLibrary::CheckNetworkTraffic(bool download) { |
198 // If we already have a pending upload and download notification, then | 200 // If we already have a pending upload and download notification, then |
199 // shortcut and return. | 201 // shortcut and return. |
200 if (traffic_type_ == (Observer::TRAFFIC_DOWNLOAD | Observer::TRAFFIC_UPLOAD)) | 202 if (traffic_type_ == (Observer::TRAFFIC_DOWNLOAD | Observer::TRAFFIC_UPLOAD)) |
201 return; | 203 return; |
202 // Figure out if we are uploading and/or downloading. We are downloading | 204 // Figure out if we are uploading and/or downloading. We are downloading |
203 // if download == true. We are uploading if we have upload progress. | 205 // if download == true. We are uploading if we have upload progress. |
204 if (download) | 206 if (download) |
205 traffic_type_ |= Observer::TRAFFIC_DOWNLOAD; | 207 traffic_type_ |= Observer::TRAFFIC_DOWNLOAD; |
206 if ((traffic_type_ & Observer::TRAFFIC_UPLOAD) == 0) { | 208 if ((traffic_type_ & Observer::TRAFFIC_UPLOAD) == 0) { |
207 URLRequestJobTracker::JobIterator it; | 209 URLRequestJobTracker::JobIterator it; |
208 for (it = g_url_request_job_tracker.begin(); | 210 for (it = g_url_request_job_tracker.begin(); |
209 it != g_url_request_job_tracker.end(); | 211 it != g_url_request_job_tracker.end(); |
210 ++it) { | 212 ++it) { |
211 URLRequestJob* job = *it; | 213 URLRequestJob* job = *it; |
212 if (job->GetUploadProgress() > 0) { | 214 if (job->GetUploadProgress() > 0) { |
213 traffic_type_ |= Observer::TRAFFIC_UPLOAD; | 215 traffic_type_ |= Observer::TRAFFIC_UPLOAD; |
214 break; | 216 break; |
215 } | 217 } |
216 } | 218 } |
217 } | 219 } |
218 // If we have new traffic data to send out and the timer is not currently | 220 // If we have new traffic data to send out and the timer is not currently |
219 // running, then start a new timer. | 221 // running, then start a new timer. |
220 if (traffic_type_ && !timer_.IsRunning()) { | 222 if (traffic_type_ && !timer_.IsRunning()) { |
221 timer_.Start(base::TimeDelta::FromSeconds(kNetworkTrafficeTimerSecs), this, | 223 timer_.Start(base::TimeDelta::FromSeconds(kNetworkTrafficeTimerSecs), this, |
222 &CrosNetworkLibrary::NetworkTrafficTimerFired); | 224 &NetworkLibrary::NetworkTrafficTimerFired); |
223 } | 225 } |
224 } | 226 } |
225 | 227 |
226 void CrosNetworkLibrary:: NetworkTrafficTimerFired() { | 228 void NetworkLibrary:: NetworkTrafficTimerFired() { |
227 ChromeThread::PostTask( | 229 ChromeThread::PostTask( |
228 ChromeThread::UI, FROM_HERE, | 230 ChromeThread::UI, FROM_HERE, |
229 NewRunnableMethod(this, &CrosNetworkLibrary::NotifyNetworkTraffic, | 231 NewRunnableMethod(this, &NetworkLibrary::NotifyNetworkTraffic, |
230 traffic_type_)); | 232 traffic_type_)); |
231 // Reset traffic type so that we don't send the same data next time. | 233 // Reset traffic type so that we don't send the same data next time. |
232 traffic_type_ = 0; | 234 traffic_type_ = 0; |
233 } | 235 } |
234 | 236 |
235 void CrosNetworkLibrary::NotifyNetworkTraffic(int traffic_type) { | 237 void NetworkLibrary::NotifyNetworkTraffic(int traffic_type) { |
236 FOR_EACH_OBSERVER(Observer, observers_, NetworkTraffic(this, traffic_type)); | 238 FOR_EACH_OBSERVER(Observer, observers_, NetworkTraffic(this, traffic_type)); |
237 } | 239 } |
| 240 |
| 241 } // namespace chromeos |
OLD | NEW |