diff --git a/Configurations/ConfigCommon.xcconfig b/Configurations/ConfigCommon.xcconfig index 5e36bdaf..f77a7b42 100644 --- a/Configurations/ConfigCommon.xcconfig +++ b/Configurations/ConfigCommon.xcconfig @@ -60,6 +60,7 @@ ARCHS = $(ARCHS_STANDARD) SPARKLE_BUNDLE_IDENTIFIER = org.sparkle-project.Sparkle SPARKLE_RELAUNCH_TOOL_NAME = Autoupdate SPARKLE_INSTALLER_PROGRESS_TOOL_NAME = Updater +SPARKLE_INSTALLER_PROGRESS_TOOL_BUNDLE_ID = $(SPARKLE_BUNDLE_IDENTIFIER).$(SPARKLE_INSTALLER_PROGRESS_TOOL_NAME) // Set these if you want to use custom name for the XPC Services // For the Installer one in particular, this name may show up in an authorization prompt @@ -106,7 +107,7 @@ GCC_INLINES_ARE_PRIVATE_EXTERN = YES PRODUCT_NAME = ${TARGET_NAME} PRODUCT_BUNDLE_IDENTIFIER = org.sparkle-project.Sparkle.${PRODUCT_NAME:rfc1034identifier} -GCC_PREPROCESSOR_DEFINITIONS_COMMON = SPU_OBJC_DIRECT=__attribute__((objc_direct)) SPU_OBJC_DIRECT_MEMBERS=__attribute__((objc_direct_members)) SPARKLE_NORMALIZE_INSTALLED_APPLICATION_NAME=$(SPARKLE_NORMALIZE_INSTALLED_APPLICATION_NAME) SPARKLE_BUILD_UI_BITS=$(SPARKLE_BUILD_UI_BITS) SPARKLE_COPY_LOCALIZATIONS=$(SPARKLE_COPY_LOCALIZATIONS) SPARKLE_BUILD_LEGACY_SUUPDATER=$(SPARKLE_BUILD_LEGACY_SUUPDATER) SPARKLE_BUILD_PACKAGE_SUPPORT=$(SPARKLE_BUILD_PACKAGE_SUPPORT) SPARKLE_BUILD_DMG_SUPPORT=$(SPARKLE_BUILD_DMG_SUPPORT) SPARKLE_BUILD_LEGACY_DELTA_SUPPORT=$(SPARKLE_BUILD_LEGACY_DELTA_SUPPORT) SPARKLE_BUILD_BZIP2_DELTA_SUPPORT=$(SPARKLE_BUILD_BZIP2_DELTA_SUPPORT) SPARKLE_BUILD_LEGACY_DSA_SUPPORT=$(SPARKLE_BUILD_LEGACY_DSA_SUPPORT) GENERATE_APPCAST_BUILD_LEGACY_DSA_SUPPORT=$(GENERATE_APPCAST_BUILD_LEGACY_DSA_SUPPORT) SPARKLE_BUNDLE_IDENTIFIER=\"$(SPARKLE_BUNDLE_IDENTIFIER)\" CURRENT_PROJECT_VERSION=\"$(CURRENT_PROJECT_VERSION)\" MARKETING_VERSION=\"$(MARKETING_VERSION)\" SPARKLE_RELAUNCH_TOOL_NAME=\"$(SPARKLE_RELAUNCH_TOOL_NAME)\" SPARKLE_INSTALLER_PROGRESS_TOOL_NAME=\"$(SPARKLE_INSTALLER_PROGRESS_TOOL_NAME)\" SPARKLE_ICON_NAME=\"$(SPARKLE_ICON_NAME)\" INSTALLER_LAUNCHER_NAME=\"${INSTALLER_LAUNCHER_NAME}\" INSTALLER_LAUNCHER_BUNDLE_ID=\"${INSTALLER_LAUNCHER_BUNDLE_ID}\" INSTALLER_LAUNCHER_XPC_SERVICE_EMBEDDED=$(SPARKLE_EMBED_INSTALLER_LAUNCHER_XPC_SERVICE) INSTALLER_CONNECTION_NAME=\"${INSTALLER_CONNECTION_NAME}\" INSTALLER_CONNECTION_BUNDLE_ID=\"${INSTALLER_CONNECTION_BUNDLE_ID}\" INSTALLER_CONNECTION_XPC_SERVICE_EMBEDDED=$(SPARKLE_EMBED_INSTALLER_CONNECTION_XPC_SERVICE) INSTALLER_STATUS_NAME=\"${INSTALLER_STATUS_NAME}\" INSTALLER_STATUS_BUNDLE_ID=\"${INSTALLER_STATUS_BUNDLE_ID}\" INSTALLER_STATUS_XPC_SERVICE_EMBEDDED=$(SPARKLE_EMBED_INSTALLER_STATUS_XPC_SERVICE) DOWNLOADER_NAME=\"${DOWNLOADER_NAME}\" DOWNLOADER_BUNDLE_ID=\"${DOWNLOADER_BUNDLE_ID}\" DOWNLOADER_XPC_SERVICE_EMBEDDED=$(SPARKLE_EMBED_DOWNLOADER_XPC_SERVICE) +GCC_PREPROCESSOR_DEFINITIONS_COMMON = SPU_OBJC_DIRECT=__attribute__((objc_direct)) SPU_OBJC_DIRECT_MEMBERS=__attribute__((objc_direct_members)) SPARKLE_NORMALIZE_INSTALLED_APPLICATION_NAME=$(SPARKLE_NORMALIZE_INSTALLED_APPLICATION_NAME) SPARKLE_BUILD_UI_BITS=$(SPARKLE_BUILD_UI_BITS) SPARKLE_COPY_LOCALIZATIONS=$(SPARKLE_COPY_LOCALIZATIONS) SPARKLE_BUILD_LEGACY_SUUPDATER=$(SPARKLE_BUILD_LEGACY_SUUPDATER) SPARKLE_BUILD_PACKAGE_SUPPORT=$(SPARKLE_BUILD_PACKAGE_SUPPORT) SPARKLE_BUILD_DMG_SUPPORT=$(SPARKLE_BUILD_DMG_SUPPORT) SPARKLE_BUILD_LEGACY_DELTA_SUPPORT=$(SPARKLE_BUILD_LEGACY_DELTA_SUPPORT) SPARKLE_BUILD_BZIP2_DELTA_SUPPORT=$(SPARKLE_BUILD_BZIP2_DELTA_SUPPORT) SPARKLE_BUILD_LEGACY_DSA_SUPPORT=$(SPARKLE_BUILD_LEGACY_DSA_SUPPORT) GENERATE_APPCAST_BUILD_LEGACY_DSA_SUPPORT=$(GENERATE_APPCAST_BUILD_LEGACY_DSA_SUPPORT) SPARKLE_BUNDLE_IDENTIFIER=\"$(SPARKLE_BUNDLE_IDENTIFIER)\" CURRENT_PROJECT_VERSION=\"$(CURRENT_PROJECT_VERSION)\" MARKETING_VERSION=\"$(MARKETING_VERSION)\" SPARKLE_RELAUNCH_TOOL_NAME=\"$(SPARKLE_RELAUNCH_TOOL_NAME)\" SPARKLE_INSTALLER_PROGRESS_TOOL_NAME=\"$(SPARKLE_INSTALLER_PROGRESS_TOOL_NAME)\" SPARKLE_INSTALLER_PROGRESS_TOOL_BUNDLE_ID=\"$(SPARKLE_INSTALLER_PROGRESS_TOOL_BUNDLE_ID)\" SPARKLE_ICON_NAME=\"$(SPARKLE_ICON_NAME)\" INSTALLER_LAUNCHER_NAME=\"${INSTALLER_LAUNCHER_NAME}\" INSTALLER_LAUNCHER_BUNDLE_ID=\"${INSTALLER_LAUNCHER_BUNDLE_ID}\" INSTALLER_LAUNCHER_XPC_SERVICE_EMBEDDED=$(SPARKLE_EMBED_INSTALLER_LAUNCHER_XPC_SERVICE) INSTALLER_CONNECTION_NAME=\"${INSTALLER_CONNECTION_NAME}\" INSTALLER_CONNECTION_BUNDLE_ID=\"${INSTALLER_CONNECTION_BUNDLE_ID}\" INSTALLER_CONNECTION_XPC_SERVICE_EMBEDDED=$(SPARKLE_EMBED_INSTALLER_CONNECTION_XPC_SERVICE) INSTALLER_STATUS_NAME=\"${INSTALLER_STATUS_NAME}\" INSTALLER_STATUS_BUNDLE_ID=\"${INSTALLER_STATUS_BUNDLE_ID}\" INSTALLER_STATUS_XPC_SERVICE_EMBEDDED=$(SPARKLE_EMBED_INSTALLER_STATUS_XPC_SERVICE) DOWNLOADER_NAME=\"${DOWNLOADER_NAME}\" DOWNLOADER_BUNDLE_ID=\"${DOWNLOADER_BUNDLE_ID}\" DOWNLOADER_XPC_SERVICE_EMBEDDED=$(SPARKLE_EMBED_DOWNLOADER_XPC_SERVICE) CODE_SIGN_IDENTITY = - SDKROOT = macosx diff --git a/Configurations/ConfigInstallerProgress.xcconfig b/Configurations/ConfigInstallerProgress.xcconfig index 0d1a5d43..7ecacec6 100644 --- a/Configurations/ConfigInstallerProgress.xcconfig +++ b/Configurations/ConfigInstallerProgress.xcconfig @@ -2,6 +2,7 @@ INFOPLIST_FILE = Sparkle/InstallerProgress/InstallerProgress-Info.plist PRODUCT_NAME = $(SPARKLE_INSTALLER_PROGRESS_TOOL_NAME) +PRODUCT_BUNDLE_IDENTIFIER = $(SPARKLE_INSTALLER_PROGRESS_TOOL_BUNDLE_ID) GCC_PREPROCESSOR_DEFINITIONS = $(GCC_PREPROCESSOR_DEFINITIONS) BUILDING_SPARKLE=0 BUILDING_SPARKLE_SOURCES_EXTERNALLY=1 SKIP_INSTALL = YES CLANG_ENABLE_MODULES = NO diff --git a/Sparkle/InstallerProgress/InstallerProgressAppController.m b/Sparkle/InstallerProgress/InstallerProgressAppController.m index 34cadf28..83223fbe 100644 --- a/Sparkle/InstallerProgress/InstallerProgressAppController.m +++ b/Sparkle/InstallerProgress/InstallerProgressAppController.m @@ -29,6 +29,12 @@ */ static const NSTimeInterval SUTerminationTimeDelay = 0.3; +#if __MAC_OS_X_VERSION_MAX_ALLOWED < 140000 +@interface NSApplication (ActivationAPIs) +- (void)activate; +@end +#endif + @interface InstallerProgressAppController () @end @@ -367,7 +373,11 @@ static const NSTimeInterval SUTerminationTimeDelay = 0.3; self->_application.applicationIconImage = [SUApplicationInfo bestIconForHost:self->_oldHost]; // Activate ourselves otherwise we will probably be in the background - [self->_application activateIgnoringOtherApps:YES]; + if (@available(macOS 14, *)) { + [self->_application activate]; + } else { + [self->_application activateIgnoringOtherApps:YES]; + } [self->_delegate installerProgressShouldDisplayWithHost:self->_oldHost]; } diff --git a/Sparkle/SPUInstallerDriver.m b/Sparkle/SPUInstallerDriver.m index 5a779b6b..429d6e7c 100644 --- a/Sparkle/SPUInstallerDriver.m +++ b/Sparkle/SPUInstallerDriver.m @@ -40,6 +40,17 @@ @end #endif +// Note: we don't want to directly pull in AppKit here especially if the main application does not need it +@interface NSObject (ActivationAPIs) +// NSApplication ++ (id)sharedApplication; +- (void)yieldActivationToApplication:(id)application; + +// NSRunningApplication ++ (NSArray *)runningApplicationsWithBundleIdentifier:(NSString *)bundleIdentifier; +- (NSURL *)bundleURL; +@end + @interface SPUInstallerDriver () @end @@ -65,6 +76,7 @@ BOOL _relaunch; BOOL _systemDomain; BOOL _aborted; + BOOL _notifiedDelegateInstallationWillFinish; } - (instancetype)initWithHost:(SUHost *)host applicationBundle:(NSBundle *)applicationBundle updater:(id)updater updaterDelegate:(id)updaterDelegate delegate:(nullable id)delegate @@ -338,7 +350,13 @@ hasTargetTerminated = (BOOL)*((const uint8_t *)data.bytes); } - [delegate installerWillFinishInstallationAndRelaunch:_relaunch]; + // If the target was already terminated this may be the first time we notify delegate that installation is about to happen + // Otherwise if the target was requested to be terminated/relaunched by the user this may be the second time + // Avoid re-notifying the delegate twice + if (!_notifiedDelegateInstallationWillFinish) { + _notifiedDelegateInstallationWillFinish = YES; + [delegate installerWillFinishInstallationAndRelaunch:_relaunch]; + } [delegate installerDidStartInstallingWithApplicationTerminated:hasTargetTerminated]; } else if (identifier == SPUInstallationFinishedStage3) { @@ -493,9 +511,11 @@ { assert(_updateItem); + id delegate = _delegate; + if (![self mayUpdateAndRestart]) { - [_delegate installerIsRequestingAbortInstallWithError:nil]; + [delegate installerIsRequestingAbortInstallWithError:nil]; return; } @@ -527,12 +547,58 @@ _relaunch = relaunch; + // If AppKit is loaded, we will yield to our Sparkle progress app + // This will let the system know it should be okay for the progress agent to activate itself (if necessary) + // Note we don't want to directly pull in AppKit here especially if the main application does not need it + if (showUI) { + if (@available(macOS 14, *)) { + // Make sure we are not root before using AppKit API + if (geteuid() != 0) { + Class applicationClass = NSClassFromString(@"NSApplication"); + if ([applicationClass respondsToSelector:@selector(sharedApplication)]) { + NSObject *application = [applicationClass sharedApplication]; + + if ([application respondsToSelector:@selector(yieldActivationToApplication:)]) { + Class runningApplicationClass = NSClassFromString(@"NSRunningApplication"); + if ([runningApplicationClass respondsToSelector:@selector(runningApplicationsWithBundleIdentifier:)]) { + NSArray *runningApplications = [runningApplicationClass runningApplicationsWithBundleIdentifier:@SPARKLE_INSTALLER_PROGRESS_TOOL_BUNDLE_ID]; + + NSString *hostBundleIdentifier = _host.bundle.bundleIdentifier; + + id targetRunningApplication = nil; + for (id runningApplication in runningApplications) { + if ([(NSObject *)runningApplication respondsToSelector:@selector(bundleURL)]) { + NSURL *bundleURL = [(NSObject *)runningApplication bundleURL]; + + if (hostBundleIdentifier != nil && [bundleURL.pathComponents containsObject:hostBundleIdentifier]) { + targetRunningApplication = runningApplication; + break; + } + } + } + + if (targetRunningApplication != nil) { + [application yieldActivationToApplication:targetRunningApplication]; + } + } + } + } + } + } + } + + // The user can request trying to relaunch/quit the app multiple times + // Avoid re-notifying the delegate twice + if (!_notifiedDelegateInstallationWillFinish) { + _notifiedDelegateInstallationWillFinish = YES; + [delegate installerWillFinishInstallationAndRelaunch:relaunch]; + } + uint8_t response[2] = {(uint8_t)relaunch, (uint8_t)showUI}; NSData *responseData = [NSData dataWithBytes:response length:sizeof(response)]; - [_installerConnection handleMessageWithIdentifier:SPUResumeInstallationToStage2 data:responseData]; - // the installer will send us SPUInstallationFinishedStage2 when stage 2 is done + [_installerConnection handleMessageWithIdentifier:SPUResumeInstallationToStage2 data:responseData]; } - (void)cancelUpdate diff --git a/Sparkle/SPUStandardUserDriver.m b/Sparkle/SPUStandardUserDriver.m index 719f5716..2a51d1b9 100644 --- a/Sparkle/SPUStandardUserDriver.m +++ b/Sparkle/SPUStandardUserDriver.m @@ -32,6 +32,12 @@ #import +#if __MAC_OS_X_VERSION_MAX_ALLOWED < 140000 +@interface NSApplication (ActivationAPIs) +- (void)activate; +@end +#endif + // The amount of time the app is allowed to be idle for us to consider showing an update prompt right away when the app is active static const NSTimeInterval SUScheduledUpdateIdleEventLeewayInterval = DEBUG ? 30.0 : 5 * 60.0; @@ -118,12 +124,21 @@ static const NSTimeInterval SUScheduledUpdateIdleEventLeewayInterval = DEBUG ? 3 #pragma mark Update Permission +- (void)_activateApplication SPU_OBJC_DIRECT +{ + if (@available(macOS 14, *)) { + [NSApp activate]; + } else { + [NSApp activateIgnoringOtherApps:YES]; + } +} + - (void)showUpdatePermissionRequest:(SPUUpdatePermissionRequest *)request reply:(void (^)(SUUpdatePermissionResponse *))reply { assert(NSThread.isMainThread); if ([SUApplicationInfo isBackgroundApplication:[NSApplication sharedApplication]]) { - [[NSApplication sharedApplication] activateIgnoringOtherApps:YES]; + [self _activateApplication]; } __weak __typeof__(self) weakSelf = self; @@ -169,7 +184,7 @@ static const NSTimeInterval SUScheduledUpdateIdleEventLeewayInterval = DEBUG ? 3 if (![NSApp isActive]) { // If the user initiated an update check, we should make the app active, // regardless if it's a background running app or not - [NSApp activateIgnoringOtherApps:YES]; + [self _activateApplication]; } [_activeUpdateAlert showWindow:nil]; @@ -301,7 +316,7 @@ static const NSTimeInterval SUScheduledUpdateIdleEventLeewayInterval = DEBUG ? 3 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidBecomeActive:) name:NSApplicationDidBecomeActiveNotification object:NSApp]; } else { if (activatingApp) { - [NSApp activateIgnoringOtherApps:YES]; + [self _activateApplication]; } if (showingUpdateInBack) { @@ -465,7 +480,7 @@ static const NSTimeInterval SUScheduledUpdateIdleEventLeewayInterval = DEBUG ? 3 if (mayNeedToActivateApp && ![NSApp isActive]) { // Make the app active if it's not already active, e.g, from a menu bar extra - [NSApp activateIgnoringOtherApps:YES]; + [self _activateApplication]; } } @@ -527,14 +542,14 @@ static const NSTimeInterval SUScheduledUpdateIdleEventLeewayInterval = DEBUG ? 3 [[_checkingController window] center]; // Force the checking controller to load its window. [_checkingController beginActionWithTitle:SULocalizedStringFromTableInBundle(@"Checking for updates…", SPARKLE_TABLE, sparkleBundle, nil) maxProgressValue:0.0 statusText:nil]; [_checkingController setButtonTitle:SULocalizedStringFromTableInBundle(@"Cancel", SPARKLE_TABLE, sparkleBundle, nil) target:self action:@selector(cancelCheckForUpdates:) isDefault:NO]; - [_checkingController showWindow:self]; // For background applications, obtain focus. // Useful if the update check is requested from another app like System Preferences. - if ([SUApplicationInfo isBackgroundApplication:[NSApplication sharedApplication]]) - { - [NSApp activateIgnoringOtherApps:YES]; + if ([SUApplicationInfo isBackgroundApplication:[NSApplication sharedApplication]]) { + [self _activateApplication]; } + + [_checkingController showWindow:self]; } - (void)closeCheckingWindow SPU_OBJC_DIRECT