Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1795)

Unified Diff: content/renderer/pepper/message_channel.cc

Issue 589213003: PPAPI: Never re-enter JavaScript for PostMessage. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Make MessageChannel observer clean up more reliably, guarantee HungPluginFilter stays alive long en… Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « content/renderer/pepper/message_channel.h ('k') | content/renderer/pepper/pepper_hung_plugin_filter.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: content/renderer/pepper/message_channel.cc
diff --git a/content/renderer/pepper/message_channel.cc b/content/renderer/pepper/message_channel.cc
index 4f1de8e2a608fb9b8d05808c763e711621effa9f..490a6fb013fe0c387244c6205cd9d33d751ecbcd 100644
--- a/content/renderer/pepper/message_channel.cc
+++ b/content/renderer/pepper/message_channel.cc
@@ -98,12 +98,15 @@ MessageChannel* MessageChannel::Create(PepperPluginInstanceImpl* instance,
}
MessageChannel::~MessageChannel() {
+ UnregisterSyncMessageStatusObserver();
+
passthrough_object_.Reset();
if (instance_)
instance_->MessageChannelDestroyed();
}
void MessageChannel::InstanceDeleted() {
+ UnregisterSyncMessageStatusObserver();
instance_ = NULL;
}
@@ -131,27 +134,38 @@ void MessageChannel::PostMessageToJavaScript(PP_Var message_data) {
WebSerializedScriptValue serialized_val =
WebSerializedScriptValue::serialize(v8_val);
- if (early_message_queue_state_ != SEND_DIRECTLY) {
+ if (js_message_queue_state_ != SEND_DIRECTLY) {
// We can't just PostTask here; the messages would arrive out of
// order. Instead, we queue them up until we're ready to post
// them.
- early_message_queue_.push_back(serialized_val);
+ js_message_queue_.push_back(serialized_val);
} else {
// The proxy sent an asynchronous message, so the plugin is already
// unblocked. Therefore, there's no need to PostTask.
- DCHECK(early_message_queue_.empty());
+ DCHECK(js_message_queue_.empty());
PostMessageToJavaScriptImpl(serialized_val);
}
}
void MessageChannel::Start() {
- // We PostTask here instead of draining the message queue directly
- // since we haven't finished initializing the PepperWebPluginImpl yet, so
- // the plugin isn't available in the DOM.
- base::MessageLoop::current()->PostTask(
- FROM_HERE,
- base::Bind(&MessageChannel::DrainEarlyMessageQueue,
- weak_ptr_factory_.GetWeakPtr()));
+ DCHECK_EQ(WAITING_TO_START, js_message_queue_state_);
+ DCHECK_EQ(WAITING_TO_START, plugin_message_queue_state_);
+
+ ppapi::proxy::HostDispatcher* dispatcher =
+ ppapi::proxy::HostDispatcher::GetForInstance(instance_->pp_instance());
+ // The dispatcher is NULL for in-process.
+ if (dispatcher) {
+ unregister_observer_callback_ =
+ dispatcher->AddSyncMessageStatusObserver(this);
+ }
+
+ // We can't drain the JS message queue directly since we haven't finished
+ // initializing the PepperWebPluginImpl yet, so the plugin isn't available in
+ // the DOM.
+ DrainJSMessageQueueSoon();
+
+ plugin_message_queue_state_ = SEND_DIRECTLY;
+ DrainCompletedPluginMessages();
}
void MessageChannel::SetPassthroughObject(v8::Handle<v8::Object> passthrough) {
@@ -170,7 +184,9 @@ void MessageChannel::SetReadOnlyProperty(PP_Var key, PP_Var value) {
MessageChannel::MessageChannel(PepperPluginInstanceImpl* instance)
: gin::NamedPropertyInterceptor(instance->GetIsolate(), this),
instance_(instance),
- early_message_queue_state_(QUEUE_MESSAGES),
+ js_message_queue_state_(WAITING_TO_START),
+ blocking_message_depth_(0),
+ plugin_message_queue_state_(WAITING_TO_START),
weak_ptr_factory_(this) {
}
@@ -180,6 +196,18 @@ gin::ObjectTemplateBuilder MessageChannel::GetObjectTemplateBuilder(
.AddNamedPropertyInterceptor();
}
+void MessageChannel::BeginBlockOnSyncMessage() {
+ js_message_queue_state_ = QUEUE_MESSAGES;
+ ++blocking_message_depth_;
+}
+
+void MessageChannel::EndBlockOnSyncMessage() {
+ DCHECK_GT(blocking_message_depth_, 0);
+ --blocking_message_depth_;
+ if (!blocking_message_depth_)
+ DrainJSMessageQueueSoon();
+}
+
v8::Local<v8::Value> MessageChannel::GetNamedProperty(
v8::Isolate* isolate,
const std::string& identifier) {
@@ -279,7 +307,7 @@ void MessageChannel::PostBlockingMessageToNative(gin::Arguments* args) {
NOTREACHED();
}
- if (early_message_queue_state_ == QUEUE_MESSAGES) {
+ if (plugin_message_queue_state_ == WAITING_TO_START) {
try_catch.ThrowException(
"Attempted to call a synchronous method on a plugin that was not "
"yet loaded.");
@@ -392,7 +420,7 @@ void MessageChannel::FromV8ValueComplete(VarConversionResult* result_holder,
void MessageChannel::DrainCompletedPluginMessages() {
DCHECK(instance_);
- if (early_message_queue_state_ == QUEUE_MESSAGES)
+ if (plugin_message_queue_state_ == WAITING_TO_START)
return;
while (!plugin_message_queue_.empty() &&
@@ -410,22 +438,35 @@ void MessageChannel::DrainCompletedPluginMessages() {
}
}
-void MessageChannel::DrainEarlyMessageQueue() {
+void MessageChannel::DrainJSMessageQueue() {
if (!instance_)
return;
- DCHECK(early_message_queue_state_ == QUEUE_MESSAGES);
+ if (js_message_queue_state_ == SEND_DIRECTLY)
+ return;
// Take a reference on the PluginInstance. This is because JavaScript code
// may delete the plugin, which would destroy the PluginInstance and its
// corresponding MessageChannel.
scoped_refptr<PepperPluginInstanceImpl> instance_ref(instance_);
- while (!early_message_queue_.empty()) {
- PostMessageToJavaScriptImpl(early_message_queue_.front());
- early_message_queue_.pop_front();
+ while (!js_message_queue_.empty()) {
+ PostMessageToJavaScriptImpl(js_message_queue_.front());
+ js_message_queue_.pop_front();
}
- early_message_queue_state_ = SEND_DIRECTLY;
+ js_message_queue_state_ = SEND_DIRECTLY;
+}
- DrainCompletedPluginMessages();
+void MessageChannel::DrainJSMessageQueueSoon() {
+ base::MessageLoop::current()->PostTask(
+ FROM_HERE,
+ base::Bind(&MessageChannel::DrainJSMessageQueue,
+ weak_ptr_factory_.GetWeakPtr()));
+}
+
+void MessageChannel::UnregisterSyncMessageStatusObserver() {
+ if (!unregister_observer_callback_.is_null()) {
+ unregister_observer_callback_.Run();
+ unregister_observer_callback_.Reset();
+ }
}
} // namespace content
« no previous file with comments | « content/renderer/pepper/message_channel.h ('k') | content/renderer/pepper/pepper_hung_plugin_filter.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698