| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/scheduler/test/lazy_scheduler_message_loop_delegate_for_tes
ts.h" | 5 #include "components/scheduler/test/lazy_scheduler_message_loop_delegate_for_tes
ts.h" |
| 6 | 6 |
| 7 namespace scheduler { | 7 namespace scheduler { |
| 8 | 8 |
| 9 // static | 9 // static |
| 10 scoped_refptr<LazySchedulerMessageLoopDelegateForTests> | 10 scoped_refptr<LazySchedulerMessageLoopDelegateForTests> |
| 11 LazySchedulerMessageLoopDelegateForTests::Create() { | 11 LazySchedulerMessageLoopDelegateForTests::Create() { |
| 12 return make_scoped_refptr(new LazySchedulerMessageLoopDelegateForTests()); | 12 return make_scoped_refptr(new LazySchedulerMessageLoopDelegateForTests()); |
| 13 } | 13 } |
| 14 | 14 |
| 15 LazySchedulerMessageLoopDelegateForTests:: | 15 LazySchedulerMessageLoopDelegateForTests:: |
| 16 LazySchedulerMessageLoopDelegateForTests() | 16 LazySchedulerMessageLoopDelegateForTests() |
| 17 : message_loop_(base::MessageLoop::current()), | 17 : message_loop_(base::MessageLoop::current()), |
| 18 thread_id_(base::PlatformThread::CurrentId()) { | 18 thread_id_(base::PlatformThread::CurrentId()) { |
| 19 if (message_loop_) | |
| 20 original_task_runner_ = message_loop_->task_runner(); | |
| 21 } | 19 } |
| 22 | 20 |
| 23 LazySchedulerMessageLoopDelegateForTests:: | 21 LazySchedulerMessageLoopDelegateForTests:: |
| 24 ~LazySchedulerMessageLoopDelegateForTests() { | 22 ~LazySchedulerMessageLoopDelegateForTests() { |
| 25 RestoreDefaultTaskRunner(); | |
| 26 } | 23 } |
| 27 | 24 |
| 28 base::MessageLoop* LazySchedulerMessageLoopDelegateForTests::EnsureMessageLoop() | 25 base::MessageLoop* LazySchedulerMessageLoopDelegateForTests::EnsureMessageLoop() |
| 29 const { | 26 const { |
| 30 if (message_loop_) | 27 if (message_loop_) |
| 31 return message_loop_; | 28 return message_loop_; |
| 32 DCHECK(RunsTasksOnCurrentThread()); | 29 DCHECK(RunsTasksOnCurrentThread()); |
| 33 message_loop_ = base::MessageLoop::current(); | 30 message_loop_ = base::MessageLoop::current(); |
| 34 DCHECK(message_loop_); | 31 DCHECK(message_loop_); |
| 35 original_task_runner_ = message_loop_->task_runner(); | |
| 36 for (auto& observer : pending_observers_) { | 32 for (auto& observer : pending_observers_) { |
| 37 message_loop_->AddTaskObserver(observer); | 33 message_loop_->AddTaskObserver(observer); |
| 38 } | 34 } |
| 39 pending_observers_.clear(); | 35 pending_observers_.clear(); |
| 40 if (pending_task_runner_) | |
| 41 message_loop_->SetTaskRunner(pending_task_runner_.Pass()); | |
| 42 return message_loop_; | 36 return message_loop_; |
| 43 } | 37 } |
| 44 | 38 |
| 45 void LazySchedulerMessageLoopDelegateForTests::SetDefaultTaskRunner( | |
| 46 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { | |
| 47 if (!HasMessageLoop()) { | |
| 48 pending_task_runner_ = task_runner.Pass(); | |
| 49 return; | |
| 50 } | |
| 51 message_loop_->SetTaskRunner(task_runner.Pass()); | |
| 52 } | |
| 53 | |
| 54 void LazySchedulerMessageLoopDelegateForTests::RestoreDefaultTaskRunner() { | |
| 55 if (HasMessageLoop() && base::MessageLoop::current() == message_loop_) | |
| 56 message_loop_->SetTaskRunner(original_task_runner_); | |
| 57 } | |
| 58 | |
| 59 bool LazySchedulerMessageLoopDelegateForTests::HasMessageLoop() const { | 39 bool LazySchedulerMessageLoopDelegateForTests::HasMessageLoop() const { |
| 60 return message_loop_ != nullptr; | 40 return message_loop_ != nullptr; |
| 61 } | 41 } |
| 62 | 42 |
| 63 bool LazySchedulerMessageLoopDelegateForTests::PostDelayedTask( | 43 bool LazySchedulerMessageLoopDelegateForTests::PostDelayedTask( |
| 64 const tracked_objects::Location& from_here, | 44 const tracked_objects::Location& from_here, |
| 65 const base::Closure& task, | 45 const base::Closure& task, |
| 66 base::TimeDelta delay) { | 46 base::TimeDelta delay) { |
| 67 EnsureMessageLoop(); | 47 return EnsureMessageLoop()->task_runner()->PostDelayedTask(from_here, task, |
| 68 return original_task_runner_->PostDelayedTask(from_here, task, delay); | 48 delay); |
| 69 } | 49 } |
| 70 | 50 |
| 71 bool LazySchedulerMessageLoopDelegateForTests::PostNonNestableDelayedTask( | 51 bool LazySchedulerMessageLoopDelegateForTests::PostNonNestableDelayedTask( |
| 72 const tracked_objects::Location& from_here, | 52 const tracked_objects::Location& from_here, |
| 73 const base::Closure& task, | 53 const base::Closure& task, |
| 74 base::TimeDelta delay) { | 54 base::TimeDelta delay) { |
| 75 EnsureMessageLoop(); | 55 return EnsureMessageLoop()->task_runner()->PostNonNestableDelayedTask( |
| 76 return original_task_runner_->PostNonNestableDelayedTask(from_here, task, | 56 from_here, task, delay); |
| 77 delay); | |
| 78 } | 57 } |
| 79 | 58 |
| 80 bool LazySchedulerMessageLoopDelegateForTests::RunsTasksOnCurrentThread() | 59 bool LazySchedulerMessageLoopDelegateForTests::RunsTasksOnCurrentThread() |
| 81 const { | 60 const { |
| 82 return thread_id_ == base::PlatformThread::CurrentId(); | 61 return thread_id_ == base::PlatformThread::CurrentId(); |
| 83 } | 62 } |
| 84 | 63 |
| 85 bool LazySchedulerMessageLoopDelegateForTests::IsNested() const { | 64 bool LazySchedulerMessageLoopDelegateForTests::IsNested() const { |
| 86 return EnsureMessageLoop()->IsNested(); | 65 return EnsureMessageLoop()->IsNested(); |
| 87 } | 66 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 98 void LazySchedulerMessageLoopDelegateForTests::RemoveTaskObserver( | 77 void LazySchedulerMessageLoopDelegateForTests::RemoveTaskObserver( |
| 99 base::MessageLoop::TaskObserver* task_observer) { | 78 base::MessageLoop::TaskObserver* task_observer) { |
| 100 if (!HasMessageLoop()) { | 79 if (!HasMessageLoop()) { |
| 101 pending_observers_.erase(task_observer); | 80 pending_observers_.erase(task_observer); |
| 102 return; | 81 return; |
| 103 } | 82 } |
| 104 EnsureMessageLoop()->RemoveTaskObserver(task_observer); | 83 EnsureMessageLoop()->RemoveTaskObserver(task_observer); |
| 105 } | 84 } |
| 106 | 85 |
| 107 } // namespace scheduler | 86 } // namespace scheduler |
| OLD | NEW |