summaryrefslogtreecommitdiffstats
path: root/src/corelib/platform/darwin/qdarwinpermissionplugin.mm
blob: 5c527f396c804702f0b01894e2a082f1ca03bd90 (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
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "qdarwinpermissionplugin_p.h"

QT_BEGIN_NAMESPACE

QDarwinPermissionPlugin::QDarwinPermissionPlugin(QDarwinPermissionHandler *handler)
    : QPermissionPlugin()
    , m_handler(handler)
{
}

QDarwinPermissionPlugin::~QDarwinPermissionPlugin()
{
    [m_handler release];
}

Qt::PermissionStatus QDarwinPermissionPlugin::checkPermission(const QPermission &permission)
{
    return [m_handler checkPermission:permission];
}

void QDarwinPermissionPlugin::requestPermission(const QPermission &permission, const PermissionCallback &callback)
{
    if (!verifyUsageDescriptions(permission)) {
        callback(Qt::PermissionStatus::Denied);
        return;
    }

    [m_handler requestPermission:permission withCallback:[=](Qt::PermissionStatus status) {
        // In case the callback comes in on a secondary thread we need to marshal it
        // back to the main thread. And if it doesn't, we still want to propagate it
        // via an event, to avoid any GCD locks deadlocking the application on iOS
        // if the user responds to the result by running a nested event loop.
        // Luckily Qt::QueuedConnection gives us exactly what we need.
        QMetaObject::invokeMethod(this, "permissionUpdated", Qt::QueuedConnection,
            Q_ARG(Qt::PermissionStatus, status), Q_ARG(PermissionCallback, callback));
    }];
}

void QDarwinPermissionPlugin::permissionUpdated(Qt::PermissionStatus status, const PermissionCallback &callback)
{
    callback(status);
}

bool QDarwinPermissionPlugin::verifyUsageDescriptions(const QPermission &permission)
{
    // FIXME: Look up the responsible process and inspect that,
    // as that's what needs to have the usage descriptions.
    // FIXME: Verify entitlements if the process is sandboxed.
    auto *infoDictionary = NSBundle.mainBundle.infoDictionary;
    for (auto description : [m_handler usageDescriptionsFor:permission]) {
        if (!infoDictionary[description.toNSString()]) {
            qCWarning(lcPermissions) <<
                "Requesting" << permission.type().name() <<
                "requires" << description << "in Info.plist";
            return false;
        }
    }
    return true;
}

QT_END_NAMESPACE

QT_USE_NAMESPACE

@implementation QDarwinPermissionHandler

- (Qt::PermissionStatus)checkPermission:(QPermission)permission
{
    Q_UNREACHABLE(); // All handlers should at least provide a check
}

- (void)requestPermission:(QPermission)permission withCallback:(PermissionCallback)callback
{
    Q_UNUSED(permission);
    qCWarning(lcPermissions).nospace() << "Could not request " << permission.type().name() << ". "
        << "Please make sure you have included the required usage description in your Info.plist";
    callback(Qt::PermissionStatus::Denied);
}

- (QStringList)usageDescriptionsFor:(QPermission)permission
{
    return {};
}

@end

#include "moc_qdarwinpermissionplugin_p.cpp"