1/*
2 * Copyright (C) 2006, 2007 Apple Inc.  All rights reserved.
3 * Copyright (C) 2008 Collabora, Ltd.  All rights reserved.
4 * Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies)
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 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 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#ifndef PluginDatabase_h
29#define PluginDatabase_h
30
31#include "PluginPackage.h"
32#include <wtf/HashSet.h>
33#include <wtf/Vector.h>
34#include <wtf/text/StringHash.h>
35#include <wtf/text/WTFString.h>
36
37namespace WebCore {
38    class Element;
39    class Frame;
40    class IntSize;
41    class URL;
42    class PluginDatabaseClient;
43    class PluginPackage;
44
45    typedef HashSet<RefPtr<PluginPackage>, PluginPackageHash, PluginPackageHashTraits> PluginSet;
46
47    class PluginDatabase {
48        WTF_MAKE_NONCOPYABLE(PluginDatabase); WTF_MAKE_FAST_ALLOCATED;
49    public:
50        PluginDatabase();
51
52        // The first call to installedPlugins creates the plugin database
53        // and by default populates it with the plugins installed on the system.
54        // For testing purposes, it is possible to not populate the database
55        // automatically, as the plugins might affect the DRT results by
56        // writing to a.o. stderr.
57        static PluginDatabase* installedPlugins(bool populate = true);
58
59        bool refresh();
60        void clear();
61        Vector<PluginPackage*> plugins() const;
62        bool isMIMETypeRegistered(const String& mimeType);
63        void addExtraPluginDirectory(const String&);
64
65        static bool isPreferredPluginDirectory(const String& directory);
66        static int preferredPluginCompare(const void*, const void*);
67
68        PluginPackage* findPlugin(const URL&, String& mimeType);
69        PluginPackage* pluginForMIMEType(const String& mimeType);
70        void setPreferredPluginForMIMEType(const String& mimeType, PluginPackage* plugin);
71
72        void setPluginDirectories(const Vector<String>& directories)
73        {
74            clear();
75            m_pluginDirectories = directories;
76        }
77
78        bool removeDisabledPluginFile(const String& fileName);
79        bool addDisabledPluginFile(const String& fileName);
80        static Vector<String> defaultPluginDirectories();
81        Vector<String> pluginDirectories() const { return m_pluginDirectories; }
82
83        String MIMETypeForExtension(const String& extension) const;
84#if ENABLE(NETSCAPE_PLUGIN_METADATA_CACHE)
85        static bool isPersistentMetadataCacheEnabled();
86        static void setPersistentMetadataCacheEnabled(bool isEnabled);
87        static String persistentMetadataCachePath();
88        static void setPersistentMetadataCachePath(const String& persistentMetadataCachePath);
89#endif
90
91    private:
92        void getPluginPathsInDirectories(HashSet<String>&) const;
93        void getDeletedPlugins(PluginSet&) const;
94        bool fileExistsAndIsNotDisabled(const String&) const;
95
96        // Returns whether the plugin was actually added or not (it won't be added if it's a duplicate of an existing plugin).
97        bool add(PassRefPtr<PluginPackage>);
98        void remove(PluginPackage*);
99#if ENABLE(NETSCAPE_PLUGIN_METADATA_CACHE)
100        void loadPersistentMetadataCache();
101        void updatePersistentMetadataCache();
102#endif
103
104        HashSet<String> m_disabledPluginFiles;
105        Vector<String> m_pluginDirectories;
106        HashSet<String> m_registeredMIMETypes;
107        PluginSet m_plugins;
108        HashMap<String, RefPtr<PluginPackage> > m_pluginsByPath;
109        HashMap<String, time_t> m_pluginPathsWithTimes;
110        HashMap<String, RefPtr<PluginPackage> > m_preferredPlugins;
111#if ENABLE(NETSCAPE_PLUGIN_METADATA_CACHE)
112        bool m_persistentMetadataCacheIsLoaded;
113#endif
114    };
115
116} // namespace WebCore
117
118#endif
119