| OLD | NEW |
| 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 "ppapi/shared_impl/ppapi_globals.h" |
| 13 #include "ppapi/shared_impl/var.h" | 14 #include "ppapi/shared_impl/var.h" |
| 15 #include "ppapi/shared_impl/var_tracker.h" |
| 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebBindings.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebBindings.h" |
| 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
| 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDOMMessageEvent.h" | 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDOMMessageEvent.h" |
| 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" | 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" |
| 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNode.h" | 21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNode.h" |
| 20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPluginContainer.h" | 22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPluginContainer.h" |
| 21 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebSerialize
dScriptValue.h" | 23 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebSerialize
dScriptValue.h" |
| 22 #include "v8/include/v8.h" | 24 #include "v8/include/v8.h" |
| 25 #include "webkit/plugins/ppapi/host_array_buffer_var.h" |
| 23 #include "webkit/plugins/ppapi/npapi_glue.h" | 26 #include "webkit/plugins/ppapi/npapi_glue.h" |
| 24 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" | 27 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" |
| 25 | 28 |
| 29 using ppapi::ArrayBufferVar; |
| 30 using ppapi::PpapiGlobals; |
| 26 using ppapi::StringVar; | 31 using ppapi::StringVar; |
| 27 using WebKit::WebBindings; | 32 using WebKit::WebBindings; |
| 28 using WebKit::WebElement; | 33 using WebKit::WebElement; |
| 29 using WebKit::WebDOMEvent; | 34 using WebKit::WebDOMEvent; |
| 30 using WebKit::WebDOMMessageEvent; | 35 using WebKit::WebDOMMessageEvent; |
| 31 using WebKit::WebPluginContainer; | 36 using WebKit::WebPluginContainer; |
| 32 using WebKit::WebSerializedScriptValue; | 37 using WebKit::WebSerializedScriptValue; |
| 33 | 38 |
| 34 namespace webkit { | 39 namespace webkit { |
| 35 | 40 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 result->Clear(); | 82 result->Clear(); |
| 78 return false; | 83 return false; |
| 79 } | 84 } |
| 80 const std::string& value = string->value(); | 85 const std::string& value = string->value(); |
| 81 // TODO(dmichael): We should consider caching the V8 string in the host- | 86 // TODO(dmichael): We should consider caching the V8 string in the host- |
| 82 // side StringVar, so that we only have to convert/copy once if a | 87 // side StringVar, so that we only have to convert/copy once if a |
| 83 // string is sent more than once. | 88 // string is sent more than once. |
| 84 *result = v8::String::New(value.c_str(), value.size()); | 89 *result = v8::String::New(value.c_str(), value.size()); |
| 85 break; | 90 break; |
| 86 } | 91 } |
| 92 case PP_VARTYPE_ARRAY_BUFFER: { |
| 93 ArrayBufferVar* buffer = ArrayBufferVar::FromPPVar(var); |
| 94 if (!buffer) { |
| 95 result->Clear(); |
| 96 return false; |
| 97 } |
| 98 HostArrayBufferVar* host_buffer = |
| 99 static_cast<HostArrayBufferVar*>(buffer); |
| 100 *result = |
| 101 v8::Local<v8::Value>::New(host_buffer->webkit_buffer().toV8Value()); |
| 102 break; |
| 103 } |
| 87 case PP_VARTYPE_OBJECT: | 104 case PP_VARTYPE_OBJECT: |
| 88 case PP_VARTYPE_ARRAY: | 105 case PP_VARTYPE_ARRAY: |
| 89 case PP_VARTYPE_DICTIONARY: | 106 case PP_VARTYPE_DICTIONARY: |
| 90 case PP_VARTYPE_ARRAY_BUFFER: | |
| 91 // These are not currently supported. | 107 // These are not currently supported. |
| 92 NOTIMPLEMENTED(); | 108 NOTIMPLEMENTED(); |
| 93 result->Clear(); | 109 result->Clear(); |
| 94 return false; | 110 return false; |
| 95 } | 111 } |
| 96 return true; | 112 return true; |
| 97 } | 113 } |
| 98 | 114 |
| 99 // Copy a PP_Var in to a PP_Var that is appropriate for sending via postMessage. | 115 // Copy a PP_Var in to a PP_Var that is appropriate for sending via postMessage. |
| 100 // This currently just copies the value. For a string Var, the result is a | 116 // This currently just copies the value. For a string Var, the result is a |
| 101 // PP_Var with the a copy of |var|'s string contents and a reference count of 1. | 117 // PP_Var with the a copy of |var|'s string contents and a reference count of 1. |
| 102 // | 118 // |
| 103 // TODO(dmichael): We need to do structured clone eventually to copy a object | 119 // TODO(dmichael): Bypass this step for out-of-process plugins, since a copy |
| 104 // structure. The details and PPAPI changes for this are TBD. | 120 // happens already when the Var is serialized. |
| 105 PP_Var CopyPPVar(const PP_Var& var) { | 121 PP_Var CopyPPVar(const PP_Var& var) { |
| 106 if (var.type == PP_VARTYPE_OBJECT) { | 122 if (var.type == PP_VARTYPE_OBJECT) { |
| 107 // Objects are not currently supported. | 123 // Objects are not currently supported. |
| 108 NOTIMPLEMENTED(); | 124 NOTIMPLEMENTED(); |
| 109 return PP_MakeUndefined(); | 125 return PP_MakeUndefined(); |
| 110 } else if (var.type == PP_VARTYPE_STRING) { | 126 } else if (var.type == PP_VARTYPE_STRING) { |
| 111 StringVar* string = StringVar::FromPPVar(var); | 127 StringVar* string = StringVar::FromPPVar(var); |
| 112 if (!string) | 128 if (!string) |
| 113 return PP_MakeUndefined(); | 129 return PP_MakeUndefined(); |
| 114 return StringVar::StringToPPVar(string->value()); | 130 return StringVar::StringToPPVar(string->value()); |
| 131 } else if (var.type == PP_VARTYPE_ARRAY_BUFFER) { |
| 132 ArrayBufferVar* buffer = ArrayBufferVar::FromPPVar(var); |
| 133 if (!buffer) |
| 134 return PP_MakeUndefined(); |
| 135 PP_Var new_buffer_var = PpapiGlobals::Get()->GetVarTracker()-> |
| 136 MakeArrayBufferPPVar(buffer->ByteLength()); |
| 137 ArrayBufferVar* new_buffer = ArrayBufferVar::FromPPVar(new_buffer_var); |
| 138 memcpy(new_buffer->Map(), buffer->Map(), buffer->ByteLength()); |
| 139 return new_buffer_var; |
| 115 } else { | 140 } else { |
| 116 return var; | 141 return var; |
| 117 } | 142 } |
| 118 } | 143 } |
| 119 | 144 |
| 120 //------------------------------------------------------------------------------ | 145 //------------------------------------------------------------------------------ |
| 121 // Implementations of NPClass functions. These are here to: | 146 // Implementations of NPClass functions. These are here to: |
| 122 // - Implement postMessage behavior. | 147 // - Implement postMessage behavior. |
| 123 // - Forward calls to the 'passthrough' object to allow backwards-compatibility | 148 // - Forward calls to the 'passthrough' object to allow backwards-compatibility |
| 124 // with GetInstanceObject() objects. | 149 // with GetInstanceObject() objects. |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 295 // reference count to 1. We release it in the destructor. | 320 // reference count to 1. We release it in the destructor. |
| 296 NPObject* obj = WebBindings::createObject(NULL, &message_channel_class); | 321 NPObject* obj = WebBindings::createObject(NULL, &message_channel_class); |
| 297 DCHECK(obj); | 322 DCHECK(obj); |
| 298 np_object_ = static_cast<MessageChannel::MessageChannelNPObject*>(obj); | 323 np_object_ = static_cast<MessageChannel::MessageChannelNPObject*>(obj); |
| 299 np_object_->message_channel = this; | 324 np_object_->message_channel = this; |
| 300 } | 325 } |
| 301 | 326 |
| 302 void MessageChannel::PostMessageToJavaScript(PP_Var message_data) { | 327 void MessageChannel::PostMessageToJavaScript(PP_Var message_data) { |
| 303 // Serialize the message data. | 328 // Serialize the message data. |
| 304 v8::HandleScope scope; | 329 v8::HandleScope scope; |
| 305 v8::Handle<v8::Value> v8_val; | 330 // Because V8 is probably not on the stack for Native->JS calls, we need to |
| 331 // enter the appropriate context for the plugin. |
| 332 v8::Local<v8::Context> context = |
| 333 instance_->container()->element().document().frame()-> |
| 334 mainWorldScriptContext(); |
| 335 context->Enter(); |
| 336 v8::Local<v8::Value> v8_val; |
| 306 if (!PPVarToV8Value(message_data, &v8_val)) { | 337 if (!PPVarToV8Value(message_data, &v8_val)) { |
| 307 NOTREACHED(); | 338 NOTREACHED(); |
| 308 return; | 339 return; |
| 309 } | 340 } |
| 310 | 341 |
| 311 WebSerializedScriptValue serialized_val = | 342 WebSerializedScriptValue serialized_val = |
| 312 WebSerializedScriptValue::serialize(v8_val); | 343 WebSerializedScriptValue::serialize(v8_val); |
| 344 context->Exit(); |
| 313 | 345 |
| 314 MessageLoop::current()->PostTask( | 346 MessageLoop::current()->PostTask( |
| 315 FROM_HERE, | 347 FROM_HERE, |
| 316 base::Bind(&MessageChannel::PostMessageToJavaScriptImpl, | 348 base::Bind(&MessageChannel::PostMessageToJavaScriptImpl, |
| 317 weak_ptr_factory_.GetWeakPtr(), | 349 weak_ptr_factory_.GetWeakPtr(), |
| 318 serialized_val)); | 350 serialized_val)); |
| 319 } | 351 } |
| 320 | 352 |
| 321 void MessageChannel::PostMessageToJavaScriptImpl( | 353 void MessageChannel::PostMessageToJavaScriptImpl( |
| 322 const WebSerializedScriptValue& message_data) { | 354 const WebSerializedScriptValue& message_data) { |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 381 // SetPassthroughObject(passthrough_object()); | 413 // SetPassthroughObject(passthrough_object()); |
| 382 if (passthrough_object_) | 414 if (passthrough_object_) |
| 383 WebBindings::releaseObject(passthrough_object_); | 415 WebBindings::releaseObject(passthrough_object_); |
| 384 | 416 |
| 385 passthrough_object_ = passthrough; | 417 passthrough_object_ = passthrough; |
| 386 } | 418 } |
| 387 | 419 |
| 388 } // namespace ppapi | 420 } // namespace ppapi |
| 389 } // namespace webkit | 421 } // namespace webkit |
| 390 | 422 |
| OLD | NEW |