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 коммит произвёл GitHub
Родитель 57b29903e3
Коммит 80b220d214
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
6 изменённых файлов: 28 добавлений и 27 удалений

Просмотреть файл

@ -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<GURL>::Leaky g_download_url_for_testing =
LAZY_INSTANCE_INITIALIZER;
+base::LazyInstance<GURL>::Leaky g_base_download_url_override =
+ LAZY_INSTANCE_INITIALIZER;
+base::NoDestructor<GURL> 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/";

Просмотреть файл

@ -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<std::string>::DestructorAtExit
g_io_thread_application_locale = LAZY_INSTANCE_INITIALIZER;
base::NoDestructor<std::string> g_io_thread_application_locale;
base::NoDestructor<std::string> 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() {

Просмотреть файл

@ -10,7 +10,7 @@
#include <utility>
#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<BrowserContextKeyedAPIFactory<ExtensionActionAPI>>::
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>*
ExtensionActionAPI::GetFactoryInstance() {
return g_extension_action_api_factory.Pointer();
static base::NoDestructor<BrowserContextKeyedAPIFactory<ExtensionActionAPI>>
instance;
return instance.get();
}
// static

Просмотреть файл

@ -8,7 +8,6 @@
#include <string>
#include <utility>
#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"

Просмотреть файл

@ -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"

Просмотреть файл

@ -7,8 +7,8 @@
#include <memory>
#include <string>
#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<ElectronPermissionMessageProvider>::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<ElectronPermissionMessageProvider> instance;
return *instance;
}
const std::string ElectronExtensionsClient::GetProductName() {