From c3f8f6bc427c34bff7e121afb2b5c93175a973d0 Mon Sep 17 00:00:00 2001 From: deepak1556 Date: Thu, 15 Mar 2018 15:29:16 +0900 Subject: [PATCH] re-use request context from IOThread --- atom/browser/atom_browser_client.cc | 62 +++----------------------- atom/browser/atom_browser_client.h | 5 --- brightray/browser/browser_main_parts.h | 2 + brightray/browser/io_thread.cc | 7 ++- brightray/browser/io_thread.h | 6 +++ 5 files changed, 20 insertions(+), 62 deletions(-) diff --git a/atom/browser/atom_browser_client.cc b/atom/browser/atom_browser_client.cc index 705d1fbbc9cb..07f74b49da65 100644 --- a/atom/browser/atom_browser_client.cc +++ b/atom/browser/atom_browser_client.cc @@ -47,9 +47,6 @@ #include "content/public/common/url_constants.h" #include "content/public/common/web_preferences.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 "ui/base/l10n/l10n_util.h" #include "v8/include/v8.h" @@ -69,45 +66,6 @@ std::string g_custom_service_worker_schemes = ""; void Noop(scoped_refptr) { } -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 GetNetworkTaskRunner() - const override { - return BrowserThread::GetTaskRunnerForThread(BrowserThread::IO); - } - - private: - std::unique_ptr url_request_context_; - DISALLOW_COPY_AND_ASSIGN(GeoURLRequestContextGetter); -}; - } // namespace // static @@ -120,9 +78,7 @@ void AtomBrowserClient::SetCustomServiceWorkerSchemes( g_custom_service_worker_schemes = base::JoinString(schemes, ","); } -AtomBrowserClient::AtomBrowserClient() - : delegate_(nullptr), - geo_request_context_getter_(new GeoURLRequestContextGetter) {} +AtomBrowserClient::AtomBrowserClient() : delegate_(nullptr) {} AtomBrowserClient::~AtomBrowserClient() { } @@ -384,20 +340,14 @@ void AtomBrowserClient::DidCreatePpapiPlugin( base::WrapUnique(new chrome::ChromeBrowserPepperHostFactory(host))); } -scoped_refptr -AtomBrowserClient::GetGeoRequestContextGetterFromUIThread() { - DCHECK_CURRENTLY_ON(BrowserThread::UI); - return geo_request_context_getter_; -} - void AtomBrowserClient::GetGeolocationRequestContext( base::OnceCallback)> callback) { - BrowserThread::PostTaskAndReplyWithResult( - BrowserThread::UI, FROM_HERE, - base::BindOnce(&AtomBrowserClient::GetGeoRequestContextGetterFromUIThread, - base::Unretained(this)), - std::move(callback)); + auto io_thread = AtomBrowserMainParts::Get()->io_thread(); + auto context = io_thread->GetRequestContext(); + base::ThreadTaskRunnerHandle::Get()->PostTask( + FROM_HERE, + base::BindOnce(std::move(callback), base::RetainedRef(context))); } std::string AtomBrowserClient::GetGeolocationApiKey() { diff --git a/atom/browser/atom_browser_client.h b/atom/browser/atom_browser_client.h index 8e085da58aca..ceacd354ab3d 100644 --- a/atom/browser/atom_browser_client.h +++ b/atom/browser/atom_browser_client.h @@ -21,7 +21,6 @@ class ClientCertificateDelegate; namespace net { class SSLCertRequestInfo; -class URLRequestContextGetter; } namespace atom { @@ -134,8 +133,6 @@ class AtomBrowserClient : public brightray::BrowserClient, bool IsRendererSandboxed(int process_id); bool RendererUsesNativeWindowOpen(int process_id); bool RendererDisablesPopups(int process_id); - scoped_refptr - GetGeoRequestContextGetterFromUIThread(); // pending_render_process => web contents. std::map pending_processes_; @@ -151,8 +148,6 @@ class AtomBrowserClient : public brightray::BrowserClient, Delegate* delegate_; - scoped_refptr geo_request_context_getter_; - DISALLOW_COPY_AND_ASSIGN(AtomBrowserClient); }; diff --git a/brightray/browser/browser_main_parts.h b/brightray/browser/browser_main_parts.h index f69682ce5161..673d50228f1d 100644 --- a/brightray/browser/browser_main_parts.h +++ b/brightray/browser/browser_main_parts.h @@ -35,6 +35,8 @@ class BrowserMainParts : public content::BrowserMainParts { BrowserMainParts(); ~BrowserMainParts(); + IOThread* io_thread() const { return io_thread_.get(); } + protected: // content::BrowserMainParts: void PreEarlyInitialization() override; diff --git a/brightray/browser/io_thread.cc b/brightray/browser/io_thread.cc index f20fb0bf9ebd..c96aa9806517 100644 --- a/brightray/browser/io_thread.cc +++ b/brightray/browser/io_thread.cc @@ -8,6 +8,7 @@ #include "net/proxy/proxy_service.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" #if defined(USE_NSS_CERTS) #include "net/cert_net/nss_ocsp.h" @@ -28,8 +29,10 @@ IOThread::~IOThread() { void IOThread::Init() { net::URLRequestContextBuilder builder; builder.set_proxy_service(net::ProxyService::CreateDirect()); - builder.DisableHttpCache(); 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) net::SetMessageLoopForNSSHttpIO(); @@ -42,6 +45,8 @@ void IOThread::CleanUp() { net::ShutdownNSSHttpIO(); net::SetURLRequestContextForNSSHttpIO(nullptr); #endif + // Explicitly release before the IO thread gets destroyed. + url_request_context_getter_->Release(); url_request_context_.reset(); } diff --git a/brightray/browser/io_thread.h b/brightray/browser/io_thread.h index c04f09fa8a9f..866008eb4e45 100644 --- a/brightray/browser/io_thread.h +++ b/brightray/browser/io_thread.h @@ -12,6 +12,7 @@ namespace net { class URLRequestContext; +class URLRequestContextGetter; } namespace brightray { @@ -21,6 +22,10 @@ class IOThread : public content::BrowserThreadDelegate { IOThread(); ~IOThread() override; + net::URLRequestContextGetter* GetRequestContext() { + return url_request_context_getter_; + } + protected: // BrowserThreadDelegate Implementation, runs on the IO thread. void Init() override; @@ -28,6 +33,7 @@ class IOThread : public content::BrowserThreadDelegate { private: std::unique_ptr url_request_context_; + net::URLRequestContextGetter* url_request_context_getter_; DISALLOW_COPY_AND_ASSIGN(IOThread); };