From a1b1a30f8a83c8817c9678dc30d877747eee077b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Tue, 23 May 2023 17:02:02 +0200 Subject: [PATCH] permissions: Remove need to check QT_CONFIG(permissions) in user code The idea was to avoid having to define return values for platforms that did not have a permission backend (yet), but the approach does not work for QML, and was a bit awkward in C++ as well. We now return Qt::PermissionStatus::Granted on all platforms without a permission backend, which allows the user code to have a single code path for checking and requesting permissions. Importantly, that code path will not be any different now that we always grant the permission then what it was when the user ifdefed out the permission check via QT_CONFIG(permissions). Task-number: QTBUG-90498 Change-Id: I564a24ccfd6b335cb90b7a621778fba61d53a56c Reviewed-by: Assam Boudjelthia Reviewed-by: Volker Hilsheimer --- src/corelib/configure.cmake | 1 - src/corelib/kernel/qpermissions.cpp | 30 ++++++++++++++++++++++------- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/corelib/configure.cmake b/src/corelib/configure.cmake index dd89b8ac998..746ac8927ea 100644 --- a/src/corelib/configure.cmake +++ b/src/corelib/configure.cmake @@ -1016,7 +1016,6 @@ qt_feature("permissions" PUBLIC SECTION "Utilities" LABEL "Application permissions" PURPOSE "Provides support for requesting user permission to access restricted data or APIs" - CONDITION APPLE OR ANDROID OR WASM ) qt_configure_add_summary_section(NAME "Qt Core") diff --git a/src/corelib/kernel/qpermissions.cpp b/src/corelib/kernel/qpermissions.cpp index 0d15901948b..46b989c50c2 100644 --- a/src/corelib/kernel/qpermissions.cpp +++ b/src/corelib/kernel/qpermissions.cpp @@ -46,7 +46,6 @@ Q_LOGGING_CATEGORY(lcPermissions, "qt.permissions", QtWarningMsg); \code void VoiceMemoWidget::onRecordingInitiated() { - #if QT_CONFIG(permissions) QMicrophonePermission microphonePermission; switch (qApp->checkPermission(microphonePermission)) { case Qt::PermissionStatus::Undetermined: @@ -57,10 +56,8 @@ Q_LOGGING_CATEGORY(lcPermissions, "qt.permissions", QtWarningMsg); m_permissionInstructionsDialog->show(); return; case Qt::PermissionStatus::Granted: - break; // Proceed + m_microphone->startRecording(); } - #endif - m_microphone->startRecording(); } \endcode @@ -76,9 +73,6 @@ Q_LOGGING_CATEGORY(lcPermissions, "qt.permissions", QtWarningMsg); why we can not record voice memos at this time (if the permission was denied), or proceed to using the microphone (if permission was granted). - The use of the \c{QT_CONFIG(permissions)} macro ensures that the code - will work as before on platforms where permissions are not available. - \note On \macOS and iOS permissions can currently only be requested for GUI applications. @@ -672,6 +666,28 @@ QDebug operator<<(QDebug debug, const QPermission &permission) #undef QT_PERMISSION_IMPL_COMMON +#if !defined(Q_OS_DARWIN) && !defined(Q_OS_ANDROID) && !defined(Q_OS_WASM) +// Default backend for platforms without a permission implementation. +// Always returns Granted, to match behavior when not using permission APIs +// https://bugreports.qt.io/browse/QTBUG-90498?focusedCommentId=725085#comment-725085 +namespace QPermissions::Private +{ + Qt::PermissionStatus checkPermission(const QPermission &permission) + { + qCDebug(lcPermissions) << "No permission backend on this platform." + << "Optimistically returning Granted for" << permission; + return Qt::PermissionStatus::Granted; + } + + void requestPermission(const QPermission &permission, const PermissionCallback &callback) + { + qCDebug(lcPermissions) << "No permission backend on this platform." + << "Optimistically returning Granted for" << permission; + callback(Qt::PermissionStatus::Granted); + } +} +#endif + QT_END_NAMESPACE #include "moc_qpermissions.cpp"