OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "webkit/plugins/ppapi/npapi_glue.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "base/memory/ref_counted.h" | |
9 #include "base/strings/string_util.h" | |
10 #include "ppapi/c/pp_var.h" | |
11 #include "third_party/npapi/bindings/npapi.h" | |
12 #include "third_party/npapi/bindings/npruntime.h" | |
13 #include "third_party/WebKit/public/web/WebBindings.h" | |
14 #include "third_party/WebKit/public/web/WebDocument.h" | |
15 #include "third_party/WebKit/public/web/WebElement.h" | |
16 #include "third_party/WebKit/public/web/WebFrame.h" | |
17 #include "third_party/WebKit/public/web/WebPluginContainer.h" | |
18 #include "v8/include/v8.h" | |
19 #include "webkit/plugins/ppapi/host_array_buffer_var.h" | |
20 #include "webkit/plugins/ppapi/host_globals.h" | |
21 #include "webkit/plugins/ppapi/host_var_tracker.h" | |
22 #include "webkit/plugins/ppapi/npobject_var.h" | |
23 #include "webkit/plugins/ppapi/plugin_module.h" | |
24 #include "webkit/plugins/ppapi/plugin_object.h" | |
25 #include "webkit/plugins/ppapi/ppapi_plugin_instance_impl.h" | |
26 | |
27 using ppapi::NPObjectVar; | |
28 using ppapi::PpapiGlobals; | |
29 using ppapi::StringVar; | |
30 using ppapi::Var; | |
31 using WebKit::WebArrayBuffer; | |
32 using WebKit::WebBindings; | |
33 using WebKit::WebPluginContainer; | |
34 | |
35 namespace webkit { | |
36 namespace ppapi { | |
37 | |
38 namespace { | |
39 | |
40 const char kInvalidPluginValue[] = "Error: Plugin returned invalid value."; | |
41 | |
42 PP_Var NPObjectToPPVarImpl(PluginInstanceImpl* instance, | |
43 NPObject* object, | |
44 v8::Local<v8::Context> context) { | |
45 DCHECK(object); | |
46 if (context.IsEmpty()) | |
47 return PP_MakeUndefined(); | |
48 v8::Context::Scope context_scope(context); | |
49 | |
50 WebArrayBuffer buffer; | |
51 // TODO(dmichael): Should I protect against duplicate Vars representing the | |
52 // same array buffer? It's probably not worth the trouble, since it will only | |
53 // affect in-process plugins. | |
54 if (WebBindings::getArrayBuffer(object, &buffer)) { | |
55 scoped_refptr<HostArrayBufferVar> buffer_var( | |
56 new HostArrayBufferVar(buffer)); | |
57 return buffer_var->GetPPVar(); | |
58 } | |
59 scoped_refptr<NPObjectVar> object_var( | |
60 HostGlobals::Get()->host_var_tracker()->NPObjectVarForNPObject( | |
61 instance->pp_instance(), object)); | |
62 if (!object_var.get()) { // No object for this module yet, make a new one. | |
63 object_var = new NPObjectVar(instance->pp_instance(), object); | |
64 } | |
65 return object_var->GetPPVar(); | |
66 } | |
67 | |
68 | |
69 } // namespace | |
70 | |
71 // Utilities ------------------------------------------------------------------- | |
72 | |
73 bool PPVarToNPVariant(PP_Var var, NPVariant* result) { | |
74 switch (var.type) { | |
75 case PP_VARTYPE_UNDEFINED: | |
76 VOID_TO_NPVARIANT(*result); | |
77 break; | |
78 case PP_VARTYPE_NULL: | |
79 NULL_TO_NPVARIANT(*result); | |
80 break; | |
81 case PP_VARTYPE_BOOL: | |
82 BOOLEAN_TO_NPVARIANT(var.value.as_bool, *result); | |
83 break; | |
84 case PP_VARTYPE_INT32: | |
85 INT32_TO_NPVARIANT(var.value.as_int, *result); | |
86 break; | |
87 case PP_VARTYPE_DOUBLE: | |
88 DOUBLE_TO_NPVARIANT(var.value.as_double, *result); | |
89 break; | |
90 case PP_VARTYPE_STRING: { | |
91 StringVar* string = StringVar::FromPPVar(var); | |
92 if (!string) { | |
93 VOID_TO_NPVARIANT(*result); | |
94 return false; | |
95 } | |
96 const std::string& value = string->value(); | |
97 char* c_string = static_cast<char*>(malloc(value.size())); | |
98 memcpy(c_string, value.data(), value.size()); | |
99 STRINGN_TO_NPVARIANT(c_string, value.size(), *result); | |
100 break; | |
101 } | |
102 case PP_VARTYPE_OBJECT: { | |
103 scoped_refptr<NPObjectVar> object(NPObjectVar::FromPPVar(var)); | |
104 if (!object.get()) { | |
105 VOID_TO_NPVARIANT(*result); | |
106 return false; | |
107 } | |
108 OBJECT_TO_NPVARIANT(WebBindings::retainObject(object->np_object()), | |
109 *result); | |
110 break; | |
111 } | |
112 // The following types are not supported for use with PPB_Var_Deprecated, | |
113 // because PPB_Var_Deprecated is only for trusted plugins, and the trusted | |
114 // plugins we have don't need these types. We can add support in the future | |
115 // if it becomes necessary. | |
116 case PP_VARTYPE_ARRAY: | |
117 case PP_VARTYPE_DICTIONARY: | |
118 case PP_VARTYPE_ARRAY_BUFFER: | |
119 VOID_TO_NPVARIANT(*result); | |
120 break; | |
121 } | |
122 return true; | |
123 } | |
124 | |
125 PP_Var NPVariantToPPVar(PluginInstanceImpl* instance, | |
126 const NPVariant* variant) { | |
127 switch (variant->type) { | |
128 case NPVariantType_Void: | |
129 return PP_MakeUndefined(); | |
130 case NPVariantType_Null: | |
131 return PP_MakeNull(); | |
132 case NPVariantType_Bool: | |
133 return PP_MakeBool(PP_FromBool(NPVARIANT_TO_BOOLEAN(*variant))); | |
134 case NPVariantType_Int32: | |
135 return PP_MakeInt32(NPVARIANT_TO_INT32(*variant)); | |
136 case NPVariantType_Double: | |
137 return PP_MakeDouble(NPVARIANT_TO_DOUBLE(*variant)); | |
138 case NPVariantType_String: | |
139 return StringVar::StringToPPVar( | |
140 NPVARIANT_TO_STRING(*variant).UTF8Characters, | |
141 NPVARIANT_TO_STRING(*variant).UTF8Length); | |
142 case NPVariantType_Object: | |
143 return NPObjectToPPVar(instance, NPVARIANT_TO_OBJECT(*variant)); | |
144 } | |
145 NOTREACHED(); | |
146 return PP_MakeUndefined(); | |
147 } | |
148 | |
149 NPIdentifier PPVarToNPIdentifier(PP_Var var) { | |
150 switch (var.type) { | |
151 case PP_VARTYPE_STRING: { | |
152 StringVar* string = StringVar::FromPPVar(var); | |
153 if (!string) | |
154 return NULL; | |
155 return WebBindings::getStringIdentifier(string->value().c_str()); | |
156 } | |
157 case PP_VARTYPE_INT32: | |
158 return WebBindings::getIntIdentifier(var.value.as_int); | |
159 default: | |
160 return NULL; | |
161 } | |
162 } | |
163 | |
164 PP_Var NPIdentifierToPPVar(NPIdentifier id) { | |
165 const NPUTF8* string_value = NULL; | |
166 int32_t int_value = 0; | |
167 bool is_string = false; | |
168 WebBindings::extractIdentifierData(id, string_value, int_value, is_string); | |
169 if (is_string) | |
170 return StringVar::StringToPPVar(string_value); | |
171 | |
172 return PP_MakeInt32(int_value); | |
173 } | |
174 | |
175 PP_Var NPObjectToPPVar(PluginInstanceImpl* instance, NPObject* object) { | |
176 WebPluginContainer* container = instance->container(); | |
177 // It's possible that container() is NULL if the plugin has been removed from | |
178 // the DOM (but the PluginInstance is not destroyed yet). | |
179 if (!container) | |
180 return PP_MakeUndefined(); | |
181 v8::HandleScope scope(instance->GetIsolate()); | |
182 v8::Local<v8::Context> context = | |
183 container->element().document().frame()->mainWorldScriptContext(); | |
184 return NPObjectToPPVarImpl(instance, object, context); | |
185 } | |
186 | |
187 PP_Var NPObjectToPPVarForTest(PluginInstanceImpl* instance, NPObject* object) { | |
188 v8::Isolate* test_isolate = v8::Isolate::New(); | |
189 PP_Var result = PP_MakeUndefined(); | |
190 { | |
191 v8::HandleScope scope(test_isolate); | |
192 v8::Isolate::Scope isolate_scope(test_isolate); | |
193 v8::Local<v8::Context> context = v8::Context::New(test_isolate); | |
194 result = NPObjectToPPVarImpl(instance, object, context); | |
195 } | |
196 test_isolate->Dispose(); | |
197 return result; | |
198 } | |
199 | |
200 // PPResultAndExceptionToNPResult ---------------------------------------------- | |
201 | |
202 PPResultAndExceptionToNPResult::PPResultAndExceptionToNPResult( | |
203 NPObject* object_var, | |
204 NPVariant* np_result) | |
205 : object_var_(object_var), | |
206 np_result_(np_result), | |
207 exception_(PP_MakeUndefined()), | |
208 success_(false), | |
209 checked_exception_(false) { | |
210 } | |
211 | |
212 PPResultAndExceptionToNPResult::~PPResultAndExceptionToNPResult() { | |
213 // The user should have called SetResult or CheckExceptionForNoResult | |
214 // before letting this class go out of scope, or the exception will have | |
215 // been lost. | |
216 DCHECK(checked_exception_); | |
217 | |
218 PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(exception_); | |
219 } | |
220 | |
221 // Call this with the return value of the PPAPI function. It will convert | |
222 // the result to the NPVariant output parameter and pass any exception on to | |
223 // the JS engine. It will update the success flag and return it. | |
224 bool PPResultAndExceptionToNPResult::SetResult(PP_Var result) { | |
225 DCHECK(!checked_exception_); // Don't call more than once. | |
226 DCHECK(np_result_); // Should be expecting a result. | |
227 | |
228 checked_exception_ = true; | |
229 | |
230 if (has_exception()) { | |
231 ThrowException(); | |
232 success_ = false; | |
233 } else if (!PPVarToNPVariant(result, np_result_)) { | |
234 WebBindings::setException(object_var_, kInvalidPluginValue); | |
235 success_ = false; | |
236 } else { | |
237 success_ = true; | |
238 } | |
239 | |
240 // No matter what happened, we need to release the reference to the | |
241 // value passed in. On success, a reference to this value will be in | |
242 // the np_result_. | |
243 PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(result); | |
244 return success_; | |
245 } | |
246 | |
247 // Call this after calling a PPAPI function that could have set the | |
248 // exception. It will pass the exception on to the JS engine and update | |
249 // the success flag. | |
250 // | |
251 // The success flag will be returned. | |
252 bool PPResultAndExceptionToNPResult::CheckExceptionForNoResult() { | |
253 DCHECK(!checked_exception_); // Don't call more than once. | |
254 DCHECK(!np_result_); // Can't have a result when doing this. | |
255 | |
256 checked_exception_ = true; | |
257 | |
258 if (has_exception()) { | |
259 ThrowException(); | |
260 success_ = false; | |
261 return false; | |
262 } | |
263 success_ = true; | |
264 return true; | |
265 } | |
266 | |
267 // Call this to ignore any exception. This prevents the DCHECK from failing | |
268 // in the destructor. | |
269 void PPResultAndExceptionToNPResult::IgnoreException() { | |
270 checked_exception_ = true; | |
271 } | |
272 | |
273 // Throws the current exception to JS. The exception must be set. | |
274 void PPResultAndExceptionToNPResult::ThrowException() { | |
275 StringVar* string = StringVar::FromPPVar(exception_); | |
276 if (string) | |
277 WebBindings::setException(object_var_, string->value().c_str()); | |
278 } | |
279 | |
280 // PPVarArrayFromNPVariantArray ------------------------------------------------ | |
281 | |
282 PPVarArrayFromNPVariantArray::PPVarArrayFromNPVariantArray( | |
283 PluginInstanceImpl* instance, | |
284 size_t size, | |
285 const NPVariant* variants) | |
286 : size_(size) { | |
287 if (size_ > 0) { | |
288 array_.reset(new PP_Var[size_]); | |
289 for (size_t i = 0; i < size_; i++) | |
290 array_[i] = NPVariantToPPVar(instance, &variants[i]); | |
291 } | |
292 } | |
293 | |
294 PPVarArrayFromNPVariantArray::~PPVarArrayFromNPVariantArray() { | |
295 ::ppapi::VarTracker* var_tracker = PpapiGlobals::Get()->GetVarTracker(); | |
296 for (size_t i = 0; i < size_; i++) | |
297 var_tracker->ReleaseVar(array_[i]); | |
298 } | |
299 | |
300 // PPVarFromNPObject ----------------------------------------------------------- | |
301 | |
302 PPVarFromNPObject::PPVarFromNPObject(PluginInstanceImpl* instance, | |
303 NPObject* object) | |
304 : var_(NPObjectToPPVar(instance, object)) { | |
305 } | |
306 | |
307 PPVarFromNPObject::~PPVarFromNPObject() { | |
308 PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(var_); | |
309 } | |
310 | |
311 // NPObjectAccessorWithIdentifier ---------------------------------------------- | |
312 | |
313 NPObjectAccessorWithIdentifier::NPObjectAccessorWithIdentifier( | |
314 NPObject* object, | |
315 NPIdentifier identifier, | |
316 bool allow_integer_identifier) | |
317 : object_(PluginObject::FromNPObject(object)), | |
318 identifier_(PP_MakeUndefined()) { | |
319 if (object_) { | |
320 identifier_ = NPIdentifierToPPVar(identifier); | |
321 if (identifier_.type == PP_VARTYPE_INT32 && !allow_integer_identifier) | |
322 identifier_.type = PP_VARTYPE_UNDEFINED; // Mark it invalid. | |
323 } | |
324 } | |
325 | |
326 NPObjectAccessorWithIdentifier::~NPObjectAccessorWithIdentifier() { | |
327 PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(identifier_); | |
328 } | |
329 | |
330 // TryCatch -------------------------------------------------------------------- | |
331 | |
332 TryCatch::TryCatch(PP_Var* exception) | |
333 : has_exception_(exception && exception->type != PP_VARTYPE_UNDEFINED), | |
334 exception_(exception) { | |
335 WebBindings::pushExceptionHandler(&TryCatch::Catch, this); | |
336 } | |
337 | |
338 TryCatch::~TryCatch() { | |
339 WebBindings::popExceptionHandler(); | |
340 } | |
341 | |
342 void TryCatch::SetException(const char* message) { | |
343 if (!has_exception()) { | |
344 has_exception_ = true; | |
345 if (exception_) { | |
346 *exception_ = ::ppapi::StringVar::StringToPPVar(message, strlen(message)); | |
347 } | |
348 } | |
349 } | |
350 | |
351 // static | |
352 void TryCatch::Catch(void* self, const char* message) { | |
353 static_cast<TryCatch*>(self)->SetException(message); | |
354 } | |
355 | |
356 } // namespace ppapi | |
357 } // namespace webkit | |
OLD | NEW |