OLD | NEW |
| (Empty) |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 /* | |
6 * Copyright (C) 2007 Apple Inc. All rights reserved. | |
7 * | |
8 * Redistribution and use in source and binary forms, with or without | |
9 * modification, are permitted provided that the following conditions | |
10 * are met: | |
11 * 1. Redistributions of source code must retain the above copyright | |
12 * notice, this list of conditions and the following disclaimer. | |
13 * 2. Redistributions in binary form must reproduce the above copyright | |
14 * notice, this list of conditions and the following disclaimer in the | |
15 * documentation and/or other materials provided with the distribution. | |
16 * | |
17 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY | |
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR | |
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
24 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | |
25 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
28 */ | |
29 | |
30 #include "content/shell/tools/plugin/test_object.h" | |
31 | |
32 #include <stdint.h> | |
33 #include <stdlib.h> | |
34 #include <string.h> | |
35 | |
36 #include "PluginObject.h" | |
37 | |
38 static bool testEnumerate(NPObject* npobj, | |
39 NPIdentifier** value, | |
40 uint32_t* count); | |
41 static bool testHasMethod(NPObject*, NPIdentifier name); | |
42 static bool testInvoke(NPObject*, | |
43 NPIdentifier name, | |
44 const NPVariant* args, | |
45 uint32_t argCount, | |
46 NPVariant* result); | |
47 static bool testHasProperty(NPObject*, NPIdentifier name); | |
48 static bool testGetProperty(NPObject*, NPIdentifier name, NPVariant*); | |
49 static NPObject* testAllocate(NPP npp, NPClass* theClass); | |
50 static void testDeallocate(NPObject* obj); | |
51 static bool testConstruct(NPObject* obj, | |
52 const NPVariant* args, | |
53 uint32_t argCount, | |
54 NPVariant* result); | |
55 | |
56 static NPClass g_test_class = { | |
57 NP_CLASS_STRUCT_VERSION, testAllocate, testDeallocate, 0, | |
58 testHasMethod, testInvoke, 0, testHasProperty, | |
59 testGetProperty, 0, 0, testEnumerate, | |
60 testConstruct}; | |
61 | |
62 | |
63 static int g_test_object_count = 0; | |
64 | |
65 typedef struct { | |
66 NPObject header; | |
67 NPObject* testObject; | |
68 } TestObject; | |
69 | |
70 static bool identifiersInitialized = false; | |
71 | |
72 #define NUM_ENUMERATABLE_TEST_IDENTIFIERS 2 | |
73 | |
74 enum { | |
75 ID_PROPERTY_FOO = 0, | |
76 ID_PROPERTY_BAR, | |
77 ID_PROPERTY_OBJECT_POINTER, | |
78 ID_PROPERTY_TEST_OBJECT, | |
79 ID_PROPERTY_REF_COUNT, | |
80 NUM_TEST_IDENTIFIERS, | |
81 }; | |
82 | |
83 static NPIdentifier testIdentifiers[NUM_TEST_IDENTIFIERS]; | |
84 static const NPUTF8* testIdentifierNames[NUM_TEST_IDENTIFIERS] = { | |
85 "foo", "bar", "objectPointer", "testObject", "refCount", }; | |
86 | |
87 #define ID_THROW_EXCEPTION_METHOD 0 | |
88 #define NUM_METHOD_IDENTIFIERS 1 | |
89 | |
90 static NPIdentifier testMethodIdentifiers[NUM_METHOD_IDENTIFIERS]; | |
91 static const NPUTF8* testMethodIdentifierNames[NUM_METHOD_IDENTIFIERS] = { | |
92 "throwException", }; | |
93 | |
94 static void initializeIdentifiers(void) { | |
95 browser->getstringidentifiers( | |
96 testIdentifierNames, NUM_TEST_IDENTIFIERS, testIdentifiers); | |
97 browser->getstringidentifiers( | |
98 testMethodIdentifierNames, NUM_METHOD_IDENTIFIERS, testMethodIdentifiers); | |
99 } | |
100 | |
101 static NPObject* testAllocate(NPP npp, NPClass* /*theClass*/) { | |
102 TestObject* newInstance = | |
103 static_cast<TestObject*>(malloc(sizeof(TestObject))); | |
104 newInstance->testObject = 0; | |
105 ++g_test_object_count; | |
106 | |
107 if (!identifiersInitialized) { | |
108 identifiersInitialized = true; | |
109 initializeIdentifiers(); | |
110 } | |
111 | |
112 return reinterpret_cast<NPObject*>(newInstance); | |
113 } | |
114 | |
115 static void testDeallocate(NPObject* obj) { | |
116 TestObject* testObject = reinterpret_cast<TestObject*>(obj); | |
117 if (testObject->testObject) | |
118 browser->releaseobject(testObject->testObject); | |
119 | |
120 --g_test_object_count; | |
121 free(obj); | |
122 } | |
123 | |
124 static bool testHasMethod(NPObject*, NPIdentifier name) { | |
125 for (unsigned i = 0; i < NUM_METHOD_IDENTIFIERS; i++) { | |
126 if (testMethodIdentifiers[i] == name) | |
127 return true; | |
128 } | |
129 return false; | |
130 } | |
131 | |
132 static bool testInvoke(NPObject* header, | |
133 NPIdentifier name, | |
134 const NPVariant* /*args*/, | |
135 uint32_t /*argCount*/, | |
136 NPVariant* /*result*/) { | |
137 if (name == testMethodIdentifiers[ID_THROW_EXCEPTION_METHOD]) { | |
138 browser->setexception(header, "test object throwException SUCCESS"); | |
139 return true; | |
140 } | |
141 return false; | |
142 } | |
143 | |
144 static bool testHasProperty(NPObject*, NPIdentifier name) { | |
145 for (unsigned i = 0; i < NUM_TEST_IDENTIFIERS; i++) { | |
146 if (testIdentifiers[i] == name) | |
147 return true; | |
148 } | |
149 | |
150 return false; | |
151 } | |
152 | |
153 static bool testGetProperty(NPObject* npobj, | |
154 NPIdentifier name, | |
155 NPVariant* result) { | |
156 if (name == testIdentifiers[ID_PROPERTY_FOO]) { | |
157 char* mem = static_cast<char*>(browser->memalloc(4)); | |
158 strcpy(mem, "foo"); | |
159 STRINGZ_TO_NPVARIANT(mem, *result); | |
160 return true; | |
161 } | |
162 if (name == testIdentifiers[ID_PROPERTY_BAR]) { | |
163 char* mem = static_cast<char*>(browser->memalloc(4)); | |
164 strcpy(mem, "bar"); | |
165 STRINGZ_TO_NPVARIANT(mem, *result); | |
166 return true; | |
167 } | |
168 if (name == testIdentifiers[ID_PROPERTY_OBJECT_POINTER]) { | |
169 int32_t objectPointer = | |
170 static_cast<int32_t>(reinterpret_cast<long long>(npobj)); | |
171 | |
172 INT32_TO_NPVARIANT(objectPointer, *result); | |
173 return true; | |
174 } | |
175 if (name == testIdentifiers[ID_PROPERTY_TEST_OBJECT]) { | |
176 TestObject* testObject = reinterpret_cast<TestObject*>(npobj); | |
177 if (!testObject->testObject) | |
178 testObject->testObject = browser->createobject(0, &g_test_class); | |
179 browser->retainobject(testObject->testObject); | |
180 OBJECT_TO_NPVARIANT(testObject->testObject, *result); | |
181 return true; | |
182 } | |
183 if (name == testIdentifiers[ID_PROPERTY_REF_COUNT]) { | |
184 INT32_TO_NPVARIANT(npobj->referenceCount, *result); | |
185 return true; | |
186 } | |
187 | |
188 return false; | |
189 } | |
190 | |
191 static bool testEnumerate(NPObject* /*npobj*/, | |
192 NPIdentifier** value, | |
193 uint32_t* count) { | |
194 *count = NUM_ENUMERATABLE_TEST_IDENTIFIERS; | |
195 | |
196 *value = (NPIdentifier*)browser->memalloc(NUM_ENUMERATABLE_TEST_IDENTIFIERS * | |
197 sizeof(NPIdentifier)); | |
198 memcpy(*value, | |
199 testIdentifiers, | |
200 sizeof(NPIdentifier) * NUM_ENUMERATABLE_TEST_IDENTIFIERS); | |
201 | |
202 return true; | |
203 } | |
204 | |
205 static bool testConstruct(NPObject* npobj, | |
206 const NPVariant* /*args*/, | |
207 uint32_t /*argCount*/, | |
208 NPVariant* result) { | |
209 browser->retainobject(npobj); | |
210 | |
211 // Just return the same object. | |
212 OBJECT_TO_NPVARIANT(npobj, *result); | |
213 return true; | |
214 } | |
215 | |
216 namespace content { | |
217 | |
218 NPClass* GetTestClass() { return &g_test_class; } | |
219 | |
220 int GetTestObjectCount() { return g_test_object_count; } | |
221 | |
222 } // namespace content | |
OLD | NEW |