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
14namespace ppapi {
15
16class MessageLoopShared;
17
18namespace internal {
19
20class 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.
55template <class FuncType>
56class 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  void RunOnTargetThread() { InternalRunOnTargetThread(base::Bind(func_)); }
70
71  template <class P1>
72  void RunOnTargetThread(const P1& p1) {
73    InternalRunOnTargetThread(base::Bind(func_, p1));
74  }
75
76  template <class P1, class P2>
77  void RunOnTargetThread(const P1& p1, const P2& p2) {
78    InternalRunOnTargetThread(base::Bind(func_, p1, p2));
79  }
80
81  template <class P1, class P2, class P3>
82  void RunOnTargetThread(const P1& p1, const P2& p2, const P3& p3) {
83    InternalRunOnTargetThread(base::Bind(func_, p1, p2, p3));
84  }
85
86  template <class P1, class P2, class P3, class P4>
87  void RunOnTargetThread(const P1& p1,
88                         const P2& p2,
89                         const P3& p3,
90                         const P4& p4) {
91    InternalRunOnTargetThread(base::Bind(func_, p1, p2, p3, p4));
92  }
93
94  template <class P1, class P2, class P3, class P4, class P5>
95  void RunOnTargetThread(const P1& p1,
96                         const P2& p2,
97                         const P3& p3,
98                         const P4& p4,
99                         const P5& p5) {
100    InternalRunOnTargetThread(base::Bind(func_, p1, p2, p3, p4, p5));
101  }
102
103 private:
104  explicit ThreadAwareCallback(FuncType func) : func_(func) {}
105
106  FuncType func_;
107};
108
109}  // namespace ppapi
110
111#endif  // PPAPI_SHARED_IMPL_THREAD_AWARE_CALLBACK_H_
112