| 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(); |
| 19 } | 21 } |
| 20 | 22 |
| 21 LazySchedulerMessageLoopDelegateForTests:: | 23 LazySchedulerMessageLoopDelegateForTests:: |
| 22 ~LazySchedulerMessageLoopDelegateForTests() { | 24 ~LazySchedulerMessageLoopDelegateForTests() { |
| 25 RestoreDefaultTaskRunner(); |
| 23 } | 26 } |
| 24 | 27 |
| 25 base::MessageLoop* LazySchedulerMessageLoopDelegateForTests::EnsureMessageLoop() | 28 base::MessageLoop* LazySchedulerMessageLoopDelegateForTests::EnsureMessageLoop() |
| 26 const { | 29 const { |
| 27 if (message_loop_) | 30 if (message_loop_) |
| 28 return message_loop_; | 31 return message_loop_; |
| 29 DCHECK(RunsTasksOnCurrentThread()); | 32 DCHECK(RunsTasksOnCurrentThread()); |
| 30 message_loop_ = base::MessageLoop::current(); | 33 message_loop_ = base::MessageLoop::current(); |
| 31 DCHECK(message_loop_); | 34 DCHECK(message_loop_); |
| 35 original_task_runner_ = message_loop_->task_runner(); |
| 32 for (auto& observer : pending_observers_) { | 36 for (auto& observer : pending_observers_) { |
| 33 message_loop_->AddTaskObserver(observer); | 37 message_loop_->AddTaskObserver(observer); |
| 34 } | 38 } |
| 35 pending_observers_.clear(); | 39 pending_observers_.clear(); |
| 40 if (pending_task_runner_) |
| 41 message_loop_->SetTaskRunner(pending_task_runner_.Pass()); |
| 36 return message_loop_; | 42 return message_loop_; |
| 37 } | 43 } |
| 38 | 44 |
| 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 |
| 39 bool LazySchedulerMessageLoopDelegateForTests::HasMessageLoop() const { | 59 bool LazySchedulerMessageLoopDelegateForTests::HasMessageLoop() const { |
| 40 return message_loop_ != nullptr; | 60 return message_loop_ != nullptr; |
| 41 } | 61 } |
| 42 | 62 |
| 43 bool LazySchedulerMessageLoopDelegateForTests::PostDelayedTask( | 63 bool LazySchedulerMessageLoopDelegateForTests::PostDelayedTask( |
| 44 const tracked_objects::Location& from_here, | 64 const tracked_objects::Location& from_here, |
| 45 const base::Closure& task, | 65 const base::Closure& task, |
| 46 base::TimeDelta delay) { | 66 base::TimeDelta delay) { |
| 47 return EnsureMessageLoop()->task_runner()->PostDelayedTask(from_here, task, | 67 EnsureMessageLoop(); |
| 48 delay); | 68 return original_task_runner_->PostDelayedTask(from_here, task, delay); |
| 49 } | 69 } |
| 50 | 70 |
| 51 bool LazySchedulerMessageLoopDelegateForTests::PostNonNestableDelayedTask( | 71 bool LazySchedulerMessageLoopDelegateForTests::PostNonNestableDelayedTask( |
| 52 const tracked_objects::Location& from_here, | 72 const tracked_objects::Location& from_here, |
| 53 const base::Closure& task, | 73 const base::Closure& task, |
| 54 base::TimeDelta delay) { | 74 base::TimeDelta delay) { |
| 55 return EnsureMessageLoop()->task_runner()->PostNonNestableDelayedTask( | 75 EnsureMessageLoop(); |
| 56 from_here, task, delay); | 76 return original_task_runner_->PostNonNestableDelayedTask(from_here, task, |
| 77 delay); |
| 57 } | 78 } |
| 58 | 79 |
| 59 bool LazySchedulerMessageLoopDelegateForTests::RunsTasksOnCurrentThread() | 80 bool LazySchedulerMessageLoopDelegateForTests::RunsTasksOnCurrentThread() |
| 60 const { | 81 const { |
| 61 return thread_id_ == base::PlatformThread::CurrentId(); | 82 return thread_id_ == base::PlatformThread::CurrentId(); |
| 62 } | 83 } |
| 63 | 84 |
| 64 bool LazySchedulerMessageLoopDelegateForTests::IsNested() const { | 85 bool LazySchedulerMessageLoopDelegateForTests::IsNested() const { |
| 65 return EnsureMessageLoop()->IsNested(); | 86 return EnsureMessageLoop()->IsNested(); |
| 66 } | 87 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 77 void LazySchedulerMessageLoopDelegateForTests::RemoveTaskObserver( | 98 void LazySchedulerMessageLoopDelegateForTests::RemoveTaskObserver( |
| 78 base::MessageLoop::TaskObserver* task_observer) { | 99 base::MessageLoop::TaskObserver* task_observer) { |
| 79 if (!HasMessageLoop()) { | 100 if (!HasMessageLoop()) { |
| 80 pending_observers_.erase(task_observer); | 101 pending_observers_.erase(task_observer); |
| 81 return; | 102 return; |
| 82 } | 103 } |
| 83 EnsureMessageLoop()->RemoveTaskObserver(task_observer); | 104 EnsureMessageLoop()->RemoveTaskObserver(task_observer); |
| 84 } | 105 } |
| 85 | 106 |
| 86 } // namespace scheduler | 107 } // namespace scheduler |
| OLD | NEW |