PluginPackage.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. * Copyright (C) 2006, 2008 Apple Inc. All rights reserved.
  3. * Copyright (C) 2008 Collabora Ltd. All rights reserved.
  4. * Copyright (C) 2009 Holger Hans Peter Freyther
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
  16. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  18. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
  19. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  23. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "config.h"
  28. #include "PluginPackage.h"
  29. #include "MIMETypeRegistry.h"
  30. #include "PluginDatabase.h"
  31. #include "PluginDebug.h"
  32. #include "Timer.h"
  33. #include "npruntime_impl.h"
  34. #include <string.h>
  35. #include <wtf/OwnArrayPtr.h>
  36. #include <wtf/text/CString.h>
  37. namespace WebCore {
  38. PluginPackage::~PluginPackage()
  39. {
  40. // This destructor gets called during refresh() if PluginDatabase's
  41. // PluginSet hash is already populated, as it removes items from
  42. // the hash table. Calling the destructor on a loaded plug-in of
  43. // course would cause a crash, so we check to call unload before we
  44. // ASSERT.
  45. // FIXME: There is probably a better way to fix this.
  46. if (!m_loadCount)
  47. unloadWithoutShutdown();
  48. else
  49. unload();
  50. ASSERT(!m_isLoaded);
  51. }
  52. void PluginPackage::freeLibrarySoon()
  53. {
  54. ASSERT(!m_freeLibraryTimer.isActive());
  55. ASSERT(m_module);
  56. ASSERT(!m_loadCount);
  57. m_freeLibraryTimer.startOneShot(0);
  58. }
  59. void PluginPackage::freeLibraryTimerFired(Timer<PluginPackage>*)
  60. {
  61. ASSERT(m_module);
  62. // Do nothing if the module got loaded again meanwhile
  63. if (!m_loadCount) {
  64. unloadModule(m_module);
  65. m_module = 0;
  66. }
  67. }
  68. int PluginPackage::compare(const PluginPackage& compareTo) const
  69. {
  70. // Sort plug-ins that allow multiple instances first.
  71. bool AallowsMultipleInstances = !quirks().contains(PluginQuirkDontAllowMultipleInstances);
  72. bool BallowsMultipleInstances = !compareTo.quirks().contains(PluginQuirkDontAllowMultipleInstances);
  73. if (AallowsMultipleInstances != BallowsMultipleInstances)
  74. return AallowsMultipleInstances ? -1 : 1;
  75. // Sort plug-ins in a preferred path first.
  76. bool AisInPreferredDirectory = PluginDatabase::isPreferredPluginDirectory(parentDirectory());
  77. bool BisInPreferredDirectory = PluginDatabase::isPreferredPluginDirectory(compareTo.parentDirectory());
  78. if (AisInPreferredDirectory != BisInPreferredDirectory)
  79. return AisInPreferredDirectory ? -1 : 1;
  80. int diff = strcmp(name().utf8().data(), compareTo.name().utf8().data());
  81. if (diff)
  82. return diff;
  83. diff = compareFileVersion(compareTo.version());
  84. if (diff)
  85. return diff;
  86. return strcmp(parentDirectory().utf8().data(), compareTo.parentDirectory().utf8().data());
  87. }
  88. PluginPackage::PluginPackage(const String& path, const time_t& lastModified)
  89. : m_isEnabled(true)
  90. , m_isLoaded(false)
  91. , m_loadCount(0)
  92. , m_path(path)
  93. , m_moduleVersion(0)
  94. , m_module(0)
  95. , m_lastModified(lastModified)
  96. , m_freeLibraryTimer(this, &PluginPackage::freeLibraryTimerFired)
  97. #if ENABLE(NETSCAPE_PLUGIN_METADATA_CACHE)
  98. , m_infoIsFromCache(true)
  99. #endif
  100. {
  101. m_fileName = pathGetFileName(m_path);
  102. m_parentDirectory = m_path.left(m_path.length() - m_fileName.length() - 1);
  103. }
  104. void PluginPackage::unload()
  105. {
  106. if (!m_isLoaded)
  107. return;
  108. if (--m_loadCount > 0)
  109. return;
  110. m_NPP_Shutdown();
  111. unloadWithoutShutdown();
  112. }
  113. void PluginPackage::unloadWithoutShutdown()
  114. {
  115. if (!m_isLoaded)
  116. return;
  117. ASSERT(!m_loadCount);
  118. ASSERT(m_module);
  119. // <rdar://5530519>: Crash when closing tab with pdf file (Reader 7 only)
  120. // If the plugin has subclassed its parent window, as with Reader 7, we may have
  121. // gotten here by way of the plugin's internal window proc forwarding a message to our
  122. // original window proc. If we free the plugin library from here, we will jump back
  123. // to code we just freed when we return, so delay calling FreeLibrary at least until
  124. // the next message loop
  125. freeLibrarySoon();
  126. m_isLoaded = false;
  127. }
  128. void PluginPackage::setEnabled(bool enabled)
  129. {
  130. m_isEnabled = enabled;
  131. }
  132. PassRefPtr<PluginPackage> PluginPackage::createPackage(const String& path, const time_t& lastModified)
  133. {
  134. RefPtr<PluginPackage> package = adoptRef(new PluginPackage(path, lastModified));
  135. if (!package->fetchInfo())
  136. return 0;
  137. return package.release();
  138. }
  139. #if ENABLE(NETSCAPE_PLUGIN_METADATA_CACHE)
  140. PassRefPtr<PluginPackage> PluginPackage::createPackageFromCache(const String& path, const time_t& lastModified, const String& name, const String& description, const String& mimeDescription)
  141. {
  142. RefPtr<PluginPackage> package = adoptRef(new PluginPackage(path, lastModified));
  143. package->m_name = name;
  144. package->m_description = description;
  145. package->determineModuleVersionFromDescription();
  146. package->setMIMEDescription(mimeDescription);
  147. package->m_infoIsFromCache = true;
  148. return package.release();
  149. }
  150. #endif
  151. #if defined(XP_UNIX)
  152. void PluginPackage::determineQuirks(const String& mimeType)
  153. {
  154. if (MIMETypeRegistry::isJavaAppletMIMEType(mimeType)) {
  155. // Because a single process cannot create multiple VMs, and we cannot reliably unload a
  156. // Java VM, we cannot unload the Java plugin, or we'll lose reference to our only VM
  157. m_quirks.add(PluginQuirkDontUnloadPlugin);
  158. // Setting the window region to an empty region causes bad scrolling repaint problems
  159. // with the Java plug-in.
  160. m_quirks.add(PluginQuirkDontClipToZeroRectWhenScrolling);
  161. return;
  162. }
  163. if (mimeType == "application/x-shockwave-flash") {
  164. static const PlatformModuleVersion flashTenVersion(0x0a000000);
  165. if (compareFileVersion(flashTenVersion) >= 0) {
  166. // Flash 10.0 b218 doesn't like having a NULL window handle
  167. m_quirks.add(PluginQuirkDontSetNullWindowHandleOnDestroy);
  168. #if PLATFORM(QT)
  169. m_quirks.add(PluginQuirkRequiresGtkToolKit);
  170. #endif
  171. } else {
  172. // Flash 9 and older requests windowless plugins if we return a mozilla user agent
  173. m_quirks.add(PluginQuirkWantsMozillaUserAgent);
  174. }
  175. #if PLATFORM(QT)
  176. // Flash will crash on repeated calls to SetWindow in windowed mode
  177. m_quirks.add(PluginQuirkDontCallSetWindowMoreThanOnce);
  178. #endif
  179. #if CPU(X86_64)
  180. // 64-bit Flash freezes if right-click is sent in windowless mode
  181. m_quirks.add(PluginQuirkIgnoreRightClickInWindowlessMode);
  182. #endif
  183. m_quirks.add(PluginQuirkRequiresDefaultScreenDepth);
  184. m_quirks.add(PluginQuirkThrottleInvalidate);
  185. m_quirks.add(PluginQuirkThrottleWMUserPlusOneMessages);
  186. m_quirks.add(PluginQuirkFlashURLNotifyBug);
  187. }
  188. }
  189. #endif
  190. #if !OS(WINDOWS)
  191. void PluginPackage::determineModuleVersionFromDescription()
  192. {
  193. // It's a bit lame to detect the plugin version by parsing it
  194. // from the plugin description string, but it doesn't seem that
  195. // version information is available in any standardized way at
  196. // the module level, like in Windows
  197. if (m_description.isEmpty())
  198. return;
  199. if (m_description.startsWith("Shockwave Flash") && m_description.length() >= 19) {
  200. // The flash version as a PlatformModuleVersion differs on Unix from Windows
  201. // since the revision can be larger than a 8 bits, so we allow it 16 here and
  202. // push the major/minor up 8 bits. Thus on Unix, Flash's version may be
  203. // 0x0a000000 instead of 0x000a0000.
  204. Vector<String> versionParts;
  205. m_description.substring(16).split(' ', /*allowEmptyEntries =*/ false, versionParts);
  206. if (versionParts.isEmpty())
  207. return;
  208. if (versionParts.size() >= 1) {
  209. Vector<String> majorMinorParts;
  210. versionParts[0].split('.', majorMinorParts);
  211. if (majorMinorParts.size() >= 1) {
  212. bool converted = false;
  213. unsigned major = majorMinorParts[0].toUInt(&converted);
  214. if (converted)
  215. m_moduleVersion = (major & 0xff) << 24;
  216. }
  217. if (majorMinorParts.size() == 2) {
  218. bool converted = false;
  219. unsigned minor = majorMinorParts[1].toUInt(&converted);
  220. if (converted)
  221. m_moduleVersion |= (minor & 0xff) << 16;
  222. }
  223. }
  224. if (versionParts.size() >= 2) {
  225. String revision = versionParts[1];
  226. if (revision.length() > 1 && (revision[0] == 'r' || revision[0] == 'b')) {
  227. revision.remove(0, 1);
  228. m_moduleVersion |= revision.toInt() & 0xffff;
  229. }
  230. }
  231. }
  232. }
  233. #endif
  234. #if ENABLE(NETSCAPE_PLUGIN_API)
  235. void PluginPackage::initializeBrowserFuncs()
  236. {
  237. memset(&m_browserFuncs, 0, sizeof(m_browserFuncs));
  238. m_browserFuncs.size = sizeof(m_browserFuncs);
  239. m_browserFuncs.version = NPVersion();
  240. m_browserFuncs.geturl = NPN_GetURL;
  241. m_browserFuncs.posturl = NPN_PostURL;
  242. m_browserFuncs.requestread = NPN_RequestRead;
  243. m_browserFuncs.newstream = NPN_NewStream;
  244. m_browserFuncs.write = NPN_Write;
  245. m_browserFuncs.destroystream = NPN_DestroyStream;
  246. m_browserFuncs.status = NPN_Status;
  247. m_browserFuncs.uagent = NPN_UserAgent;
  248. m_browserFuncs.memalloc = NPN_MemAlloc;
  249. m_browserFuncs.memfree = NPN_MemFree;
  250. m_browserFuncs.memflush = NPN_MemFlush;
  251. m_browserFuncs.reloadplugins = NPN_ReloadPlugins;
  252. m_browserFuncs.geturlnotify = NPN_GetURLNotify;
  253. m_browserFuncs.posturlnotify = NPN_PostURLNotify;
  254. m_browserFuncs.getvalue = NPN_GetValue;
  255. m_browserFuncs.setvalue = NPN_SetValue;
  256. m_browserFuncs.invalidaterect = NPN_InvalidateRect;
  257. m_browserFuncs.invalidateregion = NPN_InvalidateRegion;
  258. m_browserFuncs.forceredraw = NPN_ForceRedraw;
  259. m_browserFuncs.getJavaEnv = NPN_GetJavaEnv;
  260. m_browserFuncs.getJavaPeer = NPN_GetJavaPeer;
  261. m_browserFuncs.pushpopupsenabledstate = NPN_PushPopupsEnabledState;
  262. m_browserFuncs.poppopupsenabledstate = NPN_PopPopupsEnabledState;
  263. m_browserFuncs.pluginthreadasynccall = NPN_PluginThreadAsyncCall;
  264. m_browserFuncs.releasevariantvalue = _NPN_ReleaseVariantValue;
  265. m_browserFuncs.getstringidentifier = _NPN_GetStringIdentifier;
  266. m_browserFuncs.getstringidentifiers = _NPN_GetStringIdentifiers;
  267. m_browserFuncs.getintidentifier = _NPN_GetIntIdentifier;
  268. m_browserFuncs.identifierisstring = _NPN_IdentifierIsString;
  269. m_browserFuncs.utf8fromidentifier = _NPN_UTF8FromIdentifier;
  270. m_browserFuncs.intfromidentifier = _NPN_IntFromIdentifier;
  271. m_browserFuncs.createobject = _NPN_CreateObject;
  272. m_browserFuncs.retainobject = _NPN_RetainObject;
  273. m_browserFuncs.releaseobject = _NPN_ReleaseObject;
  274. m_browserFuncs.invoke = _NPN_Invoke;
  275. m_browserFuncs.invokeDefault = _NPN_InvokeDefault;
  276. m_browserFuncs.evaluate = _NPN_Evaluate;
  277. m_browserFuncs.getproperty = _NPN_GetProperty;
  278. m_browserFuncs.setproperty = _NPN_SetProperty;
  279. m_browserFuncs.removeproperty = _NPN_RemoveProperty;
  280. m_browserFuncs.hasproperty = _NPN_HasProperty;
  281. m_browserFuncs.hasmethod = _NPN_HasMethod;
  282. m_browserFuncs.setexception = _NPN_SetException;
  283. m_browserFuncs.enumerate = _NPN_Enumerate;
  284. m_browserFuncs.construct = _NPN_Construct;
  285. m_browserFuncs.getvalueforurl = NPN_GetValueForURL;
  286. m_browserFuncs.setvalueforurl = NPN_SetValueForURL;
  287. m_browserFuncs.getauthenticationinfo = NPN_GetAuthenticationInfo;
  288. m_browserFuncs.popupcontextmenu = NPN_PopUpContextMenu;
  289. }
  290. #endif // ENABLE(NETSCAPE_PLUGIN_API)
  291. #if ENABLE(PLUGIN_PACKAGE_SIMPLE_HASH)
  292. unsigned PluginPackage::hash() const
  293. {
  294. struct HashCodes {
  295. unsigned hash;
  296. time_t modifiedDate;
  297. } hashCodes;
  298. hashCodes.hash = m_path.impl()->hash();
  299. hashCodes.modifiedDate = m_lastModified;
  300. return StringHasher::hashMemory<sizeof(hashCodes)>(&hashCodes);
  301. }
  302. bool PluginPackage::equal(const PluginPackage& a, const PluginPackage& b)
  303. {
  304. return a.m_description == b.m_description;
  305. }
  306. #endif
  307. int PluginPackage::compareFileVersion(const PlatformModuleVersion& compareVersion) const
  308. {
  309. // return -1, 0, or 1 if plug-in version is less than, equal to, or greater than
  310. // the passed version
  311. #if OS(WINDOWS)
  312. if (m_moduleVersion.mostSig != compareVersion.mostSig)
  313. return m_moduleVersion.mostSig > compareVersion.mostSig ? 1 : -1;
  314. if (m_moduleVersion.leastSig != compareVersion.leastSig)
  315. return m_moduleVersion.leastSig > compareVersion.leastSig ? 1 : -1;
  316. #else
  317. if (m_moduleVersion != compareVersion)
  318. return m_moduleVersion > compareVersion ? 1 : -1;
  319. #endif
  320. return 0;
  321. }
  322. #if ENABLE(NETSCAPE_PLUGIN_METADATA_CACHE)
  323. bool PluginPackage::ensurePluginLoaded()
  324. {
  325. if (!m_infoIsFromCache)
  326. return m_isLoaded;
  327. m_quirks = PluginQuirkSet();
  328. m_name = String();
  329. m_description = String();
  330. m_fullMIMEDescription = String();
  331. m_moduleVersion = 0;
  332. return fetchInfo();
  333. }
  334. #endif
  335. }