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) 2010 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. AND ITS CONTRIBUTORS ``AS IS'' | |
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | |
19 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS | |
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | |
27 * THE POSSIBILITY OF SUCH DAMAGE. | |
28 */ | |
29 | |
30 #ifndef PluginTest_h | |
31 #define PluginTest_h | |
32 | |
33 #include <assert.h> | |
34 #include <bindings/npfunctions.h> | |
35 #include <stdint.h> | |
36 | |
37 #include <map> | |
38 #include <string> | |
39 | |
40 // Helper classes for implementing has_member | |
41 typedef char (&no_tag)[1]; | |
42 typedef char (&yes_tag)[2]; | |
43 | |
44 #define DEFINE_HAS_MEMBER_CHECK(member, returnType, argumentTypes) \ | |
45 template <typename T, returnType(T::*member) argumentTypes> \ | |
46 struct pmf_##member##_helper {}; \ | |
47 template <typename T> \ | |
48 no_tag has_member_##member##_helper(...); \ | |
49 template <typename T> \ | |
50 yes_tag has_member_##member##_helper(pmf_##member##_helper<T, &T::member>*); \ | |
51 template <typename T> \ | |
52 struct has_member_##member { \ | |
53 static const bool value = \ | |
54 sizeof(has_member_##member##_helper<T>(0)) == sizeof(yes_tag); \ | |
55 }; | |
56 | |
57 DEFINE_HAS_MEMBER_CHECK(hasMethod, bool, (NPIdentifier methodName)); | |
58 DEFINE_HAS_MEMBER_CHECK( | |
59 invoke, | |
60 bool, | |
61 (NPIdentifier methodName, const NPVariant*, uint32_t, NPVariant* result)); | |
62 DEFINE_HAS_MEMBER_CHECK(invokeDefault, | |
63 bool, | |
64 (const NPVariant*, uint32_t, NPVariant* result)); | |
65 DEFINE_HAS_MEMBER_CHECK(hasProperty, bool, (NPIdentifier propertyName)); | |
66 DEFINE_HAS_MEMBER_CHECK(getProperty, | |
67 bool, | |
68 (NPIdentifier propertyName, NPVariant* result)); | |
69 DEFINE_HAS_MEMBER_CHECK(removeProperty, bool, (NPIdentifier propertyName)); | |
70 | |
71 class PluginTest { | |
72 public: | |
73 static PluginTest* create(NPP, const std::string& identifier); | |
74 virtual ~PluginTest(); | |
75 | |
76 static void NP_Shutdown(); | |
77 | |
78 // NPP functions. | |
79 virtual NPError NPP_New(NPMIMEType pluginType, | |
80 uint16_t mode, | |
81 int16_t argc, | |
82 char* argn[], | |
83 char* argv[], | |
84 NPSavedData* saved); | |
85 virtual NPError NPP_Destroy(NPSavedData**); | |
86 virtual NPError NPP_SetWindow(NPWindow*); | |
87 virtual NPError NPP_NewStream(NPMIMEType, | |
88 NPStream*, | |
89 NPBool seekable, | |
90 uint16_t* stype); | |
91 virtual NPError NPP_DestroyStream(NPStream*, NPReason); | |
92 virtual int32_t NPP_WriteReady(NPStream*); | |
93 virtual int32_t NPP_Write(NPStream*, | |
94 int32_t offset, | |
95 int32_t len, | |
96 void* buffer); | |
97 virtual int16_t NPP_HandleEvent(void* event); | |
98 virtual bool NPP_URLNotify(const char* url, NPReason, void* notifyData); | |
99 virtual NPError NPP_GetValue(NPPVariable, void* value); | |
100 virtual NPError NPP_SetValue(NPNVariable, void* value); | |
101 | |
102 // NPN functions. | |
103 NPError NPN_GetValue(NPNVariable, void* value); | |
104 void NPN_InvalidateRect(NPRect* invalidRect); | |
105 bool NPN_Invoke(NPObject*, | |
106 NPIdentifier methodName, | |
107 const NPVariant* args, | |
108 uint32_t argCount, | |
109 NPVariant* result); | |
110 void* NPN_MemAlloc(uint32_t size); | |
111 | |
112 // NPRuntime NPN functions. | |
113 NPIdentifier NPN_GetStringIdentifier(const NPUTF8* name); | |
114 NPIdentifier NPN_GetIntIdentifier(int32_t intid); | |
115 bool NPN_IdentifierIsString(NPIdentifier); | |
116 NPUTF8* NPN_UTF8FromIdentifier(NPIdentifier); | |
117 int32_t NPN_IntFromIdentifier(NPIdentifier); | |
118 | |
119 NPObject* NPN_CreateObject(NPClass*); | |
120 NPObject* NPN_RetainObject(NPObject*); | |
121 void NPN_ReleaseObject(NPObject*); | |
122 bool NPN_GetProperty(NPObject*, NPIdentifier propertyName, NPVariant* value); | |
123 bool NPN_RemoveProperty(NPObject*, NPIdentifier propertyName); | |
124 void NPN_ReleaseVariantValue(NPVariant*); | |
125 | |
126 #ifdef XP_MACOSX | |
127 bool NPN_ConvertPoint(double sourceX, | |
128 double sourceY, | |
129 NPCoordinateSpace sourceSpace, | |
130 double* destX, | |
131 double* destY, | |
132 NPCoordinateSpace destSpace); | |
133 #endif | |
134 | |
135 bool executeScript(const NPString*, NPVariant* result); | |
136 void executeScript(const char*); | |
137 void log(const char* format, ...); | |
138 | |
139 void registerNPShutdownFunction(void (*)()); | |
140 | |
141 static void indicateTestFailure(); | |
142 | |
143 template <typename TestClassTy> | |
144 class Register { | |
145 public: | |
146 Register(const std::string& identifier) { | |
147 registerCreateTestFunction(identifier, Register::create); | |
148 } | |
149 | |
150 private: | |
151 static PluginTest* create(NPP npp, const std::string& identifier) { | |
152 return new TestClassTy(npp, identifier); | |
153 } | |
154 }; | |
155 | |
156 protected: | |
157 PluginTest(NPP npp, const std::string& identifier); | |
158 | |
159 // FIXME: A plugin test shouldn't need to know about it's NPP. Make this | |
160 // private. | |
161 NPP m_npp; | |
162 | |
163 const std::string& identifier() const { return m_identifier; } | |
164 | |
165 static NPNetscapeFuncs* netscapeFuncs(); | |
166 | |
167 void waitUntilDone(); | |
168 void notifyDone(); | |
169 | |
170 // NPObject helper template. | |
171 template <typename T> | |
172 struct Object : NPObject { | |
173 public: | |
174 static NPObject* create(PluginTest* pluginTest) { | |
175 Object* object = | |
176 static_cast<Object*>(pluginTest->NPN_CreateObject(npClass())); | |
177 | |
178 object->m_pluginTest = pluginTest; | |
179 return object; | |
180 } | |
181 | |
182 // These should never be called. | |
183 bool hasMethod(NPIdentifier methodName) { | |
184 assert(false); | |
185 return false; | |
186 } | |
187 | |
188 bool invoke(NPIdentifier methodName, | |
189 const NPVariant*, | |
190 uint32_t, | |
191 NPVariant* result) { | |
192 assert(false); | |
193 return false; | |
194 } | |
195 | |
196 bool invokeDefault(const NPVariant*, uint32_t, NPVariant* result) { | |
197 assert(false); | |
198 return false; | |
199 } | |
200 | |
201 bool hasProperty(NPIdentifier propertyName) { | |
202 assert(false); | |
203 return false; | |
204 } | |
205 | |
206 bool getProperty(NPIdentifier propertyName, NPVariant* result) { | |
207 assert(false); | |
208 return false; | |
209 } | |
210 | |
211 bool removeProperty(NPIdentifier propertyName) { | |
212 assert(false); | |
213 return false; | |
214 } | |
215 | |
216 // Helper functions. | |
217 bool identifierIs(NPIdentifier identifier, const char* value) { | |
218 return pluginTest()->NPN_GetStringIdentifier(value) == identifier; | |
219 } | |
220 | |
221 protected: | |
222 Object() : m_pluginTest(0) {} | |
223 | |
224 virtual ~Object() {} | |
225 | |
226 PluginTest* pluginTest() const { return m_pluginTest; } | |
227 | |
228 private: | |
229 static NPObject* NP_Allocate(NPP npp, NPClass* aClass) { return new T; } | |
230 | |
231 static void NP_Deallocate(NPObject* npObject) { | |
232 delete static_cast<T*>(npObject); | |
233 } | |
234 | |
235 static bool NP_HasMethod(NPObject* npObject, NPIdentifier methodName) { | |
236 return static_cast<T*>(npObject)->hasMethod(methodName); | |
237 } | |
238 | |
239 static bool NP_Invoke(NPObject* npObject, | |
240 NPIdentifier methodName, | |
241 const NPVariant* arguments, | |
242 uint32_t argumentCount, | |
243 NPVariant* result) { | |
244 return static_cast<T*>(npObject) | |
245 ->invoke(methodName, arguments, argumentCount, result); | |
246 } | |
247 | |
248 static bool NP_InvokeDefault(NPObject* npObject, | |
249 const NPVariant* arguments, | |
250 uint32_t argumentCount, | |
251 NPVariant* result) { | |
252 return static_cast<T*>(npObject) | |
253 ->invokeDefault(arguments, argumentCount, result); | |
254 } | |
255 | |
256 static bool NP_HasProperty(NPObject* npObject, NPIdentifier propertyName) { | |
257 return static_cast<T*>(npObject)->hasProperty(propertyName); | |
258 } | |
259 | |
260 static bool NP_GetProperty(NPObject* npObject, | |
261 NPIdentifier propertyName, | |
262 NPVariant* result) { | |
263 return static_cast<T*>(npObject)->getProperty(propertyName, result); | |
264 } | |
265 | |
266 static bool NP_RemoveProperty(NPObject* npObject, | |
267 NPIdentifier propertyName) { | |
268 return static_cast<T*>(npObject)->removeProperty(propertyName); | |
269 } | |
270 | |
271 static NPClass* npClass() { | |
272 static NPClass npClass = { | |
273 NP_CLASS_STRUCT_VERSION, NP_Allocate, NP_Deallocate, | |
274 0, // NPClass::invalidate | |
275 has_member_hasMethod<T>::value ? NP_HasMethod : 0, | |
276 has_member_invoke<T>::value ? NP_Invoke : 0, | |
277 has_member_invokeDefault<T>::value ? NP_InvokeDefault : 0, | |
278 has_member_hasProperty<T>::value ? NP_HasProperty : 0, | |
279 has_member_getProperty<T>::value ? NP_GetProperty : 0, | |
280 0, // NPClass::setProperty | |
281 has_member_removeProperty<T>::value ? NP_RemoveProperty : 0, | |
282 0, // NPClass::enumerate | |
283 0 // NPClass::construct | |
284 }; | |
285 | |
286 return &npClass; | |
287 }; | |
288 | |
289 PluginTest* m_pluginTest; | |
290 }; | |
291 | |
292 private: | |
293 typedef PluginTest* (*CreateTestFunction)(NPP, const std::string&); | |
294 | |
295 static void registerCreateTestFunction(const std::string&, | |
296 CreateTestFunction); | |
297 static std::map<std::string, CreateTestFunction>& createTestFunctions(); | |
298 | |
299 std::string m_identifier; | |
300 }; | |
301 | |
302 #endif // PluginTest_h | |
OLD | NEW |