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
unload()122 void PluginPackage::unload()
123 {
124 if (!m_isLoaded)
125 return;
126
127 if (--m_loadCount > 0)
128 return;
129
130 m_NPP_Shutdown();
131
132 unloadWithoutShutdown();
133 }
134
unloadWithoutShutdown()135 void PluginPackage::unloadWithoutShutdown()
136 {
137 if (!m_isLoaded)
138 return;
139
140 ASSERT(!m_loadCount);
141 ASSERT(m_module);
142
143 // <rdar://5530519>: Crash when closing tab with pdf file (Reader 7 only)
144 // If the plugin has subclassed its parent window, as with Reader 7, we may have
145 // gotten here by way of the plugin's internal window proc forwarding a message to our
146 // original window proc. If we free the plugin library from here, we will jump back
147 // to code we just freed when we return, so delay calling FreeLibrary at least until
148 // the next message loop
149 freeLibrarySoon();
150
151 m_isLoaded = false;
152 }
153
setEnabled(bool enabled)154 void PluginPackage::setEnabled(bool enabled)
155 {
156 m_isEnabled = enabled;
157 }
158
createPackage(const String & path,const time_t & lastModified)159 PassRefPtr<PluginPackage> PluginPackage::createPackage(const String& path, const time_t& lastModified)
160 {
161 RefPtr<PluginPackage> package = adoptRef(new PluginPackage(path, lastModified));
162
163 if (!package->fetchInfo())
164 return 0;
165
166 return package.release();
167 }
168
169 #if defined(XP_UNIX)
determineQuirks(const String & mimeType)170 void PluginPackage::determineQuirks(const String& mimeType)
171 {
172 if (MIMETypeRegistry::isJavaAppletMIMEType(mimeType)) {
173 // Because a single process cannot create multiple VMs, and we cannot reliably unload a
174 // Java VM, we cannot unload the Java plugin, or we'll lose reference to our only VM
175 m_quirks.add(PluginQuirkDontUnloadPlugin);
176
177 // Setting the window region to an empty region causes bad scrolling repaint problems
178 // with the Java plug-in.
179 m_quirks.add(PluginQuirkDontClipToZeroRectWhenScrolling);
180 return;
181 }
182
183 if (mimeType == "application/x-shockwave-flash") {
184 static const PlatformModuleVersion flashTenVersion(0x0a000000);
185
186 if (compareFileVersion(flashTenVersion) >= 0) {
187 // Flash 10.0 b218 doesn't like having a NULL window handle
188 m_quirks.add(PluginQuirkDontSetNullWindowHandleOnDestroy);
189 #if PLATFORM(QT)
190 m_quirks.add(PluginQuirkRequiresGtkToolKit);
191 #endif
192 } else {
193 // Flash 9 and older requests windowless plugins if we return a mozilla user agent
194 m_quirks.add(PluginQuirkWantsMozillaUserAgent);
195 }
196
197 m_quirks.add(PluginQuirkThrottleInvalidate);
198 m_quirks.add(PluginQuirkThrottleWMUserPlusOneMessages);
199 m_quirks.add(PluginQuirkFlashURLNotifyBug);
200 }
201 }
202 #endif
203
204 #if !PLATFORM(WIN_OS)
determineModuleVersionFromDescription()205 void PluginPackage::determineModuleVersionFromDescription()
206 {
207 // It's a bit lame to detect the plugin version by parsing it
208 // from the plugin description string, but it doesn't seem that
209 // version information is available in any standardized way at
210 // the module level, like in Windows
211
212 if (m_description.isEmpty())
213 return;
214
215 if (m_description.startsWith("Shockwave Flash") && m_description.length() >= 19) {
216 // The flash version as a PlatformModuleVersion differs on Unix from Windows
217 // since the revision can be larger than a 8 bits, so we allow it 16 here and
218 // push the major/minor up 8 bits. Thus on Unix, Flash's version may be
219 // 0x0a000000 instead of 0x000a0000.
220
221 Vector<String> versionParts;
222 m_description.substring(16).split(' ', /*allowEmptyEntries =*/ false, versionParts);
223 if (versionParts.isEmpty())
224 return;
225
226 if (versionParts.size() >= 1) {
227 Vector<String> majorMinorParts;
228 versionParts[0].split('.', majorMinorParts);
229 if (majorMinorParts.size() >= 1) {
230 bool converted = false;
231 unsigned major = majorMinorParts[0].toUInt(&converted);
232 if (converted)
233 m_moduleVersion = (major & 0xff) << 24;
234 }
235 if (majorMinorParts.size() == 2) {
236 bool converted = false;
237 unsigned minor = majorMinorParts[1].toUInt(&converted);
238 if (converted)
239 m_moduleVersion |= (minor & 0xff) << 16;
240 }
241 }
242
243 if (versionParts.size() >= 2) {
244 String revision = versionParts[1];
245 if (revision.length() > 1 && (revision[0] == 'r' || revision[0] == 'b')) {
246 revision.remove(0, 1);
247 m_moduleVersion |= revision.toInt() & 0xffff;
248 }
249 }
250 }
251 }
252 #endif
253
254 #if ENABLE(NETSCAPE_PLUGIN_API)
initializeBrowserFuncs()255 void PluginPackage::initializeBrowserFuncs()
256 {
257 memset(&m_browserFuncs, 0, sizeof(m_browserFuncs));
258 m_browserFuncs.size = sizeof(m_browserFuncs);
259 m_browserFuncs.version = NP_VERSION_MINOR;
260
261 m_browserFuncs.geturl = NPN_GetURL;
262 m_browserFuncs.posturl = NPN_PostURL;
263 m_browserFuncs.requestread = NPN_RequestRead;
264 m_browserFuncs.newstream = NPN_NewStream;
265 m_browserFuncs.write = NPN_Write;
266 m_browserFuncs.destroystream = NPN_DestroyStream;
267 m_browserFuncs.status = NPN_Status;
268 m_browserFuncs.uagent = NPN_UserAgent;
269 m_browserFuncs.memalloc = NPN_MemAlloc;
270 m_browserFuncs.memfree = NPN_MemFree;
271 m_browserFuncs.memflush = NPN_MemFlush;
272 m_browserFuncs.reloadplugins = NPN_ReloadPlugins;
273 m_browserFuncs.geturlnotify = NPN_GetURLNotify;
274 m_browserFuncs.posturlnotify = NPN_PostURLNotify;
275 m_browserFuncs.getvalue = NPN_GetValue;
276 m_browserFuncs.setvalue = NPN_SetValue;
277 m_browserFuncs.invalidaterect = NPN_InvalidateRect;
278 m_browserFuncs.invalidateregion = NPN_InvalidateRegion;
279 m_browserFuncs.forceredraw = NPN_ForceRedraw;
280 m_browserFuncs.getJavaEnv = NPN_GetJavaEnv;
281 m_browserFuncs.getJavaPeer = NPN_GetJavaPeer;
282 m_browserFuncs.pushpopupsenabledstate = NPN_PushPopupsEnabledState;
283 m_browserFuncs.poppopupsenabledstate = NPN_PopPopupsEnabledState;
284 m_browserFuncs.pluginthreadasynccall = NPN_PluginThreadAsyncCall;
285
286 m_browserFuncs.releasevariantvalue = _NPN_ReleaseVariantValue;
287 m_browserFuncs.getstringidentifier = _NPN_GetStringIdentifier;
288 m_browserFuncs.getstringidentifiers = _NPN_GetStringIdentifiers;
289 m_browserFuncs.getintidentifier = _NPN_GetIntIdentifier;
290 m_browserFuncs.identifierisstring = _NPN_IdentifierIsString;
291 m_browserFuncs.utf8fromidentifier = _NPN_UTF8FromIdentifier;
292 m_browserFuncs.intfromidentifier = _NPN_IntFromIdentifier;
293 m_browserFuncs.createobject = _NPN_CreateObject;
294 m_browserFuncs.retainobject = _NPN_RetainObject;
295 m_browserFuncs.releaseobject = _NPN_ReleaseObject;
296 m_browserFuncs.invoke = _NPN_Invoke;
297 m_browserFuncs.invokeDefault = _NPN_InvokeDefault;
298 m_browserFuncs.evaluate = _NPN_Evaluate;
299 m_browserFuncs.getproperty = _NPN_GetProperty;
300 m_browserFuncs.setproperty = _NPN_SetProperty;
301 m_browserFuncs.removeproperty = _NPN_RemoveProperty;
302 m_browserFuncs.hasproperty = _NPN_HasProperty;
303 m_browserFuncs.hasmethod = _NPN_HasMethod;
304 m_browserFuncs.setexception = _NPN_SetException;
305 m_browserFuncs.enumerate = _NPN_Enumerate;
306 m_browserFuncs.construct = _NPN_Construct;
307 }
308 #endif
309
310 #if ENABLE(PLUGIN_PACKAGE_SIMPLE_HASH)
hash() const311 unsigned PluginPackage::hash() const
312 {
313 unsigned hashCodes[] = {
314 m_path.impl()->hash(),
315 m_lastModified
316 };
317
318 return StringImpl::computeHash(reinterpret_cast<UChar*>(hashCodes), sizeof(hashCodes) / sizeof(UChar));
319 }
320
equal(const PluginPackage & a,const PluginPackage & b)321 bool PluginPackage::equal(const PluginPackage& a, const PluginPackage& b)
322 {
323 return a.m_description == b.m_description;
324 }
325
compareFileVersion(const PlatformModuleVersion & compareVersion) const326 int PluginPackage::compareFileVersion(const PlatformModuleVersion& compareVersion) const
327 {
328 // return -1, 0, or 1 if plug-in version is less than, equal to, or greater than
329 // the passed version
330 if (m_moduleVersion != compareVersion)
331 return m_moduleVersion > compareVersion ? 1 : -1;
332 return 0;
333 }
334 #endif
335
336 }
337