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 "content/renderer/pepper/message_channel.h" | 5 #include "content/renderer/pepper/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/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
13 #include "content/public/common/content_client.h" | 13 #include "content/public/common/content_client.h" |
14 #include "content/public/renderer/content_renderer_client.h" | 14 #include "content/public/renderer/content_renderer_client.h" |
15 #include "content/renderer/pepper/host_array_buffer_var.h" | 15 #include "content/renderer/pepper/host_array_buffer_var.h" |
| 16 #include "content/renderer/pepper/npapi_glue.h" |
16 #include "content/renderer/pepper/pepper_plugin_instance_impl.h" | 17 #include "content/renderer/pepper/pepper_plugin_instance_impl.h" |
17 #include "content/renderer/pepper/pepper_try_catch.h" | |
18 #include "content/renderer/pepper/plugin_module.h" | 18 #include "content/renderer/pepper/plugin_module.h" |
19 #include "content/renderer/pepper/plugin_object.h" | |
20 #include "content/renderer/pepper/v8_var_converter.h" | 19 #include "content/renderer/pepper/v8_var_converter.h" |
21 #include "gin/arguments.h" | |
22 #include "gin/converter.h" | |
23 #include "gin/function_template.h" | |
24 #include "gin/object_template_builder.h" | |
25 #include "gin/public/gin_embedders.h" | |
26 #include "ppapi/shared_impl/ppapi_globals.h" | 20 #include "ppapi/shared_impl/ppapi_globals.h" |
27 #include "ppapi/shared_impl/scoped_pp_var.h" | 21 #include "ppapi/shared_impl/scoped_pp_var.h" |
28 #include "ppapi/shared_impl/var.h" | 22 #include "ppapi/shared_impl/var.h" |
29 #include "ppapi/shared_impl/var_tracker.h" | 23 #include "ppapi/shared_impl/var_tracker.h" |
30 #include "third_party/WebKit/public/web/WebBindings.h" | 24 #include "third_party/WebKit/public/web/WebBindings.h" |
31 #include "third_party/WebKit/public/web/WebDocument.h" | 25 #include "third_party/WebKit/public/web/WebDocument.h" |
32 #include "third_party/WebKit/public/web/WebDOMMessageEvent.h" | 26 #include "third_party/WebKit/public/web/WebDOMMessageEvent.h" |
33 #include "third_party/WebKit/public/web/WebElement.h" | 27 #include "third_party/WebKit/public/web/WebElement.h" |
34 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 28 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
35 #include "third_party/WebKit/public/web/WebNode.h" | 29 #include "third_party/WebKit/public/web/WebNode.h" |
(...skipping 20 matching lines...) Expand all Loading... |
56 const char kPostMessageAndAwaitResponse[] = "postMessageAndAwaitResponse"; | 50 const char kPostMessageAndAwaitResponse[] = "postMessageAndAwaitResponse"; |
57 const char kV8ToVarConversionError[] = | 51 const char kV8ToVarConversionError[] = |
58 "Failed to convert a PostMessage " | 52 "Failed to convert a PostMessage " |
59 "argument from a JavaScript value to a PP_Var. It may have cycles or be of " | 53 "argument from a JavaScript value to a PP_Var. It may have cycles or be of " |
60 "an unsupported type."; | 54 "an unsupported type."; |
61 const char kVarToV8ConversionError[] = | 55 const char kVarToV8ConversionError[] = |
62 "Failed to convert a PostMessage " | 56 "Failed to convert a PostMessage " |
63 "argument from a PP_Var to a Javascript value. It may have cycles or be of " | 57 "argument from a PP_Var to a Javascript value. It may have cycles or be of " |
64 "an unsupported type."; | 58 "an unsupported type."; |
65 | 59 |
66 bool HasDevPermission() { | 60 // Helper function to get the MessageChannel that is associated with an |
| 61 // NPObject*. |
| 62 MessageChannel* ToMessageChannel(NPObject* object) { |
| 63 return static_cast<MessageChannel::MessageChannelNPObject*>(object) |
| 64 ->message_channel.get(); |
| 65 } |
| 66 |
| 67 NPObject* ToPassThroughObject(NPObject* object) { |
| 68 MessageChannel* channel = ToMessageChannel(object); |
| 69 return channel ? channel->passthrough_object() : NULL; |
| 70 } |
| 71 |
| 72 // Return true iff |identifier| is equal to |string|. |
| 73 bool IdentifierIs(NPIdentifier identifier, const char string[]) { |
| 74 return WebBindings::getStringIdentifier(string) == identifier; |
| 75 } |
| 76 |
| 77 bool HasDevChannelPermission(NPObject* channel_object) { |
| 78 MessageChannel* channel = ToMessageChannel(channel_object); |
| 79 if (!channel) |
| 80 return false; |
67 return GetContentClient()->renderer()->IsPluginAllowedToUseDevChannelAPIs(); | 81 return GetContentClient()->renderer()->IsPluginAllowedToUseDevChannelAPIs(); |
68 } | 82 } |
69 | 83 |
| 84 //------------------------------------------------------------------------------ |
| 85 // Implementations of NPClass functions. These are here to: |
| 86 // - Implement postMessage behavior. |
| 87 // - Forward calls to the 'passthrough' object to allow backwards-compatibility |
| 88 // with GetInstanceObject() objects. |
| 89 //------------------------------------------------------------------------------ |
| 90 NPObject* MessageChannelAllocate(NPP npp, NPClass* the_class) { |
| 91 return new MessageChannel::MessageChannelNPObject; |
| 92 } |
| 93 |
| 94 void MessageChannelDeallocate(NPObject* object) { |
| 95 MessageChannel::MessageChannelNPObject* instance = |
| 96 static_cast<MessageChannel::MessageChannelNPObject*>(object); |
| 97 delete instance; |
| 98 } |
| 99 |
| 100 bool MessageChannelHasMethod(NPObject* np_obj, NPIdentifier name) { |
| 101 if (!np_obj) |
| 102 return false; |
| 103 |
| 104 if (IdentifierIs(name, kPostMessage)) |
| 105 return true; |
| 106 if (IdentifierIs(name, kPostMessageAndAwaitResponse) && |
| 107 HasDevChannelPermission(np_obj)) { |
| 108 return true; |
| 109 } |
| 110 // Other method names we will pass to the passthrough object, if we have one. |
| 111 NPObject* passthrough = ToPassThroughObject(np_obj); |
| 112 if (passthrough) |
| 113 return WebBindings::hasMethod(NULL, passthrough, name); |
| 114 return false; |
| 115 } |
| 116 |
| 117 bool MessageChannelInvoke(NPObject* np_obj, |
| 118 NPIdentifier name, |
| 119 const NPVariant* args, |
| 120 uint32 arg_count, |
| 121 NPVariant* result) { |
| 122 if (!np_obj) |
| 123 return false; |
| 124 |
| 125 MessageChannel* message_channel = ToMessageChannel(np_obj); |
| 126 if (!message_channel) |
| 127 return false; |
| 128 |
| 129 // Check to see if we should handle this function ourselves. |
| 130 if (IdentifierIs(name, kPostMessage) && (arg_count == 1)) { |
| 131 message_channel->PostMessageToNative(&args[0]); |
| 132 return true; |
| 133 } else if (IdentifierIs(name, kPostMessageAndAwaitResponse) && |
| 134 (arg_count == 1) && |
| 135 HasDevChannelPermission(np_obj)) { |
| 136 message_channel->PostBlockingMessageToNative(&args[0], result); |
| 137 return true; |
| 138 } |
| 139 |
| 140 // Other method calls we will pass to the passthrough object, if we have one. |
| 141 NPObject* passthrough = ToPassThroughObject(np_obj); |
| 142 if (passthrough) { |
| 143 return WebBindings::invoke( |
| 144 NULL, passthrough, name, args, arg_count, result); |
| 145 } |
| 146 return false; |
| 147 } |
| 148 |
| 149 bool MessageChannelInvokeDefault(NPObject* np_obj, |
| 150 const NPVariant* args, |
| 151 uint32 arg_count, |
| 152 NPVariant* result) { |
| 153 if (!np_obj) |
| 154 return false; |
| 155 |
| 156 // Invoke on the passthrough object, if we have one. |
| 157 NPObject* passthrough = ToPassThroughObject(np_obj); |
| 158 if (passthrough) { |
| 159 return WebBindings::invokeDefault( |
| 160 NULL, passthrough, args, arg_count, result); |
| 161 } |
| 162 return false; |
| 163 } |
| 164 |
| 165 bool MessageChannelHasProperty(NPObject* np_obj, NPIdentifier name) { |
| 166 if (!np_obj) |
| 167 return false; |
| 168 |
| 169 MessageChannel* message_channel = ToMessageChannel(np_obj); |
| 170 if (message_channel) { |
| 171 if (message_channel->GetReadOnlyProperty(name, NULL)) |
| 172 return true; |
| 173 } |
| 174 |
| 175 // Invoke on the passthrough object, if we have one. |
| 176 NPObject* passthrough = ToPassThroughObject(np_obj); |
| 177 if (passthrough) |
| 178 return WebBindings::hasProperty(NULL, passthrough, name); |
| 179 return false; |
| 180 } |
| 181 |
| 182 bool MessageChannelGetProperty(NPObject* np_obj, |
| 183 NPIdentifier name, |
| 184 NPVariant* result) { |
| 185 if (!np_obj) |
| 186 return false; |
| 187 |
| 188 // Don't allow getting the postMessage functions. |
| 189 if (IdentifierIs(name, kPostMessage)) |
| 190 return false; |
| 191 if (IdentifierIs(name, kPostMessageAndAwaitResponse) && |
| 192 HasDevChannelPermission(np_obj)) { |
| 193 return false; |
| 194 } |
| 195 MessageChannel* message_channel = ToMessageChannel(np_obj); |
| 196 if (message_channel) { |
| 197 if (message_channel->GetReadOnlyProperty(name, result)) |
| 198 return true; |
| 199 } |
| 200 |
| 201 // Invoke on the passthrough object, if we have one. |
| 202 NPObject* passthrough = ToPassThroughObject(np_obj); |
| 203 if (passthrough) |
| 204 return WebBindings::getProperty(NULL, passthrough, name, result); |
| 205 return false; |
| 206 } |
| 207 |
| 208 bool MessageChannelSetProperty(NPObject* np_obj, |
| 209 NPIdentifier name, |
| 210 const NPVariant* variant) { |
| 211 if (!np_obj) |
| 212 return false; |
| 213 |
| 214 // Don't allow setting the postMessage functions. |
| 215 if (IdentifierIs(name, kPostMessage)) |
| 216 return false; |
| 217 if (IdentifierIs(name, kPostMessageAndAwaitResponse) && |
| 218 HasDevChannelPermission(np_obj)) { |
| 219 return false; |
| 220 } |
| 221 // Invoke on the passthrough object, if we have one. |
| 222 NPObject* passthrough = ToPassThroughObject(np_obj); |
| 223 if (passthrough) |
| 224 return WebBindings::setProperty(NULL, passthrough, name, variant); |
| 225 return false; |
| 226 } |
| 227 |
| 228 bool MessageChannelEnumerate(NPObject* np_obj, |
| 229 NPIdentifier** value, |
| 230 uint32_t* count) { |
| 231 if (!np_obj) |
| 232 return false; |
| 233 |
| 234 // Invoke on the passthrough object, if we have one, to enumerate its |
| 235 // properties. |
| 236 NPObject* passthrough = ToPassThroughObject(np_obj); |
| 237 if (passthrough) { |
| 238 bool success = WebBindings::enumerate(NULL, passthrough, value, count); |
| 239 if (success) { |
| 240 // Add postMessage to the list and return it. |
| 241 if (std::numeric_limits<size_t>::max() / sizeof(NPIdentifier) <= |
| 242 static_cast<size_t>(*count) + 1) // Else, "always false" x64 warning. |
| 243 return false; |
| 244 NPIdentifier* new_array = static_cast<NPIdentifier*>( |
| 245 std::malloc(sizeof(NPIdentifier) * (*count + 1))); |
| 246 std::memcpy(new_array, *value, sizeof(NPIdentifier) * (*count)); |
| 247 new_array[*count] = WebBindings::getStringIdentifier(kPostMessage); |
| 248 std::free(*value); |
| 249 *value = new_array; |
| 250 ++(*count); |
| 251 return true; |
| 252 } |
| 253 } |
| 254 |
| 255 // Otherwise, build an array that includes only postMessage. |
| 256 *value = static_cast<NPIdentifier*>(malloc(sizeof(NPIdentifier))); |
| 257 (*value)[0] = WebBindings::getStringIdentifier(kPostMessage); |
| 258 *count = 1; |
| 259 return true; |
| 260 } |
| 261 |
| 262 NPClass message_channel_class = { |
| 263 NP_CLASS_STRUCT_VERSION, &MessageChannelAllocate, |
| 264 &MessageChannelDeallocate, NULL, |
| 265 &MessageChannelHasMethod, &MessageChannelInvoke, |
| 266 &MessageChannelInvokeDefault, &MessageChannelHasProperty, |
| 267 &MessageChannelGetProperty, &MessageChannelSetProperty, |
| 268 NULL, &MessageChannelEnumerate, }; |
| 269 |
70 } // namespace | 270 } // namespace |
71 | 271 |
72 // MessageChannel -------------------------------------------------------------- | 272 // MessageChannel -------------------------------------------------------------- |
73 struct MessageChannel::VarConversionResult { | 273 struct MessageChannel::VarConversionResult { |
74 VarConversionResult() : success_(false), conversion_completed_(false) {} | 274 VarConversionResult() : success_(false), conversion_completed_(false) {} |
75 void ConversionCompleted(const ScopedPPVar& var, | 275 void ConversionCompleted(const ScopedPPVar& var, |
76 bool success) { | 276 bool success) { |
77 conversion_completed_ = true; | 277 conversion_completed_ = true; |
78 var_ = var; | 278 var_ = var; |
79 success_ = success; | 279 success_ = success; |
80 } | 280 } |
81 const ScopedPPVar& var() const { return var_; } | 281 const ScopedPPVar& var() const { return var_; } |
82 bool success() const { return success_; } | 282 bool success() const { return success_; } |
83 bool conversion_completed() const { return conversion_completed_; } | 283 bool conversion_completed() const { return conversion_completed_; } |
84 | 284 |
85 private: | 285 private: |
86 ScopedPPVar var_; | 286 ScopedPPVar var_; |
87 bool success_; | 287 bool success_; |
88 bool conversion_completed_; | 288 bool conversion_completed_; |
89 }; | 289 }; |
90 | 290 |
91 // static | 291 MessageChannel::MessageChannelNPObject::MessageChannelNPObject() {} |
92 gin::WrapperInfo MessageChannel::kWrapperInfo = {gin::kEmbedderNativeGin}; | |
93 | 292 |
94 // static | 293 MessageChannel::MessageChannelNPObject::~MessageChannelNPObject() {} |
95 MessageChannel* MessageChannel::Create(PepperPluginInstanceImpl* instance, | 294 |
96 v8::Persistent<v8::Object>* result) { | 295 MessageChannel::MessageChannel(PepperPluginInstanceImpl* instance) |
97 MessageChannel* message_channel = new MessageChannel(instance); | 296 : instance_(instance), |
98 v8::HandleScope handle_scope(instance->GetIsolate()); | 297 passthrough_object_(NULL), |
99 v8::Context::Scope context_scope(instance->GetContext()); | 298 np_object_(NULL), |
100 gin::Handle<MessageChannel> handle = | 299 early_message_queue_state_(QUEUE_MESSAGES), |
101 gin::CreateHandle(instance->GetIsolate(), message_channel); | 300 weak_ptr_factory_(this) { |
102 result->Reset(instance->GetIsolate(), handle.ToV8()->ToObject()); | 301 // Now create an NPObject for receiving calls to postMessage. This sets the |
103 return message_channel; | 302 // reference count to 1. We release it in the destructor. |
| 303 NPObject* obj = WebBindings::createObject(instance_->instanceNPP(), |
| 304 &message_channel_class); |
| 305 DCHECK(obj); |
| 306 np_object_ = static_cast<MessageChannel::MessageChannelNPObject*>(obj); |
| 307 np_object_->message_channel = weak_ptr_factory_.GetWeakPtr(); |
104 } | 308 } |
105 | 309 |
106 MessageChannel::~MessageChannel() { | 310 MessageChannel::~MessageChannel() { |
107 passthrough_object_.Reset(); | 311 WebBindings::releaseObject(np_object_); |
| 312 if (passthrough_object_) |
| 313 WebBindings::releaseObject(passthrough_object_); |
108 } | 314 } |
109 | 315 |
110 void MessageChannel::InstanceDeleted() { | 316 void MessageChannel::Start() { |
111 instance_ = NULL; | 317 // We PostTask here instead of draining the message queue directly |
| 318 // since we haven't finished initializing the PepperWebPluginImpl yet, so |
| 319 // the plugin isn't available in the DOM. |
| 320 base::MessageLoop::current()->PostTask( |
| 321 FROM_HERE, |
| 322 base::Bind(&MessageChannel::DrainEarlyMessageQueue, |
| 323 weak_ptr_factory_.GetWeakPtr())); |
| 324 } |
| 325 |
| 326 void MessageChannel::SetPassthroughObject(NPObject* passthrough) { |
| 327 // Retain the passthrough object; We need to ensure it lives as long as this |
| 328 // MessageChannel. |
| 329 if (passthrough) |
| 330 WebBindings::retainObject(passthrough); |
| 331 |
| 332 // If we had a passthrough set already, release it. Note that we retain the |
| 333 // incoming passthrough object first, so that we behave correctly if anyone |
| 334 // invokes: |
| 335 // SetPassthroughObject(passthrough_object()); |
| 336 if (passthrough_object_) |
| 337 WebBindings::releaseObject(passthrough_object_); |
| 338 |
| 339 passthrough_object_ = passthrough; |
| 340 } |
| 341 |
| 342 bool MessageChannel::GetReadOnlyProperty(NPIdentifier key, |
| 343 NPVariant* value) const { |
| 344 std::map<NPIdentifier, ScopedPPVar>::const_iterator it = |
| 345 internal_properties_.find(key); |
| 346 if (it != internal_properties_.end()) { |
| 347 if (value) |
| 348 return PPVarToNPVariant(it->second.get(), value); |
| 349 return true; |
| 350 } |
| 351 return false; |
| 352 } |
| 353 |
| 354 void MessageChannel::SetReadOnlyProperty(PP_Var key, PP_Var value) { |
| 355 internal_properties_[PPVarToNPIdentifier(key)] = ScopedPPVar(value); |
112 } | 356 } |
113 | 357 |
114 void MessageChannel::PostMessageToJavaScript(PP_Var message_data) { | 358 void MessageChannel::PostMessageToJavaScript(PP_Var message_data) { |
115 v8::HandleScope scope(v8::Isolate::GetCurrent()); | 359 v8::HandleScope scope(v8::Isolate::GetCurrent()); |
116 | 360 |
117 // Because V8 is probably not on the stack for Native->JS calls, we need to | 361 // Because V8 is probably not on the stack for Native->JS calls, we need to |
118 // enter the appropriate context for the plugin. | 362 // enter the appropriate context for the plugin. |
119 v8::Local<v8::Context> context = instance_->GetContext(); | 363 WebPluginContainer* container = instance_->container(); |
| 364 // It's possible that container() is NULL if the plugin has been removed from |
| 365 // the DOM (but the PluginInstance is not destroyed yet). |
| 366 if (!container) |
| 367 return; |
| 368 |
| 369 v8::Local<v8::Context> context = |
| 370 container->element().document().frame()->mainWorldScriptContext(); |
| 371 // If the page is being destroyed, the context may be empty. |
120 if (context.IsEmpty()) | 372 if (context.IsEmpty()) |
121 return; | 373 return; |
122 | |
123 v8::Context::Scope context_scope(context); | 374 v8::Context::Scope context_scope(context); |
124 | 375 |
125 v8::Handle<v8::Value> v8_val; | 376 v8::Handle<v8::Value> v8_val; |
126 if (!V8VarConverter(instance_->pp_instance()) | 377 if (!V8VarConverter(instance_->pp_instance()) |
127 .ToV8Value(message_data, context, &v8_val)) { | 378 .ToV8Value(message_data, context, &v8_val)) { |
128 PpapiGlobals::Get()->LogWithSource(instance_->pp_instance(), | 379 PpapiGlobals::Get()->LogWithSource(instance_->pp_instance(), |
129 PP_LOGLEVEL_ERROR, | 380 PP_LOGLEVEL_ERROR, |
130 std::string(), | 381 std::string(), |
131 kVarToV8ConversionError); | 382 kVarToV8ConversionError); |
132 return; | 383 return; |
133 } | 384 } |
134 | 385 |
135 WebSerializedScriptValue serialized_val = | 386 WebSerializedScriptValue serialized_val = |
136 WebSerializedScriptValue::serialize(v8_val); | 387 WebSerializedScriptValue::serialize(v8_val); |
137 | 388 |
138 if (early_message_queue_state_ != SEND_DIRECTLY) { | 389 if (early_message_queue_state_ != SEND_DIRECTLY) { |
139 // We can't just PostTask here; the messages would arrive out of | 390 // We can't just PostTask here; the messages would arrive out of |
140 // order. Instead, we queue them up until we're ready to post | 391 // order. Instead, we queue them up until we're ready to post |
141 // them. | 392 // them. |
142 early_message_queue_.push_back(serialized_val); | 393 early_message_queue_.push_back(serialized_val); |
143 } else { | 394 } else { |
144 // The proxy sent an asynchronous message, so the plugin is already | 395 // The proxy sent an asynchronous message, so the plugin is already |
145 // unblocked. Therefore, there's no need to PostTask. | 396 // unblocked. Therefore, there's no need to PostTask. |
146 DCHECK(early_message_queue_.empty()); | 397 DCHECK(early_message_queue_.empty()); |
147 PostMessageToJavaScriptImpl(serialized_val); | 398 PostMessageToJavaScriptImpl(serialized_val); |
148 } | 399 } |
149 } | 400 } |
150 | 401 |
151 void MessageChannel::Start() { | 402 void MessageChannel::PostMessageToNative(const NPVariant* message_data) { |
152 // We PostTask here instead of draining the message queue directly | |
153 // since we haven't finished initializing the PepperWebPluginImpl yet, so | |
154 // the plugin isn't available in the DOM. | |
155 base::MessageLoop::current()->PostTask( | |
156 FROM_HERE, | |
157 base::Bind(&MessageChannel::DrainEarlyMessageQueue, | |
158 weak_ptr_factory_.GetWeakPtr())); | |
159 } | |
160 | |
161 void MessageChannel::SetPassthroughObject(v8::Handle<v8::Object> passthrough) { | |
162 passthrough_object_.Reset(instance_->GetIsolate(), passthrough); | |
163 } | |
164 | |
165 void MessageChannel::SetReadOnlyProperty(PP_Var key, PP_Var value) { | |
166 StringVar* key_string = StringVar::FromPPVar(key); | |
167 if (key_string) { | |
168 internal_named_properties_[key_string->value()] = ScopedPPVar(value); | |
169 } else { | |
170 NOTREACHED(); | |
171 } | |
172 } | |
173 | |
174 MessageChannel::MessageChannel(PepperPluginInstanceImpl* instance) | |
175 : gin::NamedPropertyInterceptor(instance->GetIsolate(), this), | |
176 instance_(instance), | |
177 early_message_queue_state_(QUEUE_MESSAGES), | |
178 weak_ptr_factory_(this) { | |
179 } | |
180 | |
181 gin::ObjectTemplateBuilder MessageChannel::GetObjectTemplateBuilder( | |
182 v8::Isolate* isolate) { | |
183 return Wrappable<MessageChannel>::GetObjectTemplateBuilder(isolate) | |
184 .AddNamedPropertyInterceptor(); | |
185 } | |
186 | |
187 v8::Local<v8::Value> MessageChannel::GetNamedProperty( | |
188 v8::Isolate* isolate, | |
189 const std::string& identifier) { | |
190 if (!instance_) | |
191 return v8::Local<v8::Value>(); | |
192 | |
193 PepperTryCatchV8 try_catch(instance_, V8VarConverter::kDisallowObjectVars, | |
194 isolate); | |
195 if (identifier == kPostMessage) { | |
196 return gin::CreateFunctionTemplate(isolate, | |
197 base::Bind(&MessageChannel::PostMessageToNative, | |
198 weak_ptr_factory_.GetWeakPtr()))->GetFunction(); | |
199 } else if (identifier == kPostMessageAndAwaitResponse && HasDevPermission()) { | |
200 return gin::CreateFunctionTemplate(isolate, | |
201 base::Bind(&MessageChannel::PostBlockingMessageToNative, | |
202 weak_ptr_factory_.GetWeakPtr()))->GetFunction(); | |
203 } | |
204 | |
205 std::map<std::string, ScopedPPVar>::const_iterator it = | |
206 internal_named_properties_.find(identifier); | |
207 if (it != internal_named_properties_.end()) { | |
208 v8::Handle<v8::Value> result = try_catch.ToV8(it->second.get()); | |
209 if (try_catch.ThrowException()) | |
210 return v8::Local<v8::Value>(); | |
211 return result; | |
212 } | |
213 | |
214 PluginObject* plugin_object = GetPluginObject(isolate); | |
215 if (plugin_object) | |
216 return plugin_object->GetNamedProperty(isolate, identifier); | |
217 return v8::Local<v8::Value>(); | |
218 } | |
219 | |
220 bool MessageChannel::SetNamedProperty(v8::Isolate* isolate, | |
221 const std::string& identifier, | |
222 v8::Local<v8::Value> value) { | |
223 if (!instance_) | |
224 return false; | |
225 PepperTryCatchV8 try_catch(instance_, V8VarConverter::kDisallowObjectVars, | |
226 isolate); | |
227 if (identifier == kPostMessage || | |
228 (identifier == kPostMessageAndAwaitResponse && HasDevPermission())) { | |
229 try_catch.ThrowException("Cannot set properties with the name postMessage" | |
230 "or postMessageAndAwaitResponse"); | |
231 return true; | |
232 } | |
233 | |
234 // We don't forward this to the passthrough object; no plugins use that | |
235 // feature. | |
236 // TODO(raymes): Remove SetProperty support from PPP_Class. | |
237 | |
238 return false; | |
239 } | |
240 | |
241 std::vector<std::string> MessageChannel::EnumerateNamedProperties( | |
242 v8::Isolate* isolate) { | |
243 std::vector<std::string> result; | |
244 PluginObject* plugin_object = GetPluginObject(isolate); | |
245 if (plugin_object) | |
246 result = plugin_object->EnumerateNamedProperties(isolate); | |
247 result.push_back(kPostMessage); | |
248 if (HasDevPermission()) | |
249 result.push_back(kPostMessageAndAwaitResponse); | |
250 return result; | |
251 } | |
252 | |
253 void MessageChannel::PostMessageToNative(gin::Arguments* args) { | |
254 if (!instance_) | |
255 return; | |
256 if (args->Length() != 1) { | |
257 // TODO(raymes): Consider throwing an exception here. We don't now for | |
258 // backward compatibility. | |
259 return; | |
260 } | |
261 | |
262 v8::Handle<v8::Value> message_data; | |
263 if (!args->GetNext(&message_data)) { | |
264 NOTREACHED(); | |
265 } | |
266 | |
267 EnqueuePluginMessage(message_data); | 403 EnqueuePluginMessage(message_data); |
268 DrainCompletedPluginMessages(); | 404 DrainCompletedPluginMessages(); |
269 } | 405 } |
270 | 406 |
271 void MessageChannel::PostBlockingMessageToNative(gin::Arguments* args) { | 407 void MessageChannel::PostBlockingMessageToNative(const NPVariant* message_data, |
272 if (!instance_) | 408 NPVariant* np_result) { |
273 return; | |
274 PepperTryCatchV8 try_catch(instance_, V8VarConverter::kDisallowObjectVars, | |
275 args->isolate()); | |
276 if (args->Length() != 1) { | |
277 try_catch.ThrowException( | |
278 "postMessageAndAwaitResponse requires one argument"); | |
279 return; | |
280 } | |
281 | |
282 v8::Handle<v8::Value> message_data; | |
283 if (!args->GetNext(&message_data)) { | |
284 NOTREACHED(); | |
285 } | |
286 | |
287 if (early_message_queue_state_ == QUEUE_MESSAGES) { | 409 if (early_message_queue_state_ == QUEUE_MESSAGES) { |
288 try_catch.ThrowException( | 410 WebBindings::setException( |
| 411 np_object_, |
289 "Attempted to call a synchronous method on a plugin that was not " | 412 "Attempted to call a synchronous method on a plugin that was not " |
290 "yet loaded."); | 413 "yet loaded."); |
291 return; | 414 return; |
292 } | 415 } |
293 | 416 |
294 // If the queue of messages to the plugin is non-empty, we're still waiting on | 417 // If the queue of messages to the plugin is non-empty, we're still waiting on |
295 // pending Var conversions. This means at some point in the past, JavaScript | 418 // pending Var conversions. This means at some point in the past, JavaScript |
296 // called postMessage (the async one) and passed us something with a browser- | 419 // called postMessage (the async one) and passed us something with a browser- |
297 // side host (e.g., FileSystem) and we haven't gotten a response from the | 420 // side host (e.g., FileSystem) and we haven't gotten a response from the |
298 // browser yet. We can't currently support sending a sync message if the | 421 // browser yet. We can't currently support sending a sync message if the |
299 // plugin does this, because it will break the ordering of the messages | 422 // plugin does this, because it will break the ordering of the messages |
300 // arriving at the plugin. | 423 // arriving at the plugin. |
301 // TODO(dmichael): Fix this. | 424 // TODO(dmichael): Fix this. |
302 // See https://code.google.com/p/chromium/issues/detail?id=367896#c4 | 425 // See https://code.google.com/p/chromium/issues/detail?id=367896#c4 |
303 if (!plugin_message_queue_.empty()) { | 426 if (!plugin_message_queue_.empty()) { |
304 try_catch.ThrowException( | 427 WebBindings::setException( |
| 428 np_object_, |
305 "Failed to convert parameter synchronously, because a prior " | 429 "Failed to convert parameter synchronously, because a prior " |
306 "call to postMessage contained a type which required asynchronous " | 430 "call to postMessage contained a type which required asynchronous " |
307 "transfer which has not completed. Not all types are supported yet by " | 431 "transfer which has not completed. Not all types are supported yet by " |
308 "postMessageAndAwaitResponse. See crbug.com/367896."); | 432 "postMessageAndAwaitResponse. See crbug.com/367896."); |
309 return; | 433 return; |
310 } | 434 } |
311 ScopedPPVar param = try_catch.FromV8(message_data); | 435 ScopedPPVar param; |
312 if (try_catch.ThrowException()) | 436 if (message_data->type == NPVariantType_Object) { |
313 return; | 437 // Convert NPVariantType_Object in to an appropriate PP_Var like Dictionary, |
314 | 438 // Array, etc. Note NPVariantToVar would convert to an "Object" PP_Var, |
| 439 // which we don't support for Messaging. |
| 440 v8::Handle<v8::Value> v8_value = WebBindings::toV8Value(message_data); |
| 441 V8VarConverter v8_var_converter(instance_->pp_instance()); |
| 442 bool success = v8_var_converter.FromV8ValueSync( |
| 443 v8_value, |
| 444 v8::Isolate::GetCurrent()->GetCurrentContext(), |
| 445 ¶m); |
| 446 if (!success) { |
| 447 WebBindings::setException( |
| 448 np_object_, |
| 449 "Failed to convert the given parameter to a PP_Var to send to " |
| 450 "the plugin."); |
| 451 return; |
| 452 } |
| 453 } else { |
| 454 param = ScopedPPVar(ScopedPPVar::PassRef(), |
| 455 NPVariantToPPVar(instance(), message_data)); |
| 456 } |
315 ScopedPPVar pp_result; | 457 ScopedPPVar pp_result; |
316 bool was_handled = instance_->HandleBlockingMessage(param, &pp_result); | 458 bool was_handled = instance_->HandleBlockingMessage(param, &pp_result); |
317 if (!was_handled) { | 459 if (!was_handled) { |
318 try_catch.ThrowException( | 460 WebBindings::setException( |
| 461 np_object_, |
319 "The plugin has not registered a handler for synchronous messages. " | 462 "The plugin has not registered a handler for synchronous messages. " |
320 "See the documentation for PPB_Messaging::RegisterMessageHandler " | 463 "See the documentation for PPB_Messaging::RegisterMessageHandler " |
321 "and PPP_MessageHandler."); | 464 "and PPP_MessageHandler."); |
322 return; | 465 return; |
323 } | 466 } |
324 v8::Handle<v8::Value> v8_result = try_catch.ToV8(pp_result.get()); | 467 v8::Handle<v8::Value> v8_val; |
325 if (try_catch.ThrowException()) | 468 if (!V8VarConverter(instance_->pp_instance()).ToV8Value( |
| 469 pp_result.get(), |
| 470 v8::Isolate::GetCurrent()->GetCurrentContext(), |
| 471 &v8_val)) { |
| 472 WebBindings::setException( |
| 473 np_object_, |
| 474 "Failed to convert the plugin's result to a JavaScript type."); |
326 return; | 475 return; |
327 | 476 } |
328 args->Return(v8_result); | 477 // Success! Convert the result to an NPVariant. |
| 478 WebBindings::toNPVariant(v8_val, NULL, np_result); |
329 } | 479 } |
330 | 480 |
331 void MessageChannel::PostMessageToJavaScriptImpl( | 481 void MessageChannel::PostMessageToJavaScriptImpl( |
332 const WebSerializedScriptValue& message_data) { | 482 const WebSerializedScriptValue& message_data) { |
333 DCHECK(instance_); | 483 DCHECK(instance_); |
334 | 484 |
335 WebPluginContainer* container = instance_->container(); | 485 WebPluginContainer* container = instance_->container(); |
336 // It's possible that container() is NULL if the plugin has been removed from | 486 // It's possible that container() is NULL if the plugin has been removed from |
337 // the DOM (but the PluginInstance is not destroyed yet). | 487 // the DOM (but the PluginInstance is not destroyed yet). |
338 if (!container) | 488 if (!container) |
(...skipping 13 matching lines...) Expand all Loading... |
352 // sent messages, while |source| is only specified for cross-document | 502 // sent messages, while |source| is only specified for cross-document |
353 // messages: | 503 // messages: |
354 // http://www.whatwg.org/specs/web-apps/current-work/multipage/comms.html | 504 // http://www.whatwg.org/specs/web-apps/current-work/multipage/comms.html |
355 // This currently behaves like Web Workers. On Firefox, Chrome, and Safari | 505 // This currently behaves like Web Workers. On Firefox, Chrome, and Safari |
356 // at least, postMessage on Workers does not provide the origin or source. | 506 // at least, postMessage on Workers does not provide the origin or source. |
357 // TODO(dmichael): Add origin if we change to a more iframe-like origin | 507 // TODO(dmichael): Add origin if we change to a more iframe-like origin |
358 // policy (see crbug.com/81537) | 508 // policy (see crbug.com/81537) |
359 container->element().dispatchEvent(msg_event); | 509 container->element().dispatchEvent(msg_event); |
360 } | 510 } |
361 | 511 |
362 PluginObject* MessageChannel::GetPluginObject(v8::Isolate* isolate) { | 512 void MessageChannel::EnqueuePluginMessage(const NPVariant* variant) { |
363 return PluginObject::FromV8Object(isolate, | 513 plugin_message_queue_.push_back(VarConversionResult()); |
364 v8::Local<v8::Object>::New(isolate, passthrough_object_)); | 514 if (variant->type == NPVariantType_Object) { |
365 } | 515 // Convert NPVariantType_Object in to an appropriate PP_Var like Dictionary, |
| 516 // Array, etc. Note NPVariantToVar would convert to an "Object" PP_Var, |
| 517 // which we don't support for Messaging. |
366 | 518 |
367 void MessageChannel::EnqueuePluginMessage(v8::Handle<v8::Value> v8_value) { | 519 // Calling WebBindings::toV8Value creates a wrapper around NPVariant so it |
368 plugin_message_queue_.push_back(VarConversionResult()); | 520 // won't result in a deep copy. |
369 // Convert NPVariantType_Object in to an appropriate PP_Var like Dictionary, | 521 v8::Handle<v8::Value> v8_value = WebBindings::toV8Value(variant); |
370 // Array, etc. Note NPVariantToVar would convert to an "Object" PP_Var, | 522 V8VarConverter v8_var_converter(instance_->pp_instance()); |
371 // which we don't support for Messaging. | 523 V8VarConverter::VarResult conversion_result = |
372 // TODO(raymes): Possibly change this to use TryCatch to do the conversion and | 524 v8_var_converter.FromV8Value( |
373 // throw an exception if necessary. | 525 v8_value, |
374 V8VarConverter v8_var_converter(instance_->pp_instance()); | 526 v8::Isolate::GetCurrent()->GetCurrentContext(), |
375 V8VarConverter::VarResult conversion_result = | 527 base::Bind(&MessageChannel::FromV8ValueComplete, |
376 v8_var_converter.FromV8Value( | 528 weak_ptr_factory_.GetWeakPtr(), |
377 v8_value, | 529 &plugin_message_queue_.back())); |
378 v8::Isolate::GetCurrent()->GetCurrentContext(), | 530 if (conversion_result.completed_synchronously) { |
379 base::Bind(&MessageChannel::FromV8ValueComplete, | 531 plugin_message_queue_.back().ConversionCompleted( |
380 weak_ptr_factory_.GetWeakPtr(), | 532 conversion_result.var, |
381 &plugin_message_queue_.back())); | 533 conversion_result.success); |
382 if (conversion_result.completed_synchronously) { | 534 } |
| 535 } else { |
383 plugin_message_queue_.back().ConversionCompleted( | 536 plugin_message_queue_.back().ConversionCompleted( |
384 conversion_result.var, | 537 ScopedPPVar(ScopedPPVar::PassRef(), |
385 conversion_result.success); | 538 NPVariantToPPVar(instance(), variant)), |
| 539 true); |
| 540 DCHECK(plugin_message_queue_.back().var().get().type != PP_VARTYPE_OBJECT); |
386 } | 541 } |
387 } | 542 } |
388 | 543 |
389 void MessageChannel::FromV8ValueComplete(VarConversionResult* result_holder, | 544 void MessageChannel::FromV8ValueComplete(VarConversionResult* result_holder, |
390 const ScopedPPVar& result, | 545 const ScopedPPVar& result, |
391 bool success) { | 546 bool success) { |
392 if (!instance_) | |
393 return; | |
394 result_holder->ConversionCompleted(result, success); | 547 result_holder->ConversionCompleted(result, success); |
395 DrainCompletedPluginMessages(); | 548 DrainCompletedPluginMessages(); |
396 } | 549 } |
397 | 550 |
398 void MessageChannel::DrainCompletedPluginMessages() { | 551 void MessageChannel::DrainCompletedPluginMessages() { |
399 DCHECK(instance_); | |
400 if (early_message_queue_state_ == QUEUE_MESSAGES) | 552 if (early_message_queue_state_ == QUEUE_MESSAGES) |
401 return; | 553 return; |
402 | 554 |
403 while (!plugin_message_queue_.empty() && | 555 while (!plugin_message_queue_.empty() && |
404 plugin_message_queue_.front().conversion_completed()) { | 556 plugin_message_queue_.front().conversion_completed()) { |
405 const VarConversionResult& front = plugin_message_queue_.front(); | 557 const VarConversionResult& front = plugin_message_queue_.front(); |
406 if (front.success()) { | 558 if (front.success()) { |
407 instance_->HandleMessage(front.var()); | 559 instance_->HandleMessage(front.var()); |
408 } else { | 560 } else { |
409 PpapiGlobals::Get()->LogWithSource(instance()->pp_instance(), | 561 PpapiGlobals::Get()->LogWithSource(instance()->pp_instance(), |
410 PP_LOGLEVEL_ERROR, | 562 PP_LOGLEVEL_ERROR, |
411 std::string(), | 563 std::string(), |
412 kV8ToVarConversionError); | 564 kV8ToVarConversionError); |
413 } | 565 } |
414 plugin_message_queue_.pop_front(); | 566 plugin_message_queue_.pop_front(); |
415 } | 567 } |
416 } | 568 } |
417 | 569 |
418 void MessageChannel::DrainEarlyMessageQueue() { | 570 void MessageChannel::DrainEarlyMessageQueue() { |
419 if (!instance_) | |
420 return; | |
421 DCHECK(early_message_queue_state_ == QUEUE_MESSAGES); | 571 DCHECK(early_message_queue_state_ == QUEUE_MESSAGES); |
422 | 572 |
423 // Take a reference on the PluginInstance. This is because JavaScript code | 573 // Take a reference on the PluginInstance. This is because JavaScript code |
424 // may delete the plugin, which would destroy the PluginInstance and its | 574 // may delete the plugin, which would destroy the PluginInstance and its |
425 // corresponding MessageChannel. | 575 // corresponding MessageChannel. |
426 scoped_refptr<PepperPluginInstanceImpl> instance_ref(instance_); | 576 scoped_refptr<PepperPluginInstanceImpl> instance_ref(instance_); |
427 while (!early_message_queue_.empty()) { | 577 while (!early_message_queue_.empty()) { |
428 PostMessageToJavaScriptImpl(early_message_queue_.front()); | 578 PostMessageToJavaScriptImpl(early_message_queue_.front()); |
429 early_message_queue_.pop_front(); | 579 early_message_queue_.pop_front(); |
430 } | 580 } |
431 early_message_queue_state_ = SEND_DIRECTLY; | 581 early_message_queue_state_ = SEND_DIRECTLY; |
432 | 582 |
433 DrainCompletedPluginMessages(); | 583 DrainCompletedPluginMessages(); |
434 } | 584 } |
435 | 585 |
436 } // namespace content | 586 } // namespace content |
OLD | NEW |