1 // Copyright 2014 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/single_thread_idle_task_runner.h"
7 #include "base/location.h"
8 #include "base/trace_event/trace_event.h"
12 SingleThreadIdleTaskRunner::SingleThreadIdleTaskRunner(
13 scoped_refptr
<base::SingleThreadTaskRunner
> idle_priority_task_runner
,
14 scoped_refptr
<base::SingleThreadTaskRunner
> after_wakeup_task_runner
,
16 const char* tracing_category
)
17 : idle_priority_task_runner_(idle_priority_task_runner
),
18 after_wakeup_task_runner_(after_wakeup_task_runner
),
20 tracing_category_(tracing_category
),
22 DCHECK(!idle_priority_task_runner_
||
23 idle_priority_task_runner_
->RunsTasksOnCurrentThread());
24 DCHECK(!after_wakeup_task_runner_
||
25 after_wakeup_task_runner_
->RunsTasksOnCurrentThread());
26 weak_scheduler_ptr_
= weak_factory_
.GetWeakPtr();
29 SingleThreadIdleTaskRunner::~SingleThreadIdleTaskRunner() {
32 SingleThreadIdleTaskRunner::Delegate::Delegate() {
35 SingleThreadIdleTaskRunner::Delegate::~Delegate() {
38 bool SingleThreadIdleTaskRunner::RunsTasksOnCurrentThread() const {
39 return idle_priority_task_runner_
->RunsTasksOnCurrentThread();
42 void SingleThreadIdleTaskRunner::PostIdleTask(
43 const tracked_objects::Location
& from_here
,
44 const IdleTask
& idle_task
) {
45 delegate_
->OnIdleTaskPosted();
46 idle_priority_task_runner_
->PostTask(
47 from_here
, base::Bind(&SingleThreadIdleTaskRunner::RunTask
,
48 weak_scheduler_ptr_
, idle_task
));
51 void SingleThreadIdleTaskRunner::PostNonNestableIdleTask(
52 const tracked_objects::Location
& from_here
,
53 const IdleTask
& idle_task
) {
54 delegate_
->OnIdleTaskPosted();
55 idle_priority_task_runner_
->PostNonNestableTask(
56 from_here
, base::Bind(&SingleThreadIdleTaskRunner::RunTask
,
57 weak_scheduler_ptr_
, idle_task
));
60 void SingleThreadIdleTaskRunner::PostIdleTaskAfterWakeup(
61 const tracked_objects::Location
& from_here
,
62 const IdleTask
& idle_task
) {
63 // Don't signal posting of idle task to the delegate here, wait until the
64 // after-wakeup task posts the real idle task.
65 after_wakeup_task_runner_
->PostTask(
66 FROM_HERE
, base::Bind(&SingleThreadIdleTaskRunner::PostIdleTask
,
67 weak_scheduler_ptr_
, from_here
, idle_task
));
70 void SingleThreadIdleTaskRunner::RunTask(IdleTask idle_task
) {
71 base::TimeTicks deadline
= delegate_
->WillProcessIdleTask();
72 TRACE_EVENT1(tracing_category_
, "SingleThreadIdleTaskRunner::RunTask",
74 (deadline
- base::TimeTicks::Now()).InMillisecondsF());
75 idle_task
.Run(deadline
);
76 delegate_
->DidProcessIdleTask();
79 } // namespace scheduler