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 * PP_CompletionCallback_Func defines the function signature that you implement |
| 25 * receive callbacks on asynchronous completion. | 25 * to receive callbacks on asynchronous completion of an operation. |
| 26 * | |
| 27 * |user_data| is a pointer to user-specified data associated with this | |
| 28 * function at callback creation. See PP_MakeCompletionCallback() for details. | |
| 29 * | |
| 30 * |result| is the result of the operation. Non-positive values correspond to | |
| 31 * the error codes from pp_errors.h (excluding PP_OK_COMPLETIONPENDING). | |
| 32 * Positive values indicate additional information such as bytes read. | |
| 26 */ | 33 */ |
| 27 typedef void (*PP_CompletionCallback_Func)(void* user_data, int32_t result); | 34 typedef void (*PP_CompletionCallback_Func)(void* user_data, int32_t result); |
| 28 /** | 35 /** |
| 29 * @} | 36 * @} |
| 30 */ | 37 */ |
| 31 | 38 |
| 32 /** | 39 /** |
| 40 * | |
| 41 * @addtogroup Enums | |
| 42 * @{ | |
| 43 */ | |
| 44 | |
| 45 /** | |
| 46 * This enumeration contains flags used to control how non-NULL callbacks are | |
| 47 * scheduled by asynchronous methods. | |
| 48 */ | |
| 49 typedef enum { | |
| 50 /** | |
| 51 * By default, any non-NULL callback will always be invoked asynchronously, | |
| 52 * on success or error, even if the operation could complete synchronously | |
| 53 * without blocking. | |
| 54 * | |
| 55 * The method taking such callback will always return PP_OK_COMPLETIONPENDING. | |
| 56 * The callback will be invoked on the main thread of PPAPI execution. | |
| 57 */ | |
| 58 PP_COMPLETIONCALLBACK_FLAG_NONE = 0 << 0, | |
| 59 /** | |
| 60 * This flag allows any method taking such callback to complete synchronously | |
| 61 * and not call the callback if the operation would not block. This is useful | |
| 62 * when performance is an issue, and the operation bandwidth should not be | |
| 63 * limited to the processing speed of the message loop. | |
| 64 * | |
| 65 * On synchronous method completion, the completion result will be returned | |
| 66 * by the method itself. Otherwise, the method will return | |
| 67 * PP_OK_COMPLETIONPENDING, and the callback will be invoked asynchronously on | |
| 68 * the main thread of PPAPI execution. | |
| 69 */ | |
| 70 PP_COMPLETIONCALLBACK_FLAG_OPTIONAL = 1 << 0 | |
| 71 } PP_CompletionCallback_Flag; | |
| 72 PP_COMPILE_ASSERT_SIZE_IN_BYTES(PP_CompletionCallback_Flag, 4); | |
| 73 | |
| 74 | |
| 75 /** | |
| 33 * @addtogroup Structs | 76 * @addtogroup Structs |
| 34 * @{ | 77 * @{ |
| 35 */ | 78 */ |
| 36 | 79 |
| 37 /** | 80 /* Any method that takes a PP_CompletionCallback can complete asynchronously. |
| 38 * Any method that takes a PP_CompletionCallback has the option of completing | 81 * Refer to PP_CompletionCallback_Flag for more information. |
| 39 * asynchronously if the operation would block. Such a method should return | |
| 40 * PP_OK_COMPLETIONPENDING to indicate that the method will complete | |
| 41 * asynchronously and will always be invoked from the main thread of PPAPI | |
| 42 * execution. If the completion callback is NULL, then the operation will | |
| 43 * block if necessary to complete its work. PP_BlockUntilComplete() provides a | |
| 44 * convenient way to specify blocking behavior. Refer to PP_BlockUntilComplete | |
| 45 * for more information. | |
| 46 * | 82 * |
| 47 * The result parameter passes an int32_t that, if negative or equal to 0, | 83 * If PP_CompletionCallback_Func is NULL, the operation might block if necessary |
| 48 * indicate if the call will completely asynchronously (the callback will be | 84 * to complete the work. Refer to PP_BlockUntilComplete for more information. |
| 49 * called with a status code). A value greater than zero indicates additional | 85 * |
| 50 * information such as bytes read. | 86 * See PP_MakeCompletionCallback() for the description of each field. |
| 51 */ | 87 */ |
| 52 struct PP_CompletionCallback { | 88 struct PP_CompletionCallback { |
| 53 PP_CompletionCallback_Func func; | 89 PP_CompletionCallback_Func func; |
| 54 void* user_data; | 90 void* user_data; |
| 91 int32_t flags; | |
|
piman
2011/06/07 17:32:14
Because you're changing the size (and semantics) o
polina
2011/06/09 23:53:51
Done.
| |
| 55 }; | 92 }; |
| 56 /** | 93 /** |
| 57 * @} | 94 * @} |
| 58 */ | 95 */ |
| 59 | 96 |
| 60 /** | 97 /** |
| 61 * @addtogroup Functions | 98 * @addtogroup Functions |
| 62 * @{ | 99 * @{ |
| 63 */ | 100 */ |
| 64 | |
| 65 /** | 101 /** |
| 66 * PP_MakeCompletionCallback() is used to create a PP_CompletionCallback. | 102 * PP_MakeCompletionCallback() is used to create a PP_CompletionCallback |
| 103 * without flags. If you want to alter the default callback behavior, set the | |
| 104 * flags to a bit field combination of PP_CompletionCallback_Flag's. | |
| 67 * | 105 * |
| 68 * @param[in] func A PP_CompletionCallback_Func that will be called. | 106 * Example: |
| 69 * @param[in] user_data A pointer to user data passed to your callback | 107 * struct PP_CompletionCallback cc = PP_MakeCompletionCallback(Foo, NULL); |
| 70 * function. This is optional and is typically used to help track state | 108 * cc.flags = cc.flags | PP_COMPLETIONCALLBACK_FLAG_OPTIONAL; |
| 71 * when you may have multiple callbacks pending. | 109 * |
| 110 * @param[in] func A PP_CompletionCallback_Func to be called on completion. | |
| 111 * @param[in] user_data A pointer to user data passed to be passed to the | |
| 112 * callback function. This is optional and is typically used to help track state | |
| 113 * in case of multiple pending callbacks. | |
| 114 * | |
| 72 * @return A PP_CompletionCallback structure. | 115 * @return A PP_CompletionCallback structure. |
| 73 */ | 116 */ |
| 74 PP_INLINE struct PP_CompletionCallback PP_MakeCompletionCallback( | 117 PP_INLINE struct PP_CompletionCallback PP_MakeCompletionCallback( |
| 75 PP_CompletionCallback_Func func, | 118 PP_CompletionCallback_Func func, |
| 76 void* user_data) { | 119 void* user_data) { |
| 77 struct PP_CompletionCallback cc; | 120 struct PP_CompletionCallback cc; |
| 78 cc.func = func; | 121 cc.func = func; |
| 79 cc.user_data = user_data; | 122 cc.user_data = user_data; |
| 123 cc.flags = PP_COMPLETIONCALLBACK_FLAG_NONE; | |
|
piman
2011/06/07 17:32:14
I thought we were not going to change the default
polina
2011/06/09 23:53:51
You have it backwards. The idea was to have the ne
piman
2011/06/10 00:51:20
But that breaks existing plugins.... Can we do a 2
polina
2011/06/10 19:33:19
This should not break any existing plugins that pr
| |
| 80 return cc; | 124 return cc; |
| 81 } | 125 } |
| 82 /** | 126 /** |
| 83 * @} | 127 * @} |
| 84 */ | 128 */ |
| 85 | 129 |
| 86 /** | 130 /** |
| 87 * @addtogroup Functions | 131 * @addtogroup Functions |
| 88 * @{ | 132 * @{ |
| 89 */ | 133 */ |
| 90 | 134 |
| 91 /** | 135 /** |
| 92 * PP_RunCompletionCallback() is used to run a callback. | 136 * PP_RunCompletionCallback() is used to run a callback. It invokes |
| 137 * the callback function passing it user data specified on creation and | |
| 138 * completion |result|. | |
| 93 * | 139 * |
| 94 * @param[in] cc A pointer to a PP_CompletionCallback that will be run. | 140 * @param[in] cc A pointer to a PP_CompletionCallback that will be run. |
| 95 * @param[in] res The result parameter that, if negative or equal to 0, | 141 * @param[in] result The result of the operation. Non-positive values correspond |
| 96 * indicate if the call will completely asynchronously (the callback will be | 142 * to the error codes from pp_errors.h (excluding PP_OK_COMPLETIONPENDING). |
| 97 * called with a status code). A value greater than zero indicates additional | 143 * Positive values indicate additional information such as bytes read. |
| 98 * information such as bytes read. | |
| 99 */ | 144 */ |
| 100 PP_INLINE void PP_RunCompletionCallback(struct PP_CompletionCallback* cc, | 145 PP_INLINE void PP_RunCompletionCallback(struct PP_CompletionCallback* cc, |
| 101 int32_t res) { | 146 int32_t result) { |
| 102 cc->func(cc->user_data, res); | 147 cc->func(cc->user_data, result); |
| 103 } | 148 } |
| 104 | 149 |
| 105 /** | 150 /** |
| 106 * @} | 151 * @} |
| 107 */ | 152 */ |
| 108 | 153 |
| 109 /** | 154 /** |
| 110 * @addtogroup Functions | 155 * @addtogroup Functions |
| 111 * @{ | 156 * @{ |
| 112 */ | 157 */ |
| 113 | 158 |
| 114 /** | 159 /** |
| 115 * PP_BlockUntilComplete() is used in place of an actual completion callback | 160 * PP_BlockUntilComplete() is used in place of an actual completion callback |
| 116 * to request blocking behavior. If specified, the calling thread will block | 161 * to request blocking behavior. If specified, the calling thread will block |
| 117 * until the function completes. Blocking completion callbacks are only usable | 162 * until the function completes. Blocking completion callbacks are only allowed |
| 118 * from background threads. | 163 * from background threads. |
| 119 * | 164 * |
| 120 * @return A PP_CompletionCallback structure. | 165 * @return A PP_CompletionCallback structure corresponding to a NULL callback. |
| 121 */ | 166 */ |
| 122 PP_INLINE struct PP_CompletionCallback PP_BlockUntilComplete() { | 167 PP_INLINE struct PP_CompletionCallback PP_BlockUntilComplete() { |
| 123 return PP_MakeCompletionCallback(NULL, NULL); | 168 return PP_MakeCompletionCallback(NULL, NULL); |
| 124 } | 169 } |
| 125 | 170 |
| 126 /** | 171 /** |
| 127 * Runs a callback and clears the reference to it. | 172 * Runs a callback and clears the reference to it. |
| 128 * | 173 * |
| 129 * This is used when the null-ness of a completion callback is used as a signal | 174 * This is used when the null-ness of a completion callback is used as a signal |
| 130 * for whether a completion callback has been registered. In this case, after | 175 * for whether a completion callback has been registered. In this case, after |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 141 int32_t res) { | 186 int32_t res) { |
| 142 struct PP_CompletionCallback temp = *cc; | 187 struct PP_CompletionCallback temp = *cc; |
| 143 *cc = PP_BlockUntilComplete(); | 188 *cc = PP_BlockUntilComplete(); |
| 144 PP_RunCompletionCallback(&temp, res); | 189 PP_RunCompletionCallback(&temp, res); |
| 145 } | 190 } |
| 146 /** | 191 /** |
| 147 * @} | 192 * @} |
| 148 */ | 193 */ |
| 149 | 194 |
| 150 #endif /* PPAPI_C_PP_COMPLETION_CALLBACK_H_ */ | 195 #endif /* PPAPI_C_PP_COMPLETION_CALLBACK_H_ */ |
| OLD | NEW |