Chromium Code Reviews| Index: webkit/plugins/ppapi/message_channel.cc |
| diff --git a/webkit/plugins/ppapi/message_channel.cc b/webkit/plugins/ppapi/message_channel.cc |
| index caee008cfd14a9c74ae594029d8de13febcc81d9..46775c0346e6931a90ee4295abb99cc910d42da6 100644 |
| --- a/webkit/plugins/ppapi/message_channel.cc |
| +++ b/webkit/plugins/ppapi/message_channel.cc |
| @@ -12,15 +12,21 @@ |
| #include "base/message_loop.h" |
| #include "third_party/WebKit/Source/WebKit/chromium/public/WebBindings.h" |
| #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
| +#include "third_party/WebKit/Source/WebKit/chromium/public/WebDOMMessageEvent.h" |
| #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" |
| #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| +#include "third_party/WebKit/Source/WebKit/chromium/public/WebNode.h" |
| #include "third_party/WebKit/Source/WebKit/chromium/public/WebPluginContainer.h" |
| +#include "third_party/WebKit/Source/WebKit/chromium/public/WebSerializedScriptValue.h" |
| #include "ppapi/shared_impl/var.h" |
| +#include "v8/include/v8.h" |
| #include "webkit/plugins/ppapi/npapi_glue.h" |
| #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" |
| using ppapi::StringVar; |
| using WebKit::WebBindings; |
| +using WebKit::WebDOMMessageEvent; |
| +using WebKit::WebSerializedScriptValue; |
| namespace webkit { |
| @@ -286,8 +292,6 @@ MessageChannel::MessageChannel(PluginInstance* instance) |
| passthrough_object_(NULL), |
| np_object_(NULL), |
| ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { |
| - VOID_TO_NPVARIANT(onmessage_invoker_); |
| - |
| // Now create an NPObject for receiving calls to postMessage. This sets the |
| // reference count to 1. We release it in the destructor. |
| NPObject* obj = WebBindings::createObject(NULL, &message_channel_class); |
| @@ -296,93 +300,51 @@ MessageChannel::MessageChannel(PluginInstance* instance) |
| np_object_->message_channel = this; |
| } |
| -bool MessageChannel::EvaluateOnMessageInvoker() { |
| - // If we've already evaluated the function, just return. |
| - if (NPVARIANT_IS_OBJECT(onmessage_invoker_)) |
| - return true; |
| - |
| - // This is the javascript code that we invoke to create and dispatch a |
| - // message event. |
| - const char invoke_onmessage_js[] = |
| - "(function(window, module_instance, message_data) {" |
| - " if (module_instance) {" |
| - " var message_event = new MessageEvent('message', " |
| - " { data: message_data });" |
| - " module_instance.dispatchEvent(message_event);" |
| - " }" |
| - "})"; |
| - // Note that we purposely omit |origin| and |source|. The |origin| is only |
| - // specified for cross-document and server-sent messages, while |source| is |
| - // only specified for cross-document messages: |
| - // http://www.whatwg.org/specs/web-apps/current-work/multipage/comms.html |
| - // This currently behaves like Web Workers. On Firefox, Chrome, and Safari |
| - // at least, postMessage on Workers does not provide the origin or source. |
| - // TODO(dmichael): Add origin if we change to a more iframe-like origin |
| - // policy (see crbug.com/81537) |
| - |
| - NPString function_string = { invoke_onmessage_js, |
| - sizeof(invoke_onmessage_js)-1 }; |
| - // Get the current frame to pass to the evaluate function. |
| - WebKit::WebFrame* frame = |
| - instance_->container()->element().document().frame(); |
| - // Evaluate the function and obtain an NPVariant pointing to it. |
| - if (!WebBindings::evaluate(NULL, frame->windowObject(), &function_string, |
| - &onmessage_invoker_)) { |
| - // If it fails, do nothing. |
| - return false; |
| - } |
| - DCHECK(NPVARIANT_IS_OBJECT(onmessage_invoker_)); |
| - return true; |
| -} |
| - |
| void MessageChannel::PostMessageToJavaScript(PP_Var message_data) { |
| - // Make a copy of the message data for the Task we will run. |
| - PP_Var var_copy(CopyPPVar(message_data)); |
| + // Serialize the message data. |
| + NPVariant message_npvar; |
| + // TODO(dmichael): It may be helpful to use webkit/glue/cppvariant instead of |
| + // using NPVariant directly. |
| + if (!PPVarToNPVariantNoCopy(message_data, &message_npvar)) |
| + return; |
| + v8::HandleScope scope; |
| + v8::Handle<v8::Value> v8_val = WebBindings::toV8Value(&message_npvar); |
| + |
| + WebSerializedScriptValue serialized_val = |
| + 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
|
| MessageLoop::current()->PostTask( |
| FROM_HERE, |
| base::Bind(&MessageChannel::PostMessageToJavaScriptImpl, |
| weak_ptr_factory_.GetWeakPtr(), |
| - var_copy)); |
| + serialized_val)); |
| } |
| -void MessageChannel::PostMessageToJavaScriptImpl(PP_Var message_data) { |
| - // Make sure we have our function for invoking onmessage on JavaScript. |
| - bool success = EvaluateOnMessageInvoker(); |
| - DCHECK(success); |
| - if (!success) |
| - return; |
| - |
| +void MessageChannel::PostMessageToJavaScriptImpl( |
| + const WebSerializedScriptValue& message_data) { |
| DCHECK(instance_); |
| - NPVariant result_var; |
| - VOID_TO_NPVARIANT(result_var); |
| - NPVariant npvariant_args[3]; |
| - // Get the frame so we can get the window object. |
| - WebKit::WebFrame* frame = |
| - instance_->container()->element().document().frame(); |
| - if (!frame) |
| - return; |
| - |
| - OBJECT_TO_NPVARIANT(frame->windowObject(), npvariant_args[0]); |
| - OBJECT_TO_NPVARIANT(instance_->container()->scriptableObjectForElement(), |
| - npvariant_args[1]); |
| - // Convert message to an NPVariant without copying. At this point, the data |
| - // has already been copied. |
| - if (!PPVarToNPVariantNoCopy(message_data, &npvariant_args[2])) { |
| - // We couldn't create an NPVariant, so we can't invoke the method. Thus, |
| - // WebBindings::invokeDefault does not take ownership of these variants, so |
| - // we must release our references to them explicitly. |
| - WebBindings::releaseVariantValue(&npvariant_args[0]); |
| - WebBindings::releaseVariantValue(&npvariant_args[1]); |
| - return; |
| - } |
| - |
| - WebBindings::invokeDefault(NULL, |
| - NPVARIANT_TO_OBJECT(onmessage_invoker_), |
| - npvariant_args, |
| - sizeof(npvariant_args)/sizeof(*npvariant_args), |
| - &result_var); |
| + const WebKit::WebElement& element = instance_->container()->element(); |
| + |
| + WebKit::WebDOMEvent event = element.document().createEvent("MessageEvent"); |
| + WebDOMMessageEvent& msg_event(static_cast<WebDOMMessageEvent&>(event)); |
| + msg_event.initMessageEvent("message", // type |
| + false, // canBubble |
| + false, // cancelable |
| + message_data, // data |
| + "", // origin [*] |
| + NULL, // source [*] |
| + ""); // lastEventId |
| + // [*] Note that the |origin| is only specified for cross-document and server- |
| + // sent messages, while |source| is only specified for cross-document |
| + // messages: |
| + // http://www.whatwg.org/specs/web-apps/current-work/multipage/comms.html |
| + // This currently behaves like Web Workers. On Firefox, Chrome, and Safari |
| + // at least, postMessage on Workers does not provide the origin or source. |
| + // TODO(dmichael): Add origin if we change to a more iframe-like origin |
| + // policy (see crbug.com/81537) |
| + |
| + instance_->container()->element().dispatchEvent(msg_event); |
| } |
| void MessageChannel::PostMessageToNative(PP_Var message_data) { |
| @@ -403,7 +365,6 @@ MessageChannel::~MessageChannel() { |
| WebBindings::releaseObject(np_object_); |
| if (passthrough_object_) |
| WebBindings::releaseObject(passthrough_object_); |
| - WebBindings::releaseVariantValue(&onmessage_invoker_); |
| } |
| void MessageChannel::SetPassthroughObject(NPObject* passthrough) { |