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 | 5 |
| 6 /** | 6 /** |
| 7 * This file defines the API to create and run a callback. | 7 * This file defines the API to create and run a callback. |
| 8 */ | 8 */ |
| 9 | 9 |
| 10 /** | 10 /** |
| 11 * PP_CompletionCallback_Func defines the function signature that you implement | 11 * This typedef defines the signature that you implement to receive callbacks |
| 12 * to receive callbacks on asynchronous completion of an operation. | 12 * on asynchronous completion of an operation. |
| 13 * | 13 * |
| 14 * |user_data| is a pointer to user-specified data associated with this | 14 * @param[in] user_data A pointer to user data passed to a callback function. |
| 15 * function at callback creation. See PP_MakeCompletionCallback() for details. | 15 * @param[in] result If result is 0 (PP_OK), the operation succeeded. Negative |
| 16 * | 16 * values (other than -1 or PP_OK_COMPLETE) indicate error and are specified |
| 17 * |result| is the result of the operation. Non-positive values correspond to | 17 * in pp_errors.h. Positive values for result usually indicate success and have |
| 18 * the error codes from pp_errors.h (excluding PP_OK_COMPLETIONPENDING). | 18 * some operation-dependent meaning (such as bytes read). |
| 19 * Positive values indicate additional information such as bytes read. | |
| 20 */ | 19 */ |
| 21 typedef void PP_CompletionCallback_Func([inout] mem_t user_data, | 20 typedef void PP_CompletionCallback_Func([inout] mem_t user_data, |
| 22 [in] int32_t result); | 21 [in] int32_t result); |
| 23 | 22 |
| 24 /** | 23 /** |
| 25 * This enumeration contains flags used to control how non-NULL callbacks are | 24 * This enumeration contains flags used to control how non-NULL callbacks are |
| 26 * scheduled by asynchronous methods. | 25 * scheduled by asynchronous methods. |
| 27 */ | 26 */ |
| 28 [assert_size(4)] | 27 [assert_size(4)] |
| 29 enum PP_CompletionCallback_Flag { | 28 enum PP_CompletionCallback_Flag { |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 45 * On synchronous method completion, the completion result will be returned | 44 * On synchronous method completion, the completion result will be returned |
| 46 * by the method itself. Otherwise, the method will return | 45 * by the method itself. Otherwise, the method will return |
| 47 * PP_OK_COMPLETIONPENDING, and the callback will be invoked asynchronously on | 46 * PP_OK_COMPLETIONPENDING, and the callback will be invoked asynchronously on |
| 48 * the main thread of PPAPI execution. | 47 * the main thread of PPAPI execution. |
| 49 */ | 48 */ |
| 50 PP_COMPLETIONCALLBACK_FLAG_OPTIONAL = 1 << 0 | 49 PP_COMPLETIONCALLBACK_FLAG_OPTIONAL = 1 << 0 |
| 51 }; | 50 }; |
| 52 | 51 |
| 53 | 52 |
| 54 /** | 53 /** |
| 55 * Any method that takes a PP_CompletionCallback can complete asynchronously. | 54 * Any method that takes a <code>PP_CompletionCallback</code> has the option of |
| 56 * Refer to PP_CompletionCallback_Flag for more information. | 55 * completing asynchronously if the operation would block. Such a method |
| 56 * should return <code>PP_OK_COMPLETIONPENDING</code> to indicate that the | |
| 57 * method will complete asynchronously and notify the caller and will always be | |
| 58 * invoked from the main thread of PPAPI execution. If the completion callback | |
| 59 * is NULL, then the operation will block if necessary to complete its work. | |
| 60 * <code>PP_BlockUntilComplete()</code> provides a convenient way to specify | |
| 61 * blocking behavior. Refer to <code>PP_BlockUntilComplete</code> for more | |
| 62 * information. | |
| 57 * | 63 * |
| 58 * If PP_CompletionCallback_Func is NULL, the operation might block if necessary | 64 * The result parameter passed to <code>func</code> is an int32_t that, if |
| 59 * to complete the work. Refer to PP_BlockUntilComplete for more information. | 65 * negative indicates an error code whose meaning is specific to the calling |
| 60 * | 66 * method (refer to <code>pp_error.h</code> for further information). A |
| 61 * See PP_MakeCompletionCallback() for the description of each field. | 67 * positive or 0 value is a return result whose meaning depends on |
|
der Springer
2011/08/11 19:20:51
Nit: "...a return result indicating success whose
jond
2011/08/11 20:49:44
Done.
| |
| 68 * the calling method (e.g. number of bytes read). | |
| 62 */ | 69 */ |
| 63 [passByValue] struct PP_CompletionCallback { | 70 [passByValue] struct PP_CompletionCallback { |
| 71 /** | |
| 72 * This value is a callback function that will be called. | |
| 73 */ | |
| 64 PP_CompletionCallback_Func func; | 74 PP_CompletionCallback_Func func; |
| 75 /** | |
| 76 * This value is a pointer to user data passed to a callback function. | |
| 77 */ | |
| 65 mem_t user_data; | 78 mem_t user_data; |
| 79 | |
| 80 /** | |
| 81 * Flags used to control how non-NULL callbacks are scheduled by | |
| 82 * asynchronous methods. | |
| 83 */ | |
| 66 int32_t flags; | 84 int32_t flags; |
| 67 }; | 85 }; |
| 68 | 86 |
| 69 #inline c | 87 #inline c |
| 70 #include <stdlib.h> | 88 #include <stdlib.h> |
| 71 | 89 |
| 72 /** | 90 /** |
| 73 * @addtogroup Functions | 91 * @addtogroup Functions |
| 74 * @{ | 92 * @{ |
| 75 */ | 93 */ |
| 76 /** | 94 /** |
| 77 * PP_MakeCompletionCallback() is used to create a PP_CompletionCallback | 95 * PP_MakeCompletionCallback() is used to create a |
| 78 * without flags. If you want to alter the default callback behavior, set the | 96 * <code>PP_CompletionCallback</code>. |
| 79 * flags to a bit field combination of PP_CompletionCallback_Flag's. | |
| 80 * | 97 * |
| 81 * Example: | 98 * <strong>Example:</strong> |
| 82 * struct PP_CompletionCallback cc = PP_MakeCompletionCallback(Foo, NULL); | |
| 83 * cc.flags = cc.flags | PP_COMPLETIONCALLBACK_FLAG_OPTIONAL; | |
| 84 * | 99 * |
| 85 * @param[in] func A PP_CompletionCallback_Func to be called on completion. | 100 * <code> |
| 86 * @param[in] user_data A pointer to user data passed to be passed to the | 101 * struct PP_CompletionCallback cc = PP_MakeCompletionCallback(Foo, NULL); |
| 87 * callback function. This is optional and is typically used to help track state | 102 * cc.flags = cc.flags | PP_COMPLETIONCALLBACK_FLAG_OPTIONAL; |
| 88 * in case of multiple pending callbacks. | 103 * </code> |
| 89 * | 104 * |
| 90 * @return A PP_CompletionCallback structure. | 105 * @param[in] func A <code>PP_CompletionCallback_Func</code> that will be |
| 106 * called. | |
| 107 * @param[in] user_data A pointer to user data passed to your callback | |
| 108 * function. This is optional and is typically used to help track state | |
| 109 * when you may have multiple callbacks pending. | |
| 110 * | |
| 111 * @return A <code>PP_CompletionCallback</code> structure. | |
| 91 */ | 112 */ |
| 92 PP_INLINE struct PP_CompletionCallback PP_MakeCompletionCallback( | 113 PP_INLINE struct PP_CompletionCallback PP_MakeCompletionCallback( |
| 93 PP_CompletionCallback_Func func, | 114 PP_CompletionCallback_Func func, |
| 94 void* user_data) { | 115 void* user_data) { |
| 95 struct PP_CompletionCallback cc; | 116 struct PP_CompletionCallback cc; |
| 96 cc.func = func; | 117 cc.func = func; |
| 97 cc.user_data = user_data; | 118 cc.user_data = user_data; |
| 98 cc.flags = PP_COMPLETIONCALLBACK_FLAG_NONE; | 119 cc.flags = PP_COMPLETIONCALLBACK_FLAG_NONE; |
| 99 return cc; | 120 return cc; |
| 100 } | 121 } |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 124 /** | 145 /** |
| 125 * @addtogroup Functions | 146 * @addtogroup Functions |
| 126 * @{ | 147 * @{ |
| 127 */ | 148 */ |
| 128 | 149 |
| 129 /** | 150 /** |
| 130 * PP_RunCompletionCallback() is used to run a callback. It invokes | 151 * PP_RunCompletionCallback() is used to run a callback. It invokes |
| 131 * the callback function passing it user data specified on creation and | 152 * the callback function passing it user data specified on creation and |
| 132 * completion |result|. | 153 * completion |result|. |
| 133 * | 154 * |
| 134 * @param[in] cc A pointer to a PP_CompletionCallback that will be run. | 155 * @param[in] cc A pointer to a <code>PP_CompletionCallback</code> that will be |
| 156 * run. | |
| 135 * @param[in] result The result of the operation. Non-positive values correspond | 157 * @param[in] result The result of the operation. Non-positive values correspond |
| 136 * to the error codes from pp_errors.h (excluding PP_OK_COMPLETIONPENDING). | 158 * to the error codes from pp_errors.h (excluding PP_OK_COMPLETIONPENDING). |
| 137 * Positive values indicate additional information such as bytes read. | 159 * Positive values indicate additional information such as bytes read. |
| 138 */ | 160 */ |
| 139 PP_INLINE void PP_RunCompletionCallback(struct PP_CompletionCallback* cc, | 161 PP_INLINE void PP_RunCompletionCallback(struct PP_CompletionCallback* cc, |
| 140 int32_t result) { | 162 int32_t result) { |
| 141 cc->func(cc->user_data, result); | 163 cc->func(cc->user_data, result); |
| 142 } | 164 } |
| 143 | 165 |
| 144 /** | 166 /** |
| 145 * @} | 167 * @} |
| 146 */ | 168 */ |
| 147 | 169 |
| 148 /** | 170 /** |
| 149 * @addtogroup Functions | 171 * @addtogroup Functions |
| 150 * @{ | 172 * @{ |
| 151 */ | 173 */ |
| 152 | 174 |
| 153 /** | 175 /** |
| 154 * PP_BlockUntilComplete() is used in place of an actual completion callback | 176 * PP_BlockUntilComplete() is used in place of an actual completion callback |
| 155 * to request blocking behavior. If specified, the calling thread will block | 177 * to request blocking behavior. If specified, the calling thread will block |
| 156 * until the function completes. Blocking completion callbacks are only allowed | 178 * until the function completes. Blocking completion callbacks are only allowed |
| 157 * from background threads. | 179 * from background threads. |
| 158 * | 180 * |
| 159 * @return A PP_CompletionCallback structure corresponding to a NULL callback. | 181 * @return A <code>PP_CompletionCallback</code> structure. |
| 160 */ | 182 */ |
| 161 PP_INLINE struct PP_CompletionCallback PP_BlockUntilComplete() { | 183 PP_INLINE struct PP_CompletionCallback PP_BlockUntilComplete() { |
| 162 return PP_MakeCompletionCallback(NULL, NULL); | 184 return PP_MakeCompletionCallback(NULL, NULL); |
| 163 } | 185 } |
| 164 | 186 |
| 165 /** | 187 /** |
| 166 * Runs a callback and clears the reference to it. | 188 * PP_RunAndClearCompletionCallback() runs a callback and clears the reference |
| 189 * to that callback. | |
| 167 * | 190 * |
| 168 * This is used when the null-ness of a completion callback is used as a signal | 191 * This function is used when the null-ness of a completion callback is used as |
| 169 * for whether a completion callback has been registered. In this case, after | 192 * a signal for whether a completion callback has been registered. In this |
| 170 * the execution of the callback, it should be cleared. | 193 * case, after the execution of the callback, it should be cleared. However, |
| 171 * | 194 * this introduces a conflict if the completion callback wants to schedule more |
| 172 * However, this introduces a conflict if the completion callback wants to | 195 * work that involves the same completion callback again (for example, when |
| 173 * schedule more work that involves the same completion callback again (for | 196 * reading data from an URLLoader, one would typically queue up another read |
| 174 * example, when reading data from an URLLoader, one would typically queue up | 197 * callback). As a result, this function clears the pointer |
| 175 * another read callback). As a result, this function clears the pointer | 198 * before the provided callback is executed. |
| 176 * *before* the given callback is executed. | |
| 177 */ | 199 */ |
| 178 PP_INLINE void PP_RunAndClearCompletionCallback( | 200 PP_INLINE void PP_RunAndClearCompletionCallback( |
| 179 struct PP_CompletionCallback* cc, | 201 struct PP_CompletionCallback* cc, |
| 180 int32_t res) { | 202 int32_t res) { |
| 181 struct PP_CompletionCallback temp = *cc; | 203 struct PP_CompletionCallback temp = *cc; |
| 182 *cc = PP_BlockUntilComplete(); | 204 *cc = PP_BlockUntilComplete(); |
| 183 PP_RunCompletionCallback(&temp, res); | 205 PP_RunCompletionCallback(&temp, res); |
| 184 } | 206 } |
| 185 /** | 207 /** |
| 186 * @} | 208 * @} |
| 187 */ | 209 */ |
| 188 | 210 |
| 189 #endinl | 211 #endinl |
| 190 | 212 |
| OLD | NEW |