| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/scheduler/test/lazy_scheduler_message_loop_delegate_for_tes
ts.h" | |
| 6 | |
| 7 #include <utility> | |
| 8 | |
| 9 #include "base/memory/ptr_util.h" | |
| 10 #include "base/time/default_tick_clock.h" | |
| 11 | |
| 12 namespace scheduler { | |
| 13 | |
| 14 // static | |
| 15 scoped_refptr<LazySchedulerMessageLoopDelegateForTests> | |
| 16 LazySchedulerMessageLoopDelegateForTests::Create() { | |
| 17 return make_scoped_refptr(new LazySchedulerMessageLoopDelegateForTests()); | |
| 18 } | |
| 19 | |
| 20 LazySchedulerMessageLoopDelegateForTests:: | |
| 21 LazySchedulerMessageLoopDelegateForTests() | |
| 22 : message_loop_(base::MessageLoop::current()), | |
| 23 thread_id_(base::PlatformThread::CurrentId()), | |
| 24 time_source_(base::WrapUnique(new base::DefaultTickClock())) { | |
| 25 if (message_loop_) | |
| 26 original_task_runner_ = message_loop_->task_runner(); | |
| 27 } | |
| 28 | |
| 29 LazySchedulerMessageLoopDelegateForTests:: | |
| 30 ~LazySchedulerMessageLoopDelegateForTests() { | |
| 31 RestoreDefaultTaskRunner(); | |
| 32 } | |
| 33 | |
| 34 base::MessageLoop* LazySchedulerMessageLoopDelegateForTests::EnsureMessageLoop() | |
| 35 const { | |
| 36 if (message_loop_) | |
| 37 return message_loop_; | |
| 38 DCHECK(RunsTasksOnCurrentThread()); | |
| 39 message_loop_ = base::MessageLoop::current(); | |
| 40 DCHECK(message_loop_); | |
| 41 original_task_runner_ = message_loop_->task_runner(); | |
| 42 if (pending_task_runner_) | |
| 43 message_loop_->SetTaskRunner(std::move(pending_task_runner_)); | |
| 44 return message_loop_; | |
| 45 } | |
| 46 | |
| 47 void LazySchedulerMessageLoopDelegateForTests::SetDefaultTaskRunner( | |
| 48 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { | |
| 49 if (!HasMessageLoop()) { | |
| 50 pending_task_runner_ = std::move(task_runner); | |
| 51 return; | |
| 52 } | |
| 53 message_loop_->SetTaskRunner(std::move(task_runner)); | |
| 54 } | |
| 55 | |
| 56 void LazySchedulerMessageLoopDelegateForTests::RestoreDefaultTaskRunner() { | |
| 57 if (HasMessageLoop() && base::MessageLoop::current() == message_loop_) | |
| 58 message_loop_->SetTaskRunner(original_task_runner_); | |
| 59 } | |
| 60 | |
| 61 bool LazySchedulerMessageLoopDelegateForTests::HasMessageLoop() const { | |
| 62 return message_loop_ != nullptr; | |
| 63 } | |
| 64 | |
| 65 bool LazySchedulerMessageLoopDelegateForTests::PostDelayedTask( | |
| 66 const tracked_objects::Location& from_here, | |
| 67 const base::Closure& task, | |
| 68 base::TimeDelta delay) { | |
| 69 EnsureMessageLoop(); | |
| 70 return original_task_runner_->PostDelayedTask(from_here, task, delay); | |
| 71 } | |
| 72 | |
| 73 bool LazySchedulerMessageLoopDelegateForTests::PostNonNestableDelayedTask( | |
| 74 const tracked_objects::Location& from_here, | |
| 75 const base::Closure& task, | |
| 76 base::TimeDelta delay) { | |
| 77 EnsureMessageLoop(); | |
| 78 return original_task_runner_->PostNonNestableDelayedTask(from_here, task, | |
| 79 delay); | |
| 80 } | |
| 81 | |
| 82 bool LazySchedulerMessageLoopDelegateForTests::RunsTasksOnCurrentThread() | |
| 83 const { | |
| 84 return thread_id_ == base::PlatformThread::CurrentId(); | |
| 85 } | |
| 86 | |
| 87 bool LazySchedulerMessageLoopDelegateForTests::IsNested() const { | |
| 88 return EnsureMessageLoop()->IsNested(); | |
| 89 } | |
| 90 | |
| 91 base::TimeTicks LazySchedulerMessageLoopDelegateForTests::NowTicks() { | |
| 92 return time_source_->NowTicks(); | |
| 93 } | |
| 94 | |
| 95 } // namespace scheduler | |
| OLD | NEW |