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" | |
10 #include "base/string_util.h" | 9 #include "base/string_util.h" |
11 #include "chrome/browser/chrome_thread.h" | 10 #include "chrome/browser/chrome_thread.h" |
12 #include "chrome/browser/chromeos/cros_library.h" | 11 #include "chrome/browser/chromeos/cros_library.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<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 //////////////////////////////////////////////////////////////////////////////// |
23 // CrosNetworkLibrary | |
24 | |
25 // static | |
26 const int CrosNetworkLibrary::kNetworkTrafficeTimerSecs = 1; | |
27 | |
28 CrosNetworkLibrary::CrosNetworkLibrary() | |
29 : traffic_type_(0), | |
30 ethernet_connected_(false) { | |
23 if (CrosLibrary::loaded()) { | 31 if (CrosLibrary::loaded()) { |
24 Init(); | 32 Init(); |
25 } | 33 } |
34 g_url_request_job_tracker.AddObserver(this); | |
26 } | 35 } |
27 | 36 |
28 CrosNetworkLibrary::~CrosNetworkLibrary() { | 37 CrosNetworkLibrary::~CrosNetworkLibrary() { |
29 if (CrosLibrary::loaded()) { | 38 if (CrosLibrary::loaded()) { |
30 chromeos::DisconnectNetworkStatus(network_status_connection_); | 39 chromeos::DisconnectNetworkStatus(network_status_connection_); |
31 } | 40 } |
41 g_url_request_job_tracker.RemoveObserver(this); | |
32 } | 42 } |
33 | 43 |
34 // static | 44 // static |
35 CrosNetworkLibrary* CrosNetworkLibrary::Get() { | 45 CrosNetworkLibrary* CrosNetworkLibrary::Get() { |
36 return Singleton<CrosNetworkLibrary>::get(); | 46 return Singleton<CrosNetworkLibrary>::get(); |
37 } | 47 } |
38 | 48 |
39 // static | 49 // static |
40 bool CrosNetworkLibrary::loaded() { | 50 bool CrosNetworkLibrary::loaded() { |
41 return CrosLibrary::loaded(); | 51 return CrosLibrary::loaded(); |
42 } | 52 } |
43 | 53 |
54 //////////////////////////////////////////////////////////////////////////////// | |
55 // CrosNetworkLibrary, URLRequestJobTracker::JobObserver implementation: | |
56 | |
57 void CrosNetworkLibrary::OnJobAdded(URLRequestJob* job) { | |
58 CheckNetworkTraffic(false); | |
59 } | |
60 | |
61 void CrosNetworkLibrary::OnJobRemoved(URLRequestJob* job) { | |
62 CheckNetworkTraffic(false); | |
63 } | |
64 | |
65 void CrosNetworkLibrary::OnJobDone(URLRequestJob* job, | |
66 const URLRequestStatus& status) { | |
67 CheckNetworkTraffic(false); | |
68 } | |
69 | |
70 void CrosNetworkLibrary::OnJobRedirect(URLRequestJob* job, const GURL& location, | |
71 int status_code) { | |
72 CheckNetworkTraffic(false); | |
73 } | |
74 | |
75 void CrosNetworkLibrary::OnBytesRead(URLRequestJob* job, int byte_count) { | |
76 CheckNetworkTraffic(true); | |
77 } | |
78 | |
44 void CrosNetworkLibrary::AddObserver(Observer* observer) { | 79 void CrosNetworkLibrary::AddObserver(Observer* observer) { |
45 observers_.AddObserver(observer); | 80 observers_.AddObserver(observer); |
46 } | 81 } |
47 | 82 |
48 void CrosNetworkLibrary::RemoveObserver(Observer* observer) { | 83 void CrosNetworkLibrary::RemoveObserver(Observer* observer) { |
49 observers_.RemoveObserver(observer); | 84 observers_.RemoveObserver(observer); |
50 } | 85 } |
51 | 86 |
52 static const char* GetEncryptionString(chromeos::EncryptionType encryption) { | 87 static const char* GetEncryptionString(chromeos::EncryptionType encryption) { |
53 switch (encryption) { | 88 switch (encryption) { |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
151 std::sort(wifi_networks_.begin(), wifi_networks_.end()); | 186 std::sort(wifi_networks_.begin(), wifi_networks_.end()); |
152 wifi_ = WifiNetwork(); | 187 wifi_ = WifiNetwork(); |
153 for (size_t i = 0; i < wifi_networks_.size(); i++) { | 188 for (size_t i = 0; i < wifi_networks_.size(); i++) { |
154 if (wifi_networks_[i].connecting || wifi_networks_[i].connected) { | 189 if (wifi_networks_[i].connecting || wifi_networks_[i].connected) { |
155 wifi_ = wifi_networks_[i]; | 190 wifi_ = wifi_networks_[i]; |
156 break; // There is only one connected or connecting wifi network. | 191 break; // There is only one connected or connecting wifi network. |
157 } | 192 } |
158 } | 193 } |
159 FOR_EACH_OBSERVER(Observer, observers_, NetworkChanged(this)); | 194 FOR_EACH_OBSERVER(Observer, observers_, NetworkChanged(this)); |
160 } | 195 } |
196 | |
197 void CrosNetworkLibrary::CheckNetworkTraffic(bool download) { | |
198 // If we already have a pending upload and download notification, then | |
199 // shortcut and return. | |
200 if (traffic_type_ == (Observer::TRAFFIC_DOWNLOAD | Observer::TRAFFIC_UPLOAD)) | |
201 return; | |
202 // Figure out if we are uploading and/or downloading. We are downloading | |
203 // if download == true. We are uploading if we have upload progress. | |
204 if (download) | |
205 traffic_type_ |= Observer::TRAFFIC_DOWNLOAD; | |
206 URLRequestJobTracker::JobIterator it; | |
207 for (it = g_url_request_job_tracker.begin(); | |
208 it != g_url_request_job_tracker.end(); | |
209 ++it) { | |
210 URLRequestJob* job = *it; | |
211 if ((traffic_type_ & Observer::TRAFFIC_UPLOAD) == 0 && | |
212 job->GetUploadProgress() > 0) | |
213 traffic_type_ |= Observer::TRAFFIC_UPLOAD; | |
214 if (traffic_type_ == (Observer::TRAFFIC_DOWNLOAD | | |
rvargas (doing something else)
2009/10/28 01:21:15
Now we don't have to do this. Just break with the
| |
215 Observer::TRAFFIC_UPLOAD)) | |
216 break; | |
217 } | |
218 // If we have new traffic data to send out and the timer is not currently | |
219 // running, then start a new timer. | |
220 if (traffic_type_ && !timer_.IsRunning()) { | |
221 timer_.Start(base::TimeDelta::FromSeconds(kNetworkTrafficeTimerSecs), this, | |
222 &CrosNetworkLibrary::NetworkTrafficTimerFired); | |
223 } | |
224 } | |
225 | |
226 void CrosNetworkLibrary:: NetworkTrafficTimerFired() { | |
227 ChromeThread::PostTask( | |
228 ChromeThread::UI, FROM_HERE, | |
229 NewRunnableMethod(this, &CrosNetworkLibrary::NotifyNetworkTraffic, | |
230 traffic_type_)); | |
231 // Reset traffic type so that we don't send the same data next time. | |
232 traffic_type_ = 0; | |
233 } | |
234 | |
235 void CrosNetworkLibrary::NotifyNetworkTraffic(int traffic_type) { | |
236 FOR_EACH_OBSERVER(Observer, observers_, NetworkTraffic(this, traffic_type)); | |
237 } | |
OLD | NEW |