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
This commit is contained in:
Charles Kerr 2024-01-10 14:01:49 -06:00 committed by GitHub
parent c2c64d27fd
commit fac964ac0d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 21 deletions

View file

@ -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<v8::TracingController::TraceStateObserver*> observers_;
};
base::LazyInstance<EnabledStateObserverImpl>::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<EnabledStateObserverImpl> instance;
return *instance;
}
};

View file

@ -7,6 +7,7 @@
#include <algorithm>
#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<base::WeakPtr<T>> ConvertToWeakPtrVector(std::vector<T*> raw_ptrs) {
namespace electron {
// static
base::LazyInstance<base::ObserverList<WindowListObserver>>::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<WindowListObserver>& WindowList::GetObservers() {
static base::NoDestructor<base::ObserverList<WindowListObserver>> instance;
return *instance;
}
} // namespace electron

View file

@ -7,7 +7,6 @@
#include <vector>
#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<base::ObserverList<WindowListObserver>>::Leaky
observers_;
[[nodiscard]] static base::ObserverList<WindowListObserver>& GetObservers();
// A vector of the windows in this list, in the order they were added.
WindowVector windows_;
static WindowList* instance_;
};