summaryrefslogtreecommitdiffstats
path: root/src/core/permission_manager_qt.cpp
blob: 1562a02c2d3009bd9f743c06edccbed9a9864840 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtWebEngine module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "permission_manager_qt.h"

#include "content/browser/renderer_host/render_view_host_delegate.h"
#include "content/public/browser/permission_type.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"

#include "type_conversion.h"
#include "web_contents_delegate_qt.h"

namespace QtWebEngineCore {

BrowserContextAdapter::PermissionType toQt(content::PermissionType type)
{
    switch (type) {
    case content::PermissionType::GEOLOCATION:
        return BrowserContextAdapter::GeolocationPermission;
    case content::PermissionType::NOTIFICATIONS:
    case content::PermissionType::MIDI_SYSEX:
    case content::PermissionType::PUSH_MESSAGING:
    case content::PermissionType::PROTECTED_MEDIA_IDENTIFIER:
    case content::PermissionType::MIDI:
    case content::PermissionType::DURABLE_STORAGE:
    case content::PermissionType::AUDIO_CAPTURE:
    case content::PermissionType::VIDEO_CAPTURE:
    case content::PermissionType::NUM:
        break;
    }
    return BrowserContextAdapter::UnsupportedPermission;
}

PermissionManagerQt::PermissionManagerQt(BrowserContextAdapter *contextAdapter)
    : m_contextAdapter(contextAdapter)
    , m_requestIdCount(0)
    , m_subscriberIdCount(0)
{
}

PermissionManagerQt::~PermissionManagerQt()
{
}

void PermissionManagerQt::permissionRequestReply(const QUrl &origin, BrowserContextAdapter::PermissionType type, bool reply)
{
    QPair<QUrl, BrowserContextAdapter::PermissionType> key(origin, type);
    m_permissions[key] = reply;
    content::PermissionStatus status = reply ? content::PERMISSION_STATUS_GRANTED : content::PERMISSION_STATUS_DENIED;
    auto it = m_requests.begin();
    while (it != m_requests.end()) {
        if (it->origin == origin && it->type == type) {
            it->callback.Run(status);
            it = m_requests.erase(it);
        } else
            ++it;
    }
    Q_FOREACH (const RequestOrSubscription &subscriber, m_subscribers) {
        if (subscriber.origin == origin && subscriber.type == type)
            subscriber.callback.Run(status);
    }
}

int PermissionManagerQt::RequestPermission(content::PermissionType permission,
                                            content::RenderFrameHost *frameHost,
                                            const GURL& requesting_origin,
                                            bool user_gesture,
                                            const base::Callback<void(content::PermissionStatus)>& callback)
{
    Q_UNUSED(user_gesture);
    int request_id = ++m_requestIdCount;
    BrowserContextAdapter::PermissionType permissionType = toQt(permission);
    if (permissionType == BrowserContextAdapter::UnsupportedPermission) {
        callback.Run(content::PERMISSION_STATUS_DENIED);
        return kNoPendingOperation;
    }

    content::WebContents *webContents = frameHost->GetRenderViewHost()->GetDelegate()->GetAsWebContents();
    WebContentsDelegateQt* contentsDelegate = static_cast<WebContentsDelegateQt*>(webContents->GetDelegate());
    Q_ASSERT(contentsDelegate);
    RequestOrSubscription request = {
        permissionType,
        toQt(requesting_origin),
        callback
    };
    m_requests.insert(request_id, request);
    if (permissionType == BrowserContextAdapter::GeolocationPermission)
        contentsDelegate->requestGeolocationPermission(request.origin);
    return request_id;
}

int PermissionManagerQt::RequestPermissions(const std::vector<content::PermissionType>& permissions,
                                            content::RenderFrameHost* frameHost,
                                            const GURL& requesting_origin,
                                            bool user_gesture,
                                            const base::Callback<void(const std::vector<content::PermissionStatus>&)>& callback)
{
    NOTIMPLEMENTED() << "RequestPermissions has not been implemented in QtWebEngine";
    Q_UNUSED(user_gesture);
    Q_UNUSED(frameHost);

    std::vector<content::PermissionStatus> result(permissions.size());
    for (content::PermissionType permission : permissions) {
        const BrowserContextAdapter::PermissionType permissionType = toQt(permission);
        if (permissionType == BrowserContextAdapter::UnsupportedPermission)
            result.push_back(content::PERMISSION_STATUS_DENIED);
        else {
            QPair<QUrl, BrowserContextAdapter::PermissionType> key(toQt(requesting_origin), permissionType);
            // TODO: Request permission from UI
            if (m_permissions.contains(key) && m_permissions[key])
                result.push_back(content::PERMISSION_STATUS_GRANTED);
            else
                result.push_back(content::PERMISSION_STATUS_DENIED);
        }
    }

    callback.Run(result);
    return kNoPendingOperation;
}

void PermissionManagerQt::CancelPermissionRequest(int request_id)
{
    // Should we add API to cancel permissions in the UI level?
    m_requests.remove(request_id);
}

content::PermissionStatus PermissionManagerQt::GetPermissionStatus(
    content::PermissionType permission,
    const GURL& requesting_origin,
    const GURL& /*embedding_origin*/)
{
    const BrowserContextAdapter::PermissionType permissionType = toQt(permission);
    if (permissionType == BrowserContextAdapter::UnsupportedPermission)
        return content::PERMISSION_STATUS_DENIED;

    QPair<QUrl, BrowserContextAdapter::PermissionType> key(toQt(requesting_origin), permissionType);
    if (!m_permissions.contains(key))
        return content::PERMISSION_STATUS_ASK;
    if (m_permissions[key])
        return content::PERMISSION_STATUS_GRANTED;
    return content::PERMISSION_STATUS_DENIED;
}

void PermissionManagerQt::ResetPermission(
    content::PermissionType permission,
    const GURL& requesting_origin,
    const GURL& /*embedding_origin*/)
{
    const BrowserContextAdapter::PermissionType permissionType = toQt(permission);
    if (permissionType == BrowserContextAdapter::UnsupportedPermission)
        return;

    QPair<QUrl, BrowserContextAdapter::PermissionType> key(toQt(requesting_origin), permissionType);
    m_permissions.remove(key);
}

void PermissionManagerQt::RegisterPermissionUsage(
    content::PermissionType /*permission*/,
    const GURL& /*requesting_origin*/,
    const GURL& /*embedding_origin*/)
{
    // We do not currently track which permissions are used.
}

int PermissionManagerQt::SubscribePermissionStatusChange(
    content::PermissionType permission,
    const GURL& requesting_origin,
    const GURL& /*embedding_origin*/,
    const base::Callback<void(content::PermissionStatus)>& callback)
{
    int subscriber_id = ++m_subscriberIdCount;
    RequestOrSubscription subscriber = {
        toQt(permission),
        toQt(requesting_origin),
        callback
    };
    m_subscribers.insert(subscriber_id, subscriber);
    return subscriber_id;
}

void PermissionManagerQt::UnsubscribePermissionStatusChange(int subscription_id)
{
    if (!m_subscribers.remove(subscription_id))
        qWarning() << "PermissionManagerQt::UnsubscribePermissionStatusChange called on unknown subscription id" << subscription_id;
}

} // namespace QtWebEngineCore