From 80b220d2140ad46e276176b72c5afd44fe27b0c9 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 12 Jan 2024 07:50:29 -0600 Subject: [PATCH] refactor: use base::NoDestructor instead of base::LazyInstance (#40947) * refactor: use NoDestructor for g_io_thread_application_locale * refactor: use NoDestructor for ExtensionActionAPI::GetFactoryInstance() * refactor: use NoDestructor for ElectronExtensionsClient::GetPermissionMessageProvider() * refactor: use NoDestructor for feat_add_support_for_overriding_the_base_spellchecker_download_url.patch * chore: remove unused #include * fixup! refactor: use NoDestructor for ElectronExtensionsClient::GetPermissionMessageProvider() make sure instance is static * chore: remove unused #include "base/lazy_instance.h" --- ...g_the_base_spellchecker_download_url.patch | 21 ++++++++++++------- shell/browser/electron_browser_client.cc | 14 ++++++------- .../extension_action/extension_action_api.cc | 9 ++++---- .../electron_extension_host_delegate.cc | 1 - shell/common/asar/asar_util.cc | 1 - .../extensions/electron_extensions_client.cc | 9 ++++---- 6 files changed, 28 insertions(+), 27 deletions(-) diff --git a/patches/chromium/feat_add_support_for_overriding_the_base_spellchecker_download_url.patch b/patches/chromium/feat_add_support_for_overriding_the_base_spellchecker_download_url.patch index 04dacdd4e008..1004ab199285 100644 --- a/patches/chromium/feat_add_support_for_overriding_the_base_spellchecker_download_url.patch +++ b/patches/chromium/feat_add_support_for_overriding_the_base_spellchecker_download_url.patch @@ -9,15 +9,22 @@ production use cases. This is unlikely to be upstreamed as the change is entirely in //chrome. diff --git a/chrome/browser/spellchecker/spellcheck_hunspell_dictionary.cc b/chrome/browser/spellchecker/spellcheck_hunspell_dictionary.cc -index 48b7a20c212578ba9055b781b5c05b312fa7e974..3ae8136e5c938be80df141f7ca582d974fb08eed 100644 +index 48b7a20c212578ba9055b781b5c05b312fa7e974..d7d92f326ba94be7a3960d527bc2b6d8d15890fa 100644 --- a/chrome/browser/spellchecker/spellcheck_hunspell_dictionary.cc +++ b/chrome/browser/spellchecker/spellcheck_hunspell_dictionary.cc -@@ -49,6 +49,9 @@ namespace { +@@ -13,6 +13,7 @@ + #include "base/functional/bind.h" + #include "base/lazy_instance.h" + #include "base/location.h" ++#include "base/no_destructor.h" + #include "base/notreached.h" + #include "base/observer_list.h" + #include "base/path_service.h" +@@ -49,6 +50,8 @@ namespace { base::LazyInstance::Leaky g_download_url_for_testing = LAZY_INSTANCE_INITIALIZER; -+base::LazyInstance::Leaky g_base_download_url_override = -+ LAZY_INSTANCE_INITIALIZER; ++base::NoDestructor g_base_download_url_override; + // Close the file. void CloseDictionary(base::File file) { @@ -27,7 +34,7 @@ index 48b7a20c212578ba9055b781b5c05b312fa7e974..3ae8136e5c938be80df141f7ca582d97 } +void SpellcheckHunspellDictionary::SetBaseDownloadURL(const GURL url) { -+ g_base_download_url_override.Get() = url; ++ *g_base_download_url_override = url; +} + GURL SpellcheckHunspellDictionary::GetDictionaryURL() { @@ -37,8 +44,8 @@ index 48b7a20c212578ba9055b781b5c05b312fa7e974..3ae8136e5c938be80df141f7ca582d97 std::string bdict_file = dictionary_file_.path.BaseName().MaybeAsASCII(); DCHECK(!bdict_file.empty()); -+ if (g_base_download_url_override.Get() != GURL()) -+ return GURL(g_base_download_url_override.Get().spec() + base::ToLowerASCII(bdict_file)); ++ if (*g_base_download_url_override != GURL()) ++ return GURL(g_base_download_url_override->spec() + base::ToLowerASCII(bdict_file)); + static const char kDownloadServerUrl[] = "https://redirector.gvt1.com/edgedl/chrome/dict/"; diff --git a/shell/browser/electron_browser_client.cc b/shell/browser/electron_browser_client.cc index 71ff54572628..a5fcbcea7892 100644 --- a/shell/browser/electron_browser_client.cc +++ b/shell/browser/electron_browser_client.cc @@ -17,7 +17,6 @@ #include "base/environment.h" #include "base/files/file_util.h" #include "base/json/json_reader.h" -#include "base/lazy_instance.h" #include "base/no_destructor.h" #include "base/path_service.h" #include "base/stl_util.h" @@ -226,14 +225,13 @@ namespace { ElectronBrowserClient* g_browser_client = nullptr; -base::LazyInstance::DestructorAtExit - g_io_thread_application_locale = LAZY_INSTANCE_INITIALIZER; +base::NoDestructor g_io_thread_application_locale; base::NoDestructor g_application_locale; void SetApplicationLocaleOnIOThread(const std::string& locale) { DCHECK_CURRENTLY_ON(BrowserThread::IO); - g_io_thread_application_locale.Get() = locale; + *g_io_thread_application_locale = locale; } void BindNetworkHintsHandler( @@ -342,7 +340,7 @@ void ElectronBrowserClient::SetApplicationLocale(const std::string& locale) { if (!BrowserThread::IsThreadInitialized(BrowserThread::IO) || !content::GetIOThreadTaskRunner({})->PostTask( FROM_HERE, base::BindOnce(&SetApplicationLocaleOnIOThread, locale))) { - g_io_thread_application_locale.Get() = locale; + *g_io_thread_application_locale = locale; } *g_application_locale = locale; } @@ -1517,9 +1515,9 @@ void ElectronBrowserClient:: } std::string ElectronBrowserClient::GetApplicationLocale() { - if (BrowserThread::CurrentlyOn(BrowserThread::IO)) - return g_io_thread_application_locale.Get(); - return *g_application_locale; + return BrowserThread::CurrentlyOn(BrowserThread::IO) + ? *g_io_thread_application_locale + : *g_application_locale; } bool ElectronBrowserClient::ShouldEnableStrictSiteIsolation() { diff --git a/shell/browser/extensions/api/extension_action/extension_action_api.cc b/shell/browser/extensions/api/extension_action/extension_action_api.cc index 9c1df9450bca..6fc338166133 100644 --- a/shell/browser/extensions/api/extension_action/extension_action_api.cc +++ b/shell/browser/extensions/api/extension_action/extension_action_api.cc @@ -10,7 +10,7 @@ #include #include "base/functional/bind.h" -#include "base/lazy_instance.h" +#include "base/no_destructor.h" #include "base/observer_list.h" #include "extensions/browser/event_router.h" #include "extensions/browser/extension_prefs.h" @@ -38,9 +38,6 @@ ExtensionActionAPI::Observer::~Observer() {} // ExtensionActionAPI // -static base::LazyInstance>:: - DestructorAtExit g_extension_action_api_factory = LAZY_INSTANCE_INITIALIZER; - ExtensionActionAPI::ExtensionActionAPI(content::BrowserContext* context) : browser_context_(context), extension_prefs_(nullptr) {} @@ -49,7 +46,9 @@ ExtensionActionAPI::~ExtensionActionAPI() {} // static BrowserContextKeyedAPIFactory* ExtensionActionAPI::GetFactoryInstance() { - return g_extension_action_api_factory.Pointer(); + static base::NoDestructor> + instance; + return instance.get(); } // static diff --git a/shell/browser/extensions/electron_extension_host_delegate.cc b/shell/browser/extensions/electron_extension_host_delegate.cc index e2eaf83e174e..d256517f2efc 100644 --- a/shell/browser/extensions/electron_extension_host_delegate.cc +++ b/shell/browser/extensions/electron_extension_host_delegate.cc @@ -8,7 +8,6 @@ #include #include -#include "base/lazy_instance.h" #include "base/logging.h" #include "extensions/browser/media_capture_util.h" #include "shell/browser/api/electron_api_web_contents.h" diff --git a/shell/common/asar/asar_util.cc b/shell/common/asar/asar_util.cc index 56999252dc5a..f3dc4a53a417 100644 --- a/shell/common/asar/asar_util.cc +++ b/shell/common/asar/asar_util.cc @@ -10,7 +10,6 @@ #include "base/files/file_path.h" #include "base/files/file_util.h" -#include "base/lazy_instance.h" #include "base/logging.h" #include "base/no_destructor.h" #include "base/stl_util.h" diff --git a/shell/common/extensions/electron_extensions_client.cc b/shell/common/extensions/electron_extensions_client.cc index 76baa9df41c5..8234474a2d8f 100644 --- a/shell/common/extensions/electron_extensions_client.cc +++ b/shell/common/extensions/electron_extensions_client.cc @@ -7,8 +7,8 @@ #include #include -#include "base/lazy_instance.h" #include "base/logging.h" +#include "base/no_destructor.h" #include "components/version_info/version_info.h" #include "content/public/common/user_agent.h" #include "extensions/common/core_extensions_api_provider.h" @@ -60,9 +60,6 @@ class ElectronPermissionMessageProvider } }; -base::LazyInstance::DestructorAtExit - g_permission_message_provider = LAZY_INSTANCE_INITIALIZER; - } // namespace ElectronExtensionsClient::ElectronExtensionsClient() @@ -85,7 +82,9 @@ void ElectronExtensionsClient::InitializeWebStoreUrls( const extensions::PermissionMessageProvider& ElectronExtensionsClient::GetPermissionMessageProvider() const { NOTIMPLEMENTED(); - return g_permission_message_provider.Get(); + + static base::NoDestructor instance; + return *instance; } const std::string ElectronExtensionsClient::GetProductName() {