Show app icon in the prompt for authorization

This commit is contained in:
Zorg committed 2016-07-24 19:23:56 -04:00
1 parent e5643e2990
commit 1d3776d5c7
5 files changed
+144 -12

No files matched your search

+76 -1
View File
@@ -11,6 +11,11 @@
#import "SULog.h"
#import "SUMessageTypes.h"
#import "SUSystemAuthorization.h"
#import "SUBundleIcon.h"
#ifdef _APPKITDEFINES_H
#error This is a "core" class and should NOT import AppKit
#endif
@implementation SUInstallerLauncher
@@ -131,7 +136,73 @@
AuthorizationFlags flags = (AuthorizationFlags)(kAuthorizationFlagExtendRights | kAuthorizationFlagInteractionAllowed);
OSStatus copyStatus = AuthorizationCopyRights(auth, &rights, kAuthorizationEmptyEnvironment, flags, NULL);
AuthorizationItem iconAuthorizationItem = {.name = kAuthorizationEnvironmentIcon, .valueLength = 0, .value = NULL, .flags = 0};
AuthorizationEnvironment authorizationEnvironment = {.count = 0, .items = NULL};
// Find a 32x32 image representation of the icon, and write out a PNG version of it to a temporary location
// Then use the icon (if one is available) for the authorization prompt
// NSImage is not used because it relies on AppKit
NSURL *tempIconDestinationURL = nil;
NSURL *iconURL = [SUBundleIcon iconURLForBundle:hostBundle];
if (iconURL != nil) {
CGImageSourceRef imageSource = CGImageSourceCreateWithURL((CFURLRef)iconURL, (CFDictionaryRef)@{});
if (imageSource != NULL) {
size_t imageCount = CGImageSourceGetCount(imageSource);
for (size_t imageIndex = 0; imageIndex < imageCount; imageIndex++) {
CFDictionaryRef cfProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, imageIndex, (CFDictionaryRef)@{});
NSDictionary *properties = (__bridge NSDictionary *)(cfProperties);
NSNumber *pixelWidth = properties[(const NSString *)kCGImagePropertyPixelWidth];
NSNumber *pixelHeight = properties[(const NSString *)kCGImagePropertyPixelHeight];
// If we don't find a 32x32 image representation, then we don't provide an icon
// Don't try to make up with it by eg: converting an image representation to this size
// The app developer should be providing the icon representation.
// The authorization API may not take other size dimensions.
NSNumber *targetDimension = @32;
if ([pixelWidth isKindOfClass:[NSNumber class]] && [pixelHeight isKindOfClass:[NSNumber class]] && [pixelWidth isEqualToNumber:targetDimension] && [pixelHeight isEqualToNumber:targetDimension]) {
// Use /tmp rather than NSTemporaryDirectory() or SUFileManager's temp directory function because we want:
// a) no spaces in the path (SU/NSFileManager fails here)
// b) short file path that does not exceed a small threshold (NSTemporaryDirectory() fails here)
// These limitations only apply to older systems (eg: macOS 10.8)
// The file also needs to be placed in a system readable place such as /tmp
// See https://github.com/sparkle-project/Sparkle/issues/347#issuecomment-149523848 for more info
char pathBuffer[] = "/tmp/XXXXXX.png";
int tempIconFile = mkstemps(pathBuffer, strlen(".png"));
if (tempIconFile == -1) {
SULog(@"Failed to open temp icon from path buffer with error: %d", errno);
} else {
close(tempIconFile);
NSString *path = [[NSString alloc] initWithUTF8String:pathBuffer];
tempIconDestinationURL = [NSURL fileURLWithPath:path];
CGImageDestinationRef imageDestination = CGImageDestinationCreateWithURL((CFURLRef)tempIconDestinationURL, kUTTypePNG, 1, NULL);
if (imageDestination != NULL) {
CGImageDestinationAddImageFromSource(imageDestination, imageSource, imageIndex, (CFDictionaryRef)@{});
if (CGImageDestinationFinalize(imageDestination)) {
iconAuthorizationItem.valueLength = strlen(pathBuffer);
iconAuthorizationItem.value = pathBuffer;
authorizationEnvironment.count = 1;
authorizationEnvironment.items = &iconAuthorizationItem;
}
CFRelease(imageDestination);
}
}
break;
}
}
CFRelease(imageSource);
}
}
// This should prompt up the authorization dialog if necessary
OSStatus copyStatus = AuthorizationCopyRights(auth, &rights, &authorizationEnvironment, flags, NULL);
if (copyStatus != errAuthorizationSuccess) {
failedToUseSystemDomain = YES;
@@ -141,6 +212,10 @@
SULog(@"Failed copying system domain rights: %d", copyStatus);
}
}
if (tempIconDestinationURL != nil) {
[fileManager removeItemAtURL:tempIconDestinationURL error:NULL];
}
}
Boolean submittedJob = false;
+12
View File
@@ -98,6 +98,10 @@
720E217B1D0D00BF003A311C /* SUUpdaterCycle.h in Headers */ = {isa = PBXBuildFile; fileRef = 720E21791D0D00BF003A311C /* SUUpdaterCycle.h */; };
720E217C1D0D00BF003A311C /* SUUpdaterCycle.m in Sources */ = {isa = PBXBuildFile; fileRef = 720E217A1D0D00BF003A311C /* SUUpdaterCycle.m */; };
7210C7681B9A9A1500EB90AC /* SUUnarchiverTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7210C7671B9A9A1500EB90AC /* SUUnarchiverTest.swift */; };
7214B8811D456A8500CB5CED /* SUBundleIcon.h in Headers */ = {isa = PBXBuildFile; fileRef = 7214B87F1D456A8500CB5CED /* SUBundleIcon.h */; };
7214B8821D456A8500CB5CED /* SUBundleIcon.m in Sources */ = {isa = PBXBuildFile; fileRef = 7214B8801D456A8500CB5CED /* SUBundleIcon.m */; };
7214B8831D456C2400CB5CED /* SUBundleIcon.m in Sources */ = {isa = PBXBuildFile; fileRef = 7214B8801D456A8500CB5CED /* SUBundleIcon.m */; };
7214B8841D456C9500CB5CED /* SUBundleIcon.m in Sources */ = {isa = PBXBuildFile; fileRef = 7214B8801D456A8500CB5CED /* SUBundleIcon.m */; };
72162B061C82C8B30013C1C5 /* SUParameterAssert.h in Headers */ = {isa = PBXBuildFile; fileRef = 72162B051C82C8B30013C1C5 /* SUParameterAssert.h */; };
72162B081C82C9600013C1C5 /* SULocalizations.h in Headers */ = {isa = PBXBuildFile; fileRef = 72162B071C82C9600013C1C5 /* SULocalizations.h */; };
721652621D3C44A100FD13D8 /* SUSystemAuthorization.m in Sources */ = {isa = PBXBuildFile; fileRef = 724BB3AC1D347EDE005D534A /* SUSystemAuthorization.m */; };
@@ -754,6 +758,8 @@
720E21791D0D00BF003A311C /* SUUpdaterCycle.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SUUpdaterCycle.h; sourceTree = "<group>"; };
720E217A1D0D00BF003A311C /* SUUpdaterCycle.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SUUpdaterCycle.m; sourceTree = "<group>"; };
7210C7671B9A9A1500EB90AC /* SUUnarchiverTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SUUnarchiverTest.swift; sourceTree = "<group>"; };
7214B87F1D456A8500CB5CED /* SUBundleIcon.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SUBundleIcon.h; sourceTree = "<group>"; };
7214B8801D456A8500CB5CED /* SUBundleIcon.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SUBundleIcon.m; sourceTree = "<group>"; };
72162B051C82C8B30013C1C5 /* SUParameterAssert.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SUParameterAssert.h; sourceTree = "<group>"; };
72162B071C82C9600013C1C5 /* SULocalizations.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SULocalizations.h; sourceTree = "<group>"; };
721652671D3C8FED00FD13D8 /* SUAuthorizationReply.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SUAuthorizationReply.h; path = InstallerLauncher/SUAuthorizationReply.h; sourceTree = SOURCE_ROOT; };
@@ -1443,6 +1449,8 @@
7267E5E41D3D90AA00D1BF90 /* SUFileManager.m */,
721BC20C1D1CDE55002BC71E /* SULocalCacheDirectory.h */,
721BC20D1D1CDE55002BC71E /* SULocalCacheDirectory.m */,
7214B87F1D456A8500CB5CED /* SUBundleIcon.h */,
7214B8801D456A8500CB5CED /* SUBundleIcon.m */,
55C14F04136EF6DB00649790 /* SULog.h */,
55C14F05136EF6DB00649790 /* SULog.m */,
726F2CE31BC9C33D001971A4 /* SUOperatingSystem.h */,
@@ -1831,6 +1839,7 @@
72B09CE71CE6BCBD0052EF9E /* SUURLDownload.h in Headers */,
61A225A40D1C4AC000430CCD /* SUStandardVersionComparator.h in Headers */,
7229E1B91C97CC4D00CB50D0 /* SUScheduledUpdateDriver.h in Headers */,
7214B8811D456A8500CB5CED /* SUBundleIcon.h in Headers */,
7293A1AE1CEE933800B957A7 /* SUURLRequest.h in Headers */,
6196CFF909C72148000DC222 /* SUStatusController.h in Headers */,
72B767DE1C9CDB9700A07552 /* SUUserInitiatedUpdateDriver.h in Headers */,
@@ -2567,6 +2576,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
7214B8831D456C2400CB5CED /* SUBundleIcon.m in Sources */,
7267E5FA1D3DAC3600D1BF90 /* StatusInfo.m in Sources */,
7267E5EB1D3D90C200D1BF90 /* SUFileManager.m in Sources */,
7267E5DC1D3D8F1E00D1BF90 /* SUMessageTypes.m in Sources */,
@@ -2614,6 +2624,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
7214B8841D456C9500CB5CED /* SUBundleIcon.m in Sources */,
7267E5E11D3D901600D1BF90 /* SUMessageTypes.m in Sources */,
724BB3AF1D34828A005D534A /* SUSystemAuthorization.m in Sources */,
726E07C01CAF15B7001A286B /* SULog.m in Sources */,
@@ -2735,6 +2746,7 @@
720E217C1D0D00BF003A311C /* SUUpdaterCycle.m in Sources */,
72A450541C69A68900D67EEA /* SUUpdatePermission.m in Sources */,
724BB3881D32A167005D534A /* SUXPCInstallerConnection.m in Sources */,
7214B8821D456A8500CB5CED /* SUBundleIcon.m in Sources */,
7229E1BA1C97CC4D00CB50D0 /* SUScheduledUpdateDriver.m in Sources */,
725CB95B1C7121830064365A /* SUStandardUserDriver.m in Sources */,
61EF67560E25B58D00F754E0 /* SUHost.m in Sources */,
+4 -11
View File
@@ -7,6 +7,7 @@
//
#import "SUApplicationInfo.h"
#import "SUBundleIcon.h"
@implementation SUApplicationInfo
@@ -17,17 +18,9 @@
+ (NSImage *)bestIconForBundle:(NSBundle *)bundle
{
// Cache the application icon.
NSString *iconPath = [bundle pathForResource:[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 = [bundle pathForResource:[bundle objectForInfoDictionaryKey:@"CFBundleIconFile"] ofType:nil];
}
NSImage *icon = [[NSImage alloc] initWithContentsOfFile:iconPath];
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)
+19
View File
@@ -0,0 +1,19 @@
//
// SUBundleIcon.h
// Sparkle
//
// Created by Mayur Pawashe on 7/24/16.
// Copyright © 2016 Sparkle Project. All rights reserved.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface SUBundleIcon : NSObject
+ (NSURL * _Nullable)iconURLForBundle:(NSBundle *)bundle;
@end
NS_ASSUME_NONNULL_END
+33
View File
@@ -0,0 +1,33 @@
//
// SUBundleIcon.m
// Sparkle
//
// Created by Mayur Pawashe on 7/24/16.
// Copyright © 2016 Sparkle Project. All rights reserved.
//
#import "SUBundleIcon.h"
#ifdef _APPKITDEFINES_H
#error This is a "core" class and should NOT import AppKit
#endif
@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