Home | History | Annotate | Download | only in shared_impl
      1 // Copyright (c) 2013 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 PPAPI_SHARED_IMPL_THREAD_AWARE_CALLBACK_H_
      6 #define PPAPI_SHARED_IMPL_THREAD_AWARE_CALLBACK_H_
      7 
      8 #include "base/basictypes.h"
      9 #include "base/bind.h"
     10 #include "base/memory/ref_counted.h"
     11 #include "ppapi/shared_impl/ppapi_shared_export.h"
     12 #include "ppapi/shared_impl/proxy_lock.h"
     13 
     14 namespace ppapi {
     15 
     16 class MessageLoopShared;
     17 
     18 namespace internal {
     19 
     20 class PPAPI_SHARED_EXPORT ThreadAwareCallbackBase {
     21  protected:
     22   ThreadAwareCallbackBase();
     23   ~ThreadAwareCallbackBase();
     24 
     25   static bool HasTargetLoop();
     26 
     27   void InternalRunOnTargetThread(const base::Closure& closure);
     28 
     29  private:
     30   class Core;
     31 
     32   scoped_refptr<MessageLoopShared> target_loop_;
     33   scoped_refptr<Core> core_;
     34 
     35   DISALLOW_COPY_AND_ASSIGN(ThreadAwareCallbackBase);
     36 };
     37 
     38 }  // namespace internal
     39 
     40 // Some PPB interfaces have methods that set a custom callback. Usually, the
     41 // callback has to be called on the same thread as the one it was set on.
     42 // ThreadAwareCallback keeps track of the target thread, and posts a task to run
     43 // on it if requested from a different thread.
     44 //
     45 // Please note that:
     46 // - Unlike TrackedCallback, there is no restriction on how many times the
     47 //   callback will be called.
     48 // - When a ThreadAwareCallback object is destroyed, all pending tasks to run
     49 //   the callback will be ignored. It is designed this way so that when the
     50 //   resource is destroyed or the callback is cancelled by the plugin, we can
     51 //   simply delete the ThreadAwareCallback object to prevent touching the
     52 //   callback later.
     53 // - When RunOnTargetThread() is called on the target thread, the callback runs
     54 //   immediately.
     55 template <class FuncType>
     56 class ThreadAwareCallback : public internal::ThreadAwareCallbackBase {
     57  public:
     58   // The caller takes ownership of the returned object.
     59   // NULL is returned if the current thread doesn't have an associated Pepper
     60   // message loop, or |func| is NULL.
     61   static ThreadAwareCallback* Create(FuncType func) {
     62     if (!func || !HasTargetLoop())
     63       return NULL;
     64     return new ThreadAwareCallback(func);
     65   }
     66 
     67   ~ThreadAwareCallback() {
     68   }
     69 
     70   void RunOnTargetThread() {
     71     InternalRunOnTargetThread(base::Bind(func_));
     72   }
     73 
     74   template <class P1>
     75   void RunOnTargetThread(const P1& p1) {
     76     InternalRunOnTargetThread(base::Bind(func_, p1));
     77   }
     78 
     79   template <class P1, class P2>
     80   void RunOnTargetThread(const P1& p1, const P2& p2) {
     81     InternalRunOnTargetThread(base::Bind(func_, p1, p2));
     82   }
     83 
     84   template <class P1, class P2, class P3>
     85   void RunOnTargetThread(const P1& p1, const P2& p2, const P3& p3) {
     86     InternalRunOnTargetThread(base::Bind(func_, p1, p2, p3));
     87   }
     88 
     89   template <class P1, class P2, class P3, class P4>
     90   void RunOnTargetThread(const P1& p1,
     91                          const P2& p2,
     92                          const P3& p3,
     93                          const P4& p4) {
     94     InternalRunOnTargetThread(base::Bind(func_, p1, p2, p3, p4));
     95   }
     96 
     97   template <class P1, class P2, class P3, class P4, class P5>
     98   void RunOnTargetThread(const P1& p1,
     99                          const P2& p2,
    100                          const P3& p3,
    101                          const P4& p4,
    102                          const P5& p5) {
    103     InternalRunOnTargetThread(base::Bind(func_, p1, p2, p3, p4, p5));
    104   }
    105 
    106  private:
    107   explicit ThreadAwareCallback(FuncType func) : func_(func) {
    108   }
    109 
    110   FuncType func_;
    111 };
    112 
    113 }  // namespace ppapi
    114 
    115 #endif  // PPAPI_SHARED_IMPL_THREAD_AWARE_CALLBACK_H_
    116