summaryrefslogtreecommitdiffstats
path: root/ppapi/shared_impl/thread_aware_callback.h
blob: b954eec8bd1193831fa604c747bb7ffd91247d36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef PPAPI_SHARED_IMPL_THREAD_AWARE_CALLBACK_H_
#define PPAPI_SHARED_IMPL_THREAD_AWARE_CALLBACK_H_

#include "base/basictypes.h"
#include "base/bind.h"
#include "base/memory/ref_counted.h"
#include "ppapi/shared_impl/ppapi_shared_export.h"
#include "ppapi/shared_impl/proxy_lock.h"

namespace ppapi {

class MessageLoopShared;

namespace internal {

class PPAPI_SHARED_EXPORT ThreadAwareCallbackBase {
 protected:
  ThreadAwareCallbackBase();
  ~ThreadAwareCallbackBase();

  static bool HasTargetLoop();

  void InternalRunOnTargetThread(const base::Closure& closure);

 private:
  class Core;

  scoped_refptr<MessageLoopShared> target_loop_;
  scoped_refptr<Core> core_;

  DISALLOW_COPY_AND_ASSIGN(ThreadAwareCallbackBase);
};

}  // namespace internal

// Some PPB interfaces have methods that set a custom callback. Usually, the
// callback has to be called on the same thread as the one it was set on.
// ThreadAwareCallback keeps track of the target thread, and posts a task to run
// on it if requested from a different thread.
//
// Please note that:
// - Unlike TrackedCallback, there is no restriction on how many times the
//   callback will be called.
// - When a ThreadAwareCallback object is destroyed, all pending tasks to run
//   the callback will be ignored. It is designed this way so that when the
//   resource is destroyed or the callback is cancelled by the plugin, we can
//   simply delete the ThreadAwareCallback object to prevent touching the
//   callback later.
// - When RunOnTargetThread() is called on the target thread, the callback runs
//   immediately.
template <class FuncType>
class ThreadAwareCallback : public internal::ThreadAwareCallbackBase {
 public:
  // The caller takes ownership of the returned object.
  // NULL is returned if the current thread doesn't have an associated Pepper
  // message loop, or |func| is NULL.
  static ThreadAwareCallback* Create(FuncType func) {
    if (!func || !HasTargetLoop())
      return NULL;
    return new ThreadAwareCallback(func);
  }

  ~ThreadAwareCallback() {
  }

  void RunOnTargetThread() {
    InternalRunOnTargetThread(base::Bind(func_));
  }

  template <class P1>
  void RunOnTargetThread(const P1& p1) {
    InternalRunOnTargetThread(base::Bind(func_, p1));
  }

  template <class P1, class P2>
  void RunOnTargetThread(const P1& p1, const P2& p2) {
    InternalRunOnTargetThread(base::Bind(func_, p1, p2));
  }

  template <class P1, class P2, class P3>
  void RunOnTargetThread(const P1& p1, const P2& p2, const P3& p3) {
    InternalRunOnTargetThread(base::Bind(func_, p1, p2, p3));
  }

  template <class P1, class P2, class P3, class P4>
  void RunOnTargetThread(const P1& p1,
                         const P2& p2,
                         const P3& p3,
                         const P4& p4) {
    InternalRunOnTargetThread(base::Bind(func_, p1, p2, p3, p4));
  }

  template <class P1, class P2, class P3, class P4, class P5>
  void RunOnTargetThread(const P1& p1,
                         const P2& p2,
                         const P3& p3,
                         const P4& p4,
                         const P5& p5) {
    InternalRunOnTargetThread(base::Bind(func_, p1, p2, p3, p4, p5));
  }

 private:
  explicit ThreadAwareCallback(FuncType func) : func_(func) {
  }

  FuncType func_;
};

}  // namespace ppapi

#endif  // PPAPI_SHARED_IMPL_THREAD_AWARE_CALLBACK_H_