16d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
26d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
36d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)// found in the LICENSE file.
46d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
5116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#ifndef MOJO_SERVICES_HTML_VIEWER_WEBTHREAD_IMPL_H_
6116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#define MOJO_SERVICES_HTML_VIEWER_WEBTHREAD_IMPL_H_
76d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
86d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)#include <map>
96d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
106d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
116d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)#include "base/threading/thread.h"
126d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)#include "third_party/WebKit/public/platform/WebThread.h"
136d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
146d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)namespace mojo {
156d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
166d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)class WebThreadBase : public blink::WebThread {
176d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) public:
186d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  virtual ~WebThreadBase();
196d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
206d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  virtual void addTaskObserver(TaskObserver* observer);
216d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  virtual void removeTaskObserver(TaskObserver* observer);
226d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
236d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  virtual bool isCurrentThread() const = 0;
241320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  virtual blink::PlatformThreadId threadId() const = 0;
256d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
266d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) protected:
276d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  WebThreadBase();
286d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
296d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) private:
306d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  class TaskObserverAdapter;
316d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
326d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  typedef std::map<TaskObserver*, TaskObserverAdapter*> TaskObserverMap;
336d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  TaskObserverMap task_observer_map_;
346d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)};
356d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
366d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)class WebThreadImpl : public WebThreadBase {
376d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) public:
386d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  explicit WebThreadImpl(const char* name);
396d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  virtual ~WebThreadImpl();
406d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
416d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  virtual void postTask(Task* task);
426d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  virtual void postDelayedTask(Task* task, long long delay_ms);
436d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
446d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  virtual void enterRunLoop();
456d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  virtual void exitRunLoop();
466d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
476d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  base::MessageLoop* message_loop() const { return thread_->message_loop(); }
486d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
496d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  virtual bool isCurrentThread() const;
501320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  virtual blink::PlatformThreadId threadId() const;
516d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
526d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) private:
536d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  scoped_ptr<base::Thread> thread_;
546d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)};
556d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
566d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)class WebThreadImplForMessageLoop : public WebThreadBase {
576d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) public:
586d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  explicit WebThreadImplForMessageLoop(
596d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      base::MessageLoopProxy* message_loop);
606d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  virtual ~WebThreadImplForMessageLoop();
616d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
626d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  virtual void postTask(Task* task);
636d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  virtual void postDelayedTask(Task* task, long long delay_ms);
646d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
656d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  virtual void enterRunLoop();
666d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  virtual void exitRunLoop();
676d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
686d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) private:
696d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  virtual bool isCurrentThread() const;
701320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  virtual blink::PlatformThreadId threadId() const;
711320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
726d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  scoped_refptr<base::MessageLoopProxy> message_loop_;
731320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  blink::PlatformThreadId thread_id_;
746d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)};
756d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
766d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)}  // namespace mojo
776d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
78116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#endif  // MOJO_SERVICES_HTML_VIEWER_WEBTHREAD_IMPL_H_
79