]> git.sesse.net Git - vlc/blob - projects/mozilla/control/nporuntime.cpp
mozilla: rework events
[vlc] / projects / mozilla / control / nporuntime.cpp
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) 2005 the VideoLAN team
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 #include "config.h"
25
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdlib.h>
29
30 #include "nporuntime.h"
31 #include "vlcplugin.h"
32
33 char* RuntimeNPObject::stringValue(const NPString &s)
34 {
35     NPUTF8 *val = static_cast<NPUTF8*>(malloc((s.UTF8Length+1) * sizeof(*val)));
36     if( val )
37     {
38         strncpy(val, s.UTF8Characters, s.UTF8Length);
39         val[s.UTF8Length] = '\0';
40     }
41     return val;
42 }
43
44 char* RuntimeNPObject::stringValue(const NPVariant &v)
45 {
46     char *s = NULL;
47     if( NPVARIANT_IS_STRING(v) )
48     {
49         return stringValue(NPVARIANT_TO_STRING(v));
50     }
51     return s;
52 }
53
54 RuntimeNPObject::InvokeResult RuntimeNPObject::getProperty(int index, NPVariant &result)
55 {
56     /* default behaviour */
57     return INVOKERESULT_GENERIC_ERROR;
58 }
59
60 RuntimeNPObject::InvokeResult RuntimeNPObject::setProperty(int index, const NPVariant &value)
61 {
62     /* default behaviour */
63     return INVOKERESULT_GENERIC_ERROR;
64 }
65
66 RuntimeNPObject::InvokeResult RuntimeNPObject::removeProperty(int index)
67 {
68     /* default behaviour */
69     return INVOKERESULT_GENERIC_ERROR;
70 }
71
72 RuntimeNPObject::InvokeResult RuntimeNPObject::invoke(int index, const NPVariant *args, uint32_t argCount, NPVariant &result)
73 {
74     /* default beahviour */
75     return INVOKERESULT_GENERIC_ERROR;
76 }
77
78 RuntimeNPObject::InvokeResult RuntimeNPObject::invokeDefault(const NPVariant *args, uint32_t argCount, NPVariant &result)
79 {
80     /* return void */
81     VOID_TO_NPVARIANT(result);
82     return INVOKERESULT_NO_ERROR;
83 }
84
85 bool RuntimeNPObject::returnInvokeResult(RuntimeNPObject::InvokeResult result)
86 {
87     switch( result )
88     {
89         case INVOKERESULT_NO_ERROR:
90             return true;
91         case INVOKERESULT_GENERIC_ERROR:
92             break;
93         case INVOKERESULT_NO_SUCH_METHOD:
94             NPN_SetException(this, "No such method or arguments mismatch");
95             break;
96         case INVOKERESULT_INVALID_ARGS:
97             NPN_SetException(this, "Invalid arguments");
98             break;
99         case INVOKERESULT_INVALID_VALUE:
100             NPN_SetException(this, "Invalid value in assignment");
101             break;
102         case INVOKERESULT_OUT_OF_MEMORY:
103             NPN_SetException(this, "Out of memory");
104             break;
105     }
106     return false;
107 }
108
109 RuntimeNPObject::InvokeResult
110 RuntimeNPObject::invokeResultString(const char *psz, NPVariant &result)
111 {
112     if( !psz )
113         NULL_TO_NPVARIANT(result);
114     else
115     {
116         size_t len = strlen(psz);
117         NPUTF8* retval = (NPUTF8*)NPN_MemAlloc(len);
118         if( !retval )
119             return INVOKERESULT_OUT_OF_MEMORY;
120         else
121         {
122             memcpy(retval, psz, len);
123             STRINGN_TO_NPVARIANT(retval, len, result);
124         }
125     }
126     return INVOKERESULT_NO_ERROR;
127 }