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