From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Jeremy Apthorp <nornagon@nornagon.net>
Date: Wed, 21 Nov 2018 14:31:34 -0800
Subject: content: allow embedder to prevent locking scheme registry

The //content layer requires all schemes to be registered during startup,
because Add*Scheme aren't threadsafe. However, Electron exposes the option to
register additional schemes via JavaScript in the main process before the app
is ready, but after the //content layer has already locked the registry.

Without this patch, calling `registerStandardSchemes` during initialization
when in debug mode will cause a DCHECK to fire.

diff --git a/content/app/content_main_runner_impl.cc b/content/app/content_main_runner_impl.cc
index a62f2ecf52bb95261750c9236bd2ba1f53d737f4..38b25c5befd88659b2769d69ef1323de60b34ec7 100644
--- a/content/app/content_main_runner_impl.cc
+++ b/content/app/content_main_runner_impl.cc
@@ -749,7 +749,7 @@ int ContentMainRunnerImpl::Initialize(const ContentMainParams& params) {
 #endif
 
     RegisterPathProvider();
-    RegisterContentSchemes(true);
+    RegisterContentSchemes(delegate_->ShouldLockSchemeRegistry());
 
 #if defined(OS_ANDROID) && (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE)
     int icudata_fd = g_fds->MaybeGet(kAndroidICUDataDescriptor);
diff --git a/content/public/app/content_main_delegate.cc b/content/public/app/content_main_delegate.cc
index 456df421598153bde006ad0ecb0f1031360bb543..af06d78b91fa30e5daf993ad2c65c2c5e35a17a5 100644
--- a/content/public/app/content_main_delegate.cc
+++ b/content/public/app/content_main_delegate.cc
@@ -55,6 +55,10 @@ int ContentMainDelegate::TerminateForFatalInitializationError() {
   return 0;
 }
 
+bool ContentMainDelegate::ShouldLockSchemeRegistry() {
+  return true;
+}
+
 service_manager::ProcessType ContentMainDelegate::OverrideProcessType() {
   return service_manager::ProcessType::kDefault;
 }
diff --git a/content/public/app/content_main_delegate.h b/content/public/app/content_main_delegate.h
index f836a17fa0568aa33e204725f96233aac236aac5..af1d5ef18fb414a681bee48d36c3f94ed789c077 100644
--- a/content/public/app/content_main_delegate.h
+++ b/content/public/app/content_main_delegate.h
@@ -88,6 +88,9 @@ class CONTENT_EXPORT ContentMainDelegate {
   virtual void ZygoteForked() {}
 #endif  // defined(OS_LINUX)
 
+  // Allows the embedder to prevent locking the scheme registry.
+  virtual bool ShouldLockSchemeRegistry();
+
   // Fatal errors during initialization are reported by this function, so that
   // the embedder can implement graceful exit by displaying some message and
   // returning initialization error code. Default behavior is CHECK(false).
