OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "components/scheduler/test/lazy_scheduler_message_loop_delegate_for_tes
ts.h" | |
6 | |
7 namespace scheduler { | |
8 | |
9 // static | |
10 scoped_refptr<LazySchedulerMessageLoopDelegateForTests> | |
11 LazySchedulerMessageLoopDelegateForTests::Create() { | |
12 return make_scoped_refptr(new LazySchedulerMessageLoopDelegateForTests()); | |
13 } | |
14 | |
15 LazySchedulerMessageLoopDelegateForTests:: | |
16 LazySchedulerMessageLoopDelegateForTests() | |
17 : thread_id_(base::PlatformThread::CurrentId()) { | |
18 } | |
19 | |
20 LazySchedulerMessageLoopDelegateForTests:: | |
21 ~LazySchedulerMessageLoopDelegateForTests() { | |
22 } | |
23 | |
24 base::MessageLoop* LazySchedulerMessageLoopDelegateForTests::EnsureMessageLoop() | |
25 const { | |
26 base::MessageLoop* message_loop = base::MessageLoop::current(); | |
27 DCHECK(message_loop); | |
28 for (auto& observer : pending_observers_) { | |
29 message_loop->AddTaskObserver(observer); | |
30 } | |
31 pending_observers_.clear(); | |
32 return message_loop; | |
33 } | |
34 | |
35 bool LazySchedulerMessageLoopDelegateForTests::HasMessageLoop() const { | |
36 return base::MessageLoop::current() != nullptr; | |
37 } | |
38 | |
39 bool LazySchedulerMessageLoopDelegateForTests::PostDelayedTask( | |
40 const tracked_objects::Location& from_here, | |
41 const base::Closure& task, | |
42 base::TimeDelta delay) { | |
43 return EnsureMessageLoop()->task_runner()->PostDelayedTask(from_here, task, | |
44 delay); | |
45 } | |
46 | |
47 bool LazySchedulerMessageLoopDelegateForTests::PostNonNestableDelayedTask( | |
48 const tracked_objects::Location& from_here, | |
49 const base::Closure& task, | |
50 base::TimeDelta delay) { | |
51 return EnsureMessageLoop()->task_runner()->PostNonNestableDelayedTask( | |
52 from_here, task, delay); | |
53 } | |
54 | |
55 bool LazySchedulerMessageLoopDelegateForTests::RunsTasksOnCurrentThread() | |
56 const { | |
57 return thread_id_ == base::PlatformThread::CurrentId(); | |
58 } | |
59 | |
60 bool LazySchedulerMessageLoopDelegateForTests::IsNested() const { | |
61 return EnsureMessageLoop()->IsNested(); | |
62 } | |
63 | |
64 void LazySchedulerMessageLoopDelegateForTests::AddTaskObserver( | |
65 base::MessageLoop::TaskObserver* task_observer) { | |
66 if (!HasMessageLoop()) { | |
67 pending_observers_.insert(task_observer); | |
68 return; | |
69 } | |
70 EnsureMessageLoop()->AddTaskObserver(task_observer); | |
71 } | |
72 | |
73 void LazySchedulerMessageLoopDelegateForTests::RemoveTaskObserver( | |
74 base::MessageLoop::TaskObserver* task_observer) { | |
75 if (!HasMessageLoop()) { | |
76 pending_observers_.erase(task_observer); | |
77 return; | |
78 } | |
79 EnsureMessageLoop()->RemoveTaskObserver(task_observer); | |
80 } | |
81 | |
82 } // namespace scheduler | |
OLD | NEW |