| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/net/network_stats.h" | 5 #include "chrome/browser/net/network_stats.h" |
| 6 | 6 |
| 7 #include "base/bind.h" |
| 7 #include "base/callback_old.h" | 8 #include "base/callback_old.h" |
| 8 #include "base/logging.h" | 9 #include "base/logging.h" |
| 9 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| 10 #include "base/metrics/field_trial.h" | 11 #include "base/metrics/field_trial.h" |
| 11 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
| 12 #include "base/stringprintf.h" | 13 #include "base/stringprintf.h" |
| 13 #include "base/task.h" | 14 #include "base/task.h" |
| 14 #include "base/threading/platform_thread.h" | 15 #include "base/threading/platform_thread.h" |
| 15 #include "base/time.h" | 16 #include "base/time.h" |
| 16 #include "base/tuple.h" | 17 #include "base/tuple.h" |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 bytes_to_send_(0), | 83 bytes_to_send_(0), |
| 83 encoded_message_(""), | 84 encoded_message_(""), |
| 84 ALLOW_THIS_IN_INITIALIZER_LIST( | 85 ALLOW_THIS_IN_INITIALIZER_LIST( |
| 85 resolve_callback_(this, &NetworkStats::OnResolveComplete)), | 86 resolve_callback_(this, &NetworkStats::OnResolveComplete)), |
| 86 ALLOW_THIS_IN_INITIALIZER_LIST( | 87 ALLOW_THIS_IN_INITIALIZER_LIST( |
| 87 read_callback_(this, &NetworkStats::OnReadComplete)), | 88 read_callback_(this, &NetworkStats::OnReadComplete)), |
| 88 ALLOW_THIS_IN_INITIALIZER_LIST( | 89 ALLOW_THIS_IN_INITIALIZER_LIST( |
| 89 write_callback_(this, &NetworkStats::OnWriteComplete)), | 90 write_callback_(this, &NetworkStats::OnWriteComplete)), |
| 90 finished_callback_(NULL), | 91 finished_callback_(NULL), |
| 91 start_time_(base::TimeTicks::Now()), | 92 start_time_(base::TimeTicks::Now()), |
| 92 ALLOW_THIS_IN_INITIALIZER_LIST(timers_factory_(this)) { | 93 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
| 93 } | 94 } |
| 94 | 95 |
| 95 NetworkStats::~NetworkStats() { | 96 NetworkStats::~NetworkStats() { |
| 96 socket_.reset(); | 97 socket_.reset(); |
| 97 } | 98 } |
| 98 | 99 |
| 99 bool NetworkStats::Start(net::HostResolver* host_resolver, | 100 bool NetworkStats::Start(net::HostResolver* host_resolver, |
| 100 const net::HostPortPair& server_host_port_pair, | 101 const net::HostPortPair& server_host_port_pair, |
| 101 uint32 bytes_to_send, | 102 uint32 bytes_to_send, |
| 102 net::OldCompletionCallback* finished_callback) { | 103 net::OldCompletionCallback* finished_callback) { |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 | 201 |
| 201 void NetworkStats::OnReadComplete(int result) { | 202 void NetworkStats::OnReadComplete(int result) { |
| 202 if (!ReadComplete(result)) { | 203 if (!ReadComplete(result)) { |
| 203 // Called ReadData() via PostDelayedTask() to avoid recursion. Added a delay | 204 // Called ReadData() via PostDelayedTask() to avoid recursion. Added a delay |
| 204 // of 1ms so that the time-out will fire before we have time to really hog | 205 // of 1ms so that the time-out will fire before we have time to really hog |
| 205 // the CPU too extensively (waiting for the time-out) in case of an infinite | 206 // the CPU too extensively (waiting for the time-out) in case of an infinite |
| 206 // loop. | 207 // loop. |
| 207 const int kReadDataDelayMs = 1; | 208 const int kReadDataDelayMs = 1; |
| 208 MessageLoop::current()->PostDelayedTask( | 209 MessageLoop::current()->PostDelayedTask( |
| 209 FROM_HERE, | 210 FROM_HERE, |
| 210 timers_factory_.NewRunnableMethod(&NetworkStats::ReadData), | 211 base::Bind(&NetworkStats::ReadData, weak_factory_.GetWeakPtr()), |
| 211 kReadDataDelayMs); | 212 kReadDataDelayMs); |
| 212 } | 213 } |
| 213 } | 214 } |
| 214 | 215 |
| 215 void NetworkStats::OnWriteComplete(int result) { | 216 void NetworkStats::OnWriteComplete(int result) { |
| 216 DCHECK(socket_.get()); | 217 DCHECK(socket_.get()); |
| 217 DCHECK_NE(net::ERR_IO_PENDING, result); | 218 DCHECK_NE(net::ERR_IO_PENDING, result); |
| 218 if (result < 0) { | 219 if (result < 0) { |
| 219 Finish(WRITE_FAILED, result); | 220 Finish(WRITE_FAILED, result); |
| 220 return; | 221 return; |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 276 bytes_to_send_ -= rv; | 277 bytes_to_send_ -= rv; |
| 277 if (!write_buffer_->BytesRemaining()) | 278 if (!write_buffer_->BytesRemaining()) |
| 278 write_buffer_ = NULL; | 279 write_buffer_ = NULL; |
| 279 } while (bytes_to_send_); | 280 } while (bytes_to_send_); |
| 280 return net::OK; | 281 return net::OK; |
| 281 } | 282 } |
| 282 | 283 |
| 283 void NetworkStats::StartReadDataTimer(int milliseconds) { | 284 void NetworkStats::StartReadDataTimer(int milliseconds) { |
| 284 MessageLoop::current()->PostDelayedTask( | 285 MessageLoop::current()->PostDelayedTask( |
| 285 FROM_HERE, | 286 FROM_HERE, |
| 286 timers_factory_.NewRunnableMethod(&NetworkStats::OnReadDataTimeout), | 287 base::Bind(&NetworkStats::OnReadDataTimeout, weak_factory_.GetWeakPtr()), |
| 287 milliseconds); | 288 milliseconds); |
| 288 } | 289 } |
| 289 | 290 |
| 290 void NetworkStats::OnReadDataTimeout() { | 291 void NetworkStats::OnReadDataTimeout() { |
| 291 Finish(READ_TIMED_OUT, net::ERR_INVALID_ARGUMENT); | 292 Finish(READ_TIMED_OUT, net::ERR_INVALID_ARGUMENT); |
| 292 } | 293 } |
| 293 | 294 |
| 294 void NetworkStats::GetEchoRequest(net::IOBuffer* io_buffer) { | 295 void NetworkStats::GetEchoRequest(net::IOBuffer* io_buffer) { |
| 295 // Copy the <version> into the io_buffer starting from the kVersionStart | 296 // Copy the <version> into the io_buffer starting from the kVersionStart |
| 296 // position. | 297 // position. |
| (...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 523 IOThread* io_thread) { | 524 IOThread* io_thread) { |
| 524 if (network_stats_server.empty()) | 525 if (network_stats_server.empty()) |
| 525 return; | 526 return; |
| 526 | 527 |
| 527 // If we are not on IO Thread, then post a task to call CollectNetworkStats on | 528 // If we are not on IO Thread, then post a task to call CollectNetworkStats on |
| 528 // IO Thread. | 529 // IO Thread. |
| 529 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { | 530 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
| 530 BrowserThread::PostTask( | 531 BrowserThread::PostTask( |
| 531 BrowserThread::IO, | 532 BrowserThread::IO, |
| 532 FROM_HERE, | 533 FROM_HERE, |
| 533 NewRunnableFunction( | 534 base::Bind( |
| 534 &CollectNetworkStats, network_stats_server, io_thread)); | 535 &CollectNetworkStats, network_stats_server, io_thread)); |
| 535 return; | 536 return; |
| 536 } | 537 } |
| 537 | 538 |
| 538 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 539 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 539 | 540 |
| 540 // Check that there is a network connection. We get called only if UMA upload | 541 // Check that there is a network connection. We get called only if UMA upload |
| 541 // to the server has succeeded. | 542 // to the server has succeeded. |
| 542 DCHECK(!net::NetworkChangeNotifier::IsOffline()); | 543 DCHECK(!net::NetworkChangeNotifier::IsOffline()); |
| 543 | 544 |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 598 TCPStatsClient* small_tcp_client = new TCPStatsClient(); | 599 TCPStatsClient* small_tcp_client = new TCPStatsClient(); |
| 599 small_tcp_client->Start( | 600 small_tcp_client->Start( |
| 600 host_resolver, tcp_server_address, kSmallTestBytesToSend, NULL); | 601 host_resolver, tcp_server_address, kSmallTestBytesToSend, NULL); |
| 601 | 602 |
| 602 TCPStatsClient* large_tcp_client = new TCPStatsClient(); | 603 TCPStatsClient* large_tcp_client = new TCPStatsClient(); |
| 603 large_tcp_client->Start( | 604 large_tcp_client->Start( |
| 604 host_resolver, tcp_server_address, kLargeTestBytesToSend, NULL); | 605 host_resolver, tcp_server_address, kLargeTestBytesToSend, NULL); |
| 605 } | 606 } |
| 606 | 607 |
| 607 } // namespace chrome_browser_net | 608 } // namespace chrome_browser_net |
| OLD | NEW |