From 8eb87c5d2bb6efd643d3a0c55aa7d1c2e2a2fd87 Mon Sep 17 00:00:00 2001 From: Robo Date: Mon, 13 Jul 2015 21:10:44 +0530 Subject: [PATCH] protocol: create separate request context for fetch job --- atom/browser/api/atom_api_protocol.cc | 5 ++--- atom/browser/api/atom_api_protocol.h | 3 --- atom/browser/net/adapter_request_job.cc | 4 +--- atom/browser/net/adapter_request_job.h | 3 +-- atom/browser/net/url_request_fetch_job.cc | 17 ++++++++++++++--- atom/browser/net/url_request_fetch_job.h | 6 ++++-- 6 files changed, 22 insertions(+), 16 deletions(-) diff --git a/atom/browser/api/atom_api_protocol.cc b/atom/browser/api/atom_api_protocol.cc index 65ba3039d153..89b5bc86a40d 100644 --- a/atom/browser/api/atom_api_protocol.cc +++ b/atom/browser/api/atom_api_protocol.cc @@ -132,7 +132,7 @@ class CustomProtocolRequestJob : public AdapterRequestJob { BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(&AdapterRequestJob::CreateHttpJobAndStart, GetWeakPtr(), - registry_->browser_context(), url, method, referrer)); + url, method, referrer)); return; } } @@ -211,8 +211,7 @@ std::string ConvertErrorCode(int error_code) { } // namespace Protocol::Protocol(AtomBrowserContext* browser_context) - : browser_context_(browser_context), - job_factory_(browser_context->job_factory()) { + : job_factory_(browser_context->job_factory()) { CHECK(job_factory_); } diff --git a/atom/browser/api/atom_api_protocol.h b/atom/browser/api/atom_api_protocol.h index b4d56018baf3..3e6f950241cb 100644 --- a/atom/browser/api/atom_api_protocol.h +++ b/atom/browser/api/atom_api_protocol.h @@ -46,8 +46,6 @@ class Protocol : public mate::EventEmitter { JsProtocolHandler GetProtocolHandler(const std::string& scheme); - AtomBrowserContext* browser_context() const { return browser_context_; } - protected: explicit Protocol(AtomBrowserContext* browser_context); @@ -94,7 +92,6 @@ class Protocol : public mate::EventEmitter { const JsProtocolHandler& handler); int UninterceptProtocolInIO(const std::string& scheme); - AtomBrowserContext* browser_context_; AtomURLRequestJobFactory* job_factory_; ProtocolHandlersMap protocol_handlers_; diff --git a/atom/browser/net/adapter_request_job.cc b/atom/browser/net/adapter_request_job.cc index 20be9a708945..8dc472a75f91 100644 --- a/atom/browser/net/adapter_request_job.cc +++ b/atom/browser/net/adapter_request_job.cc @@ -4,7 +4,6 @@ #include "atom/browser/net/adapter_request_job.h" -#include "atom/browser/atom_browser_context.h" #include "base/threading/sequenced_worker_pool.h" #include "atom/browser/net/url_request_buffer_job.h" #include "atom/browser/net/url_request_fetch_job.h" @@ -115,7 +114,6 @@ void AdapterRequestJob::CreateFileJobAndStart(const base::FilePath& path) { } void AdapterRequestJob::CreateHttpJobAndStart( - AtomBrowserContext* browser_context, const GURL& url, const std::string& method, const std::string& referrer) { @@ -124,7 +122,7 @@ void AdapterRequestJob::CreateHttpJobAndStart( return; } - real_job_ = new URLRequestFetchJob(browser_context, request(), + real_job_ = new URLRequestFetchJob(request(), network_delegate(), url, method, referrer); real_job_->Start(); } diff --git a/atom/browser/net/adapter_request_job.h b/atom/browser/net/adapter_request_job.h index 6aff376f3021..040738b649ab 100644 --- a/atom/browser/net/adapter_request_job.h +++ b/atom/browser/net/adapter_request_job.h @@ -61,8 +61,7 @@ class AdapterRequestJob : public net::URLRequestJob { const std::string& charset, scoped_refptr data); void CreateFileJobAndStart(const base::FilePath& path); - void CreateHttpJobAndStart(AtomBrowserContext* browser_context, - const GURL& url, + void CreateHttpJobAndStart(const GURL& url, const std::string& method, const std::string& referrer); void CreateJobFromProtocolHandlerAndStart(); diff --git a/atom/browser/net/url_request_fetch_job.cc b/atom/browser/net/url_request_fetch_job.cc index e353ff170879..d823051ed8d9 100644 --- a/atom/browser/net/url_request_fetch_job.cc +++ b/atom/browser/net/url_request_fetch_job.cc @@ -7,13 +7,14 @@ #include #include -#include "atom/browser/atom_browser_context.h" #include "base/strings/string_util.h" +#include "base/thread_task_runner_handle.h" #include "net/base/io_buffer.h" #include "net/base/net_errors.h" #include "net/http/http_response_headers.h" #include "net/url_request/url_fetcher.h" #include "net/url_request/url_fetcher_response_writer.h" +#include "net/url_request/url_request_context_builder.h" #include "net/url_request/url_request_status.h" namespace atom { @@ -74,7 +75,6 @@ class ResponsePiper : public net::URLFetcherResponseWriter { } // namespace URLRequestFetchJob::URLRequestFetchJob( - AtomBrowserContext* browser_context, net::URLRequest* request, net::NetworkDelegate* network_delegate, const GURL& url, @@ -90,7 +90,7 @@ URLRequestFetchJob::URLRequestFetchJob( request_type = GetRequestType(method); fetcher_.reset(net::URLFetcher::Create(url, request_type, this)); - fetcher_->SetRequestContext(browser_context->url_request_context_getter()); + fetcher_->SetRequestContext(GetRequestContext()); fetcher_->SaveResponseWithWriter(make_scoped_ptr(new ResponsePiper(this))); // Use |request|'s referrer if |referrer| is not specified. @@ -107,6 +107,17 @@ URLRequestFetchJob::URLRequestFetchJob( } } +net::URLRequestContextGetter* URLRequestFetchJob::GetRequestContext() { + if (!url_request_context_getter_.get()) { + auto task_runner = base::ThreadTaskRunnerHandle::Get(); + net::URLRequestContextBuilder builder; + builder.set_proxy_service(net::ProxyService::CreateDirect()); + url_request_context_getter_ = + new net::TrivialURLRequestContextGetter(builder.Build(), task_runner); + } + return url_request_context_getter_.get(); +} + void URLRequestFetchJob::HeadersCompleted() { response_info_.reset(new net::HttpResponseInfo); response_info_->headers = fetcher_->GetResponseHeaders(); diff --git a/atom/browser/net/url_request_fetch_job.h b/atom/browser/net/url_request_fetch_job.h index d598e3223618..8f3a41056133 100644 --- a/atom/browser/net/url_request_fetch_job.h +++ b/atom/browser/net/url_request_fetch_job.h @@ -7,6 +7,7 @@ #include +#include "net/url_request/url_request_context_getter.h" #include "net/url_request/url_fetcher_delegate.h" #include "net/url_request/url_request_job.h" @@ -17,13 +18,13 @@ class AtomBrowserContext; class URLRequestFetchJob : public net::URLRequestJob, public net::URLFetcherDelegate { public: - URLRequestFetchJob(AtomBrowserContext* browser_context, - net::URLRequest* request, + URLRequestFetchJob(net::URLRequest* request, net::NetworkDelegate* network_delegate, const GURL& url, const std::string& method, const std::string& referrer); + net::URLRequestContextGetter* GetRequestContext(); void HeadersCompleted(); int DataAvailable(net::IOBuffer* buffer, int num_bytes); @@ -41,6 +42,7 @@ class URLRequestFetchJob : public net::URLRequestJob, void OnURLFetchComplete(const net::URLFetcher* source) override; private: + scoped_refptr url_request_context_getter_; scoped_ptr fetcher_; scoped_refptr pending_buffer_; int pending_buffer_size_;