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