| 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 | 8 |
| 9 #include <memory> |
| 9 #include <utility> | 10 #include <utility> |
| 10 | 11 |
| 11 #include "base/logging.h" | 12 #include "base/logging.h" |
| 12 #include "base/macros.h" | 13 #include "base/macros.h" |
| 13 #include "base/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
| 14 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
| 15 #include "base/run_loop.h" | 16 #include "base/run_loop.h" |
| 16 #include "base/sequenced_task_runner.h" | 17 #include "base/sequenced_task_runner.h" |
| 17 #include "base/strings/stringprintf.h" | 18 #include "base/strings/stringprintf.h" |
| 18 #include "base/threading/sequenced_task_runner_handle.h" | 19 #include "base/threading/sequenced_task_runner_handle.h" |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 | 57 |
| 57 // Sets |requests| to the vector of completed requests and clears the internal | 58 // Sets |requests| to the vector of completed requests and clears the internal |
| 58 // list. The returned requests are stored in the order in which they were | 59 // list. The returned requests are stored in the order in which they were |
| 59 // reported as being complete (not necessarily the order in which they were | 60 // reported as being complete (not necessarily the order in which they were |
| 60 // received). | 61 // received). |
| 61 void GetAndResetCompletedRequests( | 62 void GetAndResetCompletedRequests( |
| 62 TestDownloadRequestHandler::CompletedRequests* requests); | 63 TestDownloadRequestHandler::CompletedRequests* requests); |
| 63 | 64 |
| 64 // Can be called by a URLRequestJob to notify this interceptor of a completed | 65 // Can be called by a URLRequestJob to notify this interceptor of a completed |
| 65 // request. | 66 // request. |
| 66 void AddCompletedRequest( | 67 void AddCompletedRequest(std::unique_ptr<CompletedRequest> request); |
| 67 const TestDownloadRequestHandler::CompletedRequest& request); | |
| 68 | 68 |
| 69 // Returns the task runner that should be used for invoking any client | 69 // Returns the task runner that should be used for invoking any client |
| 70 // supplied callbacks. | 70 // supplied callbacks. |
| 71 scoped_refptr<base::SequencedTaskRunner> GetClientTaskRunner(); | 71 scoped_refptr<base::SequencedTaskRunner> GetClientTaskRunner(); |
| 72 | 72 |
| 73 private: | 73 private: |
| 74 Interceptor(const GURL& url, | 74 Interceptor(const GURL& url, |
| 75 scoped_refptr<base::SequencedTaskRunner> client_task_runner); | 75 scoped_refptr<base::SequencedTaskRunner> client_task_runner); |
| 76 | 76 |
| 77 // net::URLRequestInterceptor | 77 // net::URLRequestInterceptor |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 scoped_refptr<net::HttpResponseHeaders> HeadersFromString( | 194 scoped_refptr<net::HttpResponseHeaders> HeadersFromString( |
| 195 const std::string& headers_string) { | 195 const std::string& headers_string) { |
| 196 scoped_refptr<net::HttpResponseHeaders> headers = | 196 scoped_refptr<net::HttpResponseHeaders> headers = |
| 197 new net::HttpResponseHeaders(net::HttpUtil::AssembleRawHeaders( | 197 new net::HttpResponseHeaders(net::HttpUtil::AssembleRawHeaders( |
| 198 headers_string.c_str(), headers_string.size())); | 198 headers_string.c_str(), headers_string.size())); |
| 199 return headers; | 199 return headers; |
| 200 } | 200 } |
| 201 | 201 |
| 202 } // namespace | 202 } // namespace |
| 203 | 203 |
| 204 TestDownloadRequestHandler::CompletedRequest::CompletedRequest() {} |
| 205 |
| 206 TestDownloadRequestHandler::CompletedRequest::~CompletedRequest() {} |
| 207 |
| 208 TestDownloadRequestHandler::CompletedRequest::CompletedRequest( |
| 209 CompletedRequest&&) = default; |
| 210 |
| 204 // static | 211 // static |
| 205 net::URLRequestJob* TestDownloadRequestHandler::PartialResponseJob::Factory( | 212 net::URLRequestJob* TestDownloadRequestHandler::PartialResponseJob::Factory( |
| 206 const Parameters& parameters, | 213 const Parameters& parameters, |
| 207 net::URLRequest* request, | 214 net::URLRequest* request, |
| 208 net::NetworkDelegate* delegate, | 215 net::NetworkDelegate* delegate, |
| 209 base::WeakPtr<Interceptor> interceptor) { | 216 base::WeakPtr<Interceptor> interceptor) { |
| 210 return new PartialResponseJob(base::WrapUnique(new Parameters(parameters)), | 217 return new PartialResponseJob(base::WrapUnique(new Parameters(parameters)), |
| 211 interceptor, request, delegate); | 218 interceptor, request, delegate); |
| 212 } | 219 } |
| 213 | 220 |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 305 buf->data()); | 312 buf->data()); |
| 306 DVLOG(1) << "Read " << bytes_to_copy << " bytes at offset " | 313 DVLOG(1) << "Read " << bytes_to_copy << " bytes at offset " |
| 307 << offset_of_next_read_; | 314 << offset_of_next_read_; |
| 308 offset_of_next_read_ += bytes_to_copy; | 315 offset_of_next_read_ += bytes_to_copy; |
| 309 read_byte_count_ += bytes_to_copy; | 316 read_byte_count_ += bytes_to_copy; |
| 310 return bytes_to_copy; | 317 return bytes_to_copy; |
| 311 } | 318 } |
| 312 | 319 |
| 313 void TestDownloadRequestHandler::PartialResponseJob::ReportCompletedRequest() { | 320 void TestDownloadRequestHandler::PartialResponseJob::ReportCompletedRequest() { |
| 314 if (interceptor_.get()) { | 321 if (interceptor_.get()) { |
| 315 TestDownloadRequestHandler::CompletedRequest completed_request; | 322 std::unique_ptr<CompletedRequest> completed_request(new CompletedRequest); |
| 316 completed_request.transferred_byte_count = read_byte_count_; | 323 completed_request->transferred_byte_count = read_byte_count_; |
| 317 completed_request.request_headers = request()->extra_request_headers(); | 324 completed_request->request_headers = request()->extra_request_headers(); |
| 318 interceptor_->AddCompletedRequest(completed_request); | 325 completed_request->referrer = request()->referrer(); |
| 326 completed_request->referrer_policy = request()->referrer_policy(); |
| 327 completed_request->initiator = request()->initiator(); |
| 328 completed_request->first_party_for_cookies = |
| 329 request()->first_party_for_cookies(); |
| 330 completed_request->first_party_url_policy = |
| 331 request()->first_party_url_policy(); |
| 332 interceptor_->AddCompletedRequest(std::move(completed_request)); |
| 319 } | 333 } |
| 320 } | 334 } |
| 321 | 335 |
| 322 // static | 336 // static |
| 323 void TestDownloadRequestHandler::PartialResponseJob:: | 337 void TestDownloadRequestHandler::PartialResponseJob:: |
| 324 OnStartResponseCallbackOnPossiblyIncorrectThread( | 338 OnStartResponseCallbackOnPossiblyIncorrectThread( |
| 325 base::WeakPtr<PartialResponseJob> job, | 339 base::WeakPtr<PartialResponseJob> job, |
| 326 const std::string& headers, | 340 const std::string& headers, |
| 327 net::Error error) { | 341 net::Error error) { |
| 328 BrowserThread::PostTask( | 342 BrowserThread::PostTask( |
| (...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 508 job_factory_ = job_factory; | 522 job_factory_ = job_factory; |
| 509 } | 523 } |
| 510 | 524 |
| 511 void TestDownloadRequestHandler::Interceptor::GetAndResetCompletedRequests( | 525 void TestDownloadRequestHandler::Interceptor::GetAndResetCompletedRequests( |
| 512 TestDownloadRequestHandler::CompletedRequests* requests) { | 526 TestDownloadRequestHandler::CompletedRequests* requests) { |
| 513 requests->clear(); | 527 requests->clear(); |
| 514 completed_requests_.swap(*requests); | 528 completed_requests_.swap(*requests); |
| 515 } | 529 } |
| 516 | 530 |
| 517 void TestDownloadRequestHandler::Interceptor::AddCompletedRequest( | 531 void TestDownloadRequestHandler::Interceptor::AddCompletedRequest( |
| 518 const TestDownloadRequestHandler::CompletedRequest& request) { | 532 std::unique_ptr<CompletedRequest> request) { |
| 519 completed_requests_.push_back(request); | 533 completed_requests_.push_back(std::move(request)); |
| 520 } | 534 } |
| 521 | 535 |
| 522 scoped_refptr<base::SequencedTaskRunner> | 536 scoped_refptr<base::SequencedTaskRunner> |
| 523 TestDownloadRequestHandler::Interceptor::GetClientTaskRunner() { | 537 TestDownloadRequestHandler::Interceptor::GetClientTaskRunner() { |
| 524 return client_task_runner_; | 538 return client_task_runner_; |
| 525 } | 539 } |
| 526 | 540 |
| 527 TestDownloadRequestHandler::Interceptor::Interceptor( | 541 TestDownloadRequestHandler::Interceptor::Interceptor( |
| 528 const GURL& url, | 542 const GURL& url, |
| 529 scoped_refptr<base::SequencedTaskRunner> client_task_runner) | 543 scoped_refptr<base::SequencedTaskRunner> client_task_runner) |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 670 base::RunLoop run_loop; | 684 base::RunLoop run_loop; |
| 671 BrowserThread::PostTaskAndReply( | 685 BrowserThread::PostTaskAndReply( |
| 672 BrowserThread::IO, FROM_HERE, | 686 BrowserThread::IO, FROM_HERE, |
| 673 base::Bind(&Interceptor::GetAndResetCompletedRequests, interceptor_, | 687 base::Bind(&Interceptor::GetAndResetCompletedRequests, interceptor_, |
| 674 requests), | 688 requests), |
| 675 run_loop.QuitClosure()); | 689 run_loop.QuitClosure()); |
| 676 run_loop.Run(); | 690 run_loop.Run(); |
| 677 } | 691 } |
| 678 | 692 |
| 679 } // namespace content | 693 } // namespace content |
| OLD | NEW |