diff --git a/atom/browser/api/atom_api_cookies.cc b/atom/browser/api/atom_api_cookies.cc index 040efe1c360b..bf56a8dc13bd 100644 --- a/atom/browser/api/atom_api_cookies.cc +++ b/atom/browser/api/atom_api_cookies.cc @@ -9,6 +9,7 @@ #include "atom/common/native_mate_converters/value_converter.h" #include "base/bind.h" #include "base/time/time.h" +#include "base/values.h" #include "content/public/browser/browser_context.h" #include "content/public/browser/browser_thread.h" #include "native_mate/dictionary.h" @@ -179,8 +180,8 @@ namespace atom { namespace api { -Cookies::Cookies(content::BrowserContext* browser_context) : - browser_context_(browser_context) { +Cookies::Cookies(content::BrowserContext* browser_context) + : request_context_getter_(browser_context->GetRequestContext()) { } Cookies::~Cookies() { @@ -198,11 +199,9 @@ void Cookies::Get(const base::DictionaryValue& options, void Cookies::GetCookiesOnIOThread(scoped_ptr filter, const CookiesCallback& callback) { - net::CookieStore* cookie_store = browser_context_->GetRequestContext() - ->GetURLRequestContext()->cookie_store(); std::string url; filter->GetString("url", &url); - if (!GetCookieListFromStore(cookie_store, url, + if (!GetCookieListFromStore(GetCookieStore(), url, base::Bind(&Cookies::OnGetCookies, base::Unretained(this), Passed(&filter), callback))) { BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, @@ -245,9 +244,7 @@ void Cookies::Remove(const mate::Dictionary& details, void Cookies::RemoveCookiesOnIOThread(const GURL& url, const std::string& name, const CookiesCallback& callback) { - net::CookieStore* cookie_store = browser_context_->GetRequestContext() - ->GetURLRequestContext()->cookie_store(); - cookie_store->DeleteCookieAsync(url, name, + GetCookieStore()->DeleteCookieAsync(url, name, base::Bind(&Cookies::OnRemoveCookies, base::Unretained(this), callback)); } @@ -286,8 +283,6 @@ void Cookies::SetCookiesOnIOThread(scoped_ptr details, const GURL& url, const CookiesCallback& callback) { DCHECK_CURRENTLY_ON(BrowserThread::IO); - net::CookieStore* cookie_store = browser_context_->GetRequestContext() - ->GetURLRequestContext()->cookie_store(); std::string name, value, domain, path; bool secure = false; @@ -308,7 +303,7 @@ void Cookies::SetCookiesOnIOThread(scoped_ptr details, base::Time::FromDoubleT(expiration_date); } - cookie_store->GetCookieMonster()->SetCookieWithDetailsAsync( + GetCookieStore()->GetCookieMonster()->SetCookieWithDetailsAsync( url, name, value, @@ -337,6 +332,10 @@ mate::ObjectTemplateBuilder Cookies::GetObjectTemplateBuilder( .SetMethod("set", &Cookies::Set); } +net::CookieStore* Cookies::GetCookieStore() { + return request_context_getter_->GetURLRequestContext()->cookie_store(); +} + // static mate::Handle Cookies::Create( v8::Isolate* isolate, diff --git a/atom/browser/api/atom_api_cookies.h b/atom/browser/api/atom_api_cookies.h index 61357f05d75d..12cf4a220979 100644 --- a/atom/browser/api/atom_api_cookies.h +++ b/atom/browser/api/atom_api_cookies.h @@ -8,17 +8,27 @@ #include #include "base/callback.h" -#include "base/values.h" #include "native_mate/wrappable.h" #include "native_mate/handle.h" -#include "native_mate/dictionary.h" - #include "net/cookies/canonical_cookie.h" +namespace base { +class DictionaryValue; +} + namespace content { class BrowserContext; } +namespace mate { +class Dictionary; +} + +namespace net { +class CookieStore; +class URLRequestContextGetter; +} + namespace atom { namespace api { @@ -60,13 +70,15 @@ class Cookies : public mate::Wrappable { void OnSetCookies(const CookiesCallback& callback, bool set_success); - - // mate::Wrappable implementations: + // mate::Wrappable: mate::ObjectTemplateBuilder GetObjectTemplateBuilder( v8::Isolate* isolate) override; private: - content::BrowserContext* browser_context_; + // Must be called on IO thread. + net::CookieStore* GetCookieStore(); + + scoped_refptr request_context_getter_; DISALLOW_COPY_AND_ASSIGN(Cookies); }; diff --git a/atom/browser/api/atom_api_protocol.cc b/atom/browser/api/atom_api_protocol.cc index 1241a95a718f..d7d308b306ed 100644 --- a/atom/browser/api/atom_api_protocol.cc +++ b/atom/browser/api/atom_api_protocol.cc @@ -35,23 +35,6 @@ 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 { @@ -141,16 +124,27 @@ 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); + + v8::Local value; + mate::Handle session; + scoped_refptr request_context_getter; + // "session" null -> pass nullptr; + // "session" a Session object -> use passed session. + // "session" undefined -> use current session; + if (dict.Get("session", &session)) + request_context_getter = + session->browser_context()->GetRequestContext(); + else if (dict.Get("session", &value) && value->IsNull()) + request_context_getter = nullptr; + else + request_context_getter = registry_->request_context_getter(); BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(&AdapterRequestJob::CreateHttpJobAndStart, GetWeakPtr(), - base::Unretained(getter), url, method, referrer)); + request_context_getter, url, method, referrer)); return; } } @@ -237,7 +231,7 @@ std::string ConvertErrorCode(int error_code) { } // namespace Protocol::Protocol(AtomBrowserContext* browser_context) - : browser_context_(browser_context), + : request_context_getter_(browser_context->GetRequestContext()), job_factory_(browser_context->job_factory()) { CHECK(job_factory_); } @@ -361,6 +355,10 @@ int Protocol::InterceptProtocolInIO(const std::string& scheme, const JsProtocolHandler& handler) { DCHECK_CURRENTLY_ON(BrowserThread::IO); + // Force the request context to initialize, otherwise we might have nothing + // to intercept. + request_context_getter_->GetURLRequestContext(); + if (!job_factory_->HasProtocolHandler(scheme)) return ERR_NO_SCHEME; diff --git a/atom/browser/api/atom_api_protocol.h b/atom/browser/api/atom_api_protocol.h index b4d56018baf3..4dec17a74340 100644 --- a/atom/browser/api/atom_api_protocol.h +++ b/atom/browser/api/atom_api_protocol.h @@ -16,6 +16,7 @@ namespace net { class URLRequest; +class URLRequestContextGetter; } namespace atom { @@ -46,7 +47,9 @@ class Protocol : public mate::EventEmitter { JsProtocolHandler GetProtocolHandler(const std::string& scheme); - AtomBrowserContext* browser_context() const { return browser_context_; } + net::URLRequestContextGetter* request_context_getter() { + return request_context_getter_.get(); + } protected: explicit Protocol(AtomBrowserContext* browser_context); @@ -94,7 +97,8 @@ class Protocol : public mate::EventEmitter { const JsProtocolHandler& handler); int UninterceptProtocolInIO(const std::string& scheme); - AtomBrowserContext* browser_context_; + scoped_refptr request_context_getter_; + 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 b8b1434dd70d..9961d6cc6601 100644 --- a/atom/browser/api/atom_api_session.cc +++ b/atom/browser/api/atom_api_session.cc @@ -19,6 +19,7 @@ #include "chrome/common/pref_names.h" #include "content/public/browser/browser_thread.h" #include "content/public/browser/storage_partition.h" +#include "native_mate/dictionary.h" #include "native_mate/object_template_builder.h" #include "net/base/load_flags.h" #include "net/disk_cache/disk_cache.h" @@ -181,10 +182,10 @@ void OnGetBackend(disk_cache::Backend** backend_ptr, } } -void ClearHttpCacheInIO(content::BrowserContext* browser_context, - const net::CompletionCallback& callback) { - auto request_context = - browser_context->GetRequestContext()->GetURLRequestContext(); +void ClearHttpCacheInIO( + const scoped_refptr& context_getter, + const net::CompletionCallback& callback) { + auto request_context = context_getter->GetURLRequestContext(); auto http_cache = request_context->http_transaction_factory()->GetCache(); if (!http_cache) RunCallbackInUI(callback, net::ERR_FAILED); @@ -226,7 +227,7 @@ void Session::ResolveProxy(const GURL& url, ResolveProxyCallback callback) { void Session::ClearCache(const net::CompletionCallback& callback) { BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(&ClearHttpCacheInIO, - base::Unretained(browser_context_), + make_scoped_refptr(browser_context_->GetRequestContext()), callback)); } diff --git a/atom/browser/api/atom_api_session.h b/atom/browser/api/atom_api_session.h index bd12b7589914..b353c61c2109 100644 --- a/atom/browser/api/atom_api_session.h +++ b/atom/browser/api/atom_api_session.h @@ -13,14 +13,14 @@ class GURL; -namespace mate { -class Arguments; -} - namespace base { class FilePath; } +namespace mate { +class Arguments; +} + namespace atom { class AtomBrowserContext; diff --git a/atom/browser/net/adapter_request_job.cc b/atom/browser/net/adapter_request_job.cc index 5c41c6f65aac..ca7dcf2e566b 100644 --- a/atom/browser/net/adapter_request_job.cc +++ b/atom/browser/net/adapter_request_job.cc @@ -119,7 +119,7 @@ void AdapterRequestJob::CreateFileJobAndStart(const base::FilePath& path) { } void AdapterRequestJob::CreateHttpJobAndStart( - net::URLRequestContextGetter* request_context_getter, + scoped_refptr request_context_getter, const GURL& url, const std::string& method, const std::string& referrer) { diff --git a/atom/browser/net/adapter_request_job.h b/atom/browser/net/adapter_request_job.h index b1d28b1446c1..afb9d5f55d07 100644 --- a/atom/browser/net/adapter_request_job.h +++ b/atom/browser/net/adapter_request_job.h @@ -65,7 +65,7 @@ class AdapterRequestJob : public net::URLRequestJob { scoped_refptr data); void CreateFileJobAndStart(const base::FilePath& path); void CreateHttpJobAndStart( - net::URLRequestContextGetter* request_context_getter, + scoped_refptr request_context_getter, const GURL& url, const std::string& method, const std::string& referrer); diff --git a/atom/browser/net/url_request_fetch_job.cc b/atom/browser/net/url_request_fetch_job.cc index abad38d7f4d5..ee4c67b371ee 100644 --- a/atom/browser/net/url_request_fetch_job.cc +++ b/atom/browser/net/url_request_fetch_job.cc @@ -75,7 +75,7 @@ class ResponsePiper : public net::URLFetcherResponseWriter { } // namespace URLRequestFetchJob::URLRequestFetchJob( - net::URLRequestContextGetter* request_context_getter, + scoped_refptr request_context_getter, net::URLRequest* request, net::NetworkDelegate* network_delegate, const GURL& url, @@ -93,7 +93,7 @@ URLRequestFetchJob::URLRequestFetchJob( fetcher_.reset(net::URLFetcher::Create(url, request_type, this)); // Use request context if provided else create one. if (request_context_getter) - fetcher_->SetRequestContext(request_context_getter); + fetcher_->SetRequestContext(request_context_getter.get()); else fetcher_->SetRequestContext(GetRequestContext()); diff --git a/atom/browser/net/url_request_fetch_job.h b/atom/browser/net/url_request_fetch_job.h index 228af7904fe9..a14e8dd1aae3 100644 --- a/atom/browser/net/url_request_fetch_job.h +++ b/atom/browser/net/url_request_fetch_job.h @@ -18,7 +18,7 @@ class AtomBrowserContext; class URLRequestFetchJob : public net::URLRequestJob, public net::URLFetcherDelegate { public: - URLRequestFetchJob(net::URLRequestContextGetter* request_context_getter, + URLRequestFetchJob(scoped_refptr context_getter, net::URLRequest* request, net::NetworkDelegate* network_delegate, const GURL& url,