]> git.sesse.net Git - vlc/blob - mozilla/vlcruntime.h
654d8a950a22db33af370a964fb92b2a9ad10320
[vlc] / mozilla / vlcruntime.h
1 /*****************************************************************************
2  * vlcruntime.h: a VLC plugin for Mozilla
3  *****************************************************************************
4  * Copyright (C) 2002-2005 the VideoLAN team
5  * $Id: vlcruntime.h 14466 2006-02-22 23:34:54Z dionoea $
6  *
7  * Authors: Damien Fouilleul <damien.fouilleul@laposte.net>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*
25 ** support framework for runtime script objects
26 */
27
28 class VlcRuntimeObject : public NPObject
29 {
30 public:
31     VlcRuntimeObject(NPP instance, const NPClass *aClass) :
32         _instance(instance)
33     {
34         _class = const_cast<NPClass *>(aClass);
35         referenceCount = 1;
36     };
37     virtual ~VlcRuntimeObject() {};
38
39     virtual bool getProperty(int index, NPVariant *result) = 0;
40     virtual bool setProperty(int index, const NPVariant *value) = 0;
41     virtual bool removeProperty(int index) = 0;
42     virtual bool invoke(int index, const NPVariant *args, uint32_t argCount, NPVariant *result) = 0;
43     virtual bool invokeDefault(const NPVariant *args, uint32_t argCount, NPVariant *result) = 0;
44     NPP _instance;
45 };
46
47 template<class T> class VlcRuntimeClass : public NPClass
48 {
49 public:
50     VlcRuntimeClass();
51     virtual ~VlcRuntimeClass();
52
53     VlcRuntimeObject *create(NPP instance) const;
54
55     int indexOfMethod(NPIdentifier name) const;
56     int indexOfProperty(NPIdentifier name) const;
57
58 private:
59     NPIdentifier *propertyIdentifiers;
60     NPIdentifier *methodIdentifiers;
61 };
62
63 template<class T>
64 static NPObject *vlcRuntimeClassAllocate(NPP instance, NPClass *aClass)
65 {
66     const VlcRuntimeClass<T> *vClass = static_cast<VlcRuntimeClass<T> *>(aClass);
67     return (NPObject *)vClass->create(instance);
68 }
69
70 template<class T>
71 static void vlcRuntimeClassDeallocate(NPObject *npobj)
72 {
73     VlcRuntimeObject *vObj = static_cast<VlcRuntimeObject *>(npobj);
74     delete vObj;
75 }
76
77 template<class T>
78 static void vlcRuntimeClassInvalidate(NPObject *npobj)
79 {
80     VlcRuntimeObject *vObj = static_cast<VlcRuntimeObject *>(npobj);
81     vObj->_instance = NULL;
82 }
83
84 template<class T>
85 bool vlcRuntimeClassHasMethod(NPObject *npobj, NPIdentifier name)
86 {
87     const VlcRuntimeClass<T> *vClass = static_cast<VlcRuntimeClass<T> *>(npobj->_class);
88     return vClass->indexOfMethod(name) != -1;
89 }
90
91 template<class T>
92 bool vlcRuntimeClassHasProperty(NPObject *npobj, NPIdentifier name)
93 {
94     const VlcRuntimeClass<T> *vClass = static_cast<VlcRuntimeClass<T> *>(npobj->_class);
95     return vClass->indexOfProperty(name) != -1;
96 }
97
98 template<class T>
99 bool vlcRuntimeClassGetProperty(NPObject *npobj, NPIdentifier name, NPVariant *result)
100 {
101     const VlcRuntimeClass<T> *vClass = static_cast<VlcRuntimeClass<T> *>(npobj->_class);
102     int index = vClass->indexOfProperty(name);
103     if( index != -1 )
104     {
105         VlcRuntimeObject *vObj = static_cast<VlcRuntimeObject *>(npobj);
106         return vObj->getProperty(index, result);
107     }
108     return false;
109 }
110
111 template<class T>
112 bool vlcRuntimeClassSetProperty(NPObject *npobj, NPIdentifier name, const NPVariant *value)
113 {
114     const VlcRuntimeClass<T> *vClass = static_cast<VlcRuntimeClass<T> *>(npobj->_class);
115     int index = vClass->indexOfProperty(name);
116     if( index != -1 )
117     {
118         VlcRuntimeObject *vObj = static_cast<VlcRuntimeObject *>(npobj);
119         return vObj->setProperty(index, value);
120     }
121     return false;
122 }
123
124 template<class T>
125 bool vlcRuntimeClassRemoveProperty(NPObject *npobj, NPIdentifier name)
126 {
127     const VlcRuntimeClass<T> *vClass = static_cast<VlcRuntimeClass<T> *>(npobj->_class);
128     int index = vClass->indexOfProperty(name);
129     if( index != -1 )
130     {
131         VlcRuntimeObject *vObj = static_cast<VlcRuntimeObject *>(npobj);
132         return vObj->removeProperty(index);
133     }
134     return false;
135 }
136
137 template<class T>
138 static bool vlcRuntimeClassInvoke(NPObject *npobj, NPIdentifier name,
139                                     const NPVariant *args, uint32_t argCount,
140                                     NPVariant *result)
141 {
142     const VlcRuntimeClass<T> *vClass = static_cast<VlcRuntimeClass<T> *>(npobj->_class);
143     int index = vClass->indexOfMethod(name);
144     if( index != -1 )
145     {
146         VlcRuntimeObject *vObj = static_cast<VlcRuntimeObject *>(npobj);
147         return vObj->invoke(index, args, argCount, result);
148     }
149     return false;
150 }
151
152 template<class T>
153 static bool vlcRuntimeClassInvokeDefault(NPObject *npobj,
154                                            const NPVariant *args,
155                                            uint32_t argCount,
156                                            NPVariant *result)
157 {
158     VlcRuntimeObject *vObj = static_cast<VlcRuntimeObject *>(npobj);
159     return vObj->invokeDefault(args, argCount, result);
160 }
161
162 template<class T>
163 VlcRuntimeClass<T>::VlcRuntimeClass()
164 {
165     // retreive property identifiers from names
166     if( T::propertyCount > 0 )
167     {
168         propertyIdentifiers = new NPIdentifier[T::propertyCount];
169         if( propertyIdentifiers )
170             NPN_GetStringIdentifiers(const_cast<const NPUTF8**>(T::propertyNames),
171                     T::propertyCount, propertyIdentifiers);
172     }
173
174     // retreive method identifiers from names
175     if( T::methodCount > 0 )
176     {
177         methodIdentifiers = new NPIdentifier[T::methodCount];
178         if( methodIdentifiers )
179             NPN_GetStringIdentifiers(const_cast<const NPUTF8**>(T::methodNames),
180                     T::methodCount, methodIdentifiers);
181     }
182
183     // fill in NPClass structure
184     structVersion  = NP_CLASS_STRUCT_VERSION;
185     allocate       = vlcRuntimeClassAllocate<T>;
186     deallocate     = vlcRuntimeClassDeallocate<T>;
187     invalidate     = vlcRuntimeClassInvalidate<T>;
188     hasMethod      = vlcRuntimeClassHasMethod<T>;
189     invoke         = vlcRuntimeClassInvoke<T>;
190     invokeDefault  = vlcRuntimeClassInvokeDefault<T>;
191     hasProperty    = vlcRuntimeClassHasProperty<T>;
192     getProperty    = vlcRuntimeClassGetProperty<T>;
193     setProperty    = vlcRuntimeClassSetProperty<T>;
194     removeProperty = vlcRuntimeClassRemoveProperty<T>;
195 }
196
197 template<class T>
198 VlcRuntimeClass<T>::~VlcRuntimeClass()
199 {
200     delete propertyIdentifiers;
201     delete methodIdentifiers;
202 }
203
204 template<class T>
205 VlcRuntimeObject *VlcRuntimeClass<T>::create(NPP instance) const
206 {
207     return new T(instance, this);
208 }
209
210 template<class T>
211 int VlcRuntimeClass<T>::indexOfMethod(NPIdentifier name) const
212 {
213     if( methodIdentifiers )
214     {
215         for(int c=0; c< T::methodCount; ++c )
216         {
217             if( name == methodIdentifiers[c] )
218                 return c;
219         }
220     }
221     return -1;
222 }
223
224 template<class T>
225 int VlcRuntimeClass<T>::indexOfProperty(NPIdentifier name) const
226 {
227     if( propertyIdentifiers )
228     {
229         for(int c=0; c< T::propertyCount; ++c )
230         {
231             if( name == propertyIdentifiers[c] )
232                 return c;
233         }
234     }
235     return -1;
236 }
237
238 /*
239 ** defined runtime script objects
240 */
241
242 class VlcRuntimeRootObject: public VlcRuntimeObject
243 {
244 public:
245     VlcRuntimeRootObject(NPP instance, const NPClass *aClass) :
246         VlcRuntimeObject(instance, aClass) {};
247     virtual ~VlcRuntimeRootObject() {};
248
249     static const int propertyCount;
250     static const NPUTF8 * const propertyNames[];
251
252     static const int methodCount;
253     static const NPUTF8 * const methodNames[];
254
255     virtual bool getProperty(int index, NPVariant *result);
256     virtual bool setProperty(int index, const NPVariant *value);
257     virtual bool removeProperty(int index);
258     virtual bool invoke(int index, const NPVariant *args, uint32_t argCount, NPVariant *result);
259     virtual bool invokeDefault(const NPVariant *args, uint32_t argCount, NPVariant *result);
260 };
261