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
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"
19 class WebThreadBase::TaskObserverAdapter
20 : public base::MessageLoop::TaskObserver
{
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();
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
)));
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())
60 RemoveTaskObserverInternal(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
);
76 void WebThreadBase::RunWebThreadIdleTask(
77 scoped_ptr
<blink::WebThread::IdleTask
> idle_task
,
78 base::TimeTicks deadline
) {
79 idle_task
->run((deadline
- base::TimeTicks()).InSecondsF());
82 void WebThreadBase::postIdleTask(const blink::WebTraceLocation
& web_location
,
83 IdleTask
* idle_task
) {
84 tracked_objects::Location
location(web_location
.functionName(),
85 web_location
.fileName(), -1, nullptr);
86 IdleTaskRunner()->PostIdleTask(
87 location
, base::Bind(&WebThreadBase::RunWebThreadIdleTask
,
88 base::Passed(make_scoped_ptr(idle_task
))));
91 void WebThreadBase::postIdleTaskAfterWakeup(
92 const blink::WebTraceLocation
& web_location
,
93 IdleTask
* idle_task
) {
94 tracked_objects::Location
location(web_location
.functionName(),
95 web_location
.fileName(), -1, nullptr);
96 IdleTaskRunner()->PostIdleTaskAfterWakeup(
97 location
, base::Bind(&WebThreadBase::RunWebThreadIdleTask
,
98 base::Passed(make_scoped_ptr(idle_task
))));
101 bool WebThreadBase::isCurrentThread() const {
102 return TaskRunner()->BelongsToCurrentThread();
105 } // namespace scheduler