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 Thu Aug 11 14:45:09 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 indicates an error code whose meaning is specific to the calling |
86 * | 92 * method (refer to <code>pp_error.h</code> for further information). A |
87 * See PP_MakeCompletionCallback() for the description of each field. | 93 * positive or 0 value is a return result indicating success whose meaning |
| 94 * depends on the calling method (e.g. number of bytes read). |
88 */ | 95 */ |
89 struct PP_CompletionCallback { | 96 struct PP_CompletionCallback { |
| 97 /** |
| 98 * This value is a callback function that will be called. |
| 99 */ |
90 PP_CompletionCallback_Func func; | 100 PP_CompletionCallback_Func func; |
| 101 /** |
| 102 * This value is a pointer to user data passed to a callback function. |
| 103 */ |
91 void* user_data; | 104 void* user_data; |
| 105 /** |
| 106 * Flags used to control how non-NULL callbacks are scheduled by |
| 107 * asynchronous methods. |
| 108 */ |
92 int32_t flags; | 109 int32_t flags; |
93 }; | 110 }; |
94 /** | 111 /** |
95 * @} | 112 * @} |
96 */ | 113 */ |
97 | 114 |
98 #include <stdlib.h> | 115 #include <stdlib.h> |
99 | 116 |
100 /** | 117 /** |
101 * @addtogroup Functions | 118 * @addtogroup Functions |
102 * @{ | 119 * @{ |
103 */ | 120 */ |
104 /** | 121 /** |
105 * PP_MakeCompletionCallback() is used to create a PP_CompletionCallback | 122 * PP_MakeCompletionCallback() is used to create a |
106 * without flags. If you want to alter the default callback behavior, set the | 123 * <code>PP_CompletionCallback</code>. |
107 * flags to a bit field combination of PP_CompletionCallback_Flag's. | |
108 * | 124 * |
109 * Example: | 125 * <strong>Example:</strong> |
110 * struct PP_CompletionCallback cc = PP_MakeCompletionCallback(Foo, NULL); | |
111 * cc.flags = cc.flags | PP_COMPLETIONCALLBACK_FLAG_OPTIONAL; | |
112 * | 126 * |
113 * @param[in] func A PP_CompletionCallback_Func to be called on completion. | 127 * <code> |
114 * @param[in] user_data A pointer to user data passed to be passed to the | 128 * struct PP_CompletionCallback cc = PP_MakeCompletionCallback(Foo, NULL); |
115 * callback function. This is optional and is typically used to help track state | 129 * cc.flags = cc.flags | PP_COMPLETIONCALLBACK_FLAG_OPTIONAL; |
116 * in case of multiple pending callbacks. | 130 * </code> |
117 * | 131 * |
118 * @return A PP_CompletionCallback structure. | 132 * @param[in] func A <code>PP_CompletionCallback_Func</code> that will be |
| 133 * called. |
| 134 * @param[in] user_data A pointer to user data passed to your callback |
| 135 * function. This is optional and is typically used to help track state |
| 136 * when you may have multiple callbacks pending. |
| 137 * |
| 138 * @return A <code>PP_CompletionCallback</code> structure. |
119 */ | 139 */ |
120 PP_INLINE struct PP_CompletionCallback PP_MakeCompletionCallback( | 140 PP_INLINE struct PP_CompletionCallback PP_MakeCompletionCallback( |
121 PP_CompletionCallback_Func func, | 141 PP_CompletionCallback_Func func, |
122 void* user_data) { | 142 void* user_data) { |
123 struct PP_CompletionCallback cc; | 143 struct PP_CompletionCallback cc; |
124 cc.func = func; | 144 cc.func = func; |
125 cc.user_data = user_data; | 145 cc.user_data = user_data; |
126 cc.flags = PP_COMPLETIONCALLBACK_FLAG_NONE; | 146 cc.flags = PP_COMPLETIONCALLBACK_FLAG_NONE; |
127 return cc; | 147 return cc; |
128 } | 148 } |
(...skipping 23 matching lines...) Expand all Loading... |
152 /** | 172 /** |
153 * @addtogroup Functions | 173 * @addtogroup Functions |
154 * @{ | 174 * @{ |
155 */ | 175 */ |
156 | 176 |
157 /** | 177 /** |
158 * PP_RunCompletionCallback() is used to run a callback. It invokes | 178 * PP_RunCompletionCallback() is used to run a callback. It invokes |
159 * the callback function passing it user data specified on creation and | 179 * the callback function passing it user data specified on creation and |
160 * completion |result|. | 180 * completion |result|. |
161 * | 181 * |
162 * @param[in] cc A pointer to a PP_CompletionCallback that will be run. | 182 * @param[in] cc A pointer to a <code>PP_CompletionCallback</code> that will be |
| 183 * run. |
163 * @param[in] result The result of the operation. Non-positive values correspond | 184 * @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). | 185 * to the error codes from pp_errors.h (excluding PP_OK_COMPLETIONPENDING). |
165 * Positive values indicate additional information such as bytes read. | 186 * Positive values indicate additional information such as bytes read. |
166 */ | 187 */ |
167 PP_INLINE void PP_RunCompletionCallback(struct PP_CompletionCallback* cc, | 188 PP_INLINE void PP_RunCompletionCallback(struct PP_CompletionCallback* cc, |
168 int32_t result) { | 189 int32_t result) { |
169 cc->func(cc->user_data, result); | 190 cc->func(cc->user_data, result); |
170 } | 191 } |
171 | 192 |
172 /** | 193 /** |
173 * @} | 194 * @} |
174 */ | 195 */ |
175 | 196 |
176 /** | 197 /** |
177 * @addtogroup Functions | 198 * @addtogroup Functions |
178 * @{ | 199 * @{ |
179 */ | 200 */ |
180 | 201 |
181 /** | 202 /** |
182 * PP_BlockUntilComplete() is used in place of an actual completion callback | 203 * PP_BlockUntilComplete() is used in place of an actual completion callback |
183 * to request blocking behavior. If specified, the calling thread will block | 204 * to request blocking behavior. If specified, the calling thread will block |
184 * until the function completes. Blocking completion callbacks are only allowed | 205 * until the function completes. Blocking completion callbacks are only allowed |
185 * from background threads. | 206 * from background threads. |
186 * | 207 * |
187 * @return A PP_CompletionCallback structure corresponding to a NULL callback. | 208 * @return A <code>PP_CompletionCallback</code> structure. |
188 */ | 209 */ |
189 PP_INLINE struct PP_CompletionCallback PP_BlockUntilComplete() { | 210 PP_INLINE struct PP_CompletionCallback PP_BlockUntilComplete() { |
190 return PP_MakeCompletionCallback(NULL, NULL); | 211 return PP_MakeCompletionCallback(NULL, NULL); |
191 } | 212 } |
192 | 213 |
193 /** | 214 /** |
194 * Runs a callback and clears the reference to it. | 215 * PP_RunAndClearCompletionCallback() runs a callback and clears the reference |
| 216 * to that callback. |
195 * | 217 * |
196 * This is used when the null-ness of a completion callback is used as a signal | 218 * 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 | 219 * a signal for whether a completion callback has been registered. In this |
198 * the execution of the callback, it should be cleared. | 220 * case, after the execution of the callback, it should be cleared. However, |
199 * | 221 * this introduces a conflict if the completion callback wants to schedule more |
200 * However, this introduces a conflict if the completion callback wants to | 222 * work that involves the same completion callback again (for example, when |
201 * schedule more work that involves the same completion callback again (for | 223 * 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 | 224 * callback). As a result, this function clears the pointer |
203 * another read callback). As a result, this function clears the pointer | 225 * before the provided callback is executed. |
204 * *before* the given callback is executed. | |
205 */ | 226 */ |
206 PP_INLINE void PP_RunAndClearCompletionCallback( | 227 PP_INLINE void PP_RunAndClearCompletionCallback( |
207 struct PP_CompletionCallback* cc, | 228 struct PP_CompletionCallback* cc, |
208 int32_t res) { | 229 int32_t res) { |
209 struct PP_CompletionCallback temp = *cc; | 230 struct PP_CompletionCallback temp = *cc; |
210 *cc = PP_BlockUntilComplete(); | 231 *cc = PP_BlockUntilComplete(); |
211 PP_RunCompletionCallback(&temp, res); | 232 PP_RunCompletionCallback(&temp, res); |
212 } | 233 } |
213 /** | 234 /** |
214 * @} | 235 * @} |
215 */ | 236 */ |
216 | 237 |
217 #endif /* PPAPI_C_PP_COMPLETION_CALLBACK_H_ */ | 238 #endif /* PPAPI_C_PP_COMPLETION_CALLBACK_H_ */ |
| 239 |
OLD | NEW |