OLD | NEW |
| (Empty) |
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 <stdlib.h> | |
30 #include <string.h> | |
31 | |
32 static bool testEnumerate(NPObject*, NPIdentifier **value, uint32_t *count); | |
33 static bool testHasMethod(NPObject*, NPIdentifier name); | |
34 static bool testInvoke(NPObject* header, NPIdentifier name, const NPVariant* arg
s, uint32_t argCount, NPVariant* result); | |
35 static bool testInvokeDefault(NPObject*, const NPVariant *args, uint32_t argCoun
t, NPVariant *result); | |
36 static bool testHasProperty(NPObject*, NPIdentifier name); | |
37 static bool testGetProperty(NPObject*, NPIdentifier name, NPVariant *variant); | |
38 static NPObject *testAllocate(NPP npp, NPClass *theClass); | |
39 static void testDeallocate(NPObject*); | |
40 static bool testConstruct(NPObject*, const NPVariant* args, uint32_t argCount, N
PVariant* result); | |
41 | |
42 | |
43 static NPClass testClass = { | |
44 NP_CLASS_STRUCT_VERSION, | |
45 testAllocate, | |
46 testDeallocate, | |
47 0, | |
48 testHasMethod, | |
49 testInvoke, | |
50 testInvokeDefault, | |
51 testHasProperty, | |
52 testGetProperty, | |
53 0, | |
54 0, | |
55 testEnumerate, | |
56 testConstruct | |
57 }; | |
58 | |
59 NPClass *getTestClass(void) | |
60 { | |
61 return &testClass; | |
62 } | |
63 | |
64 int testObjectCount = 0; | |
65 | |
66 int getTestObjectCount(void) { | |
67 return testObjectCount; | |
68 } | |
69 | |
70 static bool identifiersInitialized = false; | |
71 | |
72 #define NUM_ENUMERABLE_TEST_IDENTIFIERS 4 | |
73 #define NUM_TEST_IDENTIFIERS 5 | |
74 | |
75 #define ID_PROPERTY_FOO 0 | |
76 #define ID_PROPERTY_BAR 1 | |
77 #define ID_PROPERTY_TEST_OBJECT 2 | |
78 #define ID_PROPERTY_REF_COUNT 3 | |
79 #define ID_PROPERTY_OBJECT_POINTER 4 | |
80 | |
81 static NPIdentifier testIdentifiers[NUM_TEST_IDENTIFIERS]; | |
82 static const NPUTF8 *testIdentifierNames[NUM_TEST_IDENTIFIERS] = { | |
83 "foo", | |
84 "bar", | |
85 "testObject", | |
86 "refCount", | |
87 "objectPointer", | |
88 }; | |
89 | |
90 #define ID_THROW_EXCEPTION_METHOD 0 | |
91 #define ID_PAGE_TEST_OBJECT_METHOD 1 | |
92 #define NUM_METHOD_IDENTIFIERS 2 | |
93 | |
94 static NPIdentifier testMethodIdentifiers[NUM_METHOD_IDENTIFIERS]; | |
95 static const NPUTF8 *testMethodIdentifierNames[NUM_METHOD_IDENTIFIERS] = { | |
96 "throwException", | |
97 "pageTestObject", | |
98 }; | |
99 | |
100 static void initializeIdentifiers(void) | |
101 { | |
102 browser->getstringidentifiers(testIdentifierNames, NUM_TEST_IDENTIFIERS, tes
tIdentifiers); | |
103 browser->getstringidentifiers(testMethodIdentifierNames, NUM_METHOD_IDENTIFI
ERS, testMethodIdentifiers); | |
104 } | |
105 | |
106 static NPObject *testAllocate(NPP npp, NPClass *theClass) | |
107 { | |
108 TestObject *newInstance = | |
109 static_cast<TestObject*>(malloc(sizeof(TestObject))); | |
110 newInstance->npp = npp; | |
111 newInstance->testObject = NULL; | |
112 newInstance->testPageObject = NULL; | |
113 ++testObjectCount; | |
114 | |
115 if (!identifiersInitialized) { | |
116 identifiersInitialized = true; | |
117 initializeIdentifiers(); | |
118 } | |
119 | |
120 return reinterpret_cast<NPObject*>(newInstance); | |
121 } | |
122 | |
123 static void testDeallocate(NPObject *obj) | |
124 { | |
125 TestObject *testObject = reinterpret_cast<TestObject*>(obj); | |
126 if (testObject->testObject) | |
127 browser->releaseobject(testObject->testObject); | |
128 if (testObject->testPageObject) | |
129 browser->releaseobject(testObject->testPageObject); | |
130 --testObjectCount; | |
131 free(obj); | |
132 } | |
133 | |
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 | |
143 static bool testInvoke(NPObject* header, NPIdentifier name, const NPVariant* /*a
rgs*/, 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 } else if (name == testMethodIdentifiers[ID_PAGE_TEST_OBJECT_METHOD]) { | |
149 TestObject* testObject = reinterpret_cast<TestObject*>(header); | |
150 if (testObject->testPageObject == NULL) { | |
151 NPObject *windowScriptObject; | |
152 browser->getvalue(testObject->npp, NPNVWindowNPObject, &windowScriptOb
ject); | |
153 | |
154 NPIdentifier pageMethod = browser->getstringidentifier("dummyMethod"); | |
155 | |
156 NPVariant functionPointer; | |
157 browser->invoke(testObject->npp, windowScriptObject, pageMethod, | |
158 NULL, 0, &functionPointer); | |
159 | |
160 if (NPVARIANT_IS_OBJECT(functionPointer)) | |
161 testObject->testPageObject = NPVARIANT_TO_OBJECT(functionPointer); | |
162 | |
163 return true; | |
164 } | |
165 } | |
166 return false; | |
167 } | |
168 | |
169 static bool testInvokeDefault(NPObject *obj, const NPVariant *args, | |
170 uint32_t argCount, NPVariant *result) | |
171 { | |
172 INT32_TO_NPVARIANT(2, *result); | |
173 return true; | |
174 } | |
175 | |
176 static bool testHasProperty(NPObject*, NPIdentifier name) | |
177 { | |
178 for (unsigned i = 0; i < NUM_TEST_IDENTIFIERS; i++) { | |
179 if (testIdentifiers[i] == name) | |
180 return true; | |
181 } | |
182 | |
183 return false; | |
184 } | |
185 | |
186 static bool testGetProperty(NPObject *obj, NPIdentifier name, | |
187 NPVariant *variant) | |
188 { | |
189 if (name == testIdentifiers[ID_PROPERTY_FOO]) { | |
190 char* mem = static_cast<char*>(browser->memalloc(4)); | |
191 strcpy(mem, "foo"); | |
192 STRINGZ_TO_NPVARIANT(mem, *variant); | |
193 return true; | |
194 } else if (name == testIdentifiers[ID_PROPERTY_BAR]) { | |
195 BOOLEAN_TO_NPVARIANT(true, *variant); | |
196 return true; | |
197 } else if (name == testIdentifiers[ID_PROPERTY_TEST_OBJECT]) { | |
198 TestObject* testObject = reinterpret_cast<TestObject*>(obj); | |
199 if (testObject->testObject == NULL) | |
200 testObject->testObject = browser->createobject(NULL, &testClass); | |
201 browser->retainobject(testObject->testObject); | |
202 OBJECT_TO_NPVARIANT(testObject->testObject, *variant); | |
203 return true; | |
204 } else if (name == testIdentifiers[ID_PROPERTY_REF_COUNT]) { | |
205 INT32_TO_NPVARIANT(obj->referenceCount, *variant); | |
206 return true; | |
207 } else if (name == testIdentifiers[ID_PROPERTY_OBJECT_POINTER]) { | |
208 int32_t objectPointer = static_cast<int32_t>(reinterpret_cast<long long>
(obj)); | |
209 INT32_TO_NPVARIANT(objectPointer, *variant); | |
210 return true; | |
211 } | |
212 return false; | |
213 } | |
214 | |
215 static bool testEnumerate(NPObject *npobj, NPIdentifier **value, uint32_t *count
) | |
216 { | |
217 *count = NUM_ENUMERABLE_TEST_IDENTIFIERS; | |
218 | |
219 *value = (NPIdentifier*)browser->memalloc(NUM_ENUMERABLE_TEST_IDENTIFIERS *
sizeof(NPIdentifier)); | |
220 memcpy(*value, testIdentifiers, sizeof(NPIdentifier) * NUM_ENUMERABLE_TEST_I
DENTIFIERS); | |
221 | |
222 return true; | |
223 } | |
224 | |
225 static bool testConstruct(NPObject* npobj, const NPVariant* args, uint32_t argCo
unt, NPVariant* result) | |
226 { | |
227 browser->retainobject(npobj); | |
228 | |
229 // Just return the same object. | |
230 OBJECT_TO_NPVARIANT(npobj, *result); | |
231 return true; | |
232 } | |
233 | |
234 | |
OLD | NEW |