diff --git a/atom/browser/api/atom_api_protocol.cc b/atom/browser/api/atom_api_protocol.cc index 65ba3039d153..d7af58802792 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()) { + *out = nullptr; + return true; + } + + atom::api::Session* session; + if (!Converter::FromV8(isolate, val, &session)) + return false; + *out = session->browser_context()->GetRequestContext(); + return true; + } +}; + } // namespace mate namespace atom { @@ -126,13 +144,16 @@ class CustomProtocolRequestJob : public AdapterRequestJob { } else if (name == "RequestHttpJob") { GURL url; std::string method, referrer; + net::URLRequestContextGetter* getter = + registry_->browser_context()->GetRequestContext(); 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(), - registry_->browser_context(), url, method, referrer)); + base::Unretained(getter), url, method, referrer)); return; } } diff --git a/atom/browser/api/atom_api_session.h b/atom/browser/api/atom_api_session.h index 59e1a9380796..8e14c3a37834 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* browser_context() const { return browser_context_; } + 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 20be9a708945..d9c9b467d48c 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,7 @@ void AdapterRequestJob::CreateFileJobAndStart(const base::FilePath& path) { } void AdapterRequestJob::CreateHttpJobAndStart( - AtomBrowserContext* browser_context, + net::URLRequestContextGetter* request_context_getter, const GURL& url, const std::string& method, const std::string& referrer) { @@ -124,7 +123,7 @@ void AdapterRequestJob::CreateHttpJobAndStart( return; } - real_job_ = new URLRequestFetchJob(browser_context, 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 6aff376f3021..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,10 +62,11 @@ 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, - 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 e353ff170879..5389b8bb6216 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,7 @@ class ResponsePiper : public net::URLFetcherResponseWriter { } // namespace URLRequestFetchJob::URLRequestFetchJob( - AtomBrowserContext* browser_context, + 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(browser_context->url_request_context_getter()); + // 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. @@ -107,6 +113,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..228af7904fe9 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,14 @@ class AtomBrowserContext; class URLRequestFetchJob : public net::URLRequestJob, public net::URLFetcherDelegate { public: - URLRequestFetchJob(AtomBrowserContext* browser_context, + URLRequestFetchJob(net::URLRequestContextGetter* request_context_getter, 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 +43,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_; diff --git a/docs/api/protocol.md b/docs/api/protocol.md index 2734546516e3..ad863f4c74fb 100644 --- a/docs/api/protocol.md +++ b/docs/api/protocol.md @@ -110,6 +110,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