mirror of
https://github.com/sparkle-project/Sparkle
synced 2026-11-24 04:38:16 +00:00
For sparkle-cli and out-of-process updaters, I added a did finish installing event. I also have logic dealing with whether an update can be silently installed or not. I made closing status windows before app termination a reponsibility for the user driver now, not the installer drivers. I improved checking for updates before starting the updater, if Sparle didn't ask the user for permission yet. Currently, Autoupdate can persist in the background even when the app is terminated, if we don't have sufficient privilleges to install right away for example, or if an update isn't silent. After much thinking, I need to ditch this strategy because: 1. An app can be manually replaced and so the installer would have to monitor that 2. We leave a zombie process running
219 lines
6.7 KiB
Objective-C
219 lines
6.7 KiB
Objective-C
//
|
|
// SUCommandLineUserDriver.m
|
|
// Sparkle
|
|
//
|
|
// Created by Mayur Pawashe on 4/10/16.
|
|
// Copyright © 2016 Sparkle Project. All rights reserved.
|
|
//
|
|
|
|
#import "SUCommandLineUserDriver.h"
|
|
#import <AppKit/AppKit.h>
|
|
|
|
@interface SUCommandLineUserDriver ()
|
|
|
|
@property (nonatomic, readonly) NSBundle *bundle;
|
|
@property (nonatomic, readonly) SUUserDriverCoreComponent *coreComponent;
|
|
@property (nonatomic) NSUInteger bytesDownloaded;
|
|
@property (nonatomic) NSUInteger bytesToDownload;
|
|
|
|
@end
|
|
|
|
@implementation SUCommandLineUserDriver
|
|
|
|
@synthesize bundle = _bundle;
|
|
@synthesize coreComponent = _coreComponent;
|
|
@synthesize bytesDownloaded = _bytesDownloaded;
|
|
@synthesize bytesToDownload = _bytesToDownload;
|
|
|
|
- (instancetype)initWithBundle:(NSBundle *)bundle
|
|
{
|
|
self = [super init];
|
|
if (self != nil) {
|
|
_bundle = bundle;
|
|
_coreComponent = [[SUUserDriverCoreComponent alloc] initWithDelegate:nil];
|
|
}
|
|
return self;
|
|
}
|
|
|
|
- (void)showCanCheckForUpdates:(BOOL)canCheckForUpdates
|
|
{
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
[self.coreComponent showCanCheckForUpdates:canCheckForUpdates];
|
|
});
|
|
}
|
|
|
|
- (void)idleOnUpdateChecks:(BOOL)shouldIdleOnUpdateChecks
|
|
{
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
[self.coreComponent idleOnUpdateChecks:shouldIdleOnUpdateChecks];
|
|
});
|
|
}
|
|
|
|
- (void)startUpdateCheckTimerWithNextTimeInterval:(NSTimeInterval)timeInterval reply:(void (^)(SUUpdateCheckTimerStatus))reply
|
|
{
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
[self.coreComponent startUpdateCheckTimerWithNextTimeInterval:timeInterval reply:reply];
|
|
});
|
|
}
|
|
|
|
- (void)invalidateUpdateCheckTimer
|
|
{
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
[self.coreComponent invalidateUpdateCheckTimer];
|
|
});
|
|
}
|
|
|
|
- (void)requestUpdatePermissionWithSystemProfile:(NSArray *)__unused systemProfile reply:(void (^)(SUUpdatePermissionPromptResult *))__unused reply
|
|
{
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
printf("Asked about making update permission decision.. Aborting because I do not want to make this decision.\n");
|
|
exit(EXIT_FAILURE);
|
|
});
|
|
}
|
|
|
|
- (void)showUserInitiatedUpdateCheckWithCompletion:(void (^)(SUUserInitiatedCheckStatus))updateCheckStatusCompletion
|
|
{
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
[self.coreComponent registerUpdateCheckStatusHandler:updateCheckStatusCompletion];
|
|
printf("Checking for Updates...\n");
|
|
});
|
|
}
|
|
|
|
- (void)dismissUserInitiatedUpdateCheck
|
|
{
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
[self.coreComponent completeUpdateCheckStatus];
|
|
});
|
|
}
|
|
|
|
- (void)showUpdateFoundWithAppcastItem:(SUAppcastItem *)appcastItem allowsAutomaticUpdates:(BOOL)__unused allowsAutomaticUpdates alreadyDownloaded:(BOOL)__unused alreadyDownloaded reply:(void (^)(SUUpdateAlertChoice))reply
|
|
{
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
printf("Found new update! (%s)\n", appcastItem.displayVersionString.UTF8String);
|
|
reply(SUInstallUpdateChoice);
|
|
});
|
|
}
|
|
|
|
- (void)showUpdateNotFoundWithAcknowledgement:(void (^)(void))__unused acknowledgement
|
|
{
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
printf("No new update available!\n");
|
|
exit(EXIT_SUCCESS);
|
|
});
|
|
}
|
|
|
|
- (void)showUpdaterError:(NSError *)error acknowledgement:(void (^)(void))__unused acknowledgement
|
|
{
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
printf("Error: Update check has failed: %s\n", error.description.UTF8String);
|
|
exit(EXIT_FAILURE);
|
|
});
|
|
}
|
|
|
|
- (void)showDownloadInitiatedWithCompletion:(void (^)(SUDownloadUpdateStatus))downloadUpdateStatusCompletion
|
|
{
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
[self.coreComponent registerDownloadStatusHandler:downloadUpdateStatusCompletion];
|
|
|
|
printf("Downloading Update...\n");
|
|
});
|
|
}
|
|
|
|
- (void)showDownloadDidReceiveResponse:(NSURLResponse *)response
|
|
{
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
printf("Downloading %llu bytes...\n", response.expectedContentLength);
|
|
self.bytesDownloaded = 0;
|
|
self.bytesToDownload = (NSUInteger)response.expectedContentLength;
|
|
});
|
|
}
|
|
|
|
- (void)showDownloadDidReceiveDataOfLength:(NSUInteger)length
|
|
{
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
self.bytesDownloaded += length;
|
|
if (self.bytesToDownload > 0) {
|
|
printf("Downloaded %lu out of %lu bytes (%.0f%%)\n", (unsigned long)self.bytesDownloaded, (unsigned long)self.bytesToDownload, (self.bytesDownloaded * 100.0 / self.bytesToDownload));
|
|
}
|
|
});
|
|
}
|
|
|
|
- (void)showDownloadFinishedAndStartedExtractingUpdate
|
|
{
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
[self.coreComponent completeDownloadStatus];
|
|
|
|
printf("Extracting update...\n");
|
|
});
|
|
}
|
|
|
|
- (void)showExtractionReceivedProgress:(double)progress
|
|
{
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
printf("Extracting Update (%.0f%%)\n", progress * 100);
|
|
});
|
|
}
|
|
|
|
- (void)showReadyToInstallAndRelaunch:(void (^)(SUInstallUpdateStatus))installUpdateHandler
|
|
{
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
[self.coreComponent registerInstallUpdateHandler:installUpdateHandler];
|
|
[self.coreComponent installAndShouldRestart:[self targetRunningApplication] != nil];
|
|
});
|
|
}
|
|
|
|
- (void)showInstallingUpdate
|
|
{
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
printf("Installing Update...\n");
|
|
});
|
|
}
|
|
|
|
- (void)showUpdateInstallationDidFinish
|
|
{
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
printf("Installation Finished...\n");
|
|
});
|
|
}
|
|
|
|
- (void)dismissUpdateInstallation
|
|
{
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
printf("Exiting...\n");
|
|
exit(EXIT_SUCCESS);
|
|
});
|
|
}
|
|
|
|
- (void)terminateApplication
|
|
{
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
NSRunningApplication *runningApplication = [self targetRunningApplication];
|
|
if (runningApplication != nil) {
|
|
if (![runningApplication terminate]) {
|
|
if (![runningApplication forceTerminate]) {
|
|
printf("Error: Failed to terminate running application. Please terminate it to update it.");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
- (NSRunningApplication *)targetRunningApplication
|
|
{
|
|
NSArray<NSRunningApplication *> *runningApplications = [[NSWorkspace sharedWorkspace] runningApplications];
|
|
// Make sure we *don't* use NSURLs for equality comparison; turns out to not work that great
|
|
NSString *bundlePath = self.bundle.bundleURL.path;
|
|
if (bundlePath != nil) {
|
|
for (NSRunningApplication *runningApplication in runningApplications) {
|
|
NSString *candidatePath = runningApplication.bundleURL.path;
|
|
if (candidatePath != nil && [bundlePath isEqualToString:candidatePath]) {
|
|
return runningApplication;
|
|
}
|
|
}
|
|
}
|
|
return nil;
|
|
}
|
|
|
|
@end
|