uv_task_runner.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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/task/single_thread_task_runner.h"
  9. #include "uv.h" // NOLINT(build/include_directory)
  10. namespace base {
  11. class Location;
  12. class TimeDelta;
  13. } // namespace base
  14. namespace electron {
  15. // TaskRunner implementation that posts tasks into libuv's default loop.
  16. class UvTaskRunner : public base::SingleThreadTaskRunner {
  17. public:
  18. explicit UvTaskRunner(uv_loop_t* loop);
  19. // disable copy
  20. UvTaskRunner(const UvTaskRunner&) = delete;
  21. UvTaskRunner& operator=(const UvTaskRunner&) = delete;
  22. // base::SingleThreadTaskRunner:
  23. bool PostDelayedTask(const base::Location& from_here,
  24. base::OnceClosure task,
  25. base::TimeDelta delay) override;
  26. bool RunsTasksInCurrentSequence() const override;
  27. bool PostNonNestableDelayedTask(const base::Location& from_here,
  28. base::OnceClosure task,
  29. base::TimeDelta delay) override;
  30. private:
  31. ~UvTaskRunner() override;
  32. static void OnTimeout(uv_timer_t* timer);
  33. static void OnClose(uv_handle_t* handle);
  34. uv_loop_t* loop_;
  35. std::map<uv_timer_t*, base::OnceClosure> tasks_;
  36. };
  37. } // namespace electron
  38. #endif // ELECTRON_SHELL_APP_UV_TASK_RUNNER_H_