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 #ifndef COMPONENTS_SCHEDULER_CHILD_SINGLE_THREAD_IDLE_TASK_RUNNER_H_
6 #define COMPONENTS_SCHEDULER_CHILD_SINGLE_THREAD_IDLE_TASK_RUNNER_H_
9 #include "base/callback.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/single_thread_task_runner.h"
12 #include "base/time/time.h"
13 #include "components/scheduler/scheduler_export.h"
17 // A SingleThreadIdleTaskRunner is a task runner for running idle tasks. Idle
18 // tasks have an unbound argument which is bound to a deadline
19 // (in base::TimeTicks) when they are run. The idle task is expected to
20 // complete by this deadline.
21 class SingleThreadIdleTaskRunner
22 : public base::RefCountedThreadSafe
<SingleThreadIdleTaskRunner
> {
24 typedef base::Callback
<void(base::TimeTicks
)> IdleTask
;
26 // Used to request idle task deadlines and signal posting of idle tasks.
27 class SCHEDULER_EXPORT Delegate
{
32 // Signals that an idle task has been posted. This will be called on the
33 // posting thread, which may not be the same thread as the
34 // SingleThreadIdleTaskRunner runs on.
35 virtual void OnIdleTaskPosted() = 0;
37 // Signals that a new idle task is about to be run and returns the deadline
38 // for this idle task.
39 virtual base::TimeTicks
WillProcessIdleTask() = 0;
41 // Signals that an idle task has finished being run.
42 virtual void DidProcessIdleTask() = 0;
45 DISALLOW_COPY_AND_ASSIGN(Delegate
);
48 // NOTE Category strings must have application lifetime (statics or
49 // literals). They may not include " chars.
50 SingleThreadIdleTaskRunner(
51 scoped_refptr
<base::SingleThreadTaskRunner
> idle_priority_task_runner
,
52 scoped_refptr
<base::SingleThreadTaskRunner
> after_wakeup_task_runner
,
54 const char* tracing_category
);
56 virtual void PostIdleTask(const tracked_objects::Location
& from_here
,
57 const IdleTask
& idle_task
);
59 virtual void PostNonNestableIdleTask(
60 const tracked_objects::Location
& from_here
,
61 const IdleTask
& idle_task
);
63 virtual void PostIdleTaskAfterWakeup(
64 const tracked_objects::Location
& from_here
,
65 const IdleTask
& idle_task
);
67 bool RunsTasksOnCurrentThread() const;
70 virtual ~SingleThreadIdleTaskRunner();
73 friend class base::RefCountedThreadSafe
<SingleThreadIdleTaskRunner
>;
75 void RunTask(IdleTask idle_task
);
77 scoped_refptr
<base::SingleThreadTaskRunner
> idle_priority_task_runner_
;
78 scoped_refptr
<base::SingleThreadTaskRunner
> after_wakeup_task_runner_
;
79 Delegate
* delegate_
; // NOT OWNED
80 const char* tracing_category_
;
81 base::WeakPtr
<SingleThreadIdleTaskRunner
> weak_scheduler_ptr_
;
82 base::WeakPtrFactory
<SingleThreadIdleTaskRunner
> weak_factory_
;
83 DISALLOW_COPY_AND_ASSIGN(SingleThreadIdleTaskRunner
);
86 } // namespace scheduler
88 #endif // COMPONENTS_SCHEDULER_CHILD_SINGLE_THREAD_IDLE_TASK_RUNNER_H_