summaryrefslogtreecommitdiff
path: root/src/3rdparty/webkit/WebKit/qt/Api/qwebplugindatabase.cpp
blob: a3781e6ce35756f04686c0f0bde854e1e00f7d4b (plain)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
    Copyright (C) 2009 Jakub Wieczorek <faw217@gmail.com>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include "config.h"
#include "qwebplugindatabase.h"

#include "PluginDatabase.h"
#include "PluginPackage.h"

using namespace WebCore;

/*!
    \typedef QWebPluginInfo::MimeType
    \since 4.6
    \brief Represents a single MIME type supported by a plugin.
*/

/*!
    \class QWebPluginInfo
    \since 4.6
    \brief The QWebPluginInfo class represents a single Netscape plugin.

    A QWebPluginInfo object represents a Netscape plugin picked up by WebKit
    and included in the plugin database. This class contains information about
    the plugin, such as its name(), description(), a list of MIME types that it
    supports (can be accessed with mimeTypes()) and the path of the plugin
    file.

    Plugins can be enabled and disabled with setEnabled(). If a plugin is
    disabled, it will not be used by WebKit to handle supported MIME types. To
    check if a plugin is enabled or not, use enabled().

    \sa QWebPluginDatabase
*/

/*!
    Constructs a null QWebPluginInfo.
*/
QWebPluginInfo::QWebPluginInfo()
    : m_package(0)
{
}

QWebPluginInfo::QWebPluginInfo(PluginPackage* package)
    : m_package(package)
{
    if (m_package)
        m_package->ref();
}

/*!
    Contructs a copy of \a other.
*/
QWebPluginInfo::QWebPluginInfo(const QWebPluginInfo& other)
    : m_package(other.m_package)
{
    if (m_package)
        m_package->ref();
}

/*!
    Destroys the plugin info.
*/
QWebPluginInfo::~QWebPluginInfo()
{
    if (m_package)
        m_package->deref();
}

/*!
    Returns the name of the plugin.

    \sa description()
*/
QString QWebPluginInfo::name() const
{
    if (!m_package)
        return QString();
    return m_package->name();
}

/*!
    Returns the description of the plugin.

    \sa name()
*/
QString QWebPluginInfo::description() const
{
    if (!m_package)
        return QString();
    return m_package->description();
}

/*!
    Returns a list of MIME types supported by the plugin.

    \sa supportsMimeType()
*/
QList<QWebPluginInfo::MimeType> QWebPluginInfo::mimeTypes() const
{
    if (m_package && m_mimeTypes.isEmpty()) {
        const MIMEToDescriptionsMap& mimeToDescriptions = m_package->mimeToDescriptions();
        MIMEToDescriptionsMap::const_iterator end = mimeToDescriptions.end();

        for (MIMEToDescriptionsMap::const_iterator it = mimeToDescriptions.begin(); it != end; ++it) {
            MimeType mimeType;
            mimeType.name = it->first;
            mimeType.description = it->second;

            QStringList fileExtensions;
            Vector<String> extensions = m_package->mimeToExtensions().get(mimeType.name);

            for (unsigned i = 0; i < extensions.size(); ++i)
                fileExtensions.append(extensions[i]);

            mimeType.fileExtensions = fileExtensions;
            m_mimeTypes.append(mimeType);
        }
    }

    return m_mimeTypes;
}

/*!
    Returns true if the plugin supports a specific \a mimeType; otherwise
    returns false.

    \sa mimeTypes()
*/
bool QWebPluginInfo::supportsMimeType(const QString& mimeType) const
{
    if (!m_package)
        return false;
    return m_package->mimeToDescriptions().contains(mimeType);
}

/*!
    Returns an absolute path to the plugin file.
*/
QString QWebPluginInfo::path() const
{
    if (!m_package)
        return QString();
    return m_package->path();
}

/*!
    Returns true if the plugin is a null plugin; otherwise returns false.
*/
bool QWebPluginInfo::isNull() const
{
    return !m_package;
}

/*!
    Enables or disables the plugin, depending on the \a enabled parameter.

    Disabled plugins will not be picked up by WebKit when looking for a plugin
    supporting a particular MIME type.

    \sa isEnabled()
*/
void QWebPluginInfo::setEnabled(bool enabled)
{
    if (!m_package)
        return;
    m_package->setEnabled(enabled);
}

/*!
    Returns true if the plugin is enabled; otherwise returns false.

    \sa setEnabled()
*/
bool QWebPluginInfo::isEnabled() const
{
    if (!m_package)
        return false;
    return m_package->isEnabled();
}

bool QWebPluginInfo::operator==(const QWebPluginInfo& other) const
{
    return m_package == other.m_package;
}

bool QWebPluginInfo::operator!=(const QWebPluginInfo& other) const
{
    return m_package != other.m_package;
}

QWebPluginInfo &QWebPluginInfo::operator=(const QWebPluginInfo& other)
{
    if (this == &other)
        return *this;

    if (m_package)
        m_package->deref();
    m_package = other.m_package;
    if (m_package)
        m_package->ref();
    m_mimeTypes = other.m_mimeTypes;

    return *this;
}

/*!
    \class QWebPluginDatabase
    \since 4.6
    \brief The QWebPluginDatabase class provides an interface for managing
    Netscape plugins used by WebKit in QWebPages.

    The QWebPluginDatabase class is a database of Netscape plugins that are used
    by WebKit. The plugins are picked up by WebKit by looking up a set of search paths.
    The default set can be accessed using defaultSearchPaths(). The search paths
    can be changed, see searchPaths() and setSearchPaths(). Additional search paths
    can also be added using addSearchPath().

    The plugins that have been detected are exposed by the plugins() method.
    The list contains QWebPlugin objects that hold both the metadata and the MIME
    types that are supported by particular plugins.

    WebKit specifies a plugin for a MIME type by looking for the first plugin that
    supports the specific MIME type. To get a plugin, that is used by WebKit to
    handle a specific MIME type, you can use the pluginForMimeType() function.

    To change the way of resolving MIME types ambiguity, you can explicitly set
    a preferred plugin for a specific MIME type, using setPreferredPluginForMimeType().

    \sa QWebPluginInfo, QWebSettings::pluginDatabase()
*/

QWebPluginDatabase::QWebPluginDatabase(QObject* parent)
    : QObject(parent)
    , m_database(PluginDatabase::installedPlugins())
{
}

QWebPluginDatabase::~QWebPluginDatabase()
{
}

/*!
    Returns a list of plugins installed in the search paths.

    This list will contain disabled plugins, although they will not be used by
    WebKit.

    \sa pluginForMimeType()
*/
QList<QWebPluginInfo> QWebPluginDatabase::plugins() const
{
    QList<QWebPluginInfo> qwebplugins;
    const Vector<PluginPackage*>& plugins = m_database->plugins();

    for (unsigned int i = 0; i < plugins.size(); ++i) {
        PluginPackage* plugin = plugins[i];
        qwebplugins.append(QWebPluginInfo(plugin));
    }

    return qwebplugins;
}

/*!
    Returns a default set of search paths.

    \sa searchPaths(), setSearchPaths()
*/
QStringList QWebPluginDatabase::defaultSearchPaths()
{
    QStringList paths;

    const Vector<String>& directories = PluginDatabase::defaultPluginDirectories();
    for (unsigned int i = 0; i < directories.size(); ++i)
        paths.append(directories[i]);

    return paths;
}

/*!
    Returns a list of search paths that are used by WebKit to look for plugins.

    \sa defaultSearchPaths(), setSearchPaths()
*/
QStringList QWebPluginDatabase::searchPaths() const
{
    QStringList paths;

    const Vector<String>& directories = m_database->pluginDirectories();
    for (unsigned int i = 0; i < directories.size(); ++i)
        paths.append(directories[i]);

    return paths;
}

/*!
    Changes the search paths to \a paths.
    The database is automatically refreshed.

    \sa searchPaths(), defaultSearchPaths()
*/
void QWebPluginDatabase::setSearchPaths(const QStringList& paths)
{
    Vector<String> directories;

    for (int i = 0; i < paths.count(); ++i)
        directories.append(paths.at(i));

    m_database->setPluginDirectories(directories);
    // PluginDatabase::setPluginDirectories() does not refresh the database.
    m_database->refresh();
}

/*!
    Adds an additional \a path to the current set.
    The database is automatically refreshed.

    \sa searchPaths(), setSearchPaths()
*/
void QWebPluginDatabase::addSearchPath(const QString& path)
{
    m_database->addExtraPluginDirectory(path);
    // PluginDatabase::addExtraPluginDirectory() does refresh the database.
}

/*!
    Refreshes the plugin database, adds new plugins that have been found and removes
    the ones that are no longer available in the search paths.

    You can call this function when the set of plugins installed in the search paths
    changes. You do not need to call this function when changing search paths,
    in that case WebKit automatically refreshes the database.
*/
void QWebPluginDatabase::refresh()
{
    m_database->refresh();
}

/*!
    Returns the plugin that is currently used by WebKit for a given \a mimeType.

    \sa setPreferredPluginForMimeType()
*/
QWebPluginInfo QWebPluginDatabase::pluginForMimeType(const QString& mimeType)
{
    return QWebPluginInfo(m_database->pluginForMIMEType(mimeType));
}

/*!
    Changes the preferred plugin for a given \a mimeType to \a plugin. The \a plugin
    has to support the given \a mimeType, otherwise the setting will have no effect.

    Calling the function with a null \a plugin resets the setting.

    \sa pluginForMimeType()
*/
void QWebPluginDatabase::setPreferredPluginForMimeType(const QString& mimeType, const QWebPluginInfo& plugin)
{
    m_database->setPreferredPluginForMIMEType(mimeType, plugin.m_package);
}