| 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 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 int GetResponseCode() const override; | 106 int GetResponseCode() const override; |
| 107 int ReadRawData(net::IOBuffer* buf, int buf_size) override; | 107 int ReadRawData(net::IOBuffer* buf, int buf_size) override; |
| 108 | 108 |
| 109 private: | 109 private: |
| 110 PartialResponseJob(scoped_ptr<Parameters> parameters, | 110 PartialResponseJob(scoped_ptr<Parameters> parameters, |
| 111 base::WeakPtr<Interceptor> interceptor, | 111 base::WeakPtr<Interceptor> interceptor, |
| 112 net::URLRequest* url_request, | 112 net::URLRequest* url_request, |
| 113 net::NetworkDelegate* network_delegate); | 113 net::NetworkDelegate* network_delegate); |
| 114 | 114 |
| 115 ~PartialResponseJob() override; | 115 ~PartialResponseJob() override; |
| 116 void ReportCompletedRequest(int64_t transferred_byte_count); | 116 void ReportCompletedRequest(); |
| 117 static void OnStartResponseCallbackOnPossiblyIncorrectThread( | 117 static void OnStartResponseCallbackOnPossiblyIncorrectThread( |
| 118 base::WeakPtr<PartialResponseJob> job, | 118 base::WeakPtr<PartialResponseJob> job, |
| 119 const std::string& headers, | 119 const std::string& headers, |
| 120 net::Error error); | 120 net::Error error); |
| 121 void OnStartResponseCallback(const std::string& headers, net::Error error); | 121 void OnStartResponseCallback(const std::string& headers, net::Error error); |
| 122 | 122 |
| 123 // In general, the Parameters object can specify an explicit OnStart handler. | 123 // In general, the Parameters object can specify an explicit OnStart handler. |
| 124 // In its absence or if the explicit OnStart handler requests the default | 124 // In its absence or if the explicit OnStart handler requests the default |
| 125 // behavior, this method can be invoked to respond to the request based on the | 125 // behavior, this method can be invoked to respond to the request based on the |
| 126 // remaining Parameters fields (as if there was no OnStart handler). | 126 // remaining Parameters fields (as if there was no OnStart handler). |
| (...skipping 15 matching lines...) Expand all 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 int64_t read_byte_count_ = 0; |
| 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 ReportCompletedRequest(); |
| 228 } |
| 226 | 229 |
| 227 void TestDownloadRequestHandler::PartialResponseJob::Start() { | 230 void TestDownloadRequestHandler::PartialResponseJob::Start() { |
| 228 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 231 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 229 DVLOG(1) << "Starting request for " << request()->url().spec(); | 232 DVLOG(1) << "Starting request for " << request()->url().spec(); |
| 230 | 233 |
| 231 if (parameters_->on_start_handler.is_null() || !interceptor_.get()) { | 234 if (parameters_->on_start_handler.is_null() || !interceptor_.get()) { |
| 232 HandleOnStartDefault(); | 235 HandleOnStartDefault(); |
| 233 return; | 236 return; |
| 234 } | 237 } |
| 235 | 238 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 265 } | 268 } |
| 266 | 269 |
| 267 int TestDownloadRequestHandler::PartialResponseJob::ReadRawData( | 270 int TestDownloadRequestHandler::PartialResponseJob::ReadRawData( |
| 268 net::IOBuffer* buf, | 271 net::IOBuffer* buf, |
| 269 int buf_size) { | 272 int buf_size) { |
| 270 DVLOG(1) << "Preparing to read " << buf_size << " bytes"; | 273 DVLOG(1) << "Preparing to read " << buf_size << " bytes"; |
| 271 | 274 |
| 272 // requested_range_begin_ == -1 implies that the body was empty. | 275 // requested_range_begin_ == -1 implies that the body was empty. |
| 273 if (offset_of_next_read_ > requested_range_end_ || | 276 if (offset_of_next_read_ > requested_range_end_ || |
| 274 requested_range_begin_ == -1) { | 277 requested_range_begin_ == -1) { |
| 275 ReportCompletedRequest(requested_range_end_ - requested_range_begin_ + 1); | |
| 276 DVLOG(1) << "Done reading."; | 278 DVLOG(1) << "Done reading."; |
| 277 return 0; | 279 return 0; |
| 278 } | 280 } |
| 279 | 281 |
| 280 int64_t range_end = | 282 int64_t range_end = |
| 281 std::min(requested_range_end_, offset_of_next_read_ + buf_size - 1); | 283 std::min(requested_range_end_, offset_of_next_read_ + buf_size - 1); |
| 282 | 284 |
| 283 if (!parameters_->injected_errors.empty()) { | 285 if (!parameters_->injected_errors.empty()) { |
| 284 const InjectedError& injected_error = parameters_->injected_errors.front(); | 286 const InjectedError& injected_error = parameters_->injected_errors.front(); |
| 285 | 287 |
| 286 if (offset_of_next_read_ == injected_error.offset) { | 288 if (offset_of_next_read_ == injected_error.offset) { |
| 287 int error = injected_error.error; | 289 int error = injected_error.error; |
| 288 DVLOG(1) << "Returning error " << net::ErrorToString(error); | 290 DVLOG(1) << "Returning error " << net::ErrorToString(error); |
| 289 ReportCompletedRequest(injected_error.offset - requested_range_begin_); | |
| 290 parameters_->injected_errors.pop(); | 291 parameters_->injected_errors.pop(); |
| 291 return error; | 292 return error; |
| 292 } | 293 } |
| 293 | 294 |
| 294 if (offset_of_next_read_ < injected_error.offset && | 295 if (offset_of_next_read_ < injected_error.offset && |
| 295 injected_error.offset <= range_end) | 296 injected_error.offset <= range_end) |
| 296 range_end = injected_error.offset - 1; | 297 range_end = injected_error.offset - 1; |
| 297 } | 298 } |
| 298 int bytes_to_copy = (range_end - offset_of_next_read_) + 1; | 299 int bytes_to_copy = (range_end - offset_of_next_read_) + 1; |
| 299 | 300 |
| 300 TestDownloadRequestHandler::GetPatternBytes( | 301 TestDownloadRequestHandler::GetPatternBytes( |
| 301 parameters_->pattern_generator_seed, offset_of_next_read_, bytes_to_copy, | 302 parameters_->pattern_generator_seed, offset_of_next_read_, bytes_to_copy, |
| 302 buf->data()); | 303 buf->data()); |
| 303 DVLOG(1) << "Read " << bytes_to_copy << " bytes at offset " | 304 DVLOG(1) << "Read " << bytes_to_copy << " bytes at offset " |
| 304 << offset_of_next_read_; | 305 << offset_of_next_read_; |
| 305 offset_of_next_read_ += bytes_to_copy; | 306 offset_of_next_read_ += bytes_to_copy; |
| 307 read_byte_count_ += bytes_to_copy; |
| 306 return bytes_to_copy; | 308 return bytes_to_copy; |
| 307 } | 309 } |
| 308 | 310 |
| 309 void TestDownloadRequestHandler::PartialResponseJob::ReportCompletedRequest( | 311 void TestDownloadRequestHandler::PartialResponseJob::ReportCompletedRequest() { |
| 310 int64_t transferred_byte_count) { | |
| 311 if (interceptor_.get()) { | 312 if (interceptor_.get()) { |
| 312 TestDownloadRequestHandler::CompletedRequest completed_request; | 313 TestDownloadRequestHandler::CompletedRequest completed_request; |
| 313 completed_request.transferred_byte_count = transferred_byte_count; | 314 completed_request.transferred_byte_count = read_byte_count_; |
| 314 completed_request.request_headers = request()->extra_request_headers(); | 315 completed_request.request_headers = request()->extra_request_headers(); |
| 315 interceptor_->AddCompletedRequest(completed_request); | 316 interceptor_->AddCompletedRequest(completed_request); |
| 316 } | 317 } |
| 317 } | 318 } |
| 318 | 319 |
| 319 // static | 320 // static |
| 320 void TestDownloadRequestHandler::PartialResponseJob:: | 321 void TestDownloadRequestHandler::PartialResponseJob:: |
| 321 OnStartResponseCallbackOnPossiblyIncorrectThread( | 322 OnStartResponseCallbackOnPossiblyIncorrectThread( |
| 322 base::WeakPtr<PartialResponseJob> job, | 323 base::WeakPtr<PartialResponseJob> job, |
| 323 const std::string& headers, | 324 const std::string& headers, |
| (...skipping 339 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 663 base::RunLoop run_loop; | 664 base::RunLoop run_loop; |
| 664 BrowserThread::PostTaskAndReply( | 665 BrowserThread::PostTaskAndReply( |
| 665 BrowserThread::IO, FROM_HERE, | 666 BrowserThread::IO, FROM_HERE, |
| 666 base::Bind(&Interceptor::GetAndResetCompletedRequests, interceptor_, | 667 base::Bind(&Interceptor::GetAndResetCompletedRequests, interceptor_, |
| 667 requests), | 668 requests), |
| 668 run_loop.QuitClosure()); | 669 run_loop.QuitClosure()); |
| 669 run_loop.Run(); | 670 run_loop.Run(); |
| 670 } | 671 } |
| 671 | 672 |
| 672 } // namespace content | 673 } // namespace content |
| OLD | NEW |