Rename GetIconID to GetIconId
[chromium-blink-merge.git] / components / scheduler / child / webthread_base.cc
bloba48849d35403514f548641cfb2ab805ace71f2bd
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 // An implementation of WebThread in terms of base::MessageLoop and
6 // base::Thread
8 #include "components/scheduler/child/webthread_base.h"
10 #include "base/bind.h"
11 #include "base/bind_helpers.h"
12 #include "base/pending_task.h"
13 #include "base/threading/platform_thread.h"
14 #include "components/scheduler/child/single_thread_idle_task_runner.h"
15 #include "third_party/WebKit/public/platform/WebTraceLocation.h"
17 namespace scheduler {
19 class WebThreadBase::TaskObserverAdapter
20 : public base::MessageLoop::TaskObserver {
21 public:
22 TaskObserverAdapter(WebThread::TaskObserver* observer)
23 : observer_(observer) {}
25 void WillProcessTask(const base::PendingTask& pending_task) override {
26 observer_->willProcessTask();
29 void DidProcessTask(const base::PendingTask& pending_task) override {
30 observer_->didProcessTask();
33 private:
34 WebThread::TaskObserver* observer_;
37 WebThreadBase::WebThreadBase() {
40 WebThreadBase::~WebThreadBase() {
41 for (auto& observer_entry : task_observer_map_) {
42 delete observer_entry.second;
46 void WebThreadBase::addTaskObserver(TaskObserver* observer) {
47 CHECK(isCurrentThread());
48 std::pair<TaskObserverMap::iterator, bool> result = task_observer_map_.insert(
49 std::make_pair(observer, static_cast<TaskObserverAdapter*>(NULL)));
50 if (result.second)
51 result.first->second = new TaskObserverAdapter(observer);
52 AddTaskObserverInternal(result.first->second);
55 void WebThreadBase::removeTaskObserver(TaskObserver* observer) {
56 CHECK(isCurrentThread());
57 TaskObserverMap::iterator iter = task_observer_map_.find(observer);
58 if (iter == task_observer_map_.end())
59 return;
60 RemoveTaskObserverInternal(iter->second);
61 delete iter->second;
62 task_observer_map_.erase(iter);
65 void WebThreadBase::AddTaskObserverInternal(
66 base::MessageLoop::TaskObserver* observer) {
67 base::MessageLoop::current()->AddTaskObserver(observer);
70 void WebThreadBase::RemoveTaskObserverInternal(
71 base::MessageLoop::TaskObserver* observer) {
72 base::MessageLoop::current()->RemoveTaskObserver(observer);
75 // RunWebThreadTask takes the ownership of |task| from base::Closure and
76 // deletes it on the first invocation of the closure for thread-safety.
77 // base::Closure made from RunWebThreadTask is copyable but Closure::Run
78 // should be called at most only once.
79 // This is because WebThread::Task can contain RefPtr to a
80 // thread-unsafe-reference-counted object (e.g. WorkerThreadTask can contain
81 // RefPtr to WebKit's StringImpl), and if we don't delete |task| here,
82 // it causes a race condition as follows:
83 // [A] In task->run(), more RefPtr's to the refcounted object can be created,
84 // and the reference counter of the object can be modified via these
85 // RefPtr's (as intended) on the thread where the task is executed.
86 // [B] However, base::Closure still retains the ownership of WebThread::Task
87 // even after RunWebThreadTask is called.
88 // When base::Closure is deleted, WebThread::Task is deleted and the
89 // reference counter of the object is decreased by one, possibly from a
90 // different thread from [A], which is a race condition.
91 // Taking the ownership of |task| here by using scoped_ptr and base::Passed
92 // removes the reference counter modification of [B] and the race condition.
93 // When the closure never runs at all, the corresponding WebThread::Task is
94 // destructed when base::Closure is deleted (like [B]). In this case, there
95 // are no reference counter modification like [A] (because task->run() is not
96 // executed), so there are no race conditions.
97 // See https://crbug.com/390851 for more details.
99 // static
100 void WebThreadBase::RunWebThreadTask(scoped_ptr<blink::WebThread::Task> task) {
101 task->run();
104 // static
105 void WebThreadBase::RunWebThreadIdleTask(
106 scoped_ptr<blink::WebThread::IdleTask> idle_task,
107 base::TimeTicks deadline) {
108 idle_task->run((deadline - base::TimeTicks()).InSecondsF());
111 void WebThreadBase::postTask(const blink::WebTraceLocation& location,
112 Task* task) {
113 postDelayedTask(location, task, 0);
116 void WebThreadBase::postDelayedTask(const blink::WebTraceLocation& web_location,
117 Task* task,
118 long long delay_ms) {
119 tracked_objects::Location location(web_location.functionName(),
120 web_location.fileName(), -1, nullptr);
121 TaskRunner()->PostDelayedTask(
122 location,
123 base::Bind(RunWebThreadTask, base::Passed(make_scoped_ptr(task))),
124 base::TimeDelta::FromMilliseconds(delay_ms));
127 void WebThreadBase::postIdleTask(const blink::WebTraceLocation& web_location,
128 IdleTask* idle_task) {
129 tracked_objects::Location location(web_location.functionName(),
130 web_location.fileName(), -1, nullptr);
131 IdleTaskRunner()->PostIdleTask(
132 location, base::Bind(&WebThreadBase::RunWebThreadIdleTask,
133 base::Passed(make_scoped_ptr(idle_task))));
136 void WebThreadBase::postIdleTaskAfterWakeup(
137 const blink::WebTraceLocation& web_location,
138 IdleTask* idle_task) {
139 tracked_objects::Location location(web_location.functionName(),
140 web_location.fileName(), -1, nullptr);
141 IdleTaskRunner()->PostIdleTaskAfterWakeup(
142 location, base::Bind(&WebThreadBase::RunWebThreadIdleTask,
143 base::Passed(make_scoped_ptr(idle_task))));
146 bool WebThreadBase::isCurrentThread() const {
147 return TaskRunner()->BelongsToCurrentThread();
150 } // namespace scheduler