]> git.sesse.net Git - vlc/blob - mozilla/control/nporuntime.h
do not show started-from-file in the prefs
[vlc] / mozilla / control / nporuntime.h
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 $
7  *
8  * Authors: Damien Fouilleul <damien.fouilleul@laposte.net>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 #ifndef __NPORUNTIME_H__
26 #define __NPORUNTIME_H__
27
28 /*
29 ** support framework for runtime script objects
30 */
31
32 #include <npapi.h>
33 #include <npruntime.h>
34
35 static void RuntimeNPClassDeallocate(NPObject *npobj);
36 static void RuntimeNPClassInvalidate(NPObject *npobj);
37 static bool RuntimeNPClassInvokeDefault(NPObject *npobj,
38                                         const NPVariant *args,
39                                         uint32_t argCount,
40                                         NPVariant *result);
41
42 class RuntimeNPObject : public NPObject
43 {
44 public:
45
46     /*
47     ** utility functions
48     */
49
50     static bool isNumberValue(const NPVariant &v)
51     {
52         return NPVARIANT_IS_INT32(v)
53             || NPVARIANT_IS_DOUBLE(v);
54     };
55
56     static int numberValue(const NPVariant &v)
57     {
58         switch( v.type ) {
59             case NPVariantType_Int32:
60                 return NPVARIANT_TO_INT32(v);
61             case NPVariantType_Double:
62                 return(int)NPVARIANT_TO_DOUBLE(v);
63             default:
64                 return 0;
65         }
66     };
67
68     static char* stringValue(const NPString &v);
69     static char* stringValue(const NPVariant &v);
70
71 protected:
72
73     RuntimeNPObject(NPP instance, const NPClass *aClass) :
74         _instance(instance)
75     {
76         _class = const_cast<NPClass *>(aClass);
77         referenceCount = 1;
78     };
79     virtual ~RuntimeNPObject() {};
80
81     enum InvokeResult
82     {
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 */
89     };
90
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,
98                                                     NPVariant *result);
99     friend bool RuntimeNPClassInvokeDefault(NPObject *npobj,
100                                             const NPVariant *args,
101                                             uint32_t argCount,
102                                             NPVariant *result);
103
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);
109
110     bool returnInvokeResult(InvokeResult result);
111
112     NPP _instance;
113 };
114
115 template<class T> class RuntimeNPClass : public NPClass
116 {
117 public:
118     static NPClass *getClass()
119     {
120         static NPClass *singleton = new RuntimeNPClass<T>;
121         return singleton;
122     }
123
124 protected:
125     RuntimeNPClass();
126     virtual ~RuntimeNPClass();
127
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,
136                                                                       NPVariant *result);
137
138     RuntimeNPObject *create(NPP instance) const;
139
140     int indexOfMethod(NPIdentifier name) const;
141     int indexOfProperty(NPIdentifier name) const;
142
143 private:
144     NPIdentifier *propertyIdentifiers;
145     NPIdentifier *methodIdentifiers;
146 };
147
148 template<class T>
149 static NPObject *RuntimeNPClassAllocate(NPP instance, NPClass *aClass)
150 {
151     const RuntimeNPClass<T> *vClass = static_cast<RuntimeNPClass<T> *>(aClass);
152     return (NPObject *)vClass->create(instance);
153 }
154
155 static void RuntimeNPClassDeallocate(NPObject *npobj)
156 {
157     RuntimeNPObject *vObj = static_cast<RuntimeNPObject *>(npobj);
158     delete vObj;
159 }
160
161 static void RuntimeNPClassInvalidate(NPObject *npobj)
162 {
163     RuntimeNPObject *vObj = static_cast<RuntimeNPObject *>(npobj);
164     vObj->_instance = NULL;
165 }
166
167 template<class T>
168 static bool RuntimeNPClassHasMethod(NPObject *npobj, NPIdentifier name)
169 {
170     const RuntimeNPClass<T> *vClass = static_cast<RuntimeNPClass<T> *>(npobj->_class);
171     return vClass->indexOfMethod(name) != -1;
172 }
173
174 template<class T>
175 static bool RuntimeNPClassHasProperty(NPObject *npobj, NPIdentifier name)
176 {
177     const RuntimeNPClass<T> *vClass = static_cast<RuntimeNPClass<T> *>(npobj->_class);
178     return vClass->indexOfProperty(name) != -1;
179 }
180
181 template<class T>
182 static bool RuntimeNPClassGetProperty(NPObject *npobj, NPIdentifier name, NPVariant *result)
183 {
184     const RuntimeNPClass<T> *vClass = static_cast<RuntimeNPClass<T> *>(npobj->_class);
185     int index = vClass->indexOfProperty(name);
186     if( index != -1 )
187     {
188         RuntimeNPObject *vObj = static_cast<RuntimeNPObject *>(npobj);
189         return vObj->returnInvokeResult(vObj->getProperty(index, *result));
190     }
191     return false;
192 }
193
194 template<class T>
195 static bool RuntimeNPClassSetProperty(NPObject *npobj, NPIdentifier name, const NPVariant *value)
196 {
197     const RuntimeNPClass<T> *vClass = static_cast<RuntimeNPClass<T> *>(npobj->_class);
198     int index = vClass->indexOfProperty(name);
199     if( index != -1 )
200     {
201         RuntimeNPObject *vObj = static_cast<RuntimeNPObject *>(npobj);
202         return vObj->returnInvokeResult(vObj->setProperty(index, *value));
203     }
204     return false;
205 }
206
207 template<class T>
208 static bool RuntimeNPClassRemoveProperty(NPObject *npobj, NPIdentifier name)
209 {
210     const RuntimeNPClass<T> *vClass = static_cast<RuntimeNPClass<T> *>(npobj->_class);
211     int index = vClass->indexOfProperty(name);
212     if( index != -1 )
213     {
214         RuntimeNPObject *vObj = static_cast<RuntimeNPObject *>(npobj);
215         return vObj->returnInvokeResult(vObj->removeProperty(index));
216     }
217     return false;
218 }
219
220 template<class T>
221 static bool RuntimeNPClassInvoke(NPObject *npobj, NPIdentifier name,
222                                     const NPVariant *args, uint32_t argCount,
223                                     NPVariant *result)
224 {
225     const RuntimeNPClass<T> *vClass = static_cast<RuntimeNPClass<T> *>(npobj->_class);
226     int index = vClass->indexOfMethod(name);
227     if( index != -1 )
228     {
229         RuntimeNPObject *vObj = static_cast<RuntimeNPObject *>(npobj);
230         return vObj->returnInvokeResult(vObj->invoke(index, args, argCount, *result));
231
232     }
233     return false;
234 }
235
236 static bool RuntimeNPClassInvokeDefault(NPObject *npobj,
237                                            const NPVariant *args,
238                                            uint32_t argCount,
239                                            NPVariant *result)
240 {
241     RuntimeNPObject *vObj = static_cast<RuntimeNPObject *>(npobj);
242     return vObj->returnInvokeResult(vObj->invokeDefault(args, argCount, *result));
243 }
244
245 template<class T>
246 RuntimeNPClass<T>::RuntimeNPClass()
247 {
248     // retreive property identifiers from names
249     if( T::propertyCount > 0 )
250     {
251         propertyIdentifiers = new NPIdentifier[T::propertyCount];
252         if( propertyIdentifiers )
253             NPN_GetStringIdentifiers(const_cast<const NPUTF8**>(T::propertyNames),
254                 T::propertyCount, propertyIdentifiers);
255     }
256
257     // retreive method identifiers from names
258     if( T::methodCount > 0 )
259     {
260         methodIdentifiers = new NPIdentifier[T::methodCount];
261         if( methodIdentifiers )
262             NPN_GetStringIdentifiers(const_cast<const NPUTF8**>(T::methodNames),
263                 T::methodCount, methodIdentifiers);
264     }
265
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>;
278 }
279
280 template<class T>
281 RuntimeNPClass<T>::~RuntimeNPClass()
282 {
283     delete propertyIdentifiers;
284     delete methodIdentifiers;
285 }
286
287 template<class T>
288 RuntimeNPObject *RuntimeNPClass<T>::create(NPP instance) const
289 {
290     return new T(instance, this);
291 }
292
293 template<class T>
294 int RuntimeNPClass<T>::indexOfMethod(NPIdentifier name) const
295 {
296     if( methodIdentifiers )
297     {
298         for(int c=0; c< T::methodCount; ++c )
299         {
300             if( name == methodIdentifiers[c] )
301                 return c;
302         }
303     }
304     return -1;
305 }
306
307 template<class T>
308 int RuntimeNPClass<T>::indexOfProperty(NPIdentifier name) const
309 {
310     if( propertyIdentifiers )
311     {
312         for(int c=0; c< T::propertyCount; ++c )
313         {
314             if( name == propertyIdentifiers[c] )
315                 return c;
316         }
317     }
318     return -1;
319 }
320
321 #endif