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 /** | 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 /** |
(...skipping 14 matching lines...) Expand all Loading... |
25 * scheduled by asynchronous methods. | 25 * scheduled by asynchronous methods. |
26 */ | 26 */ |
27 [assert_size(4)] | 27 [assert_size(4)] |
28 enum PP_CompletionCallback_Flag { | 28 enum PP_CompletionCallback_Flag { |
29 /** | 29 /** |
30 * By default any non-NULL callback will always invoked asynchronously, | 30 * By default any non-NULL callback will always invoked asynchronously, |
31 * on success or error, even if the operation could complete synchronously | 31 * on success or error, even if the operation could complete synchronously |
32 * without blocking. | 32 * without blocking. |
33 * | 33 * |
34 * The method taking such callback will always return PP_OK_COMPLETIONPENDING. | 34 * The method taking such callback will always return PP_OK_COMPLETIONPENDING. |
35 * The callback will be invoked on the main thread of PPAPI execution. | 35 * The callback will be invoked on the same thread on which the method was |
| 36 * invoked. |
| 37 * |
| 38 * NOTE: If the method taking the callback is invoked on a background |
| 39 * thread that has no valid PPB_MessageLoop resource attached, the system has |
| 40 * no way to run the callback on the correct thread. In this case, a log |
| 41 * message will be emitted and the plugin will be made to crash. |
36 */ | 42 */ |
37 PP_COMPLETIONCALLBACK_FLAG_NONE = 0 << 0, | 43 PP_COMPLETIONCALLBACK_FLAG_NONE = 0 << 0, |
38 /** | 44 /** |
39 * This flag allows any method taking such callback to complete synchronously | 45 * This flag allows any method taking such callback to complete synchronously |
40 * and not call the callback if the operation would not block. This is useful | 46 * and not call the callback if the operation would not block. This is useful |
41 * when performance is an issue, and the operation bandwidth should not be | 47 * when performance is an issue, and the operation bandwidth should not be |
42 * limited to the processing speed of the message loop. | 48 * limited to the processing speed of the message loop. |
43 * | 49 * |
44 * On synchronous method completion, the completion result will be returned | 50 * On synchronous method completion, the completion result will be returned |
45 * by the method itself. Otherwise, the method will return | 51 * by the method itself. Otherwise, the method will return |
46 * PP_OK_COMPLETIONPENDING, and the callback will be invoked asynchronously on | 52 * PP_OK_COMPLETIONPENDING, and the callback will be invoked asynchronously on |
47 * the main thread of PPAPI execution. | 53 * the same thread on which the method was invoked. If there is no valid |
| 54 * PPB_MessageLoop attached to that thread, and the callback would normally |
| 55 * run asynchronously, the invoked method will return |
| 56 * PP_ERROR_NO_MESSAGE_LOOP. |
48 */ | 57 */ |
49 PP_COMPLETIONCALLBACK_FLAG_OPTIONAL = 1 << 0 | 58 PP_COMPLETIONCALLBACK_FLAG_OPTIONAL = 1 << 0 |
50 }; | 59 }; |
51 | 60 |
52 | 61 |
53 /** | 62 /** |
54 * Any method that takes a <code>PP_CompletionCallback</code> has the option of | 63 * <code>PP_CompletionCallback</code> is a common mechanism for supporting |
55 * completing asynchronously if the operation would block. Such a method | 64 * potentially asynchronous calls in browser interfaces. Any method that takes a |
56 * should return <code>PP_OK_COMPLETIONPENDING</code> to indicate that the | 65 * <code>PP_CompletionCallback</code> can be used in one of three different |
57 * method will complete asynchronously and notify the caller and will always be | 66 * ways: |
58 * invoked from the main thread of PPAPI execution. If the completion callback | 67 * - Required: The callback will always be invoked asynchronously on the |
59 * is NULL, then the operation will block if necessary to complete its work. | 68 * thread where the associated PPB method was invoked. The method |
60 * <code>PP_BlockUntilComplete()</code> provides a convenient way to specify | 69 * will always return <code>PP_OK_COMPLETIONPENDING when a |
61 * blocking behavior. Refer to <code>PP_BlockUntilComplete</code> for more | 70 * required callback, and the callback will be invoked later |
62 * information. | 71 * (barring system or thread shutdown; see PPB_MessageLoop for |
| 72 * details). Required callbacks are the default. |
63 * | 73 * |
64 * The result parameter passed to <code>func</code> is an int32_t that, if | 74 * NOTE: If you use a required callback on a background thread, |
65 * negative indicates an error code whose meaning is specific to the calling | 75 * you must have created and attached a PPB_MessageLoop. |
66 * method (refer to <code>pp_error.h</code> for further information). A | 76 * Otherwise, the system can not run your callback on that thread, |
67 * positive or 0 value is a return result indicating success whose meaning | 77 * and will instead emit a log message and crash your plugin to |
68 * depends on the calling method (e.g. number of bytes read). | 78 * make the problem more obvious. |
| 79 * |
| 80 * - Optional: The callback may be invoked asynchronously, or the PPB method |
| 81 * may complete synchronously if it can do so without blocking. |
| 82 * If the method will complete asynchronously, it will return |
| 83 * PP_OK_COMPLETIONPENDING. Otherwise, it will complete |
| 84 * synchronously and return an appropriate code (see below for |
| 85 * more information on the return code). Optional callbacks are |
| 86 * generally more difficult to use correctly than Required |
| 87 * callbacks, but can provide better performance for some APIs |
| 88 * (especially APIs with buffered reads, such as PPB_URLLoader or |
| 89 * PPB_FileIO). |
| 90 * |
| 91 * NOTE: If you use an optional callback on a background thread, |
| 92 * and you have not created and attached a PPB_MessageLoop, then |
| 93 * the method you invoke will fail without running and return |
| 94 * PP_ERROR_NO_MESSAGE_LOOP. |
| 95 * |
| 96 * - Blocking: In this case, the callback's function pointer is NULL, and the |
| 97 * invoked method must complete synchronously. The method will |
| 98 * run to completion and return an appropriate code when finished |
| 99 * (see below for more information). Blocking completion |
| 100 * callbacks are only supported on background threads. |
| 101 * |
| 102 * <code>PP_BlockUntilComplete()</code> provides a convenient way |
| 103 * to specify blocking behavior. Refer to |
| 104 * <code>PP_BlockUntilComplete</code> for more information. |
| 105 * |
| 106 * When the callback is run asynchronously, the result parameter passed to |
| 107 * <code>func</code> is an int32_t that, if negative indicates an error code |
| 108 * whose meaning is specific to the calling method (refer to |
| 109 * <code>pp_error.h</code> for further information). A positive or 0 value is a |
| 110 * return result indicating success whose meaning depends on the calling method |
| 111 * (e.g. number of bytes read). |
69 */ | 112 */ |
70 [passByValue] struct PP_CompletionCallback { | 113 [passByValue] struct PP_CompletionCallback { |
71 /** | 114 /** |
72 * This value is a callback function that will be called. | 115 * This value is a callback function that will be called, or NULL if this is |
| 116 * a blocking completion callback. |
73 */ | 117 */ |
74 PP_CompletionCallback_Func func; | 118 PP_CompletionCallback_Func func; |
75 /** | 119 /** |
76 * This value is a pointer to user data passed to a callback function. | 120 * This value is a pointer to user data passed to a callback function. |
77 */ | 121 */ |
78 mem_t user_data; | 122 mem_t user_data; |
79 | 123 |
80 /** | 124 /** |
81 * Flags used to control how non-NULL callbacks are scheduled by | 125 * Flags used to control how non-NULL callbacks are scheduled by |
82 * asynchronous methods. | 126 * asynchronous methods. |
83 */ | 127 */ |
84 int32_t flags; | 128 int32_t flags; |
85 }; | 129 }; |
86 | 130 |
87 #inline c | 131 #inline c |
88 #include <stdlib.h> | 132 #include <stdlib.h> |
89 | 133 |
90 /** | 134 /** |
91 * @addtogroup Functions | 135 * @addtogroup Functions |
92 * @{ | 136 * @{ |
93 */ | 137 */ |
94 /** | 138 /** |
95 * PP_MakeCompletionCallback() is used to create a | 139 * PP_MakeCompletionCallback() is used to create a |
96 * <code>PP_CompletionCallback</code>. | 140 * <code>PP_CompletionCallback</code>. |
97 * | 141 * |
98 * <strong>Example:</strong> | 142 * <strong>Example, creating a Required callback:</strong> |
99 * | 143 * |
100 * <code> | 144 * <code> |
101 * struct PP_CompletionCallback cc = PP_MakeCompletionCallback(Foo, NULL); | 145 * struct PP_CompletionCallback cc = PP_MakeCompletionCallback(Foo, NULL); |
| 146 * </code> |
| 147 * |
| 148 * <strong>Example, creating an Optional callback:</strong> |
| 149 * |
| 150 * <code> |
| 151 * struct PP_CompletionCallback cc = PP_MakeCompletionCallback(Foo, NULL); |
102 * cc.flags = cc.flags | PP_COMPLETIONCALLBACK_FLAG_OPTIONAL; | 152 * cc.flags = cc.flags | PP_COMPLETIONCALLBACK_FLAG_OPTIONAL; |
103 * </code> | 153 * </code> |
104 * | 154 * |
105 * @param[in] func A <code>PP_CompletionCallback_Func</code> that will be | 155 * @param[in] func A <code>PP_CompletionCallback_Func</code> that will be |
106 * called. | 156 * called. |
107 * @param[in] user_data A pointer to user data passed to your callback | 157 * @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 | 158 * function. This is optional and is typically used to help track state |
109 * when you may have multiple callbacks pending. | 159 * when you may have multiple callbacks pending. |
110 * | 160 * |
111 * @return A <code>PP_CompletionCallback</code> structure. | 161 * @return A <code>PP_CompletionCallback</code> structure. |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 struct PP_CompletionCallback temp = *cc; | 253 struct PP_CompletionCallback temp = *cc; |
204 *cc = PP_BlockUntilComplete(); | 254 *cc = PP_BlockUntilComplete(); |
205 PP_RunCompletionCallback(&temp, res); | 255 PP_RunCompletionCallback(&temp, res); |
206 } | 256 } |
207 /** | 257 /** |
208 * @} | 258 * @} |
209 */ | 259 */ |
210 | 260 |
211 #endinl | 261 #endinl |
212 | 262 |
OLD | NEW |