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

Side by Side 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, 2 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 unified diff | Download patch
OLDNEW
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 "content/renderer/pepper/message_channel.h" 5 #include "content/renderer/pepper/message_channel.h"
6 6
7 #include <cstdlib> 7 #include <cstdlib>
8 #include <string> 8 #include <string>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
91 MessageChannel* message_channel = new MessageChannel(instance); 91 MessageChannel* message_channel = new MessageChannel(instance);
92 v8::HandleScope handle_scope(instance->GetIsolate()); 92 v8::HandleScope handle_scope(instance->GetIsolate());
93 v8::Context::Scope context_scope(instance->GetMainWorldContext()); 93 v8::Context::Scope context_scope(instance->GetMainWorldContext());
94 gin::Handle<MessageChannel> handle = 94 gin::Handle<MessageChannel> handle =
95 gin::CreateHandle(instance->GetIsolate(), message_channel); 95 gin::CreateHandle(instance->GetIsolate(), message_channel);
96 result->Reset(instance->GetIsolate(), handle.ToV8()->ToObject()); 96 result->Reset(instance->GetIsolate(), handle.ToV8()->ToObject());
97 return message_channel; 97 return message_channel;
98 } 98 }
99 99
100 MessageChannel::~MessageChannel() { 100 MessageChannel::~MessageChannel() {
101 UnregisterSyncMessageStatusObserver();
102
101 passthrough_object_.Reset(); 103 passthrough_object_.Reset();
102 if (instance_) 104 if (instance_)
103 instance_->MessageChannelDestroyed(); 105 instance_->MessageChannelDestroyed();
104 } 106 }
105 107
106 void MessageChannel::InstanceDeleted() { 108 void MessageChannel::InstanceDeleted() {
109 UnregisterSyncMessageStatusObserver();
107 instance_ = NULL; 110 instance_ = NULL;
108 } 111 }
109 112
110 void MessageChannel::PostMessageToJavaScript(PP_Var message_data) { 113 void MessageChannel::PostMessageToJavaScript(PP_Var message_data) {
111 v8::HandleScope scope(v8::Isolate::GetCurrent()); 114 v8::HandleScope scope(v8::Isolate::GetCurrent());
112 115
113 // Because V8 is probably not on the stack for Native->JS calls, we need to 116 // Because V8 is probably not on the stack for Native->JS calls, we need to
114 // enter the appropriate context for the plugin. 117 // enter the appropriate context for the plugin.
115 v8::Local<v8::Context> context = instance_->GetMainWorldContext(); 118 v8::Local<v8::Context> context = instance_->GetMainWorldContext();
116 if (context.IsEmpty()) 119 if (context.IsEmpty())
117 return; 120 return;
118 121
119 v8::Context::Scope context_scope(context); 122 v8::Context::Scope context_scope(context);
120 123
121 v8::Handle<v8::Value> v8_val; 124 v8::Handle<v8::Value> v8_val;
122 if (!V8VarConverter(instance_->pp_instance()) 125 if (!V8VarConverter(instance_->pp_instance())
123 .ToV8Value(message_data, context, &v8_val)) { 126 .ToV8Value(message_data, context, &v8_val)) {
124 PpapiGlobals::Get()->LogWithSource(instance_->pp_instance(), 127 PpapiGlobals::Get()->LogWithSource(instance_->pp_instance(),
125 PP_LOGLEVEL_ERROR, 128 PP_LOGLEVEL_ERROR,
126 std::string(), 129 std::string(),
127 kVarToV8ConversionError); 130 kVarToV8ConversionError);
128 return; 131 return;
129 } 132 }
130 133
131 WebSerializedScriptValue serialized_val = 134 WebSerializedScriptValue serialized_val =
132 WebSerializedScriptValue::serialize(v8_val); 135 WebSerializedScriptValue::serialize(v8_val);
133 136
134 if (early_message_queue_state_ != SEND_DIRECTLY) { 137 if (js_message_queue_state_ != SEND_DIRECTLY) {
135 // We can't just PostTask here; the messages would arrive out of 138 // We can't just PostTask here; the messages would arrive out of
136 // order. Instead, we queue them up until we're ready to post 139 // order. Instead, we queue them up until we're ready to post
137 // them. 140 // them.
138 early_message_queue_.push_back(serialized_val); 141 js_message_queue_.push_back(serialized_val);
139 } else { 142 } else {
140 // The proxy sent an asynchronous message, so the plugin is already 143 // The proxy sent an asynchronous message, so the plugin is already
141 // unblocked. Therefore, there's no need to PostTask. 144 // unblocked. Therefore, there's no need to PostTask.
142 DCHECK(early_message_queue_.empty()); 145 DCHECK(js_message_queue_.empty());
143 PostMessageToJavaScriptImpl(serialized_val); 146 PostMessageToJavaScriptImpl(serialized_val);
144 } 147 }
145 } 148 }
146 149
147 void MessageChannel::Start() { 150 void MessageChannel::Start() {
148 // We PostTask here instead of draining the message queue directly 151 DCHECK_EQ(WAITING_TO_START, js_message_queue_state_);
149 // since we haven't finished initializing the PepperWebPluginImpl yet, so 152 DCHECK_EQ(WAITING_TO_START, plugin_message_queue_state_);
150 // the plugin isn't available in the DOM. 153
151 base::MessageLoop::current()->PostTask( 154 ppapi::proxy::HostDispatcher* dispatcher =
152 FROM_HERE, 155 ppapi::proxy::HostDispatcher::GetForInstance(instance_->pp_instance());
153 base::Bind(&MessageChannel::DrainEarlyMessageQueue, 156 // The dispatcher is NULL for in-process.
154 weak_ptr_factory_.GetWeakPtr())); 157 if (dispatcher) {
158 unregister_observer_callback_ =
159 dispatcher->AddSyncMessageStatusObserver(this);
160 }
161
162 // We can't drain the JS message queue directly since we haven't finished
163 // initializing the PepperWebPluginImpl yet, so the plugin isn't available in
164 // the DOM.
165 DrainJSMessageQueueSoon();
166
167 plugin_message_queue_state_ = SEND_DIRECTLY;
168 DrainCompletedPluginMessages();
155 } 169 }
156 170
157 void MessageChannel::SetPassthroughObject(v8::Handle<v8::Object> passthrough) { 171 void MessageChannel::SetPassthroughObject(v8::Handle<v8::Object> passthrough) {
158 passthrough_object_.Reset(instance_->GetIsolate(), passthrough); 172 passthrough_object_.Reset(instance_->GetIsolate(), passthrough);
159 } 173 }
160 174
161 void MessageChannel::SetReadOnlyProperty(PP_Var key, PP_Var value) { 175 void MessageChannel::SetReadOnlyProperty(PP_Var key, PP_Var value) {
162 StringVar* key_string = StringVar::FromPPVar(key); 176 StringVar* key_string = StringVar::FromPPVar(key);
163 if (key_string) { 177 if (key_string) {
164 internal_named_properties_[key_string->value()] = ScopedPPVar(value); 178 internal_named_properties_[key_string->value()] = ScopedPPVar(value);
165 } else { 179 } else {
166 NOTREACHED(); 180 NOTREACHED();
167 } 181 }
168 } 182 }
169 183
170 MessageChannel::MessageChannel(PepperPluginInstanceImpl* instance) 184 MessageChannel::MessageChannel(PepperPluginInstanceImpl* instance)
171 : gin::NamedPropertyInterceptor(instance->GetIsolate(), this), 185 : gin::NamedPropertyInterceptor(instance->GetIsolate(), this),
172 instance_(instance), 186 instance_(instance),
173 early_message_queue_state_(QUEUE_MESSAGES), 187 js_message_queue_state_(WAITING_TO_START),
188 blocking_message_depth_(0),
189 plugin_message_queue_state_(WAITING_TO_START),
174 weak_ptr_factory_(this) { 190 weak_ptr_factory_(this) {
175 } 191 }
176 192
177 gin::ObjectTemplateBuilder MessageChannel::GetObjectTemplateBuilder( 193 gin::ObjectTemplateBuilder MessageChannel::GetObjectTemplateBuilder(
178 v8::Isolate* isolate) { 194 v8::Isolate* isolate) {
179 return Wrappable<MessageChannel>::GetObjectTemplateBuilder(isolate) 195 return Wrappable<MessageChannel>::GetObjectTemplateBuilder(isolate)
180 .AddNamedPropertyInterceptor(); 196 .AddNamedPropertyInterceptor();
181 } 197 }
182 198
199 void MessageChannel::BeginBlockOnSyncMessage() {
200 js_message_queue_state_ = QUEUE_MESSAGES;
201 ++blocking_message_depth_;
202 }
203
204 void MessageChannel::EndBlockOnSyncMessage() {
205 DCHECK_GT(blocking_message_depth_, 0);
206 --blocking_message_depth_;
207 if (!blocking_message_depth_)
208 DrainJSMessageQueueSoon();
209 }
210
183 v8::Local<v8::Value> MessageChannel::GetNamedProperty( 211 v8::Local<v8::Value> MessageChannel::GetNamedProperty(
184 v8::Isolate* isolate, 212 v8::Isolate* isolate,
185 const std::string& identifier) { 213 const std::string& identifier) {
186 if (!instance_) 214 if (!instance_)
187 return v8::Local<v8::Value>(); 215 return v8::Local<v8::Value>();
188 216
189 PepperTryCatchV8 try_catch(instance_, V8VarConverter::kDisallowObjectVars, 217 PepperTryCatchV8 try_catch(instance_, V8VarConverter::kDisallowObjectVars,
190 isolate); 218 isolate);
191 if (identifier == kPostMessage) { 219 if (identifier == kPostMessage) {
192 return gin::CreateFunctionTemplate(isolate, 220 return gin::CreateFunctionTemplate(isolate,
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
272 try_catch.ThrowException( 300 try_catch.ThrowException(
273 "postMessageAndAwaitResponse requires one argument"); 301 "postMessageAndAwaitResponse requires one argument");
274 return; 302 return;
275 } 303 }
276 304
277 v8::Handle<v8::Value> message_data; 305 v8::Handle<v8::Value> message_data;
278 if (!args->GetNext(&message_data)) { 306 if (!args->GetNext(&message_data)) {
279 NOTREACHED(); 307 NOTREACHED();
280 } 308 }
281 309
282 if (early_message_queue_state_ == QUEUE_MESSAGES) { 310 if (plugin_message_queue_state_ == WAITING_TO_START) {
283 try_catch.ThrowException( 311 try_catch.ThrowException(
284 "Attempted to call a synchronous method on a plugin that was not " 312 "Attempted to call a synchronous method on a plugin that was not "
285 "yet loaded."); 313 "yet loaded.");
286 return; 314 return;
287 } 315 }
288 316
289 // If the queue of messages to the plugin is non-empty, we're still waiting on 317 // If the queue of messages to the plugin is non-empty, we're still waiting on
290 // pending Var conversions. This means at some point in the past, JavaScript 318 // pending Var conversions. This means at some point in the past, JavaScript
291 // called postMessage (the async one) and passed us something with a browser- 319 // called postMessage (the async one) and passed us something with a browser-
292 // side host (e.g., FileSystem) and we haven't gotten a response from the 320 // side host (e.g., FileSystem) and we haven't gotten a response from the
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
385 const ScopedPPVar& result, 413 const ScopedPPVar& result,
386 bool success) { 414 bool success) {
387 if (!instance_) 415 if (!instance_)
388 return; 416 return;
389 result_holder->ConversionCompleted(result, success); 417 result_holder->ConversionCompleted(result, success);
390 DrainCompletedPluginMessages(); 418 DrainCompletedPluginMessages();
391 } 419 }
392 420
393 void MessageChannel::DrainCompletedPluginMessages() { 421 void MessageChannel::DrainCompletedPluginMessages() {
394 DCHECK(instance_); 422 DCHECK(instance_);
395 if (early_message_queue_state_ == QUEUE_MESSAGES) 423 if (plugin_message_queue_state_ == WAITING_TO_START)
396 return; 424 return;
397 425
398 while (!plugin_message_queue_.empty() && 426 while (!plugin_message_queue_.empty() &&
399 plugin_message_queue_.front().conversion_completed()) { 427 plugin_message_queue_.front().conversion_completed()) {
400 const VarConversionResult& front = plugin_message_queue_.front(); 428 const VarConversionResult& front = plugin_message_queue_.front();
401 if (front.success()) { 429 if (front.success()) {
402 instance_->HandleMessage(front.var()); 430 instance_->HandleMessage(front.var());
403 } else { 431 } else {
404 PpapiGlobals::Get()->LogWithSource(instance()->pp_instance(), 432 PpapiGlobals::Get()->LogWithSource(instance()->pp_instance(),
405 PP_LOGLEVEL_ERROR, 433 PP_LOGLEVEL_ERROR,
406 std::string(), 434 std::string(),
407 kV8ToVarConversionError); 435 kV8ToVarConversionError);
408 } 436 }
409 plugin_message_queue_.pop_front(); 437 plugin_message_queue_.pop_front();
410 } 438 }
411 } 439 }
412 440
413 void MessageChannel::DrainEarlyMessageQueue() { 441 void MessageChannel::DrainJSMessageQueue() {
414 if (!instance_) 442 if (!instance_)
415 return; 443 return;
416 DCHECK(early_message_queue_state_ == QUEUE_MESSAGES); 444 if (js_message_queue_state_ == SEND_DIRECTLY)
445 return;
417 446
418 // Take a reference on the PluginInstance. This is because JavaScript code 447 // Take a reference on the PluginInstance. This is because JavaScript code
419 // may delete the plugin, which would destroy the PluginInstance and its 448 // may delete the plugin, which would destroy the PluginInstance and its
420 // corresponding MessageChannel. 449 // corresponding MessageChannel.
421 scoped_refptr<PepperPluginInstanceImpl> instance_ref(instance_); 450 scoped_refptr<PepperPluginInstanceImpl> instance_ref(instance_);
422 while (!early_message_queue_.empty()) { 451 while (!js_message_queue_.empty()) {
423 PostMessageToJavaScriptImpl(early_message_queue_.front()); 452 PostMessageToJavaScriptImpl(js_message_queue_.front());
424 early_message_queue_.pop_front(); 453 js_message_queue_.pop_front();
425 } 454 }
426 early_message_queue_state_ = SEND_DIRECTLY; 455 js_message_queue_state_ = SEND_DIRECTLY;
456 }
427 457
428 DrainCompletedPluginMessages(); 458 void MessageChannel::DrainJSMessageQueueSoon() {
459 base::MessageLoop::current()->PostTask(
460 FROM_HERE,
461 base::Bind(&MessageChannel::DrainJSMessageQueue,
462 weak_ptr_factory_.GetWeakPtr()));
463 }
464
465 void MessageChannel::UnregisterSyncMessageStatusObserver() {
466 if (!unregister_observer_callback_.is_null()) {
467 unregister_observer_callback_.Run();
468 unregister_observer_callback_.Reset();
469 }
429 } 470 }
430 471
431 } // namespace content 472 } // namespace content
OLDNEW
« 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