OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // An implementation of WebThread in terms of base::MessageLoop and | 5 // An implementation of WebThread in terms of base::MessageLoop and |
6 // base::Thread | 6 // base::Thread |
7 | 7 |
8 #include "content/child/webthread_impl.h" | 8 #include "content/child/webthread_base.h" |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
12 #include "base/pending_task.h" | 12 #include "base/pending_task.h" |
13 #include "base/threading/platform_thread.h" | 13 #include "base/threading/platform_thread.h" |
| 14 #include "content/child/scheduler/single_thread_idle_task_runner.h" |
14 #include "third_party/WebKit/public/platform/WebTraceLocation.h" | 15 #include "third_party/WebKit/public/platform/WebTraceLocation.h" |
15 | 16 |
16 namespace content { | 17 namespace content { |
17 | 18 |
18 class WebThreadBase::TaskObserverAdapter | 19 class WebThreadBase::TaskObserverAdapter |
19 : public base::MessageLoop::TaskObserver { | 20 : public base::MessageLoop::TaskObserver { |
20 public: | 21 public: |
21 TaskObserverAdapter(WebThread::TaskObserver* observer) | 22 TaskObserverAdapter(WebThread::TaskObserver* observer) |
22 : observer_(observer) {} | 23 : observer_(observer) {} |
23 | 24 |
24 void WillProcessTask(const base::PendingTask& pending_task) override { | 25 void WillProcessTask(const base::PendingTask& pending_task) override { |
25 observer_->willProcessTask(); | 26 observer_->willProcessTask(); |
26 } | 27 } |
27 | 28 |
28 void DidProcessTask(const base::PendingTask& pending_task) override { | 29 void DidProcessTask(const base::PendingTask& pending_task) override { |
29 observer_->didProcessTask(); | 30 observer_->didProcessTask(); |
30 } | 31 } |
31 | 32 |
32 private: | 33 private: |
33 WebThread::TaskObserver* observer_; | 34 WebThread::TaskObserver* observer_; |
34 }; | 35 }; |
35 | 36 |
36 WebThreadBase::WebThreadBase() { | 37 WebThreadBase::WebThreadBase() { |
37 } | 38 } |
38 | 39 |
39 WebThreadBase::~WebThreadBase() { | 40 WebThreadBase::~WebThreadBase() { |
40 for (auto& observer_entry : task_observer_map_) { | 41 for (auto& observer_entry : task_observer_map_) { |
41 delete observer_entry.second; | 42 delete observer_entry.second; |
42 } | 43 } |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 // destructed when base::Closure is deleted (like [B]). In this case, there | 94 // destructed when base::Closure is deleted (like [B]). In this case, there |
94 // are no reference counter modification like [A] (because task->run() is not | 95 // are no reference counter modification like [A] (because task->run() is not |
95 // executed), so there are no race conditions. | 96 // executed), so there are no race conditions. |
96 // See https://crbug.com/390851 for more details. | 97 // See https://crbug.com/390851 for more details. |
97 // | 98 // |
98 // static | 99 // static |
99 void WebThreadBase::RunWebThreadTask(scoped_ptr<blink::WebThread::Task> task) { | 100 void WebThreadBase::RunWebThreadTask(scoped_ptr<blink::WebThread::Task> task) { |
100 task->run(); | 101 task->run(); |
101 } | 102 } |
102 | 103 |
| 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()); |
| 109 } |
| 110 |
103 void WebThreadBase::postTask(const blink::WebTraceLocation& location, | 111 void WebThreadBase::postTask(const blink::WebTraceLocation& location, |
104 Task* task) { | 112 Task* task) { |
105 postDelayedTask(location, task, 0); | 113 postDelayedTask(location, task, 0); |
106 } | 114 } |
107 | 115 |
108 void WebThreadBase::postDelayedTask(const blink::WebTraceLocation& web_location, | 116 void WebThreadBase::postDelayedTask(const blink::WebTraceLocation& web_location, |
109 Task* task, | 117 Task* task, |
110 long long delay_ms) { | 118 long long delay_ms) { |
111 tracked_objects::Location location(web_location.functionName(), | 119 tracked_objects::Location location(web_location.functionName(), |
112 web_location.fileName(), -1, nullptr); | 120 web_location.fileName(), -1, nullptr); |
113 TaskRunner()->PostDelayedTask( | 121 TaskRunner()->PostDelayedTask( |
114 location, | 122 location, |
115 base::Bind(RunWebThreadTask, base::Passed(make_scoped_ptr(task))), | 123 base::Bind(RunWebThreadTask, base::Passed(make_scoped_ptr(task))), |
116 base::TimeDelta::FromMilliseconds(delay_ms)); | 124 base::TimeDelta::FromMilliseconds(delay_ms)); |
117 } | 125 } |
118 | 126 |
| 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)))); |
| 134 } |
| 135 |
| 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)))); |
| 144 } |
| 145 |
119 void WebThreadBase::enterRunLoop() { | 146 void WebThreadBase::enterRunLoop() { |
120 CHECK(isCurrentThread()); | 147 CHECK(isCurrentThread()); |
121 CHECK(MessageLoop()); | 148 CHECK(MessageLoop()); |
122 CHECK(!MessageLoop()->is_running()); // We don't support nesting. | 149 CHECK(!MessageLoop()->is_running()); // We don't support nesting. |
123 MessageLoop()->Run(); | 150 MessageLoop()->Run(); |
124 } | 151 } |
125 | 152 |
126 void WebThreadBase::exitRunLoop() { | 153 void WebThreadBase::exitRunLoop() { |
127 CHECK(isCurrentThread()); | 154 CHECK(isCurrentThread()); |
128 CHECK(MessageLoop()); | 155 CHECK(MessageLoop()); |
129 CHECK(MessageLoop()->is_running()); | 156 CHECK(MessageLoop()->is_running()); |
130 MessageLoop()->Quit(); | 157 MessageLoop()->Quit(); |
131 } | 158 } |
132 | 159 |
133 bool WebThreadBase::isCurrentThread() const { | 160 bool WebThreadBase::isCurrentThread() const { |
134 return TaskRunner()->BelongsToCurrentThread(); | 161 return TaskRunner()->BelongsToCurrentThread(); |
135 } | 162 } |
136 | 163 |
137 blink::PlatformThreadId WebThreadImpl::threadId() const { | |
138 return thread_->thread_id(); | |
139 } | |
140 | |
141 WebThreadImpl::WebThreadImpl(const char* name) | |
142 : thread_(new base::Thread(name)) { | |
143 thread_->Start(); | |
144 } | |
145 | |
146 WebThreadImpl::~WebThreadImpl() { | |
147 thread_->Stop(); | |
148 } | |
149 | |
150 base::MessageLoop* WebThreadImpl::MessageLoop() const { | |
151 return nullptr; | |
152 } | |
153 | |
154 base::SingleThreadTaskRunner* WebThreadImpl::TaskRunner() const { | |
155 return thread_->message_loop_proxy().get(); | |
156 } | |
157 | |
158 } // namespace content | 164 } // namespace content |
OLD | NEW |