OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2011 The Native Client Authors. All rights reserved. |
| 3 * Use of this source code is governed by a BSD-style license that can be |
| 4 * found in the LICENSE file. |
| 5 */ |
| 6 |
| 7 #ifndef NATIVE_CLIENT_SRC_TRUSTED_WEAK_REF_CALL_ON_MAIN_THREAD_H_ |
| 8 #define NATIVE_CLIENT_SRC_TRUSTED_WEAK_REF_CALL_ON_MAIN_THREAD_H_ |
| 9 |
| 10 #include "native_client/src/trusted/weak_ref/weak_ref.h" |
| 11 |
| 12 #include "native_client/src/include/nacl_scoped_ptr.h" |
| 13 |
| 14 #include "ppapi/c/pp_errors.h" // for PP_OK |
| 15 #include "ppapi/cpp/completion_callback.h" // for pp::CompletionCallback |
| 16 #include "ppapi/cpp/core.h" // for pp:: |
| 17 #include "ppapi/cpp/module.h" // for pp::Module |
| 18 |
| 19 namespace plugin { |
| 20 |
| 21 // A typesafe utility to schedule a completion callback using weak |
| 22 // references. The callback function callback_fn is invoked |
| 23 // regardless of whether the anchor has been abandoned, since |
| 24 // callback_fn takes a WeakRef<R>* as argument. The intention is that |
| 25 // such callbacks, even deprived of any of its arguments (which has |
| 26 // been deleted), may wish to do some cleanup / log a message. |
| 27 template <typename R> bool WeakRefCallOnMainThread( |
| 28 nacl::WeakRefAnchor* anchor, |
| 29 int32_t delay_in_milliseconds, |
| 30 void callback_fn(nacl::WeakRef<R>* weak_data, int32_t err), |
| 31 R* raw_data) { |
| 32 nacl::WeakRef<R>* wp = anchor->MakeWeakRef<R>(raw_data); |
| 33 if (wp == NULL) { |
| 34 return false; |
| 35 } |
| 36 pp::CompletionCallback cc(reinterpret_cast<void (*)(void*, int32_t)>( |
| 37 callback_fn), |
| 38 reinterpret_cast<void*>(wp)); |
| 39 pp::Module::Get()->core()->CallOnMainThread( |
| 40 delay_in_milliseconds, |
| 41 cc, |
| 42 PP_OK); |
| 43 return true; |
| 44 } |
| 45 |
| 46 template <typename R> class WeakRefAutoAbandonWrapper { |
| 47 public: |
| 48 WeakRefAutoAbandonWrapper(void (*callback_fn)(R* raw_data, |
| 49 int32_t err), |
| 50 R* raw_data) |
| 51 : orig_callback_fn(callback_fn), |
| 52 orig_data(raw_data) {} |
| 53 |
| 54 void (*orig_callback_fn)(R* raw_data, int32_t err); |
| 55 nacl::scoped_ptr<R> orig_data; |
| 56 }; |
| 57 |
| 58 template <typename R> void WeakRefAutoAbandoner( |
| 59 nacl::WeakRef<WeakRefAutoAbandonWrapper<R> >* wr, |
| 60 int32_t err) { |
| 61 nacl::scoped_ptr<WeakRefAutoAbandonWrapper<R> > p; |
| 62 wr->ReleaseAndUnref(&p); |
| 63 if (p == NULL) { |
| 64 NaClLog(4, "WeakRefAutoAbandoner: weak ref NULL, anchor was abandoned\n"); |
| 65 return; |
| 66 } |
| 67 NaClLog(4, "WeakRefAutoAbandoner: weak ref okay, invoking callback\n"); |
| 68 (*p->orig_callback_fn)(p->orig_data.get(), err); |
| 69 return; |
| 70 } |
| 71 |
| 72 // A typesafe utility to schedule a completion callback using weak |
| 73 // references. The callback function raw_callbac_fn takes an R* as |
| 74 // argument, and is not invoked if the anchor has been abandoned. |
| 75 template <typename R> bool WeakRefCallOnMainThread( |
| 76 nacl::WeakRefAnchor* anchor, |
| 77 int32_t delay_in_milliseconds, |
| 78 void raw_callback_fn(R* raw_data, int32_t err), |
| 79 R* raw_data) { |
| 80 |
| 81 nacl::WeakRef<WeakRefAutoAbandonWrapper<R> >* wp = |
| 82 anchor->MakeWeakRef<WeakRefAutoAbandonWrapper<R> >( |
| 83 new WeakRefAutoAbandonWrapper<R>(raw_callback_fn, raw_data)); |
| 84 if (wp == NULL) { |
| 85 return false; |
| 86 } |
| 87 void (*WeakRefAutoAbandonerPtr)( |
| 88 nacl::WeakRef<WeakRefAutoAbandonWrapper<R> >* wr, |
| 89 int32_t err) = WeakRefAutoAbandoner<R>; |
| 90 pp::CompletionCallback cc( |
| 91 reinterpret_cast<void (*)(void*, int32_t)>(WeakRefAutoAbandonerPtr), |
| 92 reinterpret_cast<void*>(wp)); |
| 93 pp::Module::Get()->core()->CallOnMainThread( |
| 94 delay_in_milliseconds, |
| 95 cc, |
| 96 PP_OK); |
| 97 return true; |
| 98 } |
| 99 |
| 100 } // namespace plugin |
| 101 |
| 102 #endif |
OLD | NEW |