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 92a3241a..cbec6cc8 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..8c9d31a8 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,14 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification } self.startedUpdater = YES; - [self startUpdateCycle]; + [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 + dispatch_async(dispatch_get_main_queue(), ^{ + [self startUpdateCycle]; + }); + return YES; } @@ -261,6 +266,10 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification - (void)startUpdateCycle { + if (![self canCheckForUpdates]) { + return; + } + BOOL shouldPrompt = NO; BOOL hasLaunchedBefore = [self.host boolForUserDefaultsKey:SUHasLaunchedBeforeKey]; @@ -269,10 +278,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]; @@ -301,25 +306,18 @@ 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 } 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]; } } @@ -364,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]; } @@ -378,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]; @@ -414,13 +406,16 @@ 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; } + 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; @@ -470,16 +465,20 @@ 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; } + 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; @@ -505,12 +504,16 @@ 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; } + 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) { @@ -535,17 +538,13 @@ 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; - } + 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; @@ -555,6 +554,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; @@ -569,13 +569,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) { @@ -586,6 +585,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; } } @@ -598,12 +598,13 @@ 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 } [self cancelNextUpdateCycle]; - if (self.driver == nil) { + if (self.canCheckForUpdates) { [self scheduleNextUpdateCheck]; } } 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..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 @@ -145,20 +134,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