OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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/connection_tester.h" | 5 #include "chrome/browser/net/connection_tester.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 // run by ConnectionTester. The class initializes network dependencies according | 46 // run by ConnectionTester. The class initializes network dependencies according |
47 // to the specified "experiment". | 47 // to the specified "experiment". |
48 class ExperimentURLRequestContext : public net::URLRequestContext { | 48 class ExperimentURLRequestContext : public net::URLRequestContext { |
49 public: | 49 public: |
50 explicit ExperimentURLRequestContext( | 50 explicit ExperimentURLRequestContext( |
51 net::URLRequestContext* proxy_request_context) | 51 net::URLRequestContext* proxy_request_context) |
52 : proxy_request_context_(proxy_request_context), | 52 : proxy_request_context_(proxy_request_context), |
53 ALLOW_THIS_IN_INITIALIZER_LIST(storage_(this)), | 53 ALLOW_THIS_IN_INITIALIZER_LIST(storage_(this)), |
54 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {} | 54 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {} |
55 | 55 |
| 56 virtual ~ExperimentURLRequestContext() {} |
| 57 |
56 // Creates a proxy config service for |experiment|. On success returns net::OK | 58 // Creates a proxy config service for |experiment|. On success returns net::OK |
57 // and fills |config_service| with a new pointer. Otherwise returns a network | 59 // and fills |config_service| with a new pointer. Otherwise returns a network |
58 // error code. | 60 // error code. |
59 int CreateProxyConfigService( | 61 int CreateProxyConfigService( |
60 ConnectionTester::ProxySettingsExperiment experiment, | 62 ConnectionTester::ProxySettingsExperiment experiment, |
61 scoped_ptr<net::ProxyConfigService>* config_service, | 63 scoped_ptr<net::ProxyConfigService>* config_service, |
62 base::Callback<void(int)> callback) { | 64 base::Callback<void(int)> callback) { |
63 switch (experiment) { | 65 switch (experiment) { |
64 case ConnectionTester::PROXY_EXPERIMENT_USE_SYSTEM_SETTINGS: | 66 case ConnectionTester::PROXY_EXPERIMENT_USE_SYSTEM_SETTINGS: |
65 return CreateSystemProxyConfigService(config_service); | 67 return CreateSystemProxyConfigService(config_service); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 new net::HttpNetworkSession(session_params)); | 122 new net::HttpNetworkSession(session_params)); |
121 storage_.set_http_transaction_factory(new net::HttpCache( | 123 storage_.set_http_transaction_factory(new net::HttpCache( |
122 network_session, | 124 network_session, |
123 net::HttpCache::DefaultBackend::InMemory(0))); | 125 net::HttpCache::DefaultBackend::InMemory(0))); |
124 // In-memory cookie store. | 126 // In-memory cookie store. |
125 storage_.set_cookie_store(new net::CookieMonster(NULL, NULL)); | 127 storage_.set_cookie_store(new net::CookieMonster(NULL, NULL)); |
126 | 128 |
127 return net::OK; | 129 return net::OK; |
128 } | 130 } |
129 | 131 |
130 protected: | |
131 virtual ~ExperimentURLRequestContext() { | |
132 } | |
133 | |
134 private: | 132 private: |
135 // Creates a host resolver for |experiment|. On success returns net::OK and | 133 // Creates a host resolver for |experiment|. On success returns net::OK and |
136 // fills |host_resolver| with a new pointer. Otherwise returns a network | 134 // fills |host_resolver| with a new pointer. Otherwise returns a network |
137 // error code. | 135 // error code. |
138 int CreateHostResolver( | 136 int CreateHostResolver( |
139 ConnectionTester::HostResolverExperiment experiment, | 137 ConnectionTester::HostResolverExperiment experiment, |
140 scoped_ptr<net::HostResolver>* host_resolver) { | 138 scoped_ptr<net::HostResolver>* host_resolver) { |
141 // Create a vanilla HostResolver that disables caching. | 139 // Create a vanilla HostResolver that disables caching. |
142 const size_t kMaxJobs = 50u; | 140 const size_t kMaxJobs = 50u; |
143 const size_t kMaxRetryAttempts = 4u; | 141 const size_t kMaxRetryAttempts = 4u; |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 &ExperimentURLRequestContext::FirefoxProxySettingsReply, | 263 &ExperimentURLRequestContext::FirefoxProxySettingsReply, |
266 weak_factory_.GetWeakPtr(), config_service, | 264 weak_factory_.GetWeakPtr(), config_service, |
267 base::Owned(ff_settings), callback); | 265 base::Owned(ff_settings), callback); |
268 if (!content::BrowserThread::PostTaskAndReplyWithResult<int>( | 266 if (!content::BrowserThread::PostTaskAndReplyWithResult<int>( |
269 content::BrowserThread::FILE, FROM_HERE, task, reply)) | 267 content::BrowserThread::FILE, FROM_HERE, task, reply)) |
270 return net::ERR_FAILED; | 268 return net::ERR_FAILED; |
271 return net::ERR_IO_PENDING; | 269 return net::ERR_IO_PENDING; |
272 #endif | 270 #endif |
273 } | 271 } |
274 | 272 |
275 const scoped_refptr<net::URLRequestContext> proxy_request_context_; | 273 net::URLRequestContext* const proxy_request_context_; |
276 net::URLRequestContextStorage storage_; | 274 net::URLRequestContextStorage storage_; |
277 base::WeakPtrFactory<ExperimentURLRequestContext> weak_factory_; | 275 base::WeakPtrFactory<ExperimentURLRequestContext> weak_factory_; |
278 }; | 276 }; |
279 | 277 |
280 } // namespace | 278 } // namespace |
281 | 279 |
282 // ConnectionTester::TestRunner ---------------------------------------------- | 280 // ConnectionTester::TestRunner ---------------------------------------------- |
283 | 281 |
284 // TestRunner is a helper class for running an individual experiment. It can | 282 // TestRunner is a helper class for running an individual experiment. It can |
285 // be deleted any time after it is started, and this will abort the request. | 283 // be deleted any time after it is started, and this will abort the request. |
286 class ConnectionTester::TestRunner : public net::URLRequest::Delegate { | 284 class ConnectionTester::TestRunner : public net::URLRequest::Delegate { |
287 public: | 285 public: |
288 // |tester| must remain alive throughout the TestRunner's lifetime. | 286 // |tester| must remain alive throughout the TestRunner's lifetime. |
289 // |tester| will be notified of completion. | 287 // |tester| will be notified of completion. |
290 explicit TestRunner(ConnectionTester* tester) | 288 explicit TestRunner(ConnectionTester* tester) |
291 : tester_(tester), | 289 : tester_(tester), |
292 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {} | 290 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {} |
293 | 291 |
294 // Finish running |experiment| once a ProxyConfigService has been created. | 292 // Finish running |experiment| once a ProxyConfigService has been created. |
295 // In the case of a FirefoxProxyConfigService, this will be called back | 293 // In the case of a FirefoxProxyConfigService, this will be called back |
296 // after disk access has completed. | 294 // after disk access has completed. |
297 void ProxyConfigServiceCreated( | 295 void ProxyConfigServiceCreated( |
298 const Experiment& experiment, | 296 const Experiment& experiment, |
299 scoped_refptr<ExperimentURLRequestContext> context, | |
300 scoped_ptr<net::ProxyConfigService>* proxy_config_service, int status); | 297 scoped_ptr<net::ProxyConfigService>* proxy_config_service, int status); |
301 | 298 |
302 // Starts running |experiment|. Notifies tester->OnExperimentCompleted() when | 299 // Starts running |experiment|. Notifies tester->OnExperimentCompleted() when |
303 // it is done. | 300 // it is done. |
304 void Run(const Experiment& experiment); | 301 void Run(const Experiment& experiment); |
305 | 302 |
306 // Overridden from net::URLRequest::Delegate: | 303 // Overridden from net::URLRequest::Delegate: |
307 virtual void OnResponseStarted(net::URLRequest* request); | 304 virtual void OnResponseStarted(net::URLRequest* request); |
308 virtual void OnReadCompleted(net::URLRequest* request, int bytes_read); | 305 virtual void OnReadCompleted(net::URLRequest* request, int bytes_read); |
309 // TODO(eroman): handle cases requiring authentication. | 306 // TODO(eroman): handle cases requiring authentication. |
310 | 307 |
311 private: | 308 private: |
312 // The number of bytes to read each response body chunk. | 309 // The number of bytes to read each response body chunk. |
313 static const int kReadBufferSize = 1024; | 310 static const int kReadBufferSize = 1024; |
314 | 311 |
315 // Starts reading the response's body (and keeps reading until an error or | 312 // Starts reading the response's body (and keeps reading until an error or |
316 // end of stream). | 313 // end of stream). |
317 void ReadBody(net::URLRequest* request); | 314 void ReadBody(net::URLRequest* request); |
318 | 315 |
319 // Called when the request has completed (for both success and failure). | 316 // Called when the request has completed (for both success and failure). |
320 void OnResponseCompleted(net::URLRequest* request); | 317 void OnResponseCompleted(net::URLRequest* request); |
321 void OnExperimentCompletedWithResult(int result); | 318 void OnExperimentCompletedWithResult(int result); |
322 | 319 |
323 ConnectionTester* tester_; | 320 ConnectionTester* tester_; |
| 321 scoped_ptr<ExperimentURLRequestContext> request_context_; |
324 scoped_ptr<net::URLRequest> request_; | 322 scoped_ptr<net::URLRequest> request_; |
325 | 323 |
326 base::WeakPtrFactory<TestRunner> weak_factory_; | 324 base::WeakPtrFactory<TestRunner> weak_factory_; |
327 | 325 |
328 DISALLOW_COPY_AND_ASSIGN(TestRunner); | 326 DISALLOW_COPY_AND_ASSIGN(TestRunner); |
329 }; | 327 }; |
330 | 328 |
331 void ConnectionTester::TestRunner::OnResponseStarted(net::URLRequest* request) { | 329 void ConnectionTester::TestRunner::OnResponseStarted(net::URLRequest* request) { |
332 if (!request->status().is_success()) { | 330 if (!request->status().is_success()) { |
333 OnResponseCompleted(request); | 331 OnResponseCompleted(request); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
378 base::Bind(&TestRunner::OnExperimentCompletedWithResult, | 376 base::Bind(&TestRunner::OnExperimentCompletedWithResult, |
379 weak_factory_.GetWeakPtr(), result)); | 377 weak_factory_.GetWeakPtr(), result)); |
380 } | 378 } |
381 | 379 |
382 void ConnectionTester::TestRunner::OnExperimentCompletedWithResult(int result) { | 380 void ConnectionTester::TestRunner::OnExperimentCompletedWithResult(int result) { |
383 tester_->OnExperimentCompleted(result); | 381 tester_->OnExperimentCompleted(result); |
384 } | 382 } |
385 | 383 |
386 void ConnectionTester::TestRunner::ProxyConfigServiceCreated( | 384 void ConnectionTester::TestRunner::ProxyConfigServiceCreated( |
387 const Experiment& experiment, | 385 const Experiment& experiment, |
388 scoped_refptr<ExperimentURLRequestContext> context, | |
389 scoped_ptr<net::ProxyConfigService>* proxy_config_service, | 386 scoped_ptr<net::ProxyConfigService>* proxy_config_service, |
390 int status) { | 387 int status) { |
391 if (status == net::OK) | 388 if (status == net::OK) |
392 status = context->Init(experiment, proxy_config_service); | 389 status = request_context_->Init(experiment, proxy_config_service); |
393 if (status != net::OK) { | 390 if (status != net::OK) { |
394 tester_->OnExperimentCompleted(status); | 391 tester_->OnExperimentCompleted(status); |
395 return; | 392 return; |
396 } | 393 } |
397 // Fetch a request using the experimental context. | 394 // Fetch a request using the experimental context. |
398 request_.reset(new net::URLRequest(experiment.url, this)); | 395 request_.reset(new net::URLRequest(experiment.url, this)); |
399 request_->set_context(context); | 396 request_->set_context(request_context_.get()); |
400 request_->Start(); | 397 request_->Start(); |
401 } | 398 } |
402 | 399 |
403 void ConnectionTester::TestRunner::Run(const Experiment& experiment) { | 400 void ConnectionTester::TestRunner::Run(const Experiment& experiment) { |
404 // Try to create a net::URLRequestContext for this experiment. | 401 // Try to create a net::URLRequestContext for this experiment. |
405 scoped_refptr<ExperimentURLRequestContext> context( | 402 request_context_.reset( |
406 new ExperimentURLRequestContext(tester_->proxy_request_context_)); | 403 new ExperimentURLRequestContext(tester_->proxy_request_context_)); |
407 scoped_ptr<net::ProxyConfigService>* proxy_config_service = | 404 scoped_ptr<net::ProxyConfigService>* proxy_config_service = |
408 new scoped_ptr<net::ProxyConfigService>(); | 405 new scoped_ptr<net::ProxyConfigService>(); |
409 base::Callback<void(int)> config_service_callback = | 406 base::Callback<void(int)> config_service_callback = |
410 base::Bind( | 407 base::Bind( |
411 &TestRunner::ProxyConfigServiceCreated, weak_factory_.GetWeakPtr(), | 408 &TestRunner::ProxyConfigServiceCreated, weak_factory_.GetWeakPtr(), |
412 experiment, context, base::Owned(proxy_config_service)); | 409 experiment, base::Owned(proxy_config_service)); |
413 int rv = context->CreateProxyConfigService( | 410 int rv = request_context_->CreateProxyConfigService( |
414 experiment.proxy_settings_experiment, | 411 experiment.proxy_settings_experiment, |
415 proxy_config_service, config_service_callback); | 412 proxy_config_service, config_service_callback); |
416 if (rv != net::ERR_IO_PENDING) | 413 if (rv != net::ERR_IO_PENDING) |
417 ProxyConfigServiceCreated(experiment, context, proxy_config_service, rv); | 414 ProxyConfigServiceCreated(experiment, proxy_config_service, rv); |
418 } | 415 } |
419 | 416 |
420 // ConnectionTester ---------------------------------------------------------- | 417 // ConnectionTester ---------------------------------------------------------- |
421 | 418 |
422 ConnectionTester::ConnectionTester( | 419 ConnectionTester::ConnectionTester( |
423 Delegate* delegate, | 420 Delegate* delegate, |
424 net::URLRequestContext* proxy_request_context) | 421 net::URLRequestContext* proxy_request_context) |
425 : delegate_(delegate), proxy_request_context_(proxy_request_context) { | 422 : delegate_(delegate), proxy_request_context_(proxy_request_context) { |
426 DCHECK(delegate); | 423 DCHECK(delegate); |
427 DCHECK(proxy_request_context); | 424 DCHECK(proxy_request_context); |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
515 | 512 |
516 // Notify the delegate of completion. | 513 // Notify the delegate of completion. |
517 delegate_->OnCompletedConnectionTestExperiment(current, result); | 514 delegate_->OnCompletedConnectionTestExperiment(current, result); |
518 | 515 |
519 if (remaining_experiments_.empty()) { | 516 if (remaining_experiments_.empty()) { |
520 delegate_->OnCompletedConnectionTestSuite(); | 517 delegate_->OnCompletedConnectionTestSuite(); |
521 } else { | 518 } else { |
522 StartNextExperiment(); | 519 StartNextExperiment(); |
523 } | 520 } |
524 } | 521 } |
OLD | NEW |