]> git.sesse.net Git - vlc/blob - projects/mozilla/control/nporuntime.h
macosx: fixed menubar appearance in fullscreen mode by partially reverting [46c93c9cc...
[vlc] / projects / 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$
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     void *operator new(size_t n)
73     {
74         /*
75         ** Assume that browser has a smarter memory allocator
76         ** than plain old malloc() and use it instead.
77         */
78         return NPN_MemAlloc(n);
79     };
80
81     void operator delete(void *p)
82     {
83         NPN_MemFree(p);
84     };
85
86     bool isValid()
87     {
88         return _instance != NULL;
89     };
90
91     RuntimeNPObject(NPP instance, const NPClass *aClass) :
92         _instance(instance)
93     {
94         _class = const_cast<NPClass *>(aClass);
95         referenceCount = 1;
96     };
97     virtual ~RuntimeNPObject() {};
98
99     enum InvokeResult
100     {
101         INVOKERESULT_NO_ERROR       = 0,    /* returns no error */
102         INVOKERESULT_GENERIC_ERROR  = 1,    /* returns error */
103         INVOKERESULT_NO_SUCH_METHOD = 2,    /* throws method does not exist */
104         INVOKERESULT_INVALID_ARGS   = 3,    /* throws invalid arguments */
105         INVOKERESULT_INVALID_VALUE  = 4,    /* throws invalid value in assignment */
106         INVOKERESULT_OUT_OF_MEMORY  = 5,    /* throws out of memory */
107     };
108
109     friend void RuntimeNPClassDeallocate(NPObject *npobj);
110     friend void RuntimeNPClassInvalidate(NPObject *npobj);
111     template <class RuntimeNPObject> friend bool RuntimeNPClassGetProperty(NPObject *npobj, NPIdentifier name, NPVariant *result);
112     template <class RuntimeNPObject> friend bool RuntimeNPClassSetProperty(NPObject *npobj, NPIdentifier name, const NPVariant *value);
113     template <class RuntimeNPObject> friend bool RuntimeNPClassRemoveProperty(NPObject *npobj, NPIdentifier name);
114     template <class RuntimeNPObject> friend bool RuntimeNPClassInvoke(NPObject *npobj, NPIdentifier name,
115                                                     const NPVariant *args, uint32_t argCount,
116                                                     NPVariant *result);
117     friend bool RuntimeNPClassInvokeDefault(NPObject *npobj,
118                                             const NPVariant *args,
119                                             uint32_t argCount,
120                                             NPVariant *result);
121
122     virtual InvokeResult getProperty(int index, NPVariant &result);
123     virtual InvokeResult setProperty(int index, const NPVariant &value);
124     virtual InvokeResult removeProperty(int index);
125     virtual InvokeResult invoke(int index, const NPVariant *args, uint32_t argCount, NPVariant &result);
126     virtual InvokeResult invokeDefault(const NPVariant *args, uint32_t argCount, NPVariant &result);
127
128     bool returnInvokeResult(InvokeResult result);
129
130     bool isPluginRunning()
131     {
132         return _instance->pdata;
133     }
134     template<class T> T *getPrivate()
135     {
136         return reinterpret_cast<T *>(_instance->pdata);
137     }
138
139     NPP _instance;
140 };
141
142 template<class T> class RuntimeNPClass : public NPClass
143 {
144 public:
145     static NPClass *getClass()
146     {
147         static NPClass *singleton = new RuntimeNPClass<T>;
148         return singleton;
149     }
150
151 protected:
152     RuntimeNPClass();
153     virtual ~RuntimeNPClass();
154
155     template <class RuntimeNPObject> friend NPObject *RuntimeNPClassAllocate(NPP instance, NPClass *aClass);
156     template <class RuntimeNPObject> friend bool RuntimeNPClassHasMethod(NPObject *npobj, NPIdentifier name);
157     template <class RuntimeNPObject> friend bool RuntimeNPClassHasProperty(NPObject *npobj, NPIdentifier name);
158     template <class RuntimeNPObject> friend bool RuntimeNPClassGetProperty(NPObject *npobj, NPIdentifier name, NPVariant *result);
159     template <class RuntimeNPObject> friend bool RuntimeNPClassSetProperty(NPObject *npobj, NPIdentifier name, const NPVariant *value);
160     template <class RuntimeNPObject> friend bool RuntimeNPClassRemoveProperty(NPObject *npobj, NPIdentifier name);
161     template <class RuntimeNPObject> friend bool RuntimeNPClassInvoke(NPObject *npobj, NPIdentifier name,
162                                                                       const NPVariant *args, uint32_t argCount,
163                                                                       NPVariant *result);
164
165     RuntimeNPObject *create(NPP instance) const;
166
167     int indexOfMethod(NPIdentifier name) const;
168     int indexOfProperty(NPIdentifier name) const;
169
170 private:
171     NPIdentifier *propertyIdentifiers;
172     NPIdentifier *methodIdentifiers;
173 };
174
175 template<class T>
176 static NPObject *RuntimeNPClassAllocate(NPP instance, NPClass *aClass)
177 {
178     const RuntimeNPClass<T> *vClass = static_cast<RuntimeNPClass<T> *>(aClass);
179     return vClass->create(instance);
180 }
181
182 static void RuntimeNPClassDeallocate(NPObject *npobj)
183 {
184     RuntimeNPObject *vObj = static_cast<RuntimeNPObject *>(npobj);
185     vObj->_class = NULL;
186     delete vObj;
187 }
188
189 static void RuntimeNPClassInvalidate(NPObject *npobj)
190 {
191     RuntimeNPObject *vObj = static_cast<RuntimeNPObject *>(npobj);
192     vObj->_instance = NULL;
193 }
194
195 template<class T>
196 static bool RuntimeNPClassHasMethod(NPObject *npobj, NPIdentifier name)
197 {
198     const RuntimeNPClass<T> *vClass = static_cast<RuntimeNPClass<T> *>(npobj->_class);
199     return vClass->indexOfMethod(name) != -1;
200 }
201
202 template<class T>
203 static bool RuntimeNPClassHasProperty(NPObject *npobj, NPIdentifier name)
204 {
205     const RuntimeNPClass<T> *vClass = static_cast<RuntimeNPClass<T> *>(npobj->_class);
206     return vClass->indexOfProperty(name) != -1;
207 }
208
209 template<class T>
210 static bool RuntimeNPClassGetProperty(NPObject *npobj, NPIdentifier name, NPVariant *result)
211 {
212     RuntimeNPObject *vObj = static_cast<RuntimeNPObject *>(npobj);
213     if( vObj->isValid() )
214     {
215         const RuntimeNPClass<T> *vClass = static_cast<RuntimeNPClass<T> *>(npobj->_class);
216         int index = vClass->indexOfProperty(name);
217         if( index != -1 )
218         {
219             return vObj->returnInvokeResult(vObj->getProperty(index, *result));
220         }
221     }
222     return false;
223 }
224
225 template<class T>
226 static bool RuntimeNPClassSetProperty(NPObject *npobj, NPIdentifier name, const NPVariant *value)
227 {
228     RuntimeNPObject *vObj = static_cast<RuntimeNPObject *>(npobj);
229     if( vObj->isValid() )
230     {
231         const RuntimeNPClass<T> *vClass = static_cast<RuntimeNPClass<T> *>(npobj->_class);
232         int index = vClass->indexOfProperty(name);
233         if( index != -1 )
234         {
235             return vObj->returnInvokeResult(vObj->setProperty(index, *value));
236         }
237     }
238     return false;
239 }
240
241 template<class T>
242 static bool RuntimeNPClassRemoveProperty(NPObject *npobj, NPIdentifier name)
243 {
244     RuntimeNPObject *vObj = static_cast<RuntimeNPObject *>(npobj);
245     if( vObj->isValid() )
246     {
247         const RuntimeNPClass<T> *vClass = static_cast<RuntimeNPClass<T> *>(npobj->_class);
248         int index = vClass->indexOfProperty(name);
249         if( index != -1 )
250         {
251             return vObj->returnInvokeResult(vObj->removeProperty(index));
252         }
253     }
254     return false;
255 }
256
257 template<class T>
258 static bool RuntimeNPClassInvoke(NPObject *npobj, NPIdentifier name,
259                                     const NPVariant *args, uint32_t argCount,
260                                     NPVariant *result)
261 {
262     RuntimeNPObject *vObj = static_cast<RuntimeNPObject *>(npobj);
263     if( vObj->isValid() )
264     {
265         const RuntimeNPClass<T> *vClass = static_cast<RuntimeNPClass<T> *>(npobj->_class);
266         int index = vClass->indexOfMethod(name);
267         if( index != -1 )
268         {
269             return vObj->returnInvokeResult(vObj->invoke(index, args, argCount, *result));
270
271         }
272     }
273     return false;
274 }
275
276 static bool RuntimeNPClassInvokeDefault(NPObject *npobj,
277                                            const NPVariant *args,
278                                            uint32_t argCount,
279                                            NPVariant *result)
280 {
281     RuntimeNPObject *vObj = static_cast<RuntimeNPObject *>(npobj);
282     if( vObj->isValid() )
283     {
284         return vObj->returnInvokeResult(vObj->invokeDefault(args, argCount, *result));
285     }
286     return false;
287 }
288
289 template<class T>
290 RuntimeNPClass<T>::RuntimeNPClass()
291 {
292     // retreive property identifiers from names
293     if( T::propertyCount > 0 )
294     {
295         propertyIdentifiers = new NPIdentifier[T::propertyCount];
296         if( propertyIdentifiers )
297             NPN_GetStringIdentifiers(const_cast<const NPUTF8**>(T::propertyNames),
298                 T::propertyCount, propertyIdentifiers);
299     }
300
301     // retreive method identifiers from names
302     if( T::methodCount > 0 )
303     {
304         methodIdentifiers = new NPIdentifier[T::methodCount];
305         if( methodIdentifiers )
306             NPN_GetStringIdentifiers(const_cast<const NPUTF8**>(T::methodNames),
307                 T::methodCount, methodIdentifiers);
308     }
309
310     // fill in NPClass structure
311     structVersion  = NP_CLASS_STRUCT_VERSION;
312     allocate       = &RuntimeNPClassAllocate<T>;
313     deallocate     = &RuntimeNPClassDeallocate;
314     invalidate     = &RuntimeNPClassInvalidate;
315     hasMethod      = &RuntimeNPClassHasMethod<T>;
316     invoke         = &RuntimeNPClassInvoke<T>;
317     invokeDefault  = &RuntimeNPClassInvokeDefault;
318     hasProperty    = &RuntimeNPClassHasProperty<T>;
319     getProperty    = &RuntimeNPClassGetProperty<T>;
320     setProperty    = &RuntimeNPClassSetProperty<T>;
321     removeProperty = &RuntimeNPClassRemoveProperty<T>;
322 }
323
324 template<class T>
325 RuntimeNPClass<T>::~RuntimeNPClass()
326 {
327     delete[] propertyIdentifiers;
328     delete[] methodIdentifiers;
329 }
330
331 template<class T>
332 RuntimeNPObject *RuntimeNPClass<T>::create(NPP instance) const
333 {
334     return new T(instance, this);
335 }
336
337 template<class T>
338 int RuntimeNPClass<T>::indexOfMethod(NPIdentifier name) const
339 {
340     if( methodIdentifiers )
341     {
342         for(int c=0; c< T::methodCount; ++c )
343         {
344             if( name == methodIdentifiers[c] )
345                 return c;
346         }
347     }
348     return -1;
349 }
350
351 template<class T>
352 int RuntimeNPClass<T>::indexOfProperty(NPIdentifier name) const
353 {
354     if( propertyIdentifiers )
355     {
356         for(int c=0; c< T::propertyCount; ++c )
357         {
358             if( name == propertyIdentifiers[c] )
359                 return c;
360         }
361     }
362     return -1;
363 }
364
365 #endif