blob: ffce76d7acb8afcf122da93f850601798ec0089b [file] [log] [blame]
pewtermoose@webkit.orgfa3bc3f2008-02-28 20:46:37 +00001/*
2 * Copyright (C) 2006, 2008 Apple Inc. All rights reserved.
alp@webkit.orga55698b2008-05-01 18:15:39 +00003 * Copyright (C) 2008 Collabora Ltd. All rights reserved.
zecke@webkit.org05363c92009-05-25 03:28:41 +00004 * Copyright (C) 2009 Holger Hans Peter Freyther
pewtermoose@webkit.orgfa3bc3f2008-02-28 20:46:37 +00005 *
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
kenneth@webkit.org49395932009-07-28 20:29:19 +000025 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
pewtermoose@webkit.orgfa3bc3f2008-02-28 20:46:37 +000026 */
27
28#include "config.h"
29#include "PluginPackage.h"
30
31#include "MIMETypeRegistry.h"
alp@webkit.orga55698b2008-05-01 18:15:39 +000032#include "PluginDatabase.h"
pewtermoose@webkit.orgfa3bc3f2008-02-28 20:46:37 +000033#include "PluginDebug.h"
34#include "Timer.h"
35#include "npruntime_impl.h"
alp@webkit.orga55698b2008-05-01 18:15:39 +000036#include <string.h>
pewtermoose@webkit.orgfa3bc3f2008-02-28 20:46:37 +000037#include <wtf/OwnArrayPtr.h>
barraclough@apple.com00b79922010-03-31 05:44:34 +000038#include <wtf/text/CString.h>
pewtermoose@webkit.orgfa3bc3f2008-02-28 20:46:37 +000039
40namespace WebCore {
41
42PluginPackage::~PluginPackage()
43{
alp@webkit.orga55698b2008-05-01 18:15:39 +000044 // This destructor gets called during refresh() if PluginDatabase's
45 // PluginSet hash is already populated, as it removes items from
46 // the hash table. Calling the destructor on a loaded plug-in of
47 // course would cause a crash, so we check to call unload before we
48 // ASSERT.
49 // FIXME: There is probably a better way to fix this.
kenneth@webkit.org49395932009-07-28 20:29:19 +000050 if (!m_loadCount)
alp@webkit.orga55698b2008-05-01 18:15:39 +000051 unloadWithoutShutdown();
52 else
53 unload();
54
pewtermoose@webkit.orgfa3bc3f2008-02-28 20:46:37 +000055 ASSERT(!m_isLoaded);
56}
57
58void PluginPackage::freeLibrarySoon()
59{
60 ASSERT(!m_freeLibraryTimer.isActive());
61 ASSERT(m_module);
kenneth@webkit.org49395932009-07-28 20:29:19 +000062 ASSERT(!m_loadCount);
pewtermoose@webkit.orgfa3bc3f2008-02-28 20:46:37 +000063
64 m_freeLibraryTimer.startOneShot(0);
65}
66
67void PluginPackage::freeLibraryTimerFired(Timer<PluginPackage>*)
68{
69 ASSERT(m_module);
kenneth@webkit.org49395932009-07-28 20:29:19 +000070 ASSERT(!m_loadCount);
pewtermoose@webkit.orgfa3bc3f2008-02-28 20:46:37 +000071
72 unloadModule(m_module);
73 m_module = 0;
74}
75
alp@webkit.orga55698b2008-05-01 18:15:39 +000076
77int PluginPackage::compare(const PluginPackage& compareTo) const
78{
79 // Sort plug-ins that allow multiple instances first.
80 bool AallowsMultipleInstances = !quirks().contains(PluginQuirkDontAllowMultipleInstances);
81 bool BallowsMultipleInstances = !compareTo.quirks().contains(PluginQuirkDontAllowMultipleInstances);
82 if (AallowsMultipleInstances != BallowsMultipleInstances)
83 return AallowsMultipleInstances ? -1 : 1;
84
85 // Sort plug-ins in a preferred path first.
86 bool AisInPreferredDirectory = PluginDatabase::isPreferredPluginDirectory(parentDirectory());
87 bool BisInPreferredDirectory = PluginDatabase::isPreferredPluginDirectory(compareTo.parentDirectory());
88 if (AisInPreferredDirectory != BisInPreferredDirectory)
89 return AisInPreferredDirectory ? -1 : 1;
90
91 int diff = strcmp(name().utf8().data(), compareTo.name().utf8().data());
92 if (diff)
93 return diff;
94
staikos@webkit.org08b18292009-03-25 21:31:44 +000095 diff = compareFileVersion(compareTo.version());
96 if (diff)
alp@webkit.orga55698b2008-05-01 18:15:39 +000097 return diff;
98
99 return strcmp(parentDirectory().utf8().data(), compareTo.parentDirectory().utf8().data());
100}
101
aroben@apple.comcc718fb2008-03-31 16:53:33 +0000102PluginPackage::PluginPackage(const String& path, const time_t& lastModified)
kenneth@webkit.orgbb7c59d2009-07-29 14:36:15 +0000103 : m_isEnabled(true)
104 , m_isLoaded(false)
jchaffraix@webkit.org660eccc2008-05-14 15:50:31 +0000105 , m_loadCount(0)
106 , m_path(path)
pewtermoose@webkit.orgfa3bc3f2008-02-28 20:46:37 +0000107 , m_moduleVersion(0)
108 , m_module(0)
109 , m_lastModified(lastModified)
pewtermoose@webkit.orgfa3bc3f2008-02-28 20:46:37 +0000110 , m_freeLibraryTimer(this, &PluginPackage::freeLibraryTimerFired)
commit-queue@webkit.org67b334a2010-08-28 04:32:17 +0000111#if ENABLE(NETSCAPE_PLUGIN_METADATA_CACHE)
112 , m_infoIsFromCache(true)
113#endif
pewtermoose@webkit.orgfa3bc3f2008-02-28 20:46:37 +0000114{
115 m_fileName = pathGetFileName(m_path);
116 m_parentDirectory = m_path.left(m_path.length() - m_fileName.length() - 1);
117}
118
119void PluginPackage::unload()
120{
121 if (!m_isLoaded)
122 return;
123
124 if (--m_loadCount > 0)
125 return;
126
127 m_NPP_Shutdown();
128
129 unloadWithoutShutdown();
130}
131
132void PluginPackage::unloadWithoutShutdown()
133{
134 if (!m_isLoaded)
135 return;
136
kenneth@webkit.org49395932009-07-28 20:29:19 +0000137 ASSERT(!m_loadCount);
pewtermoose@webkit.orgfa3bc3f2008-02-28 20:46:37 +0000138 ASSERT(m_module);
139
140 // <rdar://5530519>: Crash when closing tab with pdf file (Reader 7 only)
141 // If the plugin has subclassed its parent window, as with Reader 7, we may have
142 // gotten here by way of the plugin's internal window proc forwarding a message to our
143 // original window proc. If we free the plugin library from here, we will jump back
144 // to code we just freed when we return, so delay calling FreeLibrary at least until
145 // the next message loop
146 freeLibrarySoon();
147
148 m_isLoaded = false;
149}
150
kenneth@webkit.orgbb7c59d2009-07-29 14:36:15 +0000151void PluginPackage::setEnabled(bool enabled)
152{
153 m_isEnabled = enabled;
154}
155
aroben@apple.comcc718fb2008-03-31 16:53:33 +0000156PassRefPtr<PluginPackage> PluginPackage::createPackage(const String& path, const time_t& lastModified)
pewtermoose@webkit.orgfa3bc3f2008-02-28 20:46:37 +0000157{
aroben@apple.com7cc6f852008-03-31 16:52:39 +0000158 RefPtr<PluginPackage> package = adoptRef(new PluginPackage(path, lastModified));
pewtermoose@webkit.orgfa3bc3f2008-02-28 20:46:37 +0000159
aroben@apple.com7cc6f852008-03-31 16:52:39 +0000160 if (!package->fetchInfo())
pewtermoose@webkit.orgfa3bc3f2008-02-28 20:46:37 +0000161 return 0;
hausmann@webkit.org49f97662008-11-05 15:31:10 +0000162
aroben@apple.com7cc6f852008-03-31 16:52:39 +0000163 return package.release();
pewtermoose@webkit.orgfa3bc3f2008-02-28 20:46:37 +0000164}
165
commit-queue@webkit.org67b334a2010-08-28 04:32:17 +0000166#if ENABLE(NETSCAPE_PLUGIN_METADATA_CACHE)
167PassRefPtr<PluginPackage> PluginPackage::createPackageFromCache(const String& path, const time_t& lastModified, const String& name, const String& description, const String& mimeDescription)
168{
169 RefPtr<PluginPackage> package = adoptRef(new PluginPackage(path, lastModified));
170 package->m_name = name;
171 package->m_description = description;
172 package->determineModuleVersionFromDescription();
173 package->setMIMEDescription(mimeDescription);
174 package->m_infoIsFromCache = true;
175 return package.release();
176}
177#endif
178
hausmann@webkit.org49f97662008-11-05 15:31:10 +0000179#if defined(XP_UNIX)
180void PluginPackage::determineQuirks(const String& mimeType)
181{
182 if (MIMETypeRegistry::isJavaAppletMIMEType(mimeType)) {
183 // Because a single process cannot create multiple VMs, and we cannot reliably unload a
184 // Java VM, we cannot unload the Java plugin, or we'll lose reference to our only VM
185 m_quirks.add(PluginQuirkDontUnloadPlugin);
186
187 // Setting the window region to an empty region causes bad scrolling repaint problems
188 // with the Java plug-in.
189 m_quirks.add(PluginQuirkDontClipToZeroRectWhenScrolling);
190 return;
191 }
192
193 if (mimeType == "application/x-shockwave-flash") {
194 static const PlatformModuleVersion flashTenVersion(0x0a000000);
195
196 if (compareFileVersion(flashTenVersion) >= 0) {
197 // Flash 10.0 b218 doesn't like having a NULL window handle
198 m_quirks.add(PluginQuirkDontSetNullWindowHandleOnDestroy);
hausmann@webkit.orgf9c88732008-11-05 15:31:22 +0000199#if PLATFORM(QT)
200 m_quirks.add(PluginQuirkRequiresGtkToolKit);
201#endif
hausmann@webkit.org49f97662008-11-05 15:31:10 +0000202 } else {
203 // Flash 9 and older requests windowless plugins if we return a mozilla user agent
204 m_quirks.add(PluginQuirkWantsMozillaUserAgent);
205 }
206
andreas.kling@nokia.com973afea2010-10-05 21:41:41 +0000207#if PLATFORM(QT)
208 // Flash will crash on repeated calls to SetWindow in windowed mode
andreas.kling@nokia.comc16990f2010-09-27 11:45:31 +0000209 m_quirks.add(PluginQuirkDontCallSetWindowMoreThanOnce);
mrobinson@webkit.org1da881f2011-11-18 06:09:25 +0000210#endif
girish@forwardbias.in6ec53c42010-10-12 21:12:27 +0000211
212#if CPU(X86_64)
213 // 64-bit Flash freezes if right-click is sent in windowless mode
214 m_quirks.add(PluginQuirkIgnoreRightClickInWindowlessMode);
215#endif
andreas.kling@nokia.comc16990f2010-09-27 11:45:31 +0000216
girish@forwardbias.in82054cb2010-08-17 18:39:54 +0000217 m_quirks.add(PluginQuirkRequiresDefaultScreenDepth);
hausmann@webkit.org49f97662008-11-05 15:31:10 +0000218 m_quirks.add(PluginQuirkThrottleInvalidate);
219 m_quirks.add(PluginQuirkThrottleWMUserPlusOneMessages);
220 m_quirks.add(PluginQuirkFlashURLNotifyBug);
221 }
222}
223#endif
224
mjs@apple.comacbcc282010-01-05 08:58:28 +0000225#if !OS(WINDOWS)
hausmann@webkit.org49f97662008-11-05 15:31:10 +0000226void PluginPackage::determineModuleVersionFromDescription()
227{
228 // It's a bit lame to detect the plugin version by parsing it
229 // from the plugin description string, but it doesn't seem that
230 // version information is available in any standardized way at
231 // the module level, like in Windows
232
233 if (m_description.isEmpty())
234 return;
235
236 if (m_description.startsWith("Shockwave Flash") && m_description.length() >= 19) {
237 // The flash version as a PlatformModuleVersion differs on Unix from Windows
238 // since the revision can be larger than a 8 bits, so we allow it 16 here and
239 // push the major/minor up 8 bits. Thus on Unix, Flash's version may be
240 // 0x0a000000 instead of 0x000a0000.
241
242 Vector<String> versionParts;
243 m_description.substring(16).split(' ', /*allowEmptyEntries =*/ false, versionParts);
244 if (versionParts.isEmpty())
245 return;
246
247 if (versionParts.size() >= 1) {
248 Vector<String> majorMinorParts;
249 versionParts[0].split('.', majorMinorParts);
250 if (majorMinorParts.size() >= 1) {
251 bool converted = false;
252 unsigned major = majorMinorParts[0].toUInt(&converted);
253 if (converted)
254 m_moduleVersion = (major & 0xff) << 24;
255 }
256 if (majorMinorParts.size() == 2) {
257 bool converted = false;
258 unsigned minor = majorMinorParts[1].toUInt(&converted);
259 if (converted)
260 m_moduleVersion |= (minor & 0xff) << 16;
261 }
262 }
263
264 if (versionParts.size() >= 2) {
265 String revision = versionParts[1];
266 if (revision.length() > 1 && (revision[0] == 'r' || revision[0] == 'b')) {
267 revision.remove(0, 1);
268 m_moduleVersion |= revision.toInt() & 0xffff;
269 }
270 }
271 }
272}
sfalken@apple.com206de2b2008-11-05 18:23:10 +0000273#endif
hausmann@webkit.org49f97662008-11-05 15:31:10 +0000274
ariya@webkit.org32c43562009-06-08 12:05:46 +0000275#if ENABLE(NETSCAPE_PLUGIN_API)
zecke@webkit.org05363c92009-05-25 03:28:41 +0000276void PluginPackage::initializeBrowserFuncs()
277{
278 memset(&m_browserFuncs, 0, sizeof(m_browserFuncs));
kenneth@webkit.org49395932009-07-28 20:29:19 +0000279 m_browserFuncs.size = sizeof(m_browserFuncs);
xan@webkit.orgb77d55e2010-02-18 08:06:03 +0000280 m_browserFuncs.version = NPVersion();
zecke@webkit.org05363c92009-05-25 03:28:41 +0000281
282 m_browserFuncs.geturl = NPN_GetURL;
283 m_browserFuncs.posturl = NPN_PostURL;
284 m_browserFuncs.requestread = NPN_RequestRead;
285 m_browserFuncs.newstream = NPN_NewStream;
286 m_browserFuncs.write = NPN_Write;
287 m_browserFuncs.destroystream = NPN_DestroyStream;
288 m_browserFuncs.status = NPN_Status;
289 m_browserFuncs.uagent = NPN_UserAgent;
290 m_browserFuncs.memalloc = NPN_MemAlloc;
291 m_browserFuncs.memfree = NPN_MemFree;
292 m_browserFuncs.memflush = NPN_MemFlush;
293 m_browserFuncs.reloadplugins = NPN_ReloadPlugins;
294 m_browserFuncs.geturlnotify = NPN_GetURLNotify;
295 m_browserFuncs.posturlnotify = NPN_PostURLNotify;
296 m_browserFuncs.getvalue = NPN_GetValue;
297 m_browserFuncs.setvalue = NPN_SetValue;
298 m_browserFuncs.invalidaterect = NPN_InvalidateRect;
299 m_browserFuncs.invalidateregion = NPN_InvalidateRegion;
300 m_browserFuncs.forceredraw = NPN_ForceRedraw;
301 m_browserFuncs.getJavaEnv = NPN_GetJavaEnv;
302 m_browserFuncs.getJavaPeer = NPN_GetJavaPeer;
303 m_browserFuncs.pushpopupsenabledstate = NPN_PushPopupsEnabledState;
304 m_browserFuncs.poppopupsenabledstate = NPN_PopPopupsEnabledState;
305 m_browserFuncs.pluginthreadasynccall = NPN_PluginThreadAsyncCall;
306
307 m_browserFuncs.releasevariantvalue = _NPN_ReleaseVariantValue;
308 m_browserFuncs.getstringidentifier = _NPN_GetStringIdentifier;
309 m_browserFuncs.getstringidentifiers = _NPN_GetStringIdentifiers;
310 m_browserFuncs.getintidentifier = _NPN_GetIntIdentifier;
311 m_browserFuncs.identifierisstring = _NPN_IdentifierIsString;
312 m_browserFuncs.utf8fromidentifier = _NPN_UTF8FromIdentifier;
313 m_browserFuncs.intfromidentifier = _NPN_IntFromIdentifier;
314 m_browserFuncs.createobject = _NPN_CreateObject;
315 m_browserFuncs.retainobject = _NPN_RetainObject;
316 m_browserFuncs.releaseobject = _NPN_ReleaseObject;
317 m_browserFuncs.invoke = _NPN_Invoke;
318 m_browserFuncs.invokeDefault = _NPN_InvokeDefault;
319 m_browserFuncs.evaluate = _NPN_Evaluate;
320 m_browserFuncs.getproperty = _NPN_GetProperty;
321 m_browserFuncs.setproperty = _NPN_SetProperty;
322 m_browserFuncs.removeproperty = _NPN_RemoveProperty;
323 m_browserFuncs.hasproperty = _NPN_HasProperty;
324 m_browserFuncs.hasmethod = _NPN_HasMethod;
325 m_browserFuncs.setexception = _NPN_SetException;
326 m_browserFuncs.enumerate = _NPN_Enumerate;
327 m_browserFuncs.construct = _NPN_Construct;
commit-queue@webkit.org4d93a972010-10-14 22:16:34 +0000328 m_browserFuncs.getvalueforurl = NPN_GetValueForURL;
329 m_browserFuncs.setvalueforurl = NPN_SetValueForURL;
330 m_browserFuncs.getauthenticationinfo = NPN_GetAuthenticationInfo;
zecke@webkit.org05363c92009-05-25 03:28:41 +0000331}
ariya@webkit.org32c43562009-06-08 12:05:46 +0000332#endif
zecke@webkit.org05363c92009-05-25 03:28:41 +0000333
zecke@webkit.org7d1101b2009-05-25 03:29:06 +0000334#if ENABLE(PLUGIN_PACKAGE_SIMPLE_HASH)
335unsigned PluginPackage::hash() const
336{
alexis.menard@openbossa.org6a598cf2011-05-10 15:25:02 +0000337 struct HashCodes {
338 unsigned hash;
339 time_t modifiedDate;
340 } hashCodes;
zecke@webkit.org7d1101b2009-05-25 03:29:06 +0000341
alexis.menard@openbossa.org6a598cf2011-05-10 15:25:02 +0000342 hashCodes.hash = m_path.impl()->hash();
343 hashCodes.modifiedDate = m_lastModified;
344
345 return StringHasher::hashMemory<sizeof(hashCodes)>(&hashCodes);
zecke@webkit.org7d1101b2009-05-25 03:29:06 +0000346}
347
348bool PluginPackage::equal(const PluginPackage& a, const PluginPackage& b)
349{
350 return a.m_description == b.m_description;
351}
eric@webkit.orge0fc95d2009-10-27 10:17:36 +0000352#endif
zecke@webkit.org7d1101b2009-05-25 03:29:06 +0000353
354int PluginPackage::compareFileVersion(const PlatformModuleVersion& compareVersion) const
355{
356 // return -1, 0, or 1 if plug-in version is less than, equal to, or greater than
357 // the passed version
eric@webkit.orge0fc95d2009-10-27 10:17:36 +0000358
mjs@apple.comacbcc282010-01-05 08:58:28 +0000359#if OS(WINDOWS)
eric@webkit.orge0fc95d2009-10-27 10:17:36 +0000360 if (m_moduleVersion.mostSig != compareVersion.mostSig)
361 return m_moduleVersion.mostSig > compareVersion.mostSig ? 1 : -1;
362 if (m_moduleVersion.leastSig != compareVersion.leastSig)
363 return m_moduleVersion.leastSig > compareVersion.leastSig ? 1 : -1;
364#else
zecke@webkit.org7d1101b2009-05-25 03:29:06 +0000365 if (m_moduleVersion != compareVersion)
366 return m_moduleVersion > compareVersion ? 1 : -1;
eric@webkit.orge0fc95d2009-10-27 10:17:36 +0000367#endif
368
zecke@webkit.org7d1101b2009-05-25 03:29:06 +0000369 return 0;
370}
zecke@webkit.org7d1101b2009-05-25 03:29:06 +0000371
commit-queue@webkit.org67b334a2010-08-28 04:32:17 +0000372#if ENABLE(NETSCAPE_PLUGIN_METADATA_CACHE)
373bool PluginPackage::ensurePluginLoaded()
374{
375 if (!m_infoIsFromCache)
376 return m_isLoaded;
377
378 m_quirks = PluginQuirkSet();
379 m_name = String();
380 m_description = String();
381 m_fullMIMEDescription = String();
382 m_moduleVersion = 0;
383
384 return fetchInfo();
385}
386#endif
387
pewtermoose@webkit.orgfa3bc3f2008-02-28 20:46:37 +0000388}