From 5a0529a2cd8f9b8381062b307a3db8ac10d93f35 Mon Sep 17 00:00:00 2001 From: Zorg Date: Sun, 28 Mar 2021 23:32:51 -0700 Subject: [PATCH] Provide option to open host bundle path instead of app path This is useful for plug-ins that may want Sparkle to open eg a .prefpane or .saver instead of re-launching System Preferences (which is the app used to wait for termination; normally we only relaunch an app that was proven to be alive). In a plug-in world, it may not be possible to tell which is more desirable so we provide both options. In 1.x, there was a delegate -pathToRelaunchForUpdater: method to pick between these, however in 2.x for hardening sake, there isn't an API to open any arbitrary bundle path so we provide explicit options. --- Configurations/ConfigCommon.xcconfig | 1 + .../InstallerProgressAppController.m | 38 ++++++++++++------- Sparkle/SPUUpdater.h | 2 +- Sparkle/SUConstants.h | 2 +- Sparkle/SUConstants.m | 2 +- 5 files changed, 29 insertions(+), 16 deletions(-) diff --git a/Configurations/ConfigCommon.xcconfig b/Configurations/ConfigCommon.xcconfig index 0ae30b1b..04ee7259 100644 --- a/Configurations/ConfigCommon.xcconfig +++ b/Configurations/ConfigCommon.xcconfig @@ -13,6 +13,7 @@ SPARKLE_AUTOMATED_DOWNGRADES = 0 // following on to always reset the name back to "MyApp" // If you are using this option to change the name of your app, you should // disable the option again when you no longer need it +// Note this option is only supported for regular application updates and not plug-ins SPARKLE_NORMALIZE_INSTALLED_APPLICATION_NAME = 0 SPARKLE_ICON_NAME = AppIcon diff --git a/Sparkle/InstallerProgress/InstallerProgressAppController.m b/Sparkle/InstallerProgress/InstallerProgressAppController.m index 425a63c5..5e698aea 100644 --- a/Sparkle/InstallerProgress/InstallerProgressAppController.m +++ b/Sparkle/InstallerProgress/InstallerProgressAppController.m @@ -18,6 +18,7 @@ #import "SUHost.h" #import "SUErrors.h" #import "SUNormalization.h" +#import "SUConstants.h" /*! * Terminate the application after a delay from launching the new update to avoid OS activation issues @@ -33,7 +34,9 @@ static const NSTimeInterval SUTerminationTimeDelay = 0.3; @property (nonatomic, readonly) NSXPCConnection *connection; @property (nonatomic) BOOL connected; @property (nonatomic) BOOL repliedToRegistration; -@property (nonatomic, readonly) NSBundle *hostBundle; +@property (nonatomic, readonly) SUHost *oldHost; +@property (nonatomic, readonly) BOOL shouldRelaunchHostBundle; +@property (nonatomic, readonly) NSString *oldHostBundlePath; @property (nonatomic) StatusInfo *statusInfo; @property (nonatomic) BOOL submittedLauncherJob; @property (nonatomic) BOOL willTerminate; @@ -52,7 +55,9 @@ static const NSTimeInterval SUTerminationTimeDelay = 0.3; @synthesize connection = _connection; @synthesize connected = _connected; @synthesize repliedToRegistration = _repliedToRegistration; -@synthesize hostBundle = _hostBundle; +@synthesize oldHost = _oldHost; +@synthesize shouldRelaunchHostBundle = _shouldRelaunchHostBundle; +@synthesize oldHostBundlePath = _oldHostBundlePath; @synthesize statusInfo = _statusInfo; @synthesize submittedLauncherJob = _submittedLauncherJob; @synthesize willTerminate = _willTerminate; @@ -75,19 +80,25 @@ static const NSTimeInterval SUTerminationTimeDelay = 0.3; [self cleanupAndExitWithStatus:EXIT_FAILURE error:nil]; } - _hostBundle = [NSBundle bundleWithPath:hostBundlePath]; - if (_hostBundle == nil) { + NSBundle *hostBundle = [NSBundle bundleWithPath:hostBundlePath]; + if (hostBundle == nil) { SULog(SULogLevelError, @"Error: Host bundle for target is nil"); [self cleanupAndExitWithStatus:EXIT_FAILURE error:nil]; } - NSString *hostBundleIdentifier = _hostBundle.bundleIdentifier; + NSString *hostBundleIdentifier = hostBundle.bundleIdentifier; if (hostBundleIdentifier == nil) { SULog(SULogLevelError, @"Error: Host bundle identifier for target is nil"); [self cleanupAndExitWithStatus:EXIT_FAILURE error:nil]; return nil; // just to silence analyzer warnings later about hostBundleIdentifier being nil } + SUHost *host = [[SUHost alloc] initWithBundle:hostBundle]; + _shouldRelaunchHostBundle = [host boolForInfoDictionaryKey:SURelaunchHostBundleKey]; + _oldHostBundlePath = host.bundlePath; + + _oldHost = host; + // Note that we are connecting to the installer rather than the installer connecting to us // This difference is significant. We shouldn't have a model where the 'server' tries to connect to a 'client', // nor have a model where a process that runs at the highest level (the installer can run as root) tries to connect to a user level agent or process @@ -221,14 +232,12 @@ static const NSTimeInterval SUTerminationTimeDelay = 0.3; // Compute normalized path that we may use later for relaunching the application // We compute normalized path from progress agent instead of trusting or having the installer // pass it to us - if (SPARKLE_NORMALIZE_INSTALLED_APPLICATION_NAME) { - SUHost *applicationHost = [[SUHost alloc] initWithBundle:applicationBundle]; - - NSString *normalizedPath = SUNormalizedInstallationPath(applicationHost); + if (SPARKLE_NORMALIZE_INSTALLED_APPLICATION_NAME && [applicationBundle.bundlePath isEqualToString:self.oldHostBundlePath]) { + NSString *normalizedPath = SUNormalizedInstallationPath(self.oldHost); // We only use normalized path if it doesn't already exist // Check the installer which has the same logic if (![[NSFileManager defaultManager] fileExistsAtPath:normalizedPath]) { - self.normalizedPath = SUNormalizedInstallationPath(applicationHost); + self.normalizedPath = SUNormalizedInstallationPath(self.oldHost); } } @@ -280,6 +289,9 @@ static const NSTimeInterval SUTerminationTimeDelay = 0.3; NSString *pathToRelaunch; if (self.normalizedPath != nil) { pathToRelaunch = self.normalizedPath; + } else if (self.shouldRelaunchHostBundle) { + // Use self.oldHostBundlePath because it was computed before self.oldHost could have been removed + pathToRelaunch = self.oldHostBundlePath; } else { pathToRelaunch = self.applicationBundle.bundlePath; } @@ -290,6 +302,7 @@ static const NSTimeInterval SUTerminationTimeDelay = 0.3; [self cleanupAndExitWithStatus:EXIT_FAILURE error:[NSError errorWithDomain:SUSparkleErrorDomain code:SUAgentInvalidationError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Error: Encountered invalid path to relaunch: %@", pathToRelaunch] }]]; } + // Note: we can launch application bundles or open plug-in bundles if (![[NSWorkspace sharedWorkspace] openFile:pathToRelaunch]) { SULog(SULogLevelError, @"Error: Failed to relaunch bundle at %@", pathToRelaunch); } @@ -314,13 +327,12 @@ static const NSTimeInterval SUTerminationTimeDelay = 0.3; TransformProcessType(&psn, kProcessTransformToForegroundApplication); // Note: the application icon needs to be set after showing the icon in the dock - SUHost *host = [[SUHost alloc] initWithBundle:self.hostBundle]; - self.application.applicationIconImage = [SUApplicationInfo bestIconForHost:host]; + self.application.applicationIconImage = [SUApplicationInfo bestIconForHost:self.oldHost]; // Activate ourselves otherwise we will probably be in the background [self.application activateIgnoringOtherApps:YES]; - [self.delegate installerProgressShouldDisplayWithHost:host]; + [self.delegate installerProgressShouldDisplayWithHost:self.oldHost]; } }); } diff --git a/Sparkle/SPUUpdater.h b/Sparkle/SPUUpdater.h index 53133f90..92a3241a 100644 --- a/Sparkle/SPUUpdater.h +++ b/Sparkle/SPUUpdater.h @@ -42,7 +42,7 @@ SU_EXPORT @interface SPUUpdater : NSObject Related: See SPUStandardUpdaterController which wraps a SPUUpdater instance and is suitable for instantiating in nib files @param hostBundle The bundle that should be targetted for updating. This must not be nil. - @param applicationBundle The application bundle that should be relaunched and waited for termination. Usually this can be the same as hostBundle. This may differ when updating a plug-in or other non-application bundle. + @param applicationBundle The application bundle that should be waited for termination and relaunched (unless overridden). Usually this can be the same as hostBundle. This may differ when updating a plug-in or other non-application bundle. @param userDriver The user driver that Sparkle uses for user update interaction @param delegate The delegate for SPUUpdater. This may be nil. */ diff --git a/Sparkle/SUConstants.h b/Sparkle/SUConstants.h index 44d3714e..5cca1270 100644 --- a/Sparkle/SUConstants.h +++ b/Sparkle/SUConstants.h @@ -38,7 +38,7 @@ extern NSString *const SUTechnicalErrorInformationKey; extern NSString *const SUFeedURLKey; extern NSString *const SUHasLaunchedBeforeKey; -extern NSString *const SUUpdateRelaunchingMarkerKey; +extern NSString *const SURelaunchHostBundleKey; extern NSString *const SUShowReleaseNotesKey; extern NSString *const SUSkippedVersionKey; extern NSString *const SUScheduledCheckIntervalKey; diff --git a/Sparkle/SUConstants.m b/Sparkle/SUConstants.m index e5289cb5..d46d1dd2 100644 --- a/Sparkle/SUConstants.m +++ b/Sparkle/SUConstants.m @@ -34,7 +34,7 @@ NSString *const SUTechnicalErrorInformationKey = @"SUTechnicalErrorInformation"; NSString *const SUFeedURLKey = @"SUFeedURL"; NSString *const SUHasLaunchedBeforeKey = @"SUHasLaunchedBefore"; -NSString *const SUUpdateRelaunchingMarkerKey = @"SUUpdateRelaunchingMarker"; +NSString *const SURelaunchHostBundleKey = @"SURelaunchHostBundle"; NSString *const SUShowReleaseNotesKey = @"SUShowReleaseNotes"; NSString *const SUSkippedVersionKey = @"SUSkippedVersion"; NSString *const SUScheduledCheckIntervalKey = @"SUScheduledCheckInterval";