Chromium Code Reviews| 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 "platform/scheduler/test/lazy_scheduler_message_loop_delegate_for_tests .h" | 5 #include "platform/scheduler/test/lazy_scheduler_message_loop_delegate_for_tests .h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/message_loop/message_loop.h" | |
| 10 #include "base/time/default_tick_clock.h" | 11 #include "base/time/default_tick_clock.h" |
| 11 | 12 |
| 12 namespace blink { | 13 namespace blink { |
| 13 namespace scheduler { | 14 namespace scheduler { |
| 14 | 15 |
| 15 // static | 16 // static |
| 16 scoped_refptr<LazySchedulerMessageLoopDelegateForTests> | 17 scoped_refptr<LazySchedulerMessageLoopDelegateForTests> |
| 17 LazySchedulerMessageLoopDelegateForTests::Create() { | 18 LazySchedulerMessageLoopDelegateForTests::Create() { |
| 18 return make_scoped_refptr(new LazySchedulerMessageLoopDelegateForTests()); | 19 return make_scoped_refptr(new LazySchedulerMessageLoopDelegateForTests()); |
| 19 } | 20 } |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 37 const { | 38 const { |
| 38 if (message_loop_) | 39 if (message_loop_) |
| 39 return message_loop_; | 40 return message_loop_; |
| 40 DCHECK(RunsTasksOnCurrentThread()); | 41 DCHECK(RunsTasksOnCurrentThread()); |
| 41 message_loop_ = base::MessageLoop::current(); | 42 message_loop_ = base::MessageLoop::current(); |
| 42 DCHECK(message_loop_); | 43 DCHECK(message_loop_); |
| 43 original_task_runner_ = message_loop_->task_runner(); | 44 original_task_runner_ = message_loop_->task_runner(); |
| 44 if (pending_task_runner_) | 45 if (pending_task_runner_) |
| 45 message_loop_->SetTaskRunner(std::move(pending_task_runner_)); | 46 message_loop_->SetTaskRunner(std::move(pending_task_runner_)); |
| 46 if (pending_observer_) | 47 if (pending_observer_) |
| 47 message_loop_->AddNestingObserver(pending_observer_); | 48 base::RunLoop::AddNestingObserverOnCurrentThread(pending_observer_); |
| 48 return message_loop_; | 49 return message_loop_; |
| 49 } | 50 } |
| 50 | 51 |
| 51 void LazySchedulerMessageLoopDelegateForTests::SetDefaultTaskRunner( | 52 void LazySchedulerMessageLoopDelegateForTests::SetDefaultTaskRunner( |
| 52 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { | 53 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { |
| 53 if (!HasMessageLoop()) { | 54 if (!HasMessageLoop()) { |
| 54 pending_task_runner_ = std::move(task_runner); | 55 pending_task_runner_ = std::move(task_runner); |
| 55 return; | 56 return; |
| 56 } | 57 } |
| 57 message_loop_->SetTaskRunner(std::move(task_runner)); | 58 message_loop_->SetTaskRunner(std::move(task_runner)); |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 83 return original_task_runner_->PostNonNestableDelayedTask( | 84 return original_task_runner_->PostNonNestableDelayedTask( |
| 84 from_here, std::move(task), delay); | 85 from_here, std::move(task), delay); |
| 85 } | 86 } |
| 86 | 87 |
| 87 bool LazySchedulerMessageLoopDelegateForTests::RunsTasksOnCurrentThread() | 88 bool LazySchedulerMessageLoopDelegateForTests::RunsTasksOnCurrentThread() |
| 88 const { | 89 const { |
| 89 return thread_id_ == base::PlatformThread::CurrentId(); | 90 return thread_id_ == base::PlatformThread::CurrentId(); |
| 90 } | 91 } |
| 91 | 92 |
| 92 bool LazySchedulerMessageLoopDelegateForTests::IsNested() const { | 93 bool LazySchedulerMessageLoopDelegateForTests::IsNested() const { |
| 93 return EnsureMessageLoop()->IsNested(); | 94 DCHECK(RunsTasksOnCurrentThread()); |
| 95 EnsureMessageLoop(); | |
| 96 return base::RunLoop::IsNestedOnCurrentThread(); | |
| 94 } | 97 } |
| 95 | 98 |
| 96 void LazySchedulerMessageLoopDelegateForTests::AddNestingObserver( | 99 void LazySchedulerMessageLoopDelegateForTests::AddNestingObserver( |
| 97 base::MessageLoop::NestingObserver* observer) { | 100 base::RunLoop::NestingObserver* observer) { |
| 101 // While |observer| _could_ be associated with the current thread regardless | |
| 102 // of the presence of a MessageLoop, the association is delayed until | |
| 103 // EnsureMessageLoop() is invoked. This works around a state issue where | |
| 104 // otherwise many tests fail because of the following sequence: | |
| 105 // 1) blink::scheduler::CreateRendererSchedulerForTests() | |
| 106 // -> TaskQueueManager::TaskQueueManager() | |
| 107 // -> LazySchedulerMessageLoopDelegateForTests::AddNestingObserver() | |
| 108 // 2) Any test framework with a base::MessageLoop member (and not caring | |
| 109 // about the blink scheduler) does: | |
| 110 // ThreadTaskRunnerHandle::Get()->PostTask( | |
| 111 // FROM_HERE, an_init_task_with_a_nested_loop); | |
| 112 // RunLoop.RunUntilIdle(); | |
| 113 // 3) |a_task_with_a_nested_loop| triggers | |
| 114 // TaskQueueManager::OnBeginNestedLoop() which: | |
| 115 // a) flags any_thread().is_nested = true; | |
| 116 // b) posts a task to self, which triggers: | |
| 117 // LazySchedulerMessageLoopDelegateForTests::PostDelayedTask() | |
| 118 // 4) This self-task in turn triggers TaskQueueManager::DoWork() | |
| 119 // which expects to be the only one to trigger nested loops (doesn't | |
| 120 // support TaskQueueManager::OnBeginNestedLoop() being invoked before | |
| 121 // it kicks in), resulting in it hitting: | |
| 122 // DCHECK_EQ(any_thread().is_nested, delegate_->IsNested()); (1 vs 0). | |
| 123 // TODO(skyostil): fix this convulotion as part of http://crbug.com/495659. | |
|
Sami
2017/05/04 15:15:24
typo: convolution :)
| |
| 98 if (!HasMessageLoop()) { | 124 if (!HasMessageLoop()) { |
| 99 pending_observer_ = observer; | 125 pending_observer_ = observer; |
| 100 return; | 126 return; |
| 101 } | 127 } |
| 102 message_loop_->AddNestingObserver(observer); | 128 base::RunLoop::AddNestingObserverOnCurrentThread(observer); |
| 103 } | 129 } |
| 104 | 130 |
| 105 void LazySchedulerMessageLoopDelegateForTests::RemoveNestingObserver( | 131 void LazySchedulerMessageLoopDelegateForTests::RemoveNestingObserver( |
| 106 base::MessageLoop::NestingObserver* observer) { | 132 base::RunLoop::NestingObserver* observer) { |
| 107 if (!message_loop_ || message_loop_ != base::MessageLoop::current()) { | 133 if (!message_loop_ || message_loop_ != base::MessageLoop::current()) { |
| 108 DCHECK_EQ(pending_observer_, observer); | 134 DCHECK_EQ(pending_observer_, observer); |
| 109 pending_observer_ = nullptr; | 135 pending_observer_ = nullptr; |
| 110 return; | 136 return; |
| 111 } | 137 } |
| 112 message_loop_->RemoveNestingObserver(observer); | 138 base::RunLoop::RemoveNestingObserverOnCurrentThread(observer); |
| 113 } | 139 } |
| 114 | 140 |
| 115 base::TimeTicks LazySchedulerMessageLoopDelegateForTests::NowTicks() { | 141 base::TimeTicks LazySchedulerMessageLoopDelegateForTests::NowTicks() { |
| 116 return time_source_->NowTicks(); | 142 return time_source_->NowTicks(); |
| 117 } | 143 } |
| 118 | 144 |
| 119 } // namespace scheduler | 145 } // namespace scheduler |
| 120 } // namespace blink | 146 } // namespace blink |
| OLD | NEW |