nporuntime.cpp 3.9 KB
Newer Older
1 2 3 4
/*****************************************************************************
 * runtime.cpp: support for NPRuntime API for Netscape Script-able plugins
 *              FYI: http://www.mozilla.org/projects/plugins/npruntime.html
 *****************************************************************************
5
 * Copyright (C) 2005-2012 VLC authors and VideoLAN
6 7
 *
 * Authors: Damien Fouilleul <Damien.Fouilleul@laposte.net>
8 9 10
 *          Jean-Baptiste Kempf <jb@videolan.org>
 *          JP Dinger <jpd@videolan.org>
 *          Nicolas Chauvet <kwizart@gmail.com>
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

Rafaël Carré's avatar
Rafaël Carré committed
27
#include "vlcplugin.h"
28 29 30 31 32 33 34

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "nporuntime.h"

Damien Fouilleul's avatar
Damien Fouilleul committed
35 36
char* RuntimeNPObject::stringValue(const NPString &s)
{
37
    NPUTF8 *val = static_cast<NPUTF8*>(malloc((s.UTF8Length+1) * sizeof(*val)));
Damien Fouilleul's avatar
Damien Fouilleul committed
38 39
    if( val )
    {
40 41
        strncpy(val, s.UTF8Characters, s.UTF8Length);
        val[s.UTF8Length] = '\0';
Damien Fouilleul's avatar
Damien Fouilleul committed
42 43 44 45 46 47 48
    }
    return val;
}

char* RuntimeNPObject::stringValue(const NPVariant &v)
{
    char *s = NULL;
49
    if( NPVARIANT_IS_STRING(v) )
Damien Fouilleul's avatar
Damien Fouilleul committed
50 51 52 53 54 55
    {
        return stringValue(NPVARIANT_TO_STRING(v));
    }
    return s;
}

56
RuntimeNPObject::InvokeResult RuntimeNPObject::getProperty(int, NPVariant &)
57 58 59 60 61
{
    /* default behaviour */
    return INVOKERESULT_GENERIC_ERROR;
}

62
RuntimeNPObject::InvokeResult RuntimeNPObject::setProperty(int, const NPVariant &)
63 64 65 66 67
{
    /* default behaviour */
    return INVOKERESULT_GENERIC_ERROR;
}

68
RuntimeNPObject::InvokeResult RuntimeNPObject::removeProperty(int)
69 70 71 72 73
{
    /* default behaviour */
    return INVOKERESULT_GENERIC_ERROR;
}

74
RuntimeNPObject::InvokeResult RuntimeNPObject::invoke(int, const NPVariant *, uint32_t, NPVariant &)
75 76 77 78 79
{
    /* default beahviour */
    return INVOKERESULT_GENERIC_ERROR;
}

80
RuntimeNPObject::InvokeResult RuntimeNPObject::invokeDefault(const NPVariant *, uint32_t, NPVariant &result)
81 82
{
    /* return void */
Damien Fouilleul's avatar
Damien Fouilleul committed
83
    VOID_TO_NPVARIANT(result);
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
    return INVOKERESULT_NO_ERROR;
}

bool RuntimeNPObject::returnInvokeResult(RuntimeNPObject::InvokeResult result)
{
    switch( result )
    {
        case INVOKERESULT_NO_ERROR:
            return true;
        case INVOKERESULT_GENERIC_ERROR:
            break;
        case INVOKERESULT_NO_SUCH_METHOD:
            NPN_SetException(this, "No such method or arguments mismatch");
            break;
        case INVOKERESULT_INVALID_ARGS:
            NPN_SetException(this, "Invalid arguments");
            break;
        case INVOKERESULT_INVALID_VALUE:
            NPN_SetException(this, "Invalid value in assignment");
            break;
        case INVOKERESULT_OUT_OF_MEMORY:
            NPN_SetException(this, "Out of memory");
            break;
    }
    return false;
}
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

RuntimeNPObject::InvokeResult
RuntimeNPObject::invokeResultString(const char *psz, NPVariant &result)
{
    if( !psz )
        NULL_TO_NPVARIANT(result);
    else
    {
        size_t len = strlen(psz);
        NPUTF8* retval = (NPUTF8*)NPN_MemAlloc(len);
        if( !retval )
            return INVOKERESULT_OUT_OF_MEMORY;
        else
        {
            memcpy(retval, psz, len);
            STRINGN_TO_NPVARIANT(retval, len, result);
        }
    }
    return INVOKERESULT_NO_ERROR;
}