cr: Add 'gn' command
[chromium-blink-merge.git] / mojo / services / html_viewer / webthread_impl.cc
bloba6f6f64771a4fac554071025bfd056af921c3cd7
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 "mojo/services/html_viewer/webthread_impl.h"
10 #include "base/bind.h"
11 #include "base/bind_helpers.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/pending_task.h"
14 #include "base/threading/platform_thread.h"
15 #include "third_party/WebKit/public/platform/WebTraceLocation.h"
17 namespace html_viewer {
19 WebThreadBase::WebThreadBase() {}
20 WebThreadBase::~WebThreadBase() {}
22 class WebThreadBase::TaskObserverAdapter
23 : public base::MessageLoop::TaskObserver {
24 public:
25 TaskObserverAdapter(WebThread::TaskObserver* observer)
26 : observer_(observer) {}
28 void WillProcessTask(const base::PendingTask& pending_task) override {
29 observer_->willProcessTask();
32 void DidProcessTask(const base::PendingTask& pending_task) override {
33 observer_->didProcessTask();
36 private:
37 WebThread::TaskObserver* observer_;
40 void WebThreadBase::addTaskObserver(TaskObserver* observer) {
41 CHECK(isCurrentThread());
42 std::pair<TaskObserverMap::iterator, bool> result = task_observer_map_.insert(
43 std::make_pair(observer, static_cast<TaskObserverAdapter*>(NULL)));
44 if (result.second)
45 result.first->second = new TaskObserverAdapter(observer);
46 base::MessageLoop::current()->AddTaskObserver(result.first->second);
49 void WebThreadBase::removeTaskObserver(TaskObserver* observer) {
50 CHECK(isCurrentThread());
51 TaskObserverMap::iterator iter = task_observer_map_.find(observer);
52 if (iter == task_observer_map_.end())
53 return;
54 base::MessageLoop::current()->RemoveTaskObserver(iter->second);
55 delete iter->second;
56 task_observer_map_.erase(iter);
59 WebThreadImpl::WebThreadImpl(const char* name)
60 : thread_(new base::Thread(name)),
61 web_scheduler_(new WebSchedulerImpl(thread_->task_runner())) {
62 thread_->Start();
65 // RunWebThreadTask takes the ownership of |task| from base::Closure and
66 // deletes it on the first invocation of the closure for thread-safety.
67 // base::Closure made from RunWebThreadTask is copyable but Closure::Run
68 // should be called at most only once.
69 // This is because WebThread::Task can contain RefPtr to a
70 // thread-unsafe-reference-counted object (e.g. WorkerThreadTask can contain
71 // RefPtr to WebKit's StringImpl), and if we don't delete |task| here,
72 // it causes a race condition as follows:
73 // [A] In task->run(), more RefPtr's to the refcounted object can be created,
74 // and the reference counter of the object can be modified via these
75 // RefPtr's (as intended) on the thread where the task is executed.
76 // [B] However, base::Closure still retains the ownership of WebThread::Task
77 // even after RunWebThreadTask is called.
78 // When base::Closure is deleted, WebThread::Task is deleted and the
79 // reference counter of the object is decreased by one, possibly from a
80 // different thread from [A], which is a race condition.
81 // Taking the ownership of |task| here by using scoped_ptr and base::Passed
82 // removes the reference counter modification of [B] and the race condition.
83 // When the closure never runs at all, the corresponding WebThread::Task is
84 // destructed when base::Closure is deleted (like [B]). In this case, there
85 // are no reference counter modification like [A] (because task->run() is not
86 // executed), so there are no race conditions.
87 // See https://crbug.com/390851 for more details.
88 static void RunWebThreadTask(scoped_ptr<blink::WebThread::Task> task) {
89 task->run();
92 void WebThreadImpl::postTask(const blink::WebTraceLocation& location,
93 Task* task) {
94 postDelayedTask(location, task, 0);
97 void WebThreadImpl::postDelayedTask(const blink::WebTraceLocation& web_location,
98 Task* task,
99 long long delay_ms) {
100 tracked_objects::Location location(web_location.functionName(),
101 web_location.fileName(), -1, nullptr);
102 thread_->message_loop()->PostDelayedTask(
103 location,
104 base::Bind(RunWebThreadTask, base::Passed(make_scoped_ptr(task))),
105 base::TimeDelta::FromMilliseconds(delay_ms));
108 void WebThreadImpl::enterRunLoop() {
109 CHECK(isCurrentThread());
110 CHECK(!thread_->message_loop()->is_running()); // We don't support nesting.
111 thread_->message_loop()->Run();
114 void WebThreadImpl::exitRunLoop() {
115 CHECK(isCurrentThread());
116 CHECK(thread_->message_loop()->is_running());
117 thread_->message_loop()->Quit();
120 blink::WebScheduler* WebThreadImpl::scheduler() const {
121 return web_scheduler_.get();
124 bool WebThreadImpl::isCurrentThread() const {
125 return thread_->thread_id() == base::PlatformThread::CurrentId();
128 blink::PlatformThreadId WebThreadImpl::threadId() const {
129 return thread_->thread_id();
132 WebThreadImpl::~WebThreadImpl() {
133 thread_->Stop();
136 WebThreadImplForMessageLoop::WebThreadImplForMessageLoop(
137 base::MessageLoopProxy* message_loop)
138 : message_loop_(message_loop),
139 web_scheduler_(new WebSchedulerImpl(message_loop_)) {
142 void WebThreadImplForMessageLoop::postTask(
143 const blink::WebTraceLocation& location,
144 Task* task) {
145 postDelayedTask(location, task, 0);
148 void WebThreadImplForMessageLoop::postDelayedTask(
149 const blink::WebTraceLocation& web_location,
150 Task* task,
151 long long delay_ms) {
152 tracked_objects::Location location(web_location.functionName(),
153 web_location.fileName(), -1, nullptr);
154 message_loop_->PostDelayedTask(
155 location,
156 base::Bind(RunWebThreadTask, base::Passed(make_scoped_ptr(task))),
157 base::TimeDelta::FromMilliseconds(delay_ms));
160 void WebThreadImplForMessageLoop::enterRunLoop() {
161 CHECK(isCurrentThread());
162 // We don't support nesting.
163 CHECK(!base::MessageLoop::current()->is_running());
164 base::MessageLoop::current()->Run();
167 void WebThreadImplForMessageLoop::exitRunLoop() {
168 CHECK(isCurrentThread());
169 CHECK(base::MessageLoop::current()->is_running());
170 base::MessageLoop::current()->Quit();
173 blink::WebScheduler* WebThreadImplForMessageLoop::scheduler() const {
174 return web_scheduler_.get();
177 bool WebThreadImplForMessageLoop::isCurrentThread() const {
178 return message_loop_->BelongsToCurrentThread();
181 blink::PlatformThreadId WebThreadImplForMessageLoop::threadId() const {
182 return thread_id_;
185 WebThreadImplForMessageLoop::~WebThreadImplForMessageLoop() {}
187 } // namespace html_viewer