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 #include "ppapi/thunk/enter.h" | 5 #include "ppapi/thunk/enter.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
11 #include "base/synchronization/lock.h" | 11 #include "base/synchronization/lock.h" |
12 #include "ppapi/c/pp_errors.h" | 12 #include "ppapi/c/pp_errors.h" |
13 #include "ppapi/shared_impl/ppapi_globals.h" | 13 #include "ppapi/shared_impl/ppapi_globals.h" |
14 #include "ppapi/shared_impl/tracked_callback.h" | 14 #include "ppapi/shared_impl/tracked_callback.h" |
15 #include "ppapi/thunk/ppb_instance_api.h" | 15 #include "ppapi/thunk/ppb_instance_api.h" |
16 #include "ppapi/thunk/resource_creation_api.h" | 16 #include "ppapi/thunk/resource_creation_api.h" |
17 | 17 |
18 namespace ppapi { | 18 namespace ppapi { |
19 namespace { | 19 namespace { |
20 | 20 |
21 bool IsMainThread() { | 21 bool IsMainThread() { |
22 return | 22 return |
23 PpapiGlobals::Get()->GetMainThreadMessageLoop()->BelongsToCurrentThread(); | 23 PpapiGlobals::Get()->GetMainThreadMessageLoop()->BelongsToCurrentThread(); |
24 } | 24 } |
25 | 25 |
| 26 bool CurrentThreadHandlingBlockingMessage() { |
| 27 ppapi::MessageLoopShared* current = |
| 28 PpapiGlobals::Get()->GetCurrentMessageLoop(); |
| 29 return current && current->CurrentlyHandlingBlockingMessage(); |
| 30 } |
| 31 |
26 } // namespace | 32 } // namespace |
27 | 33 |
28 namespace thunk { | 34 namespace thunk { |
29 | 35 |
30 namespace subtle { | 36 namespace subtle { |
31 | 37 |
32 EnterBase::EnterBase() | 38 EnterBase::EnterBase() |
33 : resource_(NULL), | 39 : resource_(NULL), |
34 retval_(PP_OK) { | 40 retval_(PP_OK) { |
35 PpapiGlobals::Get()->MarkPluginIsActive(); | 41 PpapiGlobals::Get()->MarkPluginIsActive(); |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 // Blocking callbacks are never allowed on the main thread. | 140 // Blocking callbacks are never allowed on the main thread. |
135 callback_->MarkAsCompleted(); | 141 callback_->MarkAsCompleted(); |
136 callback_ = NULL; | 142 callback_ = NULL; |
137 retval_ = PP_ERROR_BLOCKS_MAIN_THREAD; | 143 retval_ = PP_ERROR_BLOCKS_MAIN_THREAD; |
138 if (report_error) { | 144 if (report_error) { |
139 std::string message( | 145 std::string message( |
140 "Blocking callbacks are not allowed on the main thread."); | 146 "Blocking callbacks are not allowed on the main thread."); |
141 PpapiGlobals::Get()->BroadcastLogWithSource(0, PP_LOGLEVEL_ERROR, | 147 PpapiGlobals::Get()->BroadcastLogWithSource(0, PP_LOGLEVEL_ERROR, |
142 std::string(), message); | 148 std::string(), message); |
143 } | 149 } |
| 150 } else if (callback_->is_blocking() && |
| 151 CurrentThreadHandlingBlockingMessage()) { |
| 152 // Blocking callbacks are not allowed while handling a blocking message. |
| 153 callback_->MarkAsCompleted(); |
| 154 callback_ = NULL; |
| 155 retval_ = PP_ERROR_WOULD_BLOCK_THREAD; |
| 156 if (report_error) { |
| 157 std::string message("Blocking callbacks are not allowed while handling " |
| 158 "a blocking message from JavaScript."); |
| 159 PpapiGlobals::Get()->BroadcastLogWithSource(0, PP_LOGLEVEL_ERROR, |
| 160 std::string(), message); |
| 161 } |
144 } else if (!IsMainThread() && | 162 } else if (!IsMainThread() && |
145 callback_->has_null_target_loop() && | 163 callback_->has_null_target_loop() && |
146 !callback_->is_blocking()) { | 164 !callback_->is_blocking()) { |
147 // On a non-main thread, there must be a valid target loop for non- | 165 // 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. | 166 // blocking callbacks, or we will have no place to run them. |
149 | 167 |
150 // If the callback is required, there's no nice way to tell the plugin. | 168 // 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 | 169 // We can't run their callback asynchronously without a message loop, and |
152 // the plugin won't expect any return code other than | 170 // the plugin won't expect any return code other than |
153 // PP_OK_COMPLETIONPENDING. So we crash to make the problem more obvious. | 171 // PP_OK_COMPLETIONPENDING. So we crash to make the problem more obvious. |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
309 : EnterBase(), | 327 : EnterBase(), |
310 functions_(PpapiGlobals::Get()->GetResourceCreationAPI(instance)) { | 328 functions_(PpapiGlobals::Get()->GetResourceCreationAPI(instance)) { |
311 SetStateForFunctionError(instance, functions_, true); | 329 SetStateForFunctionError(instance, functions_, true); |
312 } | 330 } |
313 | 331 |
314 EnterResourceCreationNoLock::~EnterResourceCreationNoLock() { | 332 EnterResourceCreationNoLock::~EnterResourceCreationNoLock() { |
315 } | 333 } |
316 | 334 |
317 } // namespace thunk | 335 } // namespace thunk |
318 } // namespace ppapi | 336 } // namespace ppapi |
OLD | NEW |