From fac964ac0dc723cd8efd1a89604acac861671579 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 10 Jan 2024 14:01:49 -0600 Subject: [PATCH] refactor: migrate deprecated LazyInstance code to NoDestructor (#40927) * refactor: do not use deprecated NoDestructor in javascript_environment.cc * refactor: do not use deprecated NoDestructor in window_list.cc --- shell/browser/javascript_environment.cc | 16 +++++++++++----- shell/browser/window_list.cc | 23 +++++++++++++---------- shell/browser/window_list.h | 10 ++++------ 3 files changed, 28 insertions(+), 21 deletions(-) diff --git a/shell/browser/javascript_environment.cc b/shell/browser/javascript_environment.cc index cd8b0c9df809..6efd69abc33b 100644 --- a/shell/browser/javascript_environment.cc +++ b/shell/browser/javascript_environment.cc @@ -14,6 +14,7 @@ #include "base/bits.h" #include "base/command_line.h" #include "base/feature_list.h" +#include "base/no_destructor.h" #include "base/task/current_thread.h" #include "base/task/single_thread_task_runner.h" #include "base/task/thread_pool/initialization_util.h" @@ -184,9 +185,6 @@ class EnabledStateObserverImpl final std::unordered_set observers_; }; -base::LazyInstance::Leaky g_trace_state_dispatcher = - LAZY_INSTANCE_INITIALIZER; - class TracingControllerImpl : public node::tracing::TracingController { public: TracingControllerImpl() = default; @@ -265,11 +263,19 @@ class TracingControllerImpl : public node::tracing::TracingController { TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION(category_enabled_flag, name, traceEventHandle); } + void AddTraceStateObserver(TraceStateObserver* observer) override { - g_trace_state_dispatcher.Get().AddObserver(observer); + GetObserverDelegate().AddObserver(observer); } + void RemoveTraceStateObserver(TraceStateObserver* observer) override { - g_trace_state_dispatcher.Get().RemoveObserver(observer); + GetObserverDelegate().RemoveObserver(observer); + } + + private: + static EnabledStateObserverImpl& GetObserverDelegate() { + static base::NoDestructor instance; + return *instance; } }; diff --git a/shell/browser/window_list.cc b/shell/browser/window_list.cc index a2555e5a111e..78378f3aa9ad 100644 --- a/shell/browser/window_list.cc +++ b/shell/browser/window_list.cc @@ -7,6 +7,7 @@ #include #include "base/logging.h" +#include "base/no_destructor.h" #include "shell/browser/native_window.h" #include "shell/browser/window_list_observer.h" @@ -24,10 +25,6 @@ std::vector> ConvertToWeakPtrVector(std::vector raw_ptrs) { namespace electron { -// static -base::LazyInstance>::Leaky - WindowList::observers_ = LAZY_INSTANCE_INITIALIZER; - // static WindowList* WindowList::instance_ = nullptr; @@ -55,7 +52,7 @@ void WindowList::AddWindow(NativeWindow* window) { WindowVector& windows = GetInstance()->windows_; windows.push_back(window); - for (WindowListObserver& observer : observers_.Get()) + for (WindowListObserver& observer : GetObservers()) observer.OnWindowAdded(window); } @@ -65,29 +62,29 @@ void WindowList::RemoveWindow(NativeWindow* window) { windows.erase(std::remove(windows.begin(), windows.end(), window), windows.end()); - for (WindowListObserver& observer : observers_.Get()) + for (WindowListObserver& observer : GetObservers()) observer.OnWindowRemoved(window); if (windows.empty()) { - for (WindowListObserver& observer : observers_.Get()) + for (WindowListObserver& observer : GetObservers()) observer.OnWindowAllClosed(); } } // static void WindowList::WindowCloseCancelled(NativeWindow* window) { - for (WindowListObserver& observer : observers_.Get()) + for (WindowListObserver& observer : GetObservers()) observer.OnWindowCloseCancelled(window); } // static void WindowList::AddObserver(WindowListObserver* observer) { - observers_.Get().AddObserver(observer); + GetObservers().AddObserver(observer); } // static void WindowList::RemoveObserver(WindowListObserver* observer) { - observers_.Get().RemoveObserver(observer); + GetObservers().RemoveObserver(observer); } // static @@ -118,4 +115,10 @@ WindowList::WindowList() = default; WindowList::~WindowList() = default; +// static +base::ObserverList& WindowList::GetObservers() { + static base::NoDestructor> instance; + return *instance; +} + } // namespace electron diff --git a/shell/browser/window_list.h b/shell/browser/window_list.h index 7d8dd942f416..85d7b7d8ed92 100644 --- a/shell/browser/window_list.h +++ b/shell/browser/window_list.h @@ -7,7 +7,6 @@ #include -#include "base/lazy_instance.h" #include "base/observer_list.h" namespace electron { @@ -49,13 +48,12 @@ class WindowList { WindowList(); ~WindowList(); - // A vector of the windows in this list, in the order they were added. - WindowVector windows_; - // A list of observers which will be notified of every window addition and // removal across all WindowLists. - static base::LazyInstance>::Leaky - observers_; + [[nodiscard]] static base::ObserverList& GetObservers(); + + // A vector of the windows in this list, in the order they were added. + WindowVector windows_; static WindowList* instance_; };