Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(267)

Unified Diff: ppapi/shared_impl/thread_aware_callback.h

Issue 11859015: Pepper: Introduce ThreadAwareCallback. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Changes in response to David's suggestions Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..090e284c1e6b4951f7da8c1ecf65a8deb193b1f4
--- /dev/null
+++ b/ppapi/shared_impl/thread_aware_callback.h
@@ -0,0 +1,201 @@
+// 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();
+
+ 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 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() (defined in those sepcialized versions of
+// the template) is called on the target thread, the callback runs
+// immediately.
+template <class FuncType>
+class ThreadAwareCallback;
+
+template <>
+class ThreadAwareCallback<void (*)()>
+ : 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_);
+ }
+
+ 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_

Powered by Google App Engine
This is Rietveld 408576698