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/web_scheduler_impl.h"
8 #include "base/single_thread_task_runner.h"
9 #include "components/scheduler/child/worker_scheduler.h"
10 #include "third_party/WebKit/public/platform/WebTraceLocation.h"
14 WebSchedulerImpl::WebSchedulerImpl(
15 ChildScheduler
* child_scheduler
,
16 scoped_refptr
<SingleThreadIdleTaskRunner
> idle_task_runner
,
17 scoped_refptr
<base::SingleThreadTaskRunner
> loading_task_runner
,
18 scoped_refptr
<TaskQueue
> timer_task_runner
)
19 : child_scheduler_(child_scheduler
),
20 idle_task_runner_(idle_task_runner
),
21 loading_task_runner_(loading_task_runner
),
22 timer_task_runner_(timer_task_runner
) {}
24 WebSchedulerImpl::~WebSchedulerImpl() {
27 void WebSchedulerImpl::shutdown() {
28 child_scheduler_
->Shutdown();
31 bool WebSchedulerImpl::shouldYieldForHighPriorityWork() {
32 return child_scheduler_
->ShouldYieldForHighPriorityWork();
35 bool WebSchedulerImpl::canExceedIdleDeadlineIfRequired() {
36 return child_scheduler_
->CanExceedIdleDeadlineIfRequired();
39 void WebSchedulerImpl::runIdleTask(scoped_ptr
<blink::WebThread::IdleTask
> task
,
40 base::TimeTicks deadline
) {
41 task
->run((deadline
- base::TimeTicks()).InSecondsF());
44 void WebSchedulerImpl::runTask(scoped_ptr
<blink::WebThread::Task
> task
) {
48 void WebSchedulerImpl::postIdleTask(const blink::WebTraceLocation
& web_location
,
49 blink::WebThread::IdleTask
* task
) {
50 DCHECK(idle_task_runner_
);
51 scoped_ptr
<blink::WebThread::IdleTask
> scoped_task(task
);
52 tracked_objects::Location
location(web_location
.functionName(),
53 web_location
.fileName(), -1, nullptr);
54 idle_task_runner_
->PostIdleTask(
56 base::Bind(&WebSchedulerImpl::runIdleTask
, base::Passed(&scoped_task
)));
59 void WebSchedulerImpl::postNonNestableIdleTask(
60 const blink::WebTraceLocation
& web_location
,
61 blink::WebThread::IdleTask
* task
) {
62 DCHECK(idle_task_runner_
);
63 scoped_ptr
<blink::WebThread::IdleTask
> scoped_task(task
);
64 tracked_objects::Location
location(web_location
.functionName(),
65 web_location
.fileName(), -1, nullptr);
66 idle_task_runner_
->PostNonNestableIdleTask(
68 base::Bind(&WebSchedulerImpl::runIdleTask
, base::Passed(&scoped_task
)));
71 void WebSchedulerImpl::postIdleTaskAfterWakeup(
72 const blink::WebTraceLocation
& web_location
,
73 blink::WebThread::IdleTask
* task
) {
74 DCHECK(idle_task_runner_
);
75 scoped_ptr
<blink::WebThread::IdleTask
> scoped_task(task
);
76 tracked_objects::Location
location(web_location
.functionName(),
77 web_location
.fileName(), -1, nullptr);
78 idle_task_runner_
->PostIdleTaskAfterWakeup(
80 base::Bind(&WebSchedulerImpl::runIdleTask
, base::Passed(&scoped_task
)));
83 void WebSchedulerImpl::postLoadingTask(
84 const blink::WebTraceLocation
& web_location
,
85 blink::WebThread::Task
* task
) {
86 DCHECK(loading_task_runner_
);
87 scoped_ptr
<blink::WebThread::Task
> scoped_task(task
);
88 tracked_objects::Location
location(web_location
.functionName(),
89 web_location
.fileName(), -1, nullptr);
90 loading_task_runner_
->PostTask(
92 base::Bind(&WebSchedulerImpl::runTask
, base::Passed(&scoped_task
)));
95 void WebSchedulerImpl::postTimerTask(
96 const blink::WebTraceLocation
& web_location
,
97 blink::WebThread::Task
* task
,
99 DCHECK(timer_task_runner_
);
100 scoped_ptr
<blink::WebThread::Task
> scoped_task(task
);
101 tracked_objects::Location
location(web_location
.functionName(),
102 web_location
.fileName(), -1, nullptr);
103 timer_task_runner_
->PostDelayedTask(
105 base::Bind(&WebSchedulerImpl::runTask
, base::Passed(&scoped_task
)),
106 base::TimeDelta::FromMilliseconds(delayMs
));
109 void WebSchedulerImpl::postTimerTask(
110 const blink::WebTraceLocation
& web_location
,
111 blink::WebThread::Task
* task
,
113 DCHECK(timer_task_runner_
);
114 scoped_ptr
<blink::WebThread::Task
> scoped_task(task
);
115 tracked_objects::Location
location(web_location
.functionName(),
116 web_location
.fileName(), -1, nullptr);
117 timer_task_runner_
->PostDelayedTask(
119 base::Bind(&WebSchedulerImpl::runTask
, base::Passed(&scoped_task
)),
120 base::TimeDelta::FromSecondsD(delaySecs
));
123 void WebSchedulerImpl::postTimerTaskAt(
124 const blink::WebTraceLocation
& web_location
,
125 blink::WebThread::Task
* task
,
126 double monotonicTime
) {
127 DCHECK(timer_task_runner_
);
128 scoped_ptr
<blink::WebThread::Task
> scoped_task(task
);
129 tracked_objects::Location
location(web_location
.functionName(),
130 web_location
.fileName(), -1, nullptr);
131 timer_task_runner_
->PostDelayedTaskAt(
133 base::Bind(&WebSchedulerImpl::runTask
, base::Passed(&scoped_task
)),
134 base::TimeTicks() + base::TimeDelta::FromSecondsD(monotonicTime
));
137 } // namespace scheduler