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