common: Add C++ version of Apple authorization logic. (#6616)
parent
03dbdfc12f
commit
bfb6a5b5de
@ -1,93 +0,0 @@
|
||||
// Copyright 2020 Citra Emulator Project
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#import <AVFoundation/AVFoundation.h>
|
||||
|
||||
#include "citra_qt/macos_authorization.h"
|
||||
#include "common/logging/log.h"
|
||||
|
||||
namespace AppleAuthorization {
|
||||
|
||||
static bool authorized_camera = false;
|
||||
static bool authorized_microphone = false;
|
||||
|
||||
static bool authorized = false;
|
||||
|
||||
enum class AuthMediaType { Camera, Microphone };
|
||||
|
||||
// Based on
|
||||
// https://developer.apple.com/documentation/avfoundation/cameras_and_media_capture/requesting_authorization_for_media_capture_on_macos
|
||||
// TODO: This could be rewritten to return the authorization state, having pure c++ code deal with
|
||||
// it, log information and possibly wait for the camera access request.
|
||||
void CheckAuthorization(AuthMediaType type) {
|
||||
authorized = false;
|
||||
if (@available(macOS 10.14, *)) {
|
||||
NSString* media_type;
|
||||
if (type == AuthMediaType::Camera) {
|
||||
media_type = AVMediaTypeVideo;
|
||||
} else {
|
||||
media_type = AVMediaTypeAudio;
|
||||
}
|
||||
|
||||
// Request permission to access the camera and microphone.
|
||||
switch ([AVCaptureDevice authorizationStatusForMediaType:media_type]) {
|
||||
case AVAuthorizationStatusAuthorized:
|
||||
// The user has previously granted access to the camera.
|
||||
authorized = true;
|
||||
break;
|
||||
case AVAuthorizationStatusNotDetermined: {
|
||||
// The app hasn't yet asked the user for camera access.
|
||||
[AVCaptureDevice requestAccessForMediaType:media_type
|
||||
completionHandler:^(BOOL granted) {
|
||||
authorized = granted;
|
||||
}];
|
||||
if (type == AuthMediaType::Camera) {
|
||||
LOG_INFO(Frontend, "Camera access requested.");
|
||||
} else { // AuthMediaType::Microphone
|
||||
LOG_INFO(Frontend, "Microphone access requested.");
|
||||
}
|
||||
break;
|
||||
}
|
||||
case AVAuthorizationStatusDenied: {
|
||||
// The user has previously denied access.
|
||||
authorized = false;
|
||||
if (type == AuthMediaType::Camera) {
|
||||
LOG_WARNING(Frontend, "Camera access denied. To change this you may modify the "
|
||||
"macOS system permission settings "
|
||||
"for Citra at 'System Preferences -> Security & Privacy'");
|
||||
} else { // AuthMediaType::Microphone
|
||||
LOG_WARNING(Frontend, "Microphone access denied. To change this you may modify the "
|
||||
"macOS system permission settings "
|
||||
"for Citra at 'System Preferences -> Security & Privacy'");
|
||||
}
|
||||
return;
|
||||
}
|
||||
case AVAuthorizationStatusRestricted: {
|
||||
// The user can't grant access due to restrictions.
|
||||
authorized = false;
|
||||
return;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
authorized = true;
|
||||
}
|
||||
}
|
||||
|
||||
bool CheckAuthorizationForCamera() {
|
||||
if (!authorized_camera) {
|
||||
CheckAuthorization(AuthMediaType::Camera);
|
||||
authorized_camera = authorized;
|
||||
}
|
||||
return authorized_camera;
|
||||
}
|
||||
|
||||
bool CheckAuthorizationForMicrophone() {
|
||||
if (!authorized_microphone) {
|
||||
CheckAuthorization(AuthMediaType::Microphone);
|
||||
authorized_microphone = authorized;
|
||||
}
|
||||
return authorized_microphone;
|
||||
}
|
||||
|
||||
} // namespace AppleAuthorization
|
@ -0,0 +1,83 @@
|
||||
// Copyright 2023 Citra Emulator Project
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#include <future>
|
||||
#include <objc/message.h>
|
||||
#include "common/apple_authorization.h"
|
||||
#include "common/logging/log.h"
|
||||
|
||||
namespace AppleAuthorization {
|
||||
|
||||
// Bindings to Objective-C APIs
|
||||
|
||||
using NSString = void;
|
||||
using AVMediaType = NSString*;
|
||||
enum AVAuthorizationStatus : int {
|
||||
AVAuthorizationStatusNotDetermined = 0,
|
||||
AVAuthorizationStatusRestricted,
|
||||
AVAuthorizationStatusDenied,
|
||||
AVAuthorizationStatusAuthorized,
|
||||
};
|
||||
|
||||
typedef NSString* (*send_stringWithUTF8String)(Class, SEL, const char*);
|
||||
typedef AVAuthorizationStatus (*send_authorizationStatusForMediaType)(Class, SEL, AVMediaType);
|
||||
typedef void (*send_requestAccessForMediaType_completionHandler)(Class, SEL, AVMediaType,
|
||||
void (^callback)(bool));
|
||||
|
||||
NSString* StringToNSString(const std::string_view string) {
|
||||
return reinterpret_cast<send_stringWithUTF8String>(objc_msgSend)(
|
||||
objc_getClass("NSString"), sel_registerName("stringWithUTF8String:"), string.data());
|
||||
}
|
||||
|
||||
AVAuthorizationStatus GetAuthorizationStatus(AVMediaType media_type) {
|
||||
return reinterpret_cast<send_authorizationStatusForMediaType>(objc_msgSend)(
|
||||
objc_getClass("AVCaptureDevice"), sel_registerName("authorizationStatusForMediaType:"),
|
||||
media_type);
|
||||
}
|
||||
|
||||
void RequestAccess(AVMediaType media_type, void (^callback)(bool)) {
|
||||
reinterpret_cast<send_requestAccessForMediaType_completionHandler>(objc_msgSend)(
|
||||
objc_getClass("AVCaptureDevice"),
|
||||
sel_registerName("requestAccessForMediaType:completionHandler:"), media_type, callback);
|
||||
}
|
||||
|
||||
static AVMediaType AVMediaTypeAudio = StringToNSString("soun");
|
||||
static AVMediaType AVMediaTypeVideo = StringToNSString("vide");
|
||||
|
||||
// Authorization Logic
|
||||
|
||||
bool CheckAuthorization(AVMediaType type, const std::string_view& type_name) {
|
||||
switch (GetAuthorizationStatus(type)) {
|
||||
case AVAuthorizationStatusNotDetermined: {
|
||||
LOG_INFO(Frontend, "Requesting {} permission.", type_name);
|
||||
__block std::promise<bool> authorization_promise;
|
||||
std::future<bool> authorization_future = authorization_promise.get_future();
|
||||
RequestAccess(type, ^(bool granted) {
|
||||
LOG_INFO(Frontend, "{} permission request result: {}", type_name, granted);
|
||||
authorization_promise.set_value(granted);
|
||||
});
|
||||
return authorization_future.get();
|
||||
}
|
||||
case AVAuthorizationStatusAuthorized:
|
||||
return true;
|
||||
case AVAuthorizationStatusDenied:
|
||||
LOG_WARNING(Frontend,
|
||||
"{} permission has been denied and must be enabled via System Settings.",
|
||||
type_name);
|
||||
return false;
|
||||
case AVAuthorizationStatusRestricted:
|
||||
LOG_WARNING(Frontend, "{} permission is restricted by the system.", type_name);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool CheckAuthorizationForCamera() {
|
||||
return CheckAuthorization(AVMediaTypeVideo, "Camera");
|
||||
}
|
||||
|
||||
bool CheckAuthorizationForMicrophone() {
|
||||
return CheckAuthorization(AVMediaTypeAudio, "Microphone");
|
||||
}
|
||||
|
||||
} // namespace AppleAuthorization
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2020 Citra Emulator Project
|
||||
// Copyright 2023 Citra Emulator Project
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included.
|
||||
|
Loading…
Reference in New Issue