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.
5 #include "components/scheduler/test/lazy_scheduler_message_loop_delegate_for_tests.h"
10 scoped_refptr
<LazySchedulerMessageLoopDelegateForTests
>
11 LazySchedulerMessageLoopDelegateForTests::Create() {
12 return make_scoped_refptr(new LazySchedulerMessageLoopDelegateForTests());
15 LazySchedulerMessageLoopDelegateForTests::
16 LazySchedulerMessageLoopDelegateForTests()
17 : message_loop_(base::MessageLoop::current()),
18 thread_id_(base::PlatformThread::CurrentId()) {
20 original_task_runner_
= message_loop_
->task_runner();
23 LazySchedulerMessageLoopDelegateForTests::
24 ~LazySchedulerMessageLoopDelegateForTests() {
25 RestoreDefaultTaskRunner();
28 base::MessageLoop
* LazySchedulerMessageLoopDelegateForTests::EnsureMessageLoop()
32 DCHECK(RunsTasksOnCurrentThread());
33 message_loop_
= base::MessageLoop::current();
34 DCHECK(message_loop_
);
35 original_task_runner_
= message_loop_
->task_runner();
36 if (pending_task_runner_
)
37 message_loop_
->SetTaskRunner(pending_task_runner_
.Pass());
41 void LazySchedulerMessageLoopDelegateForTests::SetDefaultTaskRunner(
42 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner
) {
43 if (!HasMessageLoop()) {
44 pending_task_runner_
= task_runner
.Pass();
47 message_loop_
->SetTaskRunner(task_runner
.Pass());
50 void LazySchedulerMessageLoopDelegateForTests::RestoreDefaultTaskRunner() {
51 if (HasMessageLoop() && base::MessageLoop::current() == message_loop_
)
52 message_loop_
->SetTaskRunner(original_task_runner_
);
55 bool LazySchedulerMessageLoopDelegateForTests::HasMessageLoop() const {
56 return message_loop_
!= nullptr;
59 bool LazySchedulerMessageLoopDelegateForTests::PostDelayedTask(
60 const tracked_objects::Location
& from_here
,
61 const base::Closure
& task
,
62 base::TimeDelta delay
) {
64 return original_task_runner_
->PostDelayedTask(from_here
, task
, delay
);
67 bool LazySchedulerMessageLoopDelegateForTests::PostNonNestableDelayedTask(
68 const tracked_objects::Location
& from_here
,
69 const base::Closure
& task
,
70 base::TimeDelta delay
) {
72 return original_task_runner_
->PostNonNestableDelayedTask(from_here
, task
,
76 bool LazySchedulerMessageLoopDelegateForTests::RunsTasksOnCurrentThread()
78 return thread_id_
== base::PlatformThread::CurrentId();
81 bool LazySchedulerMessageLoopDelegateForTests::IsNested() const {
82 return EnsureMessageLoop()->IsNested();
85 } // namespace scheduler