Roll src/third_party/WebKit 605a979:06cb9e9 (svn 202556:202558)
[chromium-blink-merge.git] / components / scheduler / test / lazy_scheduler_message_loop_delegate_for_tests.cc
blob23c69978402bf982ee9fdd0a986e376b11a273f7
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"
7 namespace scheduler {
9 // static
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()) {
19 if (message_loop_)
20 original_task_runner_ = message_loop_->task_runner();
23 LazySchedulerMessageLoopDelegateForTests::
24 ~LazySchedulerMessageLoopDelegateForTests() {
25 RestoreDefaultTaskRunner();
28 base::MessageLoop* LazySchedulerMessageLoopDelegateForTests::EnsureMessageLoop()
29 const {
30 if (message_loop_)
31 return message_loop_;
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());
38 return message_loop_;
41 void LazySchedulerMessageLoopDelegateForTests::SetDefaultTaskRunner(
42 scoped_refptr<base::SingleThreadTaskRunner> task_runner) {
43 if (!HasMessageLoop()) {
44 pending_task_runner_ = task_runner.Pass();
45 return;
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) {
63 EnsureMessageLoop();
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) {
71 EnsureMessageLoop();
72 return original_task_runner_->PostNonNestableDelayedTask(from_here, task,
73 delay);
76 bool LazySchedulerMessageLoopDelegateForTests::RunsTasksOnCurrentThread()
77 const {
78 return thread_id_ == base::PlatformThread::CurrentId();
81 bool LazySchedulerMessageLoopDelegateForTests::IsNested() const {
82 return EnsureMessageLoop()->IsNested();
85 } // namespace scheduler