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

Side by Side Diff: webkit/plugins/ppapi/message_channel.cc

Issue 8437093: Make PostMessageToJavaScript use new WebKit API instead of script. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add nefarious code to test_case.html to test the change. Created 9 years, 1 month 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 | Annotate | Revision Log
« no previous file with comments | « webkit/plugins/ppapi/message_channel.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "webkit/plugins/ppapi/message_channel.h" 5 #include "webkit/plugins/ppapi/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"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/message_loop.h" 12 #include "base/message_loop.h"
13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebBindings.h" 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebBindings.h"
14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDOMMessageEvent.h"
15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h"
16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNode.h"
17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPluginContainer.h" 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPluginContainer.h"
20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSerializedScriptVa lue.h"
18 #include "ppapi/shared_impl/var.h" 21 #include "ppapi/shared_impl/var.h"
22 #include "v8/include/v8.h"
19 #include "webkit/plugins/ppapi/npapi_glue.h" 23 #include "webkit/plugins/ppapi/npapi_glue.h"
20 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" 24 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h"
21 25
22 using ppapi::StringVar; 26 using ppapi::StringVar;
23 using WebKit::WebBindings; 27 using WebKit::WebBindings;
28 using WebKit::WebDOMMessageEvent;
29 using WebKit::WebSerializedScriptValue;
24 30
25 namespace webkit { 31 namespace webkit {
26 32
27 namespace ppapi { 33 namespace ppapi {
28 34
29 namespace { 35 namespace {
30 36
31 const char kPostMessage[] = "postMessage"; 37 const char kPostMessage[] = "postMessage";
32 38
33 // Helper function to get the MessageChannel that is associated with an 39 // Helper function to get the MessageChannel that is associated with an
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after
279 : message_channel(NULL) { 285 : message_channel(NULL) {
280 } 286 }
281 287
282 MessageChannel::MessageChannelNPObject::~MessageChannelNPObject() {} 288 MessageChannel::MessageChannelNPObject::~MessageChannelNPObject() {}
283 289
284 MessageChannel::MessageChannel(PluginInstance* instance) 290 MessageChannel::MessageChannel(PluginInstance* instance)
285 : instance_(instance), 291 : instance_(instance),
286 passthrough_object_(NULL), 292 passthrough_object_(NULL),
287 np_object_(NULL), 293 np_object_(NULL),
288 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { 294 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) {
289 VOID_TO_NPVARIANT(onmessage_invoker_);
290
291 // Now create an NPObject for receiving calls to postMessage. This sets the 295 // Now create an NPObject for receiving calls to postMessage. This sets the
292 // reference count to 1. We release it in the destructor. 296 // reference count to 1. We release it in the destructor.
293 NPObject* obj = WebBindings::createObject(NULL, &message_channel_class); 297 NPObject* obj = WebBindings::createObject(NULL, &message_channel_class);
294 DCHECK(obj); 298 DCHECK(obj);
295 np_object_ = static_cast<MessageChannel::MessageChannelNPObject*>(obj); 299 np_object_ = static_cast<MessageChannel::MessageChannelNPObject*>(obj);
296 np_object_->message_channel = this; 300 np_object_->message_channel = this;
297 } 301 }
298 302
299 bool MessageChannel::EvaluateOnMessageInvoker() { 303 void MessageChannel::PostMessageToJavaScript(PP_Var message_data) {
300 // If we've already evaluated the function, just return. 304 // Serialize the message data.
301 if (NPVARIANT_IS_OBJECT(onmessage_invoker_)) 305 NPVariant message_npvar;
302 return true; 306 // TODO(dmichael): It may be helpful to use webkit/glue/cppvariant instead of
307 // using NPVariant directly.
308 if (!PPVarToNPVariantNoCopy(message_data, &message_npvar))
309 return;
310 v8::HandleScope scope;
311 v8::Handle<v8::Value> v8_val = WebBindings::toV8Value(&message_npvar);
303 312
304 // This is the javascript code that we invoke to create and dispatch a 313 WebSerializedScriptValue serialized_val =
305 // message event. 314 WebSerializedScriptValue::serialize(v8_val);
viettrungluu 2011/11/04 20:29:05 I don't know much about this, but having to make a
dmichael (off chromium) 2011/11/04 20:50:33 It's only kind of arbitrary. Currently it can be a
306 const char invoke_onmessage_js[] =
307 "(function(window, module_instance, message_data) {"
308 " if (module_instance) {"
309 " var message_event = new MessageEvent('message', "
310 " { data: message_data });"
311 " module_instance.dispatchEvent(message_event);"
312 " }"
313 "})";
314 // Note that we purposely omit |origin| and |source|. The |origin| is only
315 // specified for cross-document and server-sent messages, while |source| is
316 // only specified for cross-document messages:
317 // http://www.whatwg.org/specs/web-apps/current-work/multipage/comms.html
318 // This currently behaves like Web Workers. On Firefox, Chrome, and Safari
319 // at least, postMessage on Workers does not provide the origin or source.
320 // TODO(dmichael): Add origin if we change to a more iframe-like origin
321 // policy (see crbug.com/81537)
322
323 NPString function_string = { invoke_onmessage_js,
324 sizeof(invoke_onmessage_js)-1 };
325 // Get the current frame to pass to the evaluate function.
326 WebKit::WebFrame* frame =
327 instance_->container()->element().document().frame();
328 // Evaluate the function and obtain an NPVariant pointing to it.
329 if (!WebBindings::evaluate(NULL, frame->windowObject(), &function_string,
330 &onmessage_invoker_)) {
331 // If it fails, do nothing.
332 return false;
333 }
334 DCHECK(NPVARIANT_IS_OBJECT(onmessage_invoker_));
335 return true;
336 }
337
338 void MessageChannel::PostMessageToJavaScript(PP_Var message_data) {
339 // Make a copy of the message data for the Task we will run.
340 PP_Var var_copy(CopyPPVar(message_data));
341 315
342 MessageLoop::current()->PostTask( 316 MessageLoop::current()->PostTask(
343 FROM_HERE, 317 FROM_HERE,
344 base::Bind(&MessageChannel::PostMessageToJavaScriptImpl, 318 base::Bind(&MessageChannel::PostMessageToJavaScriptImpl,
345 weak_ptr_factory_.GetWeakPtr(), 319 weak_ptr_factory_.GetWeakPtr(),
346 var_copy)); 320 serialized_val));
347 } 321 }
348 322
349 void MessageChannel::PostMessageToJavaScriptImpl(PP_Var message_data) { 323 void MessageChannel::PostMessageToJavaScriptImpl(
350 // Make sure we have our function for invoking onmessage on JavaScript. 324 const WebSerializedScriptValue& message_data) {
351 bool success = EvaluateOnMessageInvoker();
352 DCHECK(success);
353 if (!success)
354 return;
355
356 DCHECK(instance_); 325 DCHECK(instance_);
357 326
358 NPVariant result_var; 327 const WebKit::WebElement& element = instance_->container()->element();
359 VOID_TO_NPVARIANT(result_var);
360 NPVariant npvariant_args[3];
361 // Get the frame so we can get the window object.
362 WebKit::WebFrame* frame =
363 instance_->container()->element().document().frame();
364 if (!frame)
365 return;
366 328
367 OBJECT_TO_NPVARIANT(frame->windowObject(), npvariant_args[0]); 329 WebKit::WebDOMEvent event = element.document().createEvent("MessageEvent");
368 OBJECT_TO_NPVARIANT(instance_->container()->scriptableObjectForElement(), 330 WebDOMMessageEvent& msg_event(static_cast<WebDOMMessageEvent&>(event));
369 npvariant_args[1]); 331 msg_event.initMessageEvent("message", // type
370 // Convert message to an NPVariant without copying. At this point, the data 332 false, // canBubble
371 // has already been copied. 333 false, // cancelable
372 if (!PPVarToNPVariantNoCopy(message_data, &npvariant_args[2])) { 334 message_data, // data
373 // We couldn't create an NPVariant, so we can't invoke the method. Thus, 335 "", // origin [*]
374 // WebBindings::invokeDefault does not take ownership of these variants, so 336 NULL, // source [*]
375 // we must release our references to them explicitly. 337 ""); // lastEventId
376 WebBindings::releaseVariantValue(&npvariant_args[0]); 338 // [*] Note that the |origin| is only specified for cross-document and server-
377 WebBindings::releaseVariantValue(&npvariant_args[1]); 339 // sent messages, while |source| is only specified for cross-document
378 return; 340 // messages:
379 } 341 // http://www.whatwg.org/specs/web-apps/current-work/multipage/comms.html
342 // This currently behaves like Web Workers. On Firefox, Chrome, and Safari
343 // at least, postMessage on Workers does not provide the origin or source.
344 // TODO(dmichael): Add origin if we change to a more iframe-like origin
345 // policy (see crbug.com/81537)
380 346
381 WebBindings::invokeDefault(NULL, 347 instance_->container()->element().dispatchEvent(msg_event);
382 NPVARIANT_TO_OBJECT(onmessage_invoker_),
383 npvariant_args,
384 sizeof(npvariant_args)/sizeof(*npvariant_args),
385 &result_var);
386 } 348 }
387 349
388 void MessageChannel::PostMessageToNative(PP_Var message_data) { 350 void MessageChannel::PostMessageToNative(PP_Var message_data) {
389 // Make a copy of the message data for the Task we will run. 351 // Make a copy of the message data for the Task we will run.
390 PP_Var var_copy(CopyPPVar(message_data)); 352 PP_Var var_copy(CopyPPVar(message_data));
391 353
392 MessageLoop::current()->PostTask(FROM_HERE, 354 MessageLoop::current()->PostTask(FROM_HERE,
393 base::Bind(&MessageChannel::PostMessageToNativeImpl, 355 base::Bind(&MessageChannel::PostMessageToNativeImpl,
394 weak_ptr_factory_.GetWeakPtr(), 356 weak_ptr_factory_.GetWeakPtr(),
395 var_copy)); 357 var_copy));
396 } 358 }
397 359
398 void MessageChannel::PostMessageToNativeImpl(PP_Var message_data) { 360 void MessageChannel::PostMessageToNativeImpl(PP_Var message_data) {
399 instance_->HandleMessage(message_data); 361 instance_->HandleMessage(message_data);
400 } 362 }
401 363
402 MessageChannel::~MessageChannel() { 364 MessageChannel::~MessageChannel() {
403 WebBindings::releaseObject(np_object_); 365 WebBindings::releaseObject(np_object_);
404 if (passthrough_object_) 366 if (passthrough_object_)
405 WebBindings::releaseObject(passthrough_object_); 367 WebBindings::releaseObject(passthrough_object_);
406 WebBindings::releaseVariantValue(&onmessage_invoker_);
407 } 368 }
408 369
409 void MessageChannel::SetPassthroughObject(NPObject* passthrough) { 370 void MessageChannel::SetPassthroughObject(NPObject* passthrough) {
410 // Retain the passthrough object; We need to ensure it lives as long as this 371 // Retain the passthrough object; We need to ensure it lives as long as this
411 // MessageChannel. 372 // MessageChannel.
412 WebBindings::retainObject(passthrough); 373 WebBindings::retainObject(passthrough);
413 374
414 // If we had a passthrough set already, release it. Note that we retain the 375 // If we had a passthrough set already, release it. Note that we retain the
415 // incoming passthrough object first, so that we behave correctly if anyone 376 // incoming passthrough object first, so that we behave correctly if anyone
416 // invokes: 377 // invokes:
417 // SetPassthroughObject(passthrough_object()); 378 // SetPassthroughObject(passthrough_object());
418 if (passthrough_object_) 379 if (passthrough_object_)
419 WebBindings::releaseObject(passthrough_object_); 380 WebBindings::releaseObject(passthrough_object_);
420 381
421 passthrough_object_ = passthrough; 382 passthrough_object_ = passthrough;
422 } 383 }
423 384
424 } // namespace ppapi 385 } // namespace ppapi
425 } // namespace webkit 386 } // namespace webkit
426 387
OLDNEW
« no previous file with comments | « webkit/plugins/ppapi/message_channel.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698