Browse Source

REVIEW: setup request context for NSS OCSP only once

deepak1556 7 years ago
parent
commit
abe1faea5c

+ 5 - 0
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<IOThread>();
+
   return 0;
 }
 
@@ -287,6 +290,8 @@ void BrowserMainParts::PostDestroyThreads() {
   device::BluetoothAdapterFactory::Shutdown();
   bluez::DBusBluezManagerWrapperLinux::Shutdown();
 #endif
+
+  io_thread_.reset();
 }
 
 }  // namespace brightray

+ 3 - 0
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<IOThread> io_thread_;
+
 #if defined(TOOLKIT_VIEWS)
   std::unique_ptr<ViewsDelegate> views_delegate_;
 #endif

+ 48 - 0
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

+ 37 - 0
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 <memory>
+
+#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<net::URLRequestContext> url_request_context_;
+
+  DISALLOW_COPY_AND_ASSIGN(IOThread);
+};
+
+}  // namespace brightray
+
+#endif  // BRIGHTRAY_BROWSER_IO_THREAD_H_

+ 0 - 11
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();

+ 2 - 0
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',