diff --git a/Sparkle/SPUStandardUpdaterController.h b/Sparkle/SPUStandardUpdaterController.h index 3851e67b..5c4fecde 100644 --- a/Sparkle/SPUStandardUpdaterController.h +++ b/Sparkle/SPUStandardUpdaterController.h @@ -37,6 +37,8 @@ NS_ASSUME_NONNULL_BEGIN It also allows hooking up the updater's and user driver's delegates. If you need more control over what bundle you want to update, or you want to provide a custom user interface (via `SPUUserDriver`), please use `SPUUpdater` directly instead. + + This class must be used on the main thread. */ SU_EXPORT @interface SPUStandardUpdaterController : NSObject { diff --git a/Sparkle/SPUUpdater.h b/Sparkle/SPUUpdater.h index 2df48152..5f1db49f 100644 --- a/Sparkle/SPUUpdater.h +++ b/Sparkle/SPUUpdater.h @@ -42,6 +42,8 @@ NS_ASSUME_NONNULL_BEGIN - `feedURL` Please view the documentation on each of these properties for more detail if you are to configure them dynamically. + + This class must be used on the main thread. */ SU_EXPORT @interface SPUUpdater : NSObject @@ -100,8 +102,9 @@ SU_EXPORT @interface SPUUpdater : NSObject (as long as the user driver is the standard `SPUStandardUserDriver` or if it implements `-[SPUUserDriver showUpdateInFocus]`). This will find updates that the user has previously opted into skipping. - See `canCheckForUpdates` property which can determine when this method may be invoked. + + This must be called on the main thread. */ - (void)checkForUpdates; @@ -127,6 +130,8 @@ SU_EXPORT @interface SPUUpdater : NSObject This will not find updates that the user has opted into skipping. This method does not do anything if there is a `sessionInProgress`. + + This must be called on the main thread. */ - (void)checkForUpdatesInBackground; @@ -145,6 +150,8 @@ SU_EXPORT @interface SPUUpdater : NSObject Updates that have been skipped by the user will not be found. This method does not do anything if there is a `sessionInProgress`. + + This must be called on the main thread. */ - (void)checkForUpdateInformation; @@ -343,6 +350,8 @@ SU_EXPORT @interface SPUUpdater : NSObject For testing purposes, the last update check is stored in the `SULastCheckTime` key in the host bundle's user defaults. For example, `defaults delete my-bundle-id SULastCheckTime` can be invoked to clear the last update check time and test if update checks are automatically scheduled. + + This property must be called on the main thread. */ @property (nonatomic, readonly, copy, nullable) NSDate *lastUpdateCheckDate; @@ -354,6 +363,8 @@ SU_EXPORT @interface SPUUpdater : NSObject If the `updateCheckInterval` or `automaticallyChecksForUpdates` properties are changed, this method is automatically invoked after a short delay using `-resetUpdateCycleAfterShortDelay`. In these cases, manually resetting the update cycle is not necessary. See also `-resetUpdateCycleAfterShortDelay` which gives the user a short delay before triggering a cycle reset. + + This must be called on the main thread. */ - (void)resetUpdateCycle; @@ -367,6 +378,8 @@ SU_EXPORT @interface SPUUpdater : NSObject no cycle reset will be done. If the `updateCheckInterval` or `automaticallyChecksForUpdates` properties are changed, this method is automatically invoked. In these cases, manually resetting the update cycle is not necessary. + + This must be called on the main thread. */ - (void)resetUpdateCycleAfterShortDelay; diff --git a/Sparkle/SPUUpdater.m b/Sparkle/SPUUpdater.m index f998303f..1de3aeb0 100644 --- a/Sparkle/SPUUpdater.m +++ b/Sparkle/SPUUpdater.m @@ -156,6 +156,13 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification - (BOOL)startUpdater:(NSError * __autoreleasing *)error { + if (![NSThread isMainThread]) { + if (error != NULL) { + *error = [NSError errorWithDomain:SUSparkleErrorDomain code:SUInvalidUpdaterError userInfo:@{ NSLocalizedDescriptionKey: @"-[SPUUpdater startUpdater:] must be called on the main thread]"}]; + } + return NO; + } + if (_startedUpdater) { return YES; } @@ -467,6 +474,10 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification - (NSDate *)lastUpdateCheckDate { + if (![NSThread isMainThread]) { + SULog(SULogLevelError, @"Error: -[SPUUpdater lastUpdateCheckDate] must be called on the main thread."); + } + if (_updateLastCheckedDate == nil) { _updateLastCheckedDate = [_host objectForUserDefaultsKey:SULastCheckTimeKey]; @@ -626,6 +637,16 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification // Sparkle internally uses _checkForUpdatesInBackground - (void)checkForUpdatesInBackground { + if (![NSThread isMainThread]) { + SULog(SULogLevelError, @"Error: -[SPUUpdater checkForUpdatesInBackground] can only be called on the main thread"); + + // Try to be nice and dispatch on main thread anyway + dispatch_async(dispatch_get_main_queue(), ^{ + [self checkForUpdatesInBackground]; + }); + return; + } + if (!_startedUpdater) { SULog(SULogLevelError, @"Error: checkForUpdatesInBackground - updater hasn't been started yet. Please call -startUpdater: first"); return; @@ -653,6 +674,17 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification - (void)checkForUpdates { + if (![NSThread isMainThread]) { + SULog(SULogLevelError, @"Error: -[SPUUpdater checkForUpdates] can only be called on the main thread"); + + // Try to be nice and dispatch on main thread anyway + dispatch_async(dispatch_get_main_queue(), ^{ + [self checkForUpdates]; + }); + + return; + } + if (_showingPermissionRequest || _driver.showingUpdate) { if ([_userDriver respondsToSelector:@selector(showUpdateInFocus)]) { [_userDriver showUpdateInFocus]; @@ -699,6 +731,17 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification - (void)checkForUpdateInformation { + if (![NSThread isMainThread]) { + SULog(SULogLevelError, @"Error: -[SPUUpdater checkForUpdateInformation] can only be called on the main thread"); + + // Try to be nice and dispatch on main thread anyway + dispatch_async(dispatch_get_main_queue(), ^{ + [self checkForUpdateInformation]; + }); + + return; + } + __weak __typeof__(self) weakSelf = self; if (!_startedUpdater) { SULog(SULogLevelError, @"Error: checkForUpdateInformation - updater hasn't been started yet. Please call -startUpdater: first"); @@ -888,6 +931,16 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification - (void)resetUpdateCycle { + if (![NSThread isMainThread]) { + SULog(SULogLevelError, @"Error: -[SPUUpdater resetUpdateCycle] must be called on the main thread."); + + // Try to be nice and dispatch on main thread anyway + dispatch_async(dispatch_get_main_queue(), ^{ + [self resetUpdateCycle]; + }); + return; + } + if (!_startedUpdater) { SULog(SULogLevelError, @"Error: resetUpdateCycle - updater hasn't been started yet. Please call -startUpdater: first"); return; // not even ready yet @@ -926,17 +979,35 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification - (void)resetUpdateCycleAfterShortDelay { + if (![NSThread isMainThread]) { + SULog(SULogLevelError, @"Error: -[SPUUpdater resetUpdateCycleAfterShortDelay] must be called on the main thread."); + + // Try to be nice and dispatch on main thread anyway + dispatch_async(dispatch_get_main_queue(), ^{ + [self resetUpdateCycleAfterShortDelay]; + }); + return; + } + [self cancelNextUpdateCycle]; [_updaterCycle resetUpdateCycleAfterDelay]; } - (void)setAutomaticallyChecksForUpdates:(BOOL)automaticallyCheckForUpdates { + if (![NSThread isMainThread]) { + SULog(SULogLevelError, @"Error: -[SPUUpdater setAutomaticallyChecksForUpdates:] must be called on the main thread."); + } + _updaterSettings.automaticallyChecksForUpdates = automaticallyCheckForUpdates; } - (BOOL)automaticallyChecksForUpdates { + if (![NSThread isMainThread]) { + SULog(SULogLevelError, @"Error: -[SPUUpdater automaticallyChecksForUpdates] must be called on the main thread."); + } + return [_updaterSettings automaticallyChecksForUpdates]; } @@ -965,11 +1036,19 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification - (void)setAutomaticallyDownloadsUpdates:(BOOL)automaticallyUpdates { + if (![NSThread isMainThread]) { + SULog(SULogLevelError, @"Error: -[SPUUpdater setAutomaticallyDownloadsUpdates:] must be called on the main thread."); + } + _updaterSettings.automaticallyDownloadsUpdates = automaticallyUpdates; } - (BOOL)automaticallyDownloadsUpdates { + if (![NSThread isMainThread]) { + SULog(SULogLevelError, @"Error: -[SPUUpdater automaticallyDownloadsUpdates] must be called on the main thread."); + } + return [_updaterSettings automaticallyDownloadsUpdates]; } @@ -1097,11 +1176,19 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification - (void)setSendsSystemProfile:(BOOL)sendsSystemProfile { + if (![NSThread isMainThread]) { + SULog(SULogLevelError, @"Error: -[SPUUpdater setSendsSystemProfile:] must be called on the main thread."); + } + _updaterSettings.sendsSystemProfile = sendsSystemProfile; } - (BOOL)sendsSystemProfile { + if (![NSThread isMainThread]) { + SULog(SULogLevelError, @"Error: -[SPUUpdater sendsSystemProfile] must be called on the main thread."); + } + return [_updaterSettings sendsSystemProfile]; } @@ -1202,11 +1289,19 @@ static NSString *escapeURLComponent(NSString *str) { - (void)setUpdateCheckInterval:(NSTimeInterval)updateCheckInterval { + if (![NSThread isMainThread]) { + SULog(SULogLevelError, @"Error: -[SPUUpdater setUpdateCheckInterval:] must be called on the main thread."); + } + _updaterSettings.updateCheckInterval = updateCheckInterval; } - (NSTimeInterval)updateCheckInterval { + if (![NSThread isMainThread]) { + SULog(SULogLevelError, @"Error: -[SPUUpdater updateCheckInterval] must be called on the main thread."); + } + return [_updaterSettings updateCheckInterval]; }