1/*
2 * Copyright (C) 2015 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 "WebKitInstallMissingMediaPluginsPermissionRequest.h"
22
23#include "WebKitInstallMissingMediaPluginsPermissionRequestPrivate.h"
24#include "WebKitPermissionRequest.h"
25#include "WebPageProxy.h"
26#include <wtf/glib/WTFGType.h>
27
28#if ENABLE(VIDEO)
29#include <WebCore/PlatformDisplay.h>
30#if PLATFORM(X11)
31#include <gdk/gdkx.h>
32#include <gtk/gtk.h>
33#endif
34#endif
35
36using namespace WebKit;
37using namespace WebCore;
38
39/**
40 * SECTION: WebKitInstallMissingMediaPluginsPermissionRequest
41 * @Short_description: A permission request for installing missing media plugins
42 * @Title: WebKitInstallMissingMediaPluginsPermissionRequest
43 * @See_also: #WebKitPermissionRequest, #WebKitWebView
44 *
45 * WebKitInstallMissingMediaPluginsPermissionRequest represents a request for
46 * permission to decide whether WebKit should try to start a helper application to
47 * install missing media plugins when the media backend couldn't play a media because
48 * the required plugins were not available.
49 *
50 * When a WebKitInstallMissingMediaPluginsPermissionRequest is not handled by the user,
51 * it is allowed by default.
52 *
53 * Since: 2.10
54 */
55
56static void webkit_permission_request_interface_init(WebKitPermissionRequestIface*);
57
58struct _WebKitInstallMissingMediaPluginsPermissionRequestPrivate {
59#if ENABLE(VIDEO)
60 RefPtr<InstallMissingMediaPluginsPermissionRequest> request;
61#endif
62 CString description;
63 bool madeDecision;
64};
65
66WEBKIT_DEFINE_TYPE_WITH_CODE(
67 WebKitInstallMissingMediaPluginsPermissionRequest, webkit_install_missing_media_plugins_permission_request, G_TYPE_OBJECT,
68 G_IMPLEMENT_INTERFACE(WEBKIT_TYPE_PERMISSION_REQUEST, webkit_permission_request_interface_init))
69
70#if ENABLE(VIDEO)
71static GUniquePtr<GstInstallPluginsContext> createGstInstallPluginsContext(WebPageProxy& page)
72{
73#if PLATFORM(X11)
74 if (PlatformDisplay::sharedDisplay().type() == PlatformDisplay::Type::X11) {
75 GUniquePtr<GstInstallPluginsContext> context(gst_install_plugins_context_new());
76 gst_install_plugins_context_set_xid(context.get(), GDK_WINDOW_XID(gtk_widget_get_window(page.viewWidget())));
77 return context;
78 }
79#endif
80
81 return nullptr;
82}
83#endif
84
85static void webkitInstallMissingMediaPluginsPermissionRequestAllow(WebKitPermissionRequest* request)
86{
87 ASSERT(WEBKIT_IS_INSTALL_MISSING_MEDIA_PLUGINS_PERMISSION_REQUEST(request));
88
89 WebKitInstallMissingMediaPluginsPermissionRequestPrivate* priv = WEBKIT_INSTALL_MISSING_MEDIA_PLUGINS_PERMISSION_REQUEST(request)->priv;
90
91 // Only one decision at a time.
92 if (priv->madeDecision)
93 return;
94#if ENABLE(VIDEO)
95 priv->request->allow(createGstInstallPluginsContext(priv->request->page()));
96#endif
97 priv->madeDecision = true;
98}
99
100static void webkitInstallMissingMediaPluginsPermissionRequestDeny(WebKitPermissionRequest* request)
101{
102 ASSERT(WEBKIT_IS_INSTALL_MISSING_MEDIA_PLUGINS_PERMISSION_REQUEST(request));
103
104 WebKitInstallMissingMediaPluginsPermissionRequestPrivate* priv = WEBKIT_INSTALL_MISSING_MEDIA_PLUGINS_PERMISSION_REQUEST(request)->priv;
105
106 // Only one decision at a time.
107 if (priv->madeDecision)
108 return;
109
110#if ENABLE(VIDEO)
111 priv->request->deny();
112#endif
113 priv->madeDecision = true;
114}
115
116static void webkit_permission_request_interface_init(WebKitPermissionRequestIface* iface)
117{
118 iface->allow = webkitInstallMissingMediaPluginsPermissionRequestAllow;
119 iface->deny = webkitInstallMissingMediaPluginsPermissionRequestDeny;
120}
121
122static void webkitInstallMissingMediaPluginsPermissionRequestDispose(GObject* object)
123{
124 // Default behaviour when no decision has been made is allowing the request for backwards compatibility.
125 webkitInstallMissingMediaPluginsPermissionRequestDeny(WEBKIT_PERMISSION_REQUEST(object));
126 G_OBJECT_CLASS(webkit_install_missing_media_plugins_permission_request_parent_class)->dispose(object);
127}
128
129static void webkit_install_missing_media_plugins_permission_request_class_init(WebKitInstallMissingMediaPluginsPermissionRequestClass* klass)
130{
131 GObjectClass* objectClass = G_OBJECT_CLASS(klass);
132 objectClass->dispose = webkitInstallMissingMediaPluginsPermissionRequestDispose;
133}
134
135#if ENABLE(VIDEO)
136WebKitInstallMissingMediaPluginsPermissionRequest* webkitInstallMissingMediaPluginsPermissionRequestCreate(InstallMissingMediaPluginsPermissionRequest& request)
137{
138 WebKitInstallMissingMediaPluginsPermissionRequest* permissionRequest = WEBKIT_INSTALL_MISSING_MEDIA_PLUGINS_PERMISSION_REQUEST(g_object_new(WEBKIT_TYPE_INSTALL_MISSING_MEDIA_PLUGINS_PERMISSION_REQUEST, nullptr));
139 permissionRequest->priv->request = &request;
140 return permissionRequest;
141}
142#endif
143
144/**
145 * webkit_install_missing_media_plugins_permission_request_get_description:
146 * @request: a #WebKitInstallMissingMediaPluginsPermissionRequest
147 *
148 * Gets the description about the missing plugins provided by the media backend when a media couldn't be played.
149 *
150 * Returns: a string with the description provided by the media backend.
151 *
152 * Since: 2.10
153 */
154const char* webkit_install_missing_media_plugins_permission_request_get_description(WebKitInstallMissingMediaPluginsPermissionRequest* request)
155{
156 g_return_val_if_fail(WEBKIT_IS_INSTALL_MISSING_MEDIA_PLUGINS_PERMISSION_REQUEST(request), nullptr);
157
158#if ENABLE(VIDEO)
159 if (!request->priv->description.isNull())
160 return request->priv->description.data();
161
162 const auto& description = request->priv->request->description();
163 ASSERT(!description.isEmpty());
164 request->priv->description = description.utf8();
165#endif
166 return request->priv->description.data();
167}
168