Roll src/third_party/WebKit 605a979:06cb9e9 (svn 202556:202558)
[chromium-blink-merge.git] / components / scheduler / child / scheduler_helper.cc
blobf39da7927192fe67c633abb3c192ebae476b2133
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/child/scheduler_helper.h"
7 #include "base/time/default_tick_clock.h"
8 #include "base/trace_event/trace_event.h"
9 #include "base/trace_event/trace_event_argument.h"
10 #include "components/scheduler/child/scheduler_task_runner_delegate.h"
11 #include "components/scheduler/child/task_queue_impl.h"
13 namespace scheduler {
15 SchedulerHelper::SchedulerHelper(
16 scoped_refptr<SchedulerTaskRunnerDelegate> main_task_runner,
17 const char* tracing_category,
18 const char* disabled_by_default_tracing_category,
19 const char* disabled_by_default_verbose_tracing_category)
20 : main_task_runner_(main_task_runner),
21 task_queue_manager_(
22 new TaskQueueManager(main_task_runner,
23 disabled_by_default_tracing_category,
24 disabled_by_default_verbose_tracing_category)),
25 control_task_runner_(NewTaskQueue(
26 TaskQueue::Spec("control_tq")
27 .SetWakeupPolicy(
28 TaskQueue::WakeupPolicy::DONT_WAKE_OTHER_QUEUES)
29 .SetShouldNotifyObservers(false))),
30 control_after_wakeup_task_runner_(NewTaskQueue(
31 TaskQueue::Spec("control_after_wakeup_tq")
32 .SetPumpPolicy(TaskQueue::PumpPolicy::AFTER_WAKEUP)
33 .SetWakeupPolicy(
34 TaskQueue::WakeupPolicy::DONT_WAKE_OTHER_QUEUES)
35 .SetShouldNotifyObservers(false))),
36 default_task_runner_(NewTaskQueue(TaskQueue::Spec("default_tq")
37 .SetShouldMonitorQuiescence(true))),
38 time_source_(new base::DefaultTickClock),
39 tracing_category_(tracing_category),
40 disabled_by_default_tracing_category_(
41 disabled_by_default_tracing_category) {
42 control_task_runner_->SetQueuePriority(TaskQueue::CONTROL_PRIORITY);
43 control_after_wakeup_task_runner_->SetQueuePriority(
44 TaskQueue::CONTROL_PRIORITY);
46 task_queue_manager_->SetWorkBatchSize(4);
48 main_task_runner_->SetDefaultTaskRunner(default_task_runner_.get());
51 SchedulerHelper::~SchedulerHelper() {
52 Shutdown();
55 void SchedulerHelper::Shutdown() {
56 CheckOnValidThread();
57 task_queue_manager_.reset();
58 main_task_runner_->RestoreDefaultTaskRunner();
61 scoped_refptr<TaskQueue> SchedulerHelper::NewTaskQueue(
62 const TaskQueue::Spec& spec) {
63 DCHECK(task_queue_manager_.get());
64 return task_queue_manager_->NewTaskQueue(spec);
67 scoped_refptr<TaskQueue> SchedulerHelper::DefaultTaskRunner() {
68 CheckOnValidThread();
69 return default_task_runner_;
72 scoped_refptr<TaskQueue> SchedulerHelper::ControlTaskRunner() {
73 return control_task_runner_;
76 scoped_refptr<TaskQueue> SchedulerHelper::ControlAfterWakeUpTaskRunner() {
77 return control_after_wakeup_task_runner_;
80 void SchedulerHelper::SetTimeSourceForTesting(
81 scoped_ptr<base::TickClock> time_source) {
82 CheckOnValidThread();
83 time_source_ = time_source.Pass();
86 void SchedulerHelper::SetWorkBatchSizeForTesting(size_t work_batch_size) {
87 CheckOnValidThread();
88 DCHECK(task_queue_manager_.get());
89 task_queue_manager_->SetWorkBatchSize(work_batch_size);
92 TaskQueueManager* SchedulerHelper::GetTaskQueueManagerForTesting() {
93 CheckOnValidThread();
94 return task_queue_manager_.get();
97 base::TimeTicks SchedulerHelper::Now() const {
98 return time_source_->NowTicks();
101 base::TimeTicks SchedulerHelper::NextPendingDelayedTaskRunTime() const {
102 CheckOnValidThread();
103 DCHECK(task_queue_manager_.get());
104 return task_queue_manager_->NextPendingDelayedTaskRunTime();
107 bool SchedulerHelper::GetAndClearSystemIsQuiescentBit() {
108 CheckOnValidThread();
109 DCHECK(task_queue_manager_.get());
110 return task_queue_manager_->GetAndClearSystemIsQuiescentBit();
113 void SchedulerHelper::AddTaskObserver(
114 base::MessageLoop::TaskObserver* task_observer) {
115 CheckOnValidThread();
116 if (task_queue_manager_)
117 task_queue_manager_->AddTaskObserver(task_observer);
120 void SchedulerHelper::RemoveTaskObserver(
121 base::MessageLoop::TaskObserver* task_observer) {
122 CheckOnValidThread();
123 if (task_queue_manager_)
124 task_queue_manager_->RemoveTaskObserver(task_observer);
127 } // namespace scheduler