From 727dc9879454c0aea3b2cf4aecbd54d95d8ae45b Mon Sep 17 00:00:00 2001 From: Damiano Galassi Date: Sat, 8 Jun 2019 17:46:01 +0200 Subject: MacGui: fix queue sleep and shutdown on 10.14 and under sandbox. --- macosx/HBQueueController.m | 17 +++++++++++++-- macosx/HBUtilities.h | 8 ++++++++ macosx/HBUtilities.m | 48 +++++++++++++++++++++++++++++++++++++++++++ macosx/HandBrake.entitlements | 4 ++++ macosx/Info.plist.m4 | 4 ++++ 5 files changed, 79 insertions(+), 2 deletions(-) diff --git a/macosx/HBQueueController.m b/macosx/HBQueueController.m index bd8564ded..87d0ceefe 100644 --- a/macosx/HBQueueController.m +++ b/macosx/HBQueueController.m @@ -1073,7 +1073,7 @@ static void *HBControllerQueueCoreContext = &HBControllerQueueCoreContext; // Sleep NSDictionary *errorDict; NSAppleScript *scriptObject = [[NSAppleScript alloc] initWithSource: - @"tell application \"Finder\" to sleep"]; + @"tell application \"System Events\" to sleep"]; [scriptObject executeAndReturnError: &errorDict]; } // If Shutdown has been selected @@ -1081,7 +1081,7 @@ static void *HBControllerQueueCoreContext = &HBControllerQueueCoreContext; { // Shut Down NSDictionary *errorDict; - NSAppleScript *scriptObject = [[NSAppleScript alloc] initWithSource:@"tell application \"Finder\" to shut down"]; + NSAppleScript *scriptObject = [[NSAppleScript alloc] initWithSource:@"tell application \"System Events\" to shut down"]; [scriptObject executeAndReturnError: &errorDict]; } } @@ -1219,6 +1219,8 @@ static void *HBControllerQueueCoreContext = &HBControllerQueueCoreContext; { [self.delegate showPreferencesWindow:nil]; } + + [self promptForAppleEventAuthorization]; } else if ([[NSUserDefaults standardUserDefaults] integerForKey:@"HBAlertWhenDone"] == HBDoneActionShutDown) { @@ -1237,6 +1239,17 @@ static void *HBControllerQueueCoreContext = &HBControllerQueueCoreContext; { [self.delegate showPreferencesWindow:nil]; } + + [self promptForAppleEventAuthorization]; + } +} + +- (void)promptForAppleEventAuthorization +{ + HBPrivacyConsentState result = [HBUtilities determinePermissionToAutomateTarget:@"com.apple.systemevents" promptIfNeeded:YES]; + if (result != HBPrivacyConsentStateGranted) + { + [HBUtilities writeToActivityLog:"Failed to get permission to automate system events"]; } } diff --git a/macosx/HBUtilities.h b/macosx/HBUtilities.h index edf25ed73..aa87f4f1b 100644 --- a/macosx/HBUtilities.h +++ b/macosx/HBUtilities.h @@ -70,6 +70,14 @@ NS_ASSUME_NONNULL_BEGIN + (NSString *)iso6392CodeFor:(NSString *)language; + (NSString *)languageCodeForIso6392Code:(NSString *)language; +typedef NS_ENUM(NSUInteger, HBPrivacyConsentState) { + HBPrivacyConsentStateUnknown, + HBPrivacyConsentStateDenied, + HBPrivacyConsentStateGranted, +}; + ++ (HBPrivacyConsentState)determinePermissionToAutomateTarget:(NSString *)bundleIdentifier promptIfNeeded:(BOOL)promptIfNeeded; + @end NS_ASSUME_NONNULL_END diff --git a/macosx/HBUtilities.m b/macosx/HBUtilities.m index 7e95f007a..9a627bf5a 100644 --- a/macosx/HBUtilities.m +++ b/macosx/HBUtilities.m @@ -383,4 +383,52 @@ static NSDateFormatter *_releaseDateFormatter = nil; return @"Unknown"; } +#if __MAC_OS_X_VERSION_MIN_REQUIRED <= __MAC_10_14 +enum { + errAEEventWouldRequireUserConsent = -1744, +}; +#endif + ++ (HBPrivacyConsentState)determinePermissionToAutomateTarget:(NSString *)bundleIdentifier promptIfNeeded:(BOOL)promptIfNeeded +{ + if (@available(macOS 10.14, *)) + { + const char *identifierCString = bundleIdentifier.UTF8String; + AEAddressDesc addressDesc; + OSErr descResult = AECreateDesc(typeApplicationBundleID, identifierCString, strlen(identifierCString), &addressDesc); + + if (descResult == noErr) + { + OSStatus permission = AEDeterminePermissionToAutomateTarget(&addressDesc, typeWildCard, typeWildCard, promptIfNeeded); + AEDisposeDesc(&addressDesc); + + HBPrivacyConsentState result; + + switch (permission) + { + case errAEEventWouldRequireUserConsent: + [HBUtilities writeToActivityLog:"Request user consent for %s.", bundleIdentifier.UTF8String]; + result = HBPrivacyConsentStateUnknown; + break; + case noErr: + [HBUtilities writeToActivityLog:"Permission granted for %s.", bundleIdentifier.UTF8String]; + result = HBPrivacyConsentStateGranted; + break; + case errAEEventNotPermitted: + [HBUtilities writeToActivityLog:"Permission not granted for %s.", bundleIdentifier.UTF8String]; + result = HBPrivacyConsentStateDenied; + break; + case procNotFound: + default: + [HBUtilities writeToActivityLog:"Permission unknown."]; + result = HBPrivacyConsentStateUnknown; + break; + } + return result; + } + } + + return HBPrivacyConsentStateGranted; +} + @end diff --git a/macosx/HandBrake.entitlements b/macosx/HandBrake.entitlements index e29f683e0..46fc9d34a 100644 --- a/macosx/HandBrake.entitlements +++ b/macosx/HandBrake.entitlements @@ -14,6 +14,10 @@ com.apple.security.files.user-selected.read-write + com.apple.security.temporary-exception.apple-events + + com.apple.systemevents + com.apple.security.temporary-exception.files.absolute-path.read-only /usr/local/ diff --git a/macosx/Info.plist.m4 b/macosx/Info.plist.m4 index cb647b9bc..aa142fae4 100644 --- a/macosx/Info.plist.m4 +++ b/macosx/Info.plist.m4 @@ -72,5 +72,9 @@ GPLv2 license. NSUserNotificationAlertStyle alert + NSAppleEventsUsageDescription + HandBrake needs access to shut down or put the computer to sleep + NSAppleScriptEnabled + -- cgit v1.2.3