npapi.mm 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * Copyright (C) 2005 Apple Computer, 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. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
  14. * its contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
  18. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  21. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #if ENABLE(NETSCAPE_PLUGIN_API)
  29. #import <WebKit/npapi.h>
  30. #import "WebNetscapePluginView.h"
  31. #import "WebKitLogging.h"
  32. #import <WebCore/PluginMainThreadScheduler.h>
  33. using namespace WebCore;
  34. WebNetscapePluginView *pluginViewForInstance(NPP instance);
  35. // general plug-in to browser functions
  36. void* NPN_MemAlloc(uint32_t size)
  37. {
  38. return malloc(size);
  39. }
  40. void NPN_MemFree(void* ptr)
  41. {
  42. free(ptr);
  43. }
  44. uint32_t NPN_MemFlush(uint32_t size)
  45. {
  46. LOG(Plugins, "NPN_MemFlush");
  47. return size;
  48. }
  49. void NPN_ReloadPlugins(NPBool reloadPages)
  50. {
  51. LOG(Plugins, "NPN_ReloadPlugins");
  52. }
  53. NPError NPN_RequestRead(NPStream* stream, NPByteRange* rangeList)
  54. {
  55. LOG(Plugins, "NPN_RequestRead");
  56. return NPERR_GENERIC_ERROR;
  57. }
  58. // instance-specific functions
  59. // The plugin view is always the ndata of the instance. Sometimes, plug-ins will call an instance-specific function
  60. // with a NULL instance. To workaround this, call the last plug-in view that made a call to a plug-in.
  61. // Currently, the current plug-in view is only set before NPP_New in [WebNetscapePluginView start].
  62. // This specifically works around Flash and Shockwave. When we call NPP_New, they call NPN_UserAgent with a NULL instance.
  63. WebNetscapePluginView *pluginViewForInstance(NPP instance)
  64. {
  65. if (instance && instance->ndata)
  66. return (WebNetscapePluginView *)instance->ndata;
  67. else
  68. return [WebNetscapePluginView currentPluginView];
  69. }
  70. NPError NPN_GetURLNotify(NPP instance, const char* URL, const char* target, void* notifyData)
  71. {
  72. return [pluginViewForInstance(instance) getURLNotify:URL target:target notifyData:notifyData];
  73. }
  74. NPError NPN_GetURL(NPP instance, const char* URL, const char* target)
  75. {
  76. return [pluginViewForInstance(instance) getURL:URL target:target];
  77. }
  78. NPError NPN_PostURLNotify(NPP instance, const char* URL, const char* target, uint32_t len, const char* buf, NPBool file, void* notifyData)
  79. {
  80. return [pluginViewForInstance(instance) postURLNotify:URL target:target len:len buf:buf file:file notifyData:notifyData];
  81. }
  82. NPError NPN_PostURL(NPP instance, const char* URL, const char* target, uint32_t len, const char* buf, NPBool file)
  83. {
  84. return [pluginViewForInstance(instance) postURL:URL target:target len:len buf:buf file:file];
  85. }
  86. NPError NPN_NewStream(NPP instance, NPMIMEType type, const char* target, NPStream** stream)
  87. {
  88. return [pluginViewForInstance(instance) newStream:type target:target stream:stream];
  89. }
  90. int32_t NPN_Write(NPP instance, NPStream* stream, int32_t len, void* buffer)
  91. {
  92. return [pluginViewForInstance(instance) write:stream len:len buffer:buffer];
  93. }
  94. NPError NPN_DestroyStream(NPP instance, NPStream* stream, NPReason reason)
  95. {
  96. return [pluginViewForInstance(instance) destroyStream:stream reason:reason];
  97. }
  98. const char* NPN_UserAgent(NPP instance)
  99. {
  100. return [pluginViewForInstance(instance) userAgent];
  101. }
  102. void NPN_Status(NPP instance, const char* message)
  103. {
  104. [pluginViewForInstance(instance) status:message];
  105. }
  106. void NPN_InvalidateRect(NPP instance, NPRect *invalidRect)
  107. {
  108. [pluginViewForInstance(instance) invalidateRect:invalidRect];
  109. }
  110. void NPN_InvalidateRegion(NPP instance, NPRegion invalidRegion)
  111. {
  112. [pluginViewForInstance(instance) invalidateRegion:invalidRegion];
  113. }
  114. void NPN_ForceRedraw(NPP instance)
  115. {
  116. [pluginViewForInstance(instance) forceRedraw];
  117. }
  118. NPError NPN_GetValue(NPP instance, NPNVariable variable, void *value)
  119. {
  120. return [pluginViewForInstance(instance) getVariable:variable value:value];
  121. }
  122. NPError NPN_SetValue(NPP instance, NPPVariable variable, void *value)
  123. {
  124. return [pluginViewForInstance(instance) setVariable:variable value:value];
  125. }
  126. // Unsupported functions
  127. void* NPN_GetJavaEnv(void)
  128. {
  129. LOG(Plugins, "NPN_GetJavaEnv");
  130. return NULL;
  131. }
  132. void* NPN_GetJavaPeer(NPP instance)
  133. {
  134. LOG(Plugins, "NPN_GetJavaPeer");
  135. return NULL;
  136. }
  137. void NPN_PushPopupsEnabledState(NPP instance, NPBool enabled)
  138. {
  139. }
  140. void NPN_PopPopupsEnabledState(NPP instance)
  141. {
  142. }
  143. void NPN_PluginThreadAsyncCall(NPP instance, void (*func) (void *), void *userData)
  144. {
  145. PluginMainThreadScheduler::scheduler().scheduleCall(instance, func, userData);
  146. }
  147. uint32_t NPN_ScheduleTimer(NPP instance, uint32_t interval, NPBool repeat, void (*timerFunc)(NPP npp, uint32_t timerID))
  148. {
  149. return [pluginViewForInstance(instance) scheduleTimerWithInterval:interval repeat:repeat timerFunc:timerFunc];
  150. }
  151. void NPN_UnscheduleTimer(NPP instance, uint32_t timerID)
  152. {
  153. [pluginViewForInstance(instance) unscheduleTimer:timerID];
  154. }
  155. NPError NPN_PopUpContextMenu(NPP instance, NPMenu *menu)
  156. {
  157. return [pluginViewForInstance(instance) popUpContextMenu:menu];
  158. }
  159. NPError NPN_GetValueForURL(NPP instance, NPNURLVariable variable, const char* url, char** value, uint32_t* len)
  160. {
  161. return [pluginViewForInstance(instance) getVariable:variable forURL:url value:value length:len];
  162. }
  163. NPError NPN_SetValueForURL(NPP instance, NPNURLVariable variable, const char* url, const char* value, uint32_t len)
  164. {
  165. return [pluginViewForInstance(instance) setVariable:variable forURL:url value:value length:len];
  166. }
  167. NPError NPN_GetAuthenticationInfo(NPP instance, const char* protocol, const char* host, int32_t port, const char* scheme, const char *realm, char** username, uint32_t* ulen, char** password, uint32_t* plen)
  168. {
  169. return [pluginViewForInstance(instance) getAuthenticationInfoWithProtocol:protocol
  170. host:host
  171. port:port
  172. scheme:scheme
  173. realm:realm
  174. username:username usernameLength:ulen
  175. password:password passwordLength:plen];
  176. }
  177. NPBool NPN_ConvertPoint(NPP instance, double sourceX, double sourceY, NPCoordinateSpace sourceSpace, double *destX, double *destY, NPCoordinateSpace destSpace)
  178. {
  179. return [pluginViewForInstance(instance) convertFromX:sourceX andY:sourceY space:sourceSpace toX:destX andY:destY space:destSpace];
  180. }
  181. uint32_t WKN_CheckIfAllowedToLoadURL(NPP instance, const char* url, const char* frame, void (*callbackFunc)(NPP npp, uint32_t, NPBool, void*), void* context)
  182. {
  183. return [pluginViewForInstance(instance) checkIfAllowedToLoadURL:url frame:frame callbackFunc:callbackFunc context:context];
  184. }
  185. void WKN_CancelCheckIfAllowedToLoadURL(NPP instance, uint32_t checkID)
  186. {
  187. [pluginViewForInstance(instance) cancelCheckIfAllowedToLoadURL:checkID];
  188. }
  189. char* WKN_ResolveURL(NPP instance, const char* url, const char* target)
  190. {
  191. return [pluginViewForInstance(instance) resolveURL:url forTarget:target];
  192. }
  193. #endif