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 "WebKitGeolocationPermissionRequest.h"
22
23#include "GeolocationPermissionRequestProxy.h"
24#include "WebKitGeolocationPermissionRequestPrivate.h"
25#include "WebKitPermissionRequest.h"
26#include <wtf/glib/WTFGType.h>
27
28using namespace WebKit;
29
30/**
31 * SECTION: WebKitGeolocationPermissionRequest
32 * @Short_description: A permission request for sharing user's location
33 * @Title: WebKitGeolocationPermissionRequest
34 * @See_also: #WebKitPermissionRequest, #WebKitWebView
35 *
36 * WebKitGeolocationPermissionRequest represents a request for
37 * permission to decide whether WebKit should provide the user's
38 * location to a website when requested through the Geolocation API.
39 *
40 * When a WebKitGeolocationPermissionRequest is not handled by the user,
41 * it is denied by default.
42 *
43 * When embedding web views in your application, you *must* configure an
44 * application identifier to allow web content to use geolocation services.
45 * The identifier *must* match the name of the `.desktop` file which describes
46 * the application, sans the suffix.
47 *
48 * If your application uses #GApplication (or any subclass like
49 * #GtkApplication), WebKit will automatically use the identifier returned by
50 * g_application_get_application_id(). This is the recommended approach for
51 * enabling geolocation in applications.
52 *
53 * If an identifier cannot be obtained through #GApplication, the value
54 * returned by g_get_prgname() will be used instead as a fallback. For
55 * programs which cannot use #GApplication, calling g_set_prgname() early
56 * during initialization is needed when the name of the executable on disk
57 * does not match the name of a valid `.desktop` file.
58 */
59
60static void webkit_permission_request_interface_init(WebKitPermissionRequestIface*);
61
62struct _WebKitGeolocationPermissionRequestPrivate {
63 RefPtr<GeolocationPermissionRequest> request;
64 bool madeDecision;
65};
66
67WEBKIT_DEFINE_TYPE_WITH_CODE(
68 WebKitGeolocationPermissionRequest, webkit_geolocation_permission_request, G_TYPE_OBJECT,
69 G_IMPLEMENT_INTERFACE(WEBKIT_TYPE_PERMISSION_REQUEST, webkit_permission_request_interface_init))
70
71static void webkitGeolocationPermissionRequestAllow(WebKitPermissionRequest* request)
72{
73 ASSERT(WEBKIT_IS_GEOLOCATION_PERMISSION_REQUEST(request));
74
75 WebKitGeolocationPermissionRequestPrivate* priv = WEBKIT_GEOLOCATION_PERMISSION_REQUEST(request)->priv;
76
77 // Only one decision at a time.
78 if (priv->madeDecision)
79 return;
80
81 priv->request->allow();
82 priv->madeDecision = true;
83}
84
85static void webkitGeolocationPermissionRequestDeny(WebKitPermissionRequest* request)
86{
87 ASSERT(WEBKIT_IS_GEOLOCATION_PERMISSION_REQUEST(request));
88
89 WebKitGeolocationPermissionRequestPrivate* priv = WEBKIT_GEOLOCATION_PERMISSION_REQUEST(request)->priv;
90
91 // Only one decision at a time.
92 if (priv->madeDecision)
93 return;
94
95 priv->request->deny();
96 priv->madeDecision = true;
97}
98
99static void webkit_permission_request_interface_init(WebKitPermissionRequestIface* iface)
100{
101 iface->allow = webkitGeolocationPermissionRequestAllow;
102 iface->deny = webkitGeolocationPermissionRequestDeny;
103}
104
105static void webkitGeolocationPermissionRequestDispose(GObject* object)
106{
107 // Default behaviour when no decision has been made is denying the request.
108 webkitGeolocationPermissionRequestDeny(WEBKIT_PERMISSION_REQUEST(object));
109 G_OBJECT_CLASS(webkit_geolocation_permission_request_parent_class)->dispose(object);
110}
111
112static void webkit_geolocation_permission_request_class_init(WebKitGeolocationPermissionRequestClass* klass)
113{
114 GObjectClass* objectClass = G_OBJECT_CLASS(klass);
115 objectClass->dispose = webkitGeolocationPermissionRequestDispose;
116}
117
118WebKitGeolocationPermissionRequest* webkitGeolocationPermissionRequestCreate(GeolocationPermissionRequest* request)
119{
120 WebKitGeolocationPermissionRequest* geolocationPermissionRequest = WEBKIT_GEOLOCATION_PERMISSION_REQUEST(g_object_new(WEBKIT_TYPE_GEOLOCATION_PERMISSION_REQUEST, NULL));
121 geolocationPermissionRequest->priv->request = request;
122 return geolocationPermissionRequest;
123}
124