npapi.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Copyright (C) 2006, 2007 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 COMPUTER, INC. ``AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
  17. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  21. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "config.h"
  26. #if ENABLE(NETSCAPE_PLUGIN_API)
  27. #include "Page.h"
  28. #include "PluginMainThreadScheduler.h"
  29. #include "PluginView.h"
  30. #include "npruntime_internal.h"
  31. using namespace WebCore;
  32. // The plugin view is always the ndata of the instance,. Sometimes, plug-ins will call an instance-specific function
  33. // with a NULL instance. To workaround this, call the last plug-in view that made a call to a plug-in.
  34. // Currently, the current plug-in view is only set before NPP_New in PluginView::start.
  35. // This specifically works around Flash and Shockwave. When we call NPP_New, they call NPN_Useragent with a NULL instance.
  36. static PluginView* pluginViewForInstance(NPP instance)
  37. {
  38. if (instance && instance->ndata)
  39. return static_cast<PluginView*>(instance->ndata);
  40. return PluginView::currentPluginView();
  41. }
  42. void* NPN_MemAlloc(uint32_t size)
  43. {
  44. return malloc(size);
  45. }
  46. void NPN_MemFree(void* ptr)
  47. {
  48. free(ptr);
  49. }
  50. uint32_t NPN_MemFlush(uint32_t)
  51. {
  52. // Do nothing
  53. return 0;
  54. }
  55. void NPN_ReloadPlugins(NPBool reloadPages)
  56. {
  57. Page::refreshPlugins(reloadPages);
  58. }
  59. NPError NPN_RequestRead(NPStream*, NPByteRange*)
  60. {
  61. return NPERR_STREAM_NOT_SEEKABLE;
  62. }
  63. NPError NPN_GetURLNotify(NPP instance, const char* url, const char* target, void* notifyData)
  64. {
  65. return pluginViewForInstance(instance)->getURLNotify(url, target, notifyData);
  66. }
  67. NPError NPN_GetURL(NPP instance, const char* url, const char* target)
  68. {
  69. return pluginViewForInstance(instance)->getURL(url, target);
  70. }
  71. NPError NPN_PostURLNotify(NPP instance, const char* url, const char* target, uint32_t len, const char* buf, NPBool file, void* notifyData)
  72. {
  73. return pluginViewForInstance(instance)->postURLNotify(url, target, len, buf, file, notifyData);
  74. }
  75. NPError NPN_PostURL(NPP instance, const char* url, const char* target, uint32_t len, const char* buf, NPBool file)
  76. {
  77. return pluginViewForInstance(instance)->postURL(url, target, len, buf, file);
  78. }
  79. NPError NPN_NewStream(NPP instance, NPMIMEType type, const char* target, NPStream** stream)
  80. {
  81. return pluginViewForInstance(instance)->newStream(type, target, stream);
  82. }
  83. int32_t NPN_Write(NPP instance, NPStream* stream, int32_t len, void* buffer)
  84. {
  85. return pluginViewForInstance(instance)->write(stream, len, buffer);
  86. }
  87. NPError NPN_DestroyStream(NPP instance, NPStream* stream, NPReason reason)
  88. {
  89. return pluginViewForInstance(instance)->destroyStream(stream, reason);
  90. }
  91. const char* NPN_UserAgent(NPP instance)
  92. {
  93. PluginView* view = pluginViewForInstance(instance);
  94. if (!view)
  95. return PluginView::userAgentStatic();
  96. return view->userAgent();
  97. }
  98. void NPN_Status(NPP instance, const char* message)
  99. {
  100. pluginViewForInstance(instance)->status(message);
  101. }
  102. void NPN_InvalidateRect(NPP instance, NPRect* invalidRect)
  103. {
  104. PluginView* view = pluginViewForInstance(instance);
  105. #if defined(XP_UNIX)
  106. // NSPluginWrapper, a plugin wrapper binary that allows running 32-bit plugins
  107. // on 64-bit architectures typically used in X11, will sometimes give us a null NPP here.
  108. if (!view)
  109. return;
  110. #endif
  111. view->invalidateRect(invalidRect);
  112. }
  113. void NPN_InvalidateRegion(NPP instance, NPRegion invalidRegion)
  114. {
  115. pluginViewForInstance(instance)->invalidateRegion(invalidRegion);
  116. }
  117. void NPN_ForceRedraw(NPP instance)
  118. {
  119. pluginViewForInstance(instance)->forceRedraw();
  120. }
  121. NPError NPN_GetValue(NPP instance, NPNVariable variable, void* value)
  122. {
  123. PluginView* view = pluginViewForInstance(instance);
  124. if (!view)
  125. return PluginView::getValueStatic(variable, value);
  126. return pluginViewForInstance(instance)->getValue(variable, value);
  127. }
  128. NPError NPN_SetValue(NPP instance, NPPVariable variable, void* value)
  129. {
  130. return pluginViewForInstance(instance)->setValue(variable, value);
  131. }
  132. void* NPN_GetJavaEnv()
  133. {
  134. // Unsupported
  135. return 0;
  136. }
  137. void* NPN_GetJavaPeer(NPP)
  138. {
  139. // Unsupported
  140. return 0;
  141. }
  142. void NPN_PushPopupsEnabledState(NPP instance, NPBool enabled)
  143. {
  144. pluginViewForInstance(instance)->pushPopupsEnabledState(enabled);
  145. }
  146. void NPN_PopPopupsEnabledState(NPP instance)
  147. {
  148. pluginViewForInstance(instance)->popPopupsEnabledState();
  149. }
  150. extern "C" typedef void PluginThreadAsyncCallFunction(void*);
  151. void NPN_PluginThreadAsyncCall(NPP instance, PluginThreadAsyncCallFunction func, void* userData)
  152. {
  153. // Callback function type only differs from MainThreadFunction by being extern "C", which doesn't affect calling convention on any compilers we use.
  154. PluginMainThreadScheduler::scheduler().scheduleCall(instance, reinterpret_cast<PluginMainThreadScheduler::MainThreadFunction*>(func), userData);
  155. }
  156. NPError NPN_GetValueForURL(NPP instance, NPNURLVariable variable, const char* url, char** value, uint32_t* len)
  157. {
  158. return pluginViewForInstance(instance)->getValueForURL(variable, url, value, len);
  159. }
  160. NPError NPN_SetValueForURL(NPP instance, NPNURLVariable variable, const char* url, const char* value, uint32_t len)
  161. {
  162. return pluginViewForInstance(instance)->setValueForURL(variable, url, value, len);
  163. }
  164. 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)
  165. {
  166. return pluginViewForInstance(instance)->getAuthenticationInfo(protocol, host, port, scheme, realm, username, ulen, password, plen);
  167. }
  168. NPError NPN_PopUpContextMenu(NPP instance, NPMenu* menu)
  169. {
  170. #if PLATFORM(QT) && defined(XP_MACOSX)
  171. PluginView* plugin = pluginViewForInstance(instance);
  172. plugin->popUpContextMenu(menu);
  173. return NPERR_NO_ERROR;
  174. #else
  175. UNUSED_PARAM(instance);
  176. UNUSED_PARAM(menu);
  177. return NPERR_NO_ERROR;
  178. #endif // PLATFORM(QT) && defined(XP_MACOSX)
  179. }
  180. #endif