From 659384383ecc150cd4e1b0aff462a4b317444192 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 15 Jun 2016 20:31:29 +0900 Subject: [PATCH] Avoid storing JobFactory in BrowserContext JobFactory should always be created and accessed in IO thread. --- atom/browser/api/atom_api_protocol.cc | 4 +--- atom/browser/api/atom_api_protocol.h | 18 ++++++++---------- atom/browser/atom_browser_context.cc | 27 +++++++++------------------ atom/browser/atom_browser_context.h | 8 +------- vendor/brightray | 2 +- 5 files changed, 20 insertions(+), 39 deletions(-) diff --git a/atom/browser/api/atom_api_protocol.cc b/atom/browser/api/atom_api_protocol.cc index 6ec7f017d817..b53fd6c2b92a 100644 --- a/atom/browser/api/atom_api_protocol.cc +++ b/atom/browser/api/atom_api_protocol.cc @@ -27,10 +27,8 @@ namespace atom { namespace api { Protocol::Protocol(v8::Isolate* isolate, AtomBrowserContext* browser_context) - : browser_context_(browser_context), - request_context_getter_(browser_context->GetRequestContext()), + : request_context_getter_(browser_context->GetRequestContext()), weak_factory_(this) { - CHECK(job_factory()); Init(isolate); } diff --git a/atom/browser/api/atom_api_protocol.h b/atom/browser/api/atom_api_protocol.h index bdcd4a9cfa68..3c262d205ab7 100644 --- a/atom/browser/api/atom_api_protocol.h +++ b/atom/browser/api/atom_api_protocol.h @@ -19,16 +19,12 @@ #include "native_mate/arguments.h" #include "native_mate/dictionary.h" #include "native_mate/handle.h" +#include "net/url_request/url_request_context.h" namespace base { class DictionaryValue; } -namespace net { -class URLRequest; -class URLRequestContextGetter; -} - namespace atom { namespace api { @@ -50,7 +46,10 @@ class Protocol : public mate::TrackableObject { Protocol(v8::Isolate* isolate, AtomBrowserContext* browser_context); AtomURLRequestJobFactory* job_factory() const { - return browser_context_->job_factory(); + request_context_getter_->GetURLRequestContext(); // Force init. + return static_cast( + static_cast( + request_context_getter_.get())->job_factory()); } base::WeakPtr GetWeakPtr() { @@ -123,7 +122,7 @@ class Protocol : public mate::TrackableObject { return PROTOCOL_REGISTERED; std::unique_ptr> protocol_handler( new CustomProtocolHandler( - isolate(), request_context_getter_, handler)); + isolate(), request_context_getter_.get(), handler)); if (job_factory()->SetProtocolHandler(scheme, std::move(protocol_handler))) return PROTOCOL_OK; else @@ -165,7 +164,7 @@ class Protocol : public mate::TrackableObject { return PROTOCOL_INTERCEPTED; std::unique_ptr> protocol_handler( new CustomProtocolHandler( - isolate(), request_context_getter_, handler)); + isolate(), request_context_getter_.get(), handler)); original_protocols_.set( scheme, job_factory()->ReplaceProtocol(scheme, std::move(protocol_handler))); @@ -182,8 +181,7 @@ class Protocol : public mate::TrackableObject { // Convert error code to string. std::string ErrorCodeToString(ProtocolError error); - scoped_refptr browser_context_; - net::URLRequestContextGetter* request_context_getter_; + scoped_refptr request_context_getter_; // Map that stores the original protocols of schemes. using OriginalProtocolsMap = base::ScopedPtrHashMap< diff --git a/atom/browser/atom_browser_context.cc b/atom/browser/atom_browser_context.cc index 6f28cf6df7da..8268a7778193 100644 --- a/atom/browser/atom_browser_context.cc +++ b/atom/browser/atom_browser_context.cc @@ -20,14 +20,15 @@ #include "atom/common/options_switches.h" #include "base/command_line.h" #include "base/files/file_path.h" +#include "base/memory/ptr_util.h" #include "base/path_service.h" -#include "components/prefs/pref_registry_simple.h" #include "base/strings/string_util.h" #include "base/strings/stringprintf.h" #include "base/threading/sequenced_worker_pool.h" #include "base/threading/worker_pool.h" #include "chrome/common/chrome_paths.h" #include "chrome/common/pref_names.h" +#include "components/prefs/pref_registry_simple.h" #include "content/public/browser/browser_thread.h" #include "content/public/common/url_constants.h" #include "content/public/common/user_agent.h" @@ -66,7 +67,6 @@ AtomBrowserContext::AtomBrowserContext(const std::string& partition, bool in_memory) : brightray::BrowserContext(partition, in_memory), cert_verifier_(new AtomCertVerifier), - job_factory_(new AtomURLRequestJobFactory), network_delegate_(new AtomNetworkDelegate) { } @@ -96,15 +96,15 @@ std::string AtomBrowserContext::GetUserAgent() { std::unique_ptr AtomBrowserContext::CreateURLRequestJobFactory( - content::ProtocolHandlerMap* handlers, - content::URLRequestInterceptorScopedVector* interceptors) { - std::unique_ptr job_factory(job_factory_); + content::ProtocolHandlerMap* protocol_handlers) { + std::unique_ptr job_factory( + new AtomURLRequestJobFactory); - for (auto& it : *handlers) { + for (auto& it : *protocol_handlers) { job_factory->SetProtocolHandler(it.first, - make_scoped_ptr(it.second.release())); + base::WrapUnique(it.second.release())); } - handlers->clear(); + protocol_handlers->clear(); job_factory->SetProtocolHandler( url::kDataScheme, make_scoped_ptr(new net::DataProtocolHandler)); @@ -132,16 +132,7 @@ AtomBrowserContext::CreateURLRequestJobFactory( make_scoped_ptr(new net::FtpProtocolHandler( new net::FtpNetworkLayer(host_resolver)))); - // Set up interceptors in the reverse order. - std::unique_ptr top_job_factory = - std::move(job_factory); - content::URLRequestInterceptorScopedVector::reverse_iterator it; - for (it = interceptors->rbegin(); it != interceptors->rend(); ++it) - top_job_factory.reset(new net::URLRequestInterceptingJobFactory( - std::move(top_job_factory), make_scoped_ptr(*it))); - interceptors->weak_clear(); - - return top_job_factory; + return std::move(job_factory); } net::HttpCache::BackendFactory* diff --git a/atom/browser/atom_browser_context.h b/atom/browser/atom_browser_context.h index 028f5d1e79c4..c9b1de947fba 100644 --- a/atom/browser/atom_browser_context.h +++ b/atom/browser/atom_browser_context.h @@ -15,7 +15,6 @@ class AtomDownloadManagerDelegate; class AtomCertVerifier; class AtomNetworkDelegate; class AtomPermissionManager; -class AtomURLRequestJobFactory; class WebViewManager; class AtomBrowserContext : public brightray::BrowserContext { @@ -27,8 +26,7 @@ class AtomBrowserContext : public brightray::BrowserContext { net::NetworkDelegate* CreateNetworkDelegate() override; std::string GetUserAgent() override; std::unique_ptr CreateURLRequestJobFactory( - content::ProtocolHandlerMap* handlers, - content::URLRequestInterceptorScopedVector* interceptors) override; + content::ProtocolHandlerMap* protocol_handlers) override; net::HttpCache::BackendFactory* CreateHttpCacheBackendFactory( const base::FilePath& base_path) override; std::unique_ptr CreateCertVerifier() override; @@ -43,9 +41,6 @@ class AtomBrowserContext : public brightray::BrowserContext { void RegisterPrefs(PrefRegistrySimple* pref_registry) override; AtomCertVerifier* cert_verifier() const { return cert_verifier_; } - - AtomURLRequestJobFactory* job_factory() const { return job_factory_; } - AtomNetworkDelegate* network_delegate() const { return network_delegate_; } private: @@ -55,7 +50,6 @@ class AtomBrowserContext : public brightray::BrowserContext { // Managed by brightray::BrowserContext. AtomCertVerifier* cert_verifier_; - AtomURLRequestJobFactory* job_factory_; AtomNetworkDelegate* network_delegate_; DISALLOW_COPY_AND_ASSIGN(AtomBrowserContext); diff --git a/vendor/brightray b/vendor/brightray index 55423bdca586..717d92968d79 160000 --- a/vendor/brightray +++ b/vendor/brightray @@ -1 +1 @@ -Subproject commit 55423bdca586a20fe052f6c0e19565710dcebd42 +Subproject commit 717d92968d7959814060b9409c4720ab647bd90e