2 * Copyright (C) 2006, 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 2008 Collabora Ltd. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "PluginPackage.h"
31 #include "MIMETypeRegistry.h"
32 #include "PluginDatabase.h"
33 #include "PluginDebug.h"
35 #include "npruntime_impl.h"
37 #include <wtf/OwnArrayPtr.h>
41 PluginPackage::~PluginPackage()
43 // This destructor gets called during refresh() if PluginDatabase's
44 // PluginSet hash is already populated, as it removes items from
45 // the hash table. Calling the destructor on a loaded plug-in of
46 // course would cause a crash, so we check to call unload before we
48 // FIXME: There is probably a better way to fix this.
50 unloadWithoutShutdown();
57 void PluginPackage::freeLibrarySoon()
59 ASSERT(!m_freeLibraryTimer.isActive());
61 ASSERT(m_loadCount == 0);
63 m_freeLibraryTimer.startOneShot(0);
66 void PluginPackage::freeLibraryTimerFired(Timer<PluginPackage>*)
69 ASSERT(m_loadCount == 0);
71 unloadModule(m_module);
76 int PluginPackage::compare(const PluginPackage& compareTo) const
78 // Sort plug-ins that allow multiple instances first.
79 bool AallowsMultipleInstances = !quirks().contains(PluginQuirkDontAllowMultipleInstances);
80 bool BallowsMultipleInstances = !compareTo.quirks().contains(PluginQuirkDontAllowMultipleInstances);
81 if (AallowsMultipleInstances != BallowsMultipleInstances)
82 return AallowsMultipleInstances ? -1 : 1;
84 // Sort plug-ins in a preferred path first.
85 bool AisInPreferredDirectory = PluginDatabase::isPreferredPluginDirectory(parentDirectory());
86 bool BisInPreferredDirectory = PluginDatabase::isPreferredPluginDirectory(compareTo.parentDirectory());
87 if (AisInPreferredDirectory != BisInPreferredDirectory)
88 return AisInPreferredDirectory ? -1 : 1;
90 int diff = strcmp(name().utf8().data(), compareTo.name().utf8().data());
94 if (diff = compareFileVersion(compareTo.version()))
97 return strcmp(parentDirectory().utf8().data(), compareTo.parentDirectory().utf8().data());
100 PluginPackage::PluginPackage(const String& path, const time_t& lastModified)
106 , m_lastModified(lastModified)
107 , m_freeLibraryTimer(this, &PluginPackage::freeLibraryTimerFired)
109 m_fileName = pathGetFileName(m_path);
110 m_parentDirectory = m_path.left(m_path.length() - m_fileName.length() - 1);
113 void PluginPackage::unload()
118 if (--m_loadCount > 0)
123 unloadWithoutShutdown();
126 void PluginPackage::unloadWithoutShutdown()
131 ASSERT(m_loadCount == 0);
134 // <rdar://5530519>: Crash when closing tab with pdf file (Reader 7 only)
135 // If the plugin has subclassed its parent window, as with Reader 7, we may have
136 // gotten here by way of the plugin's internal window proc forwarding a message to our
137 // original window proc. If we free the plugin library from here, we will jump back
138 // to code we just freed when we return, so delay calling FreeLibrary at least until
139 // the next message loop
145 PassRefPtr<PluginPackage> PluginPackage::createPackage(const String& path, const time_t& lastModified)
147 RefPtr<PluginPackage> package = adoptRef(new PluginPackage(path, lastModified));
149 if (!package->fetchInfo())
152 return package.release();