bridge_task_runner.cc 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. #include "atom/browser/bridge_task_runner.h"
  5. #include "base/message_loop/message_loop.h"
  6. namespace atom {
  7. BridgeTaskRunner::BridgeTaskRunner() = default;
  8. BridgeTaskRunner::~BridgeTaskRunner() = default;
  9. void BridgeTaskRunner::MessageLoopIsReady() {
  10. auto* message_loop = base::MessageLoop::current();
  11. CHECK(message_loop);
  12. for (TaskPair& task : tasks_) {
  13. message_loop->task_runner()->PostDelayedTask(
  14. std::get<0>(task), std::move(std::get<1>(task)), std::get<2>(task));
  15. }
  16. for (TaskPair& task : non_nestable_tasks_) {
  17. message_loop->task_runner()->PostNonNestableDelayedTask(
  18. std::get<0>(task), std::move(std::get<1>(task)), std::get<2>(task));
  19. }
  20. }
  21. bool BridgeTaskRunner::PostDelayedTask(const base::Location& from_here,
  22. base::OnceClosure task,
  23. base::TimeDelta delay) {
  24. auto* message_loop = base::MessageLoop::current();
  25. if (!message_loop) {
  26. tasks_.push_back(std::make_tuple(from_here, std::move(task), delay));
  27. return true;
  28. }
  29. return message_loop->task_runner()->PostDelayedTask(from_here,
  30. std::move(task), delay);
  31. }
  32. bool BridgeTaskRunner::RunsTasksInCurrentSequence() const {
  33. auto* message_loop = base::MessageLoop::current();
  34. if (!message_loop)
  35. return true;
  36. return message_loop->task_runner()->RunsTasksInCurrentSequence();
  37. }
  38. bool BridgeTaskRunner::PostNonNestableDelayedTask(
  39. const base::Location& from_here,
  40. base::OnceClosure task,
  41. base::TimeDelta delay) {
  42. auto* message_loop = base::MessageLoop::current();
  43. if (!message_loop) {
  44. non_nestable_tasks_.push_back(
  45. std::make_tuple(from_here, std::move(task), delay));
  46. return true;
  47. }
  48. return message_loop->task_runner()->PostNonNestableDelayedTask(
  49. from_here, std::move(task), delay);
  50. }
  51. } // namespace atom