From 30cb9843417d807657dec76112e590fb39393623 Mon Sep 17 00:00:00 2001 From: Zorg Date: Sat, 5 Mar 2016 00:17:41 -0500 Subject: [PATCH] Split SUStandardUserDriver into re-usable components --- Sparkle.xcodeproj/project.pbxproj | 38 +++- Sparkle/SUStandardUserDriver.m | 272 +++++----------------------- Sparkle/SUUserDriverCoreComponent.h | 49 +++++ Sparkle/SUUserDriverCoreComponent.m | 223 +++++++++++++++++++++++ Sparkle/SUUserDriverUIComponent.h | 39 ++++ Sparkle/SUUserDriverUIComponent.m | 181 ++++++++++++++++++ 6 files changed, 567 insertions(+), 235 deletions(-) create mode 100644 Sparkle/SUUserDriverCoreComponent.h create mode 100644 Sparkle/SUUserDriverCoreComponent.m create mode 100644 Sparkle/SUUserDriverUIComponent.h create mode 100644 Sparkle/SUUserDriverUIComponent.m diff --git a/Sparkle.xcodeproj/project.pbxproj b/Sparkle.xcodeproj/project.pbxproj index eb34de6b..ef792228 100644 --- a/Sparkle.xcodeproj/project.pbxproj +++ b/Sparkle.xcodeproj/project.pbxproj @@ -180,6 +180,10 @@ 725CB95B1C7121830064365A /* SUStandardUserDriver.m in Sources */ = {isa = PBXBuildFile; fileRef = 725CB9591C7121830064365A /* SUStandardUserDriver.m */; }; 725F97771C8A62F500265BE4 /* SUAdHocCodeSigning.m in Sources */ = {isa = PBXBuildFile; fileRef = 725F97751C8A62F500265BE4 /* SUAdHocCodeSigning.m */; }; 725F97781C8A65AC00265BE4 /* SUAdHocCodeSigning.m in Sources */ = {isa = PBXBuildFile; fileRef = 725F97751C8A62F500265BE4 /* SUAdHocCodeSigning.m */; }; + 725F977B1C8A896F00265BE4 /* SUUserDriverCoreComponent.h in Headers */ = {isa = PBXBuildFile; fileRef = 725F97791C8A896F00265BE4 /* SUUserDriverCoreComponent.h */; }; + 725F977C1C8A896F00265BE4 /* SUUserDriverCoreComponent.m in Sources */ = {isa = PBXBuildFile; fileRef = 725F977A1C8A896F00265BE4 /* SUUserDriverCoreComponent.m */; }; + 725F977F1C8A93D700265BE4 /* SUUserDriverUIComponent.h in Headers */ = {isa = PBXBuildFile; fileRef = 725F977D1C8A93D700265BE4 /* SUUserDriverUIComponent.h */; }; + 725F97801C8A93D700265BE4 /* SUUserDriverUIComponent.m in Sources */ = {isa = PBXBuildFile; fileRef = 725F977E1C8A93D700265BE4 /* SUUserDriverUIComponent.m */; }; 7268AC631AD634C200C3E0C1 /* SUBinaryDeltaCreate.m in Sources */ = {isa = PBXBuildFile; fileRef = 7268AC621AD634C200C3E0C1 /* SUBinaryDeltaCreate.m */; }; 726DF88E1C84277600188804 /* SUStatusCompletionResults.h in Headers */ = {isa = PBXBuildFile; fileRef = 726DF88D1C84277500188804 /* SUStatusCompletionResults.h */; settings = {ATTRIBUTES = (Public, ); }; }; 726E4A0E1C86881600C57C6A /* SUStandardUserDriverUIDelegate.h in Headers */ = {isa = PBXBuildFile; fileRef = 726E4A0D1C86881600C57C6A /* SUStandardUserDriverUIDelegate.h */; settings = {ATTRIBUTES = (Public, ); }; }; @@ -668,6 +672,10 @@ 725CB9591C7121830064365A /* SUStandardUserDriver.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SUStandardUserDriver.m; sourceTree = ""; }; 725F97741C8A62F500265BE4 /* SUAdHocCodeSigning.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SUAdHocCodeSigning.h; sourceTree = ""; }; 725F97751C8A62F500265BE4 /* SUAdHocCodeSigning.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SUAdHocCodeSigning.m; sourceTree = ""; }; + 725F97791C8A896F00265BE4 /* SUUserDriverCoreComponent.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SUUserDriverCoreComponent.h; sourceTree = ""; }; + 725F977A1C8A896F00265BE4 /* SUUserDriverCoreComponent.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SUUserDriverCoreComponent.m; sourceTree = ""; }; + 725F977D1C8A93D700265BE4 /* SUUserDriverUIComponent.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SUUserDriverUIComponent.h; sourceTree = ""; }; + 725F977E1C8A93D700265BE4 /* SUUserDriverUIComponent.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SUUserDriverUIComponent.m; sourceTree = ""; }; 7268AC621AD634C200C3E0C1 /* SUBinaryDeltaCreate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SUBinaryDeltaCreate.m; sourceTree = ""; }; 7268AC641AD634E400C3E0C1 /* SUBinaryDeltaCreate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SUBinaryDeltaCreate.h; sourceTree = ""; }; 726B2B5D1C645FC900388755 /* UI Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "UI Tests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -1051,13 +1059,7 @@ 61299B3A09CB056100B7442F /* User Interface */ = { isa = PBXGroup; children = ( - 7246E0A11C83B685003B4E75 /* SUStandardUpdaterController.h */, - 7246E0A21C83B685003B4E75 /* SUStandardUpdaterController.m */, - 726E4A361C89116000C57C6A /* SUStandardUserDriverDelegate.h */, - 726E4A0D1C86881600C57C6A /* SUStandardUserDriverUIDelegate.h */, - 726E4A0F1C86B5A200C57C6A /* SUStandardUserDriverRemoteDelegate.h */, - 725CB9581C7121830064365A /* SUStandardUserDriver.h */, - 725CB9591C7121830064365A /* SUStandardUserDriver.m */, + 725F97811C8A997300265BE4 /* User Driver */, 6120721009CC5C4B007FE0F6 /* SUAutomaticUpdateAlert.h */, 6120721109CC5C4B007FE0F6 /* SUAutomaticUpdateAlert.m */, 6196CFE309C71ADE000DC222 /* SUStatusController.h */, @@ -1190,6 +1192,24 @@ name = "Update Control"; sourceTree = ""; }; + 725F97811C8A997300265BE4 /* User Driver */ = { + isa = PBXGroup; + children = ( + 7246E0A11C83B685003B4E75 /* SUStandardUpdaterController.h */, + 7246E0A21C83B685003B4E75 /* SUStandardUpdaterController.m */, + 726E4A361C89116000C57C6A /* SUStandardUserDriverDelegate.h */, + 726E4A0D1C86881600C57C6A /* SUStandardUserDriverUIDelegate.h */, + 726E4A0F1C86B5A200C57C6A /* SUStandardUserDriverRemoteDelegate.h */, + 725CB9581C7121830064365A /* SUStandardUserDriver.h */, + 725CB9591C7121830064365A /* SUStandardUserDriver.m */, + 725F97791C8A896F00265BE4 /* SUUserDriverCoreComponent.h */, + 725F977A1C8A896F00265BE4 /* SUUserDriverCoreComponent.m */, + 725F977D1C8A93D700265BE4 /* SUUserDriverUIComponent.h */, + 725F977E1C8A93D700265BE4 /* SUUserDriverUIComponent.m */, + ); + name = "User Driver"; + sourceTree = ""; + }; 726B2B5E1C645FC900388755 /* UI Tests */ = { isa = PBXGroup; children = ( @@ -1295,6 +1315,7 @@ 6101347B0DD2541A0049ACDF /* SUProbingUpdateDriver.h in Headers */, 61B93C090DD112FF00DCD2F8 /* SUScheduledUpdateDriver.h in Headers */, 61A225A40D1C4AC000430CCD /* SUStandardVersionComparator.h in Headers */, + 725F977B1C8A896F00265BE4 /* SUUserDriverCoreComponent.h in Headers */, 6196CFF909C72148000DC222 /* SUStatusController.h in Headers */, 61A2279C0D1CEE7600430CCD /* SUSystemProfiler.h in Headers */, 61B93A3C0DD02D7000DCD2F8 /* SUUIBasedUpdateDriver.h in Headers */, @@ -1304,6 +1325,7 @@ 610134730DD250470049ACDF /* SUUpdateDriver.h in Headers */, 612DCBAF0D488BC60015DBEA /* SUUpdatePermissionPrompt.h in Headers */, 61B5F8ED09C4CE3C00B25A18 /* SUUpdater.h in Headers */, + 725F977F1C8A93D700265BE4 /* SUUserDriverUIComponent.h in Headers */, 6158A1C5137904B300487EC1 /* SUUpdater_Private.h in Headers */, 61A354550DF113C70076ECB1 /* SUUserInitiatedUpdateDriver.h in Headers */, 61A2259E0D1C495D00430CCD /* SUVersionComparisonProtocol.h in Headers */, @@ -1846,6 +1868,7 @@ 726F2CE61BC9C33D001971A4 /* SUOperatingSystem.m in Sources */, 618FA5230DAE8E8A0026945C /* SUPackageInstaller.m in Sources */, 61D85D6D0E10B2ED00F9B4A9 /* SUPipedUnarchiver.m in Sources */, + 725F97801C8A93D700265BE4 /* SUUserDriverUIComponent.m in Sources */, 618FA5060DAE8AB80026945C /* SUPlainInstaller.m in Sources */, 6101347C0DD2541A0049ACDF /* SUProbingUpdateDriver.m in Sources */, 61B93C0A0DD112FF00DCD2F8 /* SUScheduledUpdateDriver.m in Sources */, @@ -1855,6 +1878,7 @@ 61B93A3D0DD02D7000DCD2F8 /* SUUIBasedUpdateDriver.m in Sources */, 7246E0A41C83B685003B4E75 /* SUStandardUpdaterController.m in Sources */, 61299A8E09CA790200B7442F /* SUUnarchiver.m in Sources */, + 725F977C1C8A896F00265BE4 /* SUUserDriverCoreComponent.m in Sources */, 61B5FCDE09C52A9F00B25A18 /* SUUpdateAlert.m in Sources */, 610134740DD250470049ACDF /* SUUpdateDriver.m in Sources */, 612DCBB00D488BC60015DBEA /* SUUpdatePermissionPrompt.m in Sources */, diff --git a/Sparkle/SUStandardUserDriver.m b/Sparkle/SUStandardUserDriver.m index cf51e849..4cfcb7cb 100644 --- a/Sparkle/SUStandardUserDriver.m +++ b/Sparkle/SUStandardUserDriver.m @@ -7,9 +7,10 @@ // #import "SUStandardUserDriver.h" +#import "SUUserDriverCoreComponent.h" +#import "SUUserDriverUIComponent.h" #import "SUStandardUserDriverDelegate.h" #import "SUStandardUserDriverUIDelegate.h" -#import "SUStandardUserDriverRemoteDelegate.h" #import "SUAppcastItem.h" #import "SUVersionDisplayProtocol.h" #import "SUHost.h" @@ -17,7 +18,6 @@ #import "SUStatusController.h" #import "SUUpdateAlert.h" #import "SUAutomaticUpdateAlert.h" -#import "SUOperatingSystem.h" #import "SULocalizations.h" #import "SUApplicationInfo.h" @@ -25,51 +25,24 @@ @property (nonatomic, readonly) SUHost *host; -@property (nonatomic) BOOL askedHandlingTermination; -@property (nonatomic, readonly) BOOL handlesTermination; - -@property (nonatomic) BOOL idlesOnUpdateChecks; -@property (nonatomic) BOOL updateInProgress; - -@property (nonatomic) NSTimer *checkUpdateTimer; -@property (nonatomic, copy) void (^checkForUpdatesReply)(SUUpdateCheckTimerStatus); +@property (nonatomic, readonly) SUUserDriverCoreComponent *coreComponent; +@property (nonatomic, readonly) SUUserDriverUIComponent *uiComponent; @property (nonatomic) SUStatusController *checkingController; -@property (nonatomic, copy) void (^updateCheckStatusCompletion)(SUUserInitiatedCheckStatus); - @property (nonatomic) NSWindowController *activeUpdateAlert; - @property (nonatomic) SUStatusController *statusController; -@property (nonatomic, copy) void (^downloadStatusCompletion)(SUDownloadUpdateStatus); -@property (nonatomic, copy) void (^installUpdateHandler)(SUInstallUpdateStatus); - -@property (nonatomic, copy) void (^applicationTerminationHandler)(SUApplicationTerminationStatus); - -@property (nonatomic, copy) void (^systemPowerOffHandler)(SUSystemPowerOffStatus); - -@property (nonatomic) BOOL installingUpdateOnTermination; @end @implementation SUStandardUserDriver @synthesize host = _host; -@synthesize handlesTermination = _handlesTermination; -@synthesize askedHandlingTermination = _askedHandlingTermination; +@synthesize coreComponent = _coreComponent; +@synthesize uiComponent = _uiComponent; @synthesize delegate = _delegate; -@synthesize idlesOnUpdateChecks = _idlesOnUpdateChecks; -@synthesize updateInProgress = _updateInProgress; -@synthesize checkUpdateTimer = _checkUpdateTimer; -@synthesize checkForUpdatesReply = _checkForUpdatesReply; @synthesize checkingController = _checkingController; -@synthesize updateCheckStatusCompletion = _updateCheckStatusCompletion; @synthesize activeUpdateAlert = _activeUpdateAlert; @synthesize statusController = _statusController; -@synthesize downloadStatusCompletion = _downloadStatusCompletion; -@synthesize installUpdateHandler = _installUpdateHandler; -@synthesize applicationTerminationHandler = _applicationTerminationHandler; -@synthesize systemPowerOffHandler = _systemPowerOffHandler; -@synthesize installingUpdateOnTermination = _installingUpdateOnTermination; #pragma mark Birth @@ -79,6 +52,8 @@ if (self != nil) { _host = [[SUHost alloc] initWithBundle:hostBundle]; _delegate = delegate; + _coreComponent = [[SUUserDriverCoreComponent alloc] initWithUserDriver:self delegate:delegate]; + _uiComponent = [[SUUserDriverUIComponent alloc] initWithUserDriver:self delegate:delegate]; } return self; } @@ -87,93 +62,46 @@ - (void)idleOnUpdateChecks:(BOOL)shouldIdleOnUpdateChecks { - self.idlesOnUpdateChecks = shouldIdleOnUpdateChecks; + dispatch_async(dispatch_get_main_queue(), ^{ + [self.coreComponent idleOnUpdateChecks:shouldIdleOnUpdateChecks]; + }); +} + +- (BOOL)idlesOnUpdateChecks +{ + return self.coreComponent.idlesOnUpdateChecks; } - (void)showUpdateInProgress:(BOOL)isUpdateInProgress { dispatch_async(dispatch_get_main_queue(), ^{ - self.updateInProgress = isUpdateInProgress; + [self.coreComponent showUpdateInProgress:isUpdateInProgress]; }); } -- (BOOL)handlesTermination +- (BOOL)isUpdateInProgress { - if (!self.askedHandlingTermination) { - if ([self.delegate respondsToSelector:@selector(responsibleForSignalingApplicationTerminationForUserDriver:)]) { - _handlesTermination = ![self.delegate responsibleForSignalingApplicationTerminationForUserDriver:self]; - } else { - _handlesTermination = YES; - } - self.askedHandlingTermination = YES; - } - return _handlesTermination; + return self.coreComponent.updateInProgress; } #pragma mark Check Updates Timer -- (BOOL)isDelegateResponsibleForUpdateChecking -{ - BOOL result = NO; - if ([self.delegate respondsToSelector:@selector(responsibleForInitiatingUpdateCheckForUserDriver:)]) { - result = [self.delegate responsibleForInitiatingUpdateCheckForUserDriver:self]; - } - return result; -} - - (BOOL)willInitiateNextUpdateCheck { - return (self.checkUpdateTimer != nil); -} - -- (void)checkForUpdates:(NSTimer *)__unused timer -{ - if ([self isDelegateResponsibleForUpdateChecking]) { - if ([self.delegate respondsToSelector:@selector(initiateUpdateCheckForUserDriver:)]) { - [self.delegate initiateUpdateCheckForUserDriver:self]; - } else { - NSLog(@"Error: Delegate %@ for user driver %@ must implement initiateUpdateCheckForUserDriver: because it returned YES from responsibleForInitiatingUpdateCheckForUserDriver:", self.delegate, self); - } - } else { - if (self.checkForUpdatesReply != nil) { - self.checkForUpdatesReply(SUCheckForUpdateNow); - self.checkForUpdatesReply = nil; - } - } - - [self _invalidateUpdateCheckTimer]; + return [self.coreComponent willInitiateNextUpdateCheck]; } - (void)startUpdateCheckTimerWithNextTimeInterval:(NSTimeInterval)timeInterval reply:(void (^)(SUUpdateCheckTimerStatus))reply { dispatch_async(dispatch_get_main_queue(), ^{ - if ([self isDelegateResponsibleForUpdateChecking]) { - reply(SUCheckForUpdateWillOccurLater); - } else { - self.checkForUpdatesReply = reply; - } - - self.checkUpdateTimer = [NSTimer scheduledTimerWithTimeInterval:timeInterval target:self selector:@selector(checkForUpdates:) userInfo:nil repeats:NO]; + [self.coreComponent startUpdateCheckTimerWithNextTimeInterval:timeInterval reply:reply]; }); } -- (void)_invalidateUpdateCheckTimer -{ - if (self.checkUpdateTimer != nil) { - [self.checkUpdateTimer invalidate]; - self.checkUpdateTimer = nil; - - if (self.checkForUpdatesReply != nil) { - self.checkForUpdatesReply(SUCheckForUpdateWillOccurLater); - self.checkForUpdatesReply = nil; - } - } -} - - (void)invalidateUpdateCheckTimer { dispatch_async(dispatch_get_main_queue(), ^{ - [self _invalidateUpdateCheckTimer]; + [self.coreComponent invalidateUpdateCheckTimer]; }); } @@ -260,24 +188,13 @@ [[self.statusController window] makeKeyAndOrderFront:self]; [NSApp requestUserAttention:NSInformationalRequest]; - self.installUpdateHandler = installUpdateHandler; + [self.coreComponent registerInstallUpdateHandler:installUpdateHandler]; }); } - (void)installAndRestart:(id)__unused sender { - if (self.installUpdateHandler != nil) { - self.installUpdateHandler(SUInstallAndRelaunchUpdateNow); - self.installUpdateHandler = nil; - } -} - -- (void)cancelInstallAndRestart -{ - if (self.installUpdateHandler != nil) { - self.installUpdateHandler(SUCancelUpdateInstallation); - self.installUpdateHandler = nil; - } + [self.coreComponent installAndRestart]; } #pragma mark Check for Updates @@ -285,7 +202,7 @@ - (void)showUserInitiatedUpdateCheckWithCompletion:(void (^)(SUUserInitiatedCheckStatus))updateCheckStatusCompletion { dispatch_async(dispatch_get_main_queue(), ^{ - self.updateCheckStatusCompletion = updateCheckStatusCompletion; + [self.coreComponent registerUpdateCheckStatusHandler:updateCheckStatusCompletion]; self.checkingController = [[SUStatusController alloc] initWithHost:self.host]; [[self.checkingController window] center]; // Force the checking controller to load its window. @@ -302,13 +219,8 @@ }); } -- (void)cancelCheckForUpdates +- (void)closeCheckingWindow { - if (self.updateCheckStatusCompletion != nil) { - self.updateCheckStatusCompletion(SUUserInitiatedCheckCancelled); - self.updateCheckStatusCompletion = nil; - } - if (self.checkingController != nil) { [[self.checkingController window] close]; @@ -318,22 +230,15 @@ - (void)cancelCheckForUpdates:(id)__unused sender { - [self cancelCheckForUpdates]; + [self.coreComponent cancelUpdateCheckStatus]; + [self closeCheckingWindow]; } - (void)dismissUserInitiatedUpdateCheck { dispatch_async(dispatch_get_main_queue(), ^{ - if (self.updateCheckStatusCompletion != nil) { - self.updateCheckStatusCompletion(SUUserInitiatedCheckDone); - self.updateCheckStatusCompletion = nil; - } - - if (self.checkingController != nil) - { - [[self.checkingController window] close]; - self.checkingController = nil; - } + [self.coreComponent completeUpdateCheckStatus]; + [self closeCheckingWindow]; }); } @@ -388,7 +293,7 @@ - (void)showDownloadInitiatedWithCompletion:(void (^)(SUDownloadUpdateStatus))downloadUpdateStatusCompletion { dispatch_async(dispatch_get_main_queue(), ^{ - self.downloadStatusCompletion = downloadUpdateStatusCompletion; + [self.coreComponent registerDownloadStatusHandler:downloadUpdateStatusCompletion]; self.statusController = [[SUStatusController alloc] initWithHost:self.host]; [self.statusController beginActionWithTitle:SULocalizedString(@"Downloading update...", @"Take care not to overflow the status window.") maxProgressValue:0.0 statusText:nil]; @@ -397,17 +302,9 @@ }); } -- (void)cancelDownload -{ - if (self.downloadStatusCompletion != nil) { - self.downloadStatusCompletion(SUDownloadUpdateCancelled); - self.downloadStatusCompletion = nil; - } -} - - (void)cancelDownload:(id)__unused sender { - [self cancelDownload]; + [self.coreComponent cancelDownloadStatus]; } - (void)showDownloadDidReceiveResponse:(NSURLResponse *)response @@ -437,10 +334,7 @@ - (void)showDownloadFinishedAndStartedExtractingUpdate { dispatch_async(dispatch_get_main_queue(), ^{ - if (self.downloadStatusCompletion != nil) { - self.downloadStatusCompletion(SUDownloadUpdateDone); - self.downloadStatusCompletion = nil; - } + [self.coreComponent completeDownloadStatus]; [self.statusController beginActionWithTitle:SULocalizedString(@"Extracting update...", @"Take care not to overflow the status window.") maxProgressValue:0.0 statusText:nil]; [self.statusController setButtonEnabled:NO]; @@ -450,7 +344,6 @@ - (void)showExtractionReceivedProgress:(double)progress { dispatch_async(dispatch_get_main_queue(), ^{ - // We do this here instead of in extractUpdate so that we only have a determinate progress bar for archives with progress. if ([self.statusController maxProgressValue] == 0.0) { [self.statusController setMaxProgressValue:1]; } @@ -471,71 +364,26 @@ - (void)registerApplicationTermination:(void (^)(SUApplicationTerminationStatus))applicationTerminationHandler { dispatch_async(dispatch_get_main_queue(), ^{ - self.installingUpdateOnTermination = YES; - - // Sudden termination is available on 10.6+ - [[NSProcessInfo processInfo] disableSuddenTermination]; - - self.applicationTerminationHandler = applicationTerminationHandler; - - if (self.handlesTermination) { - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate:) name:NSApplicationWillTerminateNotification object:nil]; - } + [self.uiComponent registerApplicationTermination:applicationTerminationHandler]; }); } -- (void)cancelObservingApplicationTermination -{ - if (self.installingUpdateOnTermination) { - [[NSProcessInfo processInfo] enableSuddenTermination]; - - if (self.handlesTermination) { - [[NSNotificationCenter defaultCenter] removeObserver:self name:NSApplicationWillTerminateNotification object:nil]; - } - - if (self.applicationTerminationHandler != nil) { - self.applicationTerminationHandler(SUApplicationStoppedObservingTermination); - self.applicationTerminationHandler = nil; - } - - self.installingUpdateOnTermination = NO; - } -} - - (void)unregisterApplicationTermination { dispatch_async(dispatch_get_main_queue(), ^{ - [self cancelObservingApplicationTermination]; + [self.uiComponent unregisterApplicationTermination]; }); } -- (void)applicationWillTerminate:(NSNotification *)__unused note -{ - [self sendApplicationTerminationSignal]; -} - - (NSApplicationTerminateReply)sendApplicationTerminationSignal { - if (self.installingUpdateOnTermination) { - if (self.applicationTerminationHandler != nil) { - self.applicationTerminationHandler(SUApplicationWillTerminate); - self.applicationTerminationHandler = nil; - } - - return NSTerminateLater; - } - - return NSTerminateNow; + return [self.uiComponent sendApplicationTerminationSignal]; } - (void)terminateApplication { dispatch_async(dispatch_get_main_queue(), ^{ - if (self.installingUpdateOnTermination && !self.handlesTermination) { - [NSApp replyToApplicationShouldTerminate:YES]; - } else { - [NSApp terminate:nil]; - } + [self.uiComponent terminateApplication]; }); } @@ -544,37 +392,17 @@ - (void)registerSystemPowerOff:(void (^)(SUSystemPowerOffStatus))systemPowerOffHandler { dispatch_async(dispatch_get_main_queue(), ^{ - self.systemPowerOffHandler = systemPowerOffHandler; - - [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(systemWillPowerOff:) name:NSWorkspaceWillPowerOffNotification object:nil]; + [self.uiComponent registerSystemPowerOff:systemPowerOffHandler]; }); } -- (void)cancelObservingSystemPowerOff -{ - if (self.systemPowerOffHandler != nil) { - self.systemPowerOffHandler(SUStoppedObservingSystemPowerOff); - self.systemPowerOffHandler = nil; - - [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self name:NSWorkspaceWillPowerOffNotification object:nil]; - } -} - - (void)unregisterSystemPowerOff { dispatch_async(dispatch_get_main_queue(), ^{ - [self cancelObservingSystemPowerOff]; + [self.uiComponent unregisterSystemPowerOff]; }); } -- (void)systemWillPowerOff:(NSNotification *)__unused notification -{ - if (self.systemPowerOffHandler != nil) { - self.systemPowerOffHandler(SUSystemWillPowerOff); - self.systemPowerOffHandler = nil; - } -} - #pragma mark Aborting Everything - (void)_dismissUpdateInstallation @@ -582,14 +410,10 @@ // Make sure everything we call here does not dispatch async to main queue // because we are already on the main queue (and I've been bitten in the past by this before) - // Note: self.idlesOnUpdateChecks is intentionally not touched in case this instance is re-used + [self.coreComponent dismissUpdateInstallation]; + [self.uiComponent dismissUpdateInstallation]; - self.updateInProgress = NO; - - [self _invalidateUpdateCheckTimer]; - - [self cancelCheckForUpdates]; - [self cancelDownload]; + [self closeCheckingWindow]; if (self.statusController) { [self.statusController close]; @@ -600,10 +424,6 @@ [self.activeUpdateAlert close]; self.activeUpdateAlert = nil; } - - [self cancelObservingApplicationTermination]; - [self cancelObservingSystemPowerOff]; - [self cancelInstallAndRestart]; } - (void)dismissUpdateInstallation @@ -616,12 +436,8 @@ - (void)invalidate { // Make sure any remote handlers will not be invoked - self.applicationTerminationHandler = nil; - self.systemPowerOffHandler = nil; - self.installUpdateHandler = nil; - self.checkForUpdatesReply = nil; - self.updateCheckStatusCompletion = nil; - self.downloadStatusCompletion = nil; + [self.coreComponent invalidate]; + [self.uiComponent invalidate]; // Dismiss the installation normally [self _dismissUpdateInstallation]; diff --git a/Sparkle/SUUserDriverCoreComponent.h b/Sparkle/SUUserDriverCoreComponent.h new file mode 100644 index 00000000..2a86a831 --- /dev/null +++ b/Sparkle/SUUserDriverCoreComponent.h @@ -0,0 +1,49 @@ +// +// SUUserDriverCoreComponent.h +// Sparkle +// +// Created by Mayur Pawashe on 3/4/16. +// Copyright © 2016 Sparkle Project. All rights reserved. +// + +#import +#import "SUStatusCompletionResults.h" + +@protocol SUStandardUserDriverDelegate; +@protocol SUUserDriver; + +@interface SUUserDriverCoreComponent : NSObject + +- (instancetype)initWithUserDriver:(id)userDriver delegate:(id)delegate; + +@property (nonatomic, readonly, weak) id delegate; + +- (void)idleOnUpdateChecks:(BOOL)shouldIdleOnUpdateChecks; + +@property (nonatomic, readonly) BOOL idlesOnUpdateChecks; + +- (void)showUpdateInProgress:(BOOL)isUpdateInProgress; + +@property (nonatomic, readonly, getter=isUpdateInProgress) BOOL updateInProgress; + +@property (nonatomic, readonly) BOOL willInitiateNextUpdateCheck; + +- (void)startUpdateCheckTimerWithNextTimeInterval:(NSTimeInterval)timeInterval reply:(void (^)(SUUpdateCheckTimerStatus))reply; +- (void)invalidateUpdateCheckTimer; + +- (void)registerInstallUpdateHandler:(void (^)(SUInstallUpdateStatus))installUpdateHandler; +- (void)installAndRestart; + +- (void)registerUpdateCheckStatusHandler:(void (^)(SUUserInitiatedCheckStatus))updateCheckStatusCompletion; +- (void)cancelUpdateCheckStatus; +- (void)completeUpdateCheckStatus; + +- (void)registerDownloadStatusHandler:(void (^)(SUDownloadUpdateStatus))downloadUpdateStatusCompletion; +- (void)cancelDownloadStatus; +- (void)completeDownloadStatus; + +- (void)dismissUpdateInstallation; + +- (void)invalidate; + +@end diff --git a/Sparkle/SUUserDriverCoreComponent.m b/Sparkle/SUUserDriverCoreComponent.m new file mode 100644 index 00000000..145e5ccd --- /dev/null +++ b/Sparkle/SUUserDriverCoreComponent.m @@ -0,0 +1,223 @@ +// +// SUUserDriverCoreComponent.m +// Sparkle +// +// Created by Mayur Pawashe on 3/4/16. +// Copyright © 2016 Sparkle Project. All rights reserved. +// + +#import "SUUserDriverCoreComponent.h" +#import "SUStandardUserDriverDelegate.h" +#import "SUStandardUserDriverRemoteDelegate.h" +#import "SUUserDriver.h" + +#ifdef _APPKITDEFINES_H +#error This is a "core" class and should NOT import AppKit +#endif + +@interface SUUserDriverCoreComponent () + +@property (nonatomic, weak, readonly) id userDriver; + +@property (nonatomic) BOOL idlesOnUpdateChecks; +@property (nonatomic) BOOL updateInProgress; + +@property (nonatomic) NSTimer *checkUpdateTimer; +@property (nonatomic, copy) void (^checkForUpdatesReply)(SUUpdateCheckTimerStatus); + +@property (nonatomic, copy) void (^installUpdateHandler)(SUInstallUpdateStatus); +@property (nonatomic, copy) void (^updateCheckStatusCompletion)(SUUserInitiatedCheckStatus); +@property (nonatomic, copy) void (^downloadStatusCompletion)(SUDownloadUpdateStatus); + +@end + +@implementation SUUserDriverCoreComponent + +@synthesize userDriver = _userDriver; +@synthesize delegate = _delegate; +@synthesize idlesOnUpdateChecks = _idlesOnUpdateChecks; +@synthesize updateInProgress = _updateInProgress; +@synthesize checkUpdateTimer = _checkUpdateTimer; +@synthesize checkForUpdatesReply = _checkForUpdatesReply; +@synthesize installUpdateHandler = _installUpdateHandler; +@synthesize updateCheckStatusCompletion = _updateCheckStatusCompletion; +@synthesize downloadStatusCompletion = _downloadStatusCompletion; + +#pragma mark Birth + +- (instancetype)initWithUserDriver:(id)userDriver delegate:(id)delegate +{ + self = [super init]; + if (self != nil) { + _userDriver = userDriver; + _delegate = delegate; + } + return self; +} + +#pragma mark Is Update Busy? + +- (void)idleOnUpdateChecks:(BOOL)shouldIdleOnUpdateChecks +{ + self.idlesOnUpdateChecks = shouldIdleOnUpdateChecks; +} + +- (void)showUpdateInProgress:(BOOL)isUpdateInProgress +{ + self.updateInProgress = isUpdateInProgress; +} + +#pragma mark Check Updates Timer + +- (BOOL)isDelegateResponsibleForUpdateChecking +{ + BOOL result = NO; + if ([self.delegate respondsToSelector:@selector(responsibleForInitiatingUpdateCheckForUserDriver:)]) { + result = [self.delegate responsibleForInitiatingUpdateCheckForUserDriver:self.userDriver]; + } + return result; +} + +- (BOOL)willInitiateNextUpdateCheck +{ + return (self.checkUpdateTimer != nil); +} + +- (void)checkForUpdates:(NSTimer *)__unused timer +{ + if ([self isDelegateResponsibleForUpdateChecking]) { + if ([self.delegate respondsToSelector:@selector(initiateUpdateCheckForUserDriver:)]) { + [self.delegate initiateUpdateCheckForUserDriver:self.userDriver]; + } else { + NSLog(@"Error: Delegate %@ for user driver %@ must implement initiateUpdateCheckForUserDriver: because it returned YES from responsibleForInitiatingUpdateCheckForUserDriver:", self.delegate, self); + } + } else { + if (self.checkForUpdatesReply != nil) { + self.checkForUpdatesReply(SUCheckForUpdateNow); + self.checkForUpdatesReply = nil; + } + } + + [self invalidateUpdateCheckTimer]; +} + +- (void)startUpdateCheckTimerWithNextTimeInterval:(NSTimeInterval)timeInterval reply:(void (^)(SUUpdateCheckTimerStatus))reply +{ + if ([self isDelegateResponsibleForUpdateChecking]) { + reply(SUCheckForUpdateWillOccurLater); + } else { + self.checkForUpdatesReply = reply; + } + + self.checkUpdateTimer = [NSTimer scheduledTimerWithTimeInterval:timeInterval target:self selector:@selector(checkForUpdates:) userInfo:nil repeats:NO]; +} + +- (void)invalidateUpdateCheckTimer +{ + if (self.checkUpdateTimer != nil) { + [self.checkUpdateTimer invalidate]; + self.checkUpdateTimer = nil; + + if (self.checkForUpdatesReply != nil) { + self.checkForUpdatesReply(SUCheckForUpdateWillOccurLater); + self.checkForUpdatesReply = nil; + } + } +} + +#pragma mark Install Updates + +- (void)registerInstallUpdateHandler:(void (^)(SUInstallUpdateStatus))installUpdateHandler +{ + self.installUpdateHandler = installUpdateHandler; +} + +- (void)installAndRestart +{ + if (self.installUpdateHandler != nil) { + self.installUpdateHandler(SUInstallAndRelaunchUpdateNow); + self.installUpdateHandler = nil; + } +} + +- (void)cancelInstallAndRestart +{ + if (self.installUpdateHandler != nil) { + self.installUpdateHandler(SUCancelUpdateInstallation); + self.installUpdateHandler = nil; + } +} + +#pragma mark Update Check Status + +- (void)registerUpdateCheckStatusHandler:(void (^)(SUUserInitiatedCheckStatus))updateCheckStatusCompletion +{ + self.updateCheckStatusCompletion = updateCheckStatusCompletion; +} + +- (void)cancelUpdateCheckStatus +{ + if (self.updateCheckStatusCompletion != nil) { + self.updateCheckStatusCompletion(SUUserInitiatedCheckCancelled); + self.updateCheckStatusCompletion = nil; + } +} + +- (void)completeUpdateCheckStatus +{ + if (self.updateCheckStatusCompletion != nil) { + self.updateCheckStatusCompletion(SUUserInitiatedCheckDone); + self.updateCheckStatusCompletion = nil; + } +} + +#pragma mark Download Status + +- (void)registerDownloadStatusHandler:(void (^)(SUDownloadUpdateStatus))downloadUpdateStatusCompletion +{ + self.downloadStatusCompletion = downloadUpdateStatusCompletion; +} + +- (void)cancelDownloadStatus +{ + if (self.downloadStatusCompletion != nil) { + self.downloadStatusCompletion(SUDownloadUpdateCancelled); + self.downloadStatusCompletion = nil; + } +} + +- (void)completeDownloadStatus +{ + if (self.downloadStatusCompletion != nil) { + self.downloadStatusCompletion(SUDownloadUpdateDone); + self.downloadStatusCompletion = nil; + } +} + +#pragma mark Aborting Everything + +- (void)dismissUpdateInstallation +{ + // Note: self.idlesOnUpdateChecks is intentionally not touched in case this instance is re-used + + self.updateInProgress = NO; + + [self cancelUpdateCheckStatus]; + [self cancelDownloadStatus]; + [self cancelInstallAndRestart]; + [self invalidateUpdateCheckTimer]; +} + +- (void)invalidate +{ + // Make sure any remote handlers will not be invoked + self.checkForUpdatesReply = nil; + self.downloadStatusCompletion = nil; + self.installUpdateHandler = nil; + self.updateCheckStatusCompletion = nil; + + // Dismiss the installation normally + [self dismissUpdateInstallation]; +} + +@end diff --git a/Sparkle/SUUserDriverUIComponent.h b/Sparkle/SUUserDriverUIComponent.h new file mode 100644 index 00000000..87ee56fe --- /dev/null +++ b/Sparkle/SUUserDriverUIComponent.h @@ -0,0 +1,39 @@ +// +// SUUserDriverUIComponent.h +// Sparkle +// +// Created by Mayur Pawashe on 3/4/16. +// Copyright © 2016 Sparkle Project. All rights reserved. +// + +#import +#import "SUStatusCompletionResults.h" + +@protocol SUStandardUserDriverDelegate; +@protocol SUUserDriver; + +@interface SUUserDriverUIComponent : NSObject + +- (instancetype)initWithUserDriver:(id)userDriver delegate:(id)delegate; + +@property (nonatomic, readonly, weak) id delegate; + +- (void)registerApplicationTermination:(void (^)(SUApplicationTerminationStatus))applicationTerminationHandler; + +- (void)cancelObservingApplicationTermination; + +- (void)unregisterApplicationTermination; + +- (NSApplicationTerminateReply)sendApplicationTerminationSignal; + +- (void)terminateApplication; + +- (void)registerSystemPowerOff:(void (^)(SUSystemPowerOffStatus))systemPowerOffHandler; + +- (void)unregisterSystemPowerOff; + +- (void)dismissUpdateInstallation; + +- (void)invalidate; + +@end diff --git a/Sparkle/SUUserDriverUIComponent.m b/Sparkle/SUUserDriverUIComponent.m new file mode 100644 index 00000000..e43afdc9 --- /dev/null +++ b/Sparkle/SUUserDriverUIComponent.m @@ -0,0 +1,181 @@ +// +// SUUserDriverUIComponent.m +// Sparkle +// +// Created by Mayur Pawashe on 3/4/16. +// Copyright © 2016 Sparkle Project. All rights reserved. +// + +#import "SUUserDriverUIComponent.h" +#import "SUStandardUserDriverDelegate.h" +#import "SUStandardUserDriverRemoteDelegate.h" +#import "SUUserDriver.h" + +@interface SUUserDriverUIComponent () + +@property (nonatomic, weak, readonly) id userDriver; + +@property (nonatomic) BOOL installingUpdateOnTermination; +@property (nonatomic) BOOL askedHandlingTermination; +@property (nonatomic, readonly) BOOL handlesTermination; + +@property (nonatomic, copy) void (^applicationTerminationHandler)(SUApplicationTerminationStatus); +@property (nonatomic, copy) void (^systemPowerOffHandler)(SUSystemPowerOffStatus); + +@end + +@implementation SUUserDriverUIComponent + +@synthesize userDriver = _userDriver; +@synthesize delegate = _delegate; +@synthesize installingUpdateOnTermination = _installingUpdateOnTermination; +@synthesize askedHandlingTermination = _askedHandlingTermination; +@synthesize handlesTermination = _handlesTermination; +@synthesize applicationTerminationHandler = _applicationTerminationHandler; +@synthesize systemPowerOffHandler = _systemPowerOffHandler; + +#pragma mark Birth + +- (instancetype)initWithUserDriver:(id)userDriver delegate:(id)delegate +{ + self = [super init]; + if (self != nil) { + _userDriver = userDriver; + _delegate = delegate; + } + return self; +} + +#pragma mark Application Death + +- (BOOL)handlesTermination +{ + if (!self.askedHandlingTermination) { + if ([self.delegate respondsToSelector:@selector(responsibleForSignalingApplicationTerminationForUserDriver:)]) { + _handlesTermination = ![self.delegate responsibleForSignalingApplicationTerminationForUserDriver:self.userDriver]; + } else { + _handlesTermination = YES; + } + self.askedHandlingTermination = YES; + } + return _handlesTermination; +} + +- (void)registerApplicationTermination:(void (^)(SUApplicationTerminationStatus))applicationTerminationHandler +{ + self.installingUpdateOnTermination = YES; + + // Sudden termination is available on 10.6+ + [[NSProcessInfo processInfo] disableSuddenTermination]; + + self.applicationTerminationHandler = applicationTerminationHandler; + + if (self.handlesTermination) { + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate:) name:NSApplicationWillTerminateNotification object:nil]; + } +} + +- (void)cancelObservingApplicationTermination +{ + if (self.installingUpdateOnTermination) { + [[NSProcessInfo processInfo] enableSuddenTermination]; + + if (self.handlesTermination) { + [[NSNotificationCenter defaultCenter] removeObserver:self name:NSApplicationWillTerminateNotification object:nil]; + } + + if (self.applicationTerminationHandler != nil) { + self.applicationTerminationHandler(SUApplicationStoppedObservingTermination); + self.applicationTerminationHandler = nil; + } + + self.installingUpdateOnTermination = NO; + } +} + +- (void)unregisterApplicationTermination +{ + [self cancelObservingApplicationTermination]; +} + +- (void)applicationWillTerminate:(NSNotification *)__unused note +{ + [self sendApplicationTerminationSignal]; +} + +- (NSApplicationTerminateReply)sendApplicationTerminationSignal +{ + if (self.installingUpdateOnTermination) { + if (self.applicationTerminationHandler != nil) { + self.applicationTerminationHandler(SUApplicationWillTerminate); + self.applicationTerminationHandler = nil; + } + + return NSTerminateLater; + } + + return NSTerminateNow; +} + +- (void)terminateApplication +{ + dispatch_async(dispatch_get_main_queue(), ^{ + if (self.installingUpdateOnTermination && !self.handlesTermination) { + [NSApp replyToApplicationShouldTerminate:YES]; + } else { + [NSApp terminate:nil]; + } + }); +} + +#pragma mark System Death + +- (void)registerSystemPowerOff:(void (^)(SUSystemPowerOffStatus))systemPowerOffHandler +{ + self.systemPowerOffHandler = systemPowerOffHandler; + + [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(systemWillPowerOff:) name:NSWorkspaceWillPowerOffNotification object:nil]; +} + +- (void)cancelObservingSystemPowerOff +{ + if (self.systemPowerOffHandler != nil) { + self.systemPowerOffHandler(SUStoppedObservingSystemPowerOff); + self.systemPowerOffHandler = nil; + + [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self name:NSWorkspaceWillPowerOffNotification object:nil]; + } +} + +- (void)unregisterSystemPowerOff +{ + [self cancelObservingSystemPowerOff]; +} + +- (void)systemWillPowerOff:(NSNotification *)__unused notification +{ + if (self.systemPowerOffHandler != nil) { + self.systemPowerOffHandler(SUSystemWillPowerOff); + self.systemPowerOffHandler = nil; + } +} + +#pragma mark Aborting Everything + +- (void)dismissUpdateInstallation +{ + [self cancelObservingApplicationTermination]; + [self cancelObservingSystemPowerOff]; +} + +- (void)invalidate +{ + // Make sure any remote handlers will not be invoked + self.applicationTerminationHandler = nil; + self.systemPowerOffHandler = nil; + + // Dismiss the installation normally + [self dismissUpdateInstallation]; +} + +@end