From 57244e4718d14150fbc213a733ca3935b59a792b Mon Sep 17 00:00:00 2001 From: Robo Date: Wed, 15 Jul 2015 18:05:38 +0530 Subject: [PATCH] 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