uv_task_runner.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright (c) 2015 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #ifndef ELECTRON_SHELL_APP_UV_TASK_RUNNER_H_
  5. #define ELECTRON_SHELL_APP_UV_TASK_RUNNER_H_
  6. #include <map>
  7. #include "base/functional/callback.h"
  8. #include "base/memory/raw_ptr.h"
  9. #include "base/task/single_thread_task_runner.h"
  10. #include "uv.h" // NOLINT(build/include_directory)
  11. namespace base {
  12. class Location;
  13. class TimeDelta;
  14. } // namespace base
  15. namespace electron {
  16. // TaskRunner implementation that posts tasks into libuv's default loop.
  17. class UvTaskRunner : public base::SingleThreadTaskRunner {
  18. public:
  19. explicit UvTaskRunner(uv_loop_t* loop);
  20. // disable copy
  21. UvTaskRunner(const UvTaskRunner&) = delete;
  22. UvTaskRunner& operator=(const UvTaskRunner&) = delete;
  23. // base::SingleThreadTaskRunner:
  24. bool PostDelayedTask(const base::Location& from_here,
  25. base::OnceClosure task,
  26. base::TimeDelta delay) override;
  27. bool RunsTasksInCurrentSequence() const override;
  28. bool PostNonNestableDelayedTask(const base::Location& from_here,
  29. base::OnceClosure task,
  30. base::TimeDelta delay) override;
  31. private:
  32. ~UvTaskRunner() override;
  33. static void OnTimeout(uv_timer_t* timer);
  34. static void OnClose(uv_handle_t* handle);
  35. raw_ptr<uv_loop_t> loop_;
  36. std::map<uv_timer_t*, base::OnceClosure> tasks_;
  37. };
  38. } // namespace electron
  39. #endif // ELECTRON_SHELL_APP_UV_TASK_RUNNER_H_