Home | History | Annotate | Download | only in child
      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 #ifndef CONTENT_CHILD_WEBTHREAD_IMPL_H_
      6 #define CONTENT_CHILD_WEBTHREAD_IMPL_H_
      7 
      8 #include <map>
      9 
     10 #include "base/memory/scoped_ptr.h"
     11 #include "base/threading/thread.h"
     12 #include "content/common/content_export.h"
     13 #include "third_party/WebKit/public/platform/WebThread.h"
     14 
     15 namespace content {
     16 
     17 class CONTENT_EXPORT WebThreadBase : public blink::WebThread {
     18  public:
     19   virtual ~WebThreadBase();
     20 
     21   virtual void addTaskObserver(TaskObserver* observer);
     22   virtual void removeTaskObserver(TaskObserver* observer);
     23 
     24   virtual bool isCurrentThread() const = 0;
     25   virtual blink::PlatformThreadId threadId() const = 0;
     26 
     27  protected:
     28   WebThreadBase();
     29 
     30  private:
     31   class TaskObserverAdapter;
     32 
     33   typedef std::map<TaskObserver*, TaskObserverAdapter*> TaskObserverMap;
     34   TaskObserverMap task_observer_map_;
     35 };
     36 
     37 class CONTENT_EXPORT WebThreadImpl : public WebThreadBase {
     38  public:
     39   explicit WebThreadImpl(const char* name);
     40   virtual ~WebThreadImpl();
     41 
     42   virtual void postTask(Task* task);
     43   virtual void postDelayedTask(Task* task, long long delay_ms);
     44 
     45   virtual void enterRunLoop();
     46   virtual void exitRunLoop();
     47 
     48   base::MessageLoop* message_loop() const { return thread_->message_loop(); }
     49 
     50   virtual bool isCurrentThread() const OVERRIDE;
     51   virtual blink::PlatformThreadId threadId() const OVERRIDE;
     52 
     53  private:
     54   scoped_ptr<base::Thread> thread_;
     55 };
     56 
     57 class WebThreadImplForMessageLoop : public WebThreadBase {
     58  public:
     59   CONTENT_EXPORT explicit WebThreadImplForMessageLoop(
     60       base::MessageLoopProxy* message_loop);
     61   CONTENT_EXPORT virtual ~WebThreadImplForMessageLoop();
     62 
     63   virtual void postTask(Task* task) OVERRIDE;
     64   virtual void postDelayedTask(Task* task, long long delay_ms) OVERRIDE;
     65 
     66   virtual void enterRunLoop() OVERRIDE;
     67   virtual void exitRunLoop() OVERRIDE;
     68 
     69  private:
     70   virtual bool isCurrentThread() const OVERRIDE;
     71   virtual blink::PlatformThreadId threadId() const OVERRIDE;
     72 
     73   scoped_refptr<base::MessageLoopProxy> message_loop_;
     74   blink::PlatformThreadId thread_id_;
     75 };
     76 
     77 } // namespace content
     78 
     79 #endif  // CONTENT_CHILD_WEBTHREAD_IMPL_H_
     80