Use wall time for our updater scheduled check timer

This commit is contained in:
Zorg committed 2021-02-14 00:40:08 -08:00
1 parent 25820ccd4b
commit 8792137fda
3 files changed
+23 -12

No files matched your search

+20 -12
View File
@@ -7,6 +7,7 @@
//
#import "SPUUpdaterTimer.h"
#import "SUConstants.h"
#include "AppKitPrevention.h"
@@ -14,14 +15,14 @@
@interface SPUUpdaterTimer ()
@property (nonatomic, readonly, weak) id<SPUUpdaterTimerDelegate> 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<SPUUpdaterTimerDelegate>)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
+1
View File
@@ -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;
+2
View File
@@ -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