OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 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/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/metrics/field_trial.h" | 10 #include "base/metrics/field_trial.h" |
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 return ConnectComplete(rv); | 212 return ConnectComplete(rv); |
213 } | 213 } |
214 | 214 |
215 bool NetworkStats::ConnectComplete(int result) { | 215 bool NetworkStats::ConnectComplete(int result) { |
216 if (result < 0) { | 216 if (result < 0) { |
217 TestPhaseComplete(CONNECT_FAILED, result); | 217 TestPhaseComplete(CONNECT_FAILED, result); |
218 return false; | 218 return false; |
219 } | 219 } |
220 | 220 |
221 if (start_test_after_connect_) { | 221 if (start_test_after_connect_) { |
222 ReadData(); // This ReadData() reads data for all HelloReply and all | 222 // ReadData() reads data for all HelloReply and all subsequent probe tests. |
223 // subsequent probe tests. | 223 if (ReadData() != net::ERR_IO_PENDING) |
| 224 return false; |
224 SendHelloRequest(); | 225 SendHelloRequest(); |
225 } else { | 226 } else { |
226 // For unittesting. Only run the callback, do not destroy it. | 227 // For unittesting. Only run the callback, do not destroy it. |
227 if (!finished_callback_.is_null()) | 228 if (!finished_callback_.is_null()) |
228 finished_callback_.Run(result); | 229 finished_callback_.Run(result); |
229 } | 230 } |
230 return true; | 231 return true; |
231 } | 232 } |
232 | 233 |
233 void NetworkStats::SendHelloRequest() { | 234 void NetworkStats::SendHelloRequest() { |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
284 &probe_packet); | 285 &probe_packet); |
285 std::string output = probe_message_.MakeEncodedPacket(probe_packet); | 286 std::string output = probe_message_.MakeEncodedPacket(probe_packet); |
286 | 287 |
287 StartReadDataTimer(timeout_seconds, current_test_index_); | 288 StartReadDataTimer(timeout_seconds, current_test_index_); |
288 probe_request_time_ = base::TimeTicks::Now(); | 289 probe_request_time_ = base::TimeTicks::Now(); |
289 int result = SendData(output); | 290 int result = SendData(output); |
290 if (result < 0 && result != net::ERR_IO_PENDING) | 291 if (result < 0 && result != net::ERR_IO_PENDING) |
291 TestPhaseComplete(WRITE_FAILED, result); | 292 TestPhaseComplete(WRITE_FAILED, result); |
292 } | 293 } |
293 | 294 |
294 void NetworkStats::ReadData() { | 295 int NetworkStats::ReadData() { |
295 if (!socket_.get()) | 296 if (!socket_.get()) |
296 return; | 297 return 0; |
297 | 298 |
298 int rv = 0; | 299 int rv = 0; |
299 do { | 300 do { |
300 DCHECK(!read_buffer_.get()); | 301 DCHECK(!read_buffer_.get()); |
301 read_buffer_ = new net::IOBuffer(kMaxMessageSize); | 302 read_buffer_ = new net::IOBuffer(kMaxMessageSize); |
302 | 303 |
303 rv = socket_->Read( | 304 rv = socket_->Read( |
304 read_buffer_.get(), | 305 read_buffer_.get(), |
305 kMaxMessageSize, | 306 kMaxMessageSize, |
306 base::Bind(&NetworkStats::OnReadComplete, base::Unretained(this))); | 307 base::Bind(&NetworkStats::OnReadComplete, base::Unretained(this))); |
307 } while (rv > 0 && !ReadComplete(rv)); | 308 } while (rv > 0 && !ReadComplete(rv)); |
| 309 return rv; |
308 } | 310 } |
309 | 311 |
310 void NetworkStats::OnReadComplete(int result) { | 312 void NetworkStats::OnReadComplete(int result) { |
311 if (!ReadComplete(result)) { | 313 if (!ReadComplete(result)) { |
312 // Called ReadData() via PostDelayedTask() to avoid recursion. Added a delay | 314 // Called ReadData() via PostDelayedTask() to avoid recursion. Added a delay |
313 // of 1ms so that the time-out will fire before we have time to really hog | 315 // of 1ms so that the time-out will fire before we have time to really hog |
314 // the CPU too extensively (waiting for the time-out) in case of an infinite | 316 // the CPU too extensively (waiting for the time-out) in case of an infinite |
315 // loop. | 317 // loop. |
316 base::MessageLoop::current()->PostDelayedTask( | 318 base::MessageLoop::current()->PostDelayedTask( |
317 FROM_HERE, | 319 FROM_HERE, |
318 base::Bind(&NetworkStats::ReadData, weak_factory_.GetWeakPtr()), | 320 base::Bind(base::IgnoreResult(&NetworkStats::ReadData), |
| 321 weak_factory_.GetWeakPtr()), |
319 base::TimeDelta::FromMilliseconds(1)); | 322 base::TimeDelta::FromMilliseconds(1)); |
320 } | 323 } |
321 } | 324 } |
322 | 325 |
323 bool NetworkStats::ReadComplete(int result) { | 326 bool NetworkStats::ReadComplete(int result) { |
324 DCHECK(socket_.get()); | 327 DCHECK(socket_.get()); |
325 DCHECK_NE(net::ERR_IO_PENDING, result); | 328 DCHECK_NE(net::ERR_IO_PENDING, result); |
326 if (result < 0) { | 329 if (result < 0) { |
327 // Something is wrong, finish the test. | 330 // Something is wrong, finish the test. |
328 read_buffer_ = NULL; | 331 read_buffer_ = NULL; |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
413 | 416 |
414 int NetworkStats::SendData(const std::string& output) { | 417 int NetworkStats::SendData(const std::string& output) { |
415 if (write_buffer_.get() || !socket_.get()) | 418 if (write_buffer_.get() || !socket_.get()) |
416 return net::ERR_UNEXPECTED; | 419 return net::ERR_UNEXPECTED; |
417 scoped_refptr<net::StringIOBuffer> buffer(new net::StringIOBuffer(output)); | 420 scoped_refptr<net::StringIOBuffer> buffer(new net::StringIOBuffer(output)); |
418 write_buffer_ = new net::DrainableIOBuffer(buffer.get(), buffer->size()); | 421 write_buffer_ = new net::DrainableIOBuffer(buffer.get(), buffer->size()); |
419 | 422 |
420 int bytes_written = socket_->Write( | 423 int bytes_written = socket_->Write( |
421 write_buffer_.get(), | 424 write_buffer_.get(), |
422 write_buffer_->BytesRemaining(), | 425 write_buffer_->BytesRemaining(), |
423 base::Bind(&NetworkStats::OnWriteComplete, base::Unretained(this))); | 426 base::Bind(&NetworkStats::OnWriteComplete, weak_factory_.GetWeakPtr())); |
424 if (bytes_written < 0) | 427 if (bytes_written < 0) |
425 return bytes_written; | 428 return bytes_written; |
426 UpdateSendBuffer(bytes_written); | 429 UpdateSendBuffer(bytes_written); |
427 return net::OK; | 430 return net::OK; |
428 } | 431 } |
429 | 432 |
430 void NetworkStats::OnWriteComplete(int result) { | 433 void NetworkStats::OnWriteComplete(int result) { |
431 DCHECK(socket_.get()); | 434 DCHECK(socket_.get()); |
432 DCHECK_NE(net::ERR_IO_PENDING, result); | 435 DCHECK_NE(net::ERR_IO_PENDING, result); |
433 if (result < 0) { | 436 if (result < 0) { |
(...skipping 420 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
854 new NetworkStats(net::ClientSocketFactory::GetDefaultFactory()); | 857 new NetworkStats(net::ClientSocketFactory::GetDefaultFactory()); |
855 udp_stats_client->Start(host_resolver, | 858 udp_stats_client->Start(host_resolver, |
856 server_address, | 859 server_address, |
857 histogram_port, | 860 histogram_port, |
858 has_proxy_server, | 861 has_proxy_server, |
859 kProbePacketBytes[probe_choice], | 862 kProbePacketBytes[probe_choice], |
860 net::CompletionCallback()); | 863 net::CompletionCallback()); |
861 } | 864 } |
862 | 865 |
863 } // namespace chrome_browser_net | 866 } // namespace chrome_browser_net |
OLD | NEW |