1 /*
2 * Copyright (C) 2006, 2007, 2008, 2009 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "config.h"
32
33 #include "V8NPObject.h"
34
35 #include "HTMLPlugInElement.h"
36 #include "NPV8Object.h"
37 #include "V8CustomBinding.h"
38 #include "V8DOMMap.h"
39 #include "V8HTMLAppletElement.h"
40 #include "V8HTMLEmbedElement.h"
41 #include "V8HTMLObjectElement.h"
42 #include "V8Helpers.h"
43 #include "V8NPUtils.h"
44 #include "V8Proxy.h"
45 #include "npruntime_impl.h"
46 #include "npruntime_priv.h"
47 #include "wtf/OwnArrayPtr.h"
48
49 using namespace WebCore;
50
51 enum InvokeFunctionType {
52 InvokeMethod = 1,
53 InvokeConstruct = 2,
54 InvokeDefault = 3
55 };
56
57 // FIXME: need comments.
58 // Params: holder could be HTMLEmbedElement or NPObject
npObjectInvokeImpl(const v8::Arguments & args,InvokeFunctionType functionId)59 static v8::Handle<v8::Value> npObjectInvokeImpl(const v8::Arguments& args, InvokeFunctionType functionId)
60 {
61 NPObject* npObject;
62
63 // These three types are subtypes of HTMLPlugInElement.
64 if (V8HTMLAppletElement::HasInstance(args.Holder()) || V8HTMLEmbedElement::HasInstance(args.Holder())
65 || V8HTMLObjectElement::HasInstance(args.Holder())) {
66 // The holder object is a subtype of HTMLPlugInElement.
67 HTMLPlugInElement* element = V8DOMWrapper::convertDOMWrapperToNode<HTMLPlugInElement>(args.Holder());
68 ScriptInstance scriptInstance = element->getInstance();
69 if (scriptInstance)
70 npObject = V8DOMWrapper::convertToNativeObject<NPObject>(V8ClassIndex::NPOBJECT, scriptInstance->instance());
71 else
72 npObject = 0;
73 } else {
74 // The holder object is not a subtype of HTMLPlugInElement, it must be an NPObject which has three
75 // internal fields.
76 if (args.Holder()->InternalFieldCount() != V8Custom::kNPObjectInternalFieldCount)
77 return throwError("NPMethod called on non-NPObject", V8Proxy::ReferenceError);
78
79 npObject = V8DOMWrapper::convertToNativeObject<NPObject>(V8ClassIndex::NPOBJECT, args.Holder());
80 }
81
82 // Verify that our wrapper wasn't using a NPObject which has already been deleted.
83 if (!npObject || !_NPN_IsAlive(npObject))
84 return throwError("NPObject deleted", V8Proxy::ReferenceError);
85
86 // Wrap up parameters.
87 int numArgs = args.Length();
88 OwnArrayPtr<NPVariant> npArgs(new NPVariant[numArgs]);
89
90 for (int i = 0; i < numArgs; i++)
91 convertV8ObjectToNPVariant(args[i], npObject, &npArgs[i]);
92
93 NPVariant result;
94 VOID_TO_NPVARIANT(result);
95
96 switch (functionId) {
97 case InvokeMethod:
98 if (npObject->_class->invoke) {
99 v8::Handle<v8::String> functionName(v8::String::Cast(*args.Data()));
100 NPIdentifier identifier = getStringIdentifier(functionName);
101 npObject->_class->invoke(npObject, identifier, npArgs.get(), numArgs, &result);
102 }
103 break;
104 case InvokeConstruct:
105 if (npObject->_class->construct)
106 npObject->_class->construct(npObject, npArgs.get(), numArgs, &result);
107 break;
108 case InvokeDefault:
109 if (npObject->_class->invokeDefault)
110 npObject->_class->invokeDefault(npObject, npArgs.get(), numArgs, &result);
111 break;
112 default:
113 break;
114 }
115
116 for (int i=0; i < numArgs; i++)
117 _NPN_ReleaseVariantValue(&npArgs[i]);
118
119 // Unwrap return values.
120 v8::Handle<v8::Value> returnValue = convertNPVariantToV8Object(&result, npObject);
121 _NPN_ReleaseVariantValue(&result);
122
123 return returnValue;
124 }
125
126
npObjectMethodHandler(const v8::Arguments & args)127 v8::Handle<v8::Value> npObjectMethodHandler(const v8::Arguments& args)
128 {
129 return npObjectInvokeImpl(args, InvokeMethod);
130 }
131
132
npObjectInvokeDefaultHandler(const v8::Arguments & args)133 v8::Handle<v8::Value> npObjectInvokeDefaultHandler(const v8::Arguments& args)
134 {
135 if (args.IsConstructCall())
136 return npObjectInvokeImpl(args, InvokeConstruct);
137 else
138 return npObjectInvokeImpl(args, InvokeDefault);
139 }
140
141
142 static void weakTemplateCallback(v8::Persistent<v8::Value>, void* parameter);
143
144 // NPIdentifier is PrivateIdentifier*.
145 static WeakReferenceMap<PrivateIdentifier, v8::FunctionTemplate> staticTemplateMap(&weakTemplateCallback);
146
weakTemplateCallback(v8::Persistent<v8::Value> object,void * parameter)147 static void weakTemplateCallback(v8::Persistent<v8::Value> object, void* parameter)
148 {
149 PrivateIdentifier* identifier = static_cast<PrivateIdentifier*>(parameter);
150 ASSERT(identifier);
151 ASSERT(staticTemplateMap.contains(identifier));
152
153 staticTemplateMap.forget(identifier);
154 }
155
156
npObjectGetProperty(v8::Local<v8::Object> self,NPIdentifier identifier,v8::Local<v8::Value> key)157 static v8::Handle<v8::Value> npObjectGetProperty(v8::Local<v8::Object> self, NPIdentifier identifier, v8::Local<v8::Value> key)
158 {
159 NPObject* npObject = V8DOMWrapper::convertToNativeObject<NPObject>(V8ClassIndex::NPOBJECT, self);
160
161 // Verify that our wrapper wasn't using a NPObject which
162 // has already been deleted.
163 if (!npObject || !_NPN_IsAlive(npObject))
164 return throwError("NPObject deleted", V8Proxy::ReferenceError);
165
166
167 if (npObject->_class->hasProperty && npObject->_class->hasProperty(npObject, identifier)
168 && npObject->_class->getProperty) {
169
170 NPVariant result;
171 VOID_TO_NPVARIANT(result);
172 if (!npObject->_class->getProperty(npObject, identifier, &result))
173 return v8::Handle<v8::Value>();
174
175 v8::Handle<v8::Value> returnValue = convertNPVariantToV8Object(&result, npObject);
176 _NPN_ReleaseVariantValue(&result);
177 return returnValue;
178
179 } else if (key->IsString() && npObject->_class->hasMethod && npObject->_class->hasMethod(npObject, identifier)) {
180 PrivateIdentifier* id = static_cast<PrivateIdentifier*>(identifier);
181 v8::Persistent<v8::FunctionTemplate> functionTemplate = staticTemplateMap.get(id);
182 // Cache templates using identifier as the key.
183 if (functionTemplate.IsEmpty()) {
184 // Create a new template.
185 v8::Local<v8::FunctionTemplate> temp = v8::FunctionTemplate::New();
186 temp->SetCallHandler(npObjectMethodHandler, key);
187 functionTemplate = v8::Persistent<v8::FunctionTemplate>::New(temp);
188 staticTemplateMap.set(id, functionTemplate);
189 }
190
191 // FunctionTemplate caches function for each context.
192 v8::Local<v8::Function> v8Function = functionTemplate->GetFunction();
193 v8Function->SetName(v8::Handle<v8::String>::Cast(key));
194 return v8Function;
195 }
196
197 return v8::Handle<v8::Value>();
198 }
199
npObjectNamedPropertyGetter(v8::Local<v8::String> name,const v8::AccessorInfo & info)200 v8::Handle<v8::Value> npObjectNamedPropertyGetter(v8::Local<v8::String> name, const v8::AccessorInfo& info)
201 {
202 NPIdentifier identifier = getStringIdentifier(name);
203 return npObjectGetProperty(info.Holder(), identifier, name);
204 }
205
npObjectIndexedPropertyGetter(uint32_t index,const v8::AccessorInfo & info)206 v8::Handle<v8::Value> npObjectIndexedPropertyGetter(uint32_t index, const v8::AccessorInfo& info)
207 {
208 NPIdentifier identifier = _NPN_GetIntIdentifier(index);
209 return npObjectGetProperty(info.Holder(), identifier, v8::Number::New(index));
210 }
211
npObjectGetNamedProperty(v8::Local<v8::Object> self,v8::Local<v8::String> name)212 v8::Handle<v8::Value> npObjectGetNamedProperty(v8::Local<v8::Object> self, v8::Local<v8::String> name)
213 {
214 NPIdentifier identifier = getStringIdentifier(name);
215 return npObjectGetProperty(self, identifier, name);
216 }
217
npObjectGetIndexedProperty(v8::Local<v8::Object> self,uint32_t index)218 v8::Handle<v8::Value> npObjectGetIndexedProperty(v8::Local<v8::Object> self, uint32_t index)
219 {
220 NPIdentifier identifier = _NPN_GetIntIdentifier(index);
221 return npObjectGetProperty(self, identifier, v8::Number::New(index));
222 }
223
npObjectSetProperty(v8::Local<v8::Object> self,NPIdentifier identifier,v8::Local<v8::Value> value)224 static v8::Handle<v8::Value> npObjectSetProperty(v8::Local<v8::Object> self, NPIdentifier identifier, v8::Local<v8::Value> value)
225 {
226 NPObject* npObject = V8DOMWrapper::convertToNativeObject<NPObject>(V8ClassIndex::NPOBJECT, self);
227
228 // Verify that our wrapper wasn't using a NPObject which has already been deleted.
229 if (!npObject || !_NPN_IsAlive(npObject)) {
230 throwError("NPObject deleted", V8Proxy::ReferenceError);
231 return value; // Intercepted, but an exception was thrown.
232 }
233
234 if (npObject->_class->hasProperty && npObject->_class->hasProperty(npObject, identifier)
235 && npObject->_class->setProperty) {
236
237 NPVariant npValue;
238 VOID_TO_NPVARIANT(npValue);
239 convertV8ObjectToNPVariant(value, npObject, &npValue);
240 bool success = npObject->_class->setProperty(npObject, identifier, &npValue);
241 _NPN_ReleaseVariantValue(&npValue);
242 if (success)
243 return value; // Intercept the call.
244 }
245 return notHandledByInterceptor();
246 }
247
248
npObjectNamedPropertySetter(v8::Local<v8::String> name,v8::Local<v8::Value> value,const v8::AccessorInfo & info)249 v8::Handle<v8::Value> npObjectNamedPropertySetter(v8::Local<v8::String> name, v8::Local<v8::Value> value, const v8::AccessorInfo& info)
250 {
251 NPIdentifier identifier = getStringIdentifier(name);
252 return npObjectSetProperty(info.Holder(), identifier, value);
253 }
254
255
npObjectIndexedPropertySetter(uint32_t index,v8::Local<v8::Value> value,const v8::AccessorInfo & info)256 v8::Handle<v8::Value> npObjectIndexedPropertySetter(uint32_t index, v8::Local<v8::Value> value, const v8::AccessorInfo& info)
257 {
258 NPIdentifier identifier = _NPN_GetIntIdentifier(index);
259 return npObjectSetProperty(info.Holder(), identifier, value);
260 }
261
npObjectSetNamedProperty(v8::Local<v8::Object> self,v8::Local<v8::String> name,v8::Local<v8::Value> value)262 v8::Handle<v8::Value> npObjectSetNamedProperty(v8::Local<v8::Object> self, v8::Local<v8::String> name, v8::Local<v8::Value> value)
263 {
264 NPIdentifier identifier = getStringIdentifier(name);
265 return npObjectSetProperty(self, identifier, value);
266 }
267
npObjectSetIndexedProperty(v8::Local<v8::Object> self,uint32_t index,v8::Local<v8::Value> value)268 v8::Handle<v8::Value> npObjectSetIndexedProperty(v8::Local<v8::Object> self, uint32_t index, v8::Local<v8::Value> value)
269 {
270 NPIdentifier identifier = _NPN_GetIntIdentifier(index);
271 return npObjectSetProperty(self, identifier, value);
272 }
273
274
275 static void weakNPObjectCallback(v8::Persistent<v8::Value>, void* parameter);
276
277 static DOMWrapperMap<NPObject> staticNPObjectMap(&weakNPObjectCallback);
278
weakNPObjectCallback(v8::Persistent<v8::Value> object,void * parameter)279 static void weakNPObjectCallback(v8::Persistent<v8::Value> object, void* parameter)
280 {
281 NPObject* npObject = static_cast<NPObject*>(parameter);
282 ASSERT(staticNPObjectMap.contains(npObject));
283 ASSERT(npObject);
284
285 // Must remove from our map before calling _NPN_ReleaseObject(). _NPN_ReleaseObject can call ForgetV8ObjectForNPObject, which
286 // uses the table as well.
287 staticNPObjectMap.forget(npObject);
288
289 if (_NPN_IsAlive(npObject))
290 _NPN_ReleaseObject(npObject);
291 }
292
293
createV8ObjectForNPObject(NPObject * object,NPObject * root)294 v8::Local<v8::Object> createV8ObjectForNPObject(NPObject* object, NPObject* root)
295 {
296 static v8::Persistent<v8::FunctionTemplate> npObjectDesc;
297
298 ASSERT(v8::Context::InContext());
299
300 // If this is a v8 object, just return it.
301 if (object->_class == npScriptObjectClass) {
302 V8NPObject* v8NPObject = reinterpret_cast<V8NPObject*>(object);
303 return v8::Local<v8::Object>::New(v8NPObject->v8Object);
304 }
305
306 // If we've already wrapped this object, just return it.
307 if (staticNPObjectMap.contains(object))
308 return v8::Local<v8::Object>::New(staticNPObjectMap.get(object));
309
310 // FIXME: we should create a Wrapper type as a subclass of JSObject. It has two internal fields, field 0 is the wrapped
311 // pointer, and field 1 is the type. There should be an api function that returns unused type id. The same Wrapper type
312 // can be used by DOM bindings.
313 if (npObjectDesc.IsEmpty()) {
314 npObjectDesc = v8::Persistent<v8::FunctionTemplate>::New(v8::FunctionTemplate::New());
315 npObjectDesc->InstanceTemplate()->SetInternalFieldCount(V8Custom::kNPObjectInternalFieldCount);
316 npObjectDesc->InstanceTemplate()->SetNamedPropertyHandler(npObjectNamedPropertyGetter, npObjectNamedPropertySetter);
317 npObjectDesc->InstanceTemplate()->SetIndexedPropertyHandler(npObjectIndexedPropertyGetter, npObjectIndexedPropertySetter);
318 npObjectDesc->InstanceTemplate()->SetCallAsFunctionHandler(npObjectInvokeDefaultHandler);
319 }
320
321 v8::Handle<v8::Function> v8Function = npObjectDesc->GetFunction();
322 v8::Local<v8::Object> value = SafeAllocation::newInstance(v8Function);
323
324 // If we were unable to allocate the instance, we avoid wrapping and registering the NP object.
325 if (value.IsEmpty())
326 return value;
327
328 wrapNPObject(value, object);
329
330 // KJS retains the object as part of its wrapper (see Bindings::CInstance).
331 _NPN_RetainObject(object);
332
333 _NPN_RegisterObject(object, root);
334
335 // Maintain a weak pointer for v8 so we can cleanup the object.
336 v8::Persistent<v8::Object> weakRef = v8::Persistent<v8::Object>::New(value);
337 staticNPObjectMap.set(object, weakRef);
338
339 return value;
340 }
341
forgetV8ObjectForNPObject(NPObject * object)342 void forgetV8ObjectForNPObject(NPObject* object)
343 {
344 if (staticNPObjectMap.contains(object)) {
345 v8::HandleScope scope;
346 v8::Persistent<v8::Object> handle(staticNPObjectMap.get(object));
347 V8DOMWrapper::setDOMWrapper(handle, WebCore::V8ClassIndex::NPOBJECT, 0);
348 staticNPObjectMap.forget(object);
349 _NPN_ReleaseObject(object);
350 }
351 }
352