Files
Sparkle/TestApplication/SURemoteUpdateSettingsWindowController.m
T
Zorg a1d08a354d Add updater resumability, merge automatic & regular update prompts
Resumability works by asking the installer for its progress and for the appcast item we've given it before.

If an automatic silent download occurs, we abort it after the installer tool finishes with its basic steps.
Then, a user initiated or scheduled check can resume the update. The scheduled check fires during a longer-than-normal
span since usually the user can install by quitting the app, unless the app has been alive for a very long time.
One exception to this is when the update is marked critical; in that case, the scheduled check is fired immediately after
an automatic update download. Subsequent attempts will be fired using a normal update check interval.

Todo: delta updates probably don't work when they fail and fallback to a regular update
2016-03-21 00:46:34 -04:00

205 lines
7.8 KiB
Objective-C

//
// SURemoteUpdateSettingsWindowController.m
// Sparkle
//
// Created by Mayur Pawashe on 3/2/16.
// Copyright © 2016 Sparkle Project. All rights reserved.
//
#import "SURemoteUpdateSettingsWindowController.h"
#import "TestAppHelperProtocol.h"
#import "SUPopUpTitlebarUserDriver.h"
@interface SURemoteUpdateSettingsWindowController ()
@property (nonatomic) NSXPCConnection *connection;
@property (nonatomic) id<SUStandardUserDriver> userDriver;
@property (nonatomic) IBOutlet NSButton *automaticallyChecksForUpdatesButton;
@property (nonatomic) IBOutlet NSButton *automaticallyDownloadUpdatesButton;
@property (nonatomic) IBOutlet NSButton *sendsSystemProfileButton;
@property (nonatomic) IBOutlet NSTextField *updateCheckIntervalTextField;
@end
@implementation SURemoteUpdateSettingsWindowController
@synthesize connection = _connection;
@synthesize userDriver = _userDriver;
@synthesize automaticallyChecksForUpdatesButton = _automaticallyChecksForUpdatesButton;
@synthesize automaticallyDownloadUpdatesButton = _automaticallyDownloadUpdatesButton;
@synthesize sendsSystemProfileButton = _sendsSystemProfileButton;
@synthesize updateCheckIntervalTextField = _updateCheckIntervalTextField;
#pragma mark Birth
- (instancetype)init
{
self = [super init];
if (self != nil) {
[self setUpConnection];
}
return self;
}
- (void)dealloc
{
[self.connection invalidate];
self.connection = nil;
}
- (NSString *)windowNibName
{
return NSStringFromClass([self class]);
}
- (void)setUpConnection
{
self.connection = [[NSXPCConnection alloc] initWithServiceName:@"org.sparkle-project.TestAppHelper"];
self.connection.exportedInterface = [NSXPCInterface interfaceWithProtocol:@protocol(SUUserDriver)];
//self.userDriver = [[SUPopUpTitlebarUserDriver alloc] initWithWindow:self.window delegate:self];
self.userDriver = [[SUStandardUserDriver alloc] initWithHostBundle:[NSBundle mainBundle] delegate:self];
self.connection.exportedObject = self.userDriver;
self.connection.remoteObjectInterface = [NSXPCInterface interfaceWithProtocol:@protocol(TestAppHelperProtocol)];
__weak SURemoteUpdateSettingsWindowController *weakSelf = self;
// Try killing the TestAppHelper process to test this (might not want a debugger attached)
self.connection.interruptionHandler = ^{
NSLog(@"Connection is interrupted..!");
dispatch_async(dispatch_get_main_queue(), ^{
// Attempt to do work only if we haven't terminated yet
dispatch_async(dispatch_get_main_queue(), ^{
if (!weakSelf.userDriver.willInitiateNextUpdateCheck) {
NSLog(@"Invalidating installation..");
[weakSelf.userDriver invalidate];
}
// No need to do anything if we will be sending a message later anyway
// or if we won't have to send a message immediately anyway
if (!weakSelf.userDriver.willInitiateNextUpdateCheck && !weakSelf.userDriver.idlesOnUpdateChecks) {
NSLog(@"Starting up Sparkle again");
[weakSelf.connection.remoteObjectProxy startSparkle];
} else {
NSLog(@"Update checker is in progress or doesn't have to be; no need to panic");
}
});
});
};
// Try testing this by using the Invalidate Connection menu item
self.connection.invalidationHandler = ^{
const uint64_t delay = 60;
NSLog(@"Connection is invalidated! Rebooting connection in %llu seconds..", delay);
dispatch_async(dispatch_get_main_queue(), ^{
// Attempt to do work only if we haven't terminated yet
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf.userDriver invalidate];
weakSelf.userDriver = nil;
weakSelf.connection = nil;
// Because the connection was invalidated, it might mean creating a new one will not be easy, so let's wait a bit in our example
// (Unless the user forces the connection to be created again in our test app, by eg, playing around with the checkboxes)
// Note this is probably a "dumb" implementation. Perhaps a better one increases the time exponentially on each re-try until a certain limit is reached
// And that could be cleared out after a certain amount of idle time. Plenty of ways to approach this..
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"Rebooting connection..");
if (weakSelf != nil && weakSelf.connection == nil) {
[weakSelf setUpConnection];
}
});
});
});
};
[self.connection resume];
[self.connection.remoteObjectProxy startSparkle];
// Retrieving the settings really takes a while, perhaps a smarter app may also store defaults locally and sync them
[self.connection.remoteObjectProxy retrieveUpdateSettings:^(BOOL automaticallyCheckForUpdates, BOOL automaticallyDownloadUpdates, BOOL sendSystemProfile, NSTimeInterval updateCheckInterval) {
dispatch_async(dispatch_get_main_queue(), ^{
SURemoteUpdateSettingsWindowController *strongSelf = weakSelf;
if (strongSelf != nil) {
// Load the window before referencing our outlets
[strongSelf window];
strongSelf.automaticallyChecksForUpdatesButton.state = automaticallyCheckForUpdates ? NSOnState : NSOffState;
strongSelf.automaticallyDownloadUpdatesButton.state = automaticallyDownloadUpdates ? NSOnState : NSOffState;
strongSelf.sendsSystemProfileButton.state = sendSystemProfile ? NSOnState : NSOffState;
strongSelf.updateCheckIntervalTextField.doubleValue = updateCheckInterval;
// Ready to show our window
if (!strongSelf.window.isVisible) {
[strongSelf showWindow:nil];
}
}
});
}];
}
#pragma mark UI Actions
// When the user explicitly checks for updates via the menu item..
- (IBAction)checkForUpdates:(id)__unused sender
{
[self.connection.remoteObjectProxy initiateUserCheckForUpdates];
}
// See if the user is allowed to invoke the check for update's menu item's action
- (BOOL)validateMenuItem:(NSMenuItem *)menuItem
{
if (menuItem.action == @selector(checkForUpdates:)) {
return (self.userDriver != nil && !self.userDriver.updateInProgress);
} else if (menuItem.action == @selector(invalidateConnection:)) {
return (self.connection != nil);
}
return YES;
}
- (IBAction)invalidateConnection:(id)__unused sender
{
[self.connection invalidate];
}
#pragma mark Changing Settings
- (IBAction)changeAutomaticallyChecksForUpdates:(__unused id)sender
{
[self.connection.remoteObjectProxy setAutomaticallyChecksForUpdates:self.automaticallyChecksForUpdatesButton.state == NSOnState];
}
- (IBAction)changeAutomaticallyDownloadsUpdates:(__unused id)sender
{
[self.connection.remoteObjectProxy setAutomaticallyDownloadsUpdates:self.automaticallyDownloadUpdatesButton.state == NSOnState];
}
- (IBAction)changeSendsSystemProfile:(__unused id)sender
{
[self.connection.remoteObjectProxy setSendsSystemProfile:self.sendsSystemProfileButton.state == NSOnState];
}
- (IBAction)changeUpdateCheckInterval:(__unused id)sender
{
[self.connection.remoteObjectProxy setUpdateCheckInterval:self.updateCheckIntervalTextField.doubleValue];
}
#pragma mark Update Checks
- (BOOL)responsibleForInitiatingUpdateCheck
{
return YES;
}
- (void)initiateUpdateCheck
{
[self.connection.remoteObjectProxy checkForUpdates];
}
@end