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