PluginDatabase.cpp 11 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*
 * Copyright (C) 2006, 2007 Apple Inc.  All rights reserved.
 * Copyright (C) 2008 Collabora, Ltd.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */

#include "config.h"
#include "PluginDatabase.h"

#include "Frame.h"
31
#include "KURL.h"
32
#include "PluginPackage.h"
33
#include <stdlib.h>
34 35 36 37 38 39 40 41 42

namespace WebCore {

PluginDatabase* PluginDatabase::installedPlugins()
{
    static PluginDatabase* plugins = 0;
    
    if (!plugins) {
        plugins = new PluginDatabase;
43
        plugins->setPluginDirectories(PluginDatabase::defaultPluginDirectories());
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
        plugins->refresh();
    }

    return plugins;
}

bool PluginDatabase::isMIMETypeRegistered(const String& mimeType)
{
    if (mimeType.isNull())
        return false;
    if (m_registeredMIMETypes.contains(mimeType))
        return true;
    // No plugin was found, try refreshing the database and searching again
    return (refresh() && m_registeredMIMETypes.contains(mimeType));
}

60
void PluginDatabase::addExtraPluginDirectory(const String& directory)
61
{
62
    m_pluginDirectories.append(directory);
63 64 65 66 67 68 69 70
    refresh();
}

bool PluginDatabase::refresh()
{   
    bool pluginSetChanged = false;

    if (!m_plugins.isEmpty()) {
71 72
        PluginSet pluginsToUnload;
        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 78 79 80
            remove(it->get());

        pluginSetChanged = !pluginsToUnload.isEmpty();
    }
81

82 83
    HashSet<String> paths;
    getPluginPathsInDirectories(paths);
84

85 86 87 88 89 90 91
    HashMap<String, time_t> pathsWithTimes;

    // We should only skip unchanged files if we didn't remove any plugins above. If we did remove
    // any plugins, we need to look at every plugin file so that, e.g., if the user has two versions
    // of RealPlayer installed and just removed the newer one, we'll pick up the older one.
    bool shouldSkipUnchangedFiles = !pluginSetChanged;

92 93 94 95 96
    HashSet<String>::const_iterator pathsEnd = paths.end();
    for (HashSet<String>::const_iterator it = paths.begin(); it != pathsEnd; ++it) {
        time_t lastModified;
        if (!getFileModificationTime(*it, lastModified))
            continue;
97

98 99 100 101 102 103
        pathsWithTimes.add(*it, lastModified);

        // If the path's timestamp hasn't changed since the last time we ran refresh(), we don't have to do anything.
        if (shouldSkipUnchangedFiles && m_pluginPathsWithTimes.get(*it) == lastModified)
            continue;

104
        if (RefPtr<PluginPackage> oldPackage = m_pluginsByPath.get(*it)) {
105
            ASSERT(!shouldSkipUnchangedFiles || oldPackage->lastModified() != lastModified);
106 107 108
            remove(oldPackage.get());
        }

109 110
        RefPtr<PluginPackage> package = PluginPackage::createPackage(*it, lastModified);
        if (package && add(package.release()))
111
            pluginSetChanged = true;
112 113
    }

114 115 116 117 118 119 120 121
    // Cache all the paths we found with their timestamps for next time.
    pathsWithTimes.swap(m_pluginPathsWithTimes);

    if (!pluginSetChanged)
        return false;

    m_registeredMIMETypes.clear();

122 123 124 125 126 127 128 129 130 131
    // Register plug-in MIME types
    PluginSet::const_iterator end = m_plugins.end();
    for (PluginSet::const_iterator it = m_plugins.begin(); it != end; ++it) {
        // Get MIME types
        MIMEToDescriptionsMap::const_iterator map_end = (*it)->mimeToDescriptions().end();
        for (MIMEToDescriptionsMap::const_iterator map_it = (*it)->mimeToDescriptions().begin(); map_it != map_end; ++map_it) {
            m_registeredMIMETypes.add(map_it->first);
        }
    }

132
    return true;
133 134 135 136 137 138 139 140 141 142 143 144 145
}

Vector<PluginPackage*> PluginDatabase::plugins() const
{
    Vector<PluginPackage*> result;

    PluginSet::const_iterator end = m_plugins.end();
    for (PluginSet::const_iterator it = m_plugins.begin(); it != end; ++it)
        result.append((*it).get());

    return result;
}

146 147 148 149 150 151 152 153
int PluginDatabase::preferredPluginCompare(const void* a, const void* b)
{
    PluginPackage* pluginA = *static_cast<PluginPackage* const*>(a);
    PluginPackage* pluginB = *static_cast<PluginPackage* const*>(b);

    return pluginA->compare(*pluginB);
}

154 155 156 157 158 159 160 161
PluginPackage* PluginDatabase::pluginForMIMEType(const String& mimeType)
{
    if (mimeType.isEmpty())
        return 0;

    String key = mimeType.lower();
    PluginSet::const_iterator end = m_plugins.end();

162 163
    Vector<PluginPackage*, 2> pluginChoices;

164
    for (PluginSet::const_iterator it = m_plugins.begin(); it != end; ++it) {
165 166
        if ((*it)->mimeToDescriptions().contains(key))
            pluginChoices.append((*it).get());
167 168
    }

169 170 171 172 173 174
    if (pluginChoices.isEmpty())
        return 0;

    qsort(pluginChoices.data(), pluginChoices.size(), sizeof(PluginPackage*), PluginDatabase::preferredPluginCompare);

    return pluginChoices[0];
175 176 177 178 179 180 181 182 183
}

String PluginDatabase::MIMETypeForExtension(const String& extension) const
{
    if (extension.isEmpty())
        return String();

    PluginSet::const_iterator end = m_plugins.end();
    String mimeType;
184 185
    Vector<PluginPackage*, 2> pluginChoices;
    HashMap<PluginPackage*, String> mimeTypeForPlugin;
186 187 188 189 190 191

    for (PluginSet::const_iterator it = m_plugins.begin(); it != end; ++it) {
        MIMEToExtensionsMap::const_iterator mime_end = (*it)->mimeToExtensions().end();

        for (MIMEToExtensionsMap::const_iterator mime_it = (*it)->mimeToExtensions().begin(); mime_it != mime_end; ++mime_it) {
            const Vector<String>& extensions = mime_it->second;
192
            bool foundMapping = false;
193 194
            for (unsigned i = 0; i < extensions.size(); i++) {
                if (equalIgnoringCase(extensions[i], extension)) {
195 196 197 198 199
                    PluginPackage* plugin = (*it).get();
                    pluginChoices.append(plugin);
                    mimeTypeForPlugin.add(plugin, mime_it->first);
                    foundMapping = true;
                    break;
200 201
                }
            }
202 203
            if (foundMapping)
                break;
204 205 206
        }
    }

207 208 209 210 211 212
    if (pluginChoices.isEmpty())
        return String();

    qsort(pluginChoices.data(), pluginChoices.size(), sizeof(PluginPackage*), PluginDatabase::preferredPluginCompare);

    return mimeTypeForPlugin.get(pluginChoices[0]);
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
}

PluginPackage* PluginDatabase::findPlugin(const KURL& url, String& mimeType)
{
    PluginPackage* plugin = pluginForMIMEType(mimeType);
    String filename = url.string();
    
    if (!plugin) {
        String filename = url.lastPathComponent();
        if (!filename.endsWith("/")) {
            int extensionPos = filename.reverseFind('.');
            if (extensionPos != -1) {
                String extension = filename.substring(extensionPos + 1);

                mimeType = MIMETypeForExtension(extension);
                plugin = pluginForMIMEType(mimeType);
            }
        }
    }

    // FIXME: if no plugin could be found, query Windows for the mime type 
    // corresponding to the extension.

    return plugin;
}

239 240 241 242 243 244 245 246 247 248 249
void PluginDatabase::getDeletedPlugins(PluginSet& plugins) const
{
    PluginSet::const_iterator end = m_plugins.end();
    for (PluginSet::const_iterator it = m_plugins.begin(); it != end; ++it) {
        if (!fileExists((*it)->path()))
            plugins.add(*it);
    }
}

bool PluginDatabase::add(PassRefPtr<PluginPackage> prpPackage)
{
250 251
    ASSERT_ARG(prpPackage, prpPackage);

252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
    RefPtr<PluginPackage> package = prpPackage;

    if (!m_plugins.add(package).second)
        return false;

    m_pluginsByPath.add(package->path(), package);
    return true;
}

void PluginDatabase::remove(PluginPackage* package)
{
    m_plugins.remove(package);
    m_pluginsByPath.remove(package->path());
}

267 268 269 270 271 272 273 274 275 276
#if PLATFORM(GTK) || (PLATFORM(QT) && defined(Q_WS_X11))

#if PLATFORM(GTK)
#include <gdkconfig.h>
#endif

static void addMozillaPluginDirectories(Vector<String>& paths)
{
#if PLATFORM(QT) || defined(GDK_WINDOWING_X11)
    String userPluginPath = homeDirectoryPath();
277 278 279 280 281
    userPluginPath.append(String("/.mozilla/plugins"));
    paths.append(userPluginPath);

    userPluginPath = homeDirectoryPath();
    userPluginPath.append(String("/.netscape/plugins"));
282 283 284 285
    paths.append(userPluginPath);

    paths.append("/usr/lib/browser/plugins");
    paths.append("/usr/local/lib/mozilla/plugins");
286 287 288
    paths.append("/usr/lib/firefox/plugins");
    paths.append("/usr/lib64/browser-plugins");
    paths.append("/usr/lib/browser-plugins");
289
    paths.append("/usr/lib/mozilla/plugins");
290 291 292 293 294 295 296 297 298 299 300 301 302 303
    paths.append("/usr/local/netscape/plugins");
    paths.append("/opt/mozilla/plugins");
    paths.append("/opt/mozilla/lib/plugins");
    paths.append("/opt/netscape/plugins");
    paths.append("/opt/netscape/communicator/plugins");
    paths.append("/usr/lib/netscape/plugins");
    paths.append("/usr/lib/netscape/plugins-libc5");
    paths.append("/usr/lib/netscape/plugins-libc6");
    paths.append("/usr/lib64/netscape/plugins");
    paths.append("/usr/lib64/mozilla/plugins");

    String mozHome(getenv("MOZILLA_HOME"));
    mozHome.append("/plugins");
    paths.append(mozHome);
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337

    Vector<String> mozPaths;
    String mozPath(getenv("MOZ_PLUGIN_PATH"));
    mozPath.split(UChar(':'), /* allowEmptyEntries */ false, mozPaths);
    paths.append(mozPaths);
#endif

#if defined(GDK_WINDOWING_WIN32)
    gchar* directory = g_build_filename(g_get_home_dir(), "Application Data", "Mozilla", "plugins", NULL);
    paths.append(directory);
    g_free(directory);
#endif
}

Vector<String> PluginDatabase::defaultPluginDirectories()
{
    Vector<String> paths;

#if PLATFORM(QT)
    Vector<String> qtPaths;
    String qtPath(getenv("QTWEBKIT_PLUGIN_PATH"));
    qtPath.split(UChar(':'), /* allowEmptyEntries */ false, qtPaths);
    paths.append(qtPaths);
#endif

    addMozillaPluginDirectories(paths);

    return paths;
}

#if PLATFORM(QT) || defined(GDK_WINDOWING_X11)
bool PluginDatabase::isPreferredPluginDirectory(const String& path)
{
    String prefPath = homeDirectoryPath();
338
    prefPath.append(String("/.mozilla/plugins"));
339 340 341 342 343 344 345

    return path == prefPath;
}
#endif

#endif

346
}