refactor: prefer using `base::NoDestructor` to `base::{Singleton,LazyInstance}` (#41400)

refactor: prefer using base::NoDestructor to base::{Singleton,LazyInstance}
This commit is contained in:
Shelley Vohr 2024-02-23 10:35:20 +01:00 коммит произвёл GitHub
Родитель 26131b23b8
Коммит e95673b052
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
12 изменённых файлов: 44 добавлений и 18 удалений

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

@ -21,7 +21,8 @@ BadgeManager* BadgeManagerFactory::GetForBrowserContext(
// static
BadgeManagerFactory* BadgeManagerFactory::GetInstance() {
return base::Singleton<BadgeManagerFactory>::get();
static base::NoDestructor<BadgeManagerFactory> instance;
return instance.get();
}
BadgeManagerFactory::BadgeManagerFactory()

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

@ -9,7 +9,7 @@
namespace base {
template <typename T>
struct DefaultSingletonTraits;
class NoDestructor;
}
namespace badging {
@ -30,7 +30,7 @@ class BadgeManagerFactory : public BrowserContextKeyedServiceFactory {
BadgeManagerFactory& operator=(const BadgeManagerFactory&) = delete;
private:
friend struct base::DefaultSingletonTraits<BadgeManagerFactory>;
friend base::NoDestructor<BadgeManagerFactory>;
BadgeManagerFactory();
~BadgeManagerFactory() override;

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

@ -4,6 +4,7 @@
#include "shell/browser/extensions/electron_extension_system_factory.h"
#include "base/no_destructor.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "extensions/browser/extension_prefs_factory.h"
#include "extensions/browser/extension_registry_factory.h"
@ -21,7 +22,8 @@ ExtensionSystem* ElectronExtensionSystemFactory::GetForBrowserContext(
// static
ElectronExtensionSystemFactory* ElectronExtensionSystemFactory::GetInstance() {
return base::Singleton<ElectronExtensionSystemFactory>::get();
static base::NoDestructor<ElectronExtensionSystemFactory> instance;
return instance.get();
}
ElectronExtensionSystemFactory::ElectronExtensionSystemFactory()

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

@ -5,9 +5,13 @@
#ifndef ELECTRON_SHELL_BROWSER_EXTENSIONS_ELECTRON_EXTENSION_SYSTEM_FACTORY_H_
#define ELECTRON_SHELL_BROWSER_EXTENSIONS_ELECTRON_EXTENSION_SYSTEM_FACTORY_H_
#include "base/memory/singleton.h"
#include "extensions/browser/extension_system_provider.h"
namespace base {
template <typename T>
class NoDestructor;
} // namespace base
namespace extensions {
// A factory that provides ElectronExtensionSystem.
@ -26,7 +30,7 @@ class ElectronExtensionSystemFactory : public ExtensionSystemProvider {
const ElectronExtensionSystemFactory&) = delete;
private:
friend struct base::DefaultSingletonTraits<ElectronExtensionSystemFactory>;
friend base::NoDestructor<ElectronExtensionSystemFactory>;
ElectronExtensionSystemFactory();
~ElectronExtensionSystemFactory() override;

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

@ -3,7 +3,7 @@
// found in the LICENSE-CHROMIUM file.
#include "shell/browser/media/media_capture_devices_dispatcher.h"
// #include "base/no_destructor.h"
#include "base/logging.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/media_capture_devices.h"
@ -13,7 +13,8 @@ using content::BrowserThread;
namespace electron {
MediaCaptureDevicesDispatcher* MediaCaptureDevicesDispatcher::GetInstance() {
return base::Singleton<MediaCaptureDevicesDispatcher>::get();
static base::NoDestructor<MediaCaptureDevicesDispatcher> instance;
return instance.get();
}
MediaCaptureDevicesDispatcher::MediaCaptureDevicesDispatcher() {

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

@ -5,7 +5,6 @@
#ifndef ELECTRON_SHELL_BROWSER_MEDIA_MEDIA_CAPTURE_DEVICES_DISPATCHER_H_
#define ELECTRON_SHELL_BROWSER_MEDIA_MEDIA_CAPTURE_DEVICES_DISPATCHER_H_
#include "base/memory/singleton.h"
#include "components/webrtc/media_stream_device_enumerator_impl.h"
#include "content/public/browser/media_observer.h"
#include "content/public/browser/media_stream_request.h"
@ -45,7 +44,7 @@ class MediaCaptureDevicesDispatcher
const MediaCaptureDevicesDispatcher&) = delete;
private:
friend struct base::DefaultSingletonTraits<MediaCaptureDevicesDispatcher>;
friend base::NoDestructor<MediaCaptureDevicesDispatcher>;
MediaCaptureDevicesDispatcher();
~MediaCaptureDevicesDispatcher() override;

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

@ -4,6 +4,7 @@
#include "shell/browser/net/network_context_service_factory.h"
#include "base/no_destructor.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "shell/browser/net/network_context_service.h"
@ -16,7 +17,8 @@ NetworkContextService* NetworkContextServiceFactory::GetForContext(
}
NetworkContextServiceFactory* NetworkContextServiceFactory::GetInstance() {
return base::Singleton<NetworkContextServiceFactory>::get();
static base::NoDestructor<NetworkContextServiceFactory> instance;
return instance.get();
}
NetworkContextServiceFactory::NetworkContextServiceFactory()

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

@ -14,6 +14,11 @@ namespace content {
class BrowserContext;
}
namespace base {
template <typename T>
class NoDestructor;
}
namespace electron {
class NetworkContextService;
@ -33,7 +38,7 @@ class NetworkContextServiceFactory : public BrowserContextKeyedServiceFactory {
delete;
private:
friend struct base::DefaultSingletonTraits<NetworkContextServiceFactory>;
friend base::NoDestructor<NetworkContextServiceFactory>;
NetworkContextServiceFactory();
~NetworkContextServiceFactory() override;

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

@ -4,6 +4,7 @@
#include "shell/browser/serial/serial_chooser_context_factory.h"
#include "base/no_destructor.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "shell/browser/electron_browser_context.h"
#include "shell/browser/serial/serial_chooser_context.h"
@ -26,7 +27,8 @@ KeyedService* SerialChooserContextFactory::BuildServiceInstanceFor(
// static
SerialChooserContextFactory* SerialChooserContextFactory::GetInstance() {
return base::Singleton<SerialChooserContextFactory>::get();
static base::NoDestructor<SerialChooserContextFactory> instance;
return instance.get();
}
// static

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

@ -5,10 +5,14 @@
#ifndef ELECTRON_SHELL_BROWSER_SERIAL_SERIAL_CHOOSER_CONTEXT_FACTORY_H_
#define ELECTRON_SHELL_BROWSER_SERIAL_SERIAL_CHOOSER_CONTEXT_FACTORY_H_
#include "base/memory/singleton.h"
#include "components/keyed_service/content/browser_context_keyed_service_factory.h"
#include "shell/browser/serial/serial_chooser_context.h"
namespace base {
template <typename T>
class NoDestructor;
} // namespace base
namespace electron {
class SerialChooserContext;
@ -20,7 +24,7 @@ class SerialChooserContextFactory : public BrowserContextKeyedServiceFactory {
static SerialChooserContextFactory* GetInstance();
private:
friend struct base::DefaultSingletonTraits<SerialChooserContextFactory>;
friend base::NoDestructor<SerialChooserContextFactory>;
SerialChooserContextFactory();
~SerialChooserContextFactory() override;

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

@ -4,6 +4,7 @@
#include "shell/browser/usb/usb_chooser_context_factory.h"
#include "base/no_destructor.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "shell/browser/electron_browser_context.h"
#include "shell/browser/usb/usb_chooser_context.h"
@ -26,7 +27,8 @@ KeyedService* UsbChooserContextFactory::BuildServiceInstanceFor(
// static
UsbChooserContextFactory* UsbChooserContextFactory::GetInstance() {
return base::Singleton<UsbChooserContextFactory>::get();
static base::NoDestructor<UsbChooserContextFactory> instance;
return instance.get();
}
// static

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

@ -5,9 +5,13 @@
#ifndef ELECTRON_SHELL_BROWSER_USB_USB_CHOOSER_CONTEXT_FACTORY_H_
#define ELECTRON_SHELL_BROWSER_USB_USB_CHOOSER_CONTEXT_FACTORY_H_
#include "base/memory/singleton.h"
#include "components/keyed_service/content/browser_context_keyed_service_factory.h"
namespace base {
template <typename T>
class NoDestructor;
} // namespace base
namespace electron {
class UsbChooserContext;
@ -24,7 +28,7 @@ class UsbChooserContextFactory : public BrowserContextKeyedServiceFactory {
UsbChooserContextFactory& operator=(const UsbChooserContextFactory&) = delete;
private:
friend struct base::DefaultSingletonTraits<UsbChooserContextFactory>;
friend base::NoDestructor<UsbChooserContextFactory>;
UsbChooserContextFactory();
~UsbChooserContextFactory() override;