OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "headless/public/util/deterministic_http_protocol_handler.h" |
| 6 |
| 7 #include "base/macros.h" |
| 8 #include "base/memory/ptr_util.h" |
| 9 #include "headless/public/headless_browser_context.h" |
| 10 #include "headless/public/util/deterministic_dispatcher.h" |
| 11 #include "headless/public/util/generic_url_request_job.h" |
| 12 #include "headless/public/util/http_url_fetcher.h" |
| 13 #include "net/url_request/url_request_context.h" |
| 14 #include "net/url_request/url_request_job_factory_impl.h" |
| 15 |
| 16 namespace headless { |
| 17 |
| 18 class DeterministicHttpProtocolHandler::NopGenericURLRequestJobDelegate |
| 19 : public GenericURLRequestJob::Delegate { |
| 20 public: |
| 21 NopGenericURLRequestJobDelegate() {} |
| 22 ~NopGenericURLRequestJobDelegate() override {} |
| 23 |
| 24 // GenericURLRequestJob::Delegate methods: |
| 25 bool BlockOrRewriteRequest( |
| 26 const GURL& url, |
| 27 const std::string& method, |
| 28 const std::string& referrer, |
| 29 GenericURLRequestJob::RewriteCallback callback) override { |
| 30 return false; |
| 31 } |
| 32 |
| 33 const GenericURLRequestJob::HttpResponse* MaybeMatchResource( |
| 34 const GURL& url, |
| 35 const std::string& method, |
| 36 const net::HttpRequestHeaders& request_headers) override { |
| 37 return nullptr; |
| 38 } |
| 39 |
| 40 void OnResourceLoadComplete(const GURL& final_url, |
| 41 const std::string& mime_type, |
| 42 int http_response_code) override {} |
| 43 |
| 44 private: |
| 45 DISALLOW_COPY_AND_ASSIGN(NopGenericURLRequestJobDelegate); |
| 46 }; |
| 47 |
| 48 DeterministicHttpProtocolHandler::DeterministicHttpProtocolHandler( |
| 49 DeterministicDispatcher* deterministic_dispatcher, |
| 50 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner) |
| 51 : deterministic_dispatcher_(deterministic_dispatcher), |
| 52 io_task_runner_(io_task_runner), |
| 53 nop_delegate_(new NopGenericURLRequestJobDelegate()) {} |
| 54 |
| 55 DeterministicHttpProtocolHandler::~DeterministicHttpProtocolHandler() { |
| 56 if (url_request_context_) |
| 57 io_task_runner_->DeleteSoon(FROM_HERE, url_request_context_.release()); |
| 58 if (url_request_job_factory_) |
| 59 io_task_runner_->DeleteSoon(FROM_HERE, url_request_job_factory_.release()); |
| 60 } |
| 61 |
| 62 net::URLRequestJob* DeterministicHttpProtocolHandler::MaybeCreateJob( |
| 63 net::URLRequest* request, |
| 64 net::NetworkDelegate* network_delegate) const { |
| 65 if (!url_request_context_) { |
| 66 DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 67 // Create our own URLRequestContext with an empty URLRequestJobFactoryImpl |
| 68 // which lets us use the default http(s) RequestJobs. |
| 69 url_request_context_.reset(new net::URLRequestContext()); |
| 70 url_request_context_->CopyFrom(request->context()); |
| 71 url_request_job_factory_.reset(new net::URLRequestJobFactoryImpl()); |
| 72 url_request_context_->set_job_factory(url_request_job_factory_.get()); |
| 73 } |
| 74 return new GenericURLRequestJob( |
| 75 request, network_delegate, deterministic_dispatcher_, |
| 76 base::MakeUnique<HttpURLFetcher>(url_request_context_.get()), |
| 77 nop_delegate_.get()); |
| 78 } |
| 79 |
| 80 } // namespace headless |
OLD | NEW |