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"
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
{
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",
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
);
47 virtual bool PostNonNestableDelayedTask(
48 const tracked_objects::Location
& from_here
,
49 const base::Closure
& task
,
50 base::TimeDelta delay
) OVERRIDE
{
56 virtual ~SchedulerProxyTaskRunner() {}
59 class TaskAdapter
: public blink::WebThread::Task
{
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() {}
67 task_annotator_
->RunTask("SchedulerProxyTaskRunner::PostDelayedTask",
68 "SchedulerProxyTaskRunner::RunTask",
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_