From 923296b4ee3d978f5058a571eb126882960f8a41 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Thu, 18 Jun 2015 17:18:11 +0800 Subject: [PATCH] Use the BrowserContext from protocol --- atom/browser/api/atom_api_protocol.cc | 4 ++-- atom/browser/api/atom_api_protocol.h | 2 ++ atom/browser/net/adapter_request_job.cc | 12 +++++++----- atom/browser/net/adapter_request_job.h | 5 ++++- atom/browser/net/url_request_fetch_job.cc | 4 ++-- atom/browser/net/url_request_fetch_job.h | 5 ++++- 6 files changed, 21 insertions(+), 11 deletions(-) diff --git a/atom/browser/api/atom_api_protocol.cc b/atom/browser/api/atom_api_protocol.cc index b4ce2e327ba8..75dd70d2a460 100644 --- a/atom/browser/api/atom_api_protocol.cc +++ b/atom/browser/api/atom_api_protocol.cc @@ -132,8 +132,8 @@ class CustomProtocolRequestJob : public AdapterRequestJob { dict.Get("referrer", &referrer); BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, - base::Bind(&AdapterRequestJob::CreateHttpJobAndStart, - GetWeakPtr(), url, method, referrer)); + base::Bind(&AdapterRequestJob::CreateHttpJobAndStart, GetWeakPtr(), + registry_->browser_context(), url, method, referrer)); return; } } diff --git a/atom/browser/api/atom_api_protocol.h b/atom/browser/api/atom_api_protocol.h index 8d76008eb52c..3e2e18d33f51 100644 --- a/atom/browser/api/atom_api_protocol.h +++ b/atom/browser/api/atom_api_protocol.h @@ -33,6 +33,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); diff --git a/atom/browser/net/adapter_request_job.cc b/atom/browser/net/adapter_request_job.cc index 08331829b550..20be9a708945 100644 --- a/atom/browser/net/adapter_request_job.cc +++ b/atom/browser/net/adapter_request_job.cc @@ -114,16 +114,18 @@ void AdapterRequestJob::CreateFileJobAndStart(const base::FilePath& path) { real_job_->Start(); } -void AdapterRequestJob::CreateHttpJobAndStart(const GURL& url, - const std::string& method, - const std::string& referrer) { +void AdapterRequestJob::CreateHttpJobAndStart( + AtomBrowserContext* browser_context, + const GURL& url, + const std::string& method, + const std::string& referrer) { if (!url.is_valid()) { CreateErrorJobAndStart(net::ERR_INVALID_URL); return; } - real_job_ = new URLRequestFetchJob(request(), network_delegate(), url, - method, referrer); + real_job_ = new URLRequestFetchJob(browser_context, 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 d5e814d214d4..6aff376f3021 100644 --- a/atom/browser/net/adapter_request_job.h +++ b/atom/browser/net/adapter_request_job.h @@ -20,6 +20,8 @@ class FilePath; namespace atom { +class AtomBrowserContext; + // Ask JS which type of job it wants, and then delegate corresponding methods. class AdapterRequestJob : public net::URLRequestJob { public: @@ -59,7 +61,8 @@ class AdapterRequestJob : public net::URLRequestJob { const std::string& charset, scoped_refptr data); void CreateFileJobAndStart(const base::FilePath& path); - void CreateHttpJobAndStart(const GURL& url, + void CreateHttpJobAndStart(AtomBrowserContext* browser_context, + 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 f77379aed6c2..e353ff170879 100644 --- a/atom/browser/net/url_request_fetch_job.cc +++ b/atom/browser/net/url_request_fetch_job.cc @@ -74,6 +74,7 @@ class ResponsePiper : public net::URLFetcherResponseWriter { } // namespace URLRequestFetchJob::URLRequestFetchJob( + AtomBrowserContext* browser_context, net::URLRequest* request, net::NetworkDelegate* network_delegate, const GURL& url, @@ -89,8 +90,7 @@ URLRequestFetchJob::URLRequestFetchJob( request_type = GetRequestType(method); fetcher_.reset(net::URLFetcher::Create(url, request_type, this)); - auto context = AtomBrowserContext::Get()->url_request_context_getter(); - fetcher_->SetRequestContext(context); + fetcher_->SetRequestContext(browser_context->url_request_context_getter()); 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 7975aa715eec..d598e3223618 100644 --- a/atom/browser/net/url_request_fetch_job.h +++ b/atom/browser/net/url_request_fetch_job.h @@ -12,10 +12,13 @@ namespace atom { +class AtomBrowserContext; + class URLRequestFetchJob : public net::URLRequestJob, public net::URLFetcherDelegate { public: - URLRequestFetchJob(net::URLRequest* request, + URLRequestFetchJob(AtomBrowserContext* browser_context, + net::URLRequest* request, net::NetworkDelegate* network_delegate, const GURL& url, const std::string& method,