From abe1faea5cc12508c970657f023c71213895c944 Mon Sep 17 00:00:00 2001 From: deepak1556 Date: Mon, 18 Dec 2017 17:27:20 +0530 Subject: [PATCH] REVIEW: setup request context for NSS OCSP only once --- brightray/browser/browser_main_parts.cc | 5 ++ brightray/browser/browser_main_parts.h | 3 ++ brightray/browser/io_thread.cc | 48 +++++++++++++++++++ brightray/browser/io_thread.h | 37 ++++++++++++++ .../browser/url_request_context_getter.cc | 11 ----- brightray/filenames.gypi | 2 + 6 files changed, 95 insertions(+), 11 deletions(-) create mode 100644 brightray/browser/io_thread.cc create mode 100644 brightray/browser/io_thread.h diff --git a/brightray/browser/browser_main_parts.cc b/brightray/browser/browser_main_parts.cc index 736f068cc27e..0754c0c824fb 100644 --- a/brightray/browser/browser_main_parts.cc +++ b/brightray/browser/browser_main_parts.cc @@ -279,6 +279,9 @@ int BrowserMainParts::PreCreateThreads() { // Initialize the app locale. BrowserClient::SetApplicationLocale(l10n_util::GetApplicationLocale("")); + // Manage global state of net and other IO thread related. + io_thread_ = base::MakeUnique(); + return 0; } @@ -287,6 +290,8 @@ void BrowserMainParts::PostDestroyThreads() { device::BluetoothAdapterFactory::Shutdown(); bluez::DBusBluezManagerWrapperLinux::Shutdown(); #endif + + io_thread_.reset(); } } // namespace brightray diff --git a/brightray/browser/browser_main_parts.h b/brightray/browser/browser_main_parts.h index 45c69f15fb07..55474d442a3d 100644 --- a/brightray/browser/browser_main_parts.h +++ b/brightray/browser/browser_main_parts.h @@ -11,6 +11,7 @@ #include "base/macros.h" #include "base/path_service.h" #include "brightray/browser/brightray_paths.h" +#include "brightray/browser/io_thread.h" #include "content/public/browser/browser_main_parts.h" #include "ui/views/layout/layout_provider.h" @@ -50,6 +51,8 @@ class BrowserMainParts : public content::BrowserMainParts { void OverrideAppLogsPath(); #endif + std::unique_ptr io_thread_; + #if defined(TOOLKIT_VIEWS) std::unique_ptr views_delegate_; #endif diff --git a/brightray/browser/io_thread.cc b/brightray/browser/io_thread.cc new file mode 100644 index 000000000000..f20fb0bf9ebd --- /dev/null +++ b/brightray/browser/io_thread.cc @@ -0,0 +1,48 @@ +// Copyright (c) 2017 GitHub, Inc. +// Use of this source code is governed by the MIT license that can be +// found in the LICENSE file. + +#include "brightray/browser/io_thread.h" + +#include "content/public/browser/browser_thread.h" +#include "net/proxy/proxy_service.h" +#include "net/url_request/url_request_context.h" +#include "net/url_request/url_request_context_builder.h" + +#if defined(USE_NSS_CERTS) +#include "net/cert_net/nss_ocsp.h" +#endif + +using content::BrowserThread; + +namespace brightray { + +IOThread::IOThread() { + BrowserThread::SetIOThreadDelegate(this); +} + +IOThread::~IOThread() { + BrowserThread::SetIOThreadDelegate(nullptr); +} + +void IOThread::Init() { + net::URLRequestContextBuilder builder; + builder.set_proxy_service(net::ProxyService::CreateDirect()); + builder.DisableHttpCache(); + url_request_context_ = builder.Build(); + +#if defined(USE_NSS_CERTS) + net::SetMessageLoopForNSSHttpIO(); + net::SetURLRequestContextForNSSHttpIO(url_request_context_.get()); +#endif +} + +void IOThread::CleanUp() { +#if defined(USE_NSS_CERTS) + net::ShutdownNSSHttpIO(); + net::SetURLRequestContextForNSSHttpIO(nullptr); +#endif + url_request_context_.reset(); +} + +} // namespace brightray diff --git a/brightray/browser/io_thread.h b/brightray/browser/io_thread.h new file mode 100644 index 000000000000..c04f09fa8a9f --- /dev/null +++ b/brightray/browser/io_thread.h @@ -0,0 +1,37 @@ +// Copyright (c) 2017 GitHub, Inc. +// Use of this source code is governed by the MIT license that can be +// found in the LICENSE file. + +#ifndef BRIGHTRAY_BROWSER_IO_THREAD_H_ +#define BRIGHTRAY_BROWSER_IO_THREAD_H_ + +#include + +#include "base/macros.h" +#include "content/public/browser/browser_thread_delegate.h" + +namespace net { +class URLRequestContext; +} + +namespace brightray { + +class IOThread : public content::BrowserThreadDelegate { + public: + IOThread(); + ~IOThread() override; + + protected: + // BrowserThreadDelegate Implementation, runs on the IO thread. + void Init() override; + void CleanUp() override; + + private: + std::unique_ptr url_request_context_; + + DISALLOW_COPY_AND_ASSIGN(IOThread); +}; + +} // namespace brightray + +#endif // BRIGHTRAY_BROWSER_IO_THREAD_H_ diff --git a/brightray/browser/url_request_context_getter.cc b/brightray/browser/url_request_context_getter.cc index 21bbd1d4c827..28e988f4b49c 100644 --- a/brightray/browser/url_request_context_getter.cc +++ b/brightray/browser/url_request_context_getter.cc @@ -56,10 +56,6 @@ #include "storage/browser/quota/special_storage_policy.h" #include "url/url_constants.h" -#if defined(USE_NSS_CERTS) -#include "net/cert_net/nss_ocsp.h" -#endif - using content::BrowserThread; namespace brightray { @@ -158,9 +154,6 @@ URLRequestContextGetter::URLRequestContextGetter( } URLRequestContextGetter::~URLRequestContextGetter() { -#if defined(USE_NSS_CERTS) - net::SetURLRequestContextForNSSHttpIO(NULL); -#endif } net::HostResolver* URLRequestContextGetter::host_resolver() { @@ -175,10 +168,6 @@ net::URLRequestContext* URLRequestContextGetter::GetURLRequestContext() { auto& command_line = *base::CommandLine::ForCurrentProcess(); url_request_context_.reset(new net::URLRequestContext); -#if defined(USE_NSS_CERTS) - net::SetURLRequestContextForNSSHttpIO(url_request_context_.get()); -#endif - // --log-net-log if (net_log_) { net_log_->StartLogging(); diff --git a/brightray/filenames.gypi b/brightray/filenames.gypi index 0c6efa3a7f01..473696feb889 100644 --- a/brightray/filenames.gypi +++ b/brightray/filenames.gypi @@ -29,6 +29,8 @@ 'browser/inspectable_web_contents_view.h', 'browser/inspectable_web_contents_view_mac.h', 'browser/inspectable_web_contents_view_mac.mm', + 'browser/io_thread.cc', + 'browser/io_thread.h', 'browser/mac/bry_inspectable_web_contents_view.h', 'browser/mac/bry_inspectable_web_contents_view.mm', 'browser/mac/cocoa_notification.h',