| 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/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/message_loop.h" |
| 11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebBindings.h" | 12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebBindings.h" |
| 12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
| 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" | 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" |
| 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPluginContainer.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPluginContainer.h" |
| 16 #include "webkit/plugins/ppapi/npapi_glue.h" | 17 #include "webkit/plugins/ppapi/npapi_glue.h" |
| 17 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" | 18 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" |
| 18 #include "webkit/plugins/ppapi/var.h" | 19 #include "webkit/plugins/ppapi/var.h" |
| 19 | 20 |
| 20 using WebKit::WebBindings; | 21 using WebKit::WebBindings; |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 if (!string) { | 72 if (!string) { |
| 72 VOID_TO_NPVARIANT(*result); | 73 VOID_TO_NPVARIANT(*result); |
| 73 return false; | 74 return false; |
| 74 } | 75 } |
| 75 const std::string& value = string->value(); | 76 const std::string& value = string->value(); |
| 76 STRINGN_TO_NPVARIANT(value.c_str(), value.size(), *result); | 77 STRINGN_TO_NPVARIANT(value.c_str(), value.size(), *result); |
| 77 break; | 78 break; |
| 78 } | 79 } |
| 79 case PP_VARTYPE_OBJECT: | 80 case PP_VARTYPE_OBJECT: |
| 80 // Objects are not currently supported. | 81 // Objects are not currently supported. |
| 81 DCHECK(false); | 82 NOTIMPLEMENTED(); |
| 82 VOID_TO_NPVARIANT(*result); | 83 VOID_TO_NPVARIANT(*result); |
| 83 return false; | 84 return false; |
| 84 default: | 85 default: |
| 85 VOID_TO_NPVARIANT(*result); | 86 VOID_TO_NPVARIANT(*result); |
| 86 return false; | 87 return false; |
| 87 } | 88 } |
| 88 return true; | 89 return true; |
| 89 } | 90 } |
| 90 | 91 |
| 92 // Copy a PP_Var in to a PP_Var that is appropriate for sending via postMessage. |
| 93 // This currently just copies the value. For a string Var, the result is a |
| 94 // PP_Var with the a copy of |var|'s string contents and a reference count of 1. |
| 95 // |
| 96 // TODO(dmichael): We need to do structured clone eventually to copy a object |
| 97 // structure. The details and PPAPI changes for this are TBD. |
| 98 PP_Var CopyPPVar(const PP_Var& var) { |
| 99 if (var.type == PP_VARTYPE_OBJECT) { |
| 100 // Objects are not currently supported. |
| 101 NOTIMPLEMENTED(); |
| 102 return PP_MakeUndefined(); |
| 103 } else if (var.type == PP_VARTYPE_STRING) { |
| 104 scoped_refptr<StringVar> string(StringVar::FromPPVar(var)); |
| 105 if (!string) |
| 106 return PP_MakeUndefined(); |
| 107 return StringVar::StringToPPVar(string->module(), string->value()); |
| 108 } else { |
| 109 return var; |
| 110 } |
| 111 } |
| 112 |
| 91 //------------------------------------------------------------------------------ | 113 //------------------------------------------------------------------------------ |
| 92 // Implementations of NPClass functions. These are here to: | 114 // Implementations of NPClass functions. These are here to: |
| 93 // - Implement postMessage behavior. | 115 // - Implement postMessage behavior. |
| 94 // - Forward calls to the 'passthrough' object to allow backwards-compatibility | 116 // - Forward calls to the 'passthrough' object to allow backwards-compatibility |
| 95 // with GetInstanceObject() objects. | 117 // with GetInstanceObject() objects. |
| 96 //------------------------------------------------------------------------------ | 118 //------------------------------------------------------------------------------ |
| 97 NPObject* MessageChannelAllocate(NPP npp, NPClass* the_class) { | 119 NPObject* MessageChannelAllocate(NPP npp, NPClass* the_class) { |
| 98 return new MessageChannel::MessageChannelNPObject; | 120 return new MessageChannel::MessageChannelNPObject; |
| 99 } | 121 } |
| 100 | 122 |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 249 } // namespace | 271 } // namespace |
| 250 | 272 |
| 251 // MessageChannel -------------------------------------------------------------- | 273 // MessageChannel -------------------------------------------------------------- |
| 252 MessageChannel::MessageChannelNPObject::MessageChannelNPObject() {} | 274 MessageChannel::MessageChannelNPObject::MessageChannelNPObject() {} |
| 253 | 275 |
| 254 MessageChannel::MessageChannelNPObject::~MessageChannelNPObject() {} | 276 MessageChannel::MessageChannelNPObject::~MessageChannelNPObject() {} |
| 255 | 277 |
| 256 MessageChannel::MessageChannel(PluginInstance* instance) | 278 MessageChannel::MessageChannel(PluginInstance* instance) |
| 257 : instance_(instance), | 279 : instance_(instance), |
| 258 passthrough_object_(NULL), | 280 passthrough_object_(NULL), |
| 259 np_object_(NULL) { | 281 np_object_(NULL), |
| 282 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { |
| 260 VOID_TO_NPVARIANT(onmessage_invoker_); | 283 VOID_TO_NPVARIANT(onmessage_invoker_); |
| 261 | 284 |
| 262 // Now create an NPObject for receiving calls to postMessage. | 285 // Now create an NPObject for receiving calls to postMessage. |
| 263 NPObject* obj = WebBindings::createObject(NULL, &message_channel_class); | 286 NPObject* obj = WebBindings::createObject(NULL, &message_channel_class); |
| 264 DCHECK(obj); | 287 DCHECK(obj); |
| 265 np_object_ = static_cast<MessageChannel::MessageChannelNPObject*>(obj); | 288 np_object_ = static_cast<MessageChannel::MessageChannelNPObject*>(obj); |
| 266 np_object_->message_channel = this; | 289 np_object_->message_channel = this; |
| 267 } | 290 } |
| 268 | 291 |
| 269 bool MessageChannel::EvaluateOnMessageInvoker() { | 292 bool MessageChannel::EvaluateOnMessageInvoker() { |
| (...skipping 29 matching lines...) Expand all Loading... |
| 299 if (!WebBindings::evaluate(NULL, frame->windowObject(), &function_string, | 322 if (!WebBindings::evaluate(NULL, frame->windowObject(), &function_string, |
| 300 &onmessage_invoker_)) { | 323 &onmessage_invoker_)) { |
| 301 // If it fails, do nothing. | 324 // If it fails, do nothing. |
| 302 return false; | 325 return false; |
| 303 } | 326 } |
| 304 DCHECK(NPVARIANT_IS_OBJECT(onmessage_invoker_)); | 327 DCHECK(NPVARIANT_IS_OBJECT(onmessage_invoker_)); |
| 305 return true; | 328 return true; |
| 306 } | 329 } |
| 307 | 330 |
| 308 void MessageChannel::PostMessageToJavaScript(PP_Var message_data) { | 331 void MessageChannel::PostMessageToJavaScript(PP_Var message_data) { |
| 309 // Make sure we have our function for invoking a JavaScript onmessage | 332 // Make a copy of the message data for the Task we will run. |
| 310 // function. | 333 PP_Var var_copy(CopyPPVar(message_data)); |
| 334 |
| 335 MessageLoop::current()->PostTask( |
| 336 FROM_HERE, |
| 337 method_factory_.NewRunnableMethod( |
| 338 &MessageChannel::PostMessageToJavaScriptImpl, |
| 339 var_copy)); |
| 340 } |
| 341 |
| 342 void MessageChannel::PostMessageToJavaScriptImpl(PP_Var message_data) { |
| 343 // Make sure we have our function for invoking onmessage on JavaScript. |
| 311 bool success = EvaluateOnMessageInvoker(); | 344 bool success = EvaluateOnMessageInvoker(); |
| 312 DCHECK(success); | 345 DCHECK(success); |
| 313 if (!success) | 346 if (!success) |
| 314 return; | 347 return; |
| 315 | 348 |
| 316 DCHECK(instance_); | 349 DCHECK(instance_); |
| 317 | 350 |
| 318 NPVariant result_var; | 351 NPVariant result_var; |
| 319 VOID_TO_NPVARIANT(result_var); | 352 VOID_TO_NPVARIANT(result_var); |
| 320 NPVariant npvariant_args[2]; | 353 NPVariant npvariant_args[2]; |
| 321 OBJECT_TO_NPVARIANT(instance_->container()->scriptableObjectForElement(), | 354 OBJECT_TO_NPVARIANT(instance_->container()->scriptableObjectForElement(), |
| 322 npvariant_args[0]); | 355 npvariant_args[0]); |
| 323 // Convert message to an NPVariant without copying. Note this means that | 356 // Convert message to an NPVariant without copying. At this point, the data |
| 324 // in-process plugins will not copy the data, so isn't really following the | 357 // has already been copied. |
| 325 // postMessage spec in spirit. Copying is handled in the proxy, and we don't | |
| 326 // want to re-copy unnecessarily. | |
| 327 // | |
| 328 // TODO(dmichael): We need to do structured clone eventually to copy a object | |
| 329 // structure. The details and PPAPI changes for this are TBD. | |
| 330 if (!PPVarToNPVariantNoCopy(message_data, &npvariant_args[1])) | 358 if (!PPVarToNPVariantNoCopy(message_data, &npvariant_args[1])) |
| 331 return; | 359 return; |
| 332 | 360 |
| 333 WebBindings::invokeDefault(NULL, | 361 WebBindings::invokeDefault(NULL, |
| 334 NPVARIANT_TO_OBJECT(onmessage_invoker_), | 362 NPVARIANT_TO_OBJECT(onmessage_invoker_), |
| 335 npvariant_args, | 363 npvariant_args, |
| 336 sizeof(npvariant_args)/sizeof(*npvariant_args), | 364 sizeof(npvariant_args)/sizeof(*npvariant_args), |
| 337 &result_var); | 365 &result_var); |
| 338 } | 366 } |
| 339 | 367 |
| 340 void MessageChannel::PostMessageToNative(PP_Var message_data) { | 368 void MessageChannel::PostMessageToNative(PP_Var message_data) { |
| 369 // Make a copy of the message data for the Task we will run. |
| 370 PP_Var var_copy(CopyPPVar(message_data)); |
| 371 |
| 372 MessageLoop::current()->PostTask(FROM_HERE, |
| 373 method_factory_.NewRunnableMethod( |
| 374 &MessageChannel::PostMessageToNativeImpl, |
| 375 var_copy)); |
| 376 } |
| 377 |
| 378 void MessageChannel::PostMessageToNativeImpl(PP_Var message_data) { |
| 341 instance_->HandleMessage(message_data); | 379 instance_->HandleMessage(message_data); |
| 342 } | 380 } |
| 343 | 381 |
| 344 MessageChannel::~MessageChannel() { | 382 MessageChannel::~MessageChannel() { |
| 345 WebBindings::releaseObject(np_object_); | 383 WebBindings::releaseObject(np_object_); |
| 346 WebBindings::releaseVariantValue(&onmessage_invoker_); | 384 WebBindings::releaseVariantValue(&onmessage_invoker_); |
| 347 } | 385 } |
| 348 | 386 |
| 349 } // namespace ppapi | 387 } // namespace ppapi |
| 350 } // namespace webkit | 388 } // namespace webkit |
| 351 | 389 |
| OLD | NEW |