| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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" |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 using WebKit::WebPluginContainer; | 38 using WebKit::WebPluginContainer; |
| 39 using WebKit::WebSerializedScriptValue; | 39 using WebKit::WebSerializedScriptValue; |
| 40 | 40 |
| 41 namespace webkit { | 41 namespace webkit { |
| 42 | 42 |
| 43 namespace ppapi { | 43 namespace ppapi { |
| 44 | 44 |
| 45 namespace { | 45 namespace { |
| 46 | 46 |
| 47 const char kPostMessage[] = "postMessage"; | 47 const char kPostMessage[] = "postMessage"; |
| 48 const char kV8ToVarConversionError[] = "Failed to convert a PostMessage " |
| 49 "argument from a JavaScript value to a PP_Var. It may have cycles or be of " |
| 50 "an unsupported type."; |
| 51 const char kVarToV8ConversionError[] = "Failed to convert a PostMessage " |
| 52 "argument from a PP_Var to a Javascript value. It may have cycles or be of " |
| 53 "an unsupported type."; |
| 48 | 54 |
| 49 // Helper function to get the MessageChannel that is associated with an | 55 // Helper function to get the MessageChannel that is associated with an |
| 50 // NPObject*. | 56 // NPObject*. |
| 51 MessageChannel* ToMessageChannel(NPObject* object) { | 57 MessageChannel* ToMessageChannel(NPObject* object) { |
| 52 return static_cast<MessageChannel::MessageChannelNPObject*>(object)-> | 58 return static_cast<MessageChannel::MessageChannelNPObject*>(object)-> |
| 53 message_channel.get(); | 59 message_channel.get(); |
| 54 } | 60 } |
| 55 | 61 |
| 56 NPObject* ToPassThroughObject(NPObject* object) { | 62 NPObject* ToPassThroughObject(NPObject* object) { |
| 57 MessageChannel* channel = ToMessageChannel(object); | 63 MessageChannel* channel = ToMessageChannel(object); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 78 *result = PP_MakeInt32(NPVARIANT_TO_INT32(*variant)); | 84 *result = PP_MakeInt32(NPVARIANT_TO_INT32(*variant)); |
| 79 return true; | 85 return true; |
| 80 case NPVariantType_Double: | 86 case NPVariantType_Double: |
| 81 *result = PP_MakeDouble(NPVARIANT_TO_DOUBLE(*variant)); | 87 *result = PP_MakeDouble(NPVARIANT_TO_DOUBLE(*variant)); |
| 82 return true; | 88 return true; |
| 83 case NPVariantType_String: | 89 case NPVariantType_String: |
| 84 *result = StringVar::StringToPPVar( | 90 *result = StringVar::StringToPPVar( |
| 85 NPVARIANT_TO_STRING(*variant).UTF8Characters, | 91 NPVARIANT_TO_STRING(*variant).UTF8Characters, |
| 86 NPVARIANT_TO_STRING(*variant).UTF8Length); | 92 NPVARIANT_TO_STRING(*variant).UTF8Length); |
| 87 return true; | 93 return true; |
| 88 case NPVariantType_Object: | 94 case NPVariantType_Object: { |
| 89 V8VarConverter converter; | |
| 90 // Calling WebBindings::toV8Value creates a wrapper around NPVariant so it | 95 // Calling WebBindings::toV8Value creates a wrapper around NPVariant so it |
| 91 // shouldn't result in a deep copy. | 96 // shouldn't result in a deep copy. |
| 92 return converter.FromV8Value(WebBindings::toV8Value(variant), | 97 v8::Handle<v8::Value> v8_value = WebBindings::toV8Value(variant); |
| 93 v8::Context::GetCurrent(), result); | 98 if (!V8VarConverter::FromV8Value(v8_value, v8::Context::GetCurrent(), |
| 99 result)) { |
| 100 return false; |
| 101 } |
| 102 return true; |
| 103 } |
| 94 } | 104 } |
| 95 return false; | 105 return false; |
| 96 } | 106 } |
| 97 | 107 |
| 98 // Copy a PP_Var in to a PP_Var that is appropriate for sending via postMessage. | 108 // Copy a PP_Var in to a PP_Var that is appropriate for sending via postMessage. |
| 99 // This currently just copies the value. For a string Var, the result is a | 109 // This currently just copies the value. For a string Var, the result is a |
| 100 // PP_Var with the a copy of |var|'s string contents and a reference count of 1. | 110 // PP_Var with the a copy of |var|'s string contents and a reference count of 1. |
| 101 PP_Var CopyPPVar(const PP_Var& var) { | 111 PP_Var CopyPPVar(const PP_Var& var) { |
| 102 switch (var.type) { | 112 switch (var.type) { |
| 103 case PP_VARTYPE_UNDEFINED: | 113 case PP_VARTYPE_UNDEFINED: |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 NPVariant* result) { | 185 NPVariant* result) { |
| 176 if (!np_obj) | 186 if (!np_obj) |
| 177 return false; | 187 return false; |
| 178 | 188 |
| 179 // We only handle a function called postMessage. | 189 // We only handle a function called postMessage. |
| 180 if (IdentifierIsPostMessage(name) && (arg_count == 1)) { | 190 if (IdentifierIsPostMessage(name) && (arg_count == 1)) { |
| 181 MessageChannel* message_channel = ToMessageChannel(np_obj); | 191 MessageChannel* message_channel = ToMessageChannel(np_obj); |
| 182 if (message_channel) { | 192 if (message_channel) { |
| 183 PP_Var argument = PP_MakeUndefined(); | 193 PP_Var argument = PP_MakeUndefined(); |
| 184 if (!NPVariantToPPVar(&args[0], &argument)) { | 194 if (!NPVariantToPPVar(&args[0], &argument)) { |
| 185 NOTREACHED(); | 195 PpapiGlobals::Get()->LogWithSource( |
| 196 message_channel->instance()->pp_instance(), |
| 197 PP_LOGLEVEL_ERROR, std::string(), kV8ToVarConversionError); |
| 186 return false; | 198 return false; |
| 187 } | 199 } |
| 188 message_channel->PostMessageToNative(argument); | 200 message_channel->PostMessageToNative(argument); |
| 189 PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(argument); | 201 PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(argument); |
| 190 return true; | 202 return true; |
| 191 } else { | 203 } else { |
| 192 return false; | 204 return false; |
| 193 } | 205 } |
| 194 } | 206 } |
| 195 // Other method calls we will pass to the passthrough object, if we have one. | 207 // Other method calls we will pass to the passthrough object, if we have one. |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 339 WebPluginContainer* container = instance_->container(); | 351 WebPluginContainer* container = instance_->container(); |
| 340 // It's possible that container() is NULL if the plugin has been removed from | 352 // It's possible that container() is NULL if the plugin has been removed from |
| 341 // the DOM (but the PluginInstance is not destroyed yet). | 353 // the DOM (but the PluginInstance is not destroyed yet). |
| 342 if (!container) | 354 if (!container) |
| 343 return; | 355 return; |
| 344 | 356 |
| 345 v8::Local<v8::Context> context = | 357 v8::Local<v8::Context> context = |
| 346 container->element().document().frame()->mainWorldScriptContext(); | 358 container->element().document().frame()->mainWorldScriptContext(); |
| 347 v8::Context::Scope context_scope(context); | 359 v8::Context::Scope context_scope(context); |
| 348 | 360 |
| 349 v8::Local<v8::Value> v8_val; | 361 v8::Handle<v8::Value> v8_val; |
| 350 V8VarConverter converter; | 362 if (!V8VarConverter::ToV8Value(message_data, context, &v8_val)) { |
| 351 if (!converter.ToV8Value(message_data, context, &v8_val)) { | 363 PpapiGlobals::Get()->LogWithSource(instance_->pp_instance(), |
| 352 NOTREACHED(); | 364 PP_LOGLEVEL_ERROR, std::string(), kVarToV8ConversionError); |
| 353 return; | 365 return; |
| 354 } | 366 } |
| 355 | 367 |
| 356 // This is for backward compatibility. It usually makes sense for us to return | 368 // This is for backward compatibility. It usually makes sense for us to return |
| 357 // a string object rather than a string primitive because it allows multiple | 369 // a string object rather than a string primitive because it allows multiple |
| 358 // references to the same string (as with PP_Var strings). However, prior to | 370 // references to the same string (as with PP_Var strings). However, prior to |
| 359 // implementing dictionary and array, vars we would return a string primitive | 371 // implementing dictionary and array, vars we would return a string primitive |
| 360 // here. Changing it to an object now will break existing code that uses | 372 // here. Changing it to an object now will break existing code that uses |
| 361 // strict comparisons for strings returned from PostMessage. e.g. x === "123" | 373 // strict comparisons for strings returned from PostMessage. e.g. x === "123" |
| 362 // will no longer return true. So if the only value to return is a string | 374 // will no longer return true. So if the only value to return is a string |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 496 // invokes: | 508 // invokes: |
| 497 // SetPassthroughObject(passthrough_object()); | 509 // SetPassthroughObject(passthrough_object()); |
| 498 if (passthrough_object_) | 510 if (passthrough_object_) |
| 499 WebBindings::releaseObject(passthrough_object_); | 511 WebBindings::releaseObject(passthrough_object_); |
| 500 | 512 |
| 501 passthrough_object_ = passthrough; | 513 passthrough_object_ = passthrough; |
| 502 } | 514 } |
| 503 | 515 |
| 504 } // namespace ppapi | 516 } // namespace ppapi |
| 505 } // namespace webkit | 517 } // namespace webkit |
| OLD | NEW |