Home | History | Annotate | Download | only in jni
      1 /*
      2  * Copyright 2009, The Android Open Source Project
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  *  * Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  *  * Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
     14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
     17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 /*
     27  IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc. ("Apple") in
     28  consideration of your agreement to the following terms, and your use, installation,
     29  modification or redistribution of this Apple software constitutes acceptance of these
     30  terms.  If you do not agree with these terms, please do not use, install, modify or
     31  redistribute this Apple software.
     32 
     33  In consideration of your agreement to abide by the following terms, and subject to these
     34  terms, Apple grants you a personal, non-exclusive license, under Apples copyrights in
     35  this original Apple software (the "Apple Software"), to use, reproduce, modify and
     36  redistribute the Apple Software, with or without modifications, in source and/or binary
     37  forms; provided that if you redistribute the Apple Software in its entirety and without
     38  modifications, you must retain this notice and the following text and disclaimers in all
     39  such redistributions of the Apple Software.  Neither the name, trademarks, service marks
     40  or logos of Apple Computer, Inc. may be used to endorse or promote products derived from
     41  the Apple Software without specific prior written permission from Apple. Except as expressly
     42  stated in this notice, no other rights or licenses, express or implied, are granted by Apple
     43  herein, including but not limited to any patent rights that may be infringed by your
     44  derivative works or by other works in which the Apple Software may be incorporated.
     45 
     46  The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO WARRANTIES,
     47  EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF NON-INFRINGEMENT,
     48  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS
     49  USE AND OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
     50 
     51  IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR CONSEQUENTIAL
     52  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
     53           OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE,
     54  REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND
     55  WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR
     56  OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     57  */
     58 
     59 #include <stdlib.h>
     60 #include "main.h"
     61 #include "PluginObject.h"
     62 
     63 static void pluginInvalidate(NPObject *obj);
     64 static bool pluginHasProperty(NPObject *obj, NPIdentifier name);
     65 static bool pluginHasMethod(NPObject *obj, NPIdentifier name);
     66 static bool pluginGetProperty(NPObject *obj, NPIdentifier name, NPVariant *variant);
     67 static bool pluginSetProperty(NPObject *obj, NPIdentifier name, const NPVariant *variant);
     68 static bool pluginInvoke(NPObject *obj, NPIdentifier name, const NPVariant *args, uint32_t argCount, NPVariant *result);
     69 static bool pluginInvokeDefault(NPObject *obj, const NPVariant *args, uint32_t argCount, NPVariant *result);
     70 static NPObject *pluginAllocate(NPP npp, NPClass *theClass);
     71 static void pluginDeallocate(NPObject *obj);
     72 static bool pluginRemoveProperty(NPObject *npobj, NPIdentifier name);
     73 static bool pluginEnumerate(NPObject *npobj, NPIdentifier **value, uint32_t *count);
     74 
     75 
     76 
     77 static NPClass pluginClass = {
     78     NP_CLASS_STRUCT_VERSION,
     79     pluginAllocate,
     80     pluginDeallocate,
     81     pluginInvalidate,
     82     pluginHasMethod,
     83     pluginInvoke,
     84     pluginInvokeDefault,
     85     pluginHasProperty,
     86     pluginGetProperty,
     87     pluginSetProperty,
     88     pluginRemoveProperty,
     89     pluginEnumerate
     90 };
     91 
     92 NPClass *getPluginClass(void)
     93 {
     94     return &pluginClass;
     95 }
     96 
     97 static bool identifiersInitialized = false;
     98 
     99 #define ID_TESTFILE_PROPERTY            0
    100 #define NUM_PROPERTY_IDENTIFIERS        1
    101 
    102 static NPIdentifier pluginPropertyIdentifiers[NUM_PROPERTY_IDENTIFIERS];
    103 static const NPUTF8 *pluginPropertyIdentifierNames[NUM_PROPERTY_IDENTIFIERS] = {
    104     "testfile"
    105 };
    106 
    107 #define ID_GETTESTFILE_METHOD                   0
    108 #define NUM_METHOD_IDENTIFIERS                  1
    109 
    110 static NPIdentifier pluginMethodIdentifiers[NUM_METHOD_IDENTIFIERS];
    111 static const NPUTF8 *pluginMethodIdentifierNames[NUM_METHOD_IDENTIFIERS] = {
    112     "getTestFile"
    113 };
    114 
    115 static void initializeIdentifiers(void)
    116 {
    117     browser->getstringidentifiers(pluginPropertyIdentifierNames, NUM_PROPERTY_IDENTIFIERS, pluginPropertyIdentifiers);
    118     browser->getstringidentifiers(pluginMethodIdentifierNames, NUM_METHOD_IDENTIFIERS, pluginMethodIdentifiers);
    119 }
    120 
    121 static bool pluginHasProperty(NPObject *obj, NPIdentifier name)
    122 {
    123     int i;
    124     for (i = 0; i < NUM_PROPERTY_IDENTIFIERS; i++)
    125         if (name == pluginPropertyIdentifiers[i])
    126             return true;
    127     return false;
    128 }
    129 
    130 static bool pluginHasMethod(NPObject *obj, NPIdentifier name)
    131 {
    132     int i;
    133     for (i = 0; i < NUM_METHOD_IDENTIFIERS; i++)
    134         if (name == pluginMethodIdentifiers[i])
    135             return true;
    136     return false;
    137 }
    138 
    139 static bool pluginGetProperty(NPObject *obj, NPIdentifier name, NPVariant *variant)
    140 {
    141     PluginObject *plugin = (PluginObject *)obj;
    142     if (name == pluginPropertyIdentifiers[ID_TESTFILE_PROPERTY]) {
    143         BOOLEAN_TO_NPVARIANT(true, *variant);
    144         return true;
    145     }
    146     return false;
    147 }
    148 
    149 static bool pluginSetProperty(NPObject *obj, NPIdentifier name, const NPVariant *variant)
    150 {
    151     return false;
    152 }
    153 
    154 static bool pluginInvoke(NPObject *obj, NPIdentifier name, const NPVariant *args, uint32_t argCount, NPVariant *result)
    155 {
    156     PluginObject *plugin = (PluginObject *)obj;
    157     if (name == pluginMethodIdentifiers[ID_GETTESTFILE_METHOD]) {
    158         return true;
    159     }
    160     return false;
    161 }
    162 
    163 static bool pluginInvokeDefault(NPObject *obj, const NPVariant *args, uint32_t argCount, NPVariant *result)
    164 {
    165     return false;
    166 }
    167 
    168 static void pluginInvalidate(NPObject *obj)
    169 {
    170     // Release any remaining references to JavaScript objects.
    171 }
    172 
    173 static NPObject *pluginAllocate(NPP npp, NPClass *theClass)
    174 {
    175     PluginObject *newInstance = (PluginObject*) malloc(sizeof(PluginObject));
    176     newInstance->header._class = theClass;
    177     newInstance->header.referenceCount = 1;
    178 
    179     if (!identifiersInitialized) {
    180         identifiersInitialized = true;
    181         initializeIdentifiers();
    182     }
    183 
    184     newInstance->npp = npp;
    185 
    186     return &newInstance->header;
    187 }
    188 
    189 static void pluginDeallocate(NPObject *obj)
    190 {
    191     free(obj);
    192 }
    193 
    194 static bool pluginRemoveProperty(NPObject *npobj, NPIdentifier name)
    195 {
    196     return false;
    197 }
    198 
    199 static bool pluginEnumerate(NPObject *npobj, NPIdentifier **value, uint32_t *count)
    200 {
    201     return false;
    202 }
    203