uv_task_runner.h 1.3 KB

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