summaryrefslogtreecommitdiff
path: root/chromium/components/scheduler/child/web_scheduler_impl.cc
blob: 3dfdde0cada6e0cba1571ea920a4042dd61d314c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/scheduler/child/web_scheduler_impl.h"

#include "base/bind.h"
#include "base/single_thread_task_runner.h"
#include "components/scheduler/child/web_task_runner_impl.h"
#include "components/scheduler/child/worker_scheduler.h"
#include "third_party/WebKit/public/platform/WebTraceLocation.h"

namespace scheduler {

WebSchedulerImpl::WebSchedulerImpl(
    ChildScheduler* child_scheduler,
    scoped_refptr<SingleThreadIdleTaskRunner> idle_task_runner,
    scoped_refptr<base::SingleThreadTaskRunner> loading_task_runner,
    scoped_refptr<TaskQueue> timer_task_runner)
    : child_scheduler_(child_scheduler),
      idle_task_runner_(idle_task_runner),
      timer_task_runner_(timer_task_runner),
      loading_web_task_runner_(new WebTaskRunnerImpl(loading_task_runner)),
      timer_web_task_runner_(new WebTaskRunnerImpl(timer_task_runner)) {}

WebSchedulerImpl::~WebSchedulerImpl() {
}

void WebSchedulerImpl::shutdown() {
  child_scheduler_->Shutdown();
}

bool WebSchedulerImpl::shouldYieldForHighPriorityWork() {
  return child_scheduler_->ShouldYieldForHighPriorityWork();
}

bool WebSchedulerImpl::canExceedIdleDeadlineIfRequired() {
  return child_scheduler_->CanExceedIdleDeadlineIfRequired();
}

void WebSchedulerImpl::runIdleTask(scoped_ptr<blink::WebThread::IdleTask> task,
                                   base::TimeTicks deadline) {
  task->run((deadline - base::TimeTicks()).InSecondsF());
}

void WebSchedulerImpl::postIdleTask(const blink::WebTraceLocation& web_location,
                                    blink::WebThread::IdleTask* task) {
  DCHECK(idle_task_runner_);
  scoped_ptr<blink::WebThread::IdleTask> scoped_task(task);
  tracked_objects::Location location(web_location.functionName(),
                                     web_location.fileName(), -1, nullptr);
  idle_task_runner_->PostIdleTask(
      location,
      base::Bind(&WebSchedulerImpl::runIdleTask, base::Passed(&scoped_task)));
}

void WebSchedulerImpl::postNonNestableIdleTask(
    const blink::WebTraceLocation& web_location,
    blink::WebThread::IdleTask* task) {
  DCHECK(idle_task_runner_);
  scoped_ptr<blink::WebThread::IdleTask> scoped_task(task);
  tracked_objects::Location location(web_location.functionName(),
                                     web_location.fileName(), -1, nullptr);
  idle_task_runner_->PostNonNestableIdleTask(
      location,
      base::Bind(&WebSchedulerImpl::runIdleTask, base::Passed(&scoped_task)));
}

void WebSchedulerImpl::postIdleTaskAfterWakeup(
    const blink::WebTraceLocation& web_location,
    blink::WebThread::IdleTask* task) {
  DCHECK(idle_task_runner_);
  scoped_ptr<blink::WebThread::IdleTask> scoped_task(task);
  tracked_objects::Location location(web_location.functionName(),
                                     web_location.fileName(), -1, nullptr);
  idle_task_runner_->PostIdleTaskAfterWakeup(
      location,
      base::Bind(&WebSchedulerImpl::runIdleTask, base::Passed(&scoped_task)));
}

blink::WebTaskRunner* WebSchedulerImpl::loadingTaskRunner() {
  return loading_web_task_runner_.get();
}

blink::WebTaskRunner* WebSchedulerImpl::timerTaskRunner() {
  return timer_web_task_runner_.get();
}

void WebSchedulerImpl::postTimerTaskAt(
    const blink::WebTraceLocation& web_location,
    blink::WebTaskRunner::Task* task,
    double monotonicTime) {
  DCHECK(timer_task_runner_);
  tracked_objects::Location location(web_location.functionName(),
                                     web_location.fileName(), -1, nullptr);
  timer_task_runner_->PostDelayedTaskAt(
      location,
      base::Bind(&WebTaskRunnerImpl::runTask,
                 base::Passed(scoped_ptr<blink::WebTaskRunner::Task>(task))),
      base::TimeTicks() + base::TimeDelta::FromSecondsD(monotonicTime));
}

}  // namespace scheduler