1/*
2 * Copyright (C) 2012 Igalia S.L.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB.  If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20#include "config.h"
21#include "WebKitPlugin.h"
22
23#include "WebKitMimeInfoPrivate.h"
24#include "WebKitPluginPrivate.h"
25#include <wtf/text/CString.h>
26
27using namespace WebKit;
28
29/**
30 * SECTION: WebKitPlugin
31 * @Short_description: Represents a plugin, enabling fine-grained control
32 * @Title: WebKitPlugin
33 *
34 * This object represents a single plugin, found while scanning the
35 * various platform plugin directories. This object can be used to get
36 * more information about a plugin, and enable/disable it, allowing
37 * fine-grained control of plugins. The list of available plugins can
38 * be obtained from the #WebKitWebContext, with
39 * webkit_web_context_get_plugins().
40 *
41 */
42
43struct _WebKitPluginPrivate {
44    ~_WebKitPluginPrivate()
45    {
46        g_list_free_full(mimeInfoList, reinterpret_cast<GDestroyNotify>(webkit_mime_info_unref));
47    }
48
49    PluginModuleInfo pluginInfo;
50    CString name;
51    CString description;
52    CString path;
53    GList* mimeInfoList;
54};
55
56WEBKIT_DEFINE_TYPE(WebKitPlugin, webkit_plugin, G_TYPE_OBJECT)
57
58static void webkit_plugin_class_init(WebKitPluginClass*)
59{
60}
61
62WebKitPlugin* webkitPluginCreate(const PluginModuleInfo& pluginInfo)
63{
64    WebKitPlugin* plugin = WEBKIT_PLUGIN(g_object_new(WEBKIT_TYPE_PLUGIN, NULL));
65    plugin->priv->pluginInfo = pluginInfo;
66    return plugin;
67}
68
69/**
70 * webkit_plugin_get_name:
71 * @plugin: a #WebKitPlugin
72 *
73 * Returns: the name of the plugin.
74 */
75const char* webkit_plugin_get_name(WebKitPlugin* plugin)
76{
77    g_return_val_if_fail(WEBKIT_IS_PLUGIN(plugin), 0);
78
79    if (!plugin->priv->name.isNull())
80        return plugin->priv->name.data();
81
82    if (plugin->priv->pluginInfo.info.name.isEmpty())
83        return 0;
84
85    plugin->priv->name = plugin->priv->pluginInfo.info.name.utf8();
86    return plugin->priv->name.data();
87}
88
89/**
90 * webkit_plugin_get_description:
91 * @plugin: a #WebKitPlugin
92 *
93 * Returns: the description of the plugin.
94 */
95const char* webkit_plugin_get_description(WebKitPlugin* plugin)
96{
97    g_return_val_if_fail(WEBKIT_IS_PLUGIN(plugin), 0);
98
99    if (!plugin->priv->description.isNull())
100        return plugin->priv->description.data();
101
102    if (plugin->priv->pluginInfo.info.desc.isEmpty())
103        return 0;
104
105    plugin->priv->description = plugin->priv->pluginInfo.info.desc.utf8();
106    return plugin->priv->description.data();
107}
108
109/**
110 * webkit_plugin_get_path:
111 * @plugin: a #WebKitPlugin
112 *
113 * Returns: the absolute path where the plugin is installed.
114 */
115const char* webkit_plugin_get_path(WebKitPlugin* plugin)
116{
117    g_return_val_if_fail(WEBKIT_IS_PLUGIN(plugin), 0);
118
119    if (!plugin->priv->path.isNull())
120        return plugin->priv->path.data();
121
122    if (plugin->priv->pluginInfo.path.isEmpty())
123        return 0;
124
125    plugin->priv->path = plugin->priv->pluginInfo.path.utf8();
126    return plugin->priv->path.data();
127}
128
129/**
130 * webkit_plugin_get_mime_info_list:
131 * @plugin: a #WebKitPlugin
132 *
133 * Get information about MIME types handled by the plugin,
134 * as a list of #WebKitMimeInfo.
135 *
136 * Returns: (element-type WebKitMimeInfo) (transfer none): a #GList of #WebKitMimeInfo.
137 */
138GList* webkit_plugin_get_mime_info_list(WebKitPlugin* plugin)
139{
140    g_return_val_if_fail(WEBKIT_IS_PLUGIN(plugin), 0);
141
142    if (plugin->priv->mimeInfoList)
143        return plugin->priv->mimeInfoList;
144
145    if (plugin->priv->pluginInfo.info.mimes.isEmpty())
146        return 0;
147
148    for (size_t i = 0; i < plugin->priv->pluginInfo.info.mimes.size(); ++i)
149        plugin->priv->mimeInfoList = g_list_prepend(plugin->priv->mimeInfoList, webkitMimeInfoCreate(plugin->priv->pluginInfo.info.mimes[i]));
150    return plugin->priv->mimeInfoList;
151}
152