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
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
45 // Lazy child object cration helper. Doing this avoids
46 // ownership problems with firefox.
47 template<class T
> void InstantObj( NPObject
*&obj
);
53 static bool isNumberValue(const NPVariant
&v
)
55 return NPVARIANT_IS_INT32(v
)
56 || NPVARIANT_IS_DOUBLE(v
);
59 static int numberValue(const NPVariant
&v
)
62 case NPVariantType_Int32
:
63 return NPVARIANT_TO_INT32(v
);
64 case NPVariantType_Double
:
65 return(int)NPVARIANT_TO_DOUBLE(v
);
71 static char* stringValue(const NPString
&v
);
72 static char* stringValue(const NPVariant
&v
);
75 void *operator new(size_t n
)
78 ** Assume that browser has a smarter memory allocator
79 ** than plain old malloc() and use it instead.
81 return NPN_MemAlloc(n
);
84 void operator delete(void *p
)
91 return _instance
!= NULL
;
94 RuntimeNPObject(NPP instance
, const NPClass
*aClass
) :
97 _class
= const_cast<NPClass
*>(aClass
);
100 virtual ~RuntimeNPObject() {};
104 INVOKERESULT_NO_ERROR
= 0, /* returns no error */
105 INVOKERESULT_GENERIC_ERROR
= 1, /* returns error */
106 INVOKERESULT_NO_SUCH_METHOD
= 2, /* throws method does not exist */
107 INVOKERESULT_INVALID_ARGS
= 3, /* throws invalid arguments */
108 INVOKERESULT_INVALID_VALUE
= 4, /* throws invalid value in assignment */
109 INVOKERESULT_OUT_OF_MEMORY
= 5, /* throws out of memory */
112 friend void RuntimeNPClassDeallocate(NPObject
*npobj
);
113 friend void RuntimeNPClassInvalidate(NPObject
*npobj
);
114 template <class RuntimeNPObject
> friend bool RuntimeNPClassGetProperty(NPObject
*npobj
, NPIdentifier name
, NPVariant
*result
);
115 template <class RuntimeNPObject
> friend bool RuntimeNPClassSetProperty(NPObject
*npobj
, NPIdentifier name
, const NPVariant
*value
);
116 template <class RuntimeNPObject
> friend bool RuntimeNPClassRemoveProperty(NPObject
*npobj
, NPIdentifier name
);
117 template <class RuntimeNPObject
> friend bool RuntimeNPClassInvoke(NPObject
*npobj
, NPIdentifier name
,
118 const NPVariant
*args
, uint32_t argCount
,
120 friend bool RuntimeNPClassInvokeDefault(NPObject
*npobj
,
121 const NPVariant
*args
,
125 virtual InvokeResult
getProperty(int index
, NPVariant
&result
);
126 virtual InvokeResult
setProperty(int index
, const NPVariant
&value
);
127 virtual InvokeResult
removeProperty(int index
);
128 virtual InvokeResult
invoke(int index
, const NPVariant
*args
, uint32_t argCount
, NPVariant
&result
);
129 virtual InvokeResult
invokeDefault(const NPVariant
*args
, uint32_t argCount
, NPVariant
&result
);
131 bool returnInvokeResult(InvokeResult result
);
133 static InvokeResult
invokeResultString(const char *,NPVariant
&);
135 bool isPluginRunning()
137 return (_instance
->pdata
!= NULL
);
139 template<class T
> T
*getPrivate()
141 return reinterpret_cast<T
*>(_instance
->pdata
);
147 template<class T
> class RuntimeNPClass
: public NPClass
150 static NPClass
*getClass()
152 static NPClass
*singleton
= new RuntimeNPClass
<T
>;
158 virtual ~RuntimeNPClass();
160 template <class RuntimeNPObject
> friend NPObject
*RuntimeNPClassAllocate(NPP instance
, NPClass
*aClass
);
161 template <class RuntimeNPObject
> friend bool RuntimeNPClassHasMethod(NPObject
*npobj
, NPIdentifier name
);
162 template <class RuntimeNPObject
> friend bool RuntimeNPClassHasProperty(NPObject
*npobj
, NPIdentifier name
);
163 template <class RuntimeNPObject
> friend bool RuntimeNPClassGetProperty(NPObject
*npobj
, NPIdentifier name
, NPVariant
*result
);
164 template <class RuntimeNPObject
> friend bool RuntimeNPClassSetProperty(NPObject
*npobj
, NPIdentifier name
, const NPVariant
*value
);
165 template <class RuntimeNPObject
> friend bool RuntimeNPClassRemoveProperty(NPObject
*npobj
, NPIdentifier name
);
166 template <class RuntimeNPObject
> friend bool RuntimeNPClassInvoke(NPObject
*npobj
, NPIdentifier name
,
167 const NPVariant
*args
, uint32_t argCount
,
170 RuntimeNPObject
*create(NPP instance
) const;
172 int indexOfMethod(NPIdentifier name
) const;
173 int indexOfProperty(NPIdentifier name
) const;
176 NPIdentifier
*propertyIdentifiers
;
177 NPIdentifier
*methodIdentifiers
;
181 inline void RuntimeNPObject::InstantObj( NPObject
*&obj
)
184 obj
= NPN_CreateObject(_instance
, RuntimeNPClass
<T
>::getClass());
188 static NPObject
*RuntimeNPClassAllocate(NPP instance
, NPClass
*aClass
)
190 const RuntimeNPClass
<T
> *vClass
= static_cast<RuntimeNPClass
<T
> *>(aClass
);
191 return vClass
->create(instance
);
194 static void RuntimeNPClassDeallocate(NPObject
*npobj
)
196 RuntimeNPObject
*vObj
= static_cast<RuntimeNPObject
*>(npobj
);
201 static void RuntimeNPClassInvalidate(NPObject
*npobj
)
203 RuntimeNPObject
*vObj
= static_cast<RuntimeNPObject
*>(npobj
);
204 vObj
->_instance
= NULL
;
208 static bool RuntimeNPClassHasMethod(NPObject
*npobj
, NPIdentifier name
)
210 const RuntimeNPClass
<T
> *vClass
= static_cast<RuntimeNPClass
<T
> *>(npobj
->_class
);
211 return vClass
->indexOfMethod(name
) != -1;
215 static bool RuntimeNPClassHasProperty(NPObject
*npobj
, NPIdentifier name
)
217 const RuntimeNPClass
<T
> *vClass
= static_cast<RuntimeNPClass
<T
> *>(npobj
->_class
);
218 return vClass
->indexOfProperty(name
) != -1;
222 static bool RuntimeNPClassGetProperty(NPObject
*npobj
, NPIdentifier name
, NPVariant
*result
)
224 RuntimeNPObject
*vObj
= static_cast<RuntimeNPObject
*>(npobj
);
225 if( vObj
->isValid() )
227 const RuntimeNPClass
<T
> *vClass
= static_cast<RuntimeNPClass
<T
> *>(npobj
->_class
);
228 int index
= vClass
->indexOfProperty(name
);
231 return vObj
->returnInvokeResult(vObj
->getProperty(index
, *result
));
238 static bool RuntimeNPClassSetProperty(NPObject
*npobj
, NPIdentifier name
, const NPVariant
*value
)
240 RuntimeNPObject
*vObj
= static_cast<RuntimeNPObject
*>(npobj
);
241 if( vObj
->isValid() )
243 const RuntimeNPClass
<T
> *vClass
= static_cast<RuntimeNPClass
<T
> *>(npobj
->_class
);
244 int index
= vClass
->indexOfProperty(name
);
247 return vObj
->returnInvokeResult(vObj
->setProperty(index
, *value
));
254 static bool RuntimeNPClassRemoveProperty(NPObject
*npobj
, NPIdentifier name
)
256 RuntimeNPObject
*vObj
= static_cast<RuntimeNPObject
*>(npobj
);
257 if( vObj
->isValid() )
259 const RuntimeNPClass
<T
> *vClass
= static_cast<RuntimeNPClass
<T
> *>(npobj
->_class
);
260 int index
= vClass
->indexOfProperty(name
);
263 return vObj
->returnInvokeResult(vObj
->removeProperty(index
));
270 static bool RuntimeNPClassInvoke(NPObject
*npobj
, NPIdentifier name
,
271 const NPVariant
*args
, uint32_t argCount
,
274 RuntimeNPObject
*vObj
= static_cast<RuntimeNPObject
*>(npobj
);
275 if( vObj
->isValid() )
277 const RuntimeNPClass
<T
> *vClass
= static_cast<RuntimeNPClass
<T
> *>(npobj
->_class
);
278 int index
= vClass
->indexOfMethod(name
);
281 return vObj
->returnInvokeResult(vObj
->invoke(index
, args
, argCount
, *result
));
288 static bool RuntimeNPClassInvokeDefault(NPObject
*npobj
,
289 const NPVariant
*args
,
293 RuntimeNPObject
*vObj
= static_cast<RuntimeNPObject
*>(npobj
);
294 if( vObj
->isValid() )
296 return vObj
->returnInvokeResult(vObj
->invokeDefault(args
, argCount
, *result
));
302 RuntimeNPClass
<T
>::RuntimeNPClass()
304 // retreive property identifiers from names
305 if( T::propertyCount
> 0 )
307 propertyIdentifiers
= new NPIdentifier
[T::propertyCount
];
308 if( propertyIdentifiers
)
309 NPN_GetStringIdentifiers(const_cast<const NPUTF8
**>(T::propertyNames
),
310 T::propertyCount
, propertyIdentifiers
);
313 // retreive method identifiers from names
314 if( T::methodCount
> 0 )
316 methodIdentifiers
= new NPIdentifier
[T::methodCount
];
317 if( methodIdentifiers
)
318 NPN_GetStringIdentifiers(const_cast<const NPUTF8
**>(T::methodNames
),
319 T::methodCount
, methodIdentifiers
);
322 // fill in NPClass structure
323 structVersion
= NP_CLASS_STRUCT_VERSION
;
324 allocate
= &RuntimeNPClassAllocate
<T
>;
325 deallocate
= &RuntimeNPClassDeallocate
;
326 invalidate
= &RuntimeNPClassInvalidate
;
327 hasMethod
= &RuntimeNPClassHasMethod
<T
>;
328 invoke
= &RuntimeNPClassInvoke
<T
>;
329 invokeDefault
= &RuntimeNPClassInvokeDefault
;
330 hasProperty
= &RuntimeNPClassHasProperty
<T
>;
331 getProperty
= &RuntimeNPClassGetProperty
<T
>;
332 setProperty
= &RuntimeNPClassSetProperty
<T
>;
333 removeProperty
= &RuntimeNPClassRemoveProperty
<T
>;
337 RuntimeNPClass
<T
>::~RuntimeNPClass()
339 delete[] propertyIdentifiers
;
340 delete[] methodIdentifiers
;
344 RuntimeNPObject
*RuntimeNPClass
<T
>::create(NPP instance
) const
346 return new T(instance
, this);
350 int RuntimeNPClass
<T
>::indexOfMethod(NPIdentifier name
) const
352 if( methodIdentifiers
)
354 for(int c
=0; c
< T::methodCount
; ++c
)
356 if( name
== methodIdentifiers
[c
] )
364 int RuntimeNPClass
<T
>::indexOfProperty(NPIdentifier name
) const
366 if( propertyIdentifiers
)
368 for(int c
=0; c
< T::propertyCount
; ++c
)
370 if( name
== propertyIdentifiers
[c
] )