• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2006, 2007 Apple Inc.  All rights reserved.
3  * Copyright (C) 2008 Collabora, Ltd.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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.
25  */
26 
27 #include "config.h"
28 #include "PluginDatabase.h"
29 
30 #include "Frame.h"
31 #include "KURL.h"
32 #include "PluginPackage.h"
33 #include <stdlib.h>
34 
35 namespace WebCore {
36 
installedPlugins()37 PluginDatabase* PluginDatabase::installedPlugins()
38 {
39     static PluginDatabase* plugins = 0;
40 
41     if (!plugins) {
42         plugins = new PluginDatabase;
43         plugins->setPluginDirectories(PluginDatabase::defaultPluginDirectories());
44         plugins->refresh();
45     }
46 
47     return plugins;
48 }
49 
isMIMETypeRegistered(const String & mimeType)50 bool PluginDatabase::isMIMETypeRegistered(const String& mimeType)
51 {
52     if (mimeType.isNull())
53         return false;
54     if (m_registeredMIMETypes.contains(mimeType))
55         return true;
56     // No plugin was found, try refreshing the database and searching again
57     return (refresh() && m_registeredMIMETypes.contains(mimeType));
58 }
59 
addExtraPluginDirectory(const String & directory)60 void PluginDatabase::addExtraPluginDirectory(const String& directory)
61 {
62     m_pluginDirectories.append(directory);
63     refresh();
64 }
65 
refresh()66 bool PluginDatabase::refresh()
67 {
68     bool pluginSetChanged = false;
69 
70     if (!m_plugins.isEmpty()) {
71         PluginSet pluginsToUnload;
72         getDeletedPlugins(pluginsToUnload);
73 
74         // Unload plugins
75         PluginSet::const_iterator end = pluginsToUnload.end();
76         for (PluginSet::const_iterator it = pluginsToUnload.begin(); it != end; ++it)
77             remove(it->get());
78 
79         pluginSetChanged = !pluginsToUnload.isEmpty();
80     }
81 
82     HashSet<String> paths;
83     getPluginPathsInDirectories(paths);
84 
85     HashMap<String, time_t> pathsWithTimes;
86 
87     // We should only skip unchanged files if we didn't remove any plugins above. If we did remove
88     // any plugins, we need to look at every plugin file so that, e.g., if the user has two versions
89     // of RealPlayer installed and just removed the newer one, we'll pick up the older one.
90     bool shouldSkipUnchangedFiles = !pluginSetChanged;
91 
92     HashSet<String>::const_iterator pathsEnd = paths.end();
93     for (HashSet<String>::const_iterator it = paths.begin(); it != pathsEnd; ++it) {
94         time_t lastModified;
95         if (!getFileModificationTime(*it, lastModified))
96             continue;
97 
98         pathsWithTimes.add(*it, lastModified);
99 
100         // If the path's timestamp hasn't changed since the last time we ran refresh(), we don't have to do anything.
101         if (shouldSkipUnchangedFiles && m_pluginPathsWithTimes.get(*it) == lastModified)
102             continue;
103 
104         if (RefPtr<PluginPackage> oldPackage = m_pluginsByPath.get(*it)) {
105             ASSERT(!shouldSkipUnchangedFiles || oldPackage->lastModified() != lastModified);
106             remove(oldPackage.get());
107         }
108 
109         RefPtr<PluginPackage> package = PluginPackage::createPackage(*it, lastModified);
110         if (package && add(package.release()))
111             pluginSetChanged = true;
112     }
113 
114     // Cache all the paths we found with their timestamps for next time.
115     pathsWithTimes.swap(m_pluginPathsWithTimes);
116 
117     if (!pluginSetChanged)
118         return false;
119 
120     m_registeredMIMETypes.clear();
121 
122     // Register plug-in MIME types
123     PluginSet::const_iterator end = m_plugins.end();
124     for (PluginSet::const_iterator it = m_plugins.begin(); it != end; ++it) {
125         // Get MIME types
126         MIMEToDescriptionsMap::const_iterator map_end = (*it)->mimeToDescriptions().end();
127         for (MIMEToDescriptionsMap::const_iterator map_it = (*it)->mimeToDescriptions().begin(); map_it != map_end; ++map_it) {
128             m_registeredMIMETypes.add(map_it->first);
129         }
130     }
131 
132     return true;
133 }
134 
plugins() const135 Vector<PluginPackage*> PluginDatabase::plugins() const
136 {
137     Vector<PluginPackage*> result;
138 
139     PluginSet::const_iterator end = m_plugins.end();
140     for (PluginSet::const_iterator it = m_plugins.begin(); it != end; ++it)
141         result.append((*it).get());
142 
143     return result;
144 }
145 
preferredPluginCompare(const void * a,const void * b)146 int PluginDatabase::preferredPluginCompare(const void* a, const void* b)
147 {
148     PluginPackage* pluginA = *static_cast<PluginPackage* const*>(a);
149     PluginPackage* pluginB = *static_cast<PluginPackage* const*>(b);
150 
151     return pluginA->compare(*pluginB);
152 }
153 
pluginForMIMEType(const String & mimeType)154 PluginPackage* PluginDatabase::pluginForMIMEType(const String& mimeType)
155 {
156     if (mimeType.isEmpty())
157         return 0;
158 
159     String key = mimeType.lower();
160     PluginSet::const_iterator end = m_plugins.end();
161 
162     Vector<PluginPackage*, 2> pluginChoices;
163 
164     for (PluginSet::const_iterator it = m_plugins.begin(); it != end; ++it) {
165         if ((*it)->mimeToDescriptions().contains(key))
166             pluginChoices.append((*it).get());
167     }
168 
169     if (pluginChoices.isEmpty())
170         return 0;
171 
172     qsort(pluginChoices.data(), pluginChoices.size(), sizeof(PluginPackage*), PluginDatabase::preferredPluginCompare);
173 
174     return pluginChoices[0];
175 }
176 
MIMETypeForExtension(const String & extension) const177 String PluginDatabase::MIMETypeForExtension(const String& extension) const
178 {
179     if (extension.isEmpty())
180         return String();
181 
182     PluginSet::const_iterator end = m_plugins.end();
183     String mimeType;
184     Vector<PluginPackage*, 2> pluginChoices;
185     HashMap<PluginPackage*, String> mimeTypeForPlugin;
186 
187     for (PluginSet::const_iterator it = m_plugins.begin(); it != end; ++it) {
188         MIMEToExtensionsMap::const_iterator mime_end = (*it)->mimeToExtensions().end();
189 
190         for (MIMEToExtensionsMap::const_iterator mime_it = (*it)->mimeToExtensions().begin(); mime_it != mime_end; ++mime_it) {
191             const Vector<String>& extensions = mime_it->second;
192             bool foundMapping = false;
193             for (unsigned i = 0; i < extensions.size(); i++) {
194                 if (equalIgnoringCase(extensions[i], extension)) {
195                     PluginPackage* plugin = (*it).get();
196                     pluginChoices.append(plugin);
197                     mimeTypeForPlugin.add(plugin, mime_it->first);
198                     foundMapping = true;
199                     break;
200                 }
201             }
202             if (foundMapping)
203                 break;
204         }
205     }
206 
207     if (pluginChoices.isEmpty())
208         return String();
209 
210     qsort(pluginChoices.data(), pluginChoices.size(), sizeof(PluginPackage*), PluginDatabase::preferredPluginCompare);
211 
212     return mimeTypeForPlugin.get(pluginChoices[0]);
213 }
214 
findPlugin(const KURL & url,String & mimeType)215 PluginPackage* PluginDatabase::findPlugin(const KURL& url, String& mimeType)
216 {
217     PluginPackage* plugin = pluginForMIMEType(mimeType);
218     String filename = url.string();
219 
220     if (!plugin) {
221         String filename = url.lastPathComponent();
222         if (!filename.endsWith("/")) {
223             int extensionPos = filename.reverseFind('.');
224             if (extensionPos != -1) {
225                 String extension = filename.substring(extensionPos + 1);
226 
227                 mimeType = MIMETypeForExtension(extension);
228                 plugin = pluginForMIMEType(mimeType);
229             }
230         }
231     }
232 
233     // FIXME: if no plugin could be found, query Windows for the mime type
234     // corresponding to the extension.
235 
236     return plugin;
237 }
238 
getDeletedPlugins(PluginSet & plugins) const239 void PluginDatabase::getDeletedPlugins(PluginSet& plugins) const
240 {
241     PluginSet::const_iterator end = m_plugins.end();
242     for (PluginSet::const_iterator it = m_plugins.begin(); it != end; ++it) {
243         if (!fileExists((*it)->path()))
244             plugins.add(*it);
245     }
246 }
247 
add(PassRefPtr<PluginPackage> prpPackage)248 bool PluginDatabase::add(PassRefPtr<PluginPackage> prpPackage)
249 {
250     ASSERT_ARG(prpPackage, prpPackage);
251 
252     RefPtr<PluginPackage> package = prpPackage;
253 
254     if (!m_plugins.add(package).second)
255         return false;
256 
257     m_pluginsByPath.add(package->path(), package);
258     return true;
259 }
260 
remove(PluginPackage * package)261 void PluginDatabase::remove(PluginPackage* package)
262 {
263     m_plugins.remove(package);
264     m_pluginsByPath.remove(package->path());
265 }
266 
267 #if !PLATFORM(WIN_OS) || PLATFORM(WX)
268 // For Safari/Win the following three methods are implemented
269 // in PluginDatabaseWin.cpp, but if we can use WebCore constructs
270 // for the logic we should perhaps move it here under XP_WIN?
271 
defaultPluginDirectories()272 Vector<String> PluginDatabase::defaultPluginDirectories()
273 {
274     Vector<String> paths;
275 
276     // Add paths specific to each platform
277 #if defined(XP_UNIX)
278     String userPluginPath = homeDirectoryPath();
279     userPluginPath.append(String("/.mozilla/plugins"));
280     paths.append(userPluginPath);
281 
282     userPluginPath = homeDirectoryPath();
283     userPluginPath.append(String("/.netscape/plugins"));
284     paths.append(userPluginPath);
285 
286     paths.append("/usr/lib/browser/plugins");
287     paths.append("/usr/local/lib/mozilla/plugins");
288     paths.append("/usr/lib/firefox/plugins");
289     paths.append("/usr/lib64/browser-plugins");
290     paths.append("/usr/lib/browser-plugins");
291     paths.append("/usr/lib/mozilla/plugins");
292     paths.append("/usr/local/netscape/plugins");
293     paths.append("/opt/mozilla/plugins");
294     paths.append("/opt/mozilla/lib/plugins");
295     paths.append("/opt/netscape/plugins");
296     paths.append("/opt/netscape/communicator/plugins");
297     paths.append("/usr/lib/netscape/plugins");
298     paths.append("/usr/lib/netscape/plugins-libc5");
299     paths.append("/usr/lib/netscape/plugins-libc6");
300     paths.append("/usr/lib64/netscape/plugins");
301     paths.append("/usr/lib64/mozilla/plugins");
302 
303     String mozHome(getenv("MOZILLA_HOME"));
304     mozHome.append("/plugins");
305     paths.append(mozHome);
306 
307     Vector<String> mozPaths;
308     String mozPath(getenv("MOZ_PLUGIN_PATH"));
309     mozPath.split(UChar(':'), /* allowEmptyEntries */ false, mozPaths);
310     paths.append(mozPaths);
311 #elif defined(XP_MACOSX)
312     String userPluginPath = homeDirectoryPath();
313     userPluginPath.append(String("/Library/Internet Plug-Ins"));
314     paths.append(userPluginPath);
315     paths.append("/Library/Internet Plug-Ins");
316 #elif defined(XP_WIN)
317     String userPluginPath = homeDirectoryPath();
318     userPluginPath.append(String("\\Application Data\\Mozilla\\plugins"));
319     paths.append(userPluginPath);
320 #endif
321 
322     // Add paths specific to each port
323 #if PLATFORM(QT)
324     Vector<String> qtPaths;
325     String qtPath(getenv("QTWEBKIT_PLUGIN_PATH"));
326     qtPath.split(UChar(':'), /* allowEmptyEntries */ false, qtPaths);
327     paths.append(qtPaths);
328 #endif
329 
330     return paths;
331 }
332 
isPreferredPluginDirectory(const String & path)333 bool PluginDatabase::isPreferredPluginDirectory(const String& path)
334 {
335     String preferredPath = homeDirectoryPath();
336 
337 #if defined(XP_UNIX)
338     preferredPath.append(String("/.mozilla/plugins"));
339 #elif defined(XP_MACOSX)
340     preferredPath.append(String("/Library/Internet Plug-Ins"));
341 #elif defined(XP_WIN)
342     preferredPath.append(String("\\Application Data\\Mozilla\\plugins"));
343 #endif
344 
345     // TODO: We should normalize the path before doing a comparison.
346     return path == preferredPath;
347 }
348 
getPluginPathsInDirectories(HashSet<String> & paths) const349 void PluginDatabase::getPluginPathsInDirectories(HashSet<String>& paths) const
350 {
351     // FIXME: This should be a case insensitive set.
352     HashSet<String> uniqueFilenames;
353 
354 #if defined(XP_UNIX) || defined(ANDROID)
355     String fileNameFilter("*.so");
356 #else
357     String fileNameFilter("");
358 #endif
359 
360     Vector<String>::const_iterator dirsEnd = m_pluginDirectories.end();
361     for (Vector<String>::const_iterator dIt = m_pluginDirectories.begin(); dIt != dirsEnd; ++dIt) {
362         Vector<String> pluginPaths = listDirectory(*dIt, fileNameFilter);
363         Vector<String>::const_iterator pluginsEnd = pluginPaths.end();
364         for (Vector<String>::const_iterator pIt = pluginPaths.begin(); pIt != pluginsEnd; ++pIt) {
365             if (!fileExists(*pIt))
366                 continue;
367 
368             paths.add(*pIt);
369         }
370     }
371 }
372 
373 #endif // !PLATFORM(WIN_OS)
374 
375 }
376