From f630188255402eb29f8e055a1168c2e2d1c7ca98 Mon Sep 17 00:00:00 2001 From: Zorg Date: Sun, 28 Feb 2016 01:39:15 -0500 Subject: [PATCH] Remove/modernize a bunch of cruft Yeah, this isn't backwards compatible with older code that uses Sparkle Few notes: * Mostly everything from *.pch has been moved out for saner dependency tracking * 10.7 compatibility has been removed out so I don't import Cocoa when I don't have to * sharedUpdaters/SUUpdater singletons has been removed out so updaters can actually be deallocated properly (and since singletons are generally horrible idea anyway) * SUUpdater doesn't have UI/cocoa/bindings/menu validation code. Its new initializer method takes a user driver (perhaps in the future I'll create another class that "binds" the UI and core) --- Sparkle.xcodeproj/project.pbxproj | 8 ++ Sparkle/Autoupdate/Autoupdate.m | 1 + Sparkle/SUAppcast.m | 2 + Sparkle/SUAppcastItem.m | 1 + Sparkle/SUAutomaticUpdateAlert.m | 1 + Sparkle/SUAutomaticUpdateDriver.m | 3 +- Sparkle/SUBasicUpdateDriver.m | 2 + Sparkle/SUBinaryDeltaApply.h | 2 + Sparkle/SUBinaryDeltaUnarchiver.m | 1 - Sparkle/SUConstants.m | 1 + Sparkle/SUFileManager.m | 41 +----- Sparkle/SUGuidedPackageInstaller.m | 2 +- Sparkle/SUHost.m | 1 + Sparkle/SUInstaller.m | 1 + Sparkle/SULocalizations.h | 16 +++ Sparkle/SUPackageInstaller.m | 1 + Sparkle/SUParameterAssert.h | 17 +++ Sparkle/SUPlainInstaller.m | 2 + Sparkle/SUSparkleUserUpdaterDriver.m | 1 + Sparkle/SUStatusController.m | 1 + Sparkle/SUUpdateAlert.m | 1 + Sparkle/SUUpdatePermissionPrompt.m | 1 + Sparkle/SUUpdater.h | 6 +- Sparkle/SUUpdater.m | 157 +++++------------------ Sparkle/SUUserUpdaterDriver.h | 2 +- Sparkle/Sparkle.pch | 11 -- Vendor/CocoatechCore/NTSynchronousTask.h | 2 + 27 files changed, 110 insertions(+), 175 deletions(-) create mode 100644 Sparkle/SULocalizations.h create mode 100644 Sparkle/SUParameterAssert.h diff --git a/Sparkle.xcodeproj/project.pbxproj b/Sparkle.xcodeproj/project.pbxproj index 54552499..a8e09501 100644 --- a/Sparkle.xcodeproj/project.pbxproj +++ b/Sparkle.xcodeproj/project.pbxproj @@ -171,6 +171,8 @@ 720B16441C66433D006985FB /* UITests-Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = 720B16421C66433D006985FB /* UITests-Info.plist */; }; 720B16451C66433D006985FB /* SUTestApplicationTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 720B16431C66433D006985FB /* SUTestApplicationTest.swift */; }; 7210C7681B9A9A1500EB90AC /* SUUnarchiverTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7210C7671B9A9A1500EB90AC /* SUUnarchiverTest.swift */; }; + 72162B061C82C8B30013C1C5 /* SUParameterAssert.h in Headers */ = {isa = PBXBuildFile; fileRef = 72162B051C82C8B30013C1C5 /* SUParameterAssert.h */; }; + 72162B081C82C9600013C1C5 /* SULocalizations.h in Headers */ = {isa = PBXBuildFile; fileRef = 72162B071C82C9600013C1C5 /* SULocalizations.h */; }; 721CF1A71AD7643600D9AC09 /* bsdiff.c in Sources */ = {isa = PBXBuildFile; fileRef = 5D06E8DB0FD68CB9005AE3F6 /* bsdiff.c */; settings = {COMPILER_FLAGS = "-w"; }; }; 721CF1A81AD7644100D9AC09 /* bspatch.c in Sources */ = {isa = PBXBuildFile; fileRef = 5D06E8DC0FD68CB9005AE3F6 /* bspatch.c */; settings = {COMPILER_FLAGS = "-w"; }; }; 721CF1A91AD7644C00D9AC09 /* sais.c in Sources */ = {isa = PBXBuildFile; fileRef = 7223E7611AD1AEFF008E3161 /* sais.c */; settings = {COMPILER_FLAGS = "-w"; }; }; @@ -633,6 +635,8 @@ 720B16421C66433D006985FB /* UITests-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = "UITests-Info.plist"; path = "UITests/UITests-Info.plist"; sourceTree = SOURCE_ROOT; }; 720B16431C66433D006985FB /* SUTestApplicationTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SUTestApplicationTest.swift; path = UITests/SUTestApplicationTest.swift; sourceTree = SOURCE_ROOT; }; 7210C7671B9A9A1500EB90AC /* SUUnarchiverTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SUUnarchiverTest.swift; sourceTree = ""; }; + 72162B051C82C8B30013C1C5 /* SUParameterAssert.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SUParameterAssert.h; sourceTree = ""; }; + 72162B071C82C9600013C1C5 /* SULocalizations.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SULocalizations.h; sourceTree = ""; }; 7223E7611AD1AEFF008E3161 /* sais.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = sais.c; sourceTree = ""; }; 7223E7621AD1AEFF008E3161 /* sais.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = sais.h; sourceTree = ""; }; 725CB9561C7120410064365A /* SUUserUpdaterDriver.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SUUserUpdaterDriver.h; sourceTree = ""; }; @@ -1051,6 +1055,8 @@ children = ( 61299B3509CB04E000B7442F /* Sparkle.h */, 61CFB3280E385186007A1735 /* Sparkle.pch */, + 72162B071C82C9600013C1C5 /* SULocalizations.h */, + 72162B051C82C8B30013C1C5 /* SUParameterAssert.h */, 61299A5B09CA6D4500B7442F /* SUConstants.h */, 61299A5F09CA6EB100B7442F /* SUConstants.m */, 55E6F33219EC9F6C00005E76 /* SUErrors.h */, @@ -1189,12 +1195,14 @@ 725CB95A1C7121830064365A /* SUSparkleUserUpdaterDriver.h in Headers */, 61EF67590E25C5B400F754E0 /* SUHost.h in Headers */, 725CB9571C7120410064365A /* SUUserUpdaterDriver.h in Headers */, + 72162B061C82C8B30013C1C5 /* SUParameterAssert.h in Headers */, 72A450561C6FC10400D67EEA /* SUUpdateAlertChoice.h in Headers */, 72A450531C69A68900D67EEA /* SUUpdatePermissionPromptResult.h in Headers */, 611142E910FB1BE5009810AA /* bspatch.h in Headers */, 6160E7E10D3B4A8800E9CD71 /* NTSynchronousTask.h in Headers */, 61299B3609CB04E000B7442F /* Sparkle.h in Headers */, 61CFB3290E385186007A1735 /* Sparkle.pch in Headers */, + 72162B081C82C9600013C1C5 /* SULocalizations.h in Headers */, 61B5FC0D09C4FC8200B25A18 /* SUAppcast.h in Headers */, 61B5FC7009C51F4A00B25A18 /* SUAppcastItem.h in Headers */, 6120721209CC5C4B007FE0F6 /* SUAutomaticUpdateAlert.h in Headers */, diff --git a/Sparkle/Autoupdate/Autoupdate.m b/Sparkle/Autoupdate/Autoupdate.m index 70b39fc2..91f55851 100644 --- a/Sparkle/Autoupdate/Autoupdate.m +++ b/Sparkle/Autoupdate/Autoupdate.m @@ -4,6 +4,7 @@ #import "SUStandardVersionComparator.h" #import "SUStatusController.h" #import "SULog.h" +#import "SULocalizations.h" #include diff --git a/Sparkle/SUAppcast.m b/Sparkle/SUAppcast.m index 4e81df61..237a0e71 100644 --- a/Sparkle/SUAppcast.m +++ b/Sparkle/SUAppcast.m @@ -14,6 +14,8 @@ #import "SUAppcast.h" #import "SUConstants.h" #import "SULog.h" +#import "SUErrors.h" +#import "SULocalizations.h" @interface NSXMLElement (SUAppcastExtensions) @property (readonly, copy) NSDictionary *attributesAsDictionary; diff --git a/Sparkle/SUAppcastItem.m b/Sparkle/SUAppcastItem.m index 776b6fb5..407b9048 100644 --- a/Sparkle/SUAppcastItem.m +++ b/Sparkle/SUAppcastItem.m @@ -13,6 +13,7 @@ #import "SUVersionComparisonProtocol.h" #import "SUAppcastItem.h" #import "SULog.h" +#import "SUConstants.h" @interface SUAppcastItem () @property (copy, readwrite) NSString *title; diff --git a/Sparkle/SUAutomaticUpdateAlert.m b/Sparkle/SUAutomaticUpdateAlert.m index c1e24d7f..7ccb3e49 100644 --- a/Sparkle/SUAutomaticUpdateAlert.m +++ b/Sparkle/SUAutomaticUpdateAlert.m @@ -9,6 +9,7 @@ #import "SUAutomaticUpdateAlert.h" #import "SUHost.h" +#import "SULocalizations.h" @interface SUAutomaticUpdateAlert () @property (strong) void(^completionBlock)(SUAutomaticInstallationChoice); diff --git a/Sparkle/SUAutomaticUpdateDriver.m b/Sparkle/SUAutomaticUpdateDriver.m index 087a8b20..08e7b884 100644 --- a/Sparkle/SUAutomaticUpdateDriver.m +++ b/Sparkle/SUAutomaticUpdateDriver.m @@ -7,10 +7,11 @@ // #import "SUAutomaticUpdateDriver.h" - +#import "SULocalizations.h" #import "SUUpdater.h" #import "SUHost.h" #import "SUConstants.h" +#import "SUErrors.h" // If the user hasn't quit in a week, ask them if they want to relaunch to get the latest bits. It doesn't matter that this measure of "one day" is imprecise. static const NSTimeInterval SUAutomaticUpdatePromptImpatienceTimer = 60 * 60 * 24 * 7; diff --git a/Sparkle/SUBasicUpdateDriver.m b/Sparkle/SUBasicUpdateDriver.m index 4a799e66..076dacdf 100644 --- a/Sparkle/SUBasicUpdateDriver.m +++ b/Sparkle/SUBasicUpdateDriver.m @@ -20,6 +20,8 @@ #import "SUCodeSigningVerifier.h" #import "SUUpdater_Private.h" #import "SUFileManager.h" +#import "SUErrors.h" +#import "SULocalizations.h" @interface SUBasicUpdateDriver () diff --git a/Sparkle/SUBinaryDeltaApply.h b/Sparkle/SUBinaryDeltaApply.h index 8390d9c0..2f1f90ad 100644 --- a/Sparkle/SUBinaryDeltaApply.h +++ b/Sparkle/SUBinaryDeltaApply.h @@ -9,6 +9,8 @@ #ifndef SUBINARYDELTAAPPLY_H #define SUBINARYDELTAAPPLY_H +#import + @class NSString; BOOL applyBinaryDelta(NSString *source, NSString *destination, NSString *patchFile, BOOL verbose, NSError * __autoreleasing *error); diff --git a/Sparkle/SUBinaryDeltaUnarchiver.m b/Sparkle/SUBinaryDeltaUnarchiver.m index 7d1e1496..e9990e65 100644 --- a/Sparkle/SUBinaryDeltaUnarchiver.m +++ b/Sparkle/SUBinaryDeltaUnarchiver.m @@ -12,7 +12,6 @@ #import "SUUnarchiver_Private.h" #import "SUHost.h" #import "SULog.h" -#import "NTSynchronousTask.h" @implementation SUBinaryDeltaUnarchiver diff --git a/Sparkle/SUConstants.m b/Sparkle/SUConstants.m index dbaefe13..6e020ef8 100644 --- a/Sparkle/SUConstants.m +++ b/Sparkle/SUConstants.m @@ -12,6 +12,7 @@ #import "SUAppcastItem.h" #import "SUVersionComparisonProtocol.h" #import "SUConstants.h" +#import "SUErrors.h" #ifndef DEBUG #define DEBUG 0 diff --git a/Sparkle/SUFileManager.m b/Sparkle/SUFileManager.m index be5ef09b..404bd8cf 100644 --- a/Sparkle/SUFileManager.m +++ b/Sparkle/SUFileManager.m @@ -7,7 +7,8 @@ // #import "SUFileManager.h" -#import "SUOperatingSystem.h" +#import +#import "SUErrors.h" #include #include @@ -841,21 +842,7 @@ static BOOL SUMakeRefFromURL(NSURL *url, FSRef *ref, NSError **error) { return NO; } - NSURL *trashURL = nil; - BOOL canUseNewTrashAPI = YES; -#if __MAC_OS_X_VERSION_MIN_REQUIRED < 1080 - canUseNewTrashAPI = [SUOperatingSystem isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion){10, 8, 0}]; - if (!canUseNewTrashAPI) { - FSRef trashRef; - if (FSFindFolder(kUserDomain, kTrashFolderType, kDontCreateFolder, &trashRef) == noErr) { - trashURL = CFBridgingRelease(CFURLCreateFromFSRef(kCFAllocatorDefault, &trashRef)); - } - } -#endif - - if (canUseNewTrashAPI) { - trashURL = [_fileManager URLForDirectory:NSTrashDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil]; - } + NSURL *trashURL = [_fileManager URLForDirectory:NSTrashDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil]; if (trashURL == nil) { if (error != NULL) { @@ -889,24 +876,10 @@ static BOOL SUMakeRefFromURL(NSURL *url, FSRef *ref, NSError **error) { } // If we get here, we should be able to trash the item normally without authentication - - BOOL success = NO; -#if __MAC_OS_X_VERSION_MIN_REQUIRED < 1080 - if (!canUseNewTrashAPI) { - NSString *tempParentPath = tempItemURL.URLByDeletingLastPathComponent.path; - success = [[NSWorkspace sharedWorkspace] performFileOperation:NSWorkspaceRecycleOperation source:tempParentPath destination:@"" files:@[tempItemURL.lastPathComponent] tag:NULL]; - if (!success && error != NULL) { - *error = [NSError errorWithDomain:NSCocoaErrorDomain code:NSFileNoSuchFileError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Failed to move file %@ into the trash.", tempItemURL.lastPathComponent] }]; - } - } -#endif - - if (canUseNewTrashAPI) { - NSError *trashError = nil; - success = [_fileManager trashItemAtURL:tempItemURL resultingItemURL:NULL error:&trashError]; - if (!success && error != NULL) { - *error = trashError; - } + NSError *trashError = nil; + BOOL success = [_fileManager trashItemAtURL:tempItemURL resultingItemURL:NULL error:&trashError]; + if (!success && error != NULL) { + *error = trashError; } [self removeItemAtURL:tempDirectory error:NULL]; diff --git a/Sparkle/SUGuidedPackageInstaller.m b/Sparkle/SUGuidedPackageInstaller.m index c2d25de2..0e268473 100644 --- a/Sparkle/SUGuidedPackageInstaller.m +++ b/Sparkle/SUGuidedPackageInstaller.m @@ -8,7 +8,7 @@ #import #import - +#import "SUParameterAssert.h" #import "SUGuidedPackageInstaller.h" static BOOL AuthorizationExecuteWithPrivilegesAndWait(AuthorizationRef authorization, const char* executablePath, AuthorizationFlags options, char* const* arguments) diff --git a/Sparkle/SUHost.m b/Sparkle/SUHost.m index b0b0c00d..926b2f42 100644 --- a/Sparkle/SUHost.m +++ b/Sparkle/SUHost.m @@ -11,6 +11,7 @@ #import "SUSystemProfiler.h" #include // For statfs for isRunningOnReadOnlyVolume #import "SULog.h" +#import "SUParameterAssert.h" @interface SUHost () diff --git a/Sparkle/SUInstaller.m b/Sparkle/SUInstaller.m index 400f9597..98ba20ff 100644 --- a/Sparkle/SUInstaller.m +++ b/Sparkle/SUInstaller.m @@ -13,6 +13,7 @@ #import "SUHost.h" #import "SUConstants.h" #import "SULog.h" +#import "SUParameterAssert.h" @implementation SUInstaller diff --git a/Sparkle/SULocalizations.h b/Sparkle/SULocalizations.h new file mode 100644 index 00000000..7af9851e --- /dev/null +++ b/Sparkle/SULocalizations.h @@ -0,0 +1,16 @@ +// +// SULocalizations.h +// Sparkle +// +// Created by Mayur Pawashe on 2/28/16. +// Copyright © 2016 Sparkle Project. All rights reserved. +// + +#ifndef SULocalizations_h +#define SULocalizations_h + +#import "SUConstants.h" + +#define SULocalizedString(key, comment) NSLocalizedStringFromTableInBundle(key, @"Sparkle", (NSBundle *__nonnull)([NSBundle bundleWithIdentifier:SUBundleIdentifier] ? [NSBundle bundleWithIdentifier:SUBundleIdentifier] : [NSBundle mainBundle]), comment) + +#endif /* SULocalizations_h */ diff --git a/Sparkle/SUPackageInstaller.m b/Sparkle/SUPackageInstaller.m index 64637433..3b26667a 100644 --- a/Sparkle/SUPackageInstaller.m +++ b/Sparkle/SUPackageInstaller.m @@ -9,6 +9,7 @@ #import "SUPackageInstaller.h" #import #import "SUConstants.h" +#import "SUErrors.h" @implementation SUPackageInstaller diff --git a/Sparkle/SUParameterAssert.h b/Sparkle/SUParameterAssert.h new file mode 100644 index 00000000..8538858e --- /dev/null +++ b/Sparkle/SUParameterAssert.h @@ -0,0 +1,17 @@ +// +// SUParameterAssert.h +// Sparkle +// +// Created by Mayur Pawashe on 2/28/16. +// Copyright © 2016 Sparkle Project. All rights reserved. +// + +#ifndef SUParameterAssert_h +#define SUParameterAssert_h + +// Work around a warning generated by some versions of Xcode that we still support, +// when using NSParameterAssert +#define SUParameterAssert(condition) \ +NSAssert((condition), @"Invalid parameter not satisfying: %@", @(#condition)) + +#endif /* SUParameterAssert_h */ diff --git a/Sparkle/SUPlainInstaller.m b/Sparkle/SUPlainInstaller.m index 70a7ded9..59947ff4 100644 --- a/Sparkle/SUPlainInstaller.m +++ b/Sparkle/SUPlainInstaller.m @@ -12,6 +12,8 @@ #import "SUConstants.h" #import "SUHost.h" #import "SULog.h" +#import "SUErrors.h" +#import "SUParameterAssert.h" @implementation SUPlainInstaller diff --git a/Sparkle/SUSparkleUserUpdaterDriver.m b/Sparkle/SUSparkleUserUpdaterDriver.m index fca51a78..0698285e 100644 --- a/Sparkle/SUSparkleUserUpdaterDriver.m +++ b/Sparkle/SUSparkleUserUpdaterDriver.m @@ -15,6 +15,7 @@ #import "SUUpdateAlert.h" #import "SUAutomaticUpdateAlert.h" #import "SUOperatingSystem.h" +#import "SULocalizations.h" #if __MAC_OS_X_VERSION_MAX_ALLOWED < 1080 @interface NSByteCountFormatter : NSFormatter { diff --git a/Sparkle/SUStatusController.m b/Sparkle/SUStatusController.m index 290bfeae..c0064316 100644 --- a/Sparkle/SUStatusController.m +++ b/Sparkle/SUStatusController.m @@ -13,6 +13,7 @@ #import "SUVersionComparisonProtocol.h" #import "SUStatusController.h" #import "SUHost.h" +#import "SULocalizations.h" @interface SUStatusController () @property (copy) NSString *title, *buttonTitle; diff --git a/Sparkle/SUUpdateAlert.m b/Sparkle/SUUpdateAlert.m index 7cc87c84..324474e8 100644 --- a/Sparkle/SUUpdateAlert.m +++ b/Sparkle/SUUpdateAlert.m @@ -17,6 +17,7 @@ #import "SUConstants.h" #import "SULog.h" +#import "SULocalizations.h" // WebKit protocols are not explicitly declared until 10.11 SDK, so // declare dummy protocols to keep the build working on earlier SDKs. diff --git a/Sparkle/SUUpdatePermissionPrompt.m b/Sparkle/SUUpdatePermissionPrompt.m index 9ebd6697..7d118417 100644 --- a/Sparkle/SUUpdatePermissionPrompt.m +++ b/Sparkle/SUUpdatePermissionPrompt.m @@ -8,6 +8,7 @@ #import "SUUpdatePermissionPrompt.h" #import "SUUpdatePermissionPromptResult.h" +#import "SULocalizations.h" #import "SUHost.h" #import "SUConstants.h" diff --git a/Sparkle/SUUpdater.h b/Sparkle/SUUpdater.h index 6f79e53e..eb16fbc1 100644 --- a/Sparkle/SUUpdater.h +++ b/Sparkle/SUUpdater.h @@ -29,11 +29,9 @@ SU_EXPORT @interface SUUpdater : NSObject @property (unsafe_unretained) IBOutlet id delegate; -@property (nonatomic) id userUpdaterDriver; +@property (nonatomic, readonly) id userUpdaterDriver; -+ (SUUpdater *)sharedUpdater; -+ (SUUpdater *)updaterForBundle:(NSBundle *)bundle; -- (instancetype)initForBundle:(NSBundle *)bundle; +- (instancetype)initWithHostBundle:(NSBundle *)bundle userUpdaterDriver:(id )userUpdaterDriver; @property (readonly, strong) NSBundle *hostBundle; @property (strong, readonly) NSBundle *sparkleBundle; diff --git a/Sparkle/SUUpdater.m b/Sparkle/SUUpdater.m index deb63d8a..b98bf7bd 100644 --- a/Sparkle/SUUpdater.m +++ b/Sparkle/SUUpdater.m @@ -33,12 +33,9 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification @interface SUUpdater () @property (strong) NSBundle *sparkleBundle; -- (instancetype)initForBundle:(NSBundle *)bundle; - (void)startUpdateCycle; - (void)checkForUpdatesWithDriver:(SUUpdateDriver *)updateDriver; - (void)scheduleNextUpdateCheck; -- (void)registerAsObserver; -- (void)unregisterAsObserver; - (void)updateDriverDidFinish:(NSNotification *)note; @property (readonly, copy) NSURL *parameterizedFeedURL; @@ -58,30 +55,9 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification @synthesize sparkleBundle; @synthesize decryptionPassword; -static NSMutableDictionary *sharedUpdaters = nil; -static NSString *const SUUpdaterDefaultsObservationContext = @"SUUpdaterDefaultsObservationContext"; - -+ (SUUpdater *)sharedUpdater -{ - return [self updaterForBundle:[NSBundle mainBundle]]; -} - -// SUUpdater has a singleton for each bundle. We use the fact that NSBundle instances are also singletons, so we can use them as keys. If you don't trust that you can also use the identifier as key -+ (SUUpdater *)updaterForBundle:(NSBundle *)bundle -{ - if (bundle == nil) bundle = [NSBundle mainBundle]; - id updater = sharedUpdaters[[NSValue valueWithNonretainedObject:bundle]]; - if (updater == nil) { - updater = [[[self class] alloc] initForBundle:bundle]; - } - return updater; -} - -// This is the designated initializer for SUUpdater, important for subclasses -- (instancetype)initForBundle:(NSBundle *)bundle +- (instancetype)initWithHostBundle:(NSBundle *)bundle userUpdaterDriver:(id )userUpdaterDriver { self = [super init]; - if (bundle == nil) bundle = [NSBundle mainBundle]; // Use explicit class to use the correct bundle even when subclassed self.sparkleBundle = [NSBundle bundleForClass:[SUUpdater class]]; @@ -90,29 +66,13 @@ static NSString *const SUUpdaterDefaultsObservationContext = @"SUUpdaterDefaults return nil; } - // Register as observer straight away to avoid exceptions on -dealloc when -unregisterAsObserver is called: - if (self) { - [self registerAsObserver]; - } - - id updater = sharedUpdaters[[NSValue valueWithNonretainedObject:bundle]]; - if (updater) - { - self = updater; - } - else if (self) - { - if (sharedUpdaters == nil) { - sharedUpdaters = [[NSMutableDictionary alloc] init]; - } - sharedUpdaters[[NSValue valueWithNonretainedObject:bundle]] = self; - host = [[SUHost alloc] initWithBundle:bundle]; - - _userUpdaterDriver = [[SUSparkleUserUpdaterDriver alloc] initWithHost:host handlesTermination:YES delegate:nil]; - - // This runs the permission prompt if needed, but never before the app has finished launching because the runloop won't run before that - [self performSelector:@selector(startUpdateCycle) withObject:nil afterDelay:0]; - } + host = [[SUHost alloc] initWithBundle:bundle]; + + _userUpdaterDriver = userUpdaterDriver; + + // This runs the permission prompt if needed, but never before the app has finished launching because the runloop won't run before that + [self performSelector:@selector(startUpdateCycle) withObject:nil afterDelay:0]; + return self; } @@ -141,13 +101,6 @@ static NSString *const SUUpdaterDefaultsObservationContext = @"SUUpdaterDefaults #endif } - -// This will be used when the updater is instantiated in a nib such as MainMenu -- (instancetype)init -{ - return [self initForBundle:[NSBundle mainBundle]]; -} - - (NSString *)description { return [NSString stringWithFormat:@"%@ <%@, %@>", [self class], [self.host bundlePath], [self.host installationPath]]; } - (void)startUpdateCycle @@ -257,13 +210,9 @@ static NSString *const SUUpdaterDefaultsObservationContext = @"SUUpdaterDefaults } } - -- (void)putFeedURLIntoDictionary:(NSMutableDictionary *)theDict // You release this. -{ - theDict[@"feedURL"] = [self feedURL]; -} - -- (void)checkForUpdatesInBgReachabilityCheckWithDriver:(SUUpdateDriver *)inDriver /* RUNS ON ITS OWN THREAD */ +// RUNS ON ITS OWN THREAD +// updater should be passed as a weak reference +static void SUCheckForUpdatesInBgReachabilityCheck(SUUpdater *updater, SUUpdateDriver *inDriver) { @try { // This method *must* be called on its own thread. SCNetworkReachabilityCheckByName @@ -271,17 +220,23 @@ static NSString *const SUUpdaterDefaultsObservationContext = @"SUUpdaterDefaults // wouldn't want to beachball the main thread for a background operation. // We could use asynchronous reachability callbacks, but those aren't // reliable enough and can 'get lost' sometimes, which we don't want. - - @autoreleasepool { + + @autoreleasepool { SCNetworkConnectionFlags flags = 0; BOOL isNetworkReachable = YES; - + // Don't perform automatic checks on unconnected laptops or dial-up connections that aren't online: NSMutableDictionary *theDict = [NSMutableDictionary dictionary]; dispatch_sync(dispatch_get_main_queue(), ^{ - [self putFeedURLIntoDictionary:theDict]; // Get feed URL on main thread, it's not safe to call elsewhere. + // Get feed URL on main thread, it's not safe to call elsewhere. + theDict[@"feedURL"] = [updater feedURL];; }); - + + // Did the updater get deallocated already? + if (theDict.count == 0 || updater == nil) { + return; + } + const char *hostname = [[(NSURL *)theDict[@"feedURL"] host] cStringUsingEncoding:NSUTF8StringEncoding]; SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL, hostname); Boolean reachabilityResult = NO; @@ -290,30 +245,31 @@ static NSString *const SUUpdaterDefaultsObservationContext = @"SUUpdaterDefaults SCNetworkReachabilityGetFlags(reachability, &flags); CFRelease(reachability); } - - if( reachabilityResult ) - { + + if( reachabilityResult ) + { BOOL reachable = (flags & kSCNetworkFlagsReachable) == kSCNetworkFlagsReachable; BOOL automatic = (flags & kSCNetworkFlagsConnectionAutomatic) == kSCNetworkFlagsConnectionAutomatic; BOOL local = (flags & kSCNetworkFlagsIsLocalAddress) == kSCNetworkFlagsIsLocalAddress; - + if (!(reachable || automatic || local)) isNetworkReachable = NO; } - + // If the network's not reachable, we pass a nil driver into checkForUpdatesWithDriver, which will then reschedule the next update so we try again later. dispatch_async(dispatch_get_main_queue(), ^{ - [self checkForUpdatesWithDriver: isNetworkReachable ? inDriver : nil]; + // Is the updater still alive? + if (updater != nil) { + [updater checkForUpdatesWithDriver: isNetworkReachable ? inDriver : nil]; + } }); - } - } @catch (NSException *localException) { + } @catch (NSException *localException) { SULog(@"UNCAUGHT EXCEPTION IN UPDATE CHECK TIMER: %@", [localException reason]); // Don't propagate the exception beyond here. In Carbon apps that would trash the stack. } } - - (void)checkForUpdatesInBackground { // Background update checks should only happen if we have a network connection. @@ -321,8 +277,10 @@ static NSString *const SUUpdaterDefaultsObservationContext = @"SUUpdaterDefaults // hour or so: SUUpdateDriver *theUpdateDriver = [[([self automaticallyDownloadsUpdates] ? [SUAutomaticUpdateDriver class] : [SUScheduledUpdateDriver class])alloc] initWithUpdater:self]; + // We don't want the rechability check to act on the driver if the updater is going near death + __weak SUUpdater *updater = self; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ - [self checkForUpdatesInBgReachabilityCheckWithDriver:theUpdateDriver]; + SUCheckForUpdatesInBgReachabilityCheck(updater, theUpdateDriver); }); } @@ -389,42 +347,6 @@ static NSString *const SUUpdaterDefaultsObservationContext = @"SUUpdaterDefaults [self.driver abortUpdate]; } -- (void)registerAsObserver -{ - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateDriverDidFinish:) name:SUUpdateDriverFinishedNotification object:nil]; - [[NSUserDefaultsController sharedUserDefaultsController] addObserver:self forKeyPath:[@"values." stringByAppendingString:SUScheduledCheckIntervalKey] options:(NSKeyValueObservingOptions)0 context:(__bridge void *)(SUUpdaterDefaultsObservationContext)]; - [[NSUserDefaultsController sharedUserDefaultsController] addObserver:self forKeyPath:[@"values." stringByAppendingString:SUEnableAutomaticChecksKey] options:(NSKeyValueObservingOptions)0 context:(__bridge void *)(SUUpdaterDefaultsObservationContext)]; -} - -- (void)unregisterAsObserver -{ - @try - { - [[NSNotificationCenter defaultCenter] removeObserver:self]; - [[NSUserDefaultsController sharedUserDefaultsController] removeObserver:self forKeyPath:[@"values." stringByAppendingString:SUScheduledCheckIntervalKey]]; - [[NSUserDefaultsController sharedUserDefaultsController] removeObserver:self forKeyPath:[@"values." stringByAppendingString:SUEnableAutomaticChecksKey]]; - } - @catch (NSException *) - { - SULog(@"Error: [SUUpdater unregisterAsObserver] called, but the updater wasn't registered as an observer."); - } -} - -- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context -{ - if (context == (__bridge void *)(SUUpdaterDefaultsObservationContext)) - { - // Allow a small delay, because perhaps the user or developer wants to change both preferences. This allows the developer to interpret a zero check interval as a sign to disable automatic checking. - // Or we may get this from the developer and from our own KVO observation, this will effectively coalesce them. - [[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(resetUpdateCycle) object:nil]; - [self performSelector:@selector(resetUpdateCycle) withObject:nil afterDelay:1]; - } - else - { - [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; - } -} - - (void)resetUpdateCycle { [[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(resetUpdateCycle) object:nil]; @@ -584,18 +506,9 @@ static NSString *const SUUpdaterDefaultsObservationContext = @"SUUpdaterDefaults - (void)dealloc { - [self unregisterAsObserver]; [self.userUpdaterDriver invalidateUpdateCheckTimer]; } -- (BOOL)validateMenuItem:(NSMenuItem *)item -{ - if ([item action] == @selector(checkForUpdates:)) { - return ![self updateInProgress]; - } - return YES; -} - - (BOOL)updateInProgress { return self.driver && ([self.driver finished] == NO); diff --git a/Sparkle/SUUserUpdaterDriver.h b/Sparkle/SUUserUpdaterDriver.h index 7b13fbe6..c59948dd 100644 --- a/Sparkle/SUUserUpdaterDriver.h +++ b/Sparkle/SUUserUpdaterDriver.h @@ -6,7 +6,7 @@ // Copyright © 2016 Sparkle Project. All rights reserved. // -#import +#import #import "SUUpdateAlertChoice.h" #import "SUAutomaticInstallationChoice.h" diff --git a/Sparkle/Sparkle.pch b/Sparkle/Sparkle.pch index 7a53b80a..871b0da5 100644 --- a/Sparkle/Sparkle.pch +++ b/Sparkle/Sparkle.pch @@ -13,15 +13,4 @@ #define __nullable #endif -#import -#import "SUConstants.h" -#import "SUErrors.h" - -#define SULocalizedString(key, comment) NSLocalizedStringFromTableInBundle(key, @"Sparkle", (NSBundle *__nonnull)([NSBundle bundleWithIdentifier:SUBundleIdentifier] ? [NSBundle bundleWithIdentifier:SUBundleIdentifier] : [NSBundle mainBundle]), comment) - -// Work around a warning generated by some versions of Xcode that we still support, -// when using NSParameterAssert -#define SUParameterAssert(condition) \ - NSAssert((condition), @"Invalid parameter not satisfying: %@", @(#condition)) - #endif diff --git a/Vendor/CocoatechCore/NTSynchronousTask.h b/Vendor/CocoatechCore/NTSynchronousTask.h index cbcbf2c8..db4e453f 100644 --- a/Vendor/CocoatechCore/NTSynchronousTask.h +++ b/Vendor/CocoatechCore/NTSynchronousTask.h @@ -9,6 +9,8 @@ #ifndef NTSYNCHRONOUSTASK_H #define NTSYNCHRONOUSTASK_H +#import + @interface NTSynchronousTask : NSObject @property (readonly, strong) NSData *output; @property (readonly) int result;