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
3 // found in the LICENSE file.
5 #include "ppapi/thunk/enter.h"
8 #include "base/logging.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/synchronization/lock.h"
12 #include "ppapi/c/pp_errors.h"
13 #include "ppapi/shared_impl/ppapi_globals.h"
14 #include "ppapi/shared_impl/tracked_callback.h"
15 #include "ppapi/thunk/ppb_instance_api.h"
16 #include "ppapi/thunk/resource_creation_api.h"
23 PpapiGlobals::Get()->GetMainThreadMessageLoop()->BelongsToCurrentThread();
32 EnterBase::EnterBase()
35 PpapiGlobals::Get()->MarkPluginIsActive();
38 EnterBase::EnterBase(PP_Resource resource
)
39 : resource_(GetResource(resource
)),
41 PpapiGlobals::Get()->MarkPluginIsActive();
44 EnterBase::EnterBase(PP_Instance instance
, SingletonResourceID resource_id
)
45 : resource_(GetSingletonResource(instance
, resource_id
)),
47 PpapiGlobals::Get()->MarkPluginIsActive();
50 EnterBase::EnterBase(PP_Resource resource
,
51 const PP_CompletionCallback
& callback
)
52 : resource_(GetResource(resource
)),
54 callback_
= new TrackedCallback(resource_
, callback
);
55 PpapiGlobals::Get()->MarkPluginIsActive();
58 EnterBase::EnterBase(PP_Instance instance
, SingletonResourceID resource_id
,
59 const PP_CompletionCallback
& callback
)
60 : resource_(GetSingletonResource(instance
, resource_id
)),
63 retval_
= PP_ERROR_BADARGUMENT
;
64 callback_
= new TrackedCallback(resource_
, callback
);
65 PpapiGlobals::Get()->MarkPluginIsActive();
68 EnterBase::~EnterBase() {
69 // callback_ is cleared any time it is run, scheduled to be run, or once we
70 // know it will be completed asynchronously. So by this point it should be
72 DCHECK(!callback_
.get())
73 << "|callback_| is not NULL. Did you forget to call "
74 "|EnterBase::SetResult| in the interface's thunk?";
77 int32_t EnterBase::SetResult(int32_t result
) {
78 if (!callback_
.get()) {
79 // It doesn't make sense to call SetResult if there is no callback.
84 if (result
== PP_OK_COMPLETIONPENDING
) {
86 if (callback_
->is_blocking()) {
87 DCHECK(!IsMainThread()); // We should have returned an error before this.
88 retval_
= callback_
->BlockUntilComplete();
90 // The callback is not blocking and the operation will complete
91 // asynchronously, so there's nothing to do.
95 // The function completed synchronously.
96 if (callback_
->is_required()) {
97 // This is a required callback, so we must issue it asynchronously.
98 callback_
->PostRun(result
);
99 retval_
= PP_OK_COMPLETIONPENDING
;
101 // The callback is blocking or optional, so all we need to do is mark
102 // the callback as completed so that it won't be issued later.
103 callback_
->MarkAsCompleted();
112 Resource
* EnterBase::GetResource(PP_Resource resource
) {
113 return PpapiGlobals::Get()->GetResourceTracker()->GetResource(resource
);
117 Resource
* EnterBase::GetSingletonResource(PP_Instance instance
,
118 SingletonResourceID resource_id
) {
119 PPB_Instance_API
* ppb_instance
=
120 PpapiGlobals::Get()->GetInstanceAPI(instance
);
124 return ppb_instance
->GetSingletonResource(instance
, resource_id
);
127 void EnterBase::SetStateForCallbackError(bool report_error
) {
128 if (PpapiGlobals::Get()->IsHostGlobals()) {
129 // In-process plugins can't make PPAPI calls off the main thread.
130 CHECK(IsMainThread());
132 if (callback_
.get()) {
133 if (callback_
->is_blocking() && IsMainThread()) {
134 // Blocking callbacks are never allowed on the main thread.
135 callback_
->MarkAsCompleted();
137 retval_
= PP_ERROR_BLOCKS_MAIN_THREAD
;
140 "Blocking callbacks are not allowed on the main thread.");
141 PpapiGlobals::Get()->BroadcastLogWithSource(0, PP_LOGLEVEL_ERROR
,
142 std::string(), message
);
144 } else if (!IsMainThread() &&
145 callback_
->has_null_target_loop() &&
146 !callback_
->is_blocking()) {
147 // On a non-main thread, there must be a valid target loop for non-
148 // blocking callbacks, or we will have no place to run them.
150 // If the callback is required, there's no nice way to tell the plugin.
151 // We can't run their callback asynchronously without a message loop, and
152 // the plugin won't expect any return code other than
153 // PP_OK_COMPLETIONPENDING. So we crash to make the problem more obvious.
154 if (callback_
->is_required()) {
155 std::string
message("Attempted to use a required callback, but there "
156 "is no attached message loop on which to run the "
158 PpapiGlobals::Get()->BroadcastLogWithSource(0, PP_LOGLEVEL_ERROR
,
159 std::string(), message
);
160 LOG(FATAL
) << message
;
163 callback_
->MarkAsCompleted();
165 retval_
= PP_ERROR_NO_MESSAGE_LOOP
;
168 "The calling thread must have a message loop attached.");
169 PpapiGlobals::Get()->BroadcastLogWithSource(0, PP_LOGLEVEL_ERROR
,
170 std::string(), message
);
176 void EnterBase::ClearCallback() {
180 void EnterBase::SetStateForResourceError(PP_Resource pp_resource
,
181 Resource
* resource_base
,
184 // Check for callback errors. If we get any, SetStateForCallbackError will
185 // emit a log message. But we also want to check for resource errors. If there
186 // are both kinds of errors, we'll emit two log messages and return
187 // PP_ERROR_BADRESOURCE.
188 SetStateForCallbackError(report_error
);
191 return; // Everything worked.
193 if (callback_
.get() && callback_
->is_required()) {
194 callback_
->PostRun(static_cast<int32_t>(PP_ERROR_BADRESOURCE
));
196 retval_
= PP_OK_COMPLETIONPENDING
;
199 callback_
->MarkAsCompleted();
201 retval_
= PP_ERROR_BADRESOURCE
;
204 // We choose to silently ignore the error when the pp_resource is null
205 // because this is a pretty common case and we don't want to have lots
206 // of errors in the log. This should be an obvious case to debug.
207 if (report_error
&& pp_resource
) {
210 message
= base::StringPrintf(
211 "0x%X is not the correct type for this function.",
214 message
= base::StringPrintf(
215 "0x%X is not a valid resource ID.",
218 PpapiGlobals::Get()->BroadcastLogWithSource(0, PP_LOGLEVEL_ERROR
,
219 std::string(), message
);
223 void EnterBase::SetStateForFunctionError(PP_Instance pp_instance
,
226 // Check for callback errors. If we get any, SetStateForCallbackError will
227 // emit a log message. But we also want to check for instance errors. If there
228 // are both kinds of errors, we'll emit two log messages and return
229 // PP_ERROR_BADARGUMENT.
230 SetStateForCallbackError(report_error
);
233 return; // Everything worked.
235 if (callback_
.get() && callback_
->is_required()) {
236 callback_
->PostRun(static_cast<int32_t>(PP_ERROR_BADARGUMENT
));
238 retval_
= PP_OK_COMPLETIONPENDING
;
241 callback_
->MarkAsCompleted();
243 retval_
= PP_ERROR_BADARGUMENT
;
246 // We choose to silently ignore the error when the pp_instance is null as
247 // for PP_Resources above.
248 if (report_error
&& pp_instance
) {
250 message
= base::StringPrintf(
251 "0x%X is not a valid instance ID.",
253 PpapiGlobals::Get()->BroadcastLogWithSource(0, PP_LOGLEVEL_ERROR
,
254 std::string(), message
);
258 } // namespace subtle
260 EnterInstance::EnterInstance(PP_Instance instance
)
262 functions_(PpapiGlobals::Get()->GetInstanceAPI(instance
)) {
263 SetStateForFunctionError(instance
, functions_
, true);
266 EnterInstance::EnterInstance(PP_Instance instance
,
267 const PP_CompletionCallback
& callback
)
268 : EnterBase(0 /* resource */, callback
),
269 // TODO(dmichael): This means that the callback_ we get is not associated
270 // even with the instance, but we should handle that for
271 // MouseLock (maybe others?).
272 functions_(PpapiGlobals::Get()->GetInstanceAPI(instance
)) {
273 SetStateForFunctionError(instance
, functions_
, true);
276 EnterInstance::~EnterInstance() {
279 EnterInstanceNoLock::EnterInstanceNoLock(PP_Instance instance
)
281 functions_(PpapiGlobals::Get()->GetInstanceAPI(instance
)) {
282 SetStateForFunctionError(instance
, functions_
, true);
285 EnterInstanceNoLock::EnterInstanceNoLock(
286 PP_Instance instance
,
287 const PP_CompletionCallback
& callback
)
288 : EnterBase(0 /* resource */, callback
),
289 // TODO(dmichael): This means that the callback_ we get is not associated
290 // even with the instance, but we should handle that for
291 // MouseLock (maybe others?).
292 functions_(PpapiGlobals::Get()->GetInstanceAPI(instance
)) {
293 SetStateForFunctionError(instance
, functions_
, true);
296 EnterInstanceNoLock::~EnterInstanceNoLock() {
299 EnterResourceCreation::EnterResourceCreation(PP_Instance instance
)
301 functions_(PpapiGlobals::Get()->GetResourceCreationAPI(instance
)) {
302 SetStateForFunctionError(instance
, functions_
, true);
305 EnterResourceCreation::~EnterResourceCreation() {
308 EnterResourceCreationNoLock::EnterResourceCreationNoLock(PP_Instance instance
)
310 functions_(PpapiGlobals::Get()->GetResourceCreationAPI(instance
)) {
311 SetStateForFunctionError(instance
, functions_
, true);
314 EnterResourceCreationNoLock::~EnterResourceCreationNoLock() {