From 8eb87c5d2bb6efd643d3a0c55aa7d1c2e2a2fd87 Mon Sep 17 00:00:00 2001 From: Robo Date: Mon, 13 Jul 2015 21:10:44 +0530 Subject: [PATCH 1/3] 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_; From 57244e4718d14150fbc213a733ca3935b59a792b Mon Sep 17 00:00:00 2001 From: Robo Date: Wed, 15 Jul 2015 18:05:38 +0530 Subject: [PATCH 2/3] use request context from session if provided --- atom/browser/api/atom_api_protocol.cc | 22 +++++++++++++++++++++- atom/browser/api/atom_api_session.cc | 4 ++++ atom/browser/api/atom_api_session.h | 2 ++ atom/browser/api/lib/protocol.coffee | 2 +- atom/browser/net/adapter_request_job.cc | 3 ++- atom/browser/net/adapter_request_job.h | 9 ++++++--- atom/browser/net/url_request_fetch_job.cc | 8 +++++++- atom/browser/net/url_request_fetch_job.h | 3 ++- docs/api/protocol.md | 1 + 9 files changed, 46 insertions(+), 8 deletions(-) diff --git a/atom/browser/api/atom_api_protocol.cc b/atom/browser/api/atom_api_protocol.cc index 89b5bc86a40d..3b9eeb9fb783 100644 --- a/atom/browser/api/atom_api_protocol.cc +++ b/atom/browser/api/atom_api_protocol.cc @@ -7,6 +7,7 @@ #include "atom/browser/atom_browser_client.h" #include "atom/browser/atom_browser_context.h" #include "atom/browser/atom_browser_main_parts.h" +#include "atom/browser/api/atom_api_session.h" #include "atom/browser/net/adapter_request_job.h" #include "atom/browser/net/atom_url_request_job_factory.h" #include "atom/common/native_mate_converters/file_path_converter.h" @@ -34,6 +35,23 @@ struct Converter { } }; +template<> +struct Converter { + static bool FromV8(v8::Isolate* isolate, v8::Local val, + net::URLRequestContextGetter** out) { + if (val->IsNull() || val->IsUndefined()) { + *out = nullptr; + return true; + } + + atom::api::Session* session; + if (!Converter::FromV8(isolate, val, &session)) + return false; + *out = session->GetBrowserContext()->GetRequestContext(); + return true; + } +}; + } // namespace mate namespace atom { @@ -126,13 +144,15 @@ class CustomProtocolRequestJob : public AdapterRequestJob { } else if (name == "RequestHttpJob") { GURL url; std::string method, referrer; + net::URLRequestContextGetter* getter; dict.Get("url", &url); dict.Get("method", &method); dict.Get("referrer", &referrer); + dict.Get("session", &getter); BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(&AdapterRequestJob::CreateHttpJobAndStart, GetWeakPtr(), - url, method, referrer)); + base::Unretained(getter), url, method, referrer)); return; } } diff --git a/atom/browser/api/atom_api_session.cc b/atom/browser/api/atom_api_session.cc index 93f8de52dd45..b489df4c44ef 100644 --- a/atom/browser/api/atom_api_session.cc +++ b/atom/browser/api/atom_api_session.cc @@ -203,6 +203,10 @@ Session::Session(AtomBrowserContext* browser_context) Session::~Session() { } +AtomBrowserContext* Session::GetBrowserContext() const { + return browser_context_; +} + void Session::ResolveProxy(const GURL& url, ResolveProxyCallback callback) { new ResolveProxyHelper(browser_context_, url, callback); } diff --git a/atom/browser/api/atom_api_session.h b/atom/browser/api/atom_api_session.h index 59e1a9380796..debe01ef9dbc 100644 --- a/atom/browser/api/atom_api_session.h +++ b/atom/browser/api/atom_api_session.h @@ -31,6 +31,8 @@ class Session: public mate::TrackableObject { static mate::Handle CreateFrom( v8::Isolate* isolate, AtomBrowserContext* browser_context); + AtomBrowserContext* GetBrowserContext() const; + protected: explicit Session(AtomBrowserContext* browser_context); ~Session(); diff --git a/atom/browser/api/lib/protocol.coffee b/atom/browser/api/lib/protocol.coffee index 6e22329318b4..4a661523509c 100644 --- a/atom/browser/api/lib/protocol.coffee +++ b/atom/browser/api/lib/protocol.coffee @@ -59,6 +59,6 @@ class RequestErrorJob protocol.RequestHttpJob = class RequestHttpJob - constructor: ({@url, @method, @referrer}) -> + constructor: ({@session, @url, @method, @referrer}) -> module.exports = protocol diff --git a/atom/browser/net/adapter_request_job.cc b/atom/browser/net/adapter_request_job.cc index 8dc472a75f91..d9c9b467d48c 100644 --- a/atom/browser/net/adapter_request_job.cc +++ b/atom/browser/net/adapter_request_job.cc @@ -114,6 +114,7 @@ void AdapterRequestJob::CreateFileJobAndStart(const base::FilePath& path) { } void AdapterRequestJob::CreateHttpJobAndStart( + net::URLRequestContextGetter* request_context_getter, const GURL& url, const std::string& method, const std::string& referrer) { @@ -122,7 +123,7 @@ void AdapterRequestJob::CreateHttpJobAndStart( return; } - real_job_ = new URLRequestFetchJob(request(), + real_job_ = new URLRequestFetchJob(request_context_getter, 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 040738b649ab..768718584d03 100644 --- a/atom/browser/net/adapter_request_job.h +++ b/atom/browser/net/adapter_request_job.h @@ -10,6 +10,7 @@ #include "base/memory/ref_counted_memory.h" #include "base/memory/weak_ptr.h" #include "net/url_request/url_request_context.h" +#include "net/url_request/url_request_context_getter.h" #include "net/url_request/url_request_job.h" #include "net/url_request/url_request_job_factory.h" #include "v8/include/v8.h" @@ -61,9 +62,11 @@ class AdapterRequestJob : public net::URLRequestJob { const std::string& charset, scoped_refptr data); void CreateFileJobAndStart(const base::FilePath& path); - void CreateHttpJobAndStart(const GURL& url, - const std::string& method, - const std::string& referrer); + void CreateHttpJobAndStart( + net::URLRequestContextGetter* request_context_getter, + const GURL& url, + const std::string& method, + const std::string& referrer); void CreateJobFromProtocolHandlerAndStart(); private: diff --git a/atom/browser/net/url_request_fetch_job.cc b/atom/browser/net/url_request_fetch_job.cc index d823051ed8d9..5389b8bb6216 100644 --- a/atom/browser/net/url_request_fetch_job.cc +++ b/atom/browser/net/url_request_fetch_job.cc @@ -75,6 +75,7 @@ class ResponsePiper : public net::URLFetcherResponseWriter { } // namespace URLRequestFetchJob::URLRequestFetchJob( + net::URLRequestContextGetter* request_context_getter, net::URLRequest* request, net::NetworkDelegate* network_delegate, const GURL& url, @@ -90,7 +91,12 @@ URLRequestFetchJob::URLRequestFetchJob( request_type = GetRequestType(method); fetcher_.reset(net::URLFetcher::Create(url, request_type, this)); - fetcher_->SetRequestContext(GetRequestContext()); + // Use request context if provided else create one. + if (request_context_getter) + fetcher_->SetRequestContext(request_context_getter); + else + fetcher_->SetRequestContext(GetRequestContext()); + fetcher_->SaveResponseWithWriter(make_scoped_ptr(new ResponsePiper(this))); // Use |request|'s referrer if |referrer| is not specified. diff --git a/atom/browser/net/url_request_fetch_job.h b/atom/browser/net/url_request_fetch_job.h index 8f3a41056133..228af7904fe9 100644 --- a/atom/browser/net/url_request_fetch_job.h +++ b/atom/browser/net/url_request_fetch_job.h @@ -18,7 +18,8 @@ class AtomBrowserContext; class URLRequestFetchJob : public net::URLRequestJob, public net::URLFetcherDelegate { public: - URLRequestFetchJob(net::URLRequest* request, + URLRequestFetchJob(net::URLRequestContextGetter* request_context_getter, + net::URLRequest* request, net::NetworkDelegate* network_delegate, const GURL& url, const std::string& method, diff --git a/docs/api/protocol.md b/docs/api/protocol.md index a076e3640daa..38fd7e5ba160 100644 --- a/docs/api/protocol.md +++ b/docs/api/protocol.md @@ -101,6 +101,7 @@ Create a request job which sends a buffer as response. ## Class: protocol.RequestHttpJob(options) * `options` Object + * `session` [Session](browser-window.md#class-session) * `url` String * `method` String - Default is `GET` * `referrer` String From de3ccc4b98fd2275b64e22fae41d19322ddc1b4b Mon Sep 17 00:00:00 2001 From: Robo Date: Fri, 17 Jul 2015 02:00:43 +0530 Subject: [PATCH 3/3] use current session when not defined --- atom/browser/api/atom_api_protocol.cc | 10 ++++++---- atom/browser/api/atom_api_protocol.h | 3 +++ atom/browser/api/atom_api_session.cc | 4 ---- atom/browser/api/atom_api_session.h | 2 +- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/atom/browser/api/atom_api_protocol.cc b/atom/browser/api/atom_api_protocol.cc index 3b9eeb9fb783..d7af58802792 100644 --- a/atom/browser/api/atom_api_protocol.cc +++ b/atom/browser/api/atom_api_protocol.cc @@ -39,7 +39,7 @@ template<> struct Converter { static bool FromV8(v8::Isolate* isolate, v8::Local val, net::URLRequestContextGetter** out) { - if (val->IsNull() || val->IsUndefined()) { + if (val->IsNull()) { *out = nullptr; return true; } @@ -47,7 +47,7 @@ struct Converter { atom::api::Session* session; if (!Converter::FromV8(isolate, val, &session)) return false; - *out = session->GetBrowserContext()->GetRequestContext(); + *out = session->browser_context()->GetRequestContext(); return true; } }; @@ -144,7 +144,8 @@ class CustomProtocolRequestJob : public AdapterRequestJob { } else if (name == "RequestHttpJob") { GURL url; std::string method, referrer; - net::URLRequestContextGetter* getter; + net::URLRequestContextGetter* getter = + registry_->browser_context()->GetRequestContext(); dict.Get("url", &url); dict.Get("method", &method); dict.Get("referrer", &referrer); @@ -231,7 +232,8 @@ std::string ConvertErrorCode(int error_code) { } // namespace Protocol::Protocol(AtomBrowserContext* browser_context) - : job_factory_(browser_context->job_factory()) { + : browser_context_(browser_context), + 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 3e6f950241cb..b4d56018baf3 100644 --- a/atom/browser/api/atom_api_protocol.h +++ b/atom/browser/api/atom_api_protocol.h @@ -46,6 +46,8 @@ class Protocol : public mate::EventEmitter { JsProtocolHandler GetProtocolHandler(const std::string& scheme); + AtomBrowserContext* browser_context() const { return browser_context_; } + protected: explicit Protocol(AtomBrowserContext* browser_context); @@ -92,6 +94,7 @@ 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/api/atom_api_session.cc b/atom/browser/api/atom_api_session.cc index b489df4c44ef..93f8de52dd45 100644 --- a/atom/browser/api/atom_api_session.cc +++ b/atom/browser/api/atom_api_session.cc @@ -203,10 +203,6 @@ Session::Session(AtomBrowserContext* browser_context) Session::~Session() { } -AtomBrowserContext* Session::GetBrowserContext() const { - return browser_context_; -} - void Session::ResolveProxy(const GURL& url, ResolveProxyCallback callback) { new ResolveProxyHelper(browser_context_, url, callback); } diff --git a/atom/browser/api/atom_api_session.h b/atom/browser/api/atom_api_session.h index debe01ef9dbc..8e14c3a37834 100644 --- a/atom/browser/api/atom_api_session.h +++ b/atom/browser/api/atom_api_session.h @@ -31,7 +31,7 @@ class Session: public mate::TrackableObject { static mate::Handle CreateFrom( v8::Isolate* isolate, AtomBrowserContext* browser_context); - AtomBrowserContext* GetBrowserContext() const; + AtomBrowserContext* browser_context() const { return browser_context_; } protected: explicit Session(AtomBrowserContext* browser_context);