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/connection_tester.h" | 5 #include "chrome/browser/net/connection_tester.h" |
6 | 6 |
| 7 #include "base/bind.h" |
7 #include "base/command_line.h" | 8 #include "base/command_line.h" |
8 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
9 #include "base/logging.h" | 10 #include "base/logging.h" |
10 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
11 #include "base/task.h" | 12 #include "base/task.h" |
12 #include "base/threading/thread_restrictions.h" | 13 #include "base/threading/thread_restrictions.h" |
13 #include "base/utf_string_conversions.h" | 14 #include "base/utf_string_conversions.h" |
14 #include "chrome/browser/importer/firefox_proxy_settings.h" | 15 #include "chrome/browser/importer/firefox_proxy_settings.h" |
15 #include "chrome/common/chrome_switches.h" | 16 #include "chrome/common/chrome_switches.h" |
16 #include "net/base/cert_verifier.h" | 17 #include "net/base/cert_verifier.h" |
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
255 // ConnectionTester::TestRunner ---------------------------------------------- | 256 // ConnectionTester::TestRunner ---------------------------------------------- |
256 | 257 |
257 // TestRunner is a helper class for running an individual experiment. It can | 258 // TestRunner is a helper class for running an individual experiment. It can |
258 // be deleted any time after it is started, and this will abort the request. | 259 // be deleted any time after it is started, and this will abort the request. |
259 class ConnectionTester::TestRunner : public net::URLRequest::Delegate { | 260 class ConnectionTester::TestRunner : public net::URLRequest::Delegate { |
260 public: | 261 public: |
261 // |tester| must remain alive throughout the TestRunner's lifetime. | 262 // |tester| must remain alive throughout the TestRunner's lifetime. |
262 // |tester| will be notified of completion. | 263 // |tester| will be notified of completion. |
263 explicit TestRunner(ConnectionTester* tester) | 264 explicit TestRunner(ConnectionTester* tester) |
264 : tester_(tester), | 265 : tester_(tester), |
265 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) {} | 266 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {} |
266 | 267 |
267 // Starts running |experiment|. Notifies tester->OnExperimentCompleted() when | 268 // Starts running |experiment|. Notifies tester->OnExperimentCompleted() when |
268 // it is done. | 269 // it is done. |
269 void Run(const Experiment& experiment); | 270 void Run(const Experiment& experiment); |
270 | 271 |
271 // Overridden from net::URLRequest::Delegate: | 272 // Overridden from net::URLRequest::Delegate: |
272 virtual void OnResponseStarted(net::URLRequest* request); | 273 virtual void OnResponseStarted(net::URLRequest* request); |
273 virtual void OnReadCompleted(net::URLRequest* request, int bytes_read); | 274 virtual void OnReadCompleted(net::URLRequest* request, int bytes_read); |
274 // TODO(eroman): handle cases requiring authentication. | 275 // TODO(eroman): handle cases requiring authentication. |
275 | 276 |
276 private: | 277 private: |
277 // The number of bytes to read each response body chunk. | 278 // The number of bytes to read each response body chunk. |
278 static const int kReadBufferSize = 1024; | 279 static const int kReadBufferSize = 1024; |
279 | 280 |
280 // Starts reading the response's body (and keeps reading until an error or | 281 // Starts reading the response's body (and keeps reading until an error or |
281 // end of stream). | 282 // end of stream). |
282 void ReadBody(net::URLRequest* request); | 283 void ReadBody(net::URLRequest* request); |
283 | 284 |
284 // Called when the request has completed (for both success and failure). | 285 // Called when the request has completed (for both success and failure). |
285 void OnResponseCompleted(net::URLRequest* request); | 286 void OnResponseCompleted(net::URLRequest* request); |
286 void OnExperimentCompletedWithResult(int result); | 287 void OnExperimentCompletedWithResult(int result); |
287 | 288 |
288 ConnectionTester* tester_; | 289 ConnectionTester* tester_; |
289 scoped_ptr<net::URLRequest> request_; | 290 scoped_ptr<net::URLRequest> request_; |
290 | 291 |
291 ScopedRunnableMethodFactory<TestRunner> method_factory_; | 292 base::WeakPtrFactory<TestRunner> weak_factory_; |
292 | 293 |
293 DISALLOW_COPY_AND_ASSIGN(TestRunner); | 294 DISALLOW_COPY_AND_ASSIGN(TestRunner); |
294 }; | 295 }; |
295 | 296 |
296 void ConnectionTester::TestRunner::OnResponseStarted(net::URLRequest* request) { | 297 void ConnectionTester::TestRunner::OnResponseStarted(net::URLRequest* request) { |
297 if (!request->status().is_success()) { | 298 if (!request->status().is_success()) { |
298 OnResponseCompleted(request); | 299 OnResponseCompleted(request); |
299 return; | 300 return; |
300 } | 301 } |
301 | 302 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
333 if (!request->status().is_success()) { | 334 if (!request->status().is_success()) { |
334 DCHECK_NE(net::ERR_IO_PENDING, request->status().error()); | 335 DCHECK_NE(net::ERR_IO_PENDING, request->status().error()); |
335 result = request->status().error(); | 336 result = request->status().error(); |
336 } | 337 } |
337 | 338 |
338 // Post a task to notify the parent rather than handling it right away, | 339 // Post a task to notify the parent rather than handling it right away, |
339 // to avoid re-entrancy problems with URLRequest. (Don't want the caller | 340 // to avoid re-entrancy problems with URLRequest. (Don't want the caller |
340 // to end up deleting the URLRequest while in the middle of processing). | 341 // to end up deleting the URLRequest while in the middle of processing). |
341 MessageLoop::current()->PostTask( | 342 MessageLoop::current()->PostTask( |
342 FROM_HERE, | 343 FROM_HERE, |
343 method_factory_.NewRunnableMethod( | 344 base::Bind(&TestRunner::OnExperimentCompletedWithResult, |
344 &TestRunner::OnExperimentCompletedWithResult, result)); | 345 weak_factory_.GetWeakPtr(), result)); |
345 } | 346 } |
346 | 347 |
347 void ConnectionTester::TestRunner::OnExperimentCompletedWithResult(int result) { | 348 void ConnectionTester::TestRunner::OnExperimentCompletedWithResult(int result) { |
348 tester_->OnExperimentCompleted(result); | 349 tester_->OnExperimentCompleted(result); |
349 } | 350 } |
350 | 351 |
351 void ConnectionTester::TestRunner::Run(const Experiment& experiment) { | 352 void ConnectionTester::TestRunner::Run(const Experiment& experiment) { |
352 // Try to create a net::URLRequestContext for this experiment. | 353 // Try to create a net::URLRequestContext for this experiment. |
353 scoped_refptr<ExperimentURLRequestContext> context( | 354 scoped_refptr<ExperimentURLRequestContext> context( |
354 new ExperimentURLRequestContext(tester_->proxy_request_context_)); | 355 new ExperimentURLRequestContext(tester_->proxy_request_context_)); |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
463 | 464 |
464 // Notify the delegate of completion. | 465 // Notify the delegate of completion. |
465 delegate_->OnCompletedConnectionTestExperiment(current, result); | 466 delegate_->OnCompletedConnectionTestExperiment(current, result); |
466 | 467 |
467 if (remaining_experiments_.empty()) { | 468 if (remaining_experiments_.empty()) { |
468 delegate_->OnCompletedConnectionTestSuite(); | 469 delegate_->OnCompletedConnectionTestSuite(); |
469 } else { | 470 } else { |
470 StartNextExperiment(); | 471 StartNextExperiment(); |
471 } | 472 } |
472 } | 473 } |
OLD | NEW |