From 8792137fdac25bfe06fe52bcdee17f0db818c5f5 Mon Sep 17 00:00:00 2001 From: Zorg Date: Sun, 14 Feb 2021 00:40:08 -0800 Subject: [PATCH] Use wall time for our updater scheduled check timer --- Sparkle/SPUUpdaterTimer.m | 32 ++++++++++++++++++++------------ Sparkle/SUConstants.h | 1 + Sparkle/SUConstants.m | 2 ++ 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/Sparkle/SPUUpdaterTimer.m b/Sparkle/SPUUpdaterTimer.m index 4a6ac1c4..6baa9200 100644 --- a/Sparkle/SPUUpdaterTimer.m +++ b/Sparkle/SPUUpdaterTimer.m @@ -7,6 +7,7 @@ // #import "SPUUpdaterTimer.h" +#import "SUConstants.h" #include "AppKitPrevention.h" @@ -14,14 +15,14 @@ @interface SPUUpdaterTimer () @property (nonatomic, readonly, weak) id delegate; -@property (nonatomic, nullable) NSTimer *timer; +@property (nonatomic) dispatch_source_t source; @end @implementation SPUUpdaterTimer @synthesize delegate = _delegate; -@synthesize timer = _timer; +@synthesize source = _source; - (instancetype)initWithDelegate:(id)delegate { @@ -34,20 +35,27 @@ - (void)startAndFireAfterDelay:(NSTimeInterval)delay { - assert(self.timer == nil); - self.timer = [NSTimer scheduledTimerWithTimeInterval:delay target:self selector:@selector(fire:) userInfo:nil repeats:NO]; -} - -- (void)fire:(NSTimer *)__unused timer -{ - [self.delegate updaterTimerDidFire]; - self.timer = nil; + self.source = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue()); + + // We use the wall time instead of cpu time for our dispatch timer + // So eg if the computer sleeps we want to include that time spent in our timer + dispatch_time_t timeToFire = dispatch_walltime(NULL, (int64_t)(delay * NSEC_PER_SEC)); + dispatch_source_set_timer(self.source, timeToFire, DISPATCH_TIME_FOREVER, SULeewayUpdateCheckInterval * NSEC_PER_SEC); + + __weak __typeof__(self) weakSelf = self; + dispatch_source_set_event_handler(self.source, ^{ + [weakSelf.delegate updaterTimerDidFire]; + }); + + dispatch_resume(self.source); } - (void)invalidate { - [self.timer invalidate]; - self.timer = nil; + if (self.source != nil) { + dispatch_source_cancel(self.source); + self.source = nil; + } } @end diff --git a/Sparkle/SUConstants.h b/Sparkle/SUConstants.h index 7f5b8246..44d3714e 100644 --- a/Sparkle/SUConstants.h +++ b/Sparkle/SUConstants.h @@ -19,6 +19,7 @@ extern const NSTimeInterval SUDefaultUpdatePermissionPromptInterval; extern const NSTimeInterval SUMinimumUpdateCheckInterval; extern const NSTimeInterval SUDefaultUpdateCheckInterval; +extern const uint64_t SULeewayUpdateCheckInterval; extern const NSTimeInterval SUImpatientUpdateCheckInterval; extern NSString *const SUBundleIdentifier; diff --git a/Sparkle/SUConstants.m b/Sparkle/SUConstants.m index 990244f1..e5289cb5 100644 --- a/Sparkle/SUConstants.m +++ b/Sparkle/SUConstants.m @@ -18,6 +18,8 @@ // Define some minimum intervals to avoid DoS-like checking attacks const NSTimeInterval SUMinimumUpdateCheckInterval = DEBUG ? 60 : (60 * 60); const NSTimeInterval SUDefaultUpdateCheckInterval = DEBUG ? 60 : (60 * 60 * 24); +// The amount of time the system can defer our update check (for improved performance) +const uint64_t SULeewayUpdateCheckInterval = DEBUG ? 1 : 15; // If the update has already been automatically downloaded, we normally don't want to bug the user about the update // However if the user has gone a very long time without quitting an application, we will bug them