Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qdarwinpermissionplugin.mm
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5
7
8QDarwinPermissionPlugin::QDarwinPermissionPlugin(QDarwinPermissionHandler *handler)
9 : QPermissionPlugin()
10 , m_handler(handler)
11{
12}
13
14QDarwinPermissionPlugin::~QDarwinPermissionPlugin()
15{
16 [m_handler release];
17}
18
19Qt::PermissionStatus QDarwinPermissionPlugin::checkPermission(const QPermission &permission)
20{
21 return [m_handler checkPermission:permission];
22}
23
24void QDarwinPermissionPlugin::requestPermission(const QPermission &permission, const PermissionCallback &callback)
25{
26 if (!verifyUsageDescriptions(permission)) {
27 callback(Qt::PermissionStatus::Denied);
28 return;
29 }
30
31 // QCoreApplication::requestPermission is commonly used with an inline lambda for the
32 // callback object, meaning it won't be alive when the permission is granted later,
33 // so make sure to copy the callback object.
34 [m_handler requestPermission:permission withCallback:[&, callback](Qt::PermissionStatus status) {
35 // In case the callback comes in on a secondary thread we need to marshal it
36 // back to the main thread. And if it doesn't, we still want to propagate it
37 // via an event, to avoid any GCD locks deadlocking the application on iOS
38 // if the user responds to the result by running a nested event loop.
39 // Luckily Qt::QueuedConnection gives us exactly what we need.
40 QMetaObject::invokeMethod(this, "permissionUpdated", Qt::QueuedConnection,
41 Q_ARG(Qt::PermissionStatus, status), Q_ARG(PermissionCallback, callback));
42 }];
43}
44
45void QDarwinPermissionPlugin::permissionUpdated(Qt::PermissionStatus status, const PermissionCallback &callback)
46{
47 callback(status);
48}
49
50bool QDarwinPermissionPlugin::verifyUsageDescriptions(const QPermission &permission)
51{
52 // FIXME: Look up the responsible process and inspect that,
53 // as that's what needs to have the usage descriptions.
54 // FIXME: Verify entitlements if the process is sandboxed.
55 auto *infoDictionary = NSBundle.mainBundle.infoDictionary;
56 for (auto description : [m_handler usageDescriptionsFor:permission]) {
57 if (!infoDictionary[description.toNSString()]) {
58 qCWarning(lcPermissions) <<
59 "Requesting" << permission.type().name() <<
60 "requires" << description << "in Info.plist";
61 return false;
62 }
63 }
64 return true;
65}
66
67QT_END_NAMESPACE
68
69QT_USE_NAMESPACE
70
71@implementation QDarwinPermissionHandler
72
73- (Qt::PermissionStatus)checkPermission:(QPermission)permission
74{
75 Q_UNREACHABLE(); // All handlers should at least provide a check
76}
77
78- (void)requestPermission:(QPermission)permission withCallback:(PermissionCallback)callback
79{
80 Q_UNUSED(permission);
81 qCWarning(lcPermissions).nospace() << "Could not request " << permission.type().name() << ". "
82 << "Please make sure you have included the required usage description in your Info.plist";
83 callback(Qt::PermissionStatus::Denied);
84}
85
86- (QStringList)usageDescriptionsFor:(QPermission)permission
87{
88 return {};
89}
90
91@end
92
93#include "moc_qdarwinpermissionplugin_p.cpp"
Combined button and popup list for selecting options.
QList< QString > QStringList
Constructs a string list that contains the given string, str.