PluginTest.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. #ifndef PluginTest_h
  26. #define PluginTest_h
  27. #include <WebKit/npfunctions.h>
  28. #include <assert.h>
  29. #include <map>
  30. #include <string>
  31. // Helper classes for implementing has_member
  32. typedef char (&no_tag)[1];
  33. typedef char (&yes_tag)[2];
  34. #define DEFINE_HAS_MEMBER_CHECK(member, returnType, argumentTypes) \
  35. template<typename T, returnType (T::*member) argumentTypes> struct pmf_##member##_helper {}; \
  36. template<typename T> no_tag has_member_##member##_helper(...); \
  37. template<typename T> yes_tag has_member_##member##_helper(pmf_##member##_helper<T, &T::member >*); \
  38. template<typename T> struct has_member_##member { \
  39. static const bool value = sizeof(has_member_##member##_helper<T>(0)) == sizeof(yes_tag); \
  40. };
  41. DEFINE_HAS_MEMBER_CHECK(hasMethod, bool, (NPIdentifier methodName));
  42. DEFINE_HAS_MEMBER_CHECK(invoke, bool, (NPIdentifier methodName, const NPVariant*, uint32_t, NPVariant* result));
  43. DEFINE_HAS_MEMBER_CHECK(invokeDefault, bool, (const NPVariant*, uint32_t, NPVariant* result));
  44. DEFINE_HAS_MEMBER_CHECK(hasProperty, bool, (NPIdentifier propertyName));
  45. DEFINE_HAS_MEMBER_CHECK(getProperty, bool, (NPIdentifier propertyName, NPVariant* result));
  46. DEFINE_HAS_MEMBER_CHECK(removeProperty, bool, (NPIdentifier propertyName));
  47. class PluginTest {
  48. public:
  49. static PluginTest* create(NPP, const std::string& identifier);
  50. virtual ~PluginTest();
  51. static void NP_Shutdown();
  52. // NPP functions.
  53. virtual NPError NPP_New(NPMIMEType pluginType, uint16_t mode, int16_t argc, char *argn[], char *argv[], NPSavedData *saved);
  54. virtual NPError NPP_Destroy(NPSavedData**);
  55. virtual NPError NPP_SetWindow(NPWindow*);
  56. virtual NPError NPP_NewStream(NPMIMEType, NPStream*, NPBool seekable, uint16_t* stype);
  57. virtual NPError NPP_DestroyStream(NPStream*, NPReason);
  58. virtual int32_t NPP_WriteReady(NPStream*);
  59. virtual int32_t NPP_Write(NPStream*, int32_t offset, int32_t len, void* buffer);
  60. virtual int16_t NPP_HandleEvent(void* event);
  61. virtual bool NPP_URLNotify(const char* url, NPReason, void* notifyData);
  62. virtual NPError NPP_GetValue(NPPVariable, void* value);
  63. virtual NPError NPP_SetValue(NPNVariable, void *value);
  64. // NPN functions.
  65. NPError NPN_GetURL(const char* url, const char* target);
  66. NPError NPN_GetURLNotify(const char* url, const char* target, void* notifyData);
  67. NPError NPN_GetValue(NPNVariable, void* value);
  68. void NPN_InvalidateRect(NPRect* invalidRect);
  69. bool NPN_Invoke(NPObject *, NPIdentifier methodName, const NPVariant *args, uint32_t argCount, NPVariant *result);
  70. void* NPN_MemAlloc(uint32_t size);
  71. // NPRuntime NPN functions.
  72. NPIdentifier NPN_GetStringIdentifier(const NPUTF8* name);
  73. NPIdentifier NPN_GetIntIdentifier(int32_t intid);
  74. bool NPN_IdentifierIsString(NPIdentifier);
  75. NPUTF8* NPN_UTF8FromIdentifier(NPIdentifier);
  76. int32_t NPN_IntFromIdentifier(NPIdentifier);
  77. NPObject* NPN_CreateObject(NPClass*);
  78. NPObject* NPN_RetainObject(NPObject*);
  79. void NPN_ReleaseObject(NPObject*);
  80. bool NPN_RemoveProperty(NPObject*, NPIdentifier propertyName);
  81. void NPN_ReleaseVariantValue(NPVariant*);
  82. #ifdef XP_MACOSX
  83. bool NPN_ConvertPoint(double sourceX, double sourceY, NPCoordinateSpace sourceSpace, double *destX, double *destY, NPCoordinateSpace destSpace);
  84. #endif
  85. bool executeScript(const NPString*, NPVariant* result);
  86. void executeScript(const char*);
  87. void log(const char* format, ...);
  88. void registerNPShutdownFunction(void (*)());
  89. static void indicateTestFailure();
  90. template<typename TestClassTy> class Register {
  91. public:
  92. Register(const std::string& identifier)
  93. {
  94. registerCreateTestFunction(identifier, Register::create);
  95. }
  96. private:
  97. static PluginTest* create(NPP npp, const std::string& identifier)
  98. {
  99. return new TestClassTy(npp, identifier);
  100. }
  101. };
  102. protected:
  103. PluginTest(NPP npp, const std::string& identifier);
  104. // FIXME: A plug-in test shouldn't need to know about it's NPP. Make this private.
  105. NPP m_npp;
  106. const std::string& identifier() const { return m_identifier; }
  107. static NPNetscapeFuncs* netscapeFuncs();
  108. void waitUntilDone();
  109. void notifyDone();
  110. // NPObject helper template.
  111. template<typename T> struct Object : NPObject {
  112. public:
  113. static NPObject* create(PluginTest* pluginTest)
  114. {
  115. Object* object = static_cast<Object*>(pluginTest->NPN_CreateObject(npClass()));
  116. object->m_pluginTest = pluginTest;
  117. return object;
  118. }
  119. // These should never be called.
  120. bool hasMethod(NPIdentifier methodName)
  121. {
  122. assert(false);
  123. return false;
  124. }
  125. bool invoke(NPIdentifier methodName, const NPVariant*, uint32_t, NPVariant* result)
  126. {
  127. assert(false);
  128. return false;
  129. }
  130. bool invokeDefault(const NPVariant*, uint32_t, NPVariant* result)
  131. {
  132. assert(false);
  133. return false;
  134. }
  135. bool hasProperty(NPIdentifier propertyName)
  136. {
  137. assert(false);
  138. return false;
  139. }
  140. bool getProperty(NPIdentifier propertyName, NPVariant* result)
  141. {
  142. assert(false);
  143. return false;
  144. }
  145. bool removeProperty(NPIdentifier propertyName)
  146. {
  147. assert(false);
  148. return false;
  149. }
  150. // Helper functions.
  151. bool identifierIs(NPIdentifier identifier, const char* value)
  152. {
  153. return pluginTest()->NPN_GetStringIdentifier(value) == identifier;
  154. }
  155. protected:
  156. Object()
  157. : m_pluginTest(0)
  158. {
  159. }
  160. virtual ~Object()
  161. {
  162. }
  163. PluginTest* pluginTest() const { return m_pluginTest; }
  164. private:
  165. static NPObject* NP_Allocate(NPP npp, NPClass* aClass)
  166. {
  167. return new T;
  168. }
  169. static void NP_Deallocate(NPObject* npObject)
  170. {
  171. delete static_cast<T*>(npObject);
  172. }
  173. static bool NP_HasMethod(NPObject* npObject, NPIdentifier methodName)
  174. {
  175. return static_cast<T*>(npObject)->hasMethod(methodName);
  176. }
  177. static bool NP_Invoke(NPObject* npObject, NPIdentifier methodName, const NPVariant* arguments, uint32_t argumentCount, NPVariant* result)
  178. {
  179. return static_cast<T*>(npObject)->invoke(methodName, arguments, argumentCount, result);
  180. }
  181. static bool NP_InvokeDefault(NPObject* npObject, const NPVariant* arguments, uint32_t argumentCount, NPVariant* result)
  182. {
  183. return static_cast<T*>(npObject)->invokeDefault(arguments, argumentCount, result);
  184. }
  185. static bool NP_HasProperty(NPObject* npObject, NPIdentifier propertyName)
  186. {
  187. return static_cast<T*>(npObject)->hasProperty(propertyName);
  188. }
  189. static bool NP_GetProperty(NPObject* npObject, NPIdentifier propertyName, NPVariant* result)
  190. {
  191. return static_cast<T*>(npObject)->getProperty(propertyName, result);
  192. }
  193. static bool NP_RemoveProperty(NPObject* npObject, NPIdentifier propertyName)
  194. {
  195. return static_cast<T*>(npObject)->removeProperty(propertyName);
  196. }
  197. static NPClass* npClass()
  198. {
  199. static NPClass npClass = {
  200. NP_CLASS_STRUCT_VERSION,
  201. NP_Allocate,
  202. NP_Deallocate,
  203. 0, // NPClass::invalidate
  204. has_member_hasMethod<T>::value ? NP_HasMethod : 0,
  205. has_member_invoke<T>::value ? NP_Invoke : 0,
  206. has_member_invokeDefault<T>::value ? NP_InvokeDefault : 0,
  207. has_member_hasProperty<T>::value ? NP_HasProperty : 0,
  208. has_member_getProperty<T>::value ? NP_GetProperty : 0,
  209. 0, // NPClass::setProperty
  210. has_member_removeProperty<T>::value ? NP_RemoveProperty : 0,
  211. 0, // NPClass::enumerate
  212. 0 // NPClass::construct
  213. };
  214. return &npClass;
  215. };
  216. PluginTest* m_pluginTest;
  217. };
  218. private:
  219. typedef PluginTest* (*CreateTestFunction)(NPP, const std::string&);
  220. static void registerCreateTestFunction(const std::string&, CreateTestFunction);
  221. static std::map<std::string, CreateTestFunction>& createTestFunctions();
  222. std::string m_identifier;
  223. };
  224. #endif // PluginTest_h