re-use request context from IOThread

This commit is contained in:
deepak1556 2018-03-15 15:29:16 +09:00 committed by Aleksei Kuzmin
parent 97fcf7079b
commit c3f8f6bc42
5 changed files with 20 additions and 62 deletions

View file

@ -47,9 +47,6 @@
#include "content/public/common/url_constants.h" #include "content/public/common/url_constants.h"
#include "content/public/common/web_preferences.h" #include "content/public/common/web_preferences.h"
#include "net/ssl/ssl_cert_request_info.h" #include "net/ssl/ssl_cert_request_info.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_context_builder.h"
#include "net/url_request/url_request_context_getter.h"
#include "ppapi/host/ppapi_host.h" #include "ppapi/host/ppapi_host.h"
#include "ui/base/l10n/l10n_util.h" #include "ui/base/l10n/l10n_util.h"
#include "v8/include/v8.h" #include "v8/include/v8.h"
@ -69,45 +66,6 @@ std::string g_custom_service_worker_schemes = "";
void Noop(scoped_refptr<content::SiteInstance>) { void Noop(scoped_refptr<content::SiteInstance>) {
} }
class GeoURLRequestContextGetter : public net::URLRequestContextGetter {
public:
GeoURLRequestContextGetter() = default;
~GeoURLRequestContextGetter() override {
if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) {
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::Bind(&GeoURLRequestContextGetter::NotifyContextShutdownOnIO,
this));
}
}
void NotifyContextShutdownOnIO() {
url_request_context_.reset();
net::URLRequestContextGetter::NotifyContextShuttingDown();
}
net::URLRequestContext* GetURLRequestContext() override {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
if (!url_request_context_.get()) {
net::URLRequestContextBuilder builder;
builder.set_proxy_config_service(
net::ProxyService::CreateSystemProxyConfigService(
BrowserThread::GetTaskRunnerForThread(BrowserThread::IO)));
url_request_context_ = builder.Build();
}
return url_request_context_.get();
}
scoped_refptr<base::SingleThreadTaskRunner> GetNetworkTaskRunner()
const override {
return BrowserThread::GetTaskRunnerForThread(BrowserThread::IO);
}
private:
std::unique_ptr<net::URLRequestContext> url_request_context_;
DISALLOW_COPY_AND_ASSIGN(GeoURLRequestContextGetter);
};
} // namespace } // namespace
// static // static
@ -120,9 +78,7 @@ void AtomBrowserClient::SetCustomServiceWorkerSchemes(
g_custom_service_worker_schemes = base::JoinString(schemes, ","); g_custom_service_worker_schemes = base::JoinString(schemes, ",");
} }
AtomBrowserClient::AtomBrowserClient() AtomBrowserClient::AtomBrowserClient() : delegate_(nullptr) {}
: delegate_(nullptr),
geo_request_context_getter_(new GeoURLRequestContextGetter) {}
AtomBrowserClient::~AtomBrowserClient() { AtomBrowserClient::~AtomBrowserClient() {
} }
@ -384,20 +340,14 @@ void AtomBrowserClient::DidCreatePpapiPlugin(
base::WrapUnique(new chrome::ChromeBrowserPepperHostFactory(host))); base::WrapUnique(new chrome::ChromeBrowserPepperHostFactory(host)));
} }
scoped_refptr<net::URLRequestContextGetter>
AtomBrowserClient::GetGeoRequestContextGetterFromUIThread() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
return geo_request_context_getter_;
}
void AtomBrowserClient::GetGeolocationRequestContext( void AtomBrowserClient::GetGeolocationRequestContext(
base::OnceCallback<void(scoped_refptr<net::URLRequestContextGetter>)> base::OnceCallback<void(scoped_refptr<net::URLRequestContextGetter>)>
callback) { callback) {
BrowserThread::PostTaskAndReplyWithResult( auto io_thread = AtomBrowserMainParts::Get()->io_thread();
BrowserThread::UI, FROM_HERE, auto context = io_thread->GetRequestContext();
base::BindOnce(&AtomBrowserClient::GetGeoRequestContextGetterFromUIThread, base::ThreadTaskRunnerHandle::Get()->PostTask(
base::Unretained(this)), FROM_HERE,
std::move(callback)); base::BindOnce(std::move(callback), base::RetainedRef(context)));
} }
std::string AtomBrowserClient::GetGeolocationApiKey() { std::string AtomBrowserClient::GetGeolocationApiKey() {

View file

@ -21,7 +21,6 @@ class ClientCertificateDelegate;
namespace net { namespace net {
class SSLCertRequestInfo; class SSLCertRequestInfo;
class URLRequestContextGetter;
} }
namespace atom { namespace atom {
@ -134,8 +133,6 @@ class AtomBrowserClient : public brightray::BrowserClient,
bool IsRendererSandboxed(int process_id); bool IsRendererSandboxed(int process_id);
bool RendererUsesNativeWindowOpen(int process_id); bool RendererUsesNativeWindowOpen(int process_id);
bool RendererDisablesPopups(int process_id); bool RendererDisablesPopups(int process_id);
scoped_refptr<net::URLRequestContextGetter>
GetGeoRequestContextGetterFromUIThread();
// pending_render_process => web contents. // pending_render_process => web contents.
std::map<int, content::WebContents*> pending_processes_; std::map<int, content::WebContents*> pending_processes_;
@ -151,8 +148,6 @@ class AtomBrowserClient : public brightray::BrowserClient,
Delegate* delegate_; Delegate* delegate_;
scoped_refptr<net::URLRequestContextGetter> geo_request_context_getter_;
DISALLOW_COPY_AND_ASSIGN(AtomBrowserClient); DISALLOW_COPY_AND_ASSIGN(AtomBrowserClient);
}; };

View file

@ -35,6 +35,8 @@ class BrowserMainParts : public content::BrowserMainParts {
BrowserMainParts(); BrowserMainParts();
~BrowserMainParts(); ~BrowserMainParts();
IOThread* io_thread() const { return io_thread_.get(); }
protected: protected:
// content::BrowserMainParts: // content::BrowserMainParts:
void PreEarlyInitialization() override; void PreEarlyInitialization() override;

View file

@ -8,6 +8,7 @@
#include "net/proxy/proxy_service.h" #include "net/proxy/proxy_service.h"
#include "net/url_request/url_request_context.h" #include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_context_builder.h" #include "net/url_request/url_request_context_builder.h"
#include "net/url_request/url_request_context_getter.h"
#if defined(USE_NSS_CERTS) #if defined(USE_NSS_CERTS)
#include "net/cert_net/nss_ocsp.h" #include "net/cert_net/nss_ocsp.h"
@ -28,8 +29,10 @@ IOThread::~IOThread() {
void IOThread::Init() { void IOThread::Init() {
net::URLRequestContextBuilder builder; net::URLRequestContextBuilder builder;
builder.set_proxy_service(net::ProxyService::CreateDirect()); builder.set_proxy_service(net::ProxyService::CreateDirect());
builder.DisableHttpCache();
url_request_context_ = builder.Build(); url_request_context_ = builder.Build();
url_request_context_getter_ = new net::TrivialURLRequestContextGetter(
url_request_context_.get(), base::ThreadTaskRunnerHandle::Get());
url_request_context_getter_->AddRef();
#if defined(USE_NSS_CERTS) #if defined(USE_NSS_CERTS)
net::SetMessageLoopForNSSHttpIO(); net::SetMessageLoopForNSSHttpIO();
@ -42,6 +45,8 @@ void IOThread::CleanUp() {
net::ShutdownNSSHttpIO(); net::ShutdownNSSHttpIO();
net::SetURLRequestContextForNSSHttpIO(nullptr); net::SetURLRequestContextForNSSHttpIO(nullptr);
#endif #endif
// Explicitly release before the IO thread gets destroyed.
url_request_context_getter_->Release();
url_request_context_.reset(); url_request_context_.reset();
} }

View file

@ -12,6 +12,7 @@
namespace net { namespace net {
class URLRequestContext; class URLRequestContext;
class URLRequestContextGetter;
} }
namespace brightray { namespace brightray {
@ -21,6 +22,10 @@ class IOThread : public content::BrowserThreadDelegate {
IOThread(); IOThread();
~IOThread() override; ~IOThread() override;
net::URLRequestContextGetter* GetRequestContext() {
return url_request_context_getter_;
}
protected: protected:
// BrowserThreadDelegate Implementation, runs on the IO thread. // BrowserThreadDelegate Implementation, runs on the IO thread.
void Init() override; void Init() override;
@ -28,6 +33,7 @@ class IOThread : public content::BrowserThreadDelegate {
private: private:
std::unique_ptr<net::URLRequestContext> url_request_context_; std::unique_ptr<net::URLRequestContext> url_request_context_;
net::URLRequestContextGetter* url_request_context_getter_;
DISALLOW_COPY_AND_ASSIGN(IOThread); DISALLOW_COPY_AND_ASSIGN(IOThread);
}; };