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

Unified Diff: src/trusted/weak_ref/call_on_main_thread.h

Issue 7216027: typesafe call-on-main-thread interface. no cast to/from void* required. (Closed) Base URL: svn://svn.chromium.org/native_client/trunk/src/native_client/
Patch Set: '' Created 9 years, 6 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 | « src/trusted/plugin/service_runtime.cc ('k') | src/trusted/weak_ref/weak_ref.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/trusted/weak_ref/call_on_main_thread.h
===================================================================
--- src/trusted/weak_ref/call_on_main_thread.h (revision 0)
+++ src/trusted/weak_ref/call_on_main_thread.h (revision 0)
@@ -0,0 +1,102 @@
+/*
+ * Copyright (c) 2011 The Native Client 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 NATIVE_CLIENT_SRC_TRUSTED_WEAK_REF_CALL_ON_MAIN_THREAD_H_
+#define NATIVE_CLIENT_SRC_TRUSTED_WEAK_REF_CALL_ON_MAIN_THREAD_H_
+
+#include "native_client/src/trusted/weak_ref/weak_ref.h"
+
+#include "native_client/src/include/nacl_scoped_ptr.h"
+
+#include "ppapi/c/pp_errors.h" // for PP_OK
+#include "ppapi/cpp/completion_callback.h" // for pp::CompletionCallback
+#include "ppapi/cpp/core.h" // for pp::
+#include "ppapi/cpp/module.h" // for pp::Module
+
+namespace plugin {
+
+// A typesafe utility to schedule a completion callback using weak
+// references. The callback function callback_fn is invoked
+// regardless of whether the anchor has been abandoned, since
+// callback_fn takes a WeakRef<R>* as argument. The intention is that
+// such callbacks, even deprived of any of its arguments (which has
+// been deleted), may wish to do some cleanup / log a message.
+template <typename R> bool WeakRefCallOnMainThread(
+ nacl::WeakRefAnchor* anchor,
+ int32_t delay_in_milliseconds,
+ void callback_fn(nacl::WeakRef<R>* weak_data, int32_t err),
+ R* raw_data) {
+ nacl::WeakRef<R>* wp = anchor->MakeWeakRef<R>(raw_data);
+ if (wp == NULL) {
+ return false;
+ }
+ pp::CompletionCallback cc(reinterpret_cast<void (*)(void*, int32_t)>(
+ callback_fn),
+ reinterpret_cast<void*>(wp));
+ pp::Module::Get()->core()->CallOnMainThread(
+ delay_in_milliseconds,
+ cc,
+ PP_OK);
+ return true;
+}
+
+template <typename R> class WeakRefAutoAbandonWrapper {
+ public:
+ WeakRefAutoAbandonWrapper(void (*callback_fn)(R* raw_data,
+ int32_t err),
+ R* raw_data)
+ : orig_callback_fn(callback_fn),
+ orig_data(raw_data) {}
+
+ void (*orig_callback_fn)(R* raw_data, int32_t err);
+ nacl::scoped_ptr<R> orig_data;
+};
+
+template <typename R> void WeakRefAutoAbandoner(
+ nacl::WeakRef<WeakRefAutoAbandonWrapper<R> >* wr,
+ int32_t err) {
+ nacl::scoped_ptr<WeakRefAutoAbandonWrapper<R> > p;
+ wr->ReleaseAndUnref(&p);
+ if (p == NULL) {
+ NaClLog(4, "WeakRefAutoAbandoner: weak ref NULL, anchor was abandoned\n");
+ return;
+ }
+ NaClLog(4, "WeakRefAutoAbandoner: weak ref okay, invoking callback\n");
+ (*p->orig_callback_fn)(p->orig_data.get(), err);
+ return;
+}
+
+// A typesafe utility to schedule a completion callback using weak
+// references. The callback function raw_callbac_fn takes an R* as
+// argument, and is not invoked if the anchor has been abandoned.
+template <typename R> bool WeakRefCallOnMainThread(
+ nacl::WeakRefAnchor* anchor,
+ int32_t delay_in_milliseconds,
+ void raw_callback_fn(R* raw_data, int32_t err),
+ R* raw_data) {
+
+ nacl::WeakRef<WeakRefAutoAbandonWrapper<R> >* wp =
+ anchor->MakeWeakRef<WeakRefAutoAbandonWrapper<R> >(
+ new WeakRefAutoAbandonWrapper<R>(raw_callback_fn, raw_data));
+ if (wp == NULL) {
+ return false;
+ }
+ void (*WeakRefAutoAbandonerPtr)(
+ nacl::WeakRef<WeakRefAutoAbandonWrapper<R> >* wr,
+ int32_t err) = WeakRefAutoAbandoner<R>;
+ pp::CompletionCallback cc(
+ reinterpret_cast<void (*)(void*, int32_t)>(WeakRefAutoAbandonerPtr),
+ reinterpret_cast<void*>(wp));
+ pp::Module::Get()->core()->CallOnMainThread(
+ delay_in_milliseconds,
+ cc,
+ PP_OK);
+ return true;
+}
+
+} // namespace plugin
+
+#endif
Property changes on: src/trusted/weak_ref/call_on_main_thread.h
___________________________________________________________________
Added: svn:eol-style
+ LF
« no previous file with comments | « src/trusted/plugin/service_runtime.cc ('k') | src/trusted/weak_ref/weak_ref.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698