Make certificate viewer a tab-modal dialog.
[chromium-blink-merge.git] / ppapi / thunk / enter.cc
blob02151eb4ad0a1d249ebaa274f2e81005fa95871b
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"
7 #include "base/bind.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"
18 namespace ppapi {
19 namespace {
21 bool IsMainThread() {
22 return
23 PpapiGlobals::Get()->GetMainThreadMessageLoop()->BelongsToCurrentThread();
26 } // namespace
28 namespace thunk {
30 namespace subtle {
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.
36 if (proxy_lock)
37 proxy_lock->AssertAcquired();
40 EnterBase::EnterBase()
41 : resource_(NULL),
42 retval_(PP_OK) {
45 EnterBase::EnterBase(PP_Resource resource)
46 : resource_(GetResource(resource)),
47 retval_(PP_OK) {
50 EnterBase::EnterBase(PP_Instance instance, SingletonResourceID resource_id)
51 : resource_(GetSingletonResource(instance, resource_id)),
52 retval_(PP_OK) {
55 EnterBase::EnterBase(PP_Resource resource,
56 const PP_CompletionCallback& callback)
57 : resource_(GetResource(resource)),
58 retval_(PP_OK) {
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)),
65 retval_(PP_OK) {
66 DCHECK(resource_ || !instance);
67 if (!resource_)
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
75 // NULL.
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) {
81 if (!callback_) {
82 // It doesn't make sense to call SetResult if there is no callback.
83 NOTREACHED();
84 retval_ = result;
85 return result;
87 if (result == PP_OK_COMPLETIONPENDING) {
88 retval_ = result;
89 if (callback_->is_blocking()) {
90 DCHECK(!IsMainThread()); // We should have returned an error before this.
91 retval_ = callback_->BlockUntilComplete();
92 } else {
93 // The callback is not blocking and the operation will complete
94 // asynchronously, so there's nothing to do.
95 retval_ = result;
97 } else {
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;
103 } else {
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();
107 retval_ = result;
110 callback_ = NULL;
111 return retval_;
114 // static
115 Resource* EnterBase::GetResource(PP_Resource resource) {
116 return PpapiGlobals::Get()->GetResourceTracker()->GetResource(resource);
119 // static
120 Resource* EnterBase::GetSingletonResource(PP_Instance instance,
121 SingletonResourceID resource_id) {
122 PPB_Instance_API* ppb_instance =
123 PpapiGlobals::Get()->GetInstanceAPI(instance);
124 if (!ppb_instance)
125 return NULL;
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());
135 if (callback_) {
136 if (callback_->is_blocking() && IsMainThread()) {
137 // Blocking callbacks are never allowed on the main thread.
138 callback_->MarkAsCompleted();
139 callback_ = NULL;
140 retval_ = PP_ERROR_BLOCKS_MAIN_THREAD;
141 if (report_error) {
142 std::string message(
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 "
160 "callback.");
161 PpapiGlobals::Get()->BroadcastLogWithSource(0, PP_LOGLEVEL_ERROR,
162 std::string(), message);
163 LOG(FATAL) << message;
166 callback_->MarkAsCompleted();
167 callback_ = NULL;
168 retval_ = PP_ERROR_NO_MESSAGE_LOOP;
169 if (report_error) {
170 std::string message(
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() {
180 callback_ = NULL;
183 void EnterBase::SetStateForResourceError(PP_Resource pp_resource,
184 Resource* resource_base,
185 void* object,
186 bool report_error) {
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);
193 if (object)
194 return; // Everything worked.
196 if (callback_ && callback_->is_required()) {
197 callback_->PostRun(static_cast<int32_t>(PP_ERROR_BADRESOURCE));
198 callback_ = NULL;
199 retval_ = PP_OK_COMPLETIONPENDING;
200 } else {
201 if (callback_)
202 callback_->MarkAsCompleted();
203 callback_ = NULL;
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) {
211 std::string message;
212 if (resource_base) {
213 message = base::StringPrintf(
214 "0x%X is not the correct type for this function.",
215 pp_resource);
216 } else {
217 message = base::StringPrintf(
218 "0x%X is not a valid resource ID.",
219 pp_resource);
221 PpapiGlobals::Get()->BroadcastLogWithSource(0, PP_LOGLEVEL_ERROR,
222 std::string(), message);
226 void EnterBase::SetStateForFunctionError(PP_Instance pp_instance,
227 void* object,
228 bool report_error) {
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);
235 if (object)
236 return; // Everything worked.
238 if (callback_ && callback_->is_required()) {
239 callback_->PostRun(static_cast<int32_t>(PP_ERROR_BADARGUMENT));
240 callback_ = NULL;
241 retval_ = PP_OK_COMPLETIONPENDING;
242 } else {
243 if (callback_)
244 callback_->MarkAsCompleted();
245 callback_ = NULL;
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) {
252 std::string message;
253 message = base::StringPrintf(
254 "0x%X is not a valid instance ID.",
255 pp_instance);
256 PpapiGlobals::Get()->BroadcastLogWithSource(0, PP_LOGLEVEL_ERROR,
257 std::string(), message);
261 } // namespace subtle
263 EnterInstance::EnterInstance(PP_Instance instance)
264 : EnterBase(),
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)
283 : EnterBase(),
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)
303 : EnterBase(),
304 functions_(PpapiGlobals::Get()->GetResourceCreationAPI(instance)) {
305 SetStateForFunctionError(instance, functions_, true);
308 EnterResourceCreation::~EnterResourceCreation() {
311 EnterResourceCreationNoLock::EnterResourceCreationNoLock(PP_Instance instance)
312 : EnterBase(),
313 functions_(PpapiGlobals::Get()->GetResourceCreationAPI(instance)) {
314 SetStateForFunctionError(instance, functions_, true);
317 EnterResourceCreationNoLock::~EnterResourceCreationNoLock() {
320 } // namespace thunk
321 } // namespace ppapi