From adba3bd9453578f2d4d864be536b8cabb877c339 Mon Sep 17 00:00:00 2001 From: Zorg Date: Sat, 11 Jun 2016 23:31:24 -0400 Subject: [PATCH] Don't allow SUUpdater to be kept alive by cycle resets --- Sparkle.xcodeproj/project.pbxproj | 8 +++++ Sparkle/SUUpdater.m | 11 ++++--- Sparkle/SUUpdaterCycle.h | 32 ++++++++++++++++++++ Sparkle/SUUpdaterCycle.m | 49 +++++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+), 4 deletions(-) create mode 100644 Sparkle/SUUpdaterCycle.h create mode 100644 Sparkle/SUUpdaterCycle.m diff --git a/Sparkle.xcodeproj/project.pbxproj b/Sparkle.xcodeproj/project.pbxproj index 89f3b2e1..3b3aba40 100644 --- a/Sparkle.xcodeproj/project.pbxproj +++ b/Sparkle.xcodeproj/project.pbxproj @@ -122,6 +122,8 @@ 61FA52880E2D9EA400EF58AD /* Sparkle.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8DC2EF5B0486A6940098B216 /* Sparkle.framework */; settings = {ATTRIBUTES = (Required, ); }; }; 720B16451C66433D006985FB /* SUTestApplicationTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 720B16431C66433D006985FB /* SUTestApplicationTest.swift */; }; 720E21781D0CED2B003A311C /* SUFileOperationConstants.m in Sources */ = {isa = PBXBuildFile; fileRef = 72E641A61D090F51000404CA /* SUFileOperationConstants.m */; }; + 720E217B1D0D00BF003A311C /* SUUpdaterCycle.h in Headers */ = {isa = PBXBuildFile; fileRef = 720E21791D0D00BF003A311C /* SUUpdaterCycle.h */; }; + 720E217C1D0D00BF003A311C /* SUUpdaterCycle.m in Sources */ = {isa = PBXBuildFile; fileRef = 720E217A1D0D00BF003A311C /* SUUpdaterCycle.m */; }; 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 */; }; @@ -821,6 +823,8 @@ 61F614540E24A12D009F47E7 /* it */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.strings; name = it; path = it.lproj/Sparkle.strings; sourceTree = ""; }; 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; }; + 720E21791D0D00BF003A311C /* SUUpdaterCycle.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SUUpdaterCycle.h; sourceTree = ""; }; + 720E217A1D0D00BF003A311C /* SUUpdaterCycle.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SUUpdaterCycle.m; sourceTree = ""; }; 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 = ""; }; @@ -1547,6 +1551,8 @@ 72A6F9891C94E2D6005F404C /* SUUpdaterDelegate.h */, 72A450511C69A68900D67EEA /* SUUpdatePermission.h */, 72A450521C69A68900D67EEA /* SUUpdatePermission.m */, + 720E21791D0D00BF003A311C /* SUUpdaterCycle.h */, + 720E217A1D0D00BF003A311C /* SUUpdaterCycle.m */, ); name = "Update Control"; sourceTree = ""; @@ -1783,6 +1789,7 @@ 725F977B1C8A896F00265BE4 /* SUUserDriverCoreComponent.h in Headers */, 726E078D1CA891E9001A286B /* SUUpdaterSettings.h in Headers */, 72B144501CF8826900B971EE /* SUAuthorizationEnvironment.h in Headers */, + 720E217B1D0D00BF003A311C /* SUUpdaterCycle.h in Headers */, 72A6F98A1C94E2D6005F404C /* SUUpdaterDelegate.h in Headers */, 726E4A371C89116000C57C6A /* SUStandardUserDriverDelegate.h in Headers */, 726E4A0E1C86881600C57C6A /* SUStandardUserDriverUIDelegate.h in Headers */, @@ -2743,6 +2750,7 @@ 725602D61C83551C00DAA70E /* SUApplicationInfo.m in Sources */, 72B767DF1C9CDB9700A07552 /* SUUserInitiatedUpdateDriver.m in Sources */, 72B767CF1C9B924900A07552 /* SUInstallerDriver.m in Sources */, + 720E217C1D0D00BF003A311C /* SUUpdaterCycle.m in Sources */, 72A450541C69A68900D67EEA /* SUUpdatePermission.m in Sources */, 7229E1BA1C97CC4D00CB50D0 /* SUScheduledUpdateDriver.m in Sources */, 729746411C9119F300FC134E /* SULocalMessagePort.m in Sources */, diff --git a/Sparkle/SUUpdater.m b/Sparkle/SUUpdater.m index d5da2a46..bd2e9e07 100644 --- a/Sparkle/SUUpdater.m +++ b/Sparkle/SUUpdater.m @@ -26,6 +26,7 @@ #import "SUInstallationInfo.h" #import "SUErrors.h" #import "SUXPCServiceInfo.h" +#import "SUUpdaterCycle.h" #ifdef _APPKITDEFINES_H #error This is a "core" class and should NOT import AppKit @@ -38,13 +39,14 @@ NSString *const SUUpdaterWillRestartNotification = @"SUUpdaterWillRestartNotific NSString *const SUUpdaterAppcastItemNotificationKey = @"SUUpdaterAppcastItemNotificationKey"; NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotificationKey"; -@interface SUUpdater () +@interface SUUpdater () @property (readonly, copy) NSURL *parameterizedFeedURL; @property (nonatomic) id driver; @property (nonatomic, readonly) SUHost *host; @property (nonatomic, readonly) SUUpdaterSettings *updaterSettings; +@property (nonatomic, readonly) SUUpdaterCycle *updaterCycle; @property (nonatomic) BOOL startedUpdater; @property (nonatomic, copy) void (^preStartedScheduledUpdateBlock)(void); @@ -59,6 +61,7 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification @synthesize driver; @synthesize host = _host; @synthesize updaterSettings = _updaterSettings; +@synthesize updaterCycle = _updaterCycle; @synthesize sparkleBundle = _sparkleBundle; @synthesize startedUpdater = _startedUpdater; @synthesize preStartedScheduledUpdateBlock = _preStartedScheduledUpdateBlock; @@ -83,6 +86,7 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification _host = [[SUHost alloc] initWithBundle:bundle]; _updaterSettings = [[SUUpdaterSettings alloc] initWithHostBundle:bundle]; + _updaterCycle = [[SUUpdaterCycle alloc] initWithDelegate:self]; _userDriver = userDriver; @@ -563,7 +567,7 @@ static void SUCheckForUpdatesInBgReachabilityCheck(__weak SUUpdater *updater, id - (void)cancelNextUpdateCycle { - [[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(resetUpdateCycle) object:nil]; + [self.updaterCycle cancelNextUpdateCycle]; } - (void)resetUpdateCycle @@ -582,7 +586,7 @@ static void SUCheckForUpdatesInBgReachabilityCheck(__weak SUUpdater *updater, id - (void)resetUpdateCycleAfterShortDelay { [self cancelNextUpdateCycle]; - [self performSelector:@selector(resetUpdateCycle) withObject:nil afterDelay:1]; + [self.updaterCycle resetUpdateCycleAfterDelay]; } - (void)setAutomaticallyChecksForUpdates:(BOOL)automaticallyCheckForUpdates @@ -777,7 +781,6 @@ static void SUCheckForUpdatesInBgReachabilityCheck(__weak SUUpdater *updater, id - (void)dealloc { // Stop checking for updates -#warning useless right now because the cycles retain self meaning dealloc won't be hit unless it's been cancelled/finished [self cancelNextUpdateCycle]; // Don't tell the user driver to invalidate the update check timer diff --git a/Sparkle/SUUpdaterCycle.h b/Sparkle/SUUpdaterCycle.h new file mode 100644 index 00000000..b26356cb --- /dev/null +++ b/Sparkle/SUUpdaterCycle.h @@ -0,0 +1,32 @@ +// +// SUUpdaterCycle.h +// Sparkle +// +// Created by Mayur Pawashe on 6/11/16. +// Copyright © 2016 Sparkle Project. All rights reserved. +// + +#import + +NS_ASSUME_NONNULL_BEGIN + +@protocol SUUpdaterCycleDelegate + +- (void)resetUpdateCycle; + +@end + +// This notifies the updater for (re-)starting and canceling update cycles +// This class is used so that an updater instance isn't kept alive by a pending update cycle +@interface SUUpdaterCycle : NSObject + +// This delegate is weakly referenced +- (instancetype)initWithDelegate:(id)delegate; + +- (void)resetUpdateCycleAfterDelay; + +- (void)cancelNextUpdateCycle; + +@end + +NS_ASSUME_NONNULL_END diff --git a/Sparkle/SUUpdaterCycle.m b/Sparkle/SUUpdaterCycle.m new file mode 100644 index 00000000..71ad390c --- /dev/null +++ b/Sparkle/SUUpdaterCycle.m @@ -0,0 +1,49 @@ +// +// SUUpdaterCycle.m +// Sparkle +// +// Created by Mayur Pawashe on 6/11/16. +// Copyright © 2016 Sparkle Project. All rights reserved. +// + +#import "SUUpdaterCycle.h" + +#ifdef _APPKITDEFINES_H +#error This is a "core" class and should NOT import AppKit +#endif + +@interface SUUpdaterCycle () + +@property (nonatomic, weak, readonly) id delegate; + +@end + +@implementation SUUpdaterCycle + +@synthesize delegate = _delegate; + +- (instancetype)initWithDelegate:(id)delegate +{ + self = [super init]; + if (self != nil) { + _delegate = delegate; + } + return self; +} + +- (void)resetUpdateCycle +{ + [self.delegate resetUpdateCycle]; +} + +- (void)resetUpdateCycleAfterDelay +{ + [self performSelector:@selector(resetUpdateCycle) withObject:nil afterDelay:1]; +} + +- (void)cancelNextUpdateCycle +{ + [[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(resetUpdateCycle) object:nil]; +} + +@end