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) { | |
brettw
2011/03/26 17:34:16
Be consistent, so no {} for single-line conditiona
dmichael(do not use this one)
2011/03/26 20:57:08
Argh... old habits die hard. Thanks.
| |
106 return PP_MakeUndefined(); | |
107 } | |
108 return StringVar::StringToPPVar(string->module(), string->value()); | |
109 } else { | |
110 return var; | |
111 } | |
112 } | |
113 | |
91 //------------------------------------------------------------------------------ | 114 //------------------------------------------------------------------------------ |
92 // Implementations of NPClass functions. These are here to: | 115 // Implementations of NPClass functions. These are here to: |
93 // - Implement postMessage behavior. | 116 // - Implement postMessage behavior. |
94 // - Forward calls to the 'passthrough' object to allow backwards-compatibility | 117 // - Forward calls to the 'passthrough' object to allow backwards-compatibility |
95 // with GetInstanceObject() objects. | 118 // with GetInstanceObject() objects. |
96 //------------------------------------------------------------------------------ | 119 //------------------------------------------------------------------------------ |
97 NPObject* MessageChannelAllocate(NPP npp, NPClass* the_class) { | 120 NPObject* MessageChannelAllocate(NPP npp, NPClass* the_class) { |
98 return new MessageChannel::MessageChannelNPObject; | 121 return new MessageChannel::MessageChannelNPObject; |
99 } | 122 } |
100 | 123 |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
249 } // namespace | 272 } // namespace |
250 | 273 |
251 // MessageChannel -------------------------------------------------------------- | 274 // MessageChannel -------------------------------------------------------------- |
252 MessageChannel::MessageChannelNPObject::MessageChannelNPObject() {} | 275 MessageChannel::MessageChannelNPObject::MessageChannelNPObject() {} |
253 | 276 |
254 MessageChannel::MessageChannelNPObject::~MessageChannelNPObject() {} | 277 MessageChannel::MessageChannelNPObject::~MessageChannelNPObject() {} |
255 | 278 |
256 MessageChannel::MessageChannel(PluginInstance* instance) | 279 MessageChannel::MessageChannel(PluginInstance* instance) |
257 : instance_(instance), | 280 : instance_(instance), |
258 passthrough_object_(NULL), | 281 passthrough_object_(NULL), |
259 np_object_(NULL) { | 282 np_object_(NULL), |
283 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { | |
260 VOID_TO_NPVARIANT(onmessage_invoker_); | 284 VOID_TO_NPVARIANT(onmessage_invoker_); |
261 | 285 |
262 // Now create an NPObject for receiving calls to postMessage. | 286 // Now create an NPObject for receiving calls to postMessage. |
263 NPObject* obj = WebBindings::createObject(NULL, &message_channel_class); | 287 NPObject* obj = WebBindings::createObject(NULL, &message_channel_class); |
264 DCHECK(obj); | 288 DCHECK(obj); |
265 np_object_ = static_cast<MessageChannel::MessageChannelNPObject*>(obj); | 289 np_object_ = static_cast<MessageChannel::MessageChannelNPObject*>(obj); |
266 np_object_->message_channel = this; | 290 np_object_->message_channel = this; |
267 } | 291 } |
268 | 292 |
269 bool MessageChannel::EvaluateOnMessageInvoker() { | 293 bool MessageChannel::EvaluateOnMessageInvoker() { |
(...skipping 29 matching lines...) Expand all Loading... | |
299 if (!WebBindings::evaluate(NULL, frame->windowObject(), &function_string, | 323 if (!WebBindings::evaluate(NULL, frame->windowObject(), &function_string, |
300 &onmessage_invoker_)) { | 324 &onmessage_invoker_)) { |
301 // If it fails, do nothing. | 325 // If it fails, do nothing. |
302 return false; | 326 return false; |
303 } | 327 } |
304 DCHECK(NPVARIANT_IS_OBJECT(onmessage_invoker_)); | 328 DCHECK(NPVARIANT_IS_OBJECT(onmessage_invoker_)); |
305 return true; | 329 return true; |
306 } | 330 } |
307 | 331 |
308 void MessageChannel::PostMessageToJavaScript(PP_Var message_data) { | 332 void MessageChannel::PostMessageToJavaScript(PP_Var message_data) { |
309 // Make sure we have our function for invoking a JavaScript onmessage | 333 // Make a copy of the message data for the Task we will run. |
310 // function. | 334 PP_Var var_copy(CopyPPVar(message_data)); |
335 | |
336 MessageLoop::current()->PostTask( | |
337 FROM_HERE, | |
338 method_factory_.NewRunnableMethod( | |
339 &MessageChannel::PostMessageToJavaScriptImpl, | |
340 var_copy)); | |
341 } | |
342 | |
343 void MessageChannel::PostMessageToJavaScriptImpl(PP_Var message_data) { | |
344 // Make sure we have our function for invoking onmessage on JavaScript. | |
311 bool success = EvaluateOnMessageInvoker(); | 345 bool success = EvaluateOnMessageInvoker(); |
312 DCHECK(success); | 346 DCHECK(success); |
313 if (!success) | 347 if (!success) |
314 return; | 348 return; |
315 | 349 |
316 DCHECK(instance_); | 350 DCHECK(instance_); |
317 | 351 |
318 NPVariant result_var; | 352 NPVariant result_var; |
319 VOID_TO_NPVARIANT(result_var); | 353 VOID_TO_NPVARIANT(result_var); |
320 NPVariant npvariant_args[2]; | 354 NPVariant npvariant_args[2]; |
321 OBJECT_TO_NPVARIANT(instance_->container()->scriptableObjectForElement(), | 355 OBJECT_TO_NPVARIANT(instance_->container()->scriptableObjectForElement(), |
322 npvariant_args[0]); | 356 npvariant_args[0]); |
323 // Convert message to an NPVariant without copying. Note this means that | 357 // 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 | 358 // 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])) | 359 if (!PPVarToNPVariantNoCopy(message_data, &npvariant_args[1])) |
331 return; | 360 return; |
332 | 361 |
333 WebBindings::invokeDefault(NULL, | 362 WebBindings::invokeDefault(NULL, |
334 NPVARIANT_TO_OBJECT(onmessage_invoker_), | 363 NPVARIANT_TO_OBJECT(onmessage_invoker_), |
335 npvariant_args, | 364 npvariant_args, |
336 sizeof(npvariant_args)/sizeof(*npvariant_args), | 365 sizeof(npvariant_args)/sizeof(*npvariant_args), |
337 &result_var); | 366 &result_var); |
338 } | 367 } |
339 | 368 |
340 void MessageChannel::PostMessageToNative(PP_Var message_data) { | 369 void MessageChannel::PostMessageToNative(PP_Var message_data) { |
370 // Make a copy of the message data for the Task we will run. | |
371 PP_Var var_copy(CopyPPVar(message_data)); | |
372 | |
373 MessageLoop::current()->PostTask(FROM_HERE, | |
374 method_factory_.NewRunnableMethod( | |
375 &MessageChannel::PostMessageToNativeImpl, | |
376 var_copy)); | |
377 } | |
378 | |
379 void MessageChannel::PostMessageToNativeImpl(PP_Var message_data) { | |
341 instance_->HandleMessage(message_data); | 380 instance_->HandleMessage(message_data); |
342 } | 381 } |
343 | 382 |
344 MessageChannel::~MessageChannel() { | 383 MessageChannel::~MessageChannel() { |
345 WebBindings::releaseObject(np_object_); | 384 WebBindings::releaseObject(np_object_); |
346 WebBindings::releaseVariantValue(&onmessage_invoker_); | 385 WebBindings::releaseVariantValue(&onmessage_invoker_); |
347 } | 386 } |
348 | 387 |
349 } // namespace ppapi | 388 } // namespace ppapi |
350 } // namespace webkit | 389 } // namespace webkit |
351 | 390 |
OLD | NEW |