| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 // An implementation of WebThread in terms of base::MessageLoop and | |
| 6 // base::Thread | |
| 7 | |
| 8 #include "sky/engine/testing/platform/webthread_impl.h" | |
| 9 | |
| 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 | |
| 16 namespace sky { | |
| 17 | |
| 18 WebThreadBase::WebThreadBase() {} | |
| 19 WebThreadBase::~WebThreadBase() {} | |
| 20 | |
| 21 class WebThreadBase::TaskObserverAdapter | |
| 22 : public base::MessageLoop::TaskObserver { | |
| 23 public: | |
| 24 TaskObserverAdapter(WebThread::TaskObserver* observer) | |
| 25 : observer_(observer) {} | |
| 26 | |
| 27 virtual void WillProcessTask(const base::PendingTask& pending_task) override { | |
| 28 observer_->willProcessTask(); | |
| 29 } | |
| 30 | |
| 31 virtual void DidProcessTask(const base::PendingTask& pending_task) override { | |
| 32 observer_->didProcessTask(); | |
| 33 } | |
| 34 | |
| 35 private: | |
| 36 WebThread::TaskObserver* observer_; | |
| 37 }; | |
| 38 | |
| 39 void WebThreadBase::addTaskObserver(TaskObserver* observer) { | |
| 40 CHECK(isCurrentThread()); | |
| 41 std::pair<TaskObserverMap::iterator, bool> result = task_observer_map_.insert( | |
| 42 std::make_pair(observer, static_cast<TaskObserverAdapter*>(NULL))); | |
| 43 if (result.second) | |
| 44 result.first->second = new TaskObserverAdapter(observer); | |
| 45 base::MessageLoop::current()->AddTaskObserver(result.first->second); | |
| 46 } | |
| 47 | |
| 48 void WebThreadBase::removeTaskObserver(TaskObserver* observer) { | |
| 49 CHECK(isCurrentThread()); | |
| 50 TaskObserverMap::iterator iter = task_observer_map_.find(observer); | |
| 51 if (iter == task_observer_map_.end()) | |
| 52 return; | |
| 53 base::MessageLoop::current()->RemoveTaskObserver(iter->second); | |
| 54 delete iter->second; | |
| 55 task_observer_map_.erase(iter); | |
| 56 } | |
| 57 | |
| 58 WebThreadImpl::WebThreadImpl(const char* name) | |
| 59 : thread_(new base::Thread(name)) { | |
| 60 thread_->Start(); | |
| 61 } | |
| 62 | |
| 63 void WebThreadImpl::postTask(Task* task) { | |
| 64 thread_->message_loop()->PostTask( | |
| 65 FROM_HERE, base::Bind(&blink::WebThread::Task::run, base::Owned(task))); | |
| 66 } | |
| 67 | |
| 68 void WebThreadImpl::postDelayedTask(Task* task, long long delay_ms) { | |
| 69 thread_->message_loop()->PostDelayedTask( | |
| 70 FROM_HERE, | |
| 71 base::Bind(&blink::WebThread::Task::run, base::Owned(task)), | |
| 72 base::TimeDelta::FromMilliseconds(delay_ms)); | |
| 73 } | |
| 74 | |
| 75 void WebThreadImpl::enterRunLoop() { | |
| 76 CHECK(isCurrentThread()); | |
| 77 CHECK(!thread_->message_loop()->is_running()); // We don't support nesting. | |
| 78 thread_->message_loop()->Run(); | |
| 79 } | |
| 80 | |
| 81 void WebThreadImpl::exitRunLoop() { | |
| 82 CHECK(isCurrentThread()); | |
| 83 CHECK(thread_->message_loop()->is_running()); | |
| 84 thread_->message_loop()->Quit(); | |
| 85 } | |
| 86 | |
| 87 bool WebThreadImpl::isCurrentThread() const { | |
| 88 return thread_->thread_id() == base::PlatformThread::CurrentId(); | |
| 89 } | |
| 90 | |
| 91 blink::PlatformThreadId WebThreadImpl::threadId() const { | |
| 92 return thread_->thread_id(); | |
| 93 } | |
| 94 | |
| 95 WebThreadImpl::~WebThreadImpl() { | |
| 96 thread_->Stop(); | |
| 97 } | |
| 98 | |
| 99 WebThreadImplForMessageLoop::WebThreadImplForMessageLoop( | |
| 100 base::MessageLoopProxy* message_loop) | |
| 101 : message_loop_(message_loop) {} | |
| 102 | |
| 103 void WebThreadImplForMessageLoop::postTask(Task* task) { | |
| 104 message_loop_->PostTask( | |
| 105 FROM_HERE, base::Bind(&blink::WebThread::Task::run, base::Owned(task))); | |
| 106 } | |
| 107 | |
| 108 void WebThreadImplForMessageLoop::postDelayedTask(Task* task, | |
| 109 long long delay_ms) { | |
| 110 message_loop_->PostDelayedTask( | |
| 111 FROM_HERE, | |
| 112 base::Bind(&blink::WebThread::Task::run, base::Owned(task)), | |
| 113 base::TimeDelta::FromMilliseconds(delay_ms)); | |
| 114 } | |
| 115 | |
| 116 void WebThreadImplForMessageLoop::enterRunLoop() { | |
| 117 CHECK(isCurrentThread()); | |
| 118 // We don't support nesting. | |
| 119 CHECK(!base::MessageLoop::current()->is_running()); | |
| 120 base::MessageLoop::current()->Run(); | |
| 121 } | |
| 122 | |
| 123 void WebThreadImplForMessageLoop::exitRunLoop() { | |
| 124 CHECK(isCurrentThread()); | |
| 125 CHECK(base::MessageLoop::current()->is_running()); | |
| 126 base::MessageLoop::current()->Quit(); | |
| 127 } | |
| 128 | |
| 129 bool WebThreadImplForMessageLoop::isCurrentThread() const { | |
| 130 return message_loop_->BelongsToCurrentThread(); | |
| 131 } | |
| 132 | |
| 133 blink::PlatformThreadId WebThreadImplForMessageLoop::threadId() const { | |
| 134 return thread_id_; | |
| 135 } | |
| 136 | |
| 137 WebThreadImplForMessageLoop::~WebThreadImplForMessageLoop() {} | |
| 138 | |
| 139 } // namespace sky | |
| OLD | NEW |