Home | History | Annotate | Download | only in TestNetscapePlugIn.subproj
      1 /*
      2  * Copyright (C) 2007 Apple Inc. All rights reserved.
      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  * 1. Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  * 2. 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 APPLE INC. ``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 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 #include "TestObject.h"
     27 #include "PluginObject.h"
     28 
     29 #include <string.h>
     30 #include <stdlib.h>
     31 
     32 static bool testEnumerate(NPObject *npobj, NPIdentifier **value, uint32_t *count);
     33 static bool testHasMethod(NPObject*, NPIdentifier name);
     34 static bool testInvoke(NPObject*, NPIdentifier name, const NPVariant* args, uint32_t argCount, NPVariant* result);
     35 static bool testHasProperty(NPObject*, NPIdentifier name);
     36 static bool testGetProperty(NPObject*, NPIdentifier name, NPVariant*);
     37 static NPObject *testAllocate(NPP npp, NPClass *theClass);
     38 static void testDeallocate(NPObject *obj);
     39 static bool testConstruct(NPObject* obj, const NPVariant* args, uint32_t argCount, NPVariant* result);
     40 
     41 static NPClass testClass = {
     42     NP_CLASS_STRUCT_VERSION,
     43     testAllocate,
     44     testDeallocate,
     45     0,
     46     testHasMethod,
     47     testInvoke,
     48     0,
     49     testHasProperty,
     50     testGetProperty,
     51     0,
     52     0,
     53     testEnumerate,
     54     testConstruct
     55 };
     56 
     57 NPClass *getTestClass(void)
     58 {
     59     return &testClass;
     60 }
     61 
     62 static bool identifiersInitialized = false;
     63 
     64 #define ID_OBJECT_POINTER 2
     65 
     66 #define NUM_ENUMERATABLE_TEST_IDENTIFIERS 2
     67 #define NUM_TEST_IDENTIFIERS 3
     68 
     69 static NPIdentifier testIdentifiers[NUM_TEST_IDENTIFIERS];
     70 static const NPUTF8 *testIdentifierNames[NUM_TEST_IDENTIFIERS] = {
     71     "foo",
     72     "bar",
     73     "objectPointer",
     74 };
     75 
     76 #define ID_THROW_EXCEPTION_METHOD   0
     77 #define NUM_METHOD_IDENTIFIERS      1
     78 
     79 static NPIdentifier testMethodIdentifiers[NUM_METHOD_IDENTIFIERS];
     80 static const NPUTF8 *testMethodIdentifierNames[NUM_METHOD_IDENTIFIERS] = {
     81     "throwException",
     82 };
     83 
     84 static void initializeIdentifiers(void)
     85 {
     86     browser->getstringidentifiers(testIdentifierNames, NUM_TEST_IDENTIFIERS, testIdentifiers);
     87     browser->getstringidentifiers(testMethodIdentifierNames, NUM_METHOD_IDENTIFIERS, testMethodIdentifiers);
     88 }
     89 
     90 static NPObject *testAllocate(NPP /*npp*/, NPClass* /*theClass*/)
     91 {
     92     NPObject *newInstance = static_cast<NPObject*>(malloc(sizeof(NPObject)));
     93 
     94     if (!identifiersInitialized) {
     95         identifiersInitialized = true;
     96         initializeIdentifiers();
     97     }
     98 
     99     return newInstance;
    100 }
    101 
    102 static void testDeallocate(NPObject *obj)
    103 {
    104     free(obj);
    105 }
    106 
    107 static bool testHasMethod(NPObject*, NPIdentifier name)
    108 {
    109     for (unsigned i = 0; i < NUM_METHOD_IDENTIFIERS; i++) {
    110         if (testMethodIdentifiers[i] == name)
    111             return true;
    112     }
    113     return false;
    114 }
    115 
    116 static bool testInvoke(NPObject* header, NPIdentifier name, const NPVariant* /*args*/, uint32_t /*argCount*/, NPVariant* /*result*/)
    117 {
    118     if (name == testMethodIdentifiers[ID_THROW_EXCEPTION_METHOD]) {
    119         browser->setexception(header, "test object throwException SUCCESS");
    120         return true;
    121      }
    122      return false;
    123 }
    124 
    125 static bool testHasProperty(NPObject*, NPIdentifier name)
    126 {
    127     for (unsigned i = 0; i < NUM_TEST_IDENTIFIERS; i++) {
    128         if (testIdentifiers[i] == name)
    129             return true;
    130     }
    131 
    132     return false;
    133 }
    134 
    135 static bool testGetProperty(NPObject* npobj, NPIdentifier name, NPVariant* result)
    136 {
    137     if (name == testIdentifiers[ID_OBJECT_POINTER]) {
    138         int32_t objectPointer = static_cast<int32_t>(reinterpret_cast<long long>(npobj));
    139 
    140         INT32_TO_NPVARIANT(objectPointer, *result);
    141         return true;
    142     }
    143 
    144     return false;
    145 }
    146 
    147 
    148 static bool testEnumerate(NPObject* /*npobj*/, NPIdentifier **value, uint32_t *count)
    149 {
    150     *count = NUM_ENUMERATABLE_TEST_IDENTIFIERS;
    151 
    152     *value = (NPIdentifier*)browser->memalloc(NUM_ENUMERATABLE_TEST_IDENTIFIERS * sizeof(NPIdentifier));
    153     memcpy(*value, testIdentifiers, sizeof(NPIdentifier) * NUM_ENUMERATABLE_TEST_IDENTIFIERS);
    154 
    155     return true;
    156 }
    157 
    158 static bool testConstruct(NPObject* npobj, const NPVariant* /*args*/, uint32_t /*argCount*/, NPVariant* result)
    159 {
    160     browser->retainobject(npobj);
    161 
    162     // Just return the same object.
    163     OBJECT_TO_NPVARIANT(npobj, *result);
    164     return true;
    165 }
    166 
    167 
    168