1a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// An implementation of WebThread in terms of base::MessageLoop and
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// base::Thread
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "content/child/webthread_impl.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/bind.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/bind_helpers.h"
129ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#include "base/message_loop/message_loop.h"
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/pending_task.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/threading/platform_thread.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
16a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)namespace content {
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
18a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)WebThreadBase::WebThreadBase() {}
19a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)WebThreadBase::~WebThreadBase() {}
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
21a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class WebThreadBase::TaskObserverAdapter
22a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    : public base::MessageLoop::TaskObserver {
23b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) public:
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TaskObserverAdapter(WebThread::TaskObserver* observer)
25a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      : observer_(observer) {}
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void WillProcessTask(const base::PendingTask& pending_task) OVERRIDE {
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    observer_->willProcessTask();
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void DidProcessTask(const base::PendingTask& pending_task) OVERRIDE {
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    observer_->didProcessTask();
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)private:
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WebThread::TaskObserver* observer_;
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void WebThreadBase::addTaskObserver(TaskObserver* observer) {
4090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  CHECK(isCurrentThread());
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::pair<TaskObserverMap::iterator, bool> result = task_observer_map_.insert(
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      std::make_pair(observer, static_cast<TaskObserverAdapter*>(NULL)));
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (result.second)
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    result.first->second = new TaskObserverAdapter(observer);
45b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  base::MessageLoop::current()->AddTaskObserver(result.first->second);
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void WebThreadBase::removeTaskObserver(TaskObserver* observer) {
4990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  CHECK(isCurrentThread());
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TaskObserverMap::iterator iter = task_observer_map_.find(observer);
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (iter == task_observer_map_.end())
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
53b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  base::MessageLoop::current()->RemoveTaskObserver(iter->second);
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  delete iter->second;
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  task_observer_map_.erase(iter);
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)WebThreadImpl::WebThreadImpl(const char* name)
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : thread_(new base::Thread(name)) {
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  thread_->Start();
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void WebThreadImpl::postTask(Task* task) {
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  thread_->message_loop()->PostTask(
65f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      FROM_HERE, base::Bind(&blink::WebThread::Task::run, base::Owned(task)));
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
68a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WebThreadImpl::postDelayedTask(Task* task, long long delay_ms) {
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  thread_->message_loop()->PostDelayedTask(
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      FROM_HERE,
71f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      base::Bind(&blink::WebThread::Task::run, base::Owned(task)),
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      base::TimeDelta::FromMilliseconds(delay_ms));
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void WebThreadImpl::enterRunLoop() {
7690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  CHECK(isCurrentThread());
77a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  CHECK(!thread_->message_loop()->is_running());  // We don't support nesting.
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  thread_->message_loop()->Run();
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void WebThreadImpl::exitRunLoop() {
8290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  CHECK(isCurrentThread());
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CHECK(thread_->message_loop()->is_running());
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  thread_->message_loop()->Quit();
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)bool WebThreadImpl::isCurrentThread() const {
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return thread_->thread_id() == base::PlatformThread::CurrentId();
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
911320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciblink::PlatformThreadId WebThreadImpl::threadId() const {
921320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  return thread_->thread_id();
931320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
941320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)WebThreadImpl::~WebThreadImpl() {
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  thread_->Stop();
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)WebThreadImplForMessageLoop::WebThreadImplForMessageLoop(
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    base::MessageLoopProxy* message_loop)
1011320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    : message_loop_(message_loop),
1021320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      thread_id_(base::PlatformThread::CurrentId()) {}
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void WebThreadImplForMessageLoop::postTask(Task* task) {
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  message_loop_->PostTask(
106f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      FROM_HERE, base::Bind(&blink::WebThread::Task::run, base::Owned(task)));
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
109a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WebThreadImplForMessageLoop::postDelayedTask(Task* task,
110a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                                  long long delay_ms) {
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  message_loop_->PostDelayedTask(
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      FROM_HERE,
113f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      base::Bind(&blink::WebThread::Task::run, base::Owned(task)),
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      base::TimeDelta::FromMilliseconds(delay_ms));
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void WebThreadImplForMessageLoop::enterRunLoop() {
11890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  CHECK(isCurrentThread());
119a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // We don't support nesting.
120a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  CHECK(!base::MessageLoop::current()->is_running());
121b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  base::MessageLoop::current()->Run();
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void WebThreadImplForMessageLoop::exitRunLoop() {
12590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  CHECK(isCurrentThread());
126b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  CHECK(base::MessageLoop::current()->is_running());
127b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  base::MessageLoop::current()->Quit();
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
13090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)bool WebThreadImplForMessageLoop::isCurrentThread() const {
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return message_loop_->BelongsToCurrentThread();
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1341320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciblink::PlatformThreadId WebThreadImplForMessageLoop::threadId() const {
1351320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  return thread_id_;
1361320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
1371320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
138a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)WebThreadImplForMessageLoop::~WebThreadImplForMessageLoop() {}
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
140a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}  // namespace content
141