1/*
2 * Copyright (C) 2018 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 Lesser 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 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19#include "config.h"
20#include "WebKitDeviceInfoPermissionRequest.h"
21
22#include "DeviceIdHashSaltStorage.h"
23#include "UserMediaPermissionCheckProxy.h"
24#include "WebKitDeviceInfoPermissionRequestPrivate.h"
25#include "WebKitPermissionRequest.h"
26#include "WebsiteDataStore.h"
27#include <glib/gi18n-lib.h>
28#include <wtf/glib/WTFGType.h>
29
30using namespace WebKit;
31
32/**
33 * SECTION: WebKitDeviceInfoPermissionRequest
34 * @Short_description: A permission request for accessing user's audio/video devices.
35 * @Title: WebKitDeviceInfoPermissionRequest
36 * @See_also: #WebKitPermissionRequest, #WebKitWebView
37 *
38 * WebKitUserMediaPermissionRequest represents a request for
39 * permission to whether WebKit should be allowed to access the user's
40 * devices information when requested through the enumeraceDevices API.
41 *
42 * When a WebKitDeviceInfoPermissionRequest is not handled by the user,
43 * it is denied by default.
44 *
45 * Since: 2.24
46 */
47
48static void webkit_permission_request_interface_init(WebKitPermissionRequestIface*);
49
50struct _WebKitDeviceInfoPermissionRequestPrivate {
51 RefPtr<UserMediaPermissionCheckProxy> request;
52 RefPtr<DeviceIdHashSaltStorage> deviceIdHashSaltStorage;
53 bool madeDecision;
54};
55
56WEBKIT_DEFINE_TYPE_WITH_CODE(
57 WebKitDeviceInfoPermissionRequest, webkit_device_info_permission_request, G_TYPE_OBJECT,
58 G_IMPLEMENT_INTERFACE(WEBKIT_TYPE_PERMISSION_REQUEST, webkit_permission_request_interface_init))
59
60static void webkitDeviceInfoPermissionRequestAllow(WebKitPermissionRequest* request)
61{
62 ASSERT(WEBKIT_IS_DEVICE_INFO_PERMISSION_REQUEST(request));
63
64 auto* priv = WEBKIT_DEVICE_INFO_PERMISSION_REQUEST(request)->priv;
65
66 if (!priv->deviceIdHashSaltStorage) {
67 priv->request->setUserMediaAccessInfo(false);
68 return;
69 }
70
71 // Only one decision at a time.
72 if (priv->madeDecision)
73 return;
74
75 priv->madeDecision = true;
76 priv->request->setUserMediaAccessInfo(true);
77}
78
79static void webkitDeviceInfoPermissionRequestDeny(WebKitPermissionRequest* request)
80{
81 ASSERT(WEBKIT_IS_DEVICE_INFO_PERMISSION_REQUEST(request));
82
83 auto* priv = WEBKIT_DEVICE_INFO_PERMISSION_REQUEST(request)->priv;
84
85 if (!priv->deviceIdHashSaltStorage) {
86 priv->request->setUserMediaAccessInfo(false);
87 return;
88 }
89
90 // Only one decision at a time.
91 if (priv->madeDecision)
92 return;
93
94 priv->madeDecision = true;
95 priv->request->setUserMediaAccessInfo(false);
96}
97
98static void webkit_permission_request_interface_init(WebKitPermissionRequestIface* iface)
99{
100 iface->allow = webkitDeviceInfoPermissionRequestAllow;
101 iface->deny = webkitDeviceInfoPermissionRequestDeny;
102}
103
104static void webkitDeviceInfoPermissionRequestDispose(GObject* object)
105{
106 // Default behaviour when no decision has been made is denying the request.
107 webkitDeviceInfoPermissionRequestDeny(WEBKIT_PERMISSION_REQUEST(object));
108 G_OBJECT_CLASS(webkit_device_info_permission_request_parent_class)->dispose(object);
109}
110
111static void webkit_device_info_permission_request_class_init(WebKitDeviceInfoPermissionRequestClass* klass)
112{
113 GObjectClass* objectClass = G_OBJECT_CLASS(klass);
114 objectClass->dispose = webkitDeviceInfoPermissionRequestDispose;
115}
116
117WebKitDeviceInfoPermissionRequest* webkitDeviceInfoPermissionRequestCreate(UserMediaPermissionCheckProxy& request, DeviceIdHashSaltStorage* deviceIdHashSaltStorage)
118{
119 auto* deviceInfoPermissionRequest = WEBKIT_DEVICE_INFO_PERMISSION_REQUEST(g_object_new(WEBKIT_TYPE_DEVICE_INFO_PERMISSION_REQUEST, nullptr));
120
121 deviceInfoPermissionRequest->priv->request = &request;
122 deviceInfoPermissionRequest->priv->deviceIdHashSaltStorage = deviceIdHashSaltStorage;
123 return deviceInfoPermissionRequest;
124}
125