OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 | 216 |
217 IOThread* io_thread_; | 217 IOThread* io_thread_; |
218 }; | 218 }; |
219 | 219 |
220 } // namespace | 220 } // namespace |
221 | 221 |
222 // ConnectionTester::TestRunner ---------------------------------------------- | 222 // ConnectionTester::TestRunner ---------------------------------------------- |
223 | 223 |
224 // TestRunner is a helper class for running an individual experiment. It can | 224 // TestRunner is a helper class for running an individual experiment. It can |
225 // be deleted any time after it is started, and this will abort the request. | 225 // be deleted any time after it is started, and this will abort the request. |
226 class ConnectionTester::TestRunner : public URLRequest::Delegate { | 226 class ConnectionTester::TestRunner : public net::URLRequest::Delegate { |
227 public: | 227 public: |
228 // |tester| must remain alive throughout the TestRunner's lifetime. | 228 // |tester| must remain alive throughout the TestRunner's lifetime. |
229 // |tester| will be notified of completion. | 229 // |tester| will be notified of completion. |
230 explicit TestRunner(ConnectionTester* tester) : tester_(tester) {} | 230 explicit TestRunner(ConnectionTester* tester) : tester_(tester) {} |
231 | 231 |
232 // Starts running |experiment|. Notifies tester->OnExperimentCompleted() when | 232 // Starts running |experiment|. Notifies tester->OnExperimentCompleted() when |
233 // it is done. | 233 // it is done. |
234 void Run(const Experiment& experiment); | 234 void Run(const Experiment& experiment); |
235 | 235 |
236 // URLRequest::Delegate implementation. | 236 // Overridden from net::URLRequest::Delegate: |
237 virtual void OnResponseStarted(URLRequest* request); | 237 virtual void OnResponseStarted(net::URLRequest* request); |
238 virtual void OnReadCompleted(URLRequest* request, int bytes_read); | 238 virtual void OnReadCompleted(net::URLRequest* request, int bytes_read); |
239 // TODO(eroman): handle cases requiring authentication. | 239 // TODO(eroman): handle cases requiring authentication. |
240 | 240 |
241 private: | 241 private: |
242 // The number of bytes to read each response body chunk. | 242 // The number of bytes to read each response body chunk. |
243 static const int kReadBufferSize = 1024; | 243 static const int kReadBufferSize = 1024; |
244 | 244 |
245 // Starts reading the response's body (and keeps reading until an error or | 245 // Starts reading the response's body (and keeps reading until an error or |
246 // end of stream). | 246 // end of stream). |
247 void ReadBody(URLRequest* request); | 247 void ReadBody(net::URLRequest* request); |
248 | 248 |
249 // Called when the request has completed (for both success and failure). | 249 // Called when the request has completed (for both success and failure). |
250 void OnResponseCompleted(URLRequest* request); | 250 void OnResponseCompleted(net::URLRequest* request); |
251 | 251 |
252 ConnectionTester* tester_; | 252 ConnectionTester* tester_; |
253 scoped_ptr<URLRequest> request_; | 253 scoped_ptr<net::URLRequest> request_; |
254 | 254 |
255 DISALLOW_COPY_AND_ASSIGN(TestRunner); | 255 DISALLOW_COPY_AND_ASSIGN(TestRunner); |
256 }; | 256 }; |
257 | 257 |
258 void ConnectionTester::TestRunner::OnResponseStarted(URLRequest* request) { | 258 void ConnectionTester::TestRunner::OnResponseStarted(net::URLRequest* request) { |
259 if (!request->status().is_success()) { | 259 if (!request->status().is_success()) { |
260 OnResponseCompleted(request); | 260 OnResponseCompleted(request); |
261 return; | 261 return; |
262 } | 262 } |
263 | 263 |
264 // Start reading the body. | 264 // Start reading the body. |
265 ReadBody(request); | 265 ReadBody(request); |
266 } | 266 } |
267 | 267 |
268 void ConnectionTester::TestRunner::OnReadCompleted(URLRequest* request, | 268 void ConnectionTester::TestRunner::OnReadCompleted(net::URLRequest* request, |
269 int bytes_read) { | 269 int bytes_read) { |
270 if (bytes_read <= 0) { | 270 if (bytes_read <= 0) { |
271 OnResponseCompleted(request); | 271 OnResponseCompleted(request); |
272 return; | 272 return; |
273 } | 273 } |
274 | 274 |
275 // Keep reading until the stream is closed. Throw the data read away. | 275 // Keep reading until the stream is closed. Throw the data read away. |
276 ReadBody(request); | 276 ReadBody(request); |
277 } | 277 } |
278 | 278 |
279 void ConnectionTester::TestRunner::ReadBody(URLRequest* request) { | 279 void ConnectionTester::TestRunner::ReadBody(net::URLRequest* request) { |
280 // Read the response body |kReadBufferSize| bytes at a time. | 280 // Read the response body |kReadBufferSize| bytes at a time. |
281 scoped_refptr<net::IOBuffer> unused_buffer( | 281 scoped_refptr<net::IOBuffer> unused_buffer( |
282 new net::IOBuffer(kReadBufferSize)); | 282 new net::IOBuffer(kReadBufferSize)); |
283 int num_bytes; | 283 int num_bytes; |
284 if (request->Read(unused_buffer, kReadBufferSize, &num_bytes)) { | 284 if (request->Read(unused_buffer, kReadBufferSize, &num_bytes)) { |
285 OnReadCompleted(request, num_bytes); | 285 OnReadCompleted(request, num_bytes); |
286 } else if (!request->status().is_io_pending()) { | 286 } else if (!request->status().is_io_pending()) { |
287 // Read failed synchronously. | 287 // Read failed synchronously. |
288 OnResponseCompleted(request); | 288 OnResponseCompleted(request); |
289 } | 289 } |
290 } | 290 } |
291 | 291 |
292 void ConnectionTester::TestRunner::OnResponseCompleted(URLRequest* request) { | 292 void ConnectionTester::TestRunner::OnResponseCompleted( |
| 293 net::URLRequest* request) { |
293 int result = net::OK; | 294 int result = net::OK; |
294 if (!request->status().is_success()) { | 295 if (!request->status().is_success()) { |
295 DCHECK_NE(net::ERR_IO_PENDING, request->status().os_error()); | 296 DCHECK_NE(net::ERR_IO_PENDING, request->status().os_error()); |
296 result = request->status().os_error(); | 297 result = request->status().os_error(); |
297 } | 298 } |
298 tester_->OnExperimentCompleted(result); | 299 tester_->OnExperimentCompleted(result); |
299 } | 300 } |
300 | 301 |
301 void ConnectionTester::TestRunner::Run(const Experiment& experiment) { | 302 void ConnectionTester::TestRunner::Run(const Experiment& experiment) { |
302 // Try to create a URLRequestContext for this experiment. | 303 // Try to create a URLRequestContext for this experiment. |
303 scoped_refptr<ExperimentURLRequestContext> context( | 304 scoped_refptr<ExperimentURLRequestContext> context( |
304 new ExperimentURLRequestContext(tester_->io_thread_)); | 305 new ExperimentURLRequestContext(tester_->io_thread_)); |
305 int rv = context->Init(experiment); | 306 int rv = context->Init(experiment); |
306 if (rv != net::OK) { | 307 if (rv != net::OK) { |
307 // Complete the experiment with a failure. | 308 // Complete the experiment with a failure. |
308 tester_->OnExperimentCompleted(rv); | 309 tester_->OnExperimentCompleted(rv); |
309 return; | 310 return; |
310 } | 311 } |
311 | 312 |
312 // Fetch a request using the experimental context. | 313 // Fetch a request using the experimental context. |
313 request_.reset(new URLRequest(experiment.url, this)); | 314 request_.reset(new net::URLRequest(experiment.url, this)); |
314 request_->set_context(context); | 315 request_->set_context(context); |
315 request_->Start(); | 316 request_->Start(); |
316 } | 317 } |
317 | 318 |
318 // ConnectionTester ---------------------------------------------------------- | 319 // ConnectionTester ---------------------------------------------------------- |
319 | 320 |
320 ConnectionTester::ConnectionTester(Delegate* delegate, IOThread* io_thread) | 321 ConnectionTester::ConnectionTester(Delegate* delegate, IOThread* io_thread) |
321 : delegate_(delegate), io_thread_(io_thread) { | 322 : delegate_(delegate), io_thread_(io_thread) { |
322 DCHECK(delegate); | 323 DCHECK(delegate); |
323 DCHECK(io_thread); | 324 DCHECK(io_thread); |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
411 | 412 |
412 // Notify the delegate of completion. | 413 // Notify the delegate of completion. |
413 delegate_->OnCompletedConnectionTestExperiment(current, result); | 414 delegate_->OnCompletedConnectionTestExperiment(current, result); |
414 | 415 |
415 if (remaining_experiments_.empty()) { | 416 if (remaining_experiments_.empty()) { |
416 delegate_->OnCompletedConnectionTestSuite(); | 417 delegate_->OnCompletedConnectionTestSuite(); |
417 } else { | 418 } else { |
418 StartNextExperiment(); | 419 StartNextExperiment(); |
419 } | 420 } |
420 } | 421 } |
OLD | NEW |