Index: content/renderer/pepper/message_channel.cc |
diff --git a/content/renderer/pepper/message_channel.cc b/content/renderer/pepper/message_channel.cc |
index d5bbef9f0c2a55bb2efdb4b066d700b9d0b31abb..df56988af09c8de1b2882f0e17abd4d835d72c1c 100644 |
--- a/content/renderer/pepper/message_channel.cc |
+++ b/content/renderer/pepper/message_channel.cc |
@@ -110,6 +110,12 @@ MessageChannel::~MessageChannel() { |
} |
void MessageChannel::InstanceDeleted() { |
+ ppapi::proxy::HostDispatcher* dispatcher = |
+ ppapi::proxy::HostDispatcher::GetForInstance(instance_->pp_instance()); |
+ // The dispatcher is NULL for in-process. |
+ if (dispatcher) |
+ dispatcher->RemoveSyncMessageStatusObserver(this); |
+ |
instance_ = NULL; |
} |
@@ -137,27 +143,36 @@ 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 |
+ 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) |
+ dispatcher->AddSyncMessageStatusObserver(this); |
+ |
+ // We can't drain the JS message queue directly |
raymes
2014/09/23 02:57:09
nit: fill the 80 chars
raymes
2014/09/24 00:05:47
I think you missed this comment, but I'm ok to lan
|
// 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())); |
+ DrainJSMessageQueueSoon(); |
+ |
+ plugin_message_queue_state_ = SEND_DIRECTLY; |
+ DrainCompletedPluginMessages(); |
} |
void MessageChannel::SetPassthroughObject(v8::Handle<v8::Object> passthrough) { |
@@ -176,7 +191,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) { |
} |
@@ -186,6 +203,17 @@ gin::ObjectTemplateBuilder MessageChannel::GetObjectTemplateBuilder( |
.AddNamedPropertyInterceptor(); |
} |
+void MessageChannel::BeginBlockOnSyncMessage() { |
+ js_message_queue_state_ = QUEUE_MESSAGES; |
+ ++blocking_message_depth_; |
+} |
+ |
+void MessageChannel::EndBlockOnSyncMessage() { |
+ --blocking_message_depth_; |
raymes
2014/09/23 02:57:09
is it worth adding a DCHECK here that we don't go
raymes
2014/09/24 00:05:48
This too, but it's not crucial
|
+ if (!blocking_message_depth_) |
+ DrainJSMessageQueueSoon(); |
+} |
+ |
v8::Local<v8::Value> MessageChannel::GetNamedProperty( |
v8::Isolate* isolate, |
const std::string& identifier) { |
@@ -286,7 +314,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."); |
@@ -399,7 +427,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() && |
@@ -417,22 +445,28 @@ 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())); |
} |
} // namespace content |