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.h"
10 #include "base/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 void AssertLockHeld() {
33 base::Lock
* proxy_lock
= PpapiGlobals::Get()->GetProxyLock();
34 // The lock is only valid in the plugin side of the proxy, so it only makes
35 // sense to assert there. Otherwise, silently succeed.
37 proxy_lock
->AssertAcquired();
40 EnterBase::EnterBase()
45 EnterBase::EnterBase(PP_Resource resource
)
46 : resource_(GetResource(resource
)),
50 EnterBase::EnterBase(PP_Instance instance
, SingletonResourceID resource_id
)
51 : resource_(GetSingletonResource(instance
, resource_id
)),
55 EnterBase::EnterBase(PP_Resource resource
,
56 const PP_CompletionCallback
& callback
)
57 : resource_(GetResource(resource
)),
59 callback_
= new TrackedCallback(resource_
, callback
);
62 EnterBase::EnterBase(PP_Instance instance
, SingletonResourceID resource_id
,
63 const PP_CompletionCallback
& callback
)
64 : resource_(GetSingletonResource(instance
, resource_id
)),
66 DCHECK(resource_
|| !instance
);
68 retval_
= PP_ERROR_BADARGUMENT
;
69 callback_
= new TrackedCallback(resource_
, callback
);
72 EnterBase::~EnterBase() {
73 // callback_ is cleared any time it is run, scheduled to be run, or once we
74 // know it will be completed asynchronously. So by this point it should be
76 DCHECK(!callback_
) << "|callback_| is not NULL. Did you forget to call "
77 "|EnterBase::SetResult| in the interface's thunk?";
80 int32_t EnterBase::SetResult(int32_t result
) {
82 // It doesn't make sense to call SetResult if there is no callback.
87 if (result
== PP_OK_COMPLETIONPENDING
) {
89 if (callback_
->is_blocking()) {
90 DCHECK(!IsMainThread()); // We should have returned an error before this.
91 retval_
= callback_
->BlockUntilComplete();
93 // The callback is not blocking and the operation will complete
94 // asynchronously, so there's nothing to do.
98 // The function completed synchronously.
99 if (callback_
->is_required()) {
100 // This is a required callback, so we must issue it asynchronously.
101 callback_
->PostRun(result
);
102 retval_
= PP_OK_COMPLETIONPENDING
;
104 // The callback is blocking or optional, so all we need to do is mark
105 // the callback as completed so that it won't be issued later.
106 callback_
->MarkAsCompleted();
115 Resource
* EnterBase::GetResource(PP_Resource resource
) {
116 return PpapiGlobals::Get()->GetResourceTracker()->GetResource(resource
);
120 Resource
* EnterBase::GetSingletonResource(PP_Instance instance
,
121 SingletonResourceID resource_id
) {
122 PPB_Instance_API
* ppb_instance
=
123 PpapiGlobals::Get()->GetInstanceAPI(instance
);
127 return ppb_instance
->GetSingletonResource(instance
, resource_id
);
130 void EnterBase::SetStateForCallbackError(bool report_error
) {
131 if (PpapiGlobals::Get()->IsHostGlobals()) {
132 // In-process plugins can't make PPAPI calls off the main thread.
133 CHECK(IsMainThread());
136 if (callback_
->is_blocking() && IsMainThread()) {
137 // Blocking callbacks are never allowed on the main thread.
138 callback_
->MarkAsCompleted();
140 retval_
= PP_ERROR_BLOCKS_MAIN_THREAD
;
143 "Blocking callbacks are not allowed on the main thread.");
144 PpapiGlobals::Get()->BroadcastLogWithSource(0, PP_LOGLEVEL_ERROR
,
145 std::string(), message
);
147 } else if (!IsMainThread() &&
148 callback_
->has_null_target_loop() &&
149 !callback_
->is_blocking()) {
150 // On a non-main thread, there must be a valid target loop for non-
151 // blocking callbacks, or we will have no place to run them.
153 // If the callback is required, there's no nice way to tell the plugin.
154 // We can't run their callback asynchronously without a message loop, and
155 // the plugin won't expect any return code other than
156 // PP_OK_COMPLETIONPENDING. So we crash to make the problem more obvious.
157 if (callback_
->is_required()) {
158 std::string
message("Attempted to use a required callback, but there "
159 "is no attached message loop on which to run the "
161 PpapiGlobals::Get()->BroadcastLogWithSource(0, PP_LOGLEVEL_ERROR
,
162 std::string(), message
);
163 LOG(FATAL
) << message
;
166 callback_
->MarkAsCompleted();
168 retval_
= PP_ERROR_NO_MESSAGE_LOOP
;
171 "The calling thread must have a message loop attached.");
172 PpapiGlobals::Get()->BroadcastLogWithSource(0, PP_LOGLEVEL_ERROR
,
173 std::string(), message
);
179 void EnterBase::ClearCallback() {
183 void EnterBase::SetStateForResourceError(PP_Resource pp_resource
,
184 Resource
* resource_base
,
187 // Check for callback errors. If we get any, SetStateForCallbackError will
188 // emit a log message. But we also want to check for resource errors. If there
189 // are both kinds of errors, we'll emit two log messages and return
190 // PP_ERROR_BADRESOURCE.
191 SetStateForCallbackError(report_error
);
194 return; // Everything worked.
196 if (callback_
&& callback_
->is_required()) {
197 callback_
->PostRun(static_cast<int32_t>(PP_ERROR_BADRESOURCE
));
199 retval_
= PP_OK_COMPLETIONPENDING
;
202 callback_
->MarkAsCompleted();
204 retval_
= PP_ERROR_BADRESOURCE
;
207 // We choose to silently ignore the error when the pp_resource is null
208 // because this is a pretty common case and we don't want to have lots
209 // of errors in the log. This should be an obvious case to debug.
210 if (report_error
&& pp_resource
) {
213 message
= base::StringPrintf(
214 "0x%X is not the correct type for this function.",
217 message
= base::StringPrintf(
218 "0x%X is not a valid resource ID.",
221 PpapiGlobals::Get()->BroadcastLogWithSource(0, PP_LOGLEVEL_ERROR
,
222 std::string(), message
);
226 void EnterBase::SetStateForFunctionError(PP_Instance pp_instance
,
229 // Check for callback errors. If we get any, SetStateForCallbackError will
230 // emit a log message. But we also want to check for instance errors. If there
231 // are both kinds of errors, we'll emit two log messages and return
232 // PP_ERROR_BADARGUMENT.
233 SetStateForCallbackError(report_error
);
236 return; // Everything worked.
238 if (callback_
&& callback_
->is_required()) {
239 callback_
->PostRun(static_cast<int32_t>(PP_ERROR_BADARGUMENT
));
241 retval_
= PP_OK_COMPLETIONPENDING
;
244 callback_
->MarkAsCompleted();
246 retval_
= PP_ERROR_BADARGUMENT
;
249 // We choose to silently ignore the error when the pp_instance is null as
250 // for PP_Resources above.
251 if (report_error
&& pp_instance
) {
253 message
= base::StringPrintf(
254 "0x%X is not a valid instance ID.",
256 PpapiGlobals::Get()->BroadcastLogWithSource(0, PP_LOGLEVEL_ERROR
,
257 std::string(), message
);
261 } // namespace subtle
263 EnterInstance::EnterInstance(PP_Instance instance
)
265 functions_(PpapiGlobals::Get()->GetInstanceAPI(instance
)) {
266 SetStateForFunctionError(instance
, functions_
, true);
269 EnterInstance::EnterInstance(PP_Instance instance
,
270 const PP_CompletionCallback
& callback
)
271 : EnterBase(0 /* resource */, callback
),
272 // TODO(dmichael): This means that the callback_ we get is not associated
273 // even with the instance, but we should handle that for
274 // MouseLock (maybe others?).
275 functions_(PpapiGlobals::Get()->GetInstanceAPI(instance
)) {
276 SetStateForFunctionError(instance
, functions_
, true);
279 EnterInstance::~EnterInstance() {
282 EnterInstanceNoLock::EnterInstanceNoLock(PP_Instance instance
)
284 functions_(PpapiGlobals::Get()->GetInstanceAPI(instance
)) {
285 SetStateForFunctionError(instance
, functions_
, true);
288 EnterInstanceNoLock::EnterInstanceNoLock(
289 PP_Instance instance
,
290 const PP_CompletionCallback
& callback
)
291 : EnterBase(0 /* resource */, callback
),
292 // TODO(dmichael): This means that the callback_ we get is not associated
293 // even with the instance, but we should handle that for
294 // MouseLock (maybe others?).
295 functions_(PpapiGlobals::Get()->GetInstanceAPI(instance
)) {
296 SetStateForFunctionError(instance
, functions_
, true);
299 EnterInstanceNoLock::~EnterInstanceNoLock() {
302 EnterResourceCreation::EnterResourceCreation(PP_Instance instance
)
304 functions_(PpapiGlobals::Get()->GetResourceCreationAPI(instance
)) {
305 SetStateForFunctionError(instance
, functions_
, true);
308 EnterResourceCreation::~EnterResourceCreation() {
311 EnterResourceCreationNoLock::EnterResourceCreationNoLock(PP_Instance instance
)
313 functions_(PpapiGlobals::Get()->GetResourceCreationAPI(instance
)) {
314 SetStateForFunctionError(instance
, functions_
, true);
317 EnterResourceCreationNoLock::~EnterResourceCreationNoLock() {