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/web_task_runner_impl.h"
10 #include "components/scheduler/child/worker_scheduler.h"
11 #include "third_party/WebKit/public/platform/WebTraceLocation.h"
15 WebSchedulerImpl::WebSchedulerImpl(
16 ChildScheduler
* child_scheduler
,
17 scoped_refptr
<SingleThreadIdleTaskRunner
> idle_task_runner
,
18 scoped_refptr
<base::SingleThreadTaskRunner
> loading_task_runner
,
19 scoped_refptr
<TaskQueue
> timer_task_runner
)
20 : child_scheduler_(child_scheduler
),
21 idle_task_runner_(idle_task_runner
),
22 timer_task_runner_(timer_task_runner
),
23 loading_web_task_runner_(new WebTaskRunnerImpl(loading_task_runner
)),
24 timer_web_task_runner_(new WebTaskRunnerImpl(timer_task_runner
)) {}
26 WebSchedulerImpl::~WebSchedulerImpl() {
29 void WebSchedulerImpl::shutdown() {
30 child_scheduler_
->Shutdown();
33 bool WebSchedulerImpl::shouldYieldForHighPriorityWork() {
34 return child_scheduler_
->ShouldYieldForHighPriorityWork();
37 bool WebSchedulerImpl::canExceedIdleDeadlineIfRequired() {
38 return child_scheduler_
->CanExceedIdleDeadlineIfRequired();
41 void WebSchedulerImpl::runIdleTask(scoped_ptr
<blink::WebThread::IdleTask
> task
,
42 base::TimeTicks deadline
) {
43 task
->run((deadline
- base::TimeTicks()).InSecondsF());
46 void WebSchedulerImpl::postIdleTask(const blink::WebTraceLocation
& web_location
,
47 blink::WebThread::IdleTask
* task
) {
48 DCHECK(idle_task_runner_
);
49 scoped_ptr
<blink::WebThread::IdleTask
> scoped_task(task
);
50 tracked_objects::Location
location(web_location
.functionName(),
51 web_location
.fileName(), -1, nullptr);
52 idle_task_runner_
->PostIdleTask(
54 base::Bind(&WebSchedulerImpl::runIdleTask
, base::Passed(&scoped_task
)));
57 void WebSchedulerImpl::postNonNestableIdleTask(
58 const blink::WebTraceLocation
& web_location
,
59 blink::WebThread::IdleTask
* task
) {
60 DCHECK(idle_task_runner_
);
61 scoped_ptr
<blink::WebThread::IdleTask
> scoped_task(task
);
62 tracked_objects::Location
location(web_location
.functionName(),
63 web_location
.fileName(), -1, nullptr);
64 idle_task_runner_
->PostNonNestableIdleTask(
66 base::Bind(&WebSchedulerImpl::runIdleTask
, base::Passed(&scoped_task
)));
69 void WebSchedulerImpl::postIdleTaskAfterWakeup(
70 const blink::WebTraceLocation
& web_location
,
71 blink::WebThread::IdleTask
* task
) {
72 DCHECK(idle_task_runner_
);
73 scoped_ptr
<blink::WebThread::IdleTask
> scoped_task(task
);
74 tracked_objects::Location
location(web_location
.functionName(),
75 web_location
.fileName(), -1, nullptr);
76 idle_task_runner_
->PostIdleTaskAfterWakeup(
78 base::Bind(&WebSchedulerImpl::runIdleTask
, base::Passed(&scoped_task
)));
81 blink::WebTaskRunner
* WebSchedulerImpl::loadingTaskRunner() {
82 return loading_web_task_runner_
.get();
85 blink::WebTaskRunner
* WebSchedulerImpl::timerTaskRunner() {
86 return timer_web_task_runner_
.get();
89 void WebSchedulerImpl::postTimerTaskAt(
90 const blink::WebTraceLocation
& web_location
,
91 blink::WebTaskRunner::Task
* task
,
92 double monotonicTime
) {
93 DCHECK(timer_task_runner_
);
94 tracked_objects::Location
location(web_location
.functionName(),
95 web_location
.fileName(), -1, nullptr);
96 timer_task_runner_
->PostDelayedTaskAt(
97 location
, base::Bind(&blink::WebTaskRunner::Task::run
, base::Owned(task
)),
98 base::TimeTicks() + base::TimeDelta::FromSecondsD(monotonicTime
));
101 } // namespace scheduler