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

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: . 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
« no previous file with comments | « ppapi/shared_impl/test_globals.cc ('k') | ppapi/shared_impl/thread_aware_callback.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..b954eec8bd1193831fa604c747bb7ffd91247d36
--- /dev/null
+++ b/ppapi/shared_impl/thread_aware_callback.h
@@ -0,0 +1,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_
« no previous file with comments | « ppapi/shared_impl/test_globals.cc ('k') | ppapi/shared_impl/thread_aware_callback.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698