Revert of [Android WebView] Synthesize a fake page loading event on page source modif...
[chromium-blink-merge.git] / content / child / webthread_impl.cc
blob8a1d7cdda63ad1126ae2161f0ca5b592dd02b01d
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 "content/child/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 content {
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 void WebThreadBase::postTask(const blink::WebTraceLocation& location,
105 Task* task) {
106 postDelayedTask(location, task, 0);
109 void WebThreadBase::postDelayedTask(const blink::WebTraceLocation& web_location,
110 Task* task,
111 long long delay_ms) {
112 tracked_objects::Location location(web_location.functionName(),
113 web_location.fileName(), -1, nullptr);
114 TaskRunner()->PostDelayedTask(
115 location,
116 base::Bind(RunWebThreadTask, base::Passed(make_scoped_ptr(task))),
117 base::TimeDelta::FromMilliseconds(delay_ms));
120 void WebThreadBase::enterRunLoop() {
121 CHECK(isCurrentThread());
122 CHECK(!MessageLoop()->is_running()); // We don't support nesting.
123 MessageLoop()->Run();
126 void WebThreadBase::exitRunLoop() {
127 CHECK(isCurrentThread());
128 CHECK(MessageLoop()->is_running());
129 MessageLoop()->Quit();
132 bool WebThreadBase::isCurrentThread() const {
133 return TaskRunner()->BelongsToCurrentThread();
136 blink::PlatformThreadId WebThreadImpl::threadId() const {
137 return thread_->thread_id();
140 WebThreadImpl::WebThreadImpl(const char* name)
141 : thread_(new base::Thread(name)) {
142 thread_->Start();
145 WebThreadImpl::~WebThreadImpl() {
146 thread_->Stop();
149 base::MessageLoop* WebThreadImpl::MessageLoop() const {
150 return thread_->message_loop();
153 base::SingleThreadTaskRunner* WebThreadImpl::TaskRunner() const {
154 return thread_->message_loop_proxy().get();
157 WebThreadImplForMessageLoop::WebThreadImplForMessageLoop(
158 scoped_refptr<base::SingleThreadTaskRunner> owning_thread_task_runner)
159 : owning_thread_task_runner_(owning_thread_task_runner),
160 thread_id_(base::PlatformThread::CurrentId()) {
163 blink::PlatformThreadId WebThreadImplForMessageLoop::threadId() const {
164 return thread_id_;
167 WebThreadImplForMessageLoop::~WebThreadImplForMessageLoop() {
170 base::MessageLoop* WebThreadImplForMessageLoop::MessageLoop() const {
171 DCHECK(isCurrentThread());
172 return base::MessageLoop::current();
175 base::SingleThreadTaskRunner* WebThreadImplForMessageLoop::TaskRunner() const {
176 return owning_thread_task_runner_.get();
179 } // namespace content