OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef PPAPI_CPP_COMPLETION_CALLBACK_H_ | 5 #ifndef PPAPI_CPP_COMPLETION_CALLBACK_H_ |
6 #define PPAPI_CPP_COMPLETION_CALLBACK_H_ | 6 #define PPAPI_CPP_COMPLETION_CALLBACK_H_ |
7 | 7 |
8 #include "ppapi/c/pp_completion_callback.h" | 8 #include "ppapi/c/pp_completion_callback.h" |
9 #include "ppapi/c/pp_errors.h" | 9 #include "ppapi/c/pp_errors.h" |
10 #include "ppapi/cpp/logging.h" | 10 #include "ppapi/cpp/logging.h" |
11 #include "ppapi/cpp/module.h" | 11 #include "ppapi/cpp/module.h" |
| 12 #include "ppapi/cpp/output_traits.h" |
| 13 |
| 14 struct PP_ArrayOutput; |
12 | 15 |
13 /// @file | 16 /// @file |
14 /// This file defines the API to create and run a callback. | 17 /// This file defines the API to create and run a callback. |
15 namespace pp { | 18 namespace pp { |
16 | 19 |
| 20 template<typename T> class AsyncArrayOutputAdapter; |
| 21 template<typename T> class AsyncResourceArrayOutputAdapter; |
| 22 |
17 /// This API enables you to implement and receive callbacks when | 23 /// This API enables you to implement and receive callbacks when |
18 /// Pepper operations complete asynchronously. | 24 /// Pepper operations complete asynchronously. |
| 25 /// |
| 26 /// You can create these objects yourself, but it is most common to use the |
| 27 /// CompletionCallbackFactory to allow the callbacks to call class member |
| 28 /// functions. |
19 class CompletionCallback { | 29 class CompletionCallback { |
20 public: | 30 public: |
21 /// The default constructor will create a blocking | 31 /// The default constructor will create a blocking |
22 /// <code>CompletionCallback</code> that can be passed to a method to | 32 /// <code>CompletionCallback</code> that can be passed to a method to |
23 /// indicate that the calling thread should be blocked until the asynchronous | 33 /// indicate that the calling thread should be blocked until the asynchronous |
24 /// operation corresponding to the method completes. | 34 /// operation corresponding to the method completes. |
25 /// | 35 /// |
26 /// <strong>Note:</strong> Blocking completion callbacks are only allowed from | 36 /// <strong>Note:</strong> Blocking completion callbacks are only allowed from |
27 /// from background threads. | 37 /// from background threads. |
28 CompletionCallback() { | 38 CompletionCallback() { |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 if (result == PP_OK_COMPLETIONPENDING || IsOptional()) | 151 if (result == PP_OK_COMPLETIONPENDING || IsOptional()) |
142 return result; | 152 return result; |
143 Module::Get()->core()->CallOnMainThread(0, *this, result); | 153 Module::Get()->core()->CallOnMainThread(0, *this, result); |
144 return PP_OK_COMPLETIONPENDING; | 154 return PP_OK_COMPLETIONPENDING; |
145 } | 155 } |
146 | 156 |
147 protected: | 157 protected: |
148 PP_CompletionCallback cc_; | 158 PP_CompletionCallback cc_; |
149 }; | 159 }; |
150 | 160 |
| 161 /// A CompletionCallbackWithOutput defines a completion callback that |
| 162 /// additionally stores a pointer to some output data. Some C++ wrappers |
| 163 /// take a CompletionCallbackWithOutput when the browser is returning a |
| 164 /// bit of data as part of the function call. The "output" parameter |
| 165 /// stored in the CompletionCallbackWithOutput will receive the data from |
| 166 /// the browser. |
| 167 /// |
| 168 /// You can create this yourself, but it is most common to use with the |
| 169 /// CompletionCallbackFactory's NewCallbackWithOutput, which manages the |
| 170 /// storage for the output parameter for you and passes it as an argument |
| 171 /// to your callback function. |
| 172 /// |
| 173 /// Note that this class doesn't actually do anything with the output data, |
| 174 /// it just stores a pointer to it. C++ wrapper objects that accept a |
| 175 /// CompletionCallbackWithOutput will retrieve this pointer and pass it to |
| 176 /// the browser as the output parameter. |
| 177 template<typename T> |
| 178 class CompletionCallbackWithOutput : public CompletionCallback { |
| 179 public: |
| 180 /// The type that will actually be stored in the completion callback. In the |
| 181 /// common case, this will be equal to the template parameter (for example, |
| 182 /// CompletionCallbackWithOutput<int> would obviously take an int*. However, |
| 183 /// resources are passed as PP_Resource, vars as PP_Var, and arrays as our |
| 184 /// special ArrayOutputAdapter object. The CallbackOutputTraits defines |
| 185 /// specializations for all of these cases. |
| 186 typedef typename internal::CallbackOutputTraits<T>::StorageType |
| 187 OutputStorageType; |
| 188 |
| 189 /// The default constructor will create a blocking |
| 190 /// <code>CompletionCallback</code> that references the given output |
| 191 /// data. |
| 192 /// |
| 193 /// @param[in] output A pointer to the data associated with the callback. The |
| 194 /// caller must ensure that this pointer outlives the completion callback. |
| 195 /// |
| 196 /// <strong>Note:</strong> Blocking completion callbacks are only allowed from |
| 197 /// from background threads. |
| 198 CompletionCallbackWithOutput(OutputStorageType* output) |
| 199 : CompletionCallback(), |
| 200 output_(output) { |
| 201 } |
| 202 |
| 203 /// A constructor for creating a <code>CompletionCallback</code> that |
| 204 /// references the given output data. |
| 205 /// |
| 206 /// @param[in] user_data The user data to be passed to the callback function. |
| 207 /// This is optional and is typically used to help track state in case of |
| 208 /// multiple pending callbacks. |
| 209 /// |
| 210 /// @param[in] output A pointer to the data associated with the callback. The |
| 211 /// caller must ensure that this pointer outlives the completion callback. |
| 212 CompletionCallbackWithOutput(PP_CompletionCallback_Func func, |
| 213 void* user_data, |
| 214 OutputStorageType* output) |
| 215 : CompletionCallback(func, user_data), |
| 216 output_(output) { |
| 217 } |
| 218 |
| 219 /// A constructor for creating a <code>CompletionCallback</code> that |
| 220 /// references the given output data. |
| 221 /// |
| 222 /// @param[in] user_data The user data to be passed to the callback function. |
| 223 /// This is optional and is typically used to help track state in case of |
| 224 /// multiple pending callbacks. |
| 225 /// |
| 226 /// @param[in] flags Bit field combination of |
| 227 /// <code>PP_CompletionCallback_Flag</code> flags used to control how |
| 228 /// non-NULL callbacks are scheduled by asynchronous methods. |
| 229 /// |
| 230 /// @param[in] output A pointer to the data associated with the callback. The |
| 231 /// caller must ensure that this pointer outlives the completion callback. |
| 232 CompletionCallbackWithOutput(PP_CompletionCallback_Func func, |
| 233 void* user_data, |
| 234 int32_t flags, |
| 235 OutputStorageType* output) |
| 236 : CompletionCallback(func, user_data, flags), |
| 237 output_(output) { |
| 238 } |
| 239 |
| 240 OutputStorageType* output() const { return output_; } |
| 241 |
| 242 private: |
| 243 OutputStorageType* output_; |
| 244 }; |
| 245 |
151 /// BlockUntilComplete() is used in place of an actual completion callback | 246 /// BlockUntilComplete() is used in place of an actual completion callback |
152 /// to request blocking behavior. If specified, the calling thread will block | 247 /// to request blocking behavior. If specified, the calling thread will block |
153 /// until the function completes. Blocking completion callbacks are only | 248 /// until the function completes. Blocking completion callbacks are only |
154 /// allowed from background threads. | 249 /// allowed from background threads. |
155 /// | 250 /// |
156 /// @return A <code>CompletionCallback</code> corresponding to a NULL callback. | 251 /// @return A <code>CompletionCallback</code> corresponding to a NULL callback. |
157 CompletionCallback BlockUntilComplete(); | 252 CompletionCallback BlockUntilComplete(); |
158 | 253 |
159 } // namespace pp | 254 } // namespace pp |
160 | 255 |
161 #endif // PPAPI_CPP_COMPLETION_CALLBACK_H_ | 256 #endif // PPAPI_CPP_COMPLETION_CALLBACK_H_ |
OLD | NEW |