diff --git a/shell/app/uv_task_runner.cc b/shell/app/uv_task_runner.cc index 24bcf863bfa8..25767d541d86 100644 --- a/shell/app/uv_task_runner.cc +++ b/shell/app/uv_task_runner.cc @@ -2,32 +2,34 @@ // Use of this source code is governed by the MIT license that can be // found in the LICENSE file. +#include "shell/app/uv_task_runner.h" + #include #include "base/location.h" #include "base/stl_util.h" #include "base/time/time.h" -#include "shell/app/uv_task_runner.h" namespace electron { -UvTaskRunner::UvTaskRunner(uv_loop_t* loop) : loop_(loop) {} +UvTaskRunner::UvTaskRunner(uv_loop_t* loop) : loop_{loop} {} -UvTaskRunner::~UvTaskRunner() { - for (auto& iter : tasks_) { - uv_unref(reinterpret_cast(iter.first)); - delete iter.first; - } -} +UvTaskRunner::~UvTaskRunner() = default; bool UvTaskRunner::PostDelayedTask(const base::Location& from_here, base::OnceClosure task, base::TimeDelta delay) { - auto* timer = new uv_timer_t; + auto on_timeout = [](uv_timer_t* timer) { + auto& tasks = static_cast(timer->data)->tasks_; + if (auto iter = tasks.find(timer); iter != tasks.end()) + std::move(tasks.extract(iter).mapped()).Run(); + }; + + auto timer = UvHandle{}; timer->data = this; - uv_timer_init(loop_, timer); - uv_timer_start(timer, UvTaskRunner::OnTimeout, delay.InMilliseconds(), 0); - tasks_[timer] = std::move(task); + uv_timer_init(loop_, timer.get()); + uv_timer_start(timer.get(), on_timeout, delay.InMilliseconds(), 0); + tasks_.insert_or_assign(std::move(timer), std::move(task)); return true; } @@ -41,22 +43,4 @@ bool UvTaskRunner::PostNonNestableDelayedTask(const base::Location& from_here, return PostDelayedTask(from_here, std::move(task), delay); } -// static -void UvTaskRunner::OnTimeout(uv_timer_t* timer) { - auto& tasks = static_cast(timer->data)->tasks_; - const auto iter = tasks.find(timer); - if (iter == std::end(tasks)) - return; - - std::move(iter->second).Run(); - tasks.erase(iter); - uv_timer_stop(timer); - uv_close(reinterpret_cast(timer), UvTaskRunner::OnClose); -} - -// static -void UvTaskRunner::OnClose(uv_handle_t* handle) { - delete reinterpret_cast(handle); -} - } // namespace electron diff --git a/shell/app/uv_task_runner.h b/shell/app/uv_task_runner.h index d792d2711a1a..4968250d6c5b 100644 --- a/shell/app/uv_task_runner.h +++ b/shell/app/uv_task_runner.h @@ -10,7 +10,7 @@ #include "base/functional/callback.h" #include "base/memory/raw_ptr.h" #include "base/task/single_thread_task_runner.h" -#include "uv.h" // NOLINT(build/include_directory) +#include "shell/common/node_bindings.h" namespace base { class Location; @@ -39,12 +39,10 @@ class UvTaskRunner : public base::SingleThreadTaskRunner { private: ~UvTaskRunner() override; - static void OnTimeout(uv_timer_t* timer); - static void OnClose(uv_handle_t* handle); raw_ptr loop_; - std::map tasks_; + std::map, base::OnceClosure, UvHandleCompare> tasks_; }; } // namespace electron diff --git a/shell/common/node_bindings.h b/shell/common/node_bindings.h index d7e3a1de51fe..196aa624c996 100644 --- a/shell/common/node_bindings.h +++ b/shell/common/node_bindings.h @@ -16,6 +16,7 @@ #include "base/memory/raw_ptr.h" #include "base/memory/raw_ptr_exclusion.h" #include "base/memory/weak_ptr.h" +#include "base/types/to_address.h" #include "gin/public/context_holder.h" #include "gin/public/gin_embedders.h" #include "shell/common/node_includes.h" @@ -54,11 +55,25 @@ template ::value>::type* = nullptr> class UvHandle { public: - UvHandle() : t_(new T) {} + UvHandle() : t_{new T} {} ~UvHandle() { reset(); } + + UvHandle(UvHandle&&) = default; + UvHandle& operator=(UvHandle&&) = default; + + UvHandle(const UvHandle&) = delete; + UvHandle& operator=(const UvHandle&) = delete; + T* get() { return t_; } + T* operator->() { return t_; } + const T* get() const { return t_; } + const T* operator->() const { return t_; } + uv_handle_t* handle() { return reinterpret_cast(t_); } + // compare by handle pointer address + auto operator<=>(const UvHandle& that) const = default; + void reset() { auto* h = handle(); if (h != nullptr) { @@ -76,6 +91,16 @@ class UvHandle { RAW_PTR_EXCLUSION T* t_ = {}; }; +// Helper for comparing UvHandles and raw uv pointers, e.g. as map keys +struct UvHandleCompare { + using is_transparent = void; + + template + bool operator()(U const& u, V const& v) const { + return base::to_address(u) < base::to_address(v); + } +}; + class NodeBindings { public: enum class BrowserEnvironment { kBrowser, kRenderer, kUtility, kWorker };