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 If result is 0 (PP_OK), the operation succeeded. Negative |
| 29 * values (other than -1) indicate error and are specified in pp_errors.h. |
| 30 * Positive values for result usually indicate success and have some |
| 31 * operation-dependent meaning (such as bytes read). |
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 * PP_MakeCompletionCallback() is used to create a |
| 81 * <code>PP_CompletionCallback</code>. |
67 * | 82 * |
68 * @param[in] func A PP_CompletionCallback_Func that will be called. | 83 * @param[in] func A <code>PP_CompletionCallback_Func</code> that will be |
| 84 * called. |
69 * @param[in] user_data A pointer to user data passed to your callback | 85 * @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 | 86 * function. This is optional and is typically used to help track state |
71 * when you may have multiple callbacks pending. | 87 * when you may have multiple callbacks pending. |
72 * @return A PP_CompletionCallback structure. | 88 * @return A <code>PP_CompletionCallback</code> structure. |
73 */ | 89 */ |
74 PP_INLINE struct PP_CompletionCallback PP_MakeCompletionCallback( | 90 PP_INLINE struct PP_CompletionCallback PP_MakeCompletionCallback( |
75 PP_CompletionCallback_Func func, | 91 PP_CompletionCallback_Func func, |
76 void* user_data) { | 92 void* user_data) { |
77 struct PP_CompletionCallback cc; | 93 struct PP_CompletionCallback cc; |
78 cc.func = func; | 94 cc.func = func; |
79 cc.user_data = user_data; | 95 cc.user_data = user_data; |
80 return cc; | 96 return cc; |
81 } | 97 } |
82 /** | 98 /** |
83 * @} | 99 * @} |
84 */ | 100 */ |
85 | 101 |
86 /** | 102 /** |
87 * @addtogroup Functions | 103 * @addtogroup Functions |
88 * @{ | 104 * @{ |
89 */ | 105 */ |
90 | 106 |
91 /** | 107 /** |
92 * PP_RunCompletionCallback() is used to run a callback. | 108 * PP_RunCompletionCallback() is used to run a callback. |
93 * | 109 * |
94 * @param[in] cc A pointer to a PP_CompletionCallback that will be run. | 110 * @param[in] cc A pointer to a <code>PP_CompletionCallback</code> that will be |
| 111 * run. |
95 * @param[in] res The result parameter that, if negative or equal to 0, | 112 * @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 | 113 * indicate if the call will completely asynchronously (the callback will be |
97 * called with a status code). A value greater than zero indicates additional | 114 * called with a status code). A value greater than zero indicates additional |
98 * information such as bytes read. | 115 * information such as bytes read. |
99 */ | 116 */ |
100 PP_INLINE void PP_RunCompletionCallback(struct PP_CompletionCallback* cc, | 117 PP_INLINE void PP_RunCompletionCallback(struct PP_CompletionCallback* cc, |
101 int32_t res) { | 118 int32_t res) { |
102 cc->func(cc->user_data, res); | 119 cc->func(cc->user_data, res); |
103 } | 120 } |
104 | 121 |
105 /** | 122 /** |
106 * @} | 123 * @} |
107 */ | 124 */ |
108 | 125 |
109 /** | 126 /** |
110 * @addtogroup Functions | 127 * @addtogroup Functions |
111 * @{ | 128 * @{ |
112 */ | 129 */ |
113 | 130 |
114 /** | 131 /** |
115 * PP_BlockUntilComplete() is used in place of an actual completion callback | 132 * PP_BlockUntilComplete() is used in place of an actual completion callback |
116 * to request blocking behavior. If specified, the calling thread will block | 133 * to request blocking behavior. If specified, the calling thread will block |
117 * until the function completes. Blocking completion callbacks are only usable | 134 * until the function completes. Blocking completion callbacks are only usable |
118 * from background threads. | 135 * from background threads. |
119 * | 136 * |
120 * @return A PP_CompletionCallback structure. | 137 * @return A <code>PP_CompletionCallback</code> structure. |
121 */ | 138 */ |
122 PP_INLINE struct PP_CompletionCallback PP_BlockUntilComplete() { | 139 PP_INLINE struct PP_CompletionCallback PP_BlockUntilComplete() { |
123 return PP_MakeCompletionCallback(NULL, NULL); | 140 return PP_MakeCompletionCallback(NULL, NULL); |
124 } | 141 } |
125 | 142 |
126 /** | 143 /** |
127 * Runs a callback and clears the reference to it. | 144 * PP_RunAndClearCompletionCallback() runs a callback and clears the reference |
| 145 * to that callback. |
128 * | 146 * |
129 * This is used when the null-ness of a completion callback is used as a signal | 147 * 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 | 148 * a signal for whether a completion callback has been registered. In this |
131 * the execution of the callback, it should be cleared. | 149 * case, after the execution of the callback, it should be cleared. However, |
132 * | 150 * this introduces a conflict if the completion callback wants to schedule more |
133 * However, this introduces a conflict if the completion callback wants to | 151 * work that involves the same completion callback again (for example, when |
134 * schedule more work that involves the same completion callback again (for | 152 * 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 | 153 * another read callback). As a result, this function clears the pointer |
137 * *before* the given callback is executed. | 154 * before the provided callback is executed. |
138 */ | 155 */ |
139 PP_INLINE void PP_RunAndClearCompletionCallback( | 156 PP_INLINE void PP_RunAndClearCompletionCallback( |
140 struct PP_CompletionCallback* cc, | 157 struct PP_CompletionCallback* cc, |
141 int32_t res) { | 158 int32_t res) { |
142 struct PP_CompletionCallback temp = *cc; | 159 struct PP_CompletionCallback temp = *cc; |
143 *cc = PP_BlockUntilComplete(); | 160 *cc = PP_BlockUntilComplete(); |
144 PP_RunCompletionCallback(&temp, res); | 161 PP_RunCompletionCallback(&temp, res); |
145 } | 162 } |
146 /** | 163 /** |
147 * @} | 164 * @} |
148 */ | 165 */ |
149 | 166 |
150 #endif /* PPAPI_C_PP_COMPLETION_CALLBACK_H_ */ | 167 #endif /* PPAPI_C_PP_COMPLETION_CALLBACK_H_ */ |
OLD | NEW |