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 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 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#ifndef PluginPackage_h
28#define PluginPackage_h
29
30#include "FileSystem.h"
31#include "PluginQuirkSet.h"
32#include "Timer.h"
33#if ENABLE(NETSCAPE_PLUGIN_API)
34#include "npruntime_internal.h"
35#endif
36#include <wtf/HashMap.h>
37#include <wtf/RefCounted.h>
38#include <wtf/text/StringHash.h>
39#include <wtf/text/WTFString.h>
40
41namespace WebCore {
42    typedef HashMap<String, String> MIMEToDescriptionsMap;
43    typedef HashMap<String, Vector<String> > MIMEToExtensionsMap;
44
45    class PluginPackage : public RefCounted<PluginPackage> {
46    public:
47        ~PluginPackage();
48        static PassRefPtr<PluginPackage> createPackage(const String& path, const time_t& lastModified);
49#if ENABLE(NETSCAPE_PLUGIN_METADATA_CACHE)
50        static PassRefPtr<PluginPackage> createPackageFromCache(const String& path, const time_t& lastModified, const String& name, const String& description, const String& mimeDescription);
51#endif
52
53        const String& name() const { return m_name; }
54        const String& description() const { return m_description; }
55        const String& path() const { return m_path; }
56        const String& fileName() const { return m_fileName; }
57        const String& parentDirectory() const { return m_parentDirectory; }
58        PlatformModule module() const { return m_module; }
59        uint16_t NPVersion() const;
60        time_t lastModified() const { return m_lastModified; }
61
62        const MIMEToDescriptionsMap& mimeToDescriptions() const { return m_mimeToDescriptions; }
63        const MIMEToExtensionsMap& mimeToExtensions() const { return m_mimeToExtensions; }
64
65        unsigned hash() const;
66        static bool equal(const PluginPackage& a, const PluginPackage& b);
67
68        bool load();
69        void unload();
70        void unloadWithoutShutdown();
71
72        bool isEnabled() const { return m_isEnabled; }
73        void setEnabled(bool);
74
75#if ENABLE(NETSCAPE_PLUGIN_API)
76        const NPPluginFuncs* pluginFuncs() const { return &m_pluginFuncs; }
77#endif
78        int compareFileVersion(const PlatformModuleVersion&) const;
79        int compare(const PluginPackage&) const;
80        PluginQuirkSet quirks() const { return m_quirks; }
81        const PlatformModuleVersion& version() const { return m_moduleVersion; }
82
83#if ENABLE(NETSCAPE_PLUGIN_METADATA_CACHE)
84        bool ensurePluginLoaded();
85        void setMIMEDescription(const String& mimeDescription);
86        String fullMIMEDescription() const { return m_fullMIMEDescription;}
87#endif
88    private:
89        PluginPackage(const String& path, const time_t& lastModified);
90
91        bool fetchInfo();
92        bool isPluginBlacklisted();
93        void determineQuirks(const String& mimeType);
94
95        void determineModuleVersionFromDescription();
96        void initializeBrowserFuncs();
97
98        bool m_isEnabled;
99        bool m_isLoaded;
100        int m_loadCount;
101
102        String m_description;
103        String m_path;
104        String m_fileName;
105        String m_name;
106        String m_parentDirectory;
107
108        PlatformModuleVersion m_moduleVersion;
109
110        MIMEToDescriptionsMap m_mimeToDescriptions;
111        MIMEToExtensionsMap m_mimeToExtensions;
112
113        PlatformModule m_module;
114        time_t m_lastModified;
115
116#if ENABLE(NETSCAPE_PLUGIN_API)
117        NPP_ShutdownProcPtr m_NPP_Shutdown;
118        NPPluginFuncs m_pluginFuncs;
119        NPNetscapeFuncs m_browserFuncs;
120#endif
121
122        void freeLibrarySoon();
123        void freeLibraryTimerFired(Timer<PluginPackage>*);
124        Timer<PluginPackage> m_freeLibraryTimer;
125
126        PluginQuirkSet m_quirks;
127#if ENABLE(NETSCAPE_PLUGIN_METADATA_CACHE)
128        String m_fullMIMEDescription;
129        bool m_infoIsFromCache;
130#endif
131    };
132
133    // FIXME: This is a workaround because PluginPackageHash is broken and may consider keys with different hashes as equal.
134    struct PluginPackageHashTraits : HashTraits<RefPtr<PluginPackage> > {
135        static const int minimumTableSize = 64;
136    };
137
138    struct PluginPackageHash {
139        static unsigned hash(const uintptr_t key) { return reinterpret_cast<PluginPackage*>(key)->hash(); }
140        static unsigned hash(const RefPtr<PluginPackage>& key) { return key->hash(); }
141
142        static bool equal(const uintptr_t a, const uintptr_t b) { return equal(reinterpret_cast<PluginPackage*>(a), reinterpret_cast<PluginPackage*>(b)); }
143        static bool equal(const RefPtr<PluginPackage>& a, const RefPtr<PluginPackage>& b) { return PluginPackage::equal(*a.get(), *b.get()); }
144        static const bool safeToCompareToEmptyOrDeleted = false;
145    };
146
147} // namespace WebCore
148
149#endif
150