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