tizen beta release
[profile/ivi/webkit-efl.git] / Source / WebCore / plugins / PluginPackage.cpp
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
40 namespace WebCore {
41
42 PluginPackage::~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
58 void PluginPackage::freeLibrarySoon()
59 {
60     ASSERT(!m_freeLibraryTimer.isActive());
61     ASSERT(m_module);
62     ASSERT(!m_loadCount);
63
64     m_freeLibraryTimer.startOneShot(0);
65 }
66
67 void 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
77 int 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
102 PluginPackage::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 void 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
132 void PluginPackage::unloadWithoutShutdown()
133 {
134     if (!m_isLoaded)
135         return;
136
137     ASSERT(!m_loadCount);
138     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
151 void PluginPackage::setEnabled(bool enabled)
152 {
153     m_isEnabled = enabled;
154 }
155
156 PassRefPtr<PluginPackage> PluginPackage::createPackage(const String& path, const time_t& lastModified)
157 {
158     RefPtr<PluginPackage> package = adoptRef(new PluginPackage(path, lastModified));
159
160     if (!package->fetchInfo())
161         return 0;
162
163     return package.release();
164 }
165
166 #if ENABLE(NETSCAPE_PLUGIN_METADATA_CACHE)
167 PassRefPtr<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
179 #if defined(XP_UNIX)
180 void 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);
199 #if PLATFORM(QT)
200             m_quirks.add(PluginQuirkRequiresGtkToolKit);
201 #endif
202         } else {
203             // Flash 9 and older requests windowless plugins if we return a mozilla user agent
204             m_quirks.add(PluginQuirkWantsMozillaUserAgent);
205         }
206
207 #if PLATFORM(QT)
208         // Flash will crash on repeated calls to SetWindow in windowed mode
209         m_quirks.add(PluginQuirkDontCallSetWindowMoreThanOnce);
210 #endif
211
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
216
217         m_quirks.add(PluginQuirkRequiresDefaultScreenDepth);
218         m_quirks.add(PluginQuirkThrottleInvalidate);
219         m_quirks.add(PluginQuirkThrottleWMUserPlusOneMessages);
220         m_quirks.add(PluginQuirkFlashURLNotifyBug);
221     }
222 }
223 #endif
224
225 #if !OS(WINDOWS)
226 void 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 }
273 #endif
274
275 #if ENABLE(NETSCAPE_PLUGIN_API)
276 void PluginPackage::initializeBrowserFuncs()
277 {
278     memset(&m_browserFuncs, 0, sizeof(m_browserFuncs));
279     m_browserFuncs.size = sizeof(m_browserFuncs);
280     m_browserFuncs.version = NPVersion();
281
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;
328     m_browserFuncs.getvalueforurl = NPN_GetValueForURL;
329     m_browserFuncs.setvalueforurl = NPN_SetValueForURL;
330     m_browserFuncs.getauthenticationinfo = NPN_GetAuthenticationInfo;
331 }
332 #endif
333
334 #if ENABLE(PLUGIN_PACKAGE_SIMPLE_HASH)
335 unsigned PluginPackage::hash() const
336 {
337     struct HashCodes {
338         unsigned hash;
339         time_t modifiedDate;
340     } hashCodes;
341
342     hashCodes.hash = m_path.impl()->hash();
343     hashCodes.modifiedDate = m_lastModified;
344
345     return StringHasher::hashMemory<sizeof(hashCodes)>(&hashCodes);
346 }
347
348 bool PluginPackage::equal(const PluginPackage& a, const PluginPackage& b)
349 {
350     return a.m_description == b.m_description;
351 }
352 #endif
353
354 int 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
358
359 #if OS(WINDOWS)
360     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    
365     if (m_moduleVersion != compareVersion)
366         return m_moduleVersion > compareVersion ? 1 : -1;
367 #endif
368
369     return 0;
370 }
371
372 #if ENABLE(NETSCAPE_PLUGIN_METADATA_CACHE)
373 bool 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
388 }