1 /*****************************************************************************
2 * runtime.cpp: support for NPRuntime API for Netscape Script-able plugins
3 * FYI: http://www.mozilla.org/projects/plugins/npruntime.html
4 *****************************************************************************
5 * Copyright (C) 2002-2005 the VideoLAN team
6 * $Id: RuntimeNPObject.h 14466 2006-02-22 23:34:54Z dionoea $
8 * Authors: Damien Fouilleul <damien.fouilleul@laposte.net>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 #ifndef __NPORUNTIME_H__
26 #define __NPORUNTIME_H__
29 ** support framework for runtime script objects
33 #include <npruntime.h>
35 static void RuntimeNPClassDeallocate(NPObject *npobj);
36 static void RuntimeNPClassInvalidate(NPObject *npobj);
37 static bool RuntimeNPClassInvokeDefault(NPObject *npobj,
38 const NPVariant *args,
42 class RuntimeNPObject : public NPObject
50 static bool isNumberValue(const NPVariant &v)
52 return NPVARIANT_IS_INT32(v)
53 || NPVARIANT_IS_DOUBLE(v);
56 static int numberValue(const NPVariant &v)
59 case NPVariantType_Int32:
60 return NPVARIANT_TO_INT32(v);
61 case NPVariantType_Double:
62 return(int)NPVARIANT_TO_DOUBLE(v);
68 static char* stringValue(const NPString &v);
69 static char* stringValue(const NPVariant &v);
73 RuntimeNPObject(NPP instance, const NPClass *aClass) :
76 _class = const_cast<NPClass *>(aClass);
79 virtual ~RuntimeNPObject() {};
83 INVOKERESULT_NO_ERROR = 0, /* returns no error */
84 INVOKERESULT_GENERIC_ERROR = 1, /* returns error */
85 INVOKERESULT_NO_SUCH_METHOD = 2, /* throws method does not exist */
86 INVOKERESULT_INVALID_ARGS = 3, /* throws invalid arguments */
87 INVOKERESULT_INVALID_VALUE = 4, /* throws invalid value in assignment */
88 INVOKERESULT_OUT_OF_MEMORY = 5, /* throws out of memory */
91 friend void RuntimeNPClassDeallocate(NPObject *npobj);
92 friend void RuntimeNPClassInvalidate(NPObject *npobj);
93 template <class RuntimeNPObject> friend bool RuntimeNPClassGetProperty(NPObject *npobj, NPIdentifier name, NPVariant *result);
94 template <class RuntimeNPObject> friend bool RuntimeNPClassSetProperty(NPObject *npobj, NPIdentifier name, const NPVariant *value);
95 template <class RuntimeNPObject> friend bool RuntimeNPClassRemoveProperty(NPObject *npobj, NPIdentifier name);
96 template <class RuntimeNPObject> friend bool RuntimeNPClassInvoke(NPObject *npobj, NPIdentifier name,
97 const NPVariant *args, uint32_t argCount,
99 friend bool RuntimeNPClassInvokeDefault(NPObject *npobj,
100 const NPVariant *args,
104 virtual InvokeResult getProperty(int index, NPVariant &result);
105 virtual InvokeResult setProperty(int index, const NPVariant &value);
106 virtual InvokeResult removeProperty(int index);
107 virtual InvokeResult invoke(int index, const NPVariant *args, uint32_t argCount, NPVariant &result);
108 virtual InvokeResult invokeDefault(const NPVariant *args, uint32_t argCount, NPVariant &result);
110 bool returnInvokeResult(InvokeResult result);
115 template<class T> class RuntimeNPClass : public NPClass
118 static NPClass *getClass()
120 static NPClass *singleton = new RuntimeNPClass<T>;
126 virtual ~RuntimeNPClass();
128 template <class RuntimeNPObject> friend NPObject *RuntimeNPClassAllocate(NPP instance, NPClass *aClass);
129 template <class RuntimeNPObject> friend bool RuntimeNPClassHasMethod(NPObject *npobj, NPIdentifier name);
130 template <class RuntimeNPObject> friend bool RuntimeNPClassHasProperty(NPObject *npobj, NPIdentifier name);
131 template <class RuntimeNPObject> friend bool RuntimeNPClassGetProperty(NPObject *npobj, NPIdentifier name, NPVariant *result);
132 template <class RuntimeNPObject> friend bool RuntimeNPClassSetProperty(NPObject *npobj, NPIdentifier name, const NPVariant *value);
133 template <class RuntimeNPObject> friend bool RuntimeNPClassRemoveProperty(NPObject *npobj, NPIdentifier name);
134 template <class RuntimeNPObject> friend bool RuntimeNPClassInvoke(NPObject *npobj, NPIdentifier name,
135 const NPVariant *args, uint32_t argCount,
138 RuntimeNPObject *create(NPP instance) const;
140 int indexOfMethod(NPIdentifier name) const;
141 int indexOfProperty(NPIdentifier name) const;
144 NPIdentifier *propertyIdentifiers;
145 NPIdentifier *methodIdentifiers;
149 static NPObject *RuntimeNPClassAllocate(NPP instance, NPClass *aClass)
151 const RuntimeNPClass<T> *vClass = static_cast<RuntimeNPClass<T> *>(aClass);
152 return (NPObject *)vClass->create(instance);
155 static void RuntimeNPClassDeallocate(NPObject *npobj)
157 RuntimeNPObject *vObj = static_cast<RuntimeNPObject *>(npobj);
161 static void RuntimeNPClassInvalidate(NPObject *npobj)
163 RuntimeNPObject *vObj = static_cast<RuntimeNPObject *>(npobj);
164 vObj->_instance = NULL;
168 static bool RuntimeNPClassHasMethod(NPObject *npobj, NPIdentifier name)
170 const RuntimeNPClass<T> *vClass = static_cast<RuntimeNPClass<T> *>(npobj->_class);
171 return vClass->indexOfMethod(name) != -1;
175 static bool RuntimeNPClassHasProperty(NPObject *npobj, NPIdentifier name)
177 const RuntimeNPClass<T> *vClass = static_cast<RuntimeNPClass<T> *>(npobj->_class);
178 return vClass->indexOfProperty(name) != -1;
182 static bool RuntimeNPClassGetProperty(NPObject *npobj, NPIdentifier name, NPVariant *result)
184 const RuntimeNPClass<T> *vClass = static_cast<RuntimeNPClass<T> *>(npobj->_class);
185 int index = vClass->indexOfProperty(name);
188 RuntimeNPObject *vObj = static_cast<RuntimeNPObject *>(npobj);
189 return vObj->returnInvokeResult(vObj->getProperty(index, *result));
195 static bool RuntimeNPClassSetProperty(NPObject *npobj, NPIdentifier name, const NPVariant *value)
197 const RuntimeNPClass<T> *vClass = static_cast<RuntimeNPClass<T> *>(npobj->_class);
198 int index = vClass->indexOfProperty(name);
201 RuntimeNPObject *vObj = static_cast<RuntimeNPObject *>(npobj);
202 return vObj->returnInvokeResult(vObj->setProperty(index, *value));
208 static bool RuntimeNPClassRemoveProperty(NPObject *npobj, NPIdentifier name)
210 const RuntimeNPClass<T> *vClass = static_cast<RuntimeNPClass<T> *>(npobj->_class);
211 int index = vClass->indexOfProperty(name);
214 RuntimeNPObject *vObj = static_cast<RuntimeNPObject *>(npobj);
215 return vObj->returnInvokeResult(vObj->removeProperty(index));
221 static bool RuntimeNPClassInvoke(NPObject *npobj, NPIdentifier name,
222 const NPVariant *args, uint32_t argCount,
225 const RuntimeNPClass<T> *vClass = static_cast<RuntimeNPClass<T> *>(npobj->_class);
226 int index = vClass->indexOfMethod(name);
229 RuntimeNPObject *vObj = static_cast<RuntimeNPObject *>(npobj);
230 return vObj->returnInvokeResult(vObj->invoke(index, args, argCount, *result));
236 static bool RuntimeNPClassInvokeDefault(NPObject *npobj,
237 const NPVariant *args,
241 RuntimeNPObject *vObj = static_cast<RuntimeNPObject *>(npobj);
242 return vObj->returnInvokeResult(vObj->invokeDefault(args, argCount, *result));
246 RuntimeNPClass<T>::RuntimeNPClass()
248 // retreive property identifiers from names
249 if( T::propertyCount > 0 )
251 propertyIdentifiers = new NPIdentifier[T::propertyCount];
252 if( propertyIdentifiers )
253 NPN_GetStringIdentifiers(const_cast<const NPUTF8**>(T::propertyNames),
254 T::propertyCount, propertyIdentifiers);
257 // retreive method identifiers from names
258 if( T::methodCount > 0 )
260 methodIdentifiers = new NPIdentifier[T::methodCount];
261 if( methodIdentifiers )
262 NPN_GetStringIdentifiers(const_cast<const NPUTF8**>(T::methodNames),
263 T::methodCount, methodIdentifiers);
266 // fill in NPClass structure
267 structVersion = NP_CLASS_STRUCT_VERSION;
268 allocate = &RuntimeNPClassAllocate<T>;
269 deallocate = &RuntimeNPClassDeallocate;
270 invalidate = &RuntimeNPClassInvalidate;
271 hasMethod = &RuntimeNPClassHasMethod<T>;
272 invoke = &RuntimeNPClassInvoke<T>;
273 invokeDefault = &RuntimeNPClassInvokeDefault;
274 hasProperty = &RuntimeNPClassHasProperty<T>;
275 getProperty = &RuntimeNPClassGetProperty<T>;
276 setProperty = &RuntimeNPClassSetProperty<T>;
277 removeProperty = &RuntimeNPClassRemoveProperty<T>;
281 RuntimeNPClass<T>::~RuntimeNPClass()
283 delete propertyIdentifiers;
284 delete methodIdentifiers;
288 RuntimeNPObject *RuntimeNPClass<T>::create(NPP instance) const
290 return new T(instance, this);
294 int RuntimeNPClass<T>::indexOfMethod(NPIdentifier name) const
296 if( methodIdentifiers )
298 for(int c=0; c< T::methodCount; ++c )
300 if( name == methodIdentifiers[c] )
308 int RuntimeNPClass<T>::indexOfProperty(NPIdentifier name) const
310 if( propertyIdentifiers )
312 for(int c=0; c< T::propertyCount; ++c )
314 if( name == propertyIdentifiers[c] )