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
27using namespace WebKit;
28
29/**
30 * SECTION: WebKitGeolocationPermissionRequest
31 * @Short_description: A permission request for sharing user's location
32 * @Title: WebKitGeolocationPermissionRequest
33 * @See_also: #WebKitPermissionRequest, #WebKitWebView
34 *
35 * WebKitGeolocationPermissionRequest represents a request for
36 * permission to decide whether WebKit should provide the user's
37 * location to a website when requested throught the Geolocation API.
38 */
39
40static void webkit_permission_request_interface_init(WebKitPermissionRequestIface*);
41
42struct _WebKitGeolocationPermissionRequestPrivate {
43    RefPtr<GeolocationPermissionRequestProxy> request;
44    bool madeDecision;
45};
46
47WEBKIT_DEFINE_TYPE_WITH_CODE(
48    WebKitGeolocationPermissionRequest, webkit_geolocation_permission_request, G_TYPE_OBJECT,
49    G_IMPLEMENT_INTERFACE(WEBKIT_TYPE_PERMISSION_REQUEST, webkit_permission_request_interface_init))
50
51static void webkitGeolocationPermissionRequestAllow(WebKitPermissionRequest* request)
52{
53    ASSERT(WEBKIT_IS_GEOLOCATION_PERMISSION_REQUEST(request));
54
55    WebKitGeolocationPermissionRequestPrivate* priv = WEBKIT_GEOLOCATION_PERMISSION_REQUEST(request)->priv;
56
57    // Only one decision at a time.
58    if (priv->madeDecision)
59        return;
60
61    priv->request->allow();
62    priv->madeDecision = true;
63}
64
65static void webkitGeolocationPermissionRequestDeny(WebKitPermissionRequest* request)
66{
67    ASSERT(WEBKIT_IS_GEOLOCATION_PERMISSION_REQUEST(request));
68
69    WebKitGeolocationPermissionRequestPrivate* priv = WEBKIT_GEOLOCATION_PERMISSION_REQUEST(request)->priv;
70
71    // Only one decision at a time.
72    if (priv->madeDecision)
73        return;
74
75    priv->request->deny();
76    priv->madeDecision = true;
77}
78
79static void webkit_permission_request_interface_init(WebKitPermissionRequestIface* iface)
80{
81    iface->allow = webkitGeolocationPermissionRequestAllow;
82    iface->deny = webkitGeolocationPermissionRequestDeny;
83}
84
85static void webkitGeolocationPermissionRequestDispose(GObject* object)
86{
87    // Default behaviour when no decision has been made is denying the request.
88    webkitGeolocationPermissionRequestDeny(WEBKIT_PERMISSION_REQUEST(object));
89    G_OBJECT_CLASS(webkit_geolocation_permission_request_parent_class)->dispose(object);
90}
91
92static void webkit_geolocation_permission_request_class_init(WebKitGeolocationPermissionRequestClass* klass)
93{
94    GObjectClass* objectClass = G_OBJECT_CLASS(klass);
95    objectClass->dispose = webkitGeolocationPermissionRequestDispose;
96}
97
98WebKitGeolocationPermissionRequest* webkitGeolocationPermissionRequestCreate(GeolocationPermissionRequestProxy* request)
99{
100    WebKitGeolocationPermissionRequest* geolocationPermissionRequest = WEBKIT_GEOLOCATION_PERMISSION_REQUEST(g_object_new(WEBKIT_TYPE_GEOLOCATION_PERMISSION_REQUEST, NULL));
101    geolocationPermissionRequest->priv->request = request;
102    return geolocationPermissionRequest;
103}
104