1 /* 2 * Copyright (C) 2010 Igalia S.L. 3 * Copyright (C) 2011 Gustavo Noronha Silva <gns (at) gnome.org> 4 * 5 * This library is free software; you can redistribute it and/or 6 * modify it under the terms of the GNU Lesser General Public 7 * License as published by the Free Software Foundation; either 8 * version 2 of the License, or (at your option) any later version. 9 * 10 * This library is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 * Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public 16 * License along with this library; if not, write to the Free Software 17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 */ 19 20 #include "config.h" 21 #include "webkitwebplugin.h" 22 23 #include "PluginPackage.h" 24 #include "webkitglobalsprivate.h" 25 #include "webkitwebpluginprivate.h" 26 #include <glib/gi18n-lib.h> 27 28 /** 29 * SECTION:webkitwebplugin 30 * @short_description: Represents a plugin, enabling fine-grained control 31 * @see_also: #WebKitWebPluginDatabase 32 * 33 * This object represents a single plugin, found by WebKitGTK+ while 34 * scanning the various usual directories. This object can be used to 35 * get more information about a plugin, and enable/disable it, 36 * allowing fine-grained control of plugins. The list of available 37 * plugins can be obtained from the #WebKitWebPluginDatabase object. 38 */ 39 40 using namespace WebCore; 41 42 enum { 43 PROP_0, 44 45 PROP_ENABLED 46 }; 47 48 G_DEFINE_TYPE(WebKitWebPlugin, webkit_web_plugin, G_TYPE_OBJECT) 49 50 static void freeMIMEType(WebKitWebPluginMIMEType* mimeType) 51 { 52 if (mimeType->name) 53 g_free(mimeType->name); 54 if (mimeType->description) 55 g_free(mimeType->description); 56 if (mimeType->extensions) 57 g_strfreev(mimeType->extensions); 58 g_slice_free(WebKitWebPluginMIMEType, mimeType); 59 } 60 61 static void webkit_web_plugin_finalize(GObject* object) 62 { 63 WebKitWebPlugin* plugin = WEBKIT_WEB_PLUGIN(object); 64 WebKitWebPluginPrivate* priv = plugin->priv; 65 66 g_free(priv->path); 67 68 g_slist_foreach(priv->mimeTypes, (GFunc)freeMIMEType, 0); 69 g_slist_free(priv->mimeTypes); 70 71 delete plugin->priv; 72 73 G_OBJECT_CLASS(webkit_web_plugin_parent_class)->finalize(object); 74 } 75 76 static void webkit_web_plugin_get_property(GObject* object, guint prop_id, GValue* value, GParamSpec* paramSpec) 77 { 78 WebKitWebPlugin* plugin = WEBKIT_WEB_PLUGIN(object); 79 80 switch (prop_id) { 81 case PROP_ENABLED: 82 g_value_set_boolean(value, webkit_web_plugin_get_enabled(plugin)); 83 break; 84 default: 85 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, paramSpec); 86 } 87 } 88 89 static void webkit_web_plugin_set_property(GObject* object, guint prop_id, const GValue* value, GParamSpec* paramSpec) 90 { 91 WebKitWebPlugin* plugin = WEBKIT_WEB_PLUGIN(object); 92 93 switch (prop_id) { 94 case PROP_ENABLED: 95 webkit_web_plugin_set_enabled(plugin, g_value_get_boolean(value)); 96 break; 97 default: 98 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, paramSpec); 99 } 100 } 101 102 static void webkit_web_plugin_class_init(WebKitWebPluginClass* klass) 103 { 104 webkitInit(); 105 106 GObjectClass* gobjectClass = reinterpret_cast<GObjectClass*>(klass); 107 108 gobjectClass->finalize = webkit_web_plugin_finalize; 109 gobjectClass->get_property = webkit_web_plugin_get_property; 110 gobjectClass->set_property = webkit_web_plugin_set_property; 111 112 g_object_class_install_property(gobjectClass, 113 PROP_ENABLED, 114 g_param_spec_boolean("enabled", 115 _("Enabled"), 116 _("Whether the plugin is enabled"), 117 FALSE, 118 WEBKIT_PARAM_READWRITE)); 119 } 120 121 static void webkit_web_plugin_init(WebKitWebPlugin *plugin) 122 { 123 plugin->priv = new WebKitWebPluginPrivate(); 124 plugin->priv->mimeTypes = 0; 125 } 126 127 namespace WebKit { 128 WebKitWebPlugin* kitNew(WebCore::PluginPackage* package) 129 { 130 WebKitWebPlugin* plugin = WEBKIT_WEB_PLUGIN(g_object_new(WEBKIT_TYPE_WEB_PLUGIN, 0)); 131 132 plugin->priv->corePlugin = package; 133 134 return plugin; 135 } 136 } 137 138 /** 139 * webkit_web_plugin_get_name: 140 * @plugin: a #WebKitWebPlugin 141 * 142 * Returns: the name string for @plugin. 143 * 144 * Since: 1.3.8 145 */ 146 const char* webkit_web_plugin_get_name(WebKitWebPlugin* plugin) 147 { 148 g_return_val_if_fail(WEBKIT_IS_WEB_PLUGIN(plugin), 0); 149 150 WebKitWebPluginPrivate* priv = plugin->priv; 151 152 if (!priv->name.length()) 153 priv->name = priv->corePlugin->name().utf8(); 154 155 return priv->name.data(); 156 } 157 158 /** 159 * webkit_web_plugin_get_description: 160 * @plugin: a #WebKitWebPlugin 161 * 162 * Returns: the description string for @plugin. 163 * 164 * Since: 1.3.8 165 */ 166 const char* webkit_web_plugin_get_description(WebKitWebPlugin* plugin) 167 { 168 g_return_val_if_fail(WEBKIT_IS_WEB_PLUGIN(plugin), 0); 169 170 WebKitWebPluginPrivate* priv = plugin->priv; 171 172 if (!priv->description.length()) 173 priv->description = priv->corePlugin->description().utf8(); 174 175 return priv->description.data(); 176 } 177 178 /** 179 * webkit_web_plugin_get_path: 180 * @plugin: a #WebKitWebPlugin 181 * 182 * Returns: the absolute path to @plugin in system filename encoding 183 * or %NULL on failure to convert the filename from UTF-8. 184 * 185 * Since: 1.4.0 186 */ 187 const char* webkit_web_plugin_get_path(WebKitWebPlugin* plugin) 188 { 189 g_return_val_if_fail(WEBKIT_IS_WEB_PLUGIN(plugin), 0); 190 191 WebKitWebPluginPrivate* priv = plugin->priv; 192 193 if (priv->path) 194 return priv->path; 195 196 GError* error = 0; 197 priv->path = g_filename_from_utf8(priv->corePlugin->path().utf8().data(), -1, 0, 0, &error); 198 199 if (!error) 200 return priv->path; 201 202 // In the unlikely case the convertion fails, report the error and make sure we free 203 // any partial convertion that ended up in the variable. 204 g_free(priv->path); 205 priv->path = 0; 206 207 g_warning("Failed to convert '%s' to system filename encoding: %s", priv->corePlugin->path().utf8().data(), error->message); 208 209 g_clear_error(&error); 210 211 return 0; 212 } 213 214 215 /** 216 * webkit_web_plugin_get_mimetypes: 217 * @plugin: a #WebKitWebPlugin 218 * 219 * Returns all the #WebKitWebPluginMIMEType that @plugin is handling 220 * at the moment. 221 * 222 * Returns: (transfer none) (element-type WebKitWebPluginMIMEType): a #GSList of #WebKitWebPluginMIMEType 223 * 224 * Since: 1.3.8 225 */ 226 GSList* webkit_web_plugin_get_mimetypes(WebKitWebPlugin* plugin) 227 { 228 g_return_val_if_fail(WEBKIT_IS_WEB_PLUGIN(plugin), 0); 229 230 WebKitWebPluginPrivate* priv = plugin->priv; 231 232 if (priv->mimeTypes) 233 return priv->mimeTypes; 234 235 const MIMEToDescriptionsMap& mimeToDescriptions = priv->corePlugin->mimeToDescriptions(); 236 MIMEToDescriptionsMap::const_iterator end = mimeToDescriptions.end(); 237 238 for (MIMEToDescriptionsMap::const_iterator it = mimeToDescriptions.begin(); it != end; ++it) { 239 WebKitWebPluginMIMEType* mimeType = g_slice_new0(WebKitWebPluginMIMEType); 240 mimeType->name = g_strdup(it->first.utf8().data()); 241 mimeType->description = g_strdup(it->second.utf8().data()); 242 243 Vector<String> extensions = priv->corePlugin->mimeToExtensions().get(it->first); 244 mimeType->extensions = static_cast<gchar**>(g_malloc0(extensions.size() + 1)); 245 for (unsigned i = 0; i < extensions.size(); i++) 246 mimeType->extensions[i] = g_strdup(extensions[i].utf8().data()); 247 248 priv->mimeTypes = g_slist_append(priv->mimeTypes, mimeType); 249 } 250 251 return priv->mimeTypes; 252 } 253 254 /** 255 * webkit_web_plugin_set_enabled: 256 * @plugin: a #WebKitWebPlugin 257 * @enabled: whether to enable the plugin 258 * 259 * Sets the enabled status of the @plugin. 260 * 261 * Since: 1.3.8 262 */ 263 void webkit_web_plugin_set_enabled(WebKitWebPlugin* plugin, gboolean enabled) 264 { 265 g_return_if_fail(WEBKIT_IS_WEB_PLUGIN(plugin)); 266 WebKitWebPluginPrivate* priv = plugin->priv; 267 268 ASSERT(priv->corePlugin); 269 if (priv->corePlugin->isEnabled() == enabled) 270 return; 271 272 priv->corePlugin->setEnabled(enabled); 273 274 g_object_notify(G_OBJECT(plugin), "enabled"); 275 } 276 277 /** 278 * webkit_web_plugin_get_enabled: 279 * @plugin: a #WebKitWebPlugin 280 * 281 * Returns: %TRUE if the plugin is enabled, %FALSE otherwise 282 * 283 * Since: 1.3.8 284 */ 285 gboolean webkit_web_plugin_get_enabled(WebKitWebPlugin* plugin) 286 { 287 g_return_val_if_fail(WEBKIT_IS_WEB_PLUGIN(plugin), FALSE); 288 289 ASSERT(plugin->priv->corePlugin); 290 return plugin->priv->corePlugin->isEnabled(); 291 } 292