Roll android_tools to the latest.
[chromium-blink-merge.git] / content / renderer / scheduler_proxy_task_runner.h
blobc4ccb2a8fedfac2ec64705ec199922fb80c20096
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 CONTENT_RENDERER_SCHEDULER_PROXY_TASK_RUNNER_H_
6 #define CONTENT_RENDERER_SCHEDULER_PROXY_TASK_RUNNER_H_
8 #include "base/debug/task_annotator.h"
9 #include "base/pending_task.h"
10 #include "content/renderer/render_thread_impl.h"
11 #include "third_party/WebKit/public/platform/WebSchedulerProxy.h"
12 #include "third_party/WebKit/public/platform/WebTraceLocation.h"
14 namespace content {
16 // Helper for forwarding posted tasks into different WebSchedulerProxy queues.
17 template <void (blink::WebSchedulerProxy::*ProxyFunction)(
18 const blink::WebTraceLocation& location,
19 blink::WebThread::Task* task)>
20 class SchedulerProxyTaskRunner : public base::SingleThreadTaskRunner {
21 public:
22 SchedulerProxyTaskRunner()
23 : main_thread_id_(base::PlatformThread::CurrentId()),
24 scheduler_proxy_(blink::WebSchedulerProxy::create()),
25 next_sequence_num_(0) {}
27 // base::SingleThreadTaskRunner implementation:
28 virtual bool RunsTasksOnCurrentThread() const OVERRIDE {
29 return base::PlatformThread::CurrentId() == main_thread_id_;
32 virtual bool PostDelayedTask(const tracked_objects::Location& from_here,
33 const base::Closure& task,
34 base::TimeDelta delay) OVERRIDE {
35 DCHECK(delay == base::TimeDelta());
36 base::PendingTask pending_task(from_here, task);
37 pending_task.sequence_num = ++next_sequence_num_;
38 task_annotator_.DidQueueTask("SchedulerProxyTaskRunner::PostDelayedTask",
39 pending_task);
40 blink::WebTraceLocation location(from_here.function_name(),
41 from_here.file_name());
42 TaskAdapter* task_adapter = new TaskAdapter(&task_annotator_, pending_task);
43 (scheduler_proxy_.*ProxyFunction)(location, task_adapter);
44 return true;
47 virtual bool PostNonNestableDelayedTask(
48 const tracked_objects::Location& from_here,
49 const base::Closure& task,
50 base::TimeDelta delay) OVERRIDE {
51 NOTREACHED();
52 return false;
55 protected:
56 virtual ~SchedulerProxyTaskRunner() {}
58 private:
59 class TaskAdapter : public blink::WebThread::Task {
60 public:
61 explicit TaskAdapter(base::debug::TaskAnnotator* task_annotator,
62 const base::PendingTask& pending_task)
63 : task_annotator_(task_annotator), pending_task_(pending_task) {}
64 virtual ~TaskAdapter() {}
66 virtual void run() {
67 task_annotator_->RunTask("SchedulerProxyTaskRunner::PostDelayedTask",
68 "SchedulerProxyTaskRunner::RunTask",
69 pending_task_);
72 private:
73 base::debug::TaskAnnotator* task_annotator_;
74 base::PendingTask pending_task_;
77 const base::PlatformThreadId main_thread_id_;
78 blink::WebSchedulerProxy scheduler_proxy_;
79 base::debug::TaskAnnotator task_annotator_;
80 int next_sequence_num_;
82 DISALLOW_COPY_AND_ASSIGN(SchedulerProxyTaskRunner);
85 } // namespace content
87 #endif // CONTENT_RENDERER_SCHEDULER_PROXY_TASK_RUNNER_H_