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
28#include "config.h"
29#include "PluginPackage.h"
30
31#include "MIMETypeRegistry.h"
32#include "PluginDatabase.h"
33#include "PluginDebug.h"
34#include "Timer.h"
35#include "npruntime_impl.h"
36#include <string.h>
37#include <wtf/OwnArrayPtr.h>
38#include <wtf/text/CString.h>
39
40namespace WebCore {
41
42PluginPackage::~PluginPackage()
43{
44    // 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.
50    if (!m_loadCount)
51        unloadWithoutShutdown();
52    else
53        unload();
54
55    ASSERT(!m_isLoaded);
56}
57
58void PluginPackage::freeLibrarySoon()
59{
60    ASSERT(!m_freeLibraryTimer.isActive());
61    ASSERT(m_module);
62    ASSERT(!m_loadCount);
63
64    m_freeLibraryTimer.startOneShot(0);
65}
66
67void PluginPackage::freeLibraryTimerFired(Timer<PluginPackage>*)
68{
69    ASSERT(m_module);
70    ASSERT(!m_loadCount);
71
72    unloadModule(m_module);
73    m_module = 0;
74}
75
76
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
95    diff = compareFileVersion(compareTo.version());
96    if (diff)
97        return diff;
98
99    return strcmp(parentDirectory().utf8().data(), compareTo.parentDirectory().utf8().data());
100}
101
102PluginPackage::PluginPackage(const String& path, const time_t& lastModified)
103    : m_isEnabled(true)
104    , m_isLoaded(false)
105    , m_loadCount(0)
106    , m_path(path)
107    , m_moduleVersion(0)
108    , m_module(0)
109    , m_lastModified(lastModified)
110    , m_freeLibraryTimer(this, &PluginPackage::freeLibraryTimerFired)
111#if ENABLE(NETSCAPE_PLUGIN_METADATA_CACHE)
112    , m_infoIsFromCache(true)
113#endif
114{
115    m_fileName = pathGetFileName(m_path);
116    m_parentDirectory = m_path.left(m_path.length() - m_fileName.length() - 1);
117}
118
119#if !OS(SYMBIAN)
120void PluginPackage::unload()
121{
122    if (!m_isLoaded)
123        return;
124
125    if (--m_loadCount > 0)
126        return;
127
128    m_NPP_Shutdown();
129
130    unloadWithoutShutdown();
131}
132#endif // !OS(SYMBIAN)
133
134void PluginPackage::unloadWithoutShutdown()
135{
136    if (!m_isLoaded)
137        return;
138
139    ASSERT(!m_loadCount);
140    ASSERT(m_module);
141
142    // <rdar://5530519>: Crash when closing tab with pdf file (Reader 7 only)
143    // If the plugin has subclassed its parent window, as with Reader 7, we may have
144    // gotten here by way of the plugin's internal window proc forwarding a message to our
145    // original window proc. If we free the plugin library from here, we will jump back
146    // to code we just freed when we return, so delay calling FreeLibrary at least until
147    // the next message loop
148    freeLibrarySoon();
149
150    m_isLoaded = false;
151}
152
153void PluginPackage::setEnabled(bool enabled)
154{
155    m_isEnabled = enabled;
156}
157
158PassRefPtr<PluginPackage> PluginPackage::createPackage(const String& path, const time_t& lastModified)
159{
160    RefPtr<PluginPackage> package = adoptRef(new PluginPackage(path, lastModified));
161
162    if (!package->fetchInfo())
163        return 0;
164
165    return package.release();
166}
167
168#if ENABLE(NETSCAPE_PLUGIN_METADATA_CACHE)
169PassRefPtr<PluginPackage> PluginPackage::createPackageFromCache(const String& path, const time_t& lastModified, const String& name, const String& description, const String& mimeDescription)
170{
171    RefPtr<PluginPackage> package = adoptRef(new PluginPackage(path, lastModified));
172    package->m_name = name;
173    package->m_description = description;
174    package->determineModuleVersionFromDescription();
175    package->setMIMEDescription(mimeDescription);
176    package->m_infoIsFromCache = true;
177    return package.release();
178}
179#endif
180
181#if defined(XP_UNIX)
182void PluginPackage::determineQuirks(const String& mimeType)
183{
184    if (MIMETypeRegistry::isJavaAppletMIMEType(mimeType)) {
185        // Because a single process cannot create multiple VMs, and we cannot reliably unload a
186        // Java VM, we cannot unload the Java plugin, or we'll lose reference to our only VM
187        m_quirks.add(PluginQuirkDontUnloadPlugin);
188
189        // Setting the window region to an empty region causes bad scrolling repaint problems
190        // with the Java plug-in.
191        m_quirks.add(PluginQuirkDontClipToZeroRectWhenScrolling);
192        return;
193    }
194
195    if (mimeType == "application/x-shockwave-flash") {
196        static const PlatformModuleVersion flashTenVersion(0x0a000000);
197
198        if (compareFileVersion(flashTenVersion) >= 0) {
199            // Flash 10.0 b218 doesn't like having a NULL window handle
200            m_quirks.add(PluginQuirkDontSetNullWindowHandleOnDestroy);
201#if PLATFORM(QT)
202            m_quirks.add(PluginQuirkRequiresGtkToolKit);
203#endif
204        } else {
205            // Flash 9 and older requests windowless plugins if we return a mozilla user agent
206            m_quirks.add(PluginQuirkWantsMozillaUserAgent);
207        }
208
209#if PLATFORM(QT)
210        // Flash will crash on repeated calls to SetWindow in windowed mode
211        m_quirks.add(PluginQuirkDontCallSetWindowMoreThanOnce);
212
213#if CPU(X86_64)
214        // 64-bit Flash freezes if right-click is sent in windowless mode
215        m_quirks.add(PluginQuirkIgnoreRightClickInWindowlessMode);
216#endif
217#endif
218
219        m_quirks.add(PluginQuirkRequiresDefaultScreenDepth);
220        m_quirks.add(PluginQuirkThrottleInvalidate);
221        m_quirks.add(PluginQuirkThrottleWMUserPlusOneMessages);
222        m_quirks.add(PluginQuirkFlashURLNotifyBug);
223    }
224
225#if defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6)
226    // Passing a 32-bit depth pixmap to NPAPI plugins is too inefficient. Instead, pass a X Pixmap
227    // that has same depth as the screen depth since graphics operations are optimized
228    // for this depth.
229    m_quirks.add(PluginQuirkRequiresDefaultScreenDepth);
230#endif
231}
232#endif
233
234#if !OS(WINDOWS)
235void PluginPackage::determineModuleVersionFromDescription()
236{
237    // It's a bit lame to detect the plugin version by parsing it
238    // from the plugin description string, but it doesn't seem that
239    // version information is available in any standardized way at
240    // the module level, like in Windows
241
242    if (m_description.isEmpty())
243        return;
244
245    if (m_description.startsWith("Shockwave Flash") && m_description.length() >= 19) {
246        // The flash version as a PlatformModuleVersion differs on Unix from Windows
247        // since the revision can be larger than a 8 bits, so we allow it 16 here and
248        // push the major/minor up 8 bits. Thus on Unix, Flash's version may be
249        // 0x0a000000 instead of 0x000a0000.
250
251        Vector<String> versionParts;
252        m_description.substring(16).split(' ', /*allowEmptyEntries =*/ false, versionParts);
253        if (versionParts.isEmpty())
254            return;
255
256        if (versionParts.size() >= 1) {
257            Vector<String> majorMinorParts;
258            versionParts[0].split('.', majorMinorParts);
259            if (majorMinorParts.size() >= 1) {
260                bool converted = false;
261                unsigned major = majorMinorParts[0].toUInt(&converted);
262                if (converted)
263                    m_moduleVersion = (major & 0xff) << 24;
264            }
265            if (majorMinorParts.size() == 2) {
266                bool converted = false;
267                unsigned minor = majorMinorParts[1].toUInt(&converted);
268                if (converted)
269                    m_moduleVersion |= (minor & 0xff) << 16;
270            }
271        }
272
273        if (versionParts.size() >= 2) {
274            String revision = versionParts[1];
275            if (revision.length() > 1 && (revision[0] == 'r' || revision[0] == 'b')) {
276                revision.remove(0, 1);
277                m_moduleVersion |= revision.toInt() & 0xffff;
278            }
279        }
280    }
281}
282#endif
283
284#if ENABLE(NETSCAPE_PLUGIN_API)
285void PluginPackage::initializeBrowserFuncs()
286{
287    memset(&m_browserFuncs, 0, sizeof(m_browserFuncs));
288    m_browserFuncs.size = sizeof(m_browserFuncs);
289    m_browserFuncs.version = NPVersion();
290
291    m_browserFuncs.geturl = NPN_GetURL;
292    m_browserFuncs.posturl = NPN_PostURL;
293    m_browserFuncs.requestread = NPN_RequestRead;
294    m_browserFuncs.newstream = NPN_NewStream;
295    m_browserFuncs.write = NPN_Write;
296    m_browserFuncs.destroystream = NPN_DestroyStream;
297    m_browserFuncs.status = NPN_Status;
298    m_browserFuncs.uagent = NPN_UserAgent;
299    m_browserFuncs.memalloc = NPN_MemAlloc;
300    m_browserFuncs.memfree = NPN_MemFree;
301    m_browserFuncs.memflush = NPN_MemFlush;
302    m_browserFuncs.reloadplugins = NPN_ReloadPlugins;
303    m_browserFuncs.geturlnotify = NPN_GetURLNotify;
304    m_browserFuncs.posturlnotify = NPN_PostURLNotify;
305    m_browserFuncs.getvalue = NPN_GetValue;
306    m_browserFuncs.setvalue = NPN_SetValue;
307    m_browserFuncs.invalidaterect = NPN_InvalidateRect;
308    m_browserFuncs.invalidateregion = NPN_InvalidateRegion;
309    m_browserFuncs.forceredraw = NPN_ForceRedraw;
310    m_browserFuncs.getJavaEnv = NPN_GetJavaEnv;
311    m_browserFuncs.getJavaPeer = NPN_GetJavaPeer;
312    m_browserFuncs.pushpopupsenabledstate = NPN_PushPopupsEnabledState;
313    m_browserFuncs.poppopupsenabledstate = NPN_PopPopupsEnabledState;
314    m_browserFuncs.pluginthreadasynccall = NPN_PluginThreadAsyncCall;
315
316    m_browserFuncs.releasevariantvalue = _NPN_ReleaseVariantValue;
317    m_browserFuncs.getstringidentifier = _NPN_GetStringIdentifier;
318    m_browserFuncs.getstringidentifiers = _NPN_GetStringIdentifiers;
319    m_browserFuncs.getintidentifier = _NPN_GetIntIdentifier;
320    m_browserFuncs.identifierisstring = _NPN_IdentifierIsString;
321    m_browserFuncs.utf8fromidentifier = _NPN_UTF8FromIdentifier;
322    m_browserFuncs.intfromidentifier = _NPN_IntFromIdentifier;
323    m_browserFuncs.createobject = _NPN_CreateObject;
324    m_browserFuncs.retainobject = _NPN_RetainObject;
325    m_browserFuncs.releaseobject = _NPN_ReleaseObject;
326    m_browserFuncs.invoke = _NPN_Invoke;
327    m_browserFuncs.invokeDefault = _NPN_InvokeDefault;
328    m_browserFuncs.evaluate = _NPN_Evaluate;
329    m_browserFuncs.getproperty = _NPN_GetProperty;
330    m_browserFuncs.setproperty = _NPN_SetProperty;
331    m_browserFuncs.removeproperty = _NPN_RemoveProperty;
332    m_browserFuncs.hasproperty = _NPN_HasProperty;
333    m_browserFuncs.hasmethod = _NPN_HasMethod;
334    m_browserFuncs.setexception = _NPN_SetException;
335    m_browserFuncs.enumerate = _NPN_Enumerate;
336    m_browserFuncs.construct = _NPN_Construct;
337    m_browserFuncs.getvalueforurl = NPN_GetValueForURL;
338    m_browserFuncs.setvalueforurl = NPN_SetValueForURL;
339    m_browserFuncs.getauthenticationinfo = NPN_GetAuthenticationInfo;
340}
341#endif
342
343#if ENABLE(PLUGIN_PACKAGE_SIMPLE_HASH)
344unsigned PluginPackage::hash() const
345{
346    unsigned hashCodes[] = {
347        m_path.impl()->hash(),
348        m_lastModified
349    };
350
351    return StringHasher::hashMemory<sizeof(hashCodes)>(hashCodes);
352}
353
354bool PluginPackage::equal(const PluginPackage& a, const PluginPackage& b)
355{
356    return a.m_description == b.m_description;
357}
358#endif
359
360int PluginPackage::compareFileVersion(const PlatformModuleVersion& compareVersion) const
361{
362    // return -1, 0, or 1 if plug-in version is less than, equal to, or greater than
363    // the passed version
364
365#if OS(WINDOWS)
366    if (m_moduleVersion.mostSig != compareVersion.mostSig)
367        return m_moduleVersion.mostSig > compareVersion.mostSig ? 1 : -1;
368    if (m_moduleVersion.leastSig != compareVersion.leastSig)
369        return m_moduleVersion.leastSig > compareVersion.leastSig ? 1 : -1;
370#else
371    if (m_moduleVersion != compareVersion)
372        return m_moduleVersion > compareVersion ? 1 : -1;
373#endif
374
375    return 0;
376}
377
378#if ENABLE(NETSCAPE_PLUGIN_METADATA_CACHE)
379bool PluginPackage::ensurePluginLoaded()
380{
381    if (!m_infoIsFromCache)
382        return m_isLoaded;
383
384    m_quirks = PluginQuirkSet();
385    m_name = String();
386    m_description = String();
387    m_fullMIMEDescription = String();
388    m_moduleVersion = 0;
389
390    return fetchInfo();
391}
392#endif
393
394}
395