OLD | NEW |
1 /* -*- c++ -*- */ | 1 /* -*- c++ -*- */ |
2 /* | 2 /* |
3 * Copyright (c) 2011 The Chromium Authors. All rights reserved. | 3 * Copyright (c) 2011 The Chromium Authors. All rights reserved. |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 // EXAMPLE USAGE | 8 // EXAMPLE USAGE |
9 // | 9 // |
10 // class PluginReverseInterface { | 10 // class PluginReverseInterface { |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 public: | 158 public: |
159 WeakRefMemberFuncBinder(E* object, | 159 WeakRefMemberFuncBinder(E* object, |
160 void (E::*raw_callback_fn)(R* raw_data, | 160 void (E::*raw_callback_fn)(R* raw_data, |
161 int32_t err), | 161 int32_t err), |
162 R* raw_data) | 162 R* raw_data) |
163 : object_(object), | 163 : object_(object), |
164 raw_callback_fn_(raw_callback_fn), | 164 raw_callback_fn_(raw_callback_fn), |
165 data_(raw_data) {} | 165 data_(raw_data) {} |
166 void Invoke(int32_t err) { | 166 void Invoke(int32_t err) { |
167 NaClLog2(kPpWeakRefModuleName, 4, | 167 NaClLog2(kPpWeakRefModuleName, 4, |
168 ("WeakRefMemberFuncBinder: Invoke obj 0x%"NACL_PRIxPTR | 168 ("WeakRefMemberFuncBinder: Invoke obj 0x%" NACL_PRIxPTR |
169 ", err%"NACL_PRId32"\n"), | 169 ", err%" NACL_PRId32 "\n"), |
170 reinterpret_cast<uintptr_t>(object_), err); | 170 reinterpret_cast<uintptr_t>(object_), err); |
171 (object_->*raw_callback_fn_)(data_.get(), err); | 171 (object_->*raw_callback_fn_)(data_.get(), err); |
172 NaClLog2(kPpWeakRefModuleName, 4, | 172 NaClLog2(kPpWeakRefModuleName, 4, |
173 "WeakRefMemberFuncBinder: done\n"); | 173 "WeakRefMemberFuncBinder: done\n"); |
174 } | 174 } |
175 private: | 175 private: |
176 E* object_; | 176 E* object_; |
177 void (E::*raw_callback_fn_)(R* raw_data, int32_t err); | 177 void (E::*raw_callback_fn_)(R* raw_data, int32_t err); |
178 nacl::scoped_ptr<R> data_; | 178 nacl::scoped_ptr<R> data_; |
179 }; | 179 }; |
180 | 180 |
181 template <typename R, typename E> | 181 template <typename R, typename E> |
182 void WeakRefMemberFuncInvoker( | 182 void WeakRefMemberFuncInvoker( |
183 WeakRefMemberFuncBinder<R, E> *binder, int32_t err) { | 183 WeakRefMemberFuncBinder<R, E> *binder, int32_t err) { |
184 NaClLog2(kPpWeakRefModuleName, 4, | 184 NaClLog2(kPpWeakRefModuleName, 4, |
185 "WeakRefMemberFuncInvoker: %"NACL_PRIxPTR" %"NACL_PRId32"\n", | 185 "WeakRefMemberFuncInvoker: %" NACL_PRIxPTR " %" NACL_PRId32 "\n", |
186 (uintptr_t) binder, | 186 (uintptr_t) binder, |
187 err); | 187 err); |
188 binder->Invoke(err); | 188 binder->Invoke(err); |
189 // delete binder not needed, since WeakRefAutoAbandoner holds binder | 189 // delete binder not needed, since WeakRefAutoAbandoner holds binder |
190 // in a scoped_ptr and will automatically delete on scope exit. | 190 // in a scoped_ptr and will automatically delete on scope exit. |
191 } | 191 } |
192 | 192 |
193 | 193 |
194 // A typesafe utility to schedule a completion callback using weak | 194 // A typesafe utility to schedule a completion callback using weak |
195 // references, where the callback function is a member function. The | 195 // references, where the callback function is a member function. The |
196 // member function must take only a raw argument data pointer and a | 196 // member function must take only a raw argument data pointer and a |
197 // completion status as formal parameters. The lifetime of the | 197 // completion status as formal parameters. The lifetime of the |
198 // |object| and |raw_callback_fn| must be at least that of |anchor|. | 198 // |object| and |raw_callback_fn| must be at least that of |anchor|. |
199 // Typically |object| is just the object that controls the |anchor|, | 199 // Typically |object| is just the object that controls the |anchor|, |
200 // though it may be some sub-object that is contained within the | 200 // though it may be some sub-object that is contained within the |
201 // actual controlling object. If the |anchor| is abandoned, the | 201 // actual controlling object. If the |anchor| is abandoned, the |
202 // |raw_data| argument is deleted and the |raw_callback_fn| will not | 202 // |raw_data| argument is deleted and the |raw_callback_fn| will not |
203 // be invoked. | 203 // be invoked. |
204 template <typename R, typename E> | 204 template <typename R, typename E> |
205 pp::CompletionCallback WeakRefNewCallback( | 205 pp::CompletionCallback WeakRefNewCallback( |
206 nacl::WeakRefAnchor* anchor, | 206 nacl::WeakRefAnchor* anchor, |
207 E* object, | 207 E* object, |
208 void (E::*raw_callback_fn)(R* raw_data, int32_t err), | 208 void (E::*raw_callback_fn)(R* raw_data, int32_t err), |
209 R* raw_data) { | 209 R* raw_data) { |
210 NaClLog2(kPpWeakRefModuleName, 4, | 210 NaClLog2(kPpWeakRefModuleName, 4, |
211 "Entered WeakRefNewCallback\n"); | 211 "Entered WeakRefNewCallback\n"); |
212 NaClLog2(kPpWeakRefModuleName, 4, | 212 NaClLog2(kPpWeakRefModuleName, 4, |
213 "object 0x%"NACL_PRIxPTR"\n", | 213 "object 0x%" NACL_PRIxPTR "\n", |
214 reinterpret_cast<uintptr_t>(object)); | 214 reinterpret_cast<uintptr_t>(object)); |
215 WeakRefMemberFuncBinder<R, E>* binder = | 215 WeakRefMemberFuncBinder<R, E>* binder = |
216 new WeakRefMemberFuncBinder<R, E>(object, | 216 new WeakRefMemberFuncBinder<R, E>(object, |
217 raw_callback_fn, | 217 raw_callback_fn, |
218 raw_data); | 218 raw_data); |
219 CHECK(binder != NULL); | 219 CHECK(binder != NULL); |
220 NaClLog2(kPpWeakRefModuleName, 4, | 220 NaClLog2(kPpWeakRefModuleName, 4, |
221 "WeakRefNewCallback: binder %"NACL_PRIxPTR"\n", | 221 "WeakRefNewCallback: binder %" NACL_PRIxPTR "\n", |
222 (uintptr_t) binder); | 222 (uintptr_t) binder); |
223 void (*weak_ref_member_func_invoker_ptr)( | 223 void (*weak_ref_member_func_invoker_ptr)( |
224 WeakRefMemberFuncBinder<R, E>* binder, | 224 WeakRefMemberFuncBinder<R, E>* binder, |
225 int32_t err) = WeakRefMemberFuncInvoker<R, E>; | 225 int32_t err) = WeakRefMemberFuncInvoker<R, E>; |
226 return WeakRefNewCallback(anchor, weak_ref_member_func_invoker_ptr, | 226 return WeakRefNewCallback(anchor, weak_ref_member_func_invoker_ptr, |
227 binder); | 227 binder); |
228 } | 228 } |
229 | 229 |
230 template <typename R, typename E> void WeakRefCallOnMainThread( | 230 template <typename R, typename E> void WeakRefCallOnMainThread( |
231 nacl::WeakRefAnchor* anchor, | 231 nacl::WeakRefAnchor* anchor, |
(...skipping 11 matching lines...) Expand all Loading... |
243 delay_in_milliseconds, | 243 delay_in_milliseconds, |
244 cc, | 244 cc, |
245 PP_OK); | 245 PP_OK); |
246 NaClLog2(kPpWeakRefModuleName, 4, | 246 NaClLog2(kPpWeakRefModuleName, 4, |
247 "WeakRefCallOnMainThread: invoked PP_CallOnMainThread\n"); | 247 "WeakRefCallOnMainThread: invoked PP_CallOnMainThread\n"); |
248 } | 248 } |
249 | 249 |
250 } // namespace plugin | 250 } // namespace plugin |
251 | 251 |
252 #endif | 252 #endif |
OLD | NEW |