1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ppapi/thunk/enter.h"
#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/strings/stringprintf.h"
#include "base/synchronization/lock.h"
#include "base/task/single_thread_task_runner.h"
#include "ppapi/shared_impl/ppapi_globals.h"
#include "ppapi/shared_impl/resource_tracker.h"
#include "ppapi/shared_impl/tracked_callback.h"
#include "ppapi/thunk/ppb_instance_api.h"
#include "ppapi/thunk/resource_creation_api.h"
namespace ppapi {
namespace {
bool IsMainThread() {
return
PpapiGlobals::Get()->GetMainThreadMessageLoop()->BelongsToCurrentThread();
}
bool CurrentThreadHandlingBlockingMessage() {
ppapi::MessageLoopShared* current =
PpapiGlobals::Get()->GetCurrentMessageLoop();
return current && current->CurrentlyHandlingBlockingMessage();
}
} // namespace
namespace thunk {
namespace subtle {
EnterBase::EnterBase() {}
EnterBase::EnterBase(PP_Resource resource) : resource_(GetResource(resource)) {}
EnterBase::EnterBase(PP_Instance instance, SingletonResourceID resource_id)
: resource_(GetSingletonResource(instance, resource_id)) {
if (!resource_)
retval_ = PP_ERROR_BADARGUMENT;
}
EnterBase::EnterBase(PP_Resource resource,
const PP_CompletionCallback& callback)
: EnterBase(resource) {
callback_ = new TrackedCallback(resource_, callback);
}
EnterBase::EnterBase(PP_Instance instance,
SingletonResourceID resource_id,
const PP_CompletionCallback& callback)
: EnterBase(instance, resource_id) {
callback_ = new TrackedCallback(resource_, callback);
}
EnterBase::~EnterBase() {
// callback_ is cleared any time it is run, scheduled to be run, or once we
// know it will be completed asynchronously. So by this point it should be
// null.
DCHECK(!callback_) << "|callback_| is not null. Did you forget to call "
"|EnterBase::SetResult| in the interface's thunk?";
}
int32_t EnterBase::SetResult(int32_t result) {
if (!callback_) {
// It doesn't make sense to call SetResult if there is no callback.
NOTREACHED();
}
if (result == PP_OK_COMPLETIONPENDING) {
retval_ = result;
if (callback_->is_blocking()) {
DCHECK(!IsMainThread()); // We should have returned an error before this.
retval_ = callback_->BlockUntilComplete();
} else {
// The callback is not blocking and the operation will complete
// asynchronously, so there's nothing to do.
retval_ = result;
}
} else {
// The function completed synchronously.
if (callback_->is_required()) {
// This is a required callback, so we must issue it asynchronously.
callback_->PostRun(result);
retval_ = PP_OK_COMPLETIONPENDING;
} else {
// The callback is blocking or optional, so all we need to do is mark
// the callback as completed so that it won't be issued later.
callback_->MarkAsCompleted();
retval_ = result;
}
}
callback_ = nullptr;
return retval_;
}
// static
Resource* EnterBase::GetResource(PP_Resource resource) {
return PpapiGlobals::Get()->GetResourceTracker()->GetResource(resource);
}
// static
Resource* EnterBase::GetSingletonResource(PP_Instance instance,
SingletonResourceID resource_id) {
PPB_Instance_API* ppb_instance =
PpapiGlobals::Get()->GetInstanceAPI(instance);
if (!ppb_instance)
return nullptr;
return ppb_instance->GetSingletonResource(instance, resource_id);
}
void EnterBase::SetStateForCallbackError(bool report_error) {
if (PpapiGlobals::Get()->IsHostGlobals()) {
// In-process plugins can't make PPAPI calls off the main thread.
CHECK(IsMainThread());
}
if (callback_) {
if (callback_->is_blocking() && IsMainThread()) {
// Blocking callbacks are never allowed on the main thread.
callback_->MarkAsCompleted();
callback_ = nullptr;
retval_ = PP_ERROR_BLOCKS_MAIN_THREAD;
if (report_error) {
std::string message(
"Blocking callbacks are not allowed on the main thread.");
PpapiGlobals::Get()->BroadcastLogWithSource(0, PP_LOGLEVEL_ERROR,
std::string(), message);
}
} else if (callback_->is_blocking() &&
CurrentThreadHandlingBlockingMessage()) {
// Blocking callbacks are not allowed while handling a blocking message.
callback_->MarkAsCompleted();
callback_ = nullptr;
retval_ = PP_ERROR_WOULD_BLOCK_THREAD;
if (report_error) {
std::string message("Blocking callbacks are not allowed while handling "
"a blocking message from JavaScript.");
PpapiGlobals::Get()->BroadcastLogWithSource(0, PP_LOGLEVEL_ERROR,
std::string(), message);
}
} else if (!IsMainThread() &&
callback_->has_null_target_loop() &&
!callback_->is_blocking()) {
// On a non-main thread, there must be a valid target loop for non-
// blocking callbacks, or we will have no place to run them.
// If the callback is required, there's no nice way to tell the plugin.
// We can't run their callback asynchronously without a message loop, and
// the plugin won't expect any return code other than
// PP_OK_COMPLETIONPENDING. So we crash to make the problem more obvious.
if (callback_->is_required()) {
std::string message("Attempted to use a required callback, but there "
"is no attached message loop on which to run the "
"callback.");
PpapiGlobals::Get()->BroadcastLogWithSource(0, PP_LOGLEVEL_ERROR,
std::string(), message);
LOG(FATAL) << message;
}
callback_->MarkAsCompleted();
callback_ = nullptr;
retval_ = PP_ERROR_NO_MESSAGE_LOOP;
if (report_error) {
std::string message(
"The calling thread must have a message loop attached.");
PpapiGlobals::Get()->BroadcastLogWithSource(0, PP_LOGLEVEL_ERROR,
std::string(), message);
}
}
}
}
void EnterBase::ClearCallback() {
callback_ = nullptr;
}
void EnterBase::SetStateForResourceError(PP_Resource pp_resource,
Resource* resource_base,
void* object,
bool report_error) {
// Check for callback errors. If we get any, SetStateForCallbackError will
// emit a log message. But we also want to check for resource errors. If there
// are both kinds of errors, we'll emit two log messages and return
// PP_ERROR_BADRESOURCE.
SetStateForCallbackError(report_error);
if (object)
return; // Everything worked.
if (callback_ && callback_->is_required()) {
callback_->PostRun(static_cast<int32_t>(PP_ERROR_BADRESOURCE));
callback_ = nullptr;
retval_ = PP_OK_COMPLETIONPENDING;
} else {
if (callback_)
callback_->MarkAsCompleted();
callback_ = nullptr;
retval_ = PP_ERROR_BADRESOURCE;
}
// We choose to silently ignore the error when the pp_resource is null
// because this is a pretty common case and we don't want to have lots
// of errors in the log. This should be an obvious case to debug.
if (report_error && pp_resource) {
std::string message;
if (resource_base) {
message = base::StringPrintf(
"0x%X is not the correct type for this function.",
pp_resource);
} else {
message = base::StringPrintf(
"0x%X is not a valid resource ID.",
pp_resource);
}
PpapiGlobals::Get()->BroadcastLogWithSource(0, PP_LOGLEVEL_ERROR,
std::string(), message);
}
}
void EnterBase::SetStateForFunctionError(PP_Instance pp_instance,
void* object,
bool report_error) {
// Check for callback errors. If we get any, SetStateForCallbackError will
// emit a log message. But we also want to check for instance errors. If there
// are both kinds of errors, we'll emit two log messages and return
// PP_ERROR_BADARGUMENT.
SetStateForCallbackError(report_error);
if (object)
return; // Everything worked.
if (callback_ && callback_->is_required()) {
callback_->PostRun(static_cast<int32_t>(PP_ERROR_BADARGUMENT));
callback_ = nullptr;
retval_ = PP_OK_COMPLETIONPENDING;
} else {
if (callback_)
callback_->MarkAsCompleted();
callback_ = nullptr;
retval_ = PP_ERROR_BADARGUMENT;
}
// We choose to silently ignore the error when the pp_instance is null as
// for PP_Resources above.
if (report_error && pp_instance) {
std::string message;
message = base::StringPrintf(
"0x%X is not a valid instance ID.",
pp_instance);
PpapiGlobals::Get()->BroadcastLogWithSource(0, PP_LOGLEVEL_ERROR,
std::string(), message);
}
}
} // namespace subtle
EnterInstance::EnterInstance(PP_Instance instance)
: EnterBase(),
functions_(PpapiGlobals::Get()->GetInstanceAPI(instance)) {
SetStateForFunctionError(instance, functions_, true);
}
EnterInstance::EnterInstance(PP_Instance instance,
const PP_CompletionCallback& callback)
: EnterBase(0 /* resource */, callback),
// TODO(dmichael): This means that the callback_ we get is not associated
// even with the instance, but we should handle that for
// MouseLock (maybe others?).
functions_(PpapiGlobals::Get()->GetInstanceAPI(instance)) {
SetStateForFunctionError(instance, functions_, true);
}
EnterInstance::~EnterInstance() {
}
EnterInstanceNoLock::EnterInstanceNoLock(PP_Instance instance)
: EnterBase(),
functions_(PpapiGlobals::Get()->GetInstanceAPI(instance)) {
SetStateForFunctionError(instance, functions_, true);
}
EnterInstanceNoLock::EnterInstanceNoLock(
PP_Instance instance,
const PP_CompletionCallback& callback)
: EnterBase(0 /* resource */, callback),
// TODO(dmichael): This means that the callback_ we get is not associated
// even with the instance, but we should handle that for
// MouseLock (maybe others?).
functions_(PpapiGlobals::Get()->GetInstanceAPI(instance)) {
SetStateForFunctionError(instance, functions_, true);
}
EnterInstanceNoLock::~EnterInstanceNoLock() {
}
EnterResourceCreation::EnterResourceCreation(PP_Instance instance)
: EnterBase(),
functions_(PpapiGlobals::Get()->GetResourceCreationAPI(instance)) {
SetStateForFunctionError(instance, functions_, true);
}
EnterResourceCreation::~EnterResourceCreation() {
}
EnterResourceCreationNoLock::EnterResourceCreationNoLock(PP_Instance instance)
: EnterBase(),
functions_(PpapiGlobals::Get()->GetResourceCreationAPI(instance)) {
SetStateForFunctionError(instance, functions_, true);
}
EnterResourceCreationNoLock::~EnterResourceCreationNoLock() {
}
} // namespace thunk
} // namespace ppapi
|