Avoid storing JobFactory in BrowserContext

JobFactory should always be created and accessed in IO thread.
This commit is contained in:
Cheng Zhao 2016-06-15 20:31:29 +09:00
parent 9372d4df32
commit 659384383e
5 changed files with 20 additions and 39 deletions

View file

@ -27,10 +27,8 @@ namespace atom {
namespace api { namespace api {
Protocol::Protocol(v8::Isolate* isolate, AtomBrowserContext* browser_context) 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) { weak_factory_(this) {
CHECK(job_factory());
Init(isolate); Init(isolate);
} }

View file

@ -19,16 +19,12 @@
#include "native_mate/arguments.h" #include "native_mate/arguments.h"
#include "native_mate/dictionary.h" #include "native_mate/dictionary.h"
#include "native_mate/handle.h" #include "native_mate/handle.h"
#include "net/url_request/url_request_context.h"
namespace base { namespace base {
class DictionaryValue; class DictionaryValue;
} }
namespace net {
class URLRequest;
class URLRequestContextGetter;
}
namespace atom { namespace atom {
namespace api { namespace api {
@ -50,7 +46,10 @@ class Protocol : public mate::TrackableObject<Protocol> {
Protocol(v8::Isolate* isolate, AtomBrowserContext* browser_context); Protocol(v8::Isolate* isolate, AtomBrowserContext* browser_context);
AtomURLRequestJobFactory* job_factory() const { AtomURLRequestJobFactory* job_factory() const {
return browser_context_->job_factory(); request_context_getter_->GetURLRequestContext(); // Force init.
return static_cast<AtomURLRequestJobFactory*>(
static_cast<brightray::URLRequestContextGetter*>(
request_context_getter_.get())->job_factory());
} }
base::WeakPtr<Protocol> GetWeakPtr() { base::WeakPtr<Protocol> GetWeakPtr() {
@ -123,7 +122,7 @@ class Protocol : public mate::TrackableObject<Protocol> {
return PROTOCOL_REGISTERED; return PROTOCOL_REGISTERED;
std::unique_ptr<CustomProtocolHandler<RequestJob>> protocol_handler( std::unique_ptr<CustomProtocolHandler<RequestJob>> protocol_handler(
new CustomProtocolHandler<RequestJob>( new CustomProtocolHandler<RequestJob>(
isolate(), request_context_getter_, handler)); isolate(), request_context_getter_.get(), handler));
if (job_factory()->SetProtocolHandler(scheme, std::move(protocol_handler))) if (job_factory()->SetProtocolHandler(scheme, std::move(protocol_handler)))
return PROTOCOL_OK; return PROTOCOL_OK;
else else
@ -165,7 +164,7 @@ class Protocol : public mate::TrackableObject<Protocol> {
return PROTOCOL_INTERCEPTED; return PROTOCOL_INTERCEPTED;
std::unique_ptr<CustomProtocolHandler<RequestJob>> protocol_handler( std::unique_ptr<CustomProtocolHandler<RequestJob>> protocol_handler(
new CustomProtocolHandler<RequestJob>( new CustomProtocolHandler<RequestJob>(
isolate(), request_context_getter_, handler)); isolate(), request_context_getter_.get(), handler));
original_protocols_.set( original_protocols_.set(
scheme, scheme,
job_factory()->ReplaceProtocol(scheme, std::move(protocol_handler))); job_factory()->ReplaceProtocol(scheme, std::move(protocol_handler)));
@ -182,8 +181,7 @@ class Protocol : public mate::TrackableObject<Protocol> {
// Convert error code to string. // Convert error code to string.
std::string ErrorCodeToString(ProtocolError error); std::string ErrorCodeToString(ProtocolError error);
scoped_refptr<AtomBrowserContext> browser_context_; scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
net::URLRequestContextGetter* request_context_getter_;
// Map that stores the original protocols of schemes. // Map that stores the original protocols of schemes.
using OriginalProtocolsMap = base::ScopedPtrHashMap< using OriginalProtocolsMap = base::ScopedPtrHashMap<

View file

@ -20,14 +20,15 @@
#include "atom/common/options_switches.h" #include "atom/common/options_switches.h"
#include "base/command_line.h" #include "base/command_line.h"
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "base/memory/ptr_util.h"
#include "base/path_service.h" #include "base/path_service.h"
#include "components/prefs/pref_registry_simple.h"
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
#include "base/strings/stringprintf.h" #include "base/strings/stringprintf.h"
#include "base/threading/sequenced_worker_pool.h" #include "base/threading/sequenced_worker_pool.h"
#include "base/threading/worker_pool.h" #include "base/threading/worker_pool.h"
#include "chrome/common/chrome_paths.h" #include "chrome/common/chrome_paths.h"
#include "chrome/common/pref_names.h" #include "chrome/common/pref_names.h"
#include "components/prefs/pref_registry_simple.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
#include "content/public/common/url_constants.h" #include "content/public/common/url_constants.h"
#include "content/public/common/user_agent.h" #include "content/public/common/user_agent.h"
@ -66,7 +67,6 @@ AtomBrowserContext::AtomBrowserContext(const std::string& partition,
bool in_memory) bool in_memory)
: brightray::BrowserContext(partition, in_memory), : brightray::BrowserContext(partition, in_memory),
cert_verifier_(new AtomCertVerifier), cert_verifier_(new AtomCertVerifier),
job_factory_(new AtomURLRequestJobFactory),
network_delegate_(new AtomNetworkDelegate) { network_delegate_(new AtomNetworkDelegate) {
} }
@ -96,15 +96,15 @@ std::string AtomBrowserContext::GetUserAgent() {
std::unique_ptr<net::URLRequestJobFactory> std::unique_ptr<net::URLRequestJobFactory>
AtomBrowserContext::CreateURLRequestJobFactory( AtomBrowserContext::CreateURLRequestJobFactory(
content::ProtocolHandlerMap* handlers, content::ProtocolHandlerMap* protocol_handlers) {
content::URLRequestInterceptorScopedVector* interceptors) { std::unique_ptr<AtomURLRequestJobFactory> job_factory(
std::unique_ptr<AtomURLRequestJobFactory> job_factory(job_factory_); new AtomURLRequestJobFactory);
for (auto& it : *handlers) { for (auto& it : *protocol_handlers) {
job_factory->SetProtocolHandler(it.first, job_factory->SetProtocolHandler(it.first,
make_scoped_ptr(it.second.release())); base::WrapUnique(it.second.release()));
} }
handlers->clear(); protocol_handlers->clear();
job_factory->SetProtocolHandler( job_factory->SetProtocolHandler(
url::kDataScheme, make_scoped_ptr(new net::DataProtocolHandler)); url::kDataScheme, make_scoped_ptr(new net::DataProtocolHandler));
@ -132,16 +132,7 @@ AtomBrowserContext::CreateURLRequestJobFactory(
make_scoped_ptr(new net::FtpProtocolHandler( make_scoped_ptr(new net::FtpProtocolHandler(
new net::FtpNetworkLayer(host_resolver)))); new net::FtpNetworkLayer(host_resolver))));
// Set up interceptors in the reverse order. return std::move(job_factory);
std::unique_ptr<net::URLRequestJobFactory> 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;
} }
net::HttpCache::BackendFactory* net::HttpCache::BackendFactory*

View file

@ -15,7 +15,6 @@ class AtomDownloadManagerDelegate;
class AtomCertVerifier; class AtomCertVerifier;
class AtomNetworkDelegate; class AtomNetworkDelegate;
class AtomPermissionManager; class AtomPermissionManager;
class AtomURLRequestJobFactory;
class WebViewManager; class WebViewManager;
class AtomBrowserContext : public brightray::BrowserContext { class AtomBrowserContext : public brightray::BrowserContext {
@ -27,8 +26,7 @@ class AtomBrowserContext : public brightray::BrowserContext {
net::NetworkDelegate* CreateNetworkDelegate() override; net::NetworkDelegate* CreateNetworkDelegate() override;
std::string GetUserAgent() override; std::string GetUserAgent() override;
std::unique_ptr<net::URLRequestJobFactory> CreateURLRequestJobFactory( std::unique_ptr<net::URLRequestJobFactory> CreateURLRequestJobFactory(
content::ProtocolHandlerMap* handlers, content::ProtocolHandlerMap* protocol_handlers) override;
content::URLRequestInterceptorScopedVector* interceptors) override;
net::HttpCache::BackendFactory* CreateHttpCacheBackendFactory( net::HttpCache::BackendFactory* CreateHttpCacheBackendFactory(
const base::FilePath& base_path) override; const base::FilePath& base_path) override;
std::unique_ptr<net::CertVerifier> CreateCertVerifier() override; std::unique_ptr<net::CertVerifier> CreateCertVerifier() override;
@ -43,9 +41,6 @@ class AtomBrowserContext : public brightray::BrowserContext {
void RegisterPrefs(PrefRegistrySimple* pref_registry) override; void RegisterPrefs(PrefRegistrySimple* pref_registry) override;
AtomCertVerifier* cert_verifier() const { return cert_verifier_; } AtomCertVerifier* cert_verifier() const { return cert_verifier_; }
AtomURLRequestJobFactory* job_factory() const { return job_factory_; }
AtomNetworkDelegate* network_delegate() const { return network_delegate_; } AtomNetworkDelegate* network_delegate() const { return network_delegate_; }
private: private:
@ -55,7 +50,6 @@ class AtomBrowserContext : public brightray::BrowserContext {
// Managed by brightray::BrowserContext. // Managed by brightray::BrowserContext.
AtomCertVerifier* cert_verifier_; AtomCertVerifier* cert_verifier_;
AtomURLRequestJobFactory* job_factory_;
AtomNetworkDelegate* network_delegate_; AtomNetworkDelegate* network_delegate_;
DISALLOW_COPY_AND_ASSIGN(AtomBrowserContext); DISALLOW_COPY_AND_ASSIGN(AtomBrowserContext);

2
vendor/brightray vendored

@ -1 +1 @@
Subproject commit 55423bdca586a20fe052f6c0e19565710dcebd42 Subproject commit 717d92968d7959814060b9409c4720ab647bd90e