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