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 | 5 |
6 /* From pp_completion_callback.idl modified Sat Jul 16 16:50:26 2011. */ | 6 /* From pp_completion_callback.idl modified Wed Aug 10 15:18:25 2011. */ |
7 | 7 |
8 #ifndef PPAPI_C_PP_COMPLETION_CALLBACK_H_ | 8 #ifndef PPAPI_C_PP_COMPLETION_CALLBACK_H_ |
9 #define PPAPI_C_PP_COMPLETION_CALLBACK_H_ | 9 #define PPAPI_C_PP_COMPLETION_CALLBACK_H_ |
10 | 10 |
11 #include "ppapi/c/pp_macros.h" | 11 #include "ppapi/c/pp_macros.h" |
12 #include "ppapi/c/pp_stdint.h" | 12 #include "ppapi/c/pp_stdint.h" |
13 | 13 |
14 /** | 14 /** |
15 * @file | 15 * @file |
16 * This file defines the API to create and run a callback. | 16 * This file defines the API to create and run a callback. |
17 */ | 17 */ |
18 | 18 |
19 | 19 |
20 /** | 20 /** |
21 * @addtogroup Typedefs | 21 * @addtogroup Typedefs |
22 * @{ | 22 * @{ |
23 */ | 23 */ |
24 /** | 24 /** |
25 * PP_CompletionCallback_Func defines the function signature that you implement | 25 * This typedef defines the signature that you implement to receive callbacks |
26 * to receive callbacks on asynchronous completion of an operation. | 26 * on asynchronous completion of an operation. |
27 * | 27 * |
28 * |user_data| is a pointer to user-specified data associated with this | 28 * @param[in] user_data A pointer to user data passed to a callback function. |
29 * function at callback creation. See PP_MakeCompletionCallback() for details. | 29 * @param[in] result If result is 0 (PP_OK), the operation succeeded. Negative |
30 * | 30 * values (other than -1 or PP_OK_COMPLETE) indicate error and are specified |
31 * |result| is the result of the operation. Non-positive values correspond to | 31 * in pp_errors.h. Positive values for result usually indicate success and have |
32 * the error codes from pp_errors.h (excluding PP_OK_COMPLETIONPENDING). | 32 * some operation-dependent meaning (such as bytes read). |
33 * Positive values indicate additional information such as bytes read. | |
34 */ | 33 */ |
35 typedef void (*PP_CompletionCallback_Func)(void* user_data, int32_t result); | 34 typedef void (*PP_CompletionCallback_Func)(void* user_data, int32_t result); |
36 /** | 35 /** |
37 * @} | 36 * @} |
38 */ | 37 */ |
39 | 38 |
40 /** | 39 /** |
41 * @addtogroup Enums | 40 * @addtogroup Enums |
42 * @{ | 41 * @{ |
43 */ | 42 */ |
(...skipping 27 matching lines...) Expand all Loading... |
71 PP_COMPILE_ASSERT_SIZE_IN_BYTES(PP_CompletionCallback_Flag, 4); | 70 PP_COMPILE_ASSERT_SIZE_IN_BYTES(PP_CompletionCallback_Flag, 4); |
72 /** | 71 /** |
73 * @} | 72 * @} |
74 */ | 73 */ |
75 | 74 |
76 /** | 75 /** |
77 * @addtogroup Structs | 76 * @addtogroup Structs |
78 * @{ | 77 * @{ |
79 */ | 78 */ |
80 /** | 79 /** |
81 * Any method that takes a PP_CompletionCallback can complete asynchronously. | 80 * Any method that takes a <code>PP_CompletionCallback</code> has the option of |
82 * Refer to PP_CompletionCallback_Flag for more information. | 81 * completing asynchronously if the operation would block. Such a method |
| 82 * should return <code>PP_OK_COMPLETIONPENDING</code> to indicate that the |
| 83 * method will complete asynchronously and notify the caller and will always be |
| 84 * invoked from the main thread of PPAPI execution. If the completion callback |
| 85 * is NULL, then the operation will block if necessary to complete its work. |
| 86 * <code>PP_BlockUntilComplete()</code> provides a convenient way to specify |
| 87 * blocking behavior. Refer to <code>PP_BlockUntilComplete</code> for more |
| 88 * information. |
83 * | 89 * |
84 * If PP_CompletionCallback_Func is NULL, the operation might block if necessary | 90 * The result parameter passed to <code>func</code> is an int32_t that, if |
85 * to complete the work. Refer to PP_BlockUntilComplete for more information. | 91 * negative or equal to 0, indicate if the call will complete asynchronously |
86 * | 92 * (the callback will be called with a status code). A value greater than zero |
87 * See PP_MakeCompletionCallback() for the description of each field. | 93 * indicates an error code. |
88 */ | 94 */ |
89 struct PP_CompletionCallback { | 95 struct PP_CompletionCallback { |
| 96 /** |
| 97 * This value is a callback function that will be called. |
| 98 */ |
90 PP_CompletionCallback_Func func; | 99 PP_CompletionCallback_Func func; |
| 100 /** |
| 101 * This value is a pointer to user data passed to a callback function. |
| 102 */ |
91 void* user_data; | 103 void* user_data; |
| 104 /** |
| 105 * Flags used to control how non-NULL callbacks are scheduled by |
| 106 * asynchronous methods. |
| 107 */ |
92 int32_t flags; | 108 int32_t flags; |
93 }; | 109 }; |
94 /** | 110 /** |
95 * @} | 111 * @} |
96 */ | 112 */ |
97 | 113 |
98 #include <stdlib.h> | 114 #include <stdlib.h> |
99 | 115 |
100 /** | 116 /** |
101 * @addtogroup Functions | 117 * @addtogroup Functions |
102 * @{ | 118 * @{ |
103 */ | 119 */ |
104 /** | 120 /** |
105 * PP_MakeCompletionCallback() is used to create a PP_CompletionCallback | 121 * PP_MakeCompletionCallback() is used to create a |
106 * without flags. If you want to alter the default callback behavior, set the | 122 * <code>PP_CompletionCallback</code>. |
107 * flags to a bit field combination of PP_CompletionCallback_Flag's. | |
108 * | 123 * |
109 * Example: | 124 * <strong>Example:</strong> |
110 * struct PP_CompletionCallback cc = PP_MakeCompletionCallback(Foo, NULL); | |
111 * cc.flags = cc.flags | PP_COMPLETIONCALLBACK_FLAG_OPTIONAL; | |
112 * | 125 * |
113 * @param[in] func A PP_CompletionCallback_Func to be called on completion. | 126 * <code> |
114 * @param[in] user_data A pointer to user data passed to be passed to the | 127 * struct PP_CompletionCallback cc = PP_MakeCompletionCallback(Foo, NULL); |
115 * callback function. This is optional and is typically used to help track state | 128 * cc.flags = cc.flags | PP_COMPLETIONCALLBACK_FLAG_OPTIONAL; |
116 * in case of multiple pending callbacks. | 129 * </code> |
117 * | 130 * |
118 * @return A PP_CompletionCallback structure. | 131 * @param[in] func A <code>PP_CompletionCallback_Func</code> that will be |
| 132 * called. |
| 133 * @param[in] user_data A pointer to user data passed to your callback |
| 134 * function. This is optional and is typically used to help track state |
| 135 * when you may have multiple callbacks pending. |
| 136 * |
| 137 * @return A <code>PP_CompletionCallback</code> structure. |
119 */ | 138 */ |
120 PP_INLINE struct PP_CompletionCallback PP_MakeCompletionCallback( | 139 PP_INLINE struct PP_CompletionCallback PP_MakeCompletionCallback( |
121 PP_CompletionCallback_Func func, | 140 PP_CompletionCallback_Func func, |
122 void* user_data) { | 141 void* user_data) { |
123 struct PP_CompletionCallback cc; | 142 struct PP_CompletionCallback cc; |
124 cc.func = func; | 143 cc.func = func; |
125 cc.user_data = user_data; | 144 cc.user_data = user_data; |
126 cc.flags = PP_COMPLETIONCALLBACK_FLAG_NONE; | 145 cc.flags = PP_COMPLETIONCALLBACK_FLAG_NONE; |
127 return cc; | 146 return cc; |
128 } | 147 } |
(...skipping 23 matching lines...) Expand all Loading... |
152 /** | 171 /** |
153 * @addtogroup Functions | 172 * @addtogroup Functions |
154 * @{ | 173 * @{ |
155 */ | 174 */ |
156 | 175 |
157 /** | 176 /** |
158 * PP_RunCompletionCallback() is used to run a callback. It invokes | 177 * PP_RunCompletionCallback() is used to run a callback. It invokes |
159 * the callback function passing it user data specified on creation and | 178 * the callback function passing it user data specified on creation and |
160 * completion |result|. | 179 * completion |result|. |
161 * | 180 * |
162 * @param[in] cc A pointer to a PP_CompletionCallback that will be run. | 181 * @param[in] cc A pointer to a <code>PP_CompletionCallback</code> that will be |
| 182 * run. |
163 * @param[in] result The result of the operation. Non-positive values correspond | 183 * @param[in] result The result of the operation. Non-positive values correspond |
164 * to the error codes from pp_errors.h (excluding PP_OK_COMPLETIONPENDING). | 184 * to the error codes from pp_errors.h (excluding PP_OK_COMPLETIONPENDING). |
165 * Positive values indicate additional information such as bytes read. | 185 * Positive values indicate additional information such as bytes read. |
166 */ | 186 */ |
167 PP_INLINE void PP_RunCompletionCallback(struct PP_CompletionCallback* cc, | 187 PP_INLINE void PP_RunCompletionCallback(struct PP_CompletionCallback* cc, |
168 int32_t result) { | 188 int32_t result) { |
169 cc->func(cc->user_data, result); | 189 cc->func(cc->user_data, result); |
170 } | 190 } |
171 | 191 |
172 /** | 192 /** |
173 * @} | 193 * @} |
174 */ | 194 */ |
175 | 195 |
176 /** | 196 /** |
177 * @addtogroup Functions | 197 * @addtogroup Functions |
178 * @{ | 198 * @{ |
179 */ | 199 */ |
180 | 200 |
181 /** | 201 /** |
182 * PP_BlockUntilComplete() is used in place of an actual completion callback | 202 * PP_BlockUntilComplete() is used in place of an actual completion callback |
183 * to request blocking behavior. If specified, the calling thread will block | 203 * to request blocking behavior. If specified, the calling thread will block |
184 * until the function completes. Blocking completion callbacks are only allowed | 204 * until the function completes. Blocking completion callbacks are only allowed |
185 * from background threads. | 205 * from background threads. |
186 * | 206 * |
187 * @return A PP_CompletionCallback structure corresponding to a NULL callback. | 207 * @return A <code>PP_CompletionCallback</code> structure. |
188 */ | 208 */ |
189 PP_INLINE struct PP_CompletionCallback PP_BlockUntilComplete() { | 209 PP_INLINE struct PP_CompletionCallback PP_BlockUntilComplete() { |
190 return PP_MakeCompletionCallback(NULL, NULL); | 210 return PP_MakeCompletionCallback(NULL, NULL); |
191 } | 211 } |
192 | 212 |
193 /** | 213 /** |
194 * Runs a callback and clears the reference to it. | 214 * PP_RunAndClearCompletionCallback() runs a callback and clears the reference |
| 215 * to that callback. |
195 * | 216 * |
196 * This is used when the null-ness of a completion callback is used as a signal | 217 * This function is used when the null-ness of a completion callback is used as |
197 * for whether a completion callback has been registered. In this case, after | 218 * a signal for whether a completion callback has been registered. In this |
198 * the execution of the callback, it should be cleared. | 219 * case, after the execution of the callback, it should be cleared. However, |
199 * | 220 * this introduces a conflict if the completion callback wants to schedule more |
200 * However, this introduces a conflict if the completion callback wants to | 221 * work that involves the same completion callback again (for example, when |
201 * schedule more work that involves the same completion callback again (for | 222 * reading data from an URLLoader, one would typically queue up another read |
202 * example, when reading data from an URLLoader, one would typically queue up | 223 * callback). As a result, this function clears the pointer |
203 * another read callback). As a result, this function clears the pointer | 224 * before the provided callback is executed. |
204 * *before* the given callback is executed. | |
205 */ | 225 */ |
206 PP_INLINE void PP_RunAndClearCompletionCallback( | 226 PP_INLINE void PP_RunAndClearCompletionCallback( |
207 struct PP_CompletionCallback* cc, | 227 struct PP_CompletionCallback* cc, |
208 int32_t res) { | 228 int32_t res) { |
209 struct PP_CompletionCallback temp = *cc; | 229 struct PP_CompletionCallback temp = *cc; |
210 *cc = PP_BlockUntilComplete(); | 230 *cc = PP_BlockUntilComplete(); |
211 PP_RunCompletionCallback(&temp, res); | 231 PP_RunCompletionCallback(&temp, res); |
212 } | 232 } |
213 /** | 233 /** |
214 * @} | 234 * @} |
215 */ | 235 */ |
216 | 236 |
217 #endif /* PPAPI_C_PP_COMPLETION_CALLBACK_H_ */ | 237 #endif /* PPAPI_C_PP_COMPLETION_CALLBACK_H_ */ |
| 238 |
OLD | NEW |