uv_task_runner.h 1.3 KB

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