OSDN Git Service

Merge WebKit at r71558: Initial merge by git.
[android-x86/external-webkit.git] / WebKitTools / DumpRenderTree / TestNetscapePlugIn / PluginTest.h
1 /*
2  * Copyright (C) 2010 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. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #ifndef PluginTest_h
27 #define PluginTest_h
28
29 #include <WebKit/npfunctions.h>
30 #include <assert.h>
31 #include <map>
32 #include <string>
33
34 // Helper classes for implementing has_member
35 typedef char (&no_tag)[1];
36 typedef char (&yes_tag)[2];
37
38 #define DEFINE_HAS_MEMBER_CHECK(member, returnType, argumentTypes) \
39 template<typename T, returnType (T::*member) argumentTypes> struct pmf_##member##_helper {}; \
40 template<typename T> no_tag has_member_##member##_helper(...); \
41 template<typename T> yes_tag has_member_##member##_helper(pmf_##member##_helper<T, &T::member >*); \
42 template<typename T> struct has_member_##member { \
43 static const bool value = sizeof(has_member_##member##_helper<T>(0)) == sizeof(yes_tag); \
44 };
45
46 DEFINE_HAS_MEMBER_CHECK(hasMethod, bool, (NPIdentifier methodName));
47 DEFINE_HAS_MEMBER_CHECK(invoke, bool, (NPIdentifier methodName, const NPVariant*, uint32_t, NPVariant* result));
48 DEFINE_HAS_MEMBER_CHECK(invokeDefault, bool, (const NPVariant*, uint32_t, NPVariant* result));
49 DEFINE_HAS_MEMBER_CHECK(hasProperty, bool, (NPIdentifier propertyName));
50 DEFINE_HAS_MEMBER_CHECK(getProperty, bool, (NPIdentifier propertyName, NPVariant* result));
51
52 class PluginTest {
53 public:
54     static PluginTest* create(NPP, const std::string& identifier);
55     virtual ~PluginTest();
56
57     // NPP functions.
58     virtual NPError NPP_Destroy(NPSavedData**);
59     virtual NPError NPP_DestroyStream(NPStream* stream, NPReason reason);
60     virtual NPError NPP_GetValue(NPPVariable, void* value);
61     virtual NPError NPP_SetWindow(NPP, NPWindow*);
62
63     // NPN functions.
64     void NPN_InvalidateRect(NPRect* invalidRect);
65     NPIdentifier NPN_GetStringIdentifier(const NPUTF8* name);
66     NPIdentifier NPN_GetIntIdentifier(int32_t intid);
67     NPError NPN_GetValue(NPNVariable, void* value);
68     NPObject* NPN_CreateObject(NPClass*);
69     bool NPN_RemoveProperty(NPObject*, NPIdentifier propertyName);
70     
71     void executeScript(const char*);
72
73     template<typename TestClassTy> class Register {
74     public:
75         Register(const std::string& identifier)
76         {
77             registerCreateTestFunction(identifier, Register::create);
78         }
79     
80     private:
81         static PluginTest* create(NPP npp, const std::string& identifier) 
82         {
83             return new TestClassTy(npp, identifier);
84         }
85     };
86
87 protected:
88     PluginTest(NPP npp, const std::string& identifier);
89
90     // FIXME: A plug-in test shouldn't need to know about it's NPP. Make this private.
91     NPP m_npp;
92
93     const std::string& identifier() const { return m_identifier; }
94
95     void waitUntilDone();
96     void notifyDone();
97
98     // NPObject helper template.
99     template<typename T> struct Object : NPObject {
100     public:
101         static NPObject* create(PluginTest* pluginTest)
102         {
103             Object* object = static_cast<Object*>(pluginTest->NPN_CreateObject(npClass()));
104
105             object->m_pluginTest = pluginTest;
106             return object;
107         }
108     
109         // These should never be called.
110         bool hasMethod(NPIdentifier methodName)
111         {
112             assert(false);
113             return false;
114         }
115
116         bool invoke(NPIdentifier methodName, const NPVariant*, uint32_t, NPVariant* result)
117         {
118             assert(false);
119             return false;
120         }
121         
122         bool invokeDefault(const NPVariant*, uint32_t, NPVariant* result)
123         {
124             assert(false);
125             return false;
126         }
127
128         bool hasProperty(NPIdentifier propertyName)
129         {
130             assert(false);
131             return false;
132         }
133
134         bool getProperty(NPIdentifier propertyName, NPVariant* result)
135         {
136             assert(false);
137             return false;
138         }
139
140     protected:
141         Object()
142             : m_pluginTest(0)
143         {
144         }
145         
146         virtual ~Object() 
147         { 
148         }
149
150         PluginTest* pluginTest() const { return m_pluginTest; }
151
152     private:
153         static NPObject* NP_Allocate(NPP npp, NPClass* aClass)
154         {
155             return new T;
156         }
157
158         static void NP_Deallocate(NPObject* npObject)
159         {
160             delete static_cast<T*>(npObject);
161         }
162
163         static bool NP_HasMethod(NPObject* npObject, NPIdentifier methodName)
164         {
165             return static_cast<T*>(npObject)->hasMethod(methodName);
166         }
167
168         static bool NP_Invoke(NPObject* npObject, NPIdentifier methodName, const NPVariant* arguments, uint32_t argumentCount, NPVariant* result)
169         {
170             return static_cast<T*>(npObject)->invoke(methodName, arguments, argumentCount, result);
171         }
172
173         static bool NP_InvokeDefault(NPObject* npObject, const NPVariant* arguments, uint32_t argumentCount, NPVariant* result)
174         {
175             return static_cast<T*>(npObject)->invokeDefault(arguments, argumentCount, result);
176         }
177
178         static bool NP_HasProperty(NPObject* npObject, NPIdentifier propertyName)
179         {
180             return static_cast<T*>(npObject)->hasProperty(propertyName);
181         }
182
183         static bool NP_GetProperty(NPObject* npObject, NPIdentifier propertyName, NPVariant* result)
184         {
185             return static_cast<T*>(npObject)->getProperty(propertyName, result);
186         }
187
188         static NPClass* npClass()
189         {
190             static NPClass npClass = {
191                 NP_CLASS_STRUCT_VERSION, 
192                 NP_Allocate,
193                 NP_Deallocate,
194                 0, // NPClass::invalidate
195                 has_member_hasMethod<T>::value ? NP_HasMethod : 0,
196                 has_member_invoke<T>::value ? NP_Invoke : 0,
197                 has_member_invokeDefault<T>::value ? NP_InvokeDefault : 0,
198                 has_member_hasProperty<T>::value ? NP_HasProperty : 0,
199                 has_member_getProperty<T>::value ? NP_GetProperty : 0,
200                 0, // NPClass::setProperty
201                 0, // NPClass::removeProperty
202                 0, // NPClass::enumerate
203                 0  // NPClass::construct
204             };
205             
206             return &npClass;
207         };
208
209         PluginTest* m_pluginTest;
210     };
211     
212 private:
213     typedef PluginTest* (*CreateTestFunction)(NPP, const std::string&);
214     
215     static void registerCreateTestFunction(const std::string&, CreateTestFunction);
216     static std::map<std::string, CreateTestFunction>& createTestFunctions();
217     
218     std::string m_identifier;
219 };
220
221 #endif // PluginTest_h