Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "content/public/test/test_download_request_handler.h" | 5 #include "content/public/test/test_download_request_handler.h" |
| 6 | 6 |
| 7 #include <inttypes.h> | 7 #include <inttypes.h> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 142 // re-entrancy. | 142 // re-entrancy. |
| 143 void NotifyHeadersCompleteAndPrepareToRead(); | 143 void NotifyHeadersCompleteAndPrepareToRead(); |
| 144 | 144 |
| 145 scoped_ptr<Parameters> parameters_; | 145 scoped_ptr<Parameters> parameters_; |
| 146 | 146 |
| 147 base::WeakPtr<Interceptor> interceptor_; | 147 base::WeakPtr<Interceptor> interceptor_; |
| 148 net::HttpResponseInfo response_info_; | 148 net::HttpResponseInfo response_info_; |
| 149 int64_t offset_of_next_read_ = -1; | 149 int64_t offset_of_next_read_ = -1; |
| 150 int64_t requested_range_begin_ = -1; | 150 int64_t requested_range_begin_ = -1; |
| 151 int64_t requested_range_end_ = -1; | 151 int64_t requested_range_end_ = -1; |
| 152 bool should_report_completed_request_ = false; | |
| 152 base::WeakPtrFactory<PartialResponseJob> weak_factory_; | 153 base::WeakPtrFactory<PartialResponseJob> weak_factory_; |
| 153 | 154 |
| 154 DISALLOW_COPY_AND_ASSIGN(PartialResponseJob); | 155 DISALLOW_COPY_AND_ASSIGN(PartialResponseJob); |
| 155 }; | 156 }; |
| 156 | 157 |
| 157 namespace { | 158 namespace { |
| 158 | 159 |
| 159 template <class T> | 160 template <class T> |
| 160 void StoreValueAndInvokeClosure(const base::Closure& closure, | 161 void StoreValueAndInvokeClosure(const base::Closure& closure, |
| 161 T* value_receiver, | 162 T* value_receiver, |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 215 net::NetworkDelegate* network_delegate) | 216 net::NetworkDelegate* network_delegate) |
| 216 : net::URLRequestJob(request, network_delegate), | 217 : net::URLRequestJob(request, network_delegate), |
| 217 parameters_(std::move(parameters)), | 218 parameters_(std::move(parameters)), |
| 218 interceptor_(interceptor), | 219 interceptor_(interceptor), |
| 219 weak_factory_(this) { | 220 weak_factory_(this) { |
| 220 DCHECK(parameters_.get()); | 221 DCHECK(parameters_.get()); |
| 221 DCHECK_LT(0, parameters_->size); | 222 DCHECK_LT(0, parameters_->size); |
| 222 DCHECK_NE(-1, parameters_->pattern_generator_seed); | 223 DCHECK_NE(-1, parameters_->pattern_generator_seed); |
| 223 } | 224 } |
| 224 | 225 |
| 225 TestDownloadRequestHandler::PartialResponseJob::~PartialResponseJob() {} | 226 TestDownloadRequestHandler::PartialResponseJob::~PartialResponseJob() { |
| 227 if (should_report_completed_request_) | |
| 228 ReportCompletedRequest(0); | |
| 229 } | |
| 226 | 230 |
| 227 void TestDownloadRequestHandler::PartialResponseJob::Start() { | 231 void TestDownloadRequestHandler::PartialResponseJob::Start() { |
| 228 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 232 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 229 DVLOG(1) << "Starting request for " << request()->url().spec(); | 233 DVLOG(1) << "Starting request for " << request()->url().spec(); |
| 230 | 234 |
| 231 if (parameters_->on_start_handler.is_null() || !interceptor_.get()) { | 235 if (parameters_->on_start_handler.is_null() || !interceptor_.get()) { |
| 232 HandleOnStartDefault(); | 236 HandleOnStartDefault(); |
| 233 return; | 237 return; |
| 234 } | 238 } |
| 235 | 239 |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 302 parameters_->pattern_generator_seed, offset_of_next_read_, bytes_to_copy, | 306 parameters_->pattern_generator_seed, offset_of_next_read_, bytes_to_copy, |
| 303 buf->data()); | 307 buf->data()); |
| 304 DVLOG(1) << "Read " << bytes_to_copy << " bytes at offset " | 308 DVLOG(1) << "Read " << bytes_to_copy << " bytes at offset " |
| 305 << offset_of_next_read_; | 309 << offset_of_next_read_; |
| 306 offset_of_next_read_ += bytes_to_copy; | 310 offset_of_next_read_ += bytes_to_copy; |
| 307 return bytes_to_copy; | 311 return bytes_to_copy; |
| 308 } | 312 } |
| 309 | 313 |
| 310 void TestDownloadRequestHandler::PartialResponseJob::ReportCompletedRequest( | 314 void TestDownloadRequestHandler::PartialResponseJob::ReportCompletedRequest( |
| 311 int64_t transferred_byte_count) { | 315 int64_t transferred_byte_count) { |
| 316 should_report_completed_request_ = false; | |
|
svaldez
2016/01/13 17:29:18
Seems unnecessary since we only call this on destr
asanka
2016/01/28 02:24:17
We also call ReportCompletedRequest when the URLRe
| |
| 312 if (interceptor_.get()) { | 317 if (interceptor_.get()) { |
| 313 TestDownloadRequestHandler::CompletedRequest completed_request; | 318 TestDownloadRequestHandler::CompletedRequest completed_request; |
| 314 completed_request.transferred_byte_count = transferred_byte_count; | 319 completed_request.transferred_byte_count = transferred_byte_count; |
| 315 completed_request.request_headers = request()->extra_request_headers(); | 320 completed_request.request_headers = request()->extra_request_headers(); |
| 316 interceptor_->AddCompletedRequest(completed_request); | 321 interceptor_->AddCompletedRequest(completed_request); |
| 317 } | 322 } |
| 318 } | 323 } |
| 319 | 324 |
| 320 // static | 325 // static |
| 321 void TestDownloadRequestHandler::PartialResponseJob:: | 326 void TestDownloadRequestHandler::PartialResponseJob:: |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 467 // prevents injecting an error at offset 0, it makes it much easier to set up | 472 // prevents injecting an error at offset 0, it makes it much easier to set up |
| 468 // parameter sets for download resumption. It means that when a request is | 473 // parameter sets for download resumption. It means that when a request is |
| 469 // interrupted at offset O, a subsequent request for the range O-<end> won't | 474 // interrupted at offset O, a subsequent request for the range O-<end> won't |
| 470 // immediately interrupt as well. If we don't exclude interruptions at | 475 // immediately interrupt as well. If we don't exclude interruptions at |
| 471 // relative offset 0, then test writers would need to reset the parameter | 476 // relative offset 0, then test writers would need to reset the parameter |
| 472 // prior to each resumption rather than once at the beginning of the test. | 477 // prior to each resumption rather than once at the beginning of the test. |
| 473 while (!parameters_->injected_errors.empty() && | 478 while (!parameters_->injected_errors.empty() && |
| 474 parameters_->injected_errors.front().offset <= requested_range_begin_) | 479 parameters_->injected_errors.front().offset <= requested_range_begin_) |
| 475 parameters_->injected_errors.pop(); | 480 parameters_->injected_errors.pop(); |
| 476 | 481 |
| 482 // From this point on, the request is considered successful. | |
| 483 should_report_completed_request_ = true; | |
| 484 | |
| 477 base::MessageLoop::current()->PostTask( | 485 base::MessageLoop::current()->PostTask( |
| 478 FROM_HERE, base::Bind(&PartialResponseJob::NotifyHeadersComplete, | 486 FROM_HERE, base::Bind(&PartialResponseJob::NotifyHeadersComplete, |
| 479 weak_factory_.GetWeakPtr())); | 487 weak_factory_.GetWeakPtr())); |
| 480 } | 488 } |
| 481 | 489 |
| 482 // static | 490 // static |
| 483 base::WeakPtr<TestDownloadRequestHandler::Interceptor> | 491 base::WeakPtr<TestDownloadRequestHandler::Interceptor> |
| 484 TestDownloadRequestHandler::Interceptor::Register( | 492 TestDownloadRequestHandler::Interceptor::Register( |
| 485 const GURL& url, | 493 const GURL& url, |
| 486 scoped_refptr<base::SequencedTaskRunner> client_task_runner) { | 494 scoped_refptr<base::SequencedTaskRunner> client_task_runner) { |
| (...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 664 base::RunLoop run_loop; | 672 base::RunLoop run_loop; |
| 665 BrowserThread::PostTaskAndReply( | 673 BrowserThread::PostTaskAndReply( |
| 666 BrowserThread::IO, FROM_HERE, | 674 BrowserThread::IO, FROM_HERE, |
| 667 base::Bind(&Interceptor::GetAndResetCompletedRequests, interceptor_, | 675 base::Bind(&Interceptor::GetAndResetCompletedRequests, interceptor_, |
| 668 requests), | 676 requests), |
| 669 run_loop.QuitClosure()); | 677 run_loop.QuitClosure()); |
| 670 run_loop.Run(); | 678 run_loop.Run(); |
| 671 } | 679 } |
| 672 | 680 |
| 673 } // namespace content | 681 } // namespace content |
| OLD | NEW |