Index: webkit/glue/webthread_impl.cc |
diff --git a/webkit/glue/webthread_impl.cc b/webkit/glue/webthread_impl.cc |
index 236497e6891e07e6c64ddc005807b154ddca8631..1354bcb7f6f2903257816b504f940815ecf8d66d 100644 |
--- a/webkit/glue/webthread_impl.cc |
+++ b/webkit/glue/webthread_impl.cc |
@@ -10,9 +10,45 @@ |
#include "base/bind.h" |
#include "base/bind_helpers.h" |
#include "base/message_loop.h" |
+#include "base/threading/platform_thread.h" |
namespace webkit_glue { |
+class WebThreadBase::TaskObserverAdapter : public MessageLoop::TaskObserver { |
+public: |
+ TaskObserverAdapter(WebThread::TaskObserver* observer) |
+ : observer_(observer) { } |
+ |
+ // WebThread::TaskObserver does not have a willProcessTask method. |
+ virtual void WillProcessTask(base::TimeTicks) OVERRIDE { } |
+ |
+ virtual void DidProcessTask(base::TimeTicks) OVERRIDE { |
+ observer_->didProcessTask(); |
+ } |
+ |
+private: |
+ WebThread::TaskObserver* observer_; |
+}; |
+ |
+void WebThreadBase::addTaskObserver(TaskObserver* observer) { |
+ CHECK(IsCurrentThread()); |
+ std::pair<TaskObserverMap::iterator, bool> result = task_observer_map_.insert( |
+ std::make_pair(observer, static_cast<TaskObserverAdapter*>(NULL))); |
+ if (result.second) |
+ result.first->second = new TaskObserverAdapter(observer); |
+ MessageLoop::current()->AddTaskObserver(result.first->second); |
+} |
+ |
+void WebThreadBase::removeTaskObserver(TaskObserver* observer) { |
+ CHECK(IsCurrentThread()); |
+ TaskObserverMap::iterator iter = task_observer_map_.find(observer); |
+ if (iter == task_observer_map_.end()) |
+ return; |
+ MessageLoop::current()->RemoveTaskObserver(iter->second); |
+ delete iter->second; |
+ task_observer_map_.erase(iter); |
+} |
+ |
WebThreadImpl::WebThreadImpl(const char* name) |
: thread_(new base::Thread(name)) { |
thread_->Start(); |
@@ -22,6 +58,7 @@ void WebThreadImpl::postTask(Task* task) { |
thread_->message_loop()->PostTask( |
FROM_HERE, base::Bind(&WebKit::WebThread::Task::run, base::Owned(task))); |
} |
+ |
void WebThreadImpl::postDelayedTask( |
Task* task, long long delay_ms) { |
thread_->message_loop()->PostDelayedTask( |
@@ -30,6 +67,10 @@ void WebThreadImpl::postDelayedTask( |
delay_ms); |
} |
+bool WebThreadImpl::IsCurrentThread() const { |
+ return thread_->thread_id() == base::PlatformThread::CurrentId(); |
darin (slow to review)
2011/11/30 06:45:39
it feels like this should be a method on the Threa
adamk
2011/11/30 21:58:25
Yeah...I think this doesn't come up too often beca
|
+} |
+ |
WebThreadImpl::~WebThreadImpl() { |
thread_->Stop(); |
} |
@@ -52,6 +93,10 @@ void WebThreadImplForMessageLoop::postDelayedTask( |
delay_ms); |
} |
+bool WebThreadImplForMessageLoop::IsCurrentThread() const { |
+ return message_loop_->BelongsToCurrentThread(); |
+} |
+ |
WebThreadImplForMessageLoop::~WebThreadImplForMessageLoop() { |
} |