1/*
2 * Copyright (C) 2013 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 "WebKitNotificationPermissionRequest.h"
22
23#include "NotificationPermissionRequest.h"
24#include "WebKitNotificationPermissionRequestPrivate.h"
25#include "WebKitPermissionRequest.h"
26#include <wtf/glib/WTFGType.h>
27
28using namespace WebKit;
29
30/**
31 * SECTION: WebKitNotificationPermissionRequest
32 * @Short_description: A permission request for displaying web notifications
33 * @Title: WebKitNotificationPermissionRequest
34 * @See_also: #WebKitPermissionRequest, #WebKitWebView
35 *
36 * WebKitNotificationPermissionRequest represents a request for
37 * permission to decide whether WebKit should provide the user with
38 * notifications through the Web Notification API.
39 *
40 * When a WebKitNotificationPermissionRequest is not handled by the user,
41 * it is denied by default.
42 *
43 * Since: 2.8
44 */
45
46static void webkit_permission_request_interface_init(WebKitPermissionRequestIface*);
47
48struct _WebKitNotificationPermissionRequestPrivate {
49 RefPtr<NotificationPermissionRequest> request;
50 bool madeDecision;
51};
52
53WEBKIT_DEFINE_TYPE_WITH_CODE(
54 WebKitNotificationPermissionRequest, webkit_notification_permission_request, G_TYPE_OBJECT,
55 G_IMPLEMENT_INTERFACE(WEBKIT_TYPE_PERMISSION_REQUEST, webkit_permission_request_interface_init))
56
57static void webkitNotificationPermissionRequestAllow(WebKitPermissionRequest* request)
58{
59 ASSERT(WEBKIT_IS_NOTIFICATION_PERMISSION_REQUEST(request));
60
61 WebKitNotificationPermissionRequestPrivate* priv = WEBKIT_NOTIFICATION_PERMISSION_REQUEST(request)->priv;
62
63 // Only one decision at a time.
64 if (priv->madeDecision)
65 return;
66
67 priv->request->allow();
68 priv->madeDecision = true;
69}
70
71static void webkitNotificationPermissionRequestDeny(WebKitPermissionRequest* request)
72{
73 ASSERT(WEBKIT_IS_NOTIFICATION_PERMISSION_REQUEST(request));
74
75 WebKitNotificationPermissionRequestPrivate* priv = WEBKIT_NOTIFICATION_PERMISSION_REQUEST(request)->priv;
76
77 // Only one decision at a time.
78 if (priv->madeDecision)
79 return;
80
81 priv->request->deny();
82 priv->madeDecision = true;
83}
84
85static void webkit_permission_request_interface_init(WebKitPermissionRequestIface* iface)
86{
87 iface->allow = webkitNotificationPermissionRequestAllow;
88 iface->deny = webkitNotificationPermissionRequestDeny;
89}
90
91static void webkitNotificationPermissionRequestDispose(GObject* object)
92{
93 // Default behaviour when no decision has been made is denying the request.
94 webkitNotificationPermissionRequestDeny(WEBKIT_PERMISSION_REQUEST(object));
95 G_OBJECT_CLASS(webkit_notification_permission_request_parent_class)->dispose(object);
96}
97
98static void webkit_notification_permission_request_class_init(WebKitNotificationPermissionRequestClass* klass)
99{
100 GObjectClass* objectClass = G_OBJECT_CLASS(klass);
101 objectClass->dispose = webkitNotificationPermissionRequestDispose;
102}
103
104WebKitNotificationPermissionRequest* webkitNotificationPermissionRequestCreate(NotificationPermissionRequest* request)
105{
106 WebKitNotificationPermissionRequest* notificationPermissionRequest = WEBKIT_NOTIFICATION_PERMISSION_REQUEST(g_object_new(WEBKIT_TYPE_NOTIFICATION_PERMISSION_REQUEST, nullptr));
107 notificationPermissionRequest->priv->request = request;
108 return notificationPermissionRequest;
109}
110