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"
This commit is contained in:
Charles Kerr 2024-01-12 07:50:29 -06:00 committed by GitHub
parent 57b29903e3
commit 80b220d214
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 28 additions and 27 deletions

View file

@ -9,15 +9,22 @@ production use cases. This is unlikely to be upstreamed as the change
is entirely in //chrome. is entirely in //chrome.
diff --git a/chrome/browser/spellchecker/spellcheck_hunspell_dictionary.cc b/chrome/browser/spellchecker/spellcheck_hunspell_dictionary.cc 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 --- a/chrome/browser/spellchecker/spellcheck_hunspell_dictionary.cc
+++ b/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<GURL>::Leaky g_download_url_for_testing = base::LazyInstance<GURL>::Leaky g_download_url_for_testing =
LAZY_INSTANCE_INITIALIZER; LAZY_INSTANCE_INITIALIZER;
+base::LazyInstance<GURL>::Leaky g_base_download_url_override = +base::NoDestructor<GURL> g_base_download_url_override;
+ LAZY_INSTANCE_INITIALIZER;
+ +
// Close the file. // Close the file.
void CloseDictionary(base::File file) { void CloseDictionary(base::File file) {
@ -27,7 +34,7 @@ index 48b7a20c212578ba9055b781b5c05b312fa7e974..3ae8136e5c938be80df141f7ca582d97
} }
+void SpellcheckHunspellDictionary::SetBaseDownloadURL(const GURL url) { +void SpellcheckHunspellDictionary::SetBaseDownloadURL(const GURL url) {
+ g_base_download_url_override.Get() = url; + *g_base_download_url_override = url;
+} +}
+ +
GURL SpellcheckHunspellDictionary::GetDictionaryURL() { GURL SpellcheckHunspellDictionary::GetDictionaryURL() {
@ -37,8 +44,8 @@ index 48b7a20c212578ba9055b781b5c05b312fa7e974..3ae8136e5c938be80df141f7ca582d97
std::string bdict_file = dictionary_file_.path.BaseName().MaybeAsASCII(); std::string bdict_file = dictionary_file_.path.BaseName().MaybeAsASCII();
DCHECK(!bdict_file.empty()); DCHECK(!bdict_file.empty());
+ if (g_base_download_url_override.Get() != GURL()) + if (*g_base_download_url_override != GURL())
+ return GURL(g_base_download_url_override.Get().spec() + base::ToLowerASCII(bdict_file)); + return GURL(g_base_download_url_override->spec() + base::ToLowerASCII(bdict_file));
+ +
static const char kDownloadServerUrl[] = static const char kDownloadServerUrl[] =
"https://redirector.gvt1.com/edgedl/chrome/dict/"; "https://redirector.gvt1.com/edgedl/chrome/dict/";

View file

@ -17,7 +17,6 @@
#include "base/environment.h" #include "base/environment.h"
#include "base/files/file_util.h" #include "base/files/file_util.h"
#include "base/json/json_reader.h" #include "base/json/json_reader.h"
#include "base/lazy_instance.h"
#include "base/no_destructor.h" #include "base/no_destructor.h"
#include "base/path_service.h" #include "base/path_service.h"
#include "base/stl_util.h" #include "base/stl_util.h"
@ -226,14 +225,13 @@ namespace {
ElectronBrowserClient* g_browser_client = nullptr; ElectronBrowserClient* g_browser_client = nullptr;
base::LazyInstance<std::string>::DestructorAtExit base::NoDestructor<std::string> g_io_thread_application_locale;
g_io_thread_application_locale = LAZY_INSTANCE_INITIALIZER;
base::NoDestructor<std::string> g_application_locale; base::NoDestructor<std::string> g_application_locale;
void SetApplicationLocaleOnIOThread(const std::string& locale) { void SetApplicationLocaleOnIOThread(const std::string& locale) {
DCHECK_CURRENTLY_ON(BrowserThread::IO); DCHECK_CURRENTLY_ON(BrowserThread::IO);
g_io_thread_application_locale.Get() = locale; *g_io_thread_application_locale = locale;
} }
void BindNetworkHintsHandler( void BindNetworkHintsHandler(
@ -342,7 +340,7 @@ void ElectronBrowserClient::SetApplicationLocale(const std::string& locale) {
if (!BrowserThread::IsThreadInitialized(BrowserThread::IO) || if (!BrowserThread::IsThreadInitialized(BrowserThread::IO) ||
!content::GetIOThreadTaskRunner({})->PostTask( !content::GetIOThreadTaskRunner({})->PostTask(
FROM_HERE, base::BindOnce(&SetApplicationLocaleOnIOThread, locale))) { FROM_HERE, base::BindOnce(&SetApplicationLocaleOnIOThread, locale))) {
g_io_thread_application_locale.Get() = locale; *g_io_thread_application_locale = locale;
} }
*g_application_locale = locale; *g_application_locale = locale;
} }
@ -1517,9 +1515,9 @@ void ElectronBrowserClient::
} }
std::string ElectronBrowserClient::GetApplicationLocale() { std::string ElectronBrowserClient::GetApplicationLocale() {
if (BrowserThread::CurrentlyOn(BrowserThread::IO)) return BrowserThread::CurrentlyOn(BrowserThread::IO)
return g_io_thread_application_locale.Get(); ? *g_io_thread_application_locale
return *g_application_locale; : *g_application_locale;
} }
bool ElectronBrowserClient::ShouldEnableStrictSiteIsolation() { bool ElectronBrowserClient::ShouldEnableStrictSiteIsolation() {

View file

@ -10,7 +10,7 @@
#include <utility> #include <utility>
#include "base/functional/bind.h" #include "base/functional/bind.h"
#include "base/lazy_instance.h" #include "base/no_destructor.h"
#include "base/observer_list.h" #include "base/observer_list.h"
#include "extensions/browser/event_router.h" #include "extensions/browser/event_router.h"
#include "extensions/browser/extension_prefs.h" #include "extensions/browser/extension_prefs.h"
@ -38,9 +38,6 @@ ExtensionActionAPI::Observer::~Observer() {}
// ExtensionActionAPI // ExtensionActionAPI
// //
static base::LazyInstance<BrowserContextKeyedAPIFactory<ExtensionActionAPI>>::
DestructorAtExit g_extension_action_api_factory = LAZY_INSTANCE_INITIALIZER;
ExtensionActionAPI::ExtensionActionAPI(content::BrowserContext* context) ExtensionActionAPI::ExtensionActionAPI(content::BrowserContext* context)
: browser_context_(context), extension_prefs_(nullptr) {} : browser_context_(context), extension_prefs_(nullptr) {}
@ -49,7 +46,9 @@ ExtensionActionAPI::~ExtensionActionAPI() {}
// static // static
BrowserContextKeyedAPIFactory<ExtensionActionAPI>* BrowserContextKeyedAPIFactory<ExtensionActionAPI>*
ExtensionActionAPI::GetFactoryInstance() { ExtensionActionAPI::GetFactoryInstance() {
return g_extension_action_api_factory.Pointer(); static base::NoDestructor<BrowserContextKeyedAPIFactory<ExtensionActionAPI>>
instance;
return instance.get();
} }
// static // static

View file

@ -8,7 +8,6 @@
#include <string> #include <string>
#include <utility> #include <utility>
#include "base/lazy_instance.h"
#include "base/logging.h" #include "base/logging.h"
#include "extensions/browser/media_capture_util.h" #include "extensions/browser/media_capture_util.h"
#include "shell/browser/api/electron_api_web_contents.h" #include "shell/browser/api/electron_api_web_contents.h"

View file

@ -10,7 +10,6 @@
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "base/files/file_util.h" #include "base/files/file_util.h"
#include "base/lazy_instance.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/no_destructor.h" #include "base/no_destructor.h"
#include "base/stl_util.h" #include "base/stl_util.h"

View file

@ -7,8 +7,8 @@
#include <memory> #include <memory>
#include <string> #include <string>
#include "base/lazy_instance.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/no_destructor.h"
#include "components/version_info/version_info.h" #include "components/version_info/version_info.h"
#include "content/public/common/user_agent.h" #include "content/public/common/user_agent.h"
#include "extensions/common/core_extensions_api_provider.h" #include "extensions/common/core_extensions_api_provider.h"
@ -60,9 +60,6 @@ class ElectronPermissionMessageProvider
} }
}; };
base::LazyInstance<ElectronPermissionMessageProvider>::DestructorAtExit
g_permission_message_provider = LAZY_INSTANCE_INITIALIZER;
} // namespace } // namespace
ElectronExtensionsClient::ElectronExtensionsClient() ElectronExtensionsClient::ElectronExtensionsClient()
@ -85,7 +82,9 @@ void ElectronExtensionsClient::InitializeWebStoreUrls(
const extensions::PermissionMessageProvider& const extensions::PermissionMessageProvider&
ElectronExtensionsClient::GetPermissionMessageProvider() const { ElectronExtensionsClient::GetPermissionMessageProvider() const {
NOTIMPLEMENTED(); NOTIMPLEMENTED();
return g_permission_message_provider.Get();
static base::NoDestructor<ElectronPermissionMessageProvider> instance;
return *instance;
} }
const std::string ElectronExtensionsClient::GetProductName() { const std::string ElectronExtensionsClient::GetProductName() {