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 signature that you implement to | 24 * This typedef defines the signature that you implement to |
| 25 * receive callbacks on asynchronous completion. | 25 * receive callbacks on asynchronous completion. |
| 26 * | |
| 27 * @param[in] user_data A pointer to user data passed to a callback function. | |
| 28 * @param[in] result The result parameter that, if negative or equal to 0, | |
| 29 * indicate if the call will completely asynchronously (the callback will be | |
| 30 * called with a status code). A value greater than zero indicates additional | |
| 31 * information such as bytes read. | |
|
dmichael (off chromium)
2011/06/27 15:45:41
I know this is not your wording here, but I don't
jond
2011/06/29 21:14:38
Polinia mentioned that -1 isn't really a an error,
| |
| 26 */ | 32 */ |
| 27 typedef void (*PP_CompletionCallback_Func)(void* user_data, int32_t result); | 33 typedef void (*PP_CompletionCallback_Func)(void* user_data, int32_t result); |
| 28 /** | 34 /** |
| 29 * @} | 35 * @} |
| 30 */ | 36 */ |
| 31 | 37 |
| 32 /** | 38 /** |
| 33 * @addtogroup Structs | 39 * @addtogroup Structs |
| 34 * @{ | 40 * @{ |
| 35 */ | 41 */ |
| 36 | 42 |
| 37 /** | 43 /** |
| 38 * Any method that takes a PP_CompletionCallback has the option of completing | 44 * Any method that takes a <code>PP_CompletionCallback</code> has the option of |
| 39 * asynchronously if the operation would block. Such a method should return | 45 * completing asynchronously if the operation would block. Such a method |
| 40 * PP_OK_COMPLETIONPENDING to indicate that the method will complete | 46 * should return <code>PP_OK_COMPLETIONPENDING</code> to indicate that the |
| 41 * asynchronously and will always be invoked from the main thread of PPAPI | 47 * method will complete asynchronously and will always be invoked from the main |
| 42 * execution. If the completion callback is NULL, then the operation will | 48 * thread of PPAPI execution. If the completion callback is NULL, then the |
| 43 * block if necessary to complete its work. PP_BlockUntilComplete() provides a | 49 * operation will block if necessary to complete its work. |
| 44 * convenient way to specify blocking behavior. Refer to PP_BlockUntilComplete | 50 * <code>PP_BlockUntilComplete()</code> provides a convenient way to specify |
| 45 * for more information. | 51 * blocking behavior. Refer to <code>PP_BlockUntilComplete</code> for more |
| 52 * information. | |
| 46 * | 53 * |
| 47 * The result parameter passes an int32_t that, if negative or equal to 0, | 54 * The result parameter passes an int32_t that, if negative or equal to 0, |
| 48 * indicate if the call will completely asynchronously (the callback will be | 55 * indicate if the call will complete asynchronously (the callback will be |
| 49 * called with a status code). A value greater than zero indicates additional | 56 * called with a status code). A value greater than zero indicates additional |
| 50 * information such as bytes read. | 57 * information such as bytes read. |
| 51 */ | 58 */ |
| 52 struct PP_CompletionCallback { | 59 struct PP_CompletionCallback { |
| 60 /** | |
| 61 * This value is a callback function that will be called. | |
| 62 */ | |
| 53 PP_CompletionCallback_Func func; | 63 PP_CompletionCallback_Func func; |
| 64 | |
| 65 /** | |
| 66 * This value is a pointer to user data passed to a callback function. | |
| 67 */ | |
| 54 void* user_data; | 68 void* user_data; |
| 55 }; | 69 }; |
| 56 /** | 70 /** |
| 57 * @} | 71 * @} |
| 58 */ | 72 */ |
| 59 | 73 |
| 60 /** | 74 /** |
| 61 * @addtogroup Functions | 75 * @addtogroup Functions |
| 62 * @{ | 76 * @{ |
| 63 */ | 77 */ |
| 64 | 78 |
| 65 /** | 79 /** |
| 66 * PP_MakeCompletionCallback() is used to create a PP_CompletionCallback. | 80 * This function is used to create a <code>PP_CompletionCallback</code>. |
|
dmichael (off chromium)
2011/06/27 15:45:41
I was fine with the old wording of 'PP_MakeComplet
jond
2011/06/29 21:14:38
Done.
jond
2011/06/29 21:14:38
Done.
| |
| 67 * | 81 * |
| 68 * @param[in] func A PP_CompletionCallback_Func that will be called. | 82 * @param[in] func A <code>PP_CompletionCallback_Func</code> that will be |
| 83 * called. | |
| 69 * @param[in] user_data A pointer to user data passed to your callback | 84 * @param[in] user_data A pointer to user data passed to your callback |
| 70 * function. This is optional and is typically used to help track state | 85 * function. This is optional and is typically used to help track state |
| 71 * when you may have multiple callbacks pending. | 86 * when you may have multiple callbacks pending. |
| 72 * @return A PP_CompletionCallback structure. | 87 * @return A <code>PP_CompletionCallback</code> structure. |
| 73 */ | 88 */ |
| 74 PP_INLINE struct PP_CompletionCallback PP_MakeCompletionCallback( | 89 PP_INLINE struct PP_CompletionCallback PP_MakeCompletionCallback( |
| 75 PP_CompletionCallback_Func func, | 90 PP_CompletionCallback_Func func, |
| 76 void* user_data) { | 91 void* user_data) { |
| 77 struct PP_CompletionCallback cc; | 92 struct PP_CompletionCallback cc; |
| 78 cc.func = func; | 93 cc.func = func; |
| 79 cc.user_data = user_data; | 94 cc.user_data = user_data; |
| 80 return cc; | 95 return cc; |
| 81 } | 96 } |
| 82 /** | 97 /** |
| 83 * @} | 98 * @} |
| 84 */ | 99 */ |
| 85 | 100 |
| 86 /** | 101 /** |
| 87 * @addtogroup Functions | 102 * @addtogroup Functions |
| 88 * @{ | 103 * @{ |
| 89 */ | 104 */ |
| 90 | 105 |
| 91 /** | 106 /** |
| 92 * PP_RunCompletionCallback() is used to run a callback. | 107 * This function is used to run a callback. |
| 93 * | 108 * |
| 94 * @param[in] cc A pointer to a PP_CompletionCallback that will be run. | 109 * @param[in] cc A pointer to a <code>PP_CompletionCallback</code> that will be |
| 110 * run. | |
| 95 * @param[in] res The result parameter that, if negative or equal to 0, | 111 * @param[in] res The result parameter that, if negative or equal to 0, |
| 96 * indicate if the call will completely asynchronously (the callback will be | 112 * indicate if the call will completely asynchronously (the callback will be |
| 97 * called with a status code). A value greater than zero indicates additional | 113 * called with a status code). A value greater than zero indicates additional |
| 98 * information such as bytes read. | 114 * information such as bytes read. |
| 99 */ | 115 */ |
| 100 PP_INLINE void PP_RunCompletionCallback(struct PP_CompletionCallback* cc, | 116 PP_INLINE void PP_RunCompletionCallback(struct PP_CompletionCallback* cc, |
| 101 int32_t res) { | 117 int32_t res) { |
| 102 cc->func(cc->user_data, res); | 118 cc->func(cc->user_data, res); |
| 103 } | 119 } |
| 104 | 120 |
| 105 /** | 121 /** |
| 106 * @} | 122 * @} |
| 107 */ | 123 */ |
| 108 | 124 |
| 109 /** | 125 /** |
| 110 * @addtogroup Functions | 126 * @addtogroup Functions |
| 111 * @{ | 127 * @{ |
| 112 */ | 128 */ |
| 113 | 129 |
| 114 /** | 130 /** |
| 115 * PP_BlockUntilComplete() is used in place of an actual completion callback | 131 * This function is used in place of an actual completion callback |
| 116 * to request blocking behavior. If specified, the calling thread will block | 132 * to request blocking behavior. If specified, the calling thread will block |
| 117 * until the function completes. Blocking completion callbacks are only usable | 133 * until the function completes. Blocking completion callbacks are only usable |
| 118 * from background threads. | 134 * from background threads. |
| 119 * | 135 * |
| 120 * @return A PP_CompletionCallback structure. | 136 * @return A <code>PP_CompletionCallback</code> structure. |
| 121 */ | 137 */ |
| 122 PP_INLINE struct PP_CompletionCallback PP_BlockUntilComplete() { | 138 PP_INLINE struct PP_CompletionCallback PP_BlockUntilComplete() { |
| 123 return PP_MakeCompletionCallback(NULL, NULL); | 139 return PP_MakeCompletionCallback(NULL, NULL); |
| 124 } | 140 } |
| 125 | 141 |
| 126 /** | 142 /** |
| 127 * Runs a callback and clears the reference to it. | 143 * This function runs a callback and clears the reference to that callback. |
| 128 * | 144 * |
| 129 * This is used when the null-ness of a completion callback is used as a signal | 145 * This function is used when the null-ness of a completion callback is used as |
| 130 * for whether a completion callback has been registered. In this case, after | 146 * a signal for whether a completion callback has been registered. In this |
| 131 * the execution of the callback, it should be cleared. | 147 * case, after the execution of the callback, it should be cleared. However, |
| 132 * | 148 * this introduces a conflict if the completion callback wants to schedule more |
| 133 * However, this introduces a conflict if the completion callback wants to | 149 * work that involves the same completion callback again (for example, when |
| 134 * schedule more work that involves the same completion callback again (for | 150 * reading data from an URLLoader, one would typically queue up |
| 135 * example, when reading data from an URLLoader, one would typically queue up | |
| 136 * another read callback). As a result, this function clears the pointer | 151 * another read callback). As a result, this function clears the pointer |
| 137 * *before* the given callback is executed. | 152 * before the provided callback is executed. |
| 138 */ | 153 */ |
| 139 PP_INLINE void PP_RunAndClearCompletionCallback( | 154 PP_INLINE void PP_RunAndClearCompletionCallback( |
| 140 struct PP_CompletionCallback* cc, | 155 struct PP_CompletionCallback* cc, |
| 141 int32_t res) { | 156 int32_t res) { |
| 142 struct PP_CompletionCallback temp = *cc; | 157 struct PP_CompletionCallback temp = *cc; |
| 143 *cc = PP_BlockUntilComplete(); | 158 *cc = PP_BlockUntilComplete(); |
| 144 PP_RunCompletionCallback(&temp, res); | 159 PP_RunCompletionCallback(&temp, res); |
| 145 } | 160 } |
| 146 /** | 161 /** |
| 147 * @} | 162 * @} |
| 148 */ | 163 */ |
| 149 | 164 |
| 150 #endif /* PPAPI_C_PP_COMPLETION_CALLBACK_H_ */ | 165 #endif /* PPAPI_C_PP_COMPLETION_CALLBACK_H_ */ |
| OLD | NEW |