PluginPackageQt.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
  3. * Copyright (C) 2008 Collabora Ltd. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  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. *
  14. * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
  15. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  17. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
  18. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  19. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  20. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  21. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  22. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "config.h"
  27. #include "PluginPackage.h"
  28. #include "MIMETypeRegistry.h"
  29. #include "PluginDatabase.h"
  30. #include "PluginDebug.h"
  31. #include "npruntime_impl.h"
  32. #include <QFileInfo>
  33. #include <wtf/text/CString.h>
  34. namespace WebCore {
  35. bool PluginPackage::fetchInfo()
  36. {
  37. if (!m_module) {
  38. if (isPluginBlacklisted())
  39. return false;
  40. m_module = new QLibrary((QString)m_path);
  41. m_module->setLoadHints(QLibrary::ResolveAllSymbolsHint);
  42. if (!m_module->load()) {
  43. LOG(Plugins, "%s not loaded (%s)", m_path.utf8().data(),
  44. m_module->errorString().toLatin1().constData());
  45. return false;
  46. }
  47. // This is technically wrong (not matched by a decrement), but
  48. // it matches the previous behavior (fetchInfo calling load) and
  49. // prevents crashes in flash due to unload+load.
  50. m_loadCount++;
  51. }
  52. NPP_GetValueProcPtr gv = (NPP_GetValueProcPtr)m_module->resolve("NP_GetValue");
  53. NP_GetMIMEDescriptionFuncPtr gm =
  54. (NP_GetMIMEDescriptionFuncPtr)m_module->resolve("NP_GetMIMEDescription");
  55. if (!gm || !gv)
  56. return false;
  57. char *buf = 0;
  58. NPError err = gv(0, NPPVpluginNameString, (void*) &buf);
  59. if (err != NPERR_NO_ERROR)
  60. return false;
  61. m_name = String::fromUTF8(buf);
  62. err = gv(0, NPPVpluginDescriptionString, (void*) &buf);
  63. if (err != NPERR_NO_ERROR)
  64. return false;
  65. m_description = String::fromUTF8(buf);
  66. determineModuleVersionFromDescription();
  67. setMIMEDescription(String::fromUTF8(gm()));
  68. return true;
  69. }
  70. void PluginPackage::setMIMEDescription(const String& mimeDescription)
  71. {
  72. m_fullMIMEDescription = mimeDescription.lower();
  73. Vector<String> types;
  74. mimeDescription.lower().split(UChar(';'), false, types);
  75. for (unsigned i = 0; i < types.size(); ++i) {
  76. Vector<String> mime;
  77. types[i].split(UChar(':'), true, mime);
  78. if (mime.size() > 0) {
  79. Vector<String> exts;
  80. if (mime.size() > 1)
  81. mime[1].split(UChar(','), false, exts);
  82. determineQuirks(mime[0]);
  83. m_mimeToExtensions.add(mime[0], exts);
  84. if (mime.size() > 2)
  85. m_mimeToDescriptions.add(mime[0], mime[2]);
  86. }
  87. }
  88. }
  89. static NPError staticPluginQuirkRequiresGtkToolKit_NPN_GetValue(NPP instance, NPNVariable variable, void* value)
  90. {
  91. if (variable == NPNVToolkit) {
  92. *static_cast<uint32_t*>(value) = 2;
  93. return NPERR_NO_ERROR;
  94. }
  95. return NPN_GetValue(instance, variable, value);
  96. }
  97. static void initializeGtk(QLibrary* module = 0)
  98. {
  99. // Ensures missing Gtk initialization in some versions of Adobe's flash player
  100. // plugin do not cause crashes. See BR# 40567, 44324, and 44405 for details.
  101. if (module) {
  102. typedef void *(*gtk_init_ptr)(int*, char***);
  103. gtk_init_ptr gtkInit = (gtk_init_ptr)module->resolve("gtk_init");
  104. if (gtkInit) {
  105. // Prevent gtk_init() from replacing the X error handlers, since the Gtk
  106. // handlers abort when they receive an X error, thus killing the viewer.
  107. #ifdef Q_WS_X11
  108. int (*old_error_handler)(Display*, XErrorEvent*) = XSetErrorHandler(0);
  109. int (*old_io_error_handler)(Display*) = XSetIOErrorHandler(0);
  110. #endif
  111. gtkInit(0, 0);
  112. #ifdef Q_WS_X11
  113. XSetErrorHandler(old_error_handler);
  114. XSetIOErrorHandler(old_io_error_handler);
  115. #endif
  116. return;
  117. }
  118. }
  119. QLibrary library(QLatin1String("libgtk-x11-2.0"), 0);
  120. if (library.load()) {
  121. typedef void *(*gtk_init_check_ptr)(int*, char***);
  122. gtk_init_check_ptr gtkInitCheck = (gtk_init_check_ptr)library.resolve("gtk_init_check");
  123. // NOTE: We're using gtk_init_check() since gtk_init() calls exit() on failure.
  124. if (gtkInitCheck)
  125. (void) gtkInitCheck(0, 0);
  126. }
  127. }
  128. bool PluginPackage::isPluginBlacklisted()
  129. {
  130. // TODO: enumerate all plugins that are incompatible with Qt5.
  131. const QLatin1String pluginBlacklist[] = {
  132. QLatin1String("skypebuttons")
  133. };
  134. QString baseName = QFileInfo(static_cast<QString>(m_path)).baseName();
  135. for (unsigned i = 0; i < sizeof(pluginBlacklist) / sizeof(QLatin1String); ++i) {
  136. if (baseName == pluginBlacklist[i])
  137. return true;
  138. }
  139. return false;
  140. }
  141. bool PluginPackage::load()
  142. {
  143. if (m_isLoaded) {
  144. m_loadCount++;
  145. return true;
  146. }
  147. if (isPluginBlacklisted())
  148. return false;
  149. if (!m_module) {
  150. m_module = new QLibrary((QString)m_path);
  151. m_module->setLoadHints(QLibrary::ResolveAllSymbolsHint);
  152. if (!m_module->load()) {
  153. LOG(Plugins, "%s not loaded (%s)", m_path.utf8().data(),
  154. m_module->errorString().toLatin1().constData());
  155. return false;
  156. }
  157. }
  158. m_isLoaded = true;
  159. NP_InitializeFuncPtr NP_Initialize;
  160. NPError npErr;
  161. NP_Initialize = (NP_InitializeFuncPtr)m_module->resolve("NP_Initialize");
  162. m_NPP_Shutdown = (NPP_ShutdownProcPtr)m_module->resolve("NP_Shutdown");
  163. if (!NP_Initialize || !m_NPP_Shutdown)
  164. goto abort;
  165. memset(&m_pluginFuncs, 0, sizeof(m_pluginFuncs));
  166. m_pluginFuncs.size = sizeof(m_pluginFuncs);
  167. initializeBrowserFuncs();
  168. if (m_path.contains("npwrapper.") || m_path.contains("gnash")) {
  169. // nspluginwrapper relies on the toolkit value to know if glib is available
  170. // It does so in NP_Initialize with a null instance, therefore it is done this way:
  171. m_browserFuncs.getvalue = staticPluginQuirkRequiresGtkToolKit_NPN_GetValue;
  172. // Workaround Adobe's failure to properly initialize Gtk in some versions
  173. // of their flash player plugin.
  174. initializeGtk();
  175. } else if (m_path.contains("flashplayer")) {
  176. // Workaround Adobe's failure to properly initialize Gtk in some versions
  177. // of their flash player plugin.
  178. initializeGtk(m_module);
  179. }
  180. #if defined(XP_UNIX)
  181. npErr = NP_Initialize(&m_browserFuncs, &m_pluginFuncs);
  182. #else
  183. npErr = NP_Initialize(&m_browserFuncs);
  184. #endif
  185. if (npErr != NPERR_NO_ERROR)
  186. goto abort;
  187. m_loadCount++;
  188. return true;
  189. abort:
  190. unloadWithoutShutdown();
  191. return false;
  192. }
  193. uint16_t PluginPackage::NPVersion() const
  194. {
  195. return NP_VERSION_MINOR;
  196. }
  197. }