diff --git a/Sparkle/SUAutomaticUpdateDriver.m b/Sparkle/SUAutomaticUpdateDriver.m index 6af4920a..03840c4b 100644 --- a/Sparkle/SUAutomaticUpdateDriver.m +++ b/Sparkle/SUAutomaticUpdateDriver.m @@ -20,22 +20,28 @@ @interface SUAutomaticUpdateDriver () +@property (nonatomic, readonly, weak) id updater; +@property (nonatomic, readonly, weak, nullable) id updaterDelegate; @property (nonatomic, readonly) SUCoreBasedUpdateDriver *coreDriver; -@property (nonatomic) BOOL foundCriticalUpdate; +@property (nonatomic) SUAppcastItem* updateItem; @property (nonatomic) BOOL willInstallSilently; @end @implementation SUAutomaticUpdateDriver +@synthesize updater = _updater; +@synthesize updaterDelegate = _updaterDelegate; @synthesize coreDriver = _coreDriver; -@synthesize foundCriticalUpdate = _foundCriticalUpdate; +@synthesize updateItem = _updateItem; @synthesize willInstallSilently = _willInstallSilently; - (instancetype)initWithHost:(SUHost *)host sparkleBundle:(NSBundle *)sparkleBundle updater:(id)updater updaterDelegate:(nullable id )updaterDelegate { self = [super init]; if (self != nil) { + _updater = updater; + _updaterDelegate = updaterDelegate; _coreDriver = [[SUCoreBasedUpdateDriver alloc] initWithHost:host sparkleBundle:sparkleBundle updater:updater updaterDelegate:updaterDelegate delegate:self]; } return self; @@ -55,7 +61,7 @@ - (void)basicDriverDidFindUpdateWithAppcastItem:(SUAppcastItem *)updateItem { - self.foundCriticalUpdate = [updateItem isCriticalUpdate]; + self.updateItem = updateItem; [self.coreDriver downloadUpdateFromAppcastItem:updateItem]; } @@ -65,15 +71,37 @@ self.willInstallSilently = willInstallSilently; if (!willInstallImmediately) { - // We are done and can safely abort now - // The installer tool will keep the installation alive - [self abortUpdate]; + BOOL installationHandledByDelegate = NO; + id updaterDelegate = self.updaterDelegate; + if (self.willInstallSilently && [updaterDelegate respondsToSelector:@selector(updater:willInstallUpdateOnQuit:immediateInstallationBlock:)]) { + __weak SUAutomaticUpdateDriver *weakSelf = self; + installationHandledByDelegate = + [updaterDelegate updater:self.updater willInstallUpdateOnQuit:self.updateItem immediateInstallationBlock:^{ + [weakSelf.coreDriver finishInstallationWithResponse:SUInstallAndRelaunchUpdateNow displayingUserInterface:NO]; + }]; + } + + if (!installationHandledByDelegate) { + // We are done and can safely abort now + // The installer tool will keep the installation alive + [self abortUpdate]; + } + } +} + +// This can only be reached if the updater delegate invokes its immediate installation block above, +// otherwise the update driver will abort the update before then +- (void)installerIsRequestingAppTermination +{ + id updaterDelegate = self.updaterDelegate; + if ([updaterDelegate respondsToSelector:@selector(updaterIsRequestingQuit:)]) { + [updaterDelegate updaterIsRequestingQuit:self.updater]; } } - (BOOL)basicDriverShouldSignalShowingUpdateImmediately { - return (!self.willInstallSilently || self.foundCriticalUpdate); + return (!self.willInstallSilently || self.updateItem.isCriticalUpdate); } - (void)basicDriverIsRequestingAbortUpdateWithError:(NSError *)error diff --git a/Sparkle/SUCoreBasedUpdateDriver.h b/Sparkle/SUCoreBasedUpdateDriver.h index 4a483260..97d4d94e 100644 --- a/Sparkle/SUCoreBasedUpdateDriver.h +++ b/Sparkle/SUCoreBasedUpdateDriver.h @@ -59,7 +59,7 @@ NS_ASSUME_NONNULL_BEGIN - (void)downloadUpdateFromAppcastItem:(SUAppcastItem *)updateItem; -- (void)finishInstallationWithResponse:(SUInstallUpdateStatus)installUpdateStatus; +- (void)finishInstallationWithResponse:(SUInstallUpdateStatus)installUpdateStatus displayingUserInterface:(BOOL)displayingUserInterface; - (void)abortUpdateWithError:(nullable NSError *)error; diff --git a/Sparkle/SUCoreBasedUpdateDriver.m b/Sparkle/SUCoreBasedUpdateDriver.m index 6c2f5291..4fe5d179 100644 --- a/Sparkle/SUCoreBasedUpdateDriver.m +++ b/Sparkle/SUCoreBasedUpdateDriver.m @@ -183,17 +183,17 @@ [self.delegate installerDidFinishPreparationAndWillInstallImmediately:willInstallImmediately silently:willInstallSilently]; } -- (void)finishInstallationWithResponse:(SUInstallUpdateStatus)installUpdateStatus +- (void)finishInstallationWithResponse:(SUInstallUpdateStatus)installUpdateStatus displayingUserInterface:(BOOL)displayingUserInterface { switch (installUpdateStatus) { case SUDismissUpdateInstallation: [self.delegate coreDriverIsRequestingAbortUpdateWithError:nil]; break; case SUInstallUpdateNow: - [self.installerDriver installWithToolAndRelaunch:NO displayingUserInterface:YES]; + [self.installerDriver installWithToolAndRelaunch:NO displayingUserInterface:displayingUserInterface]; break; case SUInstallAndRelaunchUpdateNow: - [self.installerDriver installWithToolAndRelaunch:YES displayingUserInterface:YES]; + [self.installerDriver installWithToolAndRelaunch:YES displayingUserInterface:displayingUserInterface]; break; } } diff --git a/Sparkle/SUUIBasedUpdateDriver.m b/Sparkle/SUUIBasedUpdateDriver.m index 42a2c4bc..8a4d61f7 100644 --- a/Sparkle/SUUIBasedUpdateDriver.m +++ b/Sparkle/SUUIBasedUpdateDriver.m @@ -98,7 +98,7 @@ [self.userDriver showResumableUpdateFoundWithAppcastItem:updateItem reply:^(SUInstallUpdateStatus choice) { dispatch_async(dispatch_get_main_queue(), ^{ [self.host setObject:nil forUserDefaultsKey:SUSkippedVersionKey]; - [self.coreDriver finishInstallationWithResponse:choice]; + [self.coreDriver finishInstallationWithResponse:choice displayingUserInterface:YES]; }); }]; } @@ -167,7 +167,7 @@ if (!willInstallImmediately) { [self.userDriver showReadyToInstallAndRelaunch:^(SUInstallUpdateStatus installUpdateStatus) { dispatch_async(dispatch_get_main_queue(), ^{ - [self.coreDriver finishInstallationWithResponse:installUpdateStatus]; + [self.coreDriver finishInstallationWithResponse:installUpdateStatus displayingUserInterface:YES]; }); }]; } diff --git a/Sparkle/SUUpdaterDelegate.h b/Sparkle/SUUpdaterDelegate.h index d5cd0091..ff0f104b 100644 --- a/Sparkle/SUUpdaterDelegate.h +++ b/Sparkle/SUUpdaterDelegate.h @@ -290,8 +290,36 @@ SU_EXPORT extern NSString *const SUUpdaterAppcastNotificationKey; \param updater The SUUpdater instance. \param item The appcast item corresponding to the update that is proposed to be installed. \param invocation Can be used to trigger an immediate silent install and relaunch. + + \deprecated See -updater:willInstallUpdateOnQuit:immediateInstallationBlock: and -updaterIsRequestingQuit: instead. This method is no longer invoked. */ -- (void)updater:(SUUpdater *)updater willInstallUpdateOnQuit:(SUAppcastItem *)item immediateInstallationInvocation:(NSInvocation *)invocation; +- (void)updater:(SUUpdater *)updater willInstallUpdateOnQuit:(SUAppcastItem *)item immediateInstallationInvocation:(NSInvocation *)invocation __deprecated; + +/*! + Called when an update is scheduled to be silently installed on quit after downloading the update automatically. + + \param updater The SUUpdater instance. + \param item The appcast item corresponding to the update that is proposed to be installed. + \param immediateInstallHandler The install handler to immediately install the update. + If you plan to invoke this, you must return YES from this method and also implement -updaterIsRequestingQuit: + \return Return YES if the delegate will handle installing the update or NO if the updater should be given responsibility. + + If the updater is given responsibility, it can later remind the user an update is available if they have not terminated the application for a long time. + Also if the updater is given responsibility and the update item is marked critical, the new update will be presented to the user immediately after. + Even if the immediateInstallHandler is not invoked, the installer will attempt to install the update on termination. + */ +- (BOOL)updater:(SUUpdater *)updater willInstallUpdateOnQuit:(SUAppcastItem *)item immediateInstallationBlock:(void (^)(void))immediateInstallHandler; + +/*! + Called when the updater requests to quit the application after the delegate invoked its installation block from -updater:willInstallUpdateOnQuit:immediateInstallationBlock: + + \param updater the SUUpdater instance. + + The delegate should terminate the application so the installer can immediately install the update. + After the application is terminated, the installer will install the update and the application will be relaunched. + The installer will not show any UI interaction. + */ +- (void)updaterIsRequestingQuit:(SUUpdater *)updater; /*! Calls after an update that was scheduled to be silently installed on quit has been canceled.