Index: ppapi/shared_impl/thread_aware_callback.h |
diff --git a/ppapi/shared_impl/thread_aware_callback.h b/ppapi/shared_impl/thread_aware_callback.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..0495e0802f8d20939f38d48cad39e03a264a7dd6 |
--- /dev/null |
+++ b/ppapi/shared_impl/thread_aware_callback.h |
@@ -0,0 +1,198 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
dmichael (off chromium)
2013/01/15 22:43:52
nit: 2013 for new files
yzshen1
2013/01/16 18:55:59
Done. How time flies! :)
|
+// 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(); |
+ |
+ bool ShouldPostToTargetLoop(); |
+ void RunIfNotAborted(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 not restriction of how many times the |
dmichael (off chromium)
2013/01/15 22:43:52
nit: "not restriction of" -> "no restriction on"
yzshen1
2013/01/16 18:55:59
Done.
|
+// 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. |
+template <class FuncType> |
+class ThreadAwareCallback; |
dmichael (off chromium)
2013/01/15 22:43:52
It's worth noting that if called on the target thr
yzshen1
2013/01/16 18:55:59
Done. I added one more bullet point above.
On 201
|
+ |
+template <> |
+class ThreadAwareCallback<void (*)()> |
dmichael (off chromium)
2013/01/15 22:43:52
Do we really need all the specializations?
What a
yzshen1
2013/01/16 18:55:59
First, thanks a lot for writing the detailed comme
dmichael (off chromium)
2013/01/16 19:21:00
Bind is pretty cheap; I would worry more about rea
yzshen1
2013/01/16 22:37:36
I have made changes in the process of adding a Cre
|
+ : public internal::ThreadAwareCallbackBase { |
+ public: |
+ typedef void (*FuncType)(); |
+ |
+ explicit ThreadAwareCallback(FuncType func) : func_(func) { |
+ } |
+ ~ThreadAwareCallback() { |
+ } |
+ |
+ void RunOnTargetThread() { |
+ if (ShouldPostToTargetLoop()) |
+ RunIfNotAborted(base::Bind(func_)); |
+ else |
+ ppapi::CallWhileUnlocked<void>(func_); |
dmichael (off chromium)
2013/01/15 22:43:52
You shouldn't need to pass a template parameter; i
yzshen1
2013/01/16 18:55:59
For this one, it is probably okay. But for those w
dmichael (off chromium)
2013/01/16 19:21:00
Ah, I see. Presumably it fails for const-ref argum
yzshen1
2013/01/16 22:37:36
I think so, haven't looked careful enough to find
|
+ } |
+ |
+ private: |
+ FuncType func_; |
+}; |
+ |
+template <class P1> |
+class ThreadAwareCallback<void (*)(P1)> |
+ : public internal::ThreadAwareCallbackBase { |
+ public: |
+ typedef void (*FuncType)(P1); |
+ |
+ explicit ThreadAwareCallback(FuncType func) : func_(func) { |
+ } |
+ ~ThreadAwareCallback() { |
+ } |
+ |
+ void RunOnTargetThread(const P1& p1) { |
+ if (ShouldPostToTargetLoop()) |
+ RunIfNotAborted(base::Bind(func_, p1)); |
+ else |
+ ppapi::CallWhileUnlocked<void, P1>(func_, p1); |
+ } |
+ |
+ private: |
+ FuncType func_; |
+}; |
+ |
+template <class P1, class P2> |
+class ThreadAwareCallback<void (*)(P1, P2)> |
+ : public internal::ThreadAwareCallbackBase { |
+ public: |
+ typedef void (*FuncType)(P1, P2); |
+ |
+ explicit ThreadAwareCallback(FuncType func) : func_(func) { |
+ } |
+ ~ThreadAwareCallback() { |
+ } |
+ |
+ void RunOnTargetThread(const P1& p1, const P2& p2) { |
+ if (ShouldPostToTargetLoop()) |
+ RunIfNotAborted(base::Bind(func_, p1, p2)); |
+ else |
+ ppapi::CallWhileUnlocked<void, P1, P2>(func_, p1, p2); |
+ } |
+ |
+ private: |
+ FuncType func_; |
+}; |
+ |
+template <class P1, class P2, class P3> |
+class ThreadAwareCallback<void (*)(P1, P2, P3)> |
+ : public internal::ThreadAwareCallbackBase { |
+ public: |
+ typedef void (*FuncType)(P1, P2, P3); |
+ |
+ explicit ThreadAwareCallback(FuncType func) : func_(func) { |
+ } |
+ ~ThreadAwareCallback() { |
+ } |
+ |
+ void RunOnTargetThread(const P1& p1, const P2& p2, const P3& p3) { |
+ if (ShouldPostToTargetLoop()) |
+ RunIfNotAborted(base::Bind(func_, p1, p2, p3)); |
+ else |
+ ppapi::CallWhileUnlocked<void, P1, P2, P3>(func_, p1, p2, p3); |
+ } |
+ |
+ private: |
+ FuncType func_; |
+}; |
+ |
+template <class P1, class P2, class P3, class P4> |
+class ThreadAwareCallback<void (*)(P1, P2, P3, P4)> |
+ : public internal::ThreadAwareCallbackBase { |
+ public: |
+ typedef void (*FuncType)(P1, P2, P3, P4); |
+ |
+ explicit ThreadAwareCallback(FuncType func) : func_(func) { |
+ } |
+ ~ThreadAwareCallback() { |
+ } |
+ |
+ void RunOnTargetThread(const P1& p1, |
+ const P2& p2, |
+ const P3& p3, |
+ const P4& p4) { |
+ if (ShouldPostToTargetLoop()) |
+ RunIfNotAborted(base::Bind(func_, p1, p2, p3, p4)); |
+ else |
+ ppapi::CallWhileUnlocked<void, P1, P2, P3, P4>(func_, p1, p2, p3, p4); |
+ } |
+ |
+ private: |
+ FuncType func_; |
+}; |
+ |
+template <class P1, class P2, class P3, class P4, class P5> |
+class ThreadAwareCallback<void (*)(P1, P2, P3, P4, P5)> |
+ : public internal::ThreadAwareCallbackBase { |
+ public: |
+ typedef void (*FuncType)(P1, P2, P3, P4, P5); |
+ |
+ explicit ThreadAwareCallback(FuncType func) : func_(func) { |
+ } |
+ ~ThreadAwareCallback() { |
+ } |
+ |
+ void RunOnTargetThread(const P1& p1, |
+ const P2& p2, |
+ const P3& p3, |
+ const P4& p4, |
+ const P5& p5) { |
+ if (ShouldPostToTargetLoop()) { |
+ RunIfNotAborted(base::Bind(func_, p1, p2, p3, p4, p5)); |
+ } else { |
+ ppapi::CallWhileUnlocked<void, P1, P2, P3, P4, P5>(func_, p1, p2, p3, p4, |
+ p5); |
+ } |
+ } |
+ |
+ private: |
+ FuncType func_; |
+}; |
+ |
+} // namespace ppapi |
+ |
+#endif // PPAPI_SHARED_IMPL_THREAD_AWARE_CALLBACK_H_ |