From d5d9824b6c64b076b5f4e8e36029c2c8744b409b Mon Sep 17 00:00:00 2001 From: Dmytro Tretiakov Date: Mon, 11 Aug 2014 17:48:00 +0300 Subject: [PATCH] Added additional delegate methods. --- SUAutomaticUpdateDriver.m | 43 ++++++++++++++++++++++++--------------- SUBasicUpdateDriver.m | 25 +++++++++++++++++++++-- SUConstants.h | 2 +- SUUpdater.h | 6 ++++++ SUUpdater.m | 1 - 5 files changed, 57 insertions(+), 20 deletions(-) diff --git a/SUAutomaticUpdateDriver.m b/SUAutomaticUpdateDriver.m index b50e2979..88ee3151 100644 --- a/SUAutomaticUpdateDriver.m +++ b/SUAutomaticUpdateDriver.m @@ -62,22 +62,33 @@ static NSString * const kSUAutomaticUpdateParamName = @"autoupdate"; - (void)unarchiverDidFinish:(SUUnarchiver *)ua { - showErrors = YES; - alert = [[SUAutomaticUpdateAlert alloc] initWithAppcastItem:updateItem host:host delegate:self]; - - // If the app is a menubar app or the like, we need to focus it first and alter the - // update prompt to behave like a normal window. Otherwise if the window were hidden - // there may be no way for the application to be activated to make it visible again. - if ([host isBackgroundApplication]) - { - [[alert window] setHidesOnDeactivate:NO]; - [NSApp activateIgnoringOtherApps:YES]; - } - - if ([NSApp isActive]) - [[alert window] makeKeyAndOrderFront:self]; - else - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidBecomeActive:) name:NSApplicationDidBecomeActiveNotification object:NSApp]; + BOOL canAutomaticallyInstallUpdate = NO; + if ([[updater delegate] respondsToSelector:@selector(updater:canAutomaticallyInstallUpdate:)]) + canAutomaticallyInstallUpdate = [[updater delegate] updater:updater canAutomaticallyInstallUpdate:updateItem]; + + if (!canAutomaticallyInstallUpdate) + { + showErrors = YES; + alert = [[SUAutomaticUpdateAlert alloc] initWithAppcastItem:updateItem host:host delegate:self]; + + // If the app is a menubar app or the like, we need to focus it first and alter the + // update prompt to behave like a normal window. Otherwise if the window were hidden + // there may be no way for the application to be activated to make it visible again. + if ([host isBackgroundApplication]) + { + [[alert window] setHidesOnDeactivate:NO]; + [NSApp activateIgnoringOtherApps:YES]; + } + + if ([NSApp isActive]) + [[alert window] makeKeyAndOrderFront:self]; + else + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidBecomeActive:) name:NSApplicationDidBecomeActiveNotification object:NSApp]; + } + else + { + [self automaticUpdateAlert:nil finishedWithChoice:SUInstallNowChoice]; + } } - (void)applicationDidBecomeActive:(NSNotification *)aNotification diff --git a/SUBasicUpdateDriver.m b/SUBasicUpdateDriver.m index 1bc916e2..ad951c7e 100644 --- a/SUBasicUpdateDriver.m +++ b/SUBasicUpdateDriver.m @@ -393,6 +393,10 @@ NSArray *SUGetAllDevMateURLHosts(void) if (SUFinishUpdateAbort == finishTasks) { [self abortUpdate:SUUpdateAbortTasksFinished]; + + if ([[updater delegate] respondsToSelector:@selector(updater:didInstallUpdate:)]) + [[updater delegate] updater:updater didInstallUpdate:updateItem]; + return; } @@ -467,17 +471,34 @@ NSArray *SUGetAllDevMateURLHosts(void) if (SUShouldUseXPCInstaller()) { - [SUXPCInstaller launchTaskWithLaunchPath:relaunchToolPath arguments:arguments]; + [SUXPCInstaller launchTaskWithPath:relaunchToolPath + arguments:arguments + environment:nil + currentDirectoryPath:nil + inputData:nil + waitForTaskResult:!shouldFinishUpdateWithRelaunch + waitUntilDone:YES + completionHandler:nil]; } else { - [NSTask launchedTaskWithLaunchPath:relaunchToolPath arguments:arguments]; + NSTask *task = [NSTask launchedTaskWithLaunchPath:relaunchToolPath arguments:arguments]; + + if (!shouldFinishUpdateWithRelaunch) + [task waitUntilExit]; } if (shouldFinishUpdateWithRelaunch) { [NSApp terminate:nil]; } + else + { + [self abortUpdate:SUUpdateAbortTasksFinished]; + + if ([[updater delegate] respondsToSelector:@selector(updater:didInstallUpdate:)]) + [[updater delegate] updater:updater didInstallUpdate:updateItem]; + } } - (void)cleanUpDownload diff --git a/SUConstants.h b/SUConstants.h index c0ed8e83..572c9147 100644 --- a/SUConstants.h +++ b/SUConstants.h @@ -55,7 +55,7 @@ typedef NS_ENUM(NSUInteger, SUUpdateAbortReason) { SUUpdateAbortUndefined = 0, SUUpdateAbortForbiddenByDelegate, - SUUpdateAbortTasksFinished, // installed and relaunched by delegate + SUUpdateAbortTasksFinished, SUUpdateAbortCanceledByUser, SUUpdateAbortDidNotFind, SUUpdateAbortGotError diff --git a/SUUpdater.h b/SUUpdater.h index b1728f9d..c2a9c013 100644 --- a/SUUpdater.h +++ b/SUUpdater.h @@ -109,9 +109,15 @@ // Sent when a valid update is not found. - (void)updaterDidNotFindUpdate:(SUUpdater *)update; +// For cases when updater automatically downloads updates +- (BOOL)updater:(SUUpdater *)updater canAutomaticallyInstallUpdate:(SUAppcastItem *)update; + // Sent immediately before installing the specified update. - (void)updater:(SUUpdater *)updater willInstallUpdate:(SUAppcastItem *)update; +// Sent immediately after installing the specified update in case when relaunch was forbidden. +- (void)updater:(SUUpdater *)updater didInstallUpdate:(SUAppcastItem *)update; + // Override to handle update manually - (BOOL)updater:(SUUpdater *)updater overrideInstallUpdate:(SUAppcastItem *)update downloadPath:(NSString *)downloadPath shouldRelaunch:(BOOL *)outShouldRelaunch; diff --git a/SUUpdater.m b/SUUpdater.m index 17b03986..aded4d77 100644 --- a/SUUpdater.m +++ b/SUUpdater.m @@ -27,7 +27,6 @@ - (id)initForBundle:(NSBundle *)bundle; - (void)startUpdateCycle; - (void)checkForUpdatesWithDriver:(SUUpdateDriver *)updateDriver; -- (BOOL)automaticallyDownloadsUpdates; - (void)scheduleNextUpdateCheck; - (void)registerAsObserver; - (void)unregisterAsObserver;