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 #include <utility> | 9 #include <utility> |
9 | 10 |
10 #include "base/logging.h" | 11 #include "base/logging.h" |
11 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/ptr_util.h" |
12 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
13 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
14 #include "base/sequenced_task_runner.h" | 16 #include "base/sequenced_task_runner.h" |
15 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
16 #include "base/threading/sequenced_task_runner_handle.h" | 18 #include "base/threading/sequenced_task_runner_handle.h" |
17 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
18 #include "net/base/io_buffer.h" | 20 #include "net/base/io_buffer.h" |
19 #include "net/http/http_request_headers.h" | 21 #include "net/http/http_request_headers.h" |
20 #include "net/http/http_response_headers.h" | 22 #include "net/http/http_response_headers.h" |
21 #include "net/url_request/url_request_filter.h" | 23 #include "net/url_request/url_request_filter.h" |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 | 102 |
101 // URLRequestJob | 103 // URLRequestJob |
102 void Start() override; | 104 void Start() override; |
103 void GetResponseInfo(net::HttpResponseInfo* response_info) override; | 105 void GetResponseInfo(net::HttpResponseInfo* response_info) override; |
104 int64_t GetTotalReceivedBytes() const override; | 106 int64_t GetTotalReceivedBytes() const override; |
105 bool GetMimeType(std::string* mime_type) const override; | 107 bool GetMimeType(std::string* mime_type) const override; |
106 int GetResponseCode() const override; | 108 int GetResponseCode() const override; |
107 int ReadRawData(net::IOBuffer* buf, int buf_size) override; | 109 int ReadRawData(net::IOBuffer* buf, int buf_size) override; |
108 | 110 |
109 private: | 111 private: |
110 PartialResponseJob(scoped_ptr<Parameters> parameters, | 112 PartialResponseJob(std::unique_ptr<Parameters> parameters, |
111 base::WeakPtr<Interceptor> interceptor, | 113 base::WeakPtr<Interceptor> interceptor, |
112 net::URLRequest* url_request, | 114 net::URLRequest* url_request, |
113 net::NetworkDelegate* network_delegate); | 115 net::NetworkDelegate* network_delegate); |
114 | 116 |
115 ~PartialResponseJob() override; | 117 ~PartialResponseJob() override; |
116 void ReportCompletedRequest(); | 118 void ReportCompletedRequest(); |
117 static void OnStartResponseCallbackOnPossiblyIncorrectThread( | 119 static void OnStartResponseCallbackOnPossiblyIncorrectThread( |
118 base::WeakPtr<PartialResponseJob> job, | 120 base::WeakPtr<PartialResponseJob> job, |
119 const std::string& headers, | 121 const std::string& headers, |
120 net::Error error); | 122 net::Error error); |
(...skipping 14 matching lines...) Expand all Loading... |
135 // It also adds an 'Accept-Ranges' header if appropriate. | 137 // It also adds an 'Accept-Ranges' header if appropriate. |
136 void AddCommonEntityHeaders(); | 138 void AddCommonEntityHeaders(); |
137 | 139 |
138 // Schedules NotifyHeadersComplete() to be called and sets | 140 // Schedules NotifyHeadersComplete() to be called and sets |
139 // offset_of_next_read_ to begin reading. Since this interceptor is avoiding | 141 // offset_of_next_read_ to begin reading. Since this interceptor is avoiding |
140 // network requests and hence may complete synchronously, it schedules the | 142 // network requests and hence may complete synchronously, it schedules the |
141 // NotifyHeadersComplete() call asynchronously in order to avoid unexpected | 143 // NotifyHeadersComplete() call asynchronously in order to avoid unexpected |
142 // re-entrancy. | 144 // re-entrancy. |
143 void NotifyHeadersCompleteAndPrepareToRead(); | 145 void NotifyHeadersCompleteAndPrepareToRead(); |
144 | 146 |
145 scoped_ptr<Parameters> parameters_; | 147 std::unique_ptr<Parameters> parameters_; |
146 | 148 |
147 base::WeakPtr<Interceptor> interceptor_; | 149 base::WeakPtr<Interceptor> interceptor_; |
148 net::HttpResponseInfo response_info_; | 150 net::HttpResponseInfo response_info_; |
149 int64_t offset_of_next_read_ = -1; | 151 int64_t offset_of_next_read_ = -1; |
150 int64_t requested_range_begin_ = -1; | 152 int64_t requested_range_begin_ = -1; |
151 int64_t requested_range_end_ = -1; | 153 int64_t requested_range_end_ = -1; |
152 int64_t read_byte_count_ = 0; | 154 int64_t read_byte_count_ = 0; |
153 base::WeakPtrFactory<PartialResponseJob> weak_factory_; | 155 base::WeakPtrFactory<PartialResponseJob> weak_factory_; |
154 | 156 |
155 DISALLOW_COPY_AND_ASSIGN(PartialResponseJob); | 157 DISALLOW_COPY_AND_ASSIGN(PartialResponseJob); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 } | 200 } |
199 | 201 |
200 } // namespace | 202 } // namespace |
201 | 203 |
202 // static | 204 // static |
203 net::URLRequestJob* TestDownloadRequestHandler::PartialResponseJob::Factory( | 205 net::URLRequestJob* TestDownloadRequestHandler::PartialResponseJob::Factory( |
204 const Parameters& parameters, | 206 const Parameters& parameters, |
205 net::URLRequest* request, | 207 net::URLRequest* request, |
206 net::NetworkDelegate* delegate, | 208 net::NetworkDelegate* delegate, |
207 base::WeakPtr<Interceptor> interceptor) { | 209 base::WeakPtr<Interceptor> interceptor) { |
208 return new PartialResponseJob(make_scoped_ptr(new Parameters(parameters)), | 210 return new PartialResponseJob(base::WrapUnique(new Parameters(parameters)), |
209 interceptor, request, delegate); | 211 interceptor, request, delegate); |
210 } | 212 } |
211 | 213 |
212 TestDownloadRequestHandler::PartialResponseJob::PartialResponseJob( | 214 TestDownloadRequestHandler::PartialResponseJob::PartialResponseJob( |
213 scoped_ptr<Parameters> parameters, | 215 std::unique_ptr<Parameters> parameters, |
214 base::WeakPtr<Interceptor> interceptor, | 216 base::WeakPtr<Interceptor> interceptor, |
215 net::URLRequest* request, | 217 net::URLRequest* request, |
216 net::NetworkDelegate* network_delegate) | 218 net::NetworkDelegate* network_delegate) |
217 : net::URLRequestJob(request, network_delegate), | 219 : net::URLRequestJob(request, network_delegate), |
218 parameters_(std::move(parameters)), | 220 parameters_(std::move(parameters)), |
219 interceptor_(interceptor), | 221 interceptor_(interceptor), |
220 weak_factory_(this) { | 222 weak_factory_(this) { |
221 DCHECK(parameters_.get()); | 223 DCHECK(parameters_.get()); |
222 DCHECK_LT(0, parameters_->size); | 224 DCHECK_LT(0, parameters_->size); |
223 DCHECK_NE(-1, parameters_->pattern_generator_seed); | 225 DCHECK_NE(-1, parameters_->pattern_generator_seed); |
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
478 FROM_HERE, base::Bind(&PartialResponseJob::NotifyHeadersComplete, | 480 FROM_HERE, base::Bind(&PartialResponseJob::NotifyHeadersComplete, |
479 weak_factory_.GetWeakPtr())); | 481 weak_factory_.GetWeakPtr())); |
480 } | 482 } |
481 | 483 |
482 // static | 484 // static |
483 base::WeakPtr<TestDownloadRequestHandler::Interceptor> | 485 base::WeakPtr<TestDownloadRequestHandler::Interceptor> |
484 TestDownloadRequestHandler::Interceptor::Register( | 486 TestDownloadRequestHandler::Interceptor::Register( |
485 const GURL& url, | 487 const GURL& url, |
486 scoped_refptr<base::SequencedTaskRunner> client_task_runner) { | 488 scoped_refptr<base::SequencedTaskRunner> client_task_runner) { |
487 DCHECK(url.is_valid()); | 489 DCHECK(url.is_valid()); |
488 scoped_ptr<Interceptor> interceptor(new Interceptor(url, client_task_runner)); | 490 std::unique_ptr<Interceptor> interceptor( |
| 491 new Interceptor(url, client_task_runner)); |
489 base::WeakPtr<Interceptor> weak_reference = | 492 base::WeakPtr<Interceptor> weak_reference = |
490 interceptor->weak_ptr_factory_.GetWeakPtr(); | 493 interceptor->weak_ptr_factory_.GetWeakPtr(); |
491 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); | 494 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); |
492 filter->AddUrlInterceptor(url, std::move(interceptor)); | 495 filter->AddUrlInterceptor(url, std::move(interceptor)); |
493 return weak_reference; | 496 return weak_reference; |
494 } | 497 } |
495 | 498 |
496 void TestDownloadRequestHandler::Interceptor::Unregister() { | 499 void TestDownloadRequestHandler::Interceptor::Unregister() { |
497 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); | 500 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); |
498 filter->RemoveUrlHandler(url_); | 501 filter->RemoveUrlHandler(url_); |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
666 base::RunLoop run_loop; | 669 base::RunLoop run_loop; |
667 BrowserThread::PostTaskAndReply( | 670 BrowserThread::PostTaskAndReply( |
668 BrowserThread::IO, FROM_HERE, | 671 BrowserThread::IO, FROM_HERE, |
669 base::Bind(&Interceptor::GetAndResetCompletedRequests, interceptor_, | 672 base::Bind(&Interceptor::GetAndResetCompletedRequests, interceptor_, |
670 requests), | 673 requests), |
671 run_loop.QuitClosure()); | 674 run_loop.QuitClosure()); |
672 run_loop.Run(); | 675 run_loop.Run(); |
673 } | 676 } |
674 | 677 |
675 } // namespace content | 678 } // namespace content |
OLD | NEW |