From 2c1820f2a9955c25691ef402953aa6182d21b5ec Mon Sep 17 00:00:00 2001 From: Zorg Date: Fri, 23 Dec 2016 22:21:19 -0500 Subject: [PATCH] Decrease responsibility of SUHost and move code into other components --- Sparkle.xcodeproj/project.pbxproj | 28 +++++++ Sparkle/Autoupdate/Autoupdate.m | 15 +++- Sparkle/SUApplicationInfo.h | 21 ++++++ Sparkle/SUApplicationInfo.m | 36 +++++++++ Sparkle/SUAutomaticUpdateAlert.m | 6 +- Sparkle/SUAutomaticUpdateDriver.m | 3 +- Sparkle/SUBundleIcon.h | 19 +++++ Sparkle/SUBundleIcon.m | 29 +++++++ Sparkle/SUHost.h | 13 ---- Sparkle/SUHost.m | 104 ++------------------------ Sparkle/SUOperatingSystem.h | 8 ++ Sparkle/SUOperatingSystem.m | 6 ++ Sparkle/SUStatusController.m | 7 +- Sparkle/SUSystemProfiler.h | 5 +- Sparkle/SUSystemProfiler.m | 14 +--- Sparkle/SUSystemUpdateInfo.h | 21 ++++++ Sparkle/SUSystemUpdateInfo.m | 58 ++++++++++++++ Sparkle/SUUIBasedUpdateDriver.m | 7 +- Sparkle/SUUpdateAlert.m | 8 +- Sparkle/SUUpdateDriver.m | 2 +- Sparkle/SUUpdatePermissionPrompt.m | 5 +- Sparkle/SUUpdater.m | 12 +-- Sparkle/SUUserInitiatedUpdateDriver.m | 3 +- 23 files changed, 284 insertions(+), 146 deletions(-) create mode 100644 Sparkle/SUApplicationInfo.h create mode 100644 Sparkle/SUApplicationInfo.m create mode 100644 Sparkle/SUBundleIcon.h create mode 100644 Sparkle/SUBundleIcon.m create mode 100644 Sparkle/SUSystemUpdateInfo.h create mode 100644 Sparkle/SUSystemUpdateInfo.m diff --git a/Sparkle.xcodeproj/project.pbxproj b/Sparkle.xcodeproj/project.pbxproj index 68189b76..b14a1470 100644 --- a/Sparkle.xcodeproj/project.pbxproj +++ b/Sparkle.xcodeproj/project.pbxproj @@ -190,6 +190,14 @@ 722954CB1D04EA6900ECF9CA /* Security.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 61B5F8F609C4CEB300B25A18 /* Security.framework */; }; 722954CC1D04EA7000ECF9CA /* SUOperatingSystem.m in Sources */ = {isa = PBXBuildFile; fileRef = 726F2CE41BC9C33D001971A4 /* SUOperatingSystem.m */; }; 72316BD61E0DBF730039EFD9 /* SULocalizations.h in Headers */ = {isa = PBXBuildFile; fileRef = 72316BD51E0DBF730039EFD9 /* SULocalizations.h */; }; + 72316BD91E0E17180039EFD9 /* SUBundleIcon.h in Headers */ = {isa = PBXBuildFile; fileRef = 72316BD71E0E17180039EFD9 /* SUBundleIcon.h */; }; + 72316BDA1E0E17180039EFD9 /* SUBundleIcon.m in Sources */ = {isa = PBXBuildFile; fileRef = 72316BD81E0E17180039EFD9 /* SUBundleIcon.m */; }; + 72316BDB1E0E17180039EFD9 /* SUBundleIcon.m in Sources */ = {isa = PBXBuildFile; fileRef = 72316BD81E0E17180039EFD9 /* SUBundleIcon.m */; }; + 72316BDE1E0E17910039EFD9 /* SUApplicationInfo.h in Headers */ = {isa = PBXBuildFile; fileRef = 72316BDC1E0E17910039EFD9 /* SUApplicationInfo.h */; }; + 72316BDF1E0E17910039EFD9 /* SUApplicationInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = 72316BDD1E0E17910039EFD9 /* SUApplicationInfo.m */; }; + 72316BE01E0E17910039EFD9 /* SUApplicationInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = 72316BDD1E0E17910039EFD9 /* SUApplicationInfo.m */; }; + 72316BE31E0E1C7F0039EFD9 /* SUSystemUpdateInfo.h in Headers */ = {isa = PBXBuildFile; fileRef = 72316BE11E0E1C7F0039EFD9 /* SUSystemUpdateInfo.h */; }; + 72316BE41E0E1C7F0039EFD9 /* SUSystemUpdateInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = 72316BE21E0E1C7F0039EFD9 /* SUSystemUpdateInfo.m */; }; 723A920E1D722438004A9DED /* SUSpotlightImporterTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 723A920D1D722438004A9DED /* SUSpotlightImporterTest.swift */; }; 723B252D1CEAB3A600909873 /* bscommon.c in Sources */ = {isa = PBXBuildFile; fileRef = 723B252B1CEAB3A600909873 /* bscommon.c */; }; 723B252E1CEAB3A600909873 /* bscommon.c in Sources */ = {isa = PBXBuildFile; fileRef = 723B252B1CEAB3A600909873 /* bscommon.c */; }; @@ -687,6 +695,12 @@ 722FB7481DD51350001D40CE /* ConfigSwiftDebug.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = ConfigSwiftDebug.xcconfig; sourceTree = ""; }; 722FB7491DD51350001D40CE /* ConfigSwiftRelease.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = ConfigSwiftRelease.xcconfig; sourceTree = ""; }; 72316BD51E0DBF730039EFD9 /* SULocalizations.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SULocalizations.h; sourceTree = ""; }; + 72316BD71E0E17180039EFD9 /* SUBundleIcon.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SUBundleIcon.h; sourceTree = ""; }; + 72316BD81E0E17180039EFD9 /* SUBundleIcon.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SUBundleIcon.m; sourceTree = ""; }; + 72316BDC1E0E17910039EFD9 /* SUApplicationInfo.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SUApplicationInfo.h; sourceTree = ""; }; + 72316BDD1E0E17910039EFD9 /* SUApplicationInfo.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SUApplicationInfo.m; sourceTree = ""; }; + 72316BE11E0E1C7F0039EFD9 /* SUSystemUpdateInfo.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SUSystemUpdateInfo.h; sourceTree = ""; }; + 72316BE21E0E1C7F0039EFD9 /* SUSystemUpdateInfo.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SUSystemUpdateInfo.m; sourceTree = ""; }; 723A920D1D722438004A9DED /* SUSpotlightImporterTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SUSpotlightImporterTest.swift; sourceTree = ""; }; 723B252B1CEAB3A600909873 /* bscommon.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = bscommon.c; sourceTree = ""; }; 723B252C1CEAB3A600909873 /* bscommon.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = bscommon.h; sourceTree = ""; }; @@ -1112,6 +1126,12 @@ 61EF67550E25B58D00F754E0 /* SUHost.m */, 55C14F04136EF6DB00649790 /* SULog.h */, 55C14F05136EF6DB00649790 /* SULog.m */, + 72316BD71E0E17180039EFD9 /* SUBundleIcon.h */, + 72316BD81E0E17180039EFD9 /* SUBundleIcon.m */, + 72316BDC1E0E17910039EFD9 /* SUApplicationInfo.h */, + 72316BDD1E0E17910039EFD9 /* SUApplicationInfo.m */, + 72316BE11E0E1C7F0039EFD9 /* SUSystemUpdateInfo.h */, + 72316BE21E0E1C7F0039EFD9 /* SUSystemUpdateInfo.m */, 726F2CE31BC9C33D001971A4 /* SUOperatingSystem.h */, 726F2CE41BC9C33D001971A4 /* SUOperatingSystem.m */, ); @@ -1265,6 +1285,7 @@ 5D06E9390FD69271005AE3F6 /* SUBinaryDeltaUnarchiver.h in Headers */, 61B078CE15A5FB6100600039 /* SUCodeSigningVerifier.h in Headers */, 61299A5C09CA6D4500B7442F /* SUConstants.h in Headers */, + 72316BE31E0E1C7F0039EFD9 /* SUSystemUpdateInfo.h in Headers */, 6102FE4A0E07803800F85D09 /* SUDiskImageUnarchiver.h in Headers */, 61299A2F09CA2DAB00B7442F /* SUDSAVerifier.h in Headers */, 55E6F33319EC9F6C00005E76 /* SUErrors.h in Headers */, @@ -1291,9 +1312,11 @@ 61299A8D09CA790200B7442F /* SUUnarchiver.h in Headers */, 61B5FCDF09C52A9F00B25A18 /* SUUpdateAlert.h in Headers */, 610134730DD250470049ACDF /* SUUpdateDriver.h in Headers */, + 72316BD91E0E17180039EFD9 /* SUBundleIcon.h in Headers */, 612DCBAF0D488BC60015DBEA /* SUUpdatePermissionPrompt.h in Headers */, 61B5F8ED09C4CE3C00B25A18 /* SUUpdater.h in Headers */, 6158A1C5137904B300487EC1 /* SUUpdater_Private.h in Headers */, + 72316BDE1E0E17910039EFD9 /* SUApplicationInfo.h in Headers */, 61A354550DF113C70076ECB1 /* SUUserInitiatedUpdateDriver.h in Headers */, 61A2259E0D1C495D00430CCD /* SUVersionComparisonProtocol.h in Headers */, 3772FEA913DE0B6B00F79537 /* SUVersionDisplayProtocol.h in Headers */, @@ -1712,9 +1735,11 @@ files = ( 55C14BD4136EEFCE00649790 /* Autoupdate.m in Sources */, F8761EB61ADC5E7A000C9034 /* SUCodeSigningVerifier.m in Sources */, + 72316BE01E0E17910039EFD9 /* SUApplicationInfo.m in Sources */, 55C14F00136EF6B700649790 /* SUConstants.m in Sources */, 7275F9C31B5F1F2900B1D19E /* SUFileManager.m in Sources */, 767B61AE1972D488004E0C3C /* SUGuidedPackageInstaller.m in Sources */, + 72316BDB1E0E17180039EFD9 /* SUBundleIcon.m in Sources */, 55C14F0C136EF6EA00649790 /* SUHost.m in Sources */, 55C14F0D136EF6F200649790 /* SUInstaller.m in Sources */, 55C14F08136EF6DB00649790 /* SULog.m in Sources */, @@ -1830,6 +1855,7 @@ 61EF67560E25B58D00F754E0 /* SUHost.m in Sources */, 618FA5020DAE88B40026945C /* SUInstaller.m in Sources */, 55C14F07136EF6DB00649790 /* SULog.m in Sources */, + 72316BDA1E0E17180039EFD9 /* SUBundleIcon.m in Sources */, 729924741DF3478A00DBCDF5 /* SUUpdateValidator.m in Sources */, 723B252D1CEAB3A600909873 /* bscommon.c in Sources */, 726F2CE61BC9C33D001971A4 /* SUOperatingSystem.m in Sources */, @@ -1843,9 +1869,11 @@ 61A2279D0D1CEE7600430CCD /* SUSystemProfiler.m in Sources */, 61B93A3D0DD02D7000DCD2F8 /* SUUIBasedUpdateDriver.m in Sources */, 61299A8E09CA790200B7442F /* SUUnarchiver.m in Sources */, + 72316BDF1E0E17910039EFD9 /* SUApplicationInfo.m in Sources */, 61B5FCDE09C52A9F00B25A18 /* SUUpdateAlert.m in Sources */, 610134740DD250470049ACDF /* SUUpdateDriver.m in Sources */, 612DCBB00D488BC60015DBEA /* SUUpdatePermissionPrompt.m in Sources */, + 72316BE41E0E1C7F0039EFD9 /* SUSystemUpdateInfo.m in Sources */, 61B5F8EE09C4CE3C00B25A18 /* SUUpdater.m in Sources */, 61A354560DF113C70076ECB1 /* SUUserInitiatedUpdateDriver.m in Sources */, 61180BCB0D64138900B4E0D1 /* SUWindowController.m in Sources */, diff --git a/Sparkle/Autoupdate/Autoupdate.m b/Sparkle/Autoupdate/Autoupdate.m index e92ebfb0..55f608ef 100644 --- a/Sparkle/Autoupdate/Autoupdate.m +++ b/Sparkle/Autoupdate/Autoupdate.m @@ -154,11 +154,24 @@ static const NSTimeInterval SUTerminationTimeDelay = 0.5; }]; } +- (NSString *)installationPathForBundle:(NSBundle *)bundle +{ + if (SPARKLE_NORMALIZE_INSTALLED_APPLICATION_NAME) { + // We'll install to "#{CFBundleName}.app", but only if that path doesn't already exist. If we're "Foo 4.2.app," and there's a "Foo.app" in this directory, we don't want to overwrite it! But if there's no "Foo.app," we'll take that name. + NSString *normalizedAppPath = [[[bundle bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", [bundle objectForInfoDictionaryKey:(__bridge NSString *)kCFBundleNameKey], [[bundle bundlePath] pathExtension]]]; + + if (![[NSFileManager defaultManager] fileExistsAtPath:normalizedAppPath]) { + return normalizedAppPath; + } + } + return [bundle bundlePath]; +} + - (void)install { NSBundle *theBundle = [NSBundle bundleWithPath:self.hostPath]; SUHost *host = [[SUHost alloc] initWithBundle:theBundle]; - NSString *installationPath = [[host installationPath] copy]; + NSString *installationPath = [self installationPathForBundle:theBundle]; if (self.shouldShowUI) { self.statusController = [[SUStatusController alloc] initWithHost:host]; diff --git a/Sparkle/SUApplicationInfo.h b/Sparkle/SUApplicationInfo.h new file mode 100644 index 00000000..db4dd1ca --- /dev/null +++ b/Sparkle/SUApplicationInfo.h @@ -0,0 +1,21 @@ +// +// SUApplicationInfo.h +// Sparkle +// +// Created by Mayur Pawashe on 2/28/16. +// Copyright © 2016 Sparkle Project. All rights reserved. +// + +#import + +NS_ASSUME_NONNULL_BEGIN + +@interface SUApplicationInfo : NSObject + ++ (BOOL)isBackgroundApplication:(NSApplication *)application; + ++ (NSImage *)bestIconForBundle:(NSBundle *)bundle; + +@end + +NS_ASSUME_NONNULL_END diff --git a/Sparkle/SUApplicationInfo.m b/Sparkle/SUApplicationInfo.m new file mode 100644 index 00000000..c8fec0be --- /dev/null +++ b/Sparkle/SUApplicationInfo.m @@ -0,0 +1,36 @@ +// +// SUApplicationInfo.m +// Sparkle +// +// Created by Mayur Pawashe on 2/28/16. +// Copyright © 2016 Sparkle Project. All rights reserved. +// + +#import "SUApplicationInfo.h" +#import "SUBundleIcon.h" + +@implementation SUApplicationInfo + ++ (BOOL)isBackgroundApplication:(NSApplication *)application +{ + return (application.activationPolicy == NSApplicationActivationPolicyAccessory); +} + ++ (NSImage *)bestIconForBundle:(NSBundle *)bundle +{ + NSURL *iconURL = [SUBundleIcon iconURLForBundle:bundle]; + + NSImage *icon = (iconURL == nil) ? nil : [[NSImage alloc] initWithContentsOfURL:iconURL]; + // Use a default icon if none is defined. + if (!icon) { + // this asumption may not be correct (eg. even though we're not the main bundle, it could be still be a regular app) + // but still better than nothing if no icon was included + BOOL isMainBundle = (bundle == [NSBundle mainBundle]); + + NSString *fileType = isMainBundle ? (__bridge NSString *)kUTTypeApplication : (__bridge NSString *)kUTTypeBundle; + icon = [[NSWorkspace sharedWorkspace] iconForFileType:fileType]; + } + return icon; +} + +@end diff --git a/Sparkle/SUAutomaticUpdateAlert.m b/Sparkle/SUAutomaticUpdateAlert.m index 91dbddec..f49dae6e 100644 --- a/Sparkle/SUAutomaticUpdateAlert.m +++ b/Sparkle/SUAutomaticUpdateAlert.m @@ -9,7 +9,7 @@ #import "SUAutomaticUpdateAlert.h" #import "SULocalizations.h" #import "SUAppcastItem.h" - +#import "SUApplicationInfo.h" #import "SUHost.h" @interface SUAutomaticUpdateAlert () @@ -36,7 +36,7 @@ return self; } -- (NSString *__nonnull)description { return [NSString stringWithFormat:@"%@ <%@, %@>", [self class], [self.host bundlePath], [self.host installationPath]]; } +- (NSString *__nonnull)description { return [NSString stringWithFormat:@"%@ <%@>", [self class], [self.host bundlePath]]; } - (IBAction)installNow:(id)__unused sender { @@ -61,7 +61,7 @@ - (NSImage *__nonnull)applicationIcon { - return [self.host icon]; + return [SUApplicationInfo bestIconForBundle:self.host.bundle]; } - (NSString *__nonnull)titleText diff --git a/Sparkle/SUAutomaticUpdateDriver.m b/Sparkle/SUAutomaticUpdateDriver.m index 8e7629fd..973ccfd7 100644 --- a/Sparkle/SUAutomaticUpdateDriver.m +++ b/Sparkle/SUAutomaticUpdateDriver.m @@ -14,6 +14,7 @@ #import "SUHost.h" #import "SUConstants.h" #import "SUAppcastItem.h" +#import "SUApplicationInfo.h" // If the user hasn't quit in a week, ask them if they want to relaunch to get the latest bits. It doesn't matter that this measure of "one day" is imprecise. static const NSTimeInterval SUAutomaticUpdatePromptImpatienceTimer = 60 * 60 * 24 * 7; @@ -54,7 +55,7 @@ static const NSTimeInterval SUAutomaticUpdatePromptImpatienceTimer = 60 * 60 * 2 // If the app is a menubar app or the like, we need to focus it first and alter the // update prompt to behave like a normal window. Otherwise if the window were hidden // there may be no way for the application to be activated to make it visible again. - if ([self.host isBackgroundApplication]) { + if ([SUApplicationInfo isBackgroundApplication:[NSApplication sharedApplication]]) { [[self.alert window] setHidesOnDeactivate:NO]; [NSApp activateIgnoringOtherApps:YES]; } diff --git a/Sparkle/SUBundleIcon.h b/Sparkle/SUBundleIcon.h new file mode 100644 index 00000000..f42be2fd --- /dev/null +++ b/Sparkle/SUBundleIcon.h @@ -0,0 +1,19 @@ +// +// SUBundleIcon.h +// Sparkle +// +// Created by Mayur Pawashe on 7/24/16. +// Copyright © 2016 Sparkle Project. All rights reserved. +// + +#import + +NS_ASSUME_NONNULL_BEGIN + +@interface SUBundleIcon : NSObject + ++ (NSURL * _Nullable)iconURLForBundle:(NSBundle *)bundle; + +@end + +NS_ASSUME_NONNULL_END diff --git a/Sparkle/SUBundleIcon.m b/Sparkle/SUBundleIcon.m new file mode 100644 index 00000000..a3d86a80 --- /dev/null +++ b/Sparkle/SUBundleIcon.m @@ -0,0 +1,29 @@ +// +// SUBundleIcon.m +// Sparkle +// +// Created by Mayur Pawashe on 7/24/16. +// Copyright © 2016 Sparkle Project. All rights reserved. +// + +#import "SUBundleIcon.h" + +@implementation SUBundleIcon + ++ (NSURL *)iconURLForBundle:(NSBundle *)bundle +{ + NSString *resource = [bundle objectForInfoDictionaryKey:@"CFBundleIconFile"]; + if (resource == nil || ![resource isKindOfClass:[NSString class]]) { + return nil; + } + + NSURL *iconURL = [bundle URLForResource:resource withExtension:@"icns"]; + + // The resource could already be containing the path extension, so try again without the extra extension + if (iconURL == nil) { + iconURL = [bundle URLForResource:resource withExtension:nil]; + } + return iconURL; +} + +@end diff --git a/Sparkle/SUHost.h b/Sparkle/SUHost.h index d3390692..aa3c72cf 100644 --- a/Sparkle/SUHost.h +++ b/Sparkle/SUHost.h @@ -7,31 +7,18 @@ #import -#if __MAC_OS_X_VERSION_MAX_ALLOWED < 101000 -typedef struct { - NSInteger majorVersion; - NSInteger minorVersion; - NSInteger patchVersion; -} NSOperatingSystemVersion; -#endif - @interface SUHost : NSObject @property (strong, readonly) NSBundle *bundle; - (instancetype)initWithBundle:(NSBundle *)aBundle; @property (readonly, copy) NSString *bundlePath; -@property (readonly) BOOL allowsAutomaticUpdates; -@property (readonly, copy) NSString *installationPath; @property (readonly, copy) NSString *name; @property (readonly, copy) NSString *version; @property (readonly, copy) NSString *displayVersion; -@property (readonly, copy) NSImage *icon; @property (getter=isRunningOnReadOnlyVolume, readonly) BOOL runningOnReadOnlyVolume; -@property (getter=isBackgroundApplication, readonly) BOOL backgroundApplication; @property (readonly, copy) NSString *publicDSAKey; @property (readonly, nonatomic, copy) NSString *publicDSAKeyFileKey; -@property (readonly, copy) NSArray *systemProfile; - (id)objectForInfoDictionaryKey:(NSString *)key; - (BOOL)boolForInfoDictionaryKey:(NSString *)key; diff --git a/Sparkle/SUHost.m b/Sparkle/SUHost.m index 3d59f1a9..8470ae22 100644 --- a/Sparkle/SUHost.m +++ b/Sparkle/SUHost.m @@ -6,19 +6,16 @@ // #import "SUHost.h" -#import // for NSImage related code #import "SUConstants.h" -#import "SUSystemProfiler.h" #include // For statfs for isRunningOnReadOnlyVolume #import "SULog.h" -#import "SUFileManager.h" -#if __MAC_OS_X_VERSION_MAX_ALLOWED < 101000 -@interface NSProcessInfo () -- (NSOperatingSystemVersion)operatingSystemVersion; -@end -#endif +// This class should not rely on AppKit and should also be process independent +// For example, it should not have code that tests writabilty to somewhere on disk, +// as that may depend on the privileges of the process owner. Or code that depends on +// if the process is sandboxed or not; eg: finding the user's caches directory. Or code that depends +// on compilation flags and if other files exist relative to the host bundle. @interface SUHost () @@ -57,68 +54,13 @@ } -- (NSString *)description { return [NSString stringWithFormat:@"%@ <%@, %@>", [self class], [self bundlePath], [self installationPath]]; } +- (NSString *)description { return [NSString stringWithFormat:@"%@ <%@>", [self class], [self bundlePath]]; } - (NSString *)bundlePath { return [self.bundle bundlePath]; } -- (BOOL)allowsAutomaticUpdates -{ - // Does the developer want us to disable automatic updates? - NSNumber *developerAllowsAutomaticUpdates = [self objectForInfoDictionaryKey:SUAllowsAutomaticUpdatesKey]; - if (developerAllowsAutomaticUpdates != nil && !developerAllowsAutomaticUpdates.boolValue) { - return NO; - } - - // Can we automatically update in the background without bugging the user (e.g, with a administrator password prompt)? - // Note it's very well possible to have the bundle be writable but not be able to write into the parent directory - // And if the bundle isn't writable, but we can write into the parent directory, we will still need to authorize to replace it - NSString *bundlePath = [self bundlePath]; - if (![[NSFileManager defaultManager] isWritableFileAtPath:bundlePath.stringByDeletingLastPathComponent] || ![[NSFileManager defaultManager] isWritableFileAtPath:bundlePath]) { - return NO; - } - - // Just because we have writability access does not mean we can set the correct owner/group silently - // Test if we can set the owner/group on a temporarily created file - // If we can, then we can probably perform an update without authorization - // One place where this matters is if you copy and run an app from /tmp/ - - NSString *tempFilename = @"permission_test" ; - - SUFileManager *suFileManager = [SUFileManager defaultManager]; - NSURL *tempDirectoryURL = [suFileManager makeTemporaryDirectoryWithPreferredName:tempFilename appropriateForDirectoryURL:[NSURL fileURLWithPath:NSTemporaryDirectory()] error:NULL]; - - if (tempDirectoryURL == nil) { - // I don't imagine this ever happening but in case it does, requesting authorization may be the better option - return NO; - } - - NSURL *tempFileURL = [tempDirectoryURL URLByAppendingPathComponent:tempFilename]; - - BOOL changeOwnerAndGroupSuccess = - [[NSData data] writeToURL:tempFileURL atomically:NO] && - [suFileManager changeOwnerAndGroupOfItemAtRootURL:tempFileURL toMatchURL:self.bundle.bundleURL error:NULL]; - - [suFileManager removeItemAtURL:tempDirectoryURL error:NULL]; - - return changeOwnerAndGroupSuccess; -} - -- (NSString *)installationPath -{ - if (SPARKLE_NORMALIZE_INSTALLED_APPLICATION_NAME) { - // We'll install to "#{CFBundleName}.app", but only if that path doesn't already exist. If we're "Foo 4.2.app," and there's a "Foo.app" in this directory, we don't want to overwrite it! But if there's no "Foo.app," we'll take that name. - NSString *normalizedAppPath = [[[self.bundle bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", [self.bundle objectForInfoDictionaryKey:(__bridge NSString *)kCFBundleNameKey], [[self.bundle bundlePath] pathExtension]]]; - - if (![[NSFileManager defaultManager] fileExistsAtPath:normalizedAppPath]) { - return normalizedAppPath; - } - } - return [self.bundle bundlePath]; -} - - (NSString *__nonnull)name { NSString *name; @@ -153,29 +95,6 @@ return [self version]; // Fall back on the normal version string. } -- (NSImage *__nonnull)icon -{ - // Cache the application icon. - NSString *iconPath = [self.bundle pathForResource:[self.bundle objectForInfoDictionaryKey:@"CFBundleIconFile"] ofType:@"icns"]; - // According to the macOS docs, "CFBundleIconFile - This key identifies the file containing - // the icon for the bundle. The filename you specify does not need to include the .icns - // extension, although it may." - // - // However, if it *does* include the '.icns' the above method fails (tested on macOS 10.3.9) so we'll also try: - if (!iconPath) { - iconPath = [self.bundle pathForResource:[self.bundle objectForInfoDictionaryKey:@"CFBundleIconFile"] ofType:nil]; - } - NSImage *icon = [[NSImage alloc] initWithContentsOfFile:iconPath]; - // Use a default icon if none is defined. - if (!icon) { - BOOL isMainBundle = (self.bundle == [NSBundle mainBundle]); - - NSString *fileType = isMainBundle ? (__bridge NSString *)kUTTypeApplication : (__bridge NSString *)kUTTypeBundle; - icon = [[NSWorkspace sharedWorkspace] iconForFileType:fileType]; - } - return icon; -} - - (BOOL)isRunningOnReadOnlyVolume { struct statfs statfs_info; @@ -183,11 +102,6 @@ return (statfs_info.f_flags & MNT_RDONLY) != 0; } -- (BOOL)isBackgroundApplication -{ - return ([[NSApplication sharedApplication] activationPolicy] == NSApplicationActivationPolicyAccessory); -} - - (NSString *__nullable)publicDSAKey { // Maybe the key is just a string in the Info.plist. @@ -214,11 +128,6 @@ return [self objectForInfoDictionaryKey:SUPublicDSAKeyFileKey]; } -- (NSArray *)systemProfile -{ - return [[SUSystemProfiler sharedSystemProfiler] systemProfileArrayForHost:self]; -} - - (id)objectForInfoDictionaryKey:(NSString *)key { return [self.bundle objectForInfoDictionaryKey:key]; @@ -246,6 +155,7 @@ return CFBridgingRelease(obj); } +// Note this handles nil being passed for defaultName, in which case the user default will be removed - (void)setObject:(id)value forUserDefaultsKey:(NSString *)defaultName { if (self.usesStandardUserDefaults) diff --git a/Sparkle/SUOperatingSystem.h b/Sparkle/SUOperatingSystem.h index bd65275f..e4334d90 100644 --- a/Sparkle/SUOperatingSystem.h +++ b/Sparkle/SUOperatingSystem.h @@ -7,6 +7,14 @@ #import +#if __MAC_OS_X_VERSION_MAX_ALLOWED < 101000 +typedef struct { + NSInteger majorVersion; + NSInteger minorVersion; + NSInteger patchVersion; +} NSOperatingSystemVersion; +#endif + @interface SUOperatingSystem : NSObject + (NSOperatingSystemVersion)operatingSystemVersion; diff --git a/Sparkle/SUOperatingSystem.m b/Sparkle/SUOperatingSystem.m index 10993585..4a7d73aa 100644 --- a/Sparkle/SUOperatingSystem.m +++ b/Sparkle/SUOperatingSystem.m @@ -7,6 +7,12 @@ #import "SUOperatingSystem.h" +#if __MAC_OS_X_VERSION_MAX_ALLOWED < 101000 +@interface NSProcessInfo () +- (NSOperatingSystemVersion)operatingSystemVersion; +@end +#endif + @implementation SUOperatingSystem + (NSOperatingSystemVersion)operatingSystemVersion diff --git a/Sparkle/SUStatusController.m b/Sparkle/SUStatusController.m index 9ee2ec92..cdca9ff8 100644 --- a/Sparkle/SUStatusController.m +++ b/Sparkle/SUStatusController.m @@ -10,6 +10,7 @@ #import "SUStatusController.h" #import "SUHost.h" #import "SULocalizations.h" +#import "SUApplicationInfo.h" @interface SUStatusController () @property (copy) NSString *title, *buttonTitle; @@ -38,11 +39,11 @@ return self; } -- (NSString *)description { return [NSString stringWithFormat:@"%@ <%@, %@>", [self class], [self.host bundlePath], [self.host installationPath]]; } +- (NSString *)description { return [NSString stringWithFormat:@"%@ <%@>", [self class], [self.host bundlePath]]; } - (void)windowDidLoad { - if ([self.host isBackgroundApplication]) { + if ([SUApplicationInfo isBackgroundApplication:[NSApplication sharedApplication]]) { [[self window] setLevel:NSFloatingWindowLevel]; } @@ -65,7 +66,7 @@ - (NSImage *)applicationIcon { - return [self.host icon]; + return [SUApplicationInfo bestIconForBundle:self.host.bundle]; } - (void)beginActionWithTitle:(NSString *)aTitle maxProgressValue:(double)aMaxProgressValue statusText:(NSString *)aStatusText diff --git a/Sparkle/SUSystemProfiler.h b/Sparkle/SUSystemProfiler.h index 6058288b..6223e22e 100644 --- a/Sparkle/SUSystemProfiler.h +++ b/Sparkle/SUSystemProfiler.h @@ -15,8 +15,9 @@ NS_ASSUME_NONNULL_BEGIN @class SUHost; @interface SUSystemProfiler : NSObject -+ (SUSystemProfiler *)sharedSystemProfiler; -- (NSMutableArray *)systemProfileArrayForHost:(SUHost *)host; + ++ (NSArray *)systemProfileArrayForHost:(SUHost *)host; + @end NS_ASSUME_NONNULL_END diff --git a/Sparkle/SUSystemProfiler.m b/Sparkle/SUSystemProfiler.m index 099d1ecc..ae2f10f5 100644 --- a/Sparkle/SUSystemProfiler.m +++ b/Sparkle/SUSystemProfiler.m @@ -27,23 +27,15 @@ static NSString *const SUSystemProfilerOperatingSystemVersionKey = @"osVersion"; static NSString *const SUSystemProfilerPreferredLanguageKey = @"lang"; @implementation SUSystemProfiler -+ (SUSystemProfiler *)sharedSystemProfiler -{ - static SUSystemProfiler *sharedSystemProfiler = nil; - if (!sharedSystemProfiler) { - sharedSystemProfiler = [[self alloc] init]; - } - return sharedSystemProfiler; -} -- (NSDictionary *)modelTranslationTable ++ (NSDictionary *)modelTranslationTable { // Use explicit class to use the correct bundle even when subclassed NSString *path = [[NSBundle bundleForClass:[SUSystemProfiler class]] pathForResource:@"SUModelTranslation" ofType:@"plist"]; return [[NSDictionary alloc] initWithContentsOfFile:path]; } -- (NSMutableArray *)systemProfileArrayForHost:(SUHost *)host ++ (NSArray *)systemProfileArrayForHost:(SUHost *)host { NSDictionary *modelTranslation = [self modelTranslationTable]; @@ -166,7 +158,7 @@ static NSString *const SUSystemProfilerPreferredLanguageKey = @"lang"; [profileArray addObject:[NSDictionary dictionaryWithObjects:@[SUSystemProfilerMemoryKey, @"Memory (MB)", @(megabytes), @(megabytes)] forKeys:profileDictKeys]]; } - return profileArray; + return [profileArray copy]; } @end diff --git a/Sparkle/SUSystemUpdateInfo.h b/Sparkle/SUSystemUpdateInfo.h new file mode 100644 index 00000000..28f89978 --- /dev/null +++ b/Sparkle/SUSystemUpdateInfo.h @@ -0,0 +1,21 @@ +// +// SUSystemUpdateInfo.h +// Sparkle +// +// Created by Mayur Pawashe on 12/23/16. +// Copyright © 2016 Sparkle Project. All rights reserved. +// + +#import + +NS_ASSUME_NONNULL_BEGIN + +@class SUHost; + +@interface SUSystemUpdateInfo : NSObject + ++ (BOOL)systemAllowsAutomaticUpdatesForHost:(SUHost *)host; + +@end + +NS_ASSUME_NONNULL_END diff --git a/Sparkle/SUSystemUpdateInfo.m b/Sparkle/SUSystemUpdateInfo.m new file mode 100644 index 00000000..7ef2dd8f --- /dev/null +++ b/Sparkle/SUSystemUpdateInfo.m @@ -0,0 +1,58 @@ +// +// SUSystemUpdateInfo.m +// Sparkle +// +// Created by Mayur Pawashe on 12/23/16. +// Copyright © 2016 Sparkle Project. All rights reserved. +// + +#import "SUSystemUpdateInfo.h" +#import "SUHost.h" +#import "SUConstants.h" +#import "SUFileManager.h" + +@implementation SUSystemUpdateInfo + ++ (BOOL)systemAllowsAutomaticUpdatesForHost:(SUHost *)host +{ + // Does the developer want us to disable automatic updates? + NSNumber *developerAllowsAutomaticUpdates = [host objectForInfoDictionaryKey:SUAllowsAutomaticUpdatesKey]; + if (developerAllowsAutomaticUpdates != nil && !developerAllowsAutomaticUpdates.boolValue) { + return NO; + } + + // Can we automatically update in the background without bugging the user (e.g, with a administrator password prompt)? + // Note it's very well possible to have the bundle be writable but not be able to write into the parent directory + // And if the bundle isn't writable, but we can write into the parent directory, we will still need to authorize to replace it + NSString *bundlePath = [host bundlePath]; + if (![[NSFileManager defaultManager] isWritableFileAtPath:bundlePath.stringByDeletingLastPathComponent] || ![[NSFileManager defaultManager] isWritableFileAtPath:bundlePath]) { + return NO; + } + + // Just because we have writability access does not mean we can set the correct owner/group silently + // Test if we can set the owner/group on a temporarily created file + // If we can, then we can probably perform an update without authorization + // One place where this matters is if you copy and run an app from /tmp/ + + NSString *tempFilename = @"permission_test" ; + + SUFileManager *suFileManager = [SUFileManager defaultManager]; + NSURL *tempDirectoryURL = [suFileManager makeTemporaryDirectoryWithPreferredName:tempFilename appropriateForDirectoryURL:[NSURL fileURLWithPath:NSTemporaryDirectory()] error:NULL]; + + if (tempDirectoryURL == nil) { + // I don't imagine this ever happening but in case it does, requesting authorization may be the better option + return NO; + } + + NSURL *tempFileURL = [tempDirectoryURL URLByAppendingPathComponent:tempFilename]; + + BOOL changeOwnerAndGroupSuccess = + [[NSData data] writeToURL:tempFileURL atomically:NO] && + [suFileManager changeOwnerAndGroupOfItemAtRootURL:tempFileURL toMatchURL:host.bundle.bundleURL error:NULL]; + + [suFileManager removeItemAtURL:tempDirectoryURL error:NULL]; + + return changeOwnerAndGroupSuccess; +} + +@end diff --git a/Sparkle/SUUIBasedUpdateDriver.m b/Sparkle/SUUIBasedUpdateDriver.m index 3919fc57..a57e5c6c 100644 --- a/Sparkle/SUUIBasedUpdateDriver.m +++ b/Sparkle/SUUIBasedUpdateDriver.m @@ -16,6 +16,7 @@ #import "SUConstants.h" #import "SULocalizations.h" #import "SUAppcastItem.h" +#import "SUApplicationInfo.h" #if __MAC_OS_X_VERSION_MAX_ALLOWED < 1080 @interface NSByteCountFormatter : NSFormatter { @@ -76,7 +77,7 @@ // If the app is a menubar app or the like, we need to focus it first and alter the // update prompt to behave like a normal window. Otherwise if the window were hidden // there may be no way for the application to be activated to make it visible again. - if ([self.host isBackgroundApplication]) { + if ([SUApplicationInfo isBackgroundApplication:[NSApplication sharedApplication]]) { [[self.updateAlert window] setHidesOnDeactivate:NO]; [NSApp activateIgnoringOtherApps:YES]; } @@ -297,9 +298,9 @@ // When showing a modal alert we need to ensure that background applications // are focused to inform the user since there is no dock icon to notify them. - if ([self.host isBackgroundApplication]) { [NSApp activateIgnoringOtherApps:YES]; } + if ([SUApplicationInfo isBackgroundApplication:[NSApplication sharedApplication]]) { [NSApp activateIgnoringOtherApps:YES]; } - [alert setIcon:[self.host icon]]; + [alert setIcon:[SUApplicationInfo bestIconForBundle:self.host.bundle]]; [alert runModal]; if ([[self.updater delegate] respondsToSelector:@selector(updaterDidShowModalAlert:)]) diff --git a/Sparkle/SUUpdateAlert.m b/Sparkle/SUUpdateAlert.m index ebb925ca..aa4da80e 100644 --- a/Sparkle/SUUpdateAlert.m +++ b/Sparkle/SUUpdateAlert.m @@ -19,6 +19,8 @@ #import "SULog.h" #import "SULocalizations.h" #import "SUAppcastItem.h" +#import "SUApplicationInfo.h" +#import "SUSystemUpdateInfo.h" // WebKit protocols are not explicitly declared until 10.11 SDK, so // declare dummy protocols to keep the build working on earlier SDKs. @@ -169,7 +171,7 @@ - (BOOL)allowsAutomaticUpdates { - return self.host.allowsAutomaticUpdates; + return [SUSystemUpdateInfo systemAllowsAutomaticUpdatesForHost:self.host]; } - (void)windowDidLoad @@ -178,7 +180,7 @@ [self.window setFrameAutosaveName: showReleaseNotes ? @"SUUpdateAlert" : @"SUUpdateAlertSmall" ]; - if ([self.host isBackgroundApplication]) { + if ([SUApplicationInfo isBackgroundApplication:[NSApplication sharedApplication]]) { [self.window setLevel:NSFloatingWindowLevel]; // This means the window will float over all other apps, if our app is switched out ?! } @@ -218,7 +220,7 @@ - (NSImage *)applicationIcon { - return [self.host icon]; + return [SUApplicationInfo bestIconForBundle:self.host.bundle]; } - (NSString *)titleText diff --git a/Sparkle/SUUpdateDriver.m b/Sparkle/SUUpdateDriver.m index 1b9d5249..0b98fe3c 100644 --- a/Sparkle/SUUpdateDriver.m +++ b/Sparkle/SUUpdateDriver.m @@ -37,7 +37,7 @@ NSString *const SUUpdateDriverFinishedNotification = @"SUUpdateDriverFinished"; return self; } -- (NSString *)description { return [NSString stringWithFormat:@"%@ <%@, %@>", [self class], [self.host bundlePath], [self.host installationPath]]; } +- (NSString *)description { return [NSString stringWithFormat:@"%@ <%@>", [self class], [self.host bundlePath]]; } - (void)checkForUpdatesAtURL:(NSURL *)URL host:(SUHost *)h { diff --git a/Sparkle/SUUpdatePermissionPrompt.m b/Sparkle/SUUpdatePermissionPrompt.m index 0a62bbfe..ba8e2cc3 100644 --- a/Sparkle/SUUpdatePermissionPrompt.m +++ b/Sparkle/SUUpdatePermissionPrompt.m @@ -11,6 +11,7 @@ #import "SUHost.h" #import "SUConstants.h" #import "SULocalizations.h" +#import "SUApplicationInfo.h" @interface SUUpdatePermissionPrompt () @@ -64,7 +65,7 @@ // If this is a background application we need to focus it in order to bring the prompt // to the user's attention. Otherwise the prompt would be hidden behind other applications and // the user would not know why the application was paused. - if ([aHost isBackgroundApplication]) { + if ([SUApplicationInfo isBackgroundApplication:[NSApplication sharedApplication]]) { [[NSApplication sharedApplication] activateIgnoringOtherApps:YES]; } @@ -97,7 +98,7 @@ - (NSImage *)icon { - return [self.host icon]; + return [SUApplicationInfo bestIconForBundle:self.host.bundle]; } - (NSString *)promptDescription diff --git a/Sparkle/SUUpdater.m b/Sparkle/SUUpdater.m index 2d114abf..b03d196e 100644 --- a/Sparkle/SUUpdater.m +++ b/Sparkle/SUUpdater.m @@ -20,6 +20,8 @@ #import "SUCodeSigningVerifier.h" #import "SULocalizations.h" #include +#import "SUSystemProfiler.h" +#import "SUSystemUpdateInfo.h" NSString *const SUUpdaterDidFinishLoadingAppCastNotification = @"SUUpdaterDidFinishLoadingAppCastNotification"; NSString *const SUUpdaterDidFindValidUpdateNotification = @"SUUpdaterDidFindValidUpdateNotification"; @@ -178,7 +180,7 @@ static NSString *const SUUpdaterDefaultsObservationContext = @"SUUpdaterDefaults return [self initForBundle:[NSBundle mainBundle]]; } -- (NSString *)description { return [NSString stringWithFormat:@"%@ <%@, %@>", [self class], [self.host bundlePath], [self.host installationPath]]; } +- (NSString *)description { return [NSString stringWithFormat:@"%@ <%@>", [self class], [self.host bundlePath]]; } - (void)startUpdateCycle { @@ -205,7 +207,7 @@ static NSString *const SUUpdaterDefaultsObservationContext = @"SUUpdaterDefaults } if (shouldPrompt) { - NSArray *profileInfo = [self.host systemProfile]; + NSArray *profileInfo = [SUSystemProfiler systemProfileArrayForHost:self.host]; // Always say we're sending the system profile here so that the delegate displays the parameters it would send. if ([self.delegate respondsToSelector:@selector(feedParametersForUpdater:sendingSystemProfile:)]) { profileInfo = [profileInfo arrayByAddingObjectsFromArray:[self.delegate feedParametersForUpdater:self sendingSystemProfile:YES]]; @@ -412,8 +414,8 @@ static NSString *const SUUpdaterDefaultsObservationContext = @"SUUpdaterDefaults - (BOOL)automaticallyDownloadsUpdates { - // If the host doesn't allow automatic updates, don't ever let them happen - if (!self.host.allowsAutomaticUpdates) { + // If we aren't allowed automatic updates, don't ever let them happen + if (![SUSystemUpdateInfo systemAllowsAutomaticUpdatesForHost:self.host]) { return NO; } @@ -491,7 +493,7 @@ static NSString *const SUUpdaterDefaultsObservationContext = @"SUUpdaterDefaults } if (sendingSystemProfile) { - parameters = [parameters arrayByAddingObjectsFromArray:[self.host systemProfile]]; + parameters = [parameters arrayByAddingObjectsFromArray:[SUSystemProfiler systemProfileArrayForHost:self.host]]; [self.host setObject:[NSDate date] forUserDefaultsKey:SULastProfileSubmitDateKey]; } if ([parameters count] == 0) { return baseFeedURL; } diff --git a/Sparkle/SUUserInitiatedUpdateDriver.m b/Sparkle/SUUserInitiatedUpdateDriver.m index 0d463ec8..816640aa 100644 --- a/Sparkle/SUUserInitiatedUpdateDriver.m +++ b/Sparkle/SUUserInitiatedUpdateDriver.m @@ -11,6 +11,7 @@ #import "SUStatusController.h" #import "SUHost.h" #import "SULocalizations.h" +#import "SUApplicationInfo.h" @interface SUUserInitiatedUpdateDriver () @@ -50,7 +51,7 @@ // For background applications, obtain focus. // Useful if the update check is requested from another app like System Preferences. - if ([aHost isBackgroundApplication]) + if ([SUApplicationInfo isBackgroundApplication:[NSApplication sharedApplication]]) { [NSApp activateIgnoringOtherApps:YES]; }