Roll src/third_party/WebKit 605a979:06cb9e9 (svn 202556:202558)
[chromium-blink-merge.git] / components / scheduler / child / scheduler_helper.h
blob16dc77a40daf70dcb6c4076d5c68c8a2eb4b4f8f
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 #ifndef COMPONENTS_SCHEDULER_CHILD_SCHEDULER_HELPER_H_
6 #define COMPONENTS_SCHEDULER_CHILD_SCHEDULER_HELPER_H_
8 #include "components/scheduler/child/task_queue_manager.h"
9 #include "components/scheduler/child/task_queue_selector.h"
10 #include "components/scheduler/scheduler_export.h"
12 namespace base {
13 class TickClock;
16 namespace scheduler {
18 class SchedulerTaskRunnerDelegate;
20 // Common scheduler functionality for default tasks.
21 class SCHEDULER_EXPORT SchedulerHelper {
22 public:
23 // Category strings must have application lifetime (statics or
24 // literals). They may not include " chars.
25 SchedulerHelper(scoped_refptr<SchedulerTaskRunnerDelegate> main_task_runner,
26 const char* tracing_category,
27 const char* disabled_by_default_tracing_category,
28 const char* disabled_by_default_verbose_tracing_category);
29 ~SchedulerHelper();
31 // Returns the default task runner.
32 scoped_refptr<TaskQueue> DefaultTaskRunner();
34 // Returns the control task runner. Tasks posted to this runner are executed
35 // with the highest priority. Care must be taken to avoid starvation of other
36 // task queues.
37 scoped_refptr<TaskQueue> ControlTaskRunner();
39 // Returns the control task after wakeup runner. Tasks posted to this runner
40 // are executed with the highest priority but do not cause the scheduler to
41 // wake up. Care must be taken to avoid starvation of other task queues.
42 scoped_refptr<TaskQueue> ControlAfterWakeUpTaskRunner();
44 // Adds or removes a task observer from the scheduler. The observer will be
45 // notified before and after every executed task. These functions can only be
46 // called on the thread this class was created on.
47 void AddTaskObserver(base::MessageLoop::TaskObserver* task_observer);
48 void RemoveTaskObserver(base::MessageLoop::TaskObserver* task_observer);
50 // Shuts down the scheduler by dropping any remaining pending work in the work
51 // queues. After this call any work posted to the task runners will be
52 // silently dropped.
53 void Shutdown();
55 // Returns true if Shutdown() has been called. Otherwise returns false.
56 bool IsShutdown() const { return !task_queue_manager_.get(); }
58 void CheckOnValidThread() const {
59 DCHECK(thread_checker_.CalledOnValidThread());
62 // Creates a new TaskQueue with the given |spec|.
63 scoped_refptr<TaskQueue> NewTaskQueue(const TaskQueue::Spec& spec);
65 // Accessor methods.
66 base::TimeTicks Now() const;
67 base::TimeTicks NextPendingDelayedTaskRunTime() const;
68 bool GetAndClearSystemIsQuiescentBit();
70 // Test helpers.
71 void SetTimeSourceForTesting(scoped_ptr<base::TickClock> time_source);
72 void SetWorkBatchSizeForTesting(size_t work_batch_size);
73 TaskQueueManager* GetTaskQueueManagerForTesting();
75 private:
76 friend class SchedulerHelperTest;
78 base::ThreadChecker thread_checker_;
79 scoped_refptr<SchedulerTaskRunnerDelegate> main_task_runner_;
80 scoped_ptr<TaskQueueManager> task_queue_manager_;
81 scoped_refptr<TaskQueue> control_task_runner_;
82 scoped_refptr<TaskQueue> control_after_wakeup_task_runner_;
83 scoped_refptr<TaskQueue> default_task_runner_;
85 scoped_ptr<base::TickClock> time_source_;
87 const char* tracing_category_;
88 const char* disabled_by_default_tracing_category_;
90 DISALLOW_COPY_AND_ASSIGN(SchedulerHelper);
93 } // namespace scheduler
95 #endif // COMPONENTS_SCHEDULER_CHILD_SCHEDULER_HELPER_H_