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 $
7 * Authors: Damien Fouilleul <damien.fouilleul@laposte.net>
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.
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.
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 *****************************************************************************/
25 ** support framework for runtime script objects
28 class VlcRuntimeObject : public NPObject
31 VlcRuntimeObject(NPP instance, const NPClass *aClass) :
34 _class = const_cast<NPClass *>(aClass);
37 virtual ~VlcRuntimeObject() {};
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;
47 template<class T> class VlcRuntimeClass : public NPClass
51 virtual ~VlcRuntimeClass();
53 VlcRuntimeObject *create(NPP instance) const;
55 int indexOfMethod(NPIdentifier name) const;
56 int indexOfProperty(NPIdentifier name) const;
59 NPIdentifier *propertyIdentifiers;
60 NPIdentifier *methodIdentifiers;
64 static NPObject *vlcRuntimeClassAllocate(NPP instance, NPClass *aClass)
66 const VlcRuntimeClass<T> *vClass = static_cast<VlcRuntimeClass<T> *>(aClass);
67 return (NPObject *)vClass->create(instance);
71 static void vlcRuntimeClassDeallocate(NPObject *npobj)
73 VlcRuntimeObject *vObj = static_cast<VlcRuntimeObject *>(npobj);
78 static void vlcRuntimeClassInvalidate(NPObject *npobj)
80 VlcRuntimeObject *vObj = static_cast<VlcRuntimeObject *>(npobj);
81 vObj->_instance = NULL;
85 bool vlcRuntimeClassHasMethod(NPObject *npobj, NPIdentifier name)
87 const VlcRuntimeClass<T> *vClass = static_cast<VlcRuntimeClass<T> *>(npobj->_class);
88 return vClass->indexOfMethod(name) != -1;
92 bool vlcRuntimeClassHasProperty(NPObject *npobj, NPIdentifier name)
94 const VlcRuntimeClass<T> *vClass = static_cast<VlcRuntimeClass<T> *>(npobj->_class);
95 return vClass->indexOfProperty(name) != -1;
99 bool vlcRuntimeClassGetProperty(NPObject *npobj, NPIdentifier name, NPVariant *result)
101 const VlcRuntimeClass<T> *vClass = static_cast<VlcRuntimeClass<T> *>(npobj->_class);
102 int index = vClass->indexOfProperty(name);
105 VlcRuntimeObject *vObj = static_cast<VlcRuntimeObject *>(npobj);
106 return vObj->getProperty(index, result);
112 bool vlcRuntimeClassSetProperty(NPObject *npobj, NPIdentifier name, const NPVariant *value)
114 const VlcRuntimeClass<T> *vClass = static_cast<VlcRuntimeClass<T> *>(npobj->_class);
115 int index = vClass->indexOfProperty(name);
118 VlcRuntimeObject *vObj = static_cast<VlcRuntimeObject *>(npobj);
119 return vObj->setProperty(index, value);
125 bool vlcRuntimeClassRemoveProperty(NPObject *npobj, NPIdentifier name)
127 const VlcRuntimeClass<T> *vClass = static_cast<VlcRuntimeClass<T> *>(npobj->_class);
128 int index = vClass->indexOfProperty(name);
131 VlcRuntimeObject *vObj = static_cast<VlcRuntimeObject *>(npobj);
132 return vObj->removeProperty(index);
138 static bool vlcRuntimeClassInvoke(NPObject *npobj, NPIdentifier name,
139 const NPVariant *args, uint32_t argCount,
142 const VlcRuntimeClass<T> *vClass = static_cast<VlcRuntimeClass<T> *>(npobj->_class);
143 int index = vClass->indexOfMethod(name);
146 VlcRuntimeObject *vObj = static_cast<VlcRuntimeObject *>(npobj);
147 return vObj->invoke(index, args, argCount, result);
153 static bool vlcRuntimeClassInvokeDefault(NPObject *npobj,
154 const NPVariant *args,
158 VlcRuntimeObject *vObj = static_cast<VlcRuntimeObject *>(npobj);
159 return vObj->invokeDefault(args, argCount, result);
163 VlcRuntimeClass<T>::VlcRuntimeClass()
165 // retreive property identifiers from names
166 if( T::propertyCount > 0 )
168 propertyIdentifiers = new NPIdentifier[T::propertyCount];
169 if( propertyIdentifiers )
170 NPN_GetStringIdentifiers(const_cast<const NPUTF8**>(T::propertyNames),
171 T::propertyCount, propertyIdentifiers);
174 // retreive method identifiers from names
175 if( T::methodCount > 0 )
177 methodIdentifiers = new NPIdentifier[T::methodCount];
178 if( methodIdentifiers )
179 NPN_GetStringIdentifiers(const_cast<const NPUTF8**>(T::methodNames),
180 T::methodCount, methodIdentifiers);
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>;
198 VlcRuntimeClass<T>::~VlcRuntimeClass()
200 delete propertyIdentifiers;
201 delete methodIdentifiers;
205 VlcRuntimeObject *VlcRuntimeClass<T>::create(NPP instance) const
207 return new T(instance, this);
211 int VlcRuntimeClass<T>::indexOfMethod(NPIdentifier name) const
213 if( methodIdentifiers )
215 for(int c=0; c< T::methodCount; ++c )
217 if( name == methodIdentifiers[c] )
225 int VlcRuntimeClass<T>::indexOfProperty(NPIdentifier name) const
227 if( propertyIdentifiers )
229 for(int c=0; c< T::propertyCount; ++c )
231 if( name == propertyIdentifiers[c] )
239 ** defined runtime script objects
242 class VlcRuntimeRootObject: public VlcRuntimeObject
245 VlcRuntimeRootObject(NPP instance, const NPClass *aClass) :
246 VlcRuntimeObject(instance, aClass) {};
247 virtual ~VlcRuntimeRootObject() {};
249 static const int propertyCount;
250 static const NPUTF8 * const propertyNames[];
252 static const int methodCount;
253 static const NPUTF8 * const methodNames[];
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);