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
getTestClass(void)57 NPClass *getTestClass(void)
58 {
59 return &testClass;
60 }
61
62 static int testObjectCount = 0;
63
getTestObjectCount()64 int getTestObjectCount()
65 {
66 return testObjectCount;
67 }
68
69 typedef struct {
70 NPObject header;
71 NPObject* testObject;
72 } TestObject;
73
74 static bool identifiersInitialized = false;
75
76 #define NUM_ENUMERATABLE_TEST_IDENTIFIERS 2
77
78 enum {
79 ID_PROPERTY_FOO = 0,
80 ID_PROPERTY_BAR,
81 ID_PROPERTY_OBJECT_POINTER,
82 ID_PROPERTY_TEST_OBJECT,
83 ID_PROPERTY_REF_COUNT,
84 NUM_TEST_IDENTIFIERS,
85 };
86
87 static NPIdentifier testIdentifiers[NUM_TEST_IDENTIFIERS];
88 static const NPUTF8 *testIdentifierNames[NUM_TEST_IDENTIFIERS] = {
89 "foo",
90 "bar",
91 "objectPointer",
92 "testObject",
93 "refCount",
94 };
95
96 #define ID_THROW_EXCEPTION_METHOD 0
97 #define NUM_METHOD_IDENTIFIERS 1
98
99 static NPIdentifier testMethodIdentifiers[NUM_METHOD_IDENTIFIERS];
100 static const NPUTF8 *testMethodIdentifierNames[NUM_METHOD_IDENTIFIERS] = {
101 "throwException",
102 };
103
initializeIdentifiers(void)104 static void initializeIdentifiers(void)
105 {
106 browser->getstringidentifiers(testIdentifierNames, NUM_TEST_IDENTIFIERS, testIdentifiers);
107 browser->getstringidentifiers(testMethodIdentifierNames, NUM_METHOD_IDENTIFIERS, testMethodIdentifiers);
108 }
109
testAllocate(NPP,NPClass *)110 static NPObject* testAllocate(NPP /*npp*/, NPClass* /*theClass*/)
111 {
112 TestObject* newInstance = static_cast<TestObject*>(malloc(sizeof(TestObject)));
113 newInstance->testObject = 0;
114 ++testObjectCount;
115
116 if (!identifiersInitialized) {
117 identifiersInitialized = true;
118 initializeIdentifiers();
119 }
120
121 return reinterpret_cast<NPObject*>(newInstance);
122 }
123
testDeallocate(NPObject * obj)124 static void testDeallocate(NPObject *obj)
125 {
126 TestObject* testObject = reinterpret_cast<TestObject*>(obj);
127 if (testObject->testObject)
128 browser->releaseobject(testObject->testObject);
129
130 --testObjectCount;
131 free(obj);
132 }
133
testHasMethod(NPObject *,NPIdentifier name)134 static bool testHasMethod(NPObject*, NPIdentifier name)
135 {
136 for (unsigned i = 0; i < NUM_METHOD_IDENTIFIERS; i++) {
137 if (testMethodIdentifiers[i] == name)
138 return true;
139 }
140 return false;
141 }
142
testInvoke(NPObject * header,NPIdentifier name,const NPVariant *,uint32_t,NPVariant *)143 static bool testInvoke(NPObject* header, NPIdentifier name, const NPVariant* /*args*/, uint32_t /*argCount*/, NPVariant* /*result*/)
144 {
145 if (name == testMethodIdentifiers[ID_THROW_EXCEPTION_METHOD]) {
146 browser->setexception(header, "test object throwException SUCCESS");
147 return true;
148 }
149 return false;
150 }
151
testHasProperty(NPObject *,NPIdentifier name)152 static bool testHasProperty(NPObject*, NPIdentifier name)
153 {
154 for (unsigned i = 0; i < NUM_TEST_IDENTIFIERS; i++) {
155 if (testIdentifiers[i] == name)
156 return true;
157 }
158
159 return false;
160 }
161
testGetProperty(NPObject * npobj,NPIdentifier name,NPVariant * result)162 static bool testGetProperty(NPObject* npobj, NPIdentifier name, NPVariant* result)
163 {
164 if (name == testIdentifiers[ID_PROPERTY_FOO]) {
165 char* mem = static_cast<char*>(browser->memalloc(4));
166 strcpy(mem, "foo");
167 STRINGZ_TO_NPVARIANT(mem, *result);
168 return true;
169 }
170 if (name == testIdentifiers[ID_PROPERTY_OBJECT_POINTER]) {
171 int32_t objectPointer = static_cast<int32_t>(reinterpret_cast<long long>(npobj));
172
173 INT32_TO_NPVARIANT(objectPointer, *result);
174 return true;
175 }
176 if (name == testIdentifiers[ID_PROPERTY_TEST_OBJECT]) {
177 TestObject* testObject = reinterpret_cast<TestObject*>(npobj);
178 if (!testObject->testObject)
179 testObject->testObject = browser->createobject(0, &testClass);
180 browser->retainobject(testObject->testObject);
181 OBJECT_TO_NPVARIANT(testObject->testObject, *result);
182 return true;
183 }
184 if (name == testIdentifiers[ID_PROPERTY_REF_COUNT]) {
185 INT32_TO_NPVARIANT(npobj->referenceCount, *result);
186 return true;
187 }
188
189 return false;
190 }
191
testEnumerate(NPObject *,NPIdentifier ** value,uint32_t * count)192 static bool testEnumerate(NPObject* /*npobj*/, NPIdentifier **value, uint32_t *count)
193 {
194 *count = NUM_ENUMERATABLE_TEST_IDENTIFIERS;
195
196 *value = (NPIdentifier*)browser->memalloc(NUM_ENUMERATABLE_TEST_IDENTIFIERS * sizeof(NPIdentifier));
197 memcpy(*value, testIdentifiers, sizeof(NPIdentifier) * NUM_ENUMERATABLE_TEST_IDENTIFIERS);
198
199 return true;
200 }
201
testConstruct(NPObject * npobj,const NPVariant *,uint32_t,NPVariant * result)202 static bool testConstruct(NPObject* npobj, const NPVariant* /*args*/, uint32_t /*argCount*/, NPVariant* result)
203 {
204 browser->retainobject(npobj);
205
206 // Just return the same object.
207 OBJECT_TO_NPVARIANT(npobj, *result);
208 return true;
209 }
210