From f2986ef09089bd6e859d845520d857b377a13877 Mon Sep 17 00:00:00 2001 From: Zorg Date: Sun, 28 Mar 2021 13:17:27 -0700 Subject: [PATCH 1/4] Start updater one runloop cycle later And remove logic in SPUStandardUpdaterController and SUUpdater that defers starting the updater one cycle later, because that logic is now in SPUUpdater which they invoke. It is too easy to start the updater "too soon" (if not using SPUStandardUpdaterController or SUUpdater) and I had some weird approach before that allowed the developer to check for updates before starting the updater and it would get deferred until the updater was started (without showing a permission prompt). Instead, defer the update cycle check allowing developer to check for updates immediately after starting the updater if needed (sparkle-cli needs to do this in one case). Errors are now logged when checking for updates without having started the updater. This fixes edge cases of UI being shown too early. --- Sparkle/SPUStandardUpdaterController.m | 6 +---- Sparkle/SPUUpdater.h | 10 +++---- Sparkle/SPUUpdater.m | 37 +++++++++----------------- Sparkle/SUUpdater.m | 14 ++++------ sparkle-cli/SPUCommandLineDriver.m | 7 ++--- 5 files changed, 28 insertions(+), 46 deletions(-) diff --git a/Sparkle/SPUStandardUpdaterController.m b/Sparkle/SPUStandardUpdaterController.m index ba1f0f76..45674d62 100644 --- a/Sparkle/SPUStandardUpdaterController.m +++ b/Sparkle/SPUStandardUpdaterController.m @@ -60,11 +60,7 @@ static NSString *const SUUpdaterDefaultsObservationContext = @"SUUpdaterDefaults self.updater = [[SPUUpdater alloc] initWithHostBundle:hostBundle applicationBundle:hostBundle userDriver:userDriver delegate:self.updaterDelegate]; self.userDriver = userDriver; - // In the case this is being called right as an application is being launched, - // the application may not have finished launching - we shouldn't do anything before the main runloop is started - // Note we can't say, register for an application did finish launching notification - // because we can't assume when our framework or this class will be loaded/instantiated before that - [self performSelector:@selector(startUpdater) withObject:nil afterDelay:0]; + [self startUpdater]; } } diff --git a/Sparkle/SPUUpdater.h b/Sparkle/SPUUpdater.h index 53133f90..475dbb6e 100644 --- a/Sparkle/SPUUpdater.h +++ b/Sparkle/SPUUpdater.h @@ -54,12 +54,12 @@ SU_EXPORT @interface SPUUpdater : NSObject This method checks if Sparkle is configured properly. A valid feed URL should be set before this method is invoked. Other properties of this SPUUpdater instance can be set before this method is invoked as well, such as automatic update checks. - If the configuration is valid, this method may bring up a permission prompt (if needed) for checking if the user wants automatic update checking. - This method then starts the regular update cycle if automatic update checks are enabled. + If the configuration is valid, an update cycle is started in the next main runloop cycle. + During this cycle, a permission prompt may be brought up (if needed) for checking if the user wants automatic update checking. + Otherwise if automatic update checks are enabled, a scheduled update alert may be brought up if enough time has elapsed since the last check. - One of -checkForUpdates, -checkForUpdatesInBackground, or -checkForUpdateInformation can be invoked before starting the updater. - This preschedules an update action before starting the updater. When the updater is started, the prescheduled action is immediately invoked. - This may be useful for example if you want to check for updates right away without a permission prompt potentially showing. + After starting the updater and before the next runloop cycle, one of -checkForUpdates, -checkForUpdatesInBackground, or -checkForUpdateInformation can be invoked. + This may be useful if you want to check for updates immediately or without showing a permission prompt. This must be called on the main thread. diff --git a/Sparkle/SPUUpdater.m b/Sparkle/SPUUpdater.m index e34be078..4e7a4679 100644 --- a/Sparkle/SPUUpdater.m +++ b/Sparkle/SPUUpdater.m @@ -54,7 +54,6 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification @property (nonatomic, readonly) SPUUpdaterCycle *updaterCycle; @property (nonatomic, readonly) SPUUpdaterTimer *updaterTimer; @property (nonatomic) BOOL startedUpdater; -@property (nonatomic, copy) void (^preStartedScheduledUpdateBlock)(void); @property (nonatomic, nullable) id resumableUpdate; @property (nonatomic) BOOL canCheckForUpdates; @@ -79,7 +78,6 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification @synthesize updaterTimer = _updaterTimer; @synthesize sparkleBundle = _sparkleBundle; @synthesize startedUpdater = _startedUpdater; -@synthesize preStartedScheduledUpdateBlock = _preStartedScheduledUpdateBlock; @synthesize resumableUpdate = _resumableUpdate; @synthesize canCheckForUpdates = _canCheckForUpdates; @synthesize updateLastCheckedDate = _updateLastCheckedDate; @@ -149,7 +147,13 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification } self.startedUpdater = YES; - [self startUpdateCycle]; + + // Start updater on next update cycle so we make sure the application invoking the updater is ready + // This also gives the developer a cycle to check for updates before Sparkle's update cycle scheduler kicks in + dispatch_async(dispatch_get_main_queue(), ^{ + [self startUpdateCycle]; + }); + return YES; } @@ -269,10 +273,6 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification if ([self.host objectForUserDefaultsKey:SUEnableAutomaticChecksKey] != nil) { shouldPrompt = NO; } - // If the developer wants to check for updates, we shouldn't bug the user about a prompt yet - else if (self.preStartedScheduledUpdateBlock != nil) { - shouldPrompt = NO; - } // Does the delegate want to take care of the logic for when we should ask permission to update? else if ([self.delegate respondsToSelector:@selector((updaterShouldPromptForPermissionToCheckForUpdates:))]) { shouldPrompt = [self.delegate updaterShouldPromptForPermissionToCheckForUpdates:self]; @@ -313,13 +313,8 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification // We start the update checks and register as observer for changes after the prompt finishes } else { - if (self.preStartedScheduledUpdateBlock != nil) { - self.preStartedScheduledUpdateBlock(); - self.preStartedScheduledUpdateBlock = nil; - } else { - // We check if the user's said they want updates, or they haven't said anything, and the default is set to checking. - [self scheduleNextUpdateCheck]; - } + // We check if the user's said they want updates, or they haven't said anything, and the default is set to checking. + [self scheduleNextUpdateCheck]; } } @@ -414,10 +409,7 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification - (void)checkForUpdatesInBackground { if (!self.startedUpdater) { - __weak SPUUpdater *weakSelf = self; - self.preStartedScheduledUpdateBlock = ^{ - [weakSelf checkForUpdatesInBackground]; - }; + SULog(SULogLevelError, @"Error: checkForUpdatesInBackground - updater hasn't been started yet. Please call -startUpdater: first"); return; } @@ -470,9 +462,7 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification { __weak SPUUpdater *weakSelf = self; if (!self.startedUpdater) { - self.preStartedScheduledUpdateBlock = ^{ - [weakSelf checkForUpdates]; - }; + SULog(SULogLevelError, @"Error: checkForUpdates - updater hasn't been started yet. Please call -startUpdater: first"); return; } @@ -505,9 +495,7 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification { __weak SPUUpdater *weakSelf = self; if (!self.startedUpdater) { - self.preStartedScheduledUpdateBlock = ^{ - [weakSelf checkForUpdateInformation]; - }; + SULog(SULogLevelError, @"Error: checkForUpdateInformation - updater hasn't been started yet. Please call -startUpdater: first"); return; } @@ -598,6 +586,7 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification - (void)resetUpdateCycle { if (!self.startedUpdater) { + SULog(SULogLevelError, @"Error: resetUpdateCycle - updater hasn't been started yet. Please call -startUpdater: first"); return; // not even ready yet } diff --git a/Sparkle/SUUpdater.m b/Sparkle/SUUpdater.m index eedfb4d2..2217f78d 100644 --- a/Sparkle/SUUpdater.m +++ b/Sparkle/SUUpdater.m @@ -79,15 +79,11 @@ static NSMutableDictionary *sharedUpdaters = nil; // See -[SUUpdater _standardUserDriverRequestsPathToRelaunch] and -[SUUpdater _pathToRelaunchForUpdater:] implemented below which resolves this _userDriver = [[SPUStandardUserDriver alloc] initWithHostBundle:bundle delegate:self]; _updater = [[SPUUpdater alloc] initWithHostBundle:bundle applicationBundle:bundle userDriver:_userDriver delegate:self]; - - // Delay starting the updater, so that the host has a chance to configure the updater - // either in applicationWillFinishLaunching: or by setting properties right after initialisation - dispatch_async(dispatch_get_main_queue(), ^{ - NSError *updaterError = nil; - if (![self.updater startUpdater:&updaterError]) { - SULog(SULogLevelError, @"Error: Failed to start updater with error: %@", updaterError); - } - }); + + NSError *updaterError = nil; + if (![self.updater startUpdater:&updaterError]) { + SULog(SULogLevelError, @"Error: Failed to start updater with error: %@", updaterError); + } } return self; } diff --git a/sparkle-cli/SPUCommandLineDriver.m b/sparkle-cli/SPUCommandLineDriver.m index e880b55a..04dcfad7 100644 --- a/sparkle-cli/SPUCommandLineDriver.m +++ b/sparkle-cli/SPUCommandLineDriver.m @@ -145,20 +145,21 @@ - (void)runAndCheckForUpdatesNow:(BOOL)checkForUpdatesNow { + [self startUpdater]; + if (checkForUpdatesNow) { // When we start the updater, this scheduled check will start afterwards too [self.updater checkForUpdates]; } - - [self startUpdater]; } - (void)probeForUpdates { + [self startUpdater]; + // When we start the updater, this info check will start afterwards too self.probingForUpdates = YES; [self.updater checkForUpdateInformation]; - [self startUpdater]; } @end From 5ae944778a710afd35d2fa1330ca24c7d33b051f Mon Sep 17 00:00:00 2001 From: Zorg Date: Sun, 28 Mar 2021 16:25:05 -0700 Subject: [PATCH 2/4] Set canCheckForUpdates to NO before probing install status --- Sparkle/SPUUpdater.m | 59 ++++++++++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 24 deletions(-) diff --git a/Sparkle/SPUUpdater.m b/Sparkle/SPUUpdater.m index 4e7a4679..b66aa1d7 100644 --- a/Sparkle/SPUUpdater.m +++ b/Sparkle/SPUUpdater.m @@ -147,6 +147,7 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification } self.startedUpdater = YES; + [self setCanCheckForUpdates:YES]; // Start updater on next update cycle so we make sure the application invoking the updater is ready // This also gives the developer a cycle to check for updates before Sparkle's update cycle scheduler kicks in @@ -265,6 +266,10 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification - (void)startUpdateCycle { + if (![self canCheckForUpdates]) { + return; + } + BOOL shouldPrompt = NO; BOOL hasLaunchedBefore = [self.host boolForUserDefaultsKey:SUHasLaunchedBeforeKey]; @@ -301,14 +306,12 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification __weak SPUUpdater *weakSelf = self; [self.userDriver showUpdatePermissionRequest:updatePermissionRequest reply:^(SUUpdatePermissionResponse *response) { - dispatch_async(dispatch_get_main_queue(), ^{ - SPUUpdater *strongSelf = weakSelf; - if (strongSelf != nil) { - [strongSelf updatePermissionRequestFinishedWithResponse:response]; - // Schedule checks, but make sure we ignore the delayed call from KVO - [strongSelf resetUpdateCycle]; - } - }); + SPUUpdater *strongSelf = weakSelf; + if (strongSelf != nil) { + [strongSelf updatePermissionRequestFinishedWithResponse:response]; + // Schedule checks, but make sure we ignore the delayed call from KVO + [strongSelf resetUpdateCycle]; + } }]; // We start the update checks and register as observer for changes after the prompt finishes @@ -359,11 +362,7 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification { [self.updaterTimer invalidate]; - BOOL automaticallyCheckForUpdates = [self automaticallyChecksForUpdates]; - - [self setCanCheckForUpdates:!automaticallyCheckForUpdates]; - - if (!automaticallyCheckForUpdates) { + if (![self automaticallyChecksForUpdates]) { if ([self.delegate respondsToSelector:@selector(updaterWillIdleSchedulingUpdates:)]) { [self.delegate updaterWillIdleSchedulingUpdates:self]; } @@ -373,8 +372,6 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification if (firingImmediately) { [self checkForUpdatesInBackground]; } else { - [self setCanCheckForUpdates:YES]; - [self retrieveNextUpdateCheckInterval:^(NSTimeInterval updateCheckInterval) { // This callback is asynchronous, so the timer may be set. Invalidate to make sure it isn't. [self.updaterTimer invalidate]; @@ -413,6 +410,12 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification return; } + if (!self.canCheckForUpdates) { + SULog(SULogLevelError, @"Error: -checkForUpdatesInBackground called but .canCheckForUpdates == NO"); + } + + [self setCanCheckForUpdates:NO]; + // We don't want the probe check to act on the driver if the updater is going near death __weak SPUUpdater *weakSelf = self; @@ -466,10 +469,16 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification return; } + if (!self.canCheckForUpdates) { + SULog(SULogLevelError, @"Error: -checkForUpdates called but .canCheckForUpdates == NO"); + } + if (self.driver != nil) { return; } + [self setCanCheckForUpdates:NO]; + id theUpdateDriver = [[SPUUserInitiatedUpdateDriver alloc] initWithHost:self.host applicationBundle:self.applicationBundle sparkleBundle:self.sparkleBundle updater:self userDriver:self.userDriver updaterDelegate:self.delegate]; NSString *bundleIdentifier = self.host.bundle.bundleIdentifier; @@ -499,6 +508,12 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification return; } + if (!self.canCheckForUpdates) { + SULog(SULogLevelError, @"Error: -checkForUpdateInformation called but .canCheckForUpdates == NO"); + } + + [self setCanCheckForUpdates:NO]; + NSString *bundleIdentifier = self.host.bundle.bundleIdentifier; assert(bundleIdentifier != nil); [SPUProbeInstallStatus probeInstallerInProgressForHostBundleIdentifier:bundleIdentifier completion:^(BOOL installerInProgress) { @@ -523,17 +538,12 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification if( [self.delegate respondsToSelector: @selector((updaterMayCheckForUpdates:))] && ![self.delegate updaterMayCheckForUpdates:self] ) { + [self setCanCheckForUpdates:YES]; [self scheduleNextUpdateCheck]; return; } self.driver = d; - - // If we're not given a driver at all, just schedule the next update check and bail. - if (!self.driver) { - [self scheduleNextUpdateCheck]; - return; - } // Because an application can change the configuration (eg: the feed url) at any point, we should always check if it's valid NSError *configurationError = nil; @@ -543,6 +553,7 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification SULog(SULogLevelError, @"Sparkle configuration error (%ld): %@", (long)configurationError.code, configurationError.localizedDescription); SULog(SULogLevelDefault, @"Disabling scheduled updates.."); + [self setCanCheckForUpdates:YES]; [self.driver abortUpdateWithError:configurationError]; self.driver = nil; @@ -557,13 +568,12 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification if (strongSelf != nil) { strongSelf.resumableUpdate = resumableUpdate; strongSelf.driver = nil; + [strongSelf setCanCheckForUpdates:YES]; [strongSelf updateLastUpdateCheckDate]; [strongSelf scheduleNextUpdateCheckFiringImmediately:shouldShowUpdateImmediately]; } }; - [self setCanCheckForUpdates:NO]; - if (installerInProgress) { [self.driver resumeInstallingUpdateWithCompletion:completionBlock]; } else if (self.resumableUpdate != nil) { @@ -574,6 +584,7 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification } else { // I think this is really unlikely to occur but better be safe [self.driver abortUpdateWithError:[NSError errorWithDomain:SUSparkleErrorDomain code:SUInvalidFeedURLError userInfo:@{ NSLocalizedDescriptionKey: @"Sparkle cannot form a valid feed URL." }]]; + [self setCanCheckForUpdates:YES]; self.driver = nil; } } @@ -592,7 +603,7 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification [self cancelNextUpdateCycle]; - if (self.driver == nil) { + if (self.canCheckForUpdates) { [self scheduleNextUpdateCheck]; } } From 2614af86fff7206c0327596e032df23d9a3bbcfc Mon Sep 17 00:00:00 2001 From: Zorg Date: Sun, 28 Mar 2021 16:35:21 -0700 Subject: [PATCH 3/4] Don't defer some of sparkle-cli delegate callbacks --- sparkle-cli/SPUCommandLineDriver.m | 51 ++++++++++++------------------ 1 file changed, 20 insertions(+), 31 deletions(-) diff --git a/sparkle-cli/SPUCommandLineDriver.m b/sparkle-cli/SPUCommandLineDriver.m index 04dcfad7..6903cc3c 100644 --- a/sparkle-cli/SPUCommandLineDriver.m +++ b/sparkle-cli/SPUCommandLineDriver.m @@ -57,27 +57,20 @@ return self; } -// Because the user driver dispatches to the main queue asynchronously, we should do so here too -// to preserve the order of handled events - -- (void)updater:(SPUUpdater *)__unused updater willScheduleUpdateCheckAfterDelay:(NSTimeInterval)delay +- (void)updater:(SPUUpdater *)__unused updater willScheduleUpdateCheckAfterDelay:(NSTimeInterval)delay __attribute__((noreturn)) { - dispatch_async(dispatch_get_main_queue(), ^{ - if (self.verbose) { - fprintf(stderr, "Last update check occurred too soon. Try again after %0.0f second(s).", delay); - } - exit(EXIT_SUCCESS); - }); + if (self.verbose) { + fprintf(stderr, "Last update check occurred too soon. Try again after %0.0f second(s).", delay); + } + exit(EXIT_SUCCESS); } -- (void)updaterWillIdleSchedulingUpdates:(SPUUpdater *)__unused updater +- (void)updaterWillIdleSchedulingUpdates:(SPUUpdater *)__unused updater __attribute__((noreturn)) { - dispatch_async(dispatch_get_main_queue(), ^{ - if (self.verbose) { - fprintf(stderr, "Automatic update checks are disabled. Exiting.\n"); - } - exit(EXIT_SUCCESS); - }); + if (self.verbose) { + fprintf(stderr, "Automatic update checks are disabled. Exiting.\n"); + } + exit(EXIT_SUCCESS); } // If the installation is interactive, we can show an authorization prompt for requesting additional privileges, @@ -99,24 +92,20 @@ // In case we find an update during probing, otherwise we leave this to the user driver - (void)updater:(SPUUpdater *)__unused updater didFindValidUpdate:(SUAppcastItem *)__unused item { - dispatch_async(dispatch_get_main_queue(), ^{ - if (self.probingForUpdates) { - if (self.verbose) { - fprintf(stderr, "Update available!\n"); - } - exit(EXIT_SUCCESS); + if (self.probingForUpdates) { + if (self.verbose) { + fprintf(stderr, "Update available!\n"); } - }); + exit(EXIT_SUCCESS); + } } -- (void)updaterDidNotFindUpdate:(SPUUpdater *)__unused updater +- (void)updaterDidNotFindUpdate:(SPUUpdater *)__unused updater __attribute__((noreturn)) { - dispatch_async(dispatch_get_main_queue(), ^{ - if (self.verbose) { - fprintf(stderr, "No update available!\n"); - } - exit(EXIT_FAILURE); - }); + if (self.verbose) { + fprintf(stderr, "No update available!\n"); + } + exit(EXIT_FAILURE); } - (void)updater:(SPUUpdater *)__unused updater didAbortWithError:(NSError *)error From 93b8700060d567222b7285d4617cfbaff45db99c Mon Sep 17 00:00:00 2001 From: Zorg Date: Sun, 28 Mar 2021 17:10:10 -0700 Subject: [PATCH 4/4] Add self.driver != nil assert --- Sparkle/SPUUpdater.m | 1 + 1 file changed, 1 insertion(+) diff --git a/Sparkle/SPUUpdater.m b/Sparkle/SPUUpdater.m index b66aa1d7..8c9d31a8 100644 --- a/Sparkle/SPUUpdater.m +++ b/Sparkle/SPUUpdater.m @@ -544,6 +544,7 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification } self.driver = d; + assert(self.driver != nil); // Because an application can change the configuration (eg: the feed url) at any point, we should always check if it's valid NSError *configurationError = nil;