Upstream version 11.39.266.0
[platform/framework/web/crosswalk.git] / src / content / renderer / scheduler_proxy_task_runner.h
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.
4
5 #ifndef CONTENT_RENDERER_SCHEDULER_PROXY_TASK_RUNNER_H_
6 #define CONTENT_RENDERER_SCHEDULER_PROXY_TASK_RUNNER_H_
7
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"
13
14 namespace content {
15
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) {}
26
27   // base::SingleThreadTaskRunner implementation:
28   virtual bool RunsTasksOnCurrentThread() const OVERRIDE {
29     return base::PlatformThread::CurrentId() == main_thread_id_;
30   }
31
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;
45   }
46
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;
53   }
54
55  protected:
56   virtual ~SchedulerProxyTaskRunner() {}
57
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() {}
65
66     virtual void run() {
67       task_annotator_->RunTask("SchedulerProxyTaskRunner::PostDelayedTask",
68                                "SchedulerProxyTaskRunner::RunTask",
69                                pending_task_);
70     }
71
72    private:
73     base::debug::TaskAnnotator* task_annotator_;
74     base::PendingTask pending_task_;
75   };
76
77   const base::PlatformThreadId main_thread_id_;
78   blink::WebSchedulerProxy scheduler_proxy_;
79   base::debug::TaskAnnotator task_annotator_;
80   int next_sequence_num_;
81
82   DISALLOW_COPY_AND_ASSIGN(SchedulerProxyTaskRunner);
83 };
84
85 }  // namespace content
86
87 #endif  // CONTENT_RENDERER_SCHEDULER_PROXY_TASK_RUNNER_H_