OLD | NEW |
1 /* Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 /* Copyright (c) 2012 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 /* From pp_completion_callback.idl modified Wed Oct 5 14:06:02 2011. */ | 6 /* From pp_completion_callback.idl modified Wed Nov 7 11:20:18 2012. */ |
7 | 7 |
8 #ifndef PPAPI_C_PP_COMPLETION_CALLBACK_H_ | 8 #ifndef PPAPI_C_PP_COMPLETION_CALLBACK_H_ |
9 #define PPAPI_C_PP_COMPLETION_CALLBACK_H_ | 9 #define PPAPI_C_PP_COMPLETION_CALLBACK_H_ |
10 | 10 |
11 #include "ppapi/c/pp_macros.h" | 11 #include "ppapi/c/pp_macros.h" |
12 #include "ppapi/c/pp_stdint.h" | 12 #include "ppapi/c/pp_stdint.h" |
13 | 13 |
14 /** | 14 /** |
15 * @file | 15 * @file |
16 * This file defines the API to create and run a callback. | 16 * This file defines the API to create and run a callback. |
(...skipping 27 matching lines...) Expand all Loading... |
44 * This enumeration contains flags used to control how non-NULL callbacks are | 44 * This enumeration contains flags used to control how non-NULL callbacks are |
45 * scheduled by asynchronous methods. | 45 * scheduled by asynchronous methods. |
46 */ | 46 */ |
47 typedef enum { | 47 typedef enum { |
48 /** | 48 /** |
49 * By default any non-NULL callback will always invoked asynchronously, | 49 * By default any non-NULL callback will always invoked asynchronously, |
50 * on success or error, even if the operation could complete synchronously | 50 * on success or error, even if the operation could complete synchronously |
51 * without blocking. | 51 * without blocking. |
52 * | 52 * |
53 * The method taking such callback will always return PP_OK_COMPLETIONPENDING. | 53 * The method taking such callback will always return PP_OK_COMPLETIONPENDING. |
54 * The callback will be invoked on the main thread of PPAPI execution. | 54 * The callback will be invoked on the same thread on which the method was |
| 55 * invoked. |
| 56 * |
| 57 * NOTE: If the method taking the callback is invoked on a background |
| 58 * thread that has no valid PPB_MessageLoop resource attached, the system has |
| 59 * no way to run the callback on the correct thread. In this case, a log |
| 60 * message will be emitted and the plugin will be made to crash. |
55 */ | 61 */ |
56 PP_COMPLETIONCALLBACK_FLAG_NONE = 0 << 0, | 62 PP_COMPLETIONCALLBACK_FLAG_NONE = 0 << 0, |
57 /** | 63 /** |
58 * This flag allows any method taking such callback to complete synchronously | 64 * This flag allows any method taking such callback to complete synchronously |
59 * and not call the callback if the operation would not block. This is useful | 65 * and not call the callback if the operation would not block. This is useful |
60 * when performance is an issue, and the operation bandwidth should not be | 66 * when performance is an issue, and the operation bandwidth should not be |
61 * limited to the processing speed of the message loop. | 67 * limited to the processing speed of the message loop. |
62 * | 68 * |
63 * On synchronous method completion, the completion result will be returned | 69 * On synchronous method completion, the completion result will be returned |
64 * by the method itself. Otherwise, the method will return | 70 * by the method itself. Otherwise, the method will return |
65 * PP_OK_COMPLETIONPENDING, and the callback will be invoked asynchronously on | 71 * PP_OK_COMPLETIONPENDING, and the callback will be invoked asynchronously on |
66 * the main thread of PPAPI execution. | 72 * the same thread on which the method was invoked. If there is no valid |
| 73 * PPB_MessageLoop attached to that thread, and the callback would normally |
| 74 * run asynchronously, the invoked method will return |
| 75 * PP_ERROR_NO_MESSAGE_LOOP. |
67 */ | 76 */ |
68 PP_COMPLETIONCALLBACK_FLAG_OPTIONAL = 1 << 0 | 77 PP_COMPLETIONCALLBACK_FLAG_OPTIONAL = 1 << 0 |
69 } PP_CompletionCallback_Flag; | 78 } PP_CompletionCallback_Flag; |
70 PP_COMPILE_ASSERT_SIZE_IN_BYTES(PP_CompletionCallback_Flag, 4); | 79 PP_COMPILE_ASSERT_SIZE_IN_BYTES(PP_CompletionCallback_Flag, 4); |
71 /** | 80 /** |
72 * @} | 81 * @} |
73 */ | 82 */ |
74 | 83 |
75 /** | 84 /** |
76 * @addtogroup Structs | 85 * @addtogroup Structs |
77 * @{ | 86 * @{ |
78 */ | 87 */ |
79 /** | 88 /** |
80 * Any method that takes a <code>PP_CompletionCallback</code> has the option of | 89 * <code>PP_CompletionCallback</code> is a common mechanism for supporting |
81 * completing asynchronously if the operation would block. Such a method | 90 * potentially asynchronous calls in browser interfaces. Any method that takes a |
82 * should return <code>PP_OK_COMPLETIONPENDING</code> to indicate that the | 91 * <code>PP_CompletionCallback</code> can be used in one of three different |
83 * method will complete asynchronously and notify the caller and will always be | 92 * ways: |
84 * invoked from the main thread of PPAPI execution. If the completion callback | 93 * - Required: The callback will always be invoked asynchronously on the |
85 * is NULL, then the operation will block if necessary to complete its work. | 94 * thread where the associated PPB method was invoked. The method |
86 * <code>PP_BlockUntilComplete()</code> provides a convenient way to specify | 95 * will always return <code>PP_OK_COMPLETIONPENDING when a |
87 * blocking behavior. Refer to <code>PP_BlockUntilComplete</code> for more | 96 * required callback, and the callback will be invoked later |
88 * information. | 97 * (barring system or thread shutdown; see PPB_MessageLoop for |
| 98 * details). Required callbacks are the default. |
89 * | 99 * |
90 * The result parameter passed to <code>func</code> is an int32_t that, if | 100 * NOTE: If you use a required callback on a background thread, |
91 * negative indicates an error code whose meaning is specific to the calling | 101 * you must have created and attached a PPB_MessageLoop. |
92 * method (refer to <code>pp_error.h</code> for further information). A | 102 * Otherwise, the system can not run your callback on that thread, |
93 * positive or 0 value is a return result indicating success whose meaning | 103 * and will instead emit a log message and crash your plugin to |
94 * depends on the calling method (e.g. number of bytes read). | 104 * make the problem more obvious. |
| 105 * |
| 106 * - Optional: The callback may be invoked asynchronously, or the PPB method |
| 107 * may complete synchronously if it can do so without blocking. |
| 108 * If the method will complete asynchronously, it will return |
| 109 * PP_OK_COMPLETIONPENDING. Otherwise, it will complete |
| 110 * synchronously and return an appropriate code (see below for |
| 111 * more information on the return code). Optional callbacks are |
| 112 * generally more difficult to use correctly than Required |
| 113 * callbacks, but can provide better performance for some APIs |
| 114 * (especially APIs with buffered reads, such as PPB_URLLoader or |
| 115 * PPB_FileIO). |
| 116 * |
| 117 * NOTE: If you use an optional callback on a background thread, |
| 118 * and you have not created and attached a PPB_MessageLoop, then |
| 119 * the method you invoke will fail without running and return |
| 120 * PP_ERROR_NO_MESSAGE_LOOP. |
| 121 * |
| 122 * - Blocking: In this case, the callback's function pointer is NULL, and the |
| 123 * invoked method must complete synchronously. The method will |
| 124 * run to completion and return an appropriate code when finished |
| 125 * (see below for more information). Blocking completion |
| 126 * callbacks are only supported on background threads. |
| 127 * |
| 128 * <code>PP_BlockUntilComplete()</code> provides a convenient way |
| 129 * to specify blocking behavior. Refer to |
| 130 * <code>PP_BlockUntilComplete</code> for more information. |
| 131 * |
| 132 * When the callback is run asynchronously, the result parameter passed to |
| 133 * <code>func</code> is an int32_t that, if negative indicates an error code |
| 134 * whose meaning is specific to the calling method (refer to |
| 135 * <code>pp_error.h</code> for further information). A positive or 0 value is a |
| 136 * return result indicating success whose meaning depends on the calling method |
| 137 * (e.g. number of bytes read). |
95 */ | 138 */ |
96 struct PP_CompletionCallback { | 139 struct PP_CompletionCallback { |
97 /** | 140 /** |
98 * This value is a callback function that will be called. | 141 * This value is a callback function that will be called, or NULL if this is |
| 142 * a blocking completion callback. |
99 */ | 143 */ |
100 PP_CompletionCallback_Func func; | 144 PP_CompletionCallback_Func func; |
101 /** | 145 /** |
102 * This value is a pointer to user data passed to a callback function. | 146 * This value is a pointer to user data passed to a callback function. |
103 */ | 147 */ |
104 void* user_data; | 148 void* user_data; |
105 /** | 149 /** |
106 * Flags used to control how non-NULL callbacks are scheduled by | 150 * Flags used to control how non-NULL callbacks are scheduled by |
107 * asynchronous methods. | 151 * asynchronous methods. |
108 */ | 152 */ |
109 int32_t flags; | 153 int32_t flags; |
110 }; | 154 }; |
111 /** | 155 /** |
112 * @} | 156 * @} |
113 */ | 157 */ |
114 | 158 |
115 #include <stdlib.h> | 159 #include <stdlib.h> |
116 | 160 |
117 /** | 161 /** |
118 * @addtogroup Functions | 162 * @addtogroup Functions |
119 * @{ | 163 * @{ |
120 */ | 164 */ |
121 /** | 165 /** |
122 * PP_MakeCompletionCallback() is used to create a | 166 * PP_MakeCompletionCallback() is used to create a |
123 * <code>PP_CompletionCallback</code>. | 167 * <code>PP_CompletionCallback</code>. |
124 * | 168 * |
125 * <strong>Example:</strong> | 169 * <strong>Example, creating a Required callback:</strong> |
126 * | 170 * |
127 * <code> | 171 * <code> |
128 * struct PP_CompletionCallback cc = PP_MakeCompletionCallback(Foo, NULL); | 172 * struct PP_CompletionCallback cc = PP_MakeCompletionCallback(Foo, NULL); |
| 173 * </code> |
| 174 * |
| 175 * <strong>Example, creating an Optional callback:</strong> |
| 176 * |
| 177 * <code> |
| 178 * struct PP_CompletionCallback cc = PP_MakeCompletionCallback(Foo, NULL); |
129 * cc.flags = cc.flags | PP_COMPLETIONCALLBACK_FLAG_OPTIONAL; | 179 * cc.flags = cc.flags | PP_COMPLETIONCALLBACK_FLAG_OPTIONAL; |
130 * </code> | 180 * </code> |
131 * | 181 * |
132 * @param[in] func A <code>PP_CompletionCallback_Func</code> that will be | 182 * @param[in] func A <code>PP_CompletionCallback_Func</code> that will be |
133 * called. | 183 * called. |
134 * @param[in] user_data A pointer to user data passed to your callback | 184 * @param[in] user_data A pointer to user data passed to your callback |
135 * function. This is optional and is typically used to help track state | 185 * function. This is optional and is typically used to help track state |
136 * when you may have multiple callbacks pending. | 186 * when you may have multiple callbacks pending. |
137 * | 187 * |
138 * @return A <code>PP_CompletionCallback</code> structure. | 188 * @return A <code>PP_CompletionCallback</code> structure. |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 struct PP_CompletionCallback temp = *cc; | 280 struct PP_CompletionCallback temp = *cc; |
231 *cc = PP_BlockUntilComplete(); | 281 *cc = PP_BlockUntilComplete(); |
232 PP_RunCompletionCallback(&temp, res); | 282 PP_RunCompletionCallback(&temp, res); |
233 } | 283 } |
234 /** | 284 /** |
235 * @} | 285 * @} |
236 */ | 286 */ |
237 | 287 |
238 #endif /* PPAPI_C_PP_COMPLETION_CALLBACK_H_ */ | 288 #endif /* PPAPI_C_PP_COMPLETION_CALLBACK_H_ */ |
239 | 289 |
OLD | NEW |