@native-sdk/cli 0.9.5 → 0.10.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/app.zig +7 -0
- package/build/ts_run.mjs +1 -1
- package/build.zig +30 -5
- package/package.json +10 -10
- package/packages/core/package-lock.json +12 -11
- package/packages/core/package.json +2 -2
- package/packages/core/scripts/compiler_command.mjs +1 -1
- package/schemas/app.schema.json +12 -1
- package/skill-data/core/references/web-engines-packaging-debugging.md +2 -1
- package/skill-data/ts-services/references/service-surface.md +1 -1
- package/src/app_runner/root.zig +25 -0
- package/src/platform/macos/appkit_host.h +25 -0
- package/src/platform/macos/appkit_host.m +537 -15
- package/src/platform/macos/cef_host.mm +17 -0
- package/src/platform/macos/root.zig +95 -0
- package/src/platform/root.zig +3 -0
- package/src/platform/types.zig +15 -0
- package/src/primitives/app_manifest/tests.zig +19 -0
- package/src/primitives/app_manifest/validation.zig +14 -2
- package/src/primitives/canvas/font_ttf.zig +24 -0
- package/src/primitives/canvas/gpu.zig +5 -0
- package/src/primitives/canvas/reference.zig +4 -1
- package/src/primitives/canvas/render_tests.zig +1 -1
- package/src/primitives/canvas/root.zig +1 -0
- package/src/primitives/canvas/serialization.zig +1 -0
- package/src/primitives/canvas/test_support.zig +1 -0
- package/src/primitives/canvas/text.zig +1 -0
- package/src/primitives/canvas/text_layout.zig +84 -15
- package/src/primitives/canvas/text_layout_hash.zig +3 -1
- package/src/primitives/canvas/text_layout_types.zig +3 -0
- package/src/primitives/canvas/text_metrics.zig +28 -1
- package/src/primitives/canvas/text_tests.zig +61 -6
- package/src/primitives/canvas/widget_render.zig +6 -0
- package/src/primitives/canvas/widget_render_controls.zig +1 -0
- package/src/root.zig +2 -0
- package/src/runtime/canvas_fonts.zig +87 -0
- package/src/runtime/core.zig +1 -0
- package/src/runtime/flow.zig +7 -0
- package/src/runtime/native_command_event_tests.zig +58 -0
- package/src/tooling/codesign.zig +125 -74
- package/src/tooling/manifest.zig +79 -2
- package/src/tooling/package.zig +118 -1
- package/src/tooling/raw_manifest.zig +7 -0
- package/src/tooling/root.zig +1 -0
- package/src/tooling/templates.zig +45 -0
- package/src/tooling/update.zig +576 -0
- package/src/updater/c_api.zig +123 -0
- package/src/updater/feed.zig +215 -0
- package/src/updater/root.zig +10 -0
|
@@ -25,6 +25,7 @@
|
|
|
25
25
|
#include <stdlib.h>
|
|
26
26
|
#include <string.h>
|
|
27
27
|
#include <time.h>
|
|
28
|
+
#include <unistd.h>
|
|
28
29
|
|
|
29
30
|
#include "../ios/apple_image_fit.h"
|
|
30
31
|
|
|
@@ -158,6 +159,17 @@ static NSString *NativeSdkStringFromTextInput(id value) {
|
|
|
158
159
|
return [value description] ?: @"";
|
|
159
160
|
}
|
|
160
161
|
|
|
162
|
+
/* The authored runner identity is the right fallback for unbundled dev
|
|
163
|
+
* executables. Once the process is inside a packaged .app, Info.plist is the
|
|
164
|
+
* installed identity and must win: Finder/package tooling can legitimately
|
|
165
|
+
* produce a bundle whose identifier differs from stale generated runner
|
|
166
|
+
* source, and update feeds are bound to the bundle actually being replaced. */
|
|
167
|
+
static NSString *NativeSdkPackagedBundleIdentifier(void) {
|
|
168
|
+
NSBundle *bundle = NSBundle.mainBundle;
|
|
169
|
+
if (![[bundle.bundlePath pathExtension].lowercaseString isEqualToString:@"app"]) return nil;
|
|
170
|
+
return bundle.bundleIdentifier.length > 0 ? bundle.bundleIdentifier : nil;
|
|
171
|
+
}
|
|
172
|
+
|
|
161
173
|
static int NativeSdkAppKitColorSchemeForAppearance(NSAppearance *appearance) {
|
|
162
174
|
NSAppearance *effective = appearance ?: NSApp.effectiveAppearance;
|
|
163
175
|
NSString *bestMatch = [effective bestMatchFromAppearancesWithNames:@[NSAppearanceNameAqua, NSAppearanceNameDarkAqua]];
|
|
@@ -1005,6 +1017,19 @@ static int NativeSdkCredentialStatus(OSStatus status, int missingCode) {
|
|
|
1005
1017
|
@property(nonatomic, strong) NSImage *appIcon;
|
|
1006
1018
|
@property(nonatomic, strong) NSString *bundleIdentifier;
|
|
1007
1019
|
@property(nonatomic, strong) NSString *iconPath;
|
|
1020
|
+
@property(nonatomic, strong) NSString *updateFeedURL;
|
|
1021
|
+
@property(nonatomic, strong) NSString *updatePublicKey;
|
|
1022
|
+
@property(nonatomic, strong) NSString *updateTarget;
|
|
1023
|
+
@property(nonatomic, assign) BOOL updateCheckOnStart;
|
|
1024
|
+
@property(nonatomic, assign) BOOL updateCheckRunning;
|
|
1025
|
+
@property(nonatomic, strong) NSURLSessionDownloadTask *updateFeedTask;
|
|
1026
|
+
@property(nonatomic, strong) NSURLSessionDownloadTask *updateDownloadTask;
|
|
1027
|
+
@property(nonatomic, strong) NSTimer *updateFeedLimitTimer;
|
|
1028
|
+
@property(nonatomic, strong) NSTimer *updateDownloadLimitTimer;
|
|
1029
|
+
@property(nonatomic, assign) uint64_t updateExpectedArchiveBytes;
|
|
1030
|
+
@property(nonatomic, assign) BOOL updateDownloadCancelledByUser;
|
|
1031
|
+
@property(nonatomic, assign) BOOL updateDownloadAlertIsAppModal;
|
|
1032
|
+
@property(nonatomic, strong) NSAlert *updateDownloadAlert;
|
|
1008
1033
|
@property(nonatomic, strong) NSString *windowLabel;
|
|
1009
1034
|
@property(nonatomic, assign) native_sdk_appkit_event_callback_t callback;
|
|
1010
1035
|
@property(nonatomic, assign) native_sdk_appkit_bridge_callback_t bridgeCallback;
|
|
@@ -1093,6 +1118,15 @@ static int NativeSdkCredentialStatus(OSStatus status, int missingCode) {
|
|
|
1093
1118
|
- (void)configureApplication;
|
|
1094
1119
|
- (void)buildMenuBar;
|
|
1095
1120
|
- (void)addApplicationMenuToMenu:(NSMenu *)mainMenu;
|
|
1121
|
+
- (void)checkForUpdatesMenuItem:(id)sender;
|
|
1122
|
+
- (void)checkForUpdatesUserInitiated:(BOOL)userInitiated;
|
|
1123
|
+
- (NSWindow *)updatePresentationWindow;
|
|
1124
|
+
- (void)presentUpdateAlert:(NSAlert *)alert completionHandler:(void (^)(NSModalResponse responseCode))completionHandler;
|
|
1125
|
+
- (void)downloadUpdateVersion:(NSString *)version archiveURL:(NSString *)archiveURL archiveBytes:(uint64_t)archiveBytes sha256:(NSString *)sha256 releaseNotes:(NSString *)releaseNotes;
|
|
1126
|
+
- (void)installDownloadedUpdate:(NSURL *)downloadURL version:(NSString *)version archiveBytes:(uint64_t)archiveBytes sha256:(NSString *)sha256;
|
|
1127
|
+
- (void)showUpdateError:(NSString *)message userInitiated:(BOOL)userInitiated;
|
|
1128
|
+
- (void)updateFeedLimitTimerFired:(NSTimer *)timer;
|
|
1129
|
+
- (void)updateDownloadLimitTimerFired:(NSTimer *)timer;
|
|
1096
1130
|
- (NSMenuItem *)menuItem:(NSString *)title action:(SEL)action key:(NSString *)key modifiers:(NSEventModifierFlags)modifiers;
|
|
1097
1131
|
- (NSMenuItem *)commandMenuItem:(NSString *)title command:(NSString *)command key:(NSString *)key modifiers:(uint32_t)modifiers enabled:(BOOL)enabled checked:(BOOL)checked;
|
|
1098
1132
|
- (void)menuCommandItemClicked:(NSMenuItem *)menuItem;
|
|
@@ -2045,6 +2079,34 @@ static NSTextAlignment NativeSdkPacketTextAlignment(NSString *align) {
|
|
|
2045
2079
|
return NSTextAlignmentNatural;
|
|
2046
2080
|
}
|
|
2047
2081
|
|
|
2082
|
+
static BOOL NativeSdkPacketDrawAttributedText(
|
|
2083
|
+
NSString *value,
|
|
2084
|
+
NSDictionary *attributes,
|
|
2085
|
+
CGFloat x,
|
|
2086
|
+
CGFloat baseline,
|
|
2087
|
+
CGFloat width,
|
|
2088
|
+
CGFloat height
|
|
2089
|
+
) {
|
|
2090
|
+
if (value.length == 0) return YES;
|
|
2091
|
+
|
|
2092
|
+
NSTextStorage *storage = [[NSTextStorage alloc] initWithString:value attributes:attributes];
|
|
2093
|
+
NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
|
|
2094
|
+
NSTextContainer *container = [[NSTextContainer alloc] initWithContainerSize:NSMakeSize(
|
|
2095
|
+
width > 0 ? width : CGFLOAT_MAX,
|
|
2096
|
+
height > 0 ? height : CGFLOAT_MAX
|
|
2097
|
+
)];
|
|
2098
|
+
container.lineFragmentPadding = 0;
|
|
2099
|
+
[layoutManager addTextContainer:container];
|
|
2100
|
+
[storage addLayoutManager:layoutManager];
|
|
2101
|
+
[layoutManager ensureLayoutForTextContainer:container];
|
|
2102
|
+
|
|
2103
|
+
NSRange glyphRange = [layoutManager glyphRangeForTextContainer:container];
|
|
2104
|
+
if (glyphRange.length == 0) return YES;
|
|
2105
|
+
CGFloat firstLineOffset = [layoutManager locationForGlyphAtIndex:glyphRange.location].y;
|
|
2106
|
+
[layoutManager drawGlyphsForGlyphRange:glyphRange atPoint:NSMakePoint(x, baseline - firstLineOffset)];
|
|
2107
|
+
return YES;
|
|
2108
|
+
}
|
|
2109
|
+
|
|
2048
2110
|
// Italicizes a resolved sans face for the reserved italic span font ids
|
|
2049
2111
|
// (5 and 6). Prefers a real italic face from the same family via
|
|
2050
2112
|
// NSFontManager (SF has one; Geist does not ship a sans italic), and falls
|
|
@@ -2185,6 +2247,16 @@ static NSCache<NSString *, NSNumber *> *NativeSdkMeasuredWidthCache(void) {
|
|
|
2185
2247
|
return cache;
|
|
2186
2248
|
}
|
|
2187
2249
|
|
|
2250
|
+
static NSCache<NSString *, NSValue *> *NativeSdkMeasuredInkCache(void) {
|
|
2251
|
+
static NSCache<NSString *, NSValue *> *cache = nil;
|
|
2252
|
+
static dispatch_once_t onceToken;
|
|
2253
|
+
dispatch_once(&onceToken, ^{
|
|
2254
|
+
cache = [[NSCache alloc] init];
|
|
2255
|
+
cache.countLimit = 16384;
|
|
2256
|
+
});
|
|
2257
|
+
return cache;
|
|
2258
|
+
}
|
|
2259
|
+
|
|
2188
2260
|
// The id's current registration token AND the registered face that
|
|
2189
2261
|
// token resolves to, snapshotted under ONE acquisition of the
|
|
2190
2262
|
// descriptor table's @synchronized guard. The pair must be atomic
|
|
@@ -2269,6 +2341,7 @@ int native_sdk_appkit_register_font(uint64_t font_id, const uint8_t *bytes, size
|
|
|
2269
2341
|
for (NSString *cachedKey in cachedKeys) {
|
|
2270
2342
|
if ([cachedKey hasPrefix:stalePrefix]) [sizeCache removeObjectForKey:cachedKey];
|
|
2271
2343
|
}
|
|
2344
|
+
[NativeSdkMeasuredInkCache() removeAllObjects];
|
|
2272
2345
|
// The measured-width cache holds the same stale state but
|
|
2273
2346
|
// cannot be enumerated for eviction, so stamp the id with a
|
|
2274
2347
|
// fresh process-global token instead: width-cache keys
|
|
@@ -2346,6 +2419,7 @@ int native_sdk_appkit_unregister_font(uint64_t font_id, uint64_t token) {
|
|
|
2346
2419
|
// id's token under this same guard before caching, and this
|
|
2347
2420
|
// removal already retired the token it snapshotted.
|
|
2348
2421
|
[NativeSdkMeasuredWidthCache() removeAllObjects];
|
|
2422
|
+
[NativeSdkMeasuredInkCache() removeAllObjects];
|
|
2349
2423
|
}
|
|
2350
2424
|
return 1;
|
|
2351
2425
|
}
|
|
@@ -2492,6 +2566,47 @@ double native_sdk_appkit_measure_text(uint64_t font_id, double size, const char
|
|
|
2492
2566
|
}
|
|
2493
2567
|
}
|
|
2494
2568
|
|
|
2569
|
+
int native_sdk_appkit_measure_text_ink(uint64_t font_id, double size, const char *text, size_t text_len, double *min_x, double *max_x, double *min_y, double *max_y) {
|
|
2570
|
+
if (!text || text_len == 0 || !min_x || !max_x || !min_y || !max_y) return 0;
|
|
2571
|
+
CGFloat clamped = MAX(1, size);
|
|
2572
|
+
@autoreleasepool {
|
|
2573
|
+
NSString *value = [[NSString alloc] initWithBytes:text length:text_len encoding:NSUTF8StringEncoding];
|
|
2574
|
+
if (!value) return 0;
|
|
2575
|
+
|
|
2576
|
+
unsigned long long token = 0;
|
|
2577
|
+
NSFont *registered = NativeSdkRegisteredFontSnapshot((unsigned long long)font_id, clamped, &token);
|
|
2578
|
+
NSFont *font = registered ?: NativeSdkBuiltInFontForFontId(font_id, clamped);
|
|
2579
|
+
if (!font) return 0;
|
|
2580
|
+
|
|
2581
|
+
NSString *key = [NSString stringWithFormat:@"%llu/%llu/%.3f/%@", (unsigned long long)font_id, token, (double)clamped, value];
|
|
2582
|
+
NSValue *cached = [NativeSdkMeasuredInkCache() objectForKey:key];
|
|
2583
|
+
if (cached) {
|
|
2584
|
+
CGRect bounds = cached.rectValue;
|
|
2585
|
+
*min_x = CGRectGetMinX(bounds);
|
|
2586
|
+
*max_x = CGRectGetMaxX(bounds);
|
|
2587
|
+
*min_y = CGRectGetMinY(bounds);
|
|
2588
|
+
*max_y = CGRectGetMaxY(bounds);
|
|
2589
|
+
return 1;
|
|
2590
|
+
}
|
|
2591
|
+
|
|
2592
|
+
NSAttributedString *attributed = [[NSAttributedString alloc] initWithString:value
|
|
2593
|
+
attributes:@{ NSFontAttributeName : font }];
|
|
2594
|
+
CTLineRef line = CTLineCreateWithAttributedString((__bridge CFAttributedStringRef)attributed);
|
|
2595
|
+
if (!line) return 0;
|
|
2596
|
+
CGRect bounds = CTLineGetBoundsWithOptions(line, kCTLineBoundsUseGlyphPathBounds);
|
|
2597
|
+
CFRelease(line);
|
|
2598
|
+
if (!isfinite(CGRectGetMinX(bounds)) || !isfinite(CGRectGetMaxX(bounds)) ||
|
|
2599
|
+
!isfinite(CGRectGetMinY(bounds)) || !isfinite(CGRectGetMaxY(bounds))) return 0;
|
|
2600
|
+
|
|
2601
|
+
[NativeSdkMeasuredInkCache() setObject:[NSValue valueWithRect:bounds] forKey:key];
|
|
2602
|
+
*min_x = CGRectGetMinX(bounds);
|
|
2603
|
+
*max_x = CGRectGetMaxX(bounds);
|
|
2604
|
+
*min_y = CGRectGetMinY(bounds);
|
|
2605
|
+
*max_y = CGRectGetMaxY(bounds);
|
|
2606
|
+
return 1;
|
|
2607
|
+
}
|
|
2608
|
+
}
|
|
2609
|
+
|
|
2495
2610
|
// Batched per-cluster advances for a single-line run: one CTLine over
|
|
2496
2611
|
// the whole run (the same attributed-string shaping the width above
|
|
2497
2612
|
// summarizes), glyph advances accumulated onto the UTF-16 character
|
|
@@ -2704,8 +2819,7 @@ static BOOL NativeSdkPacketDrawText(NSDictionary *text, CGFloat opacity) {
|
|
|
2704
2819
|
};
|
|
2705
2820
|
NSDictionary *layout = NativeSdkPacketDictionary(text[@"layout"]);
|
|
2706
2821
|
if (!layout) {
|
|
2707
|
-
|
|
2708
|
-
return YES;
|
|
2822
|
+
return NativeSdkPacketDrawAttributedText(value, baseAttributes, origin.x, origin.y, CGFLOAT_MAX, CGFLOAT_MAX);
|
|
2709
2823
|
}
|
|
2710
2824
|
|
|
2711
2825
|
// Engine-measured line breaks: the packet carries the exact lines the
|
|
@@ -2724,7 +2838,7 @@ static BOOL NativeSdkPacketDrawText(NSDictionary *text, CGFloat opacity) {
|
|
|
2724
2838
|
if (lineText.length == 0) continue;
|
|
2725
2839
|
CGFloat lineX = NativeSdkPacketNumber(line[@"x"], origin.x);
|
|
2726
2840
|
CGFloat baseline = NativeSdkPacketNumber(line[@"baseline"], origin.y);
|
|
2727
|
-
|
|
2841
|
+
if (!NativeSdkPacketDrawAttributedText(lineText, baseAttributes, lineX, baseline, CGFLOAT_MAX, CGFLOAT_MAX)) return NO;
|
|
2728
2842
|
}
|
|
2729
2843
|
return YES;
|
|
2730
2844
|
}
|
|
@@ -2743,17 +2857,8 @@ static BOOL NativeSdkPacketDrawText(NSDictionary *text, CGFloat opacity) {
|
|
|
2743
2857
|
NSMutableDictionary *attributes = [baseAttributes mutableCopy];
|
|
2744
2858
|
attributes[NSParagraphStyleAttributeName] = paragraph;
|
|
2745
2859
|
CGFloat maxWidth = NativeSdkPacketNumber(layout[@"maxWidth"], 0);
|
|
2746
|
-
CGFloat
|
|
2747
|
-
|
|
2748
|
-
CGFloat textHeight = MAX(lineHeight > 0 ? lineHeight : size * 1.25, size * 1.25);
|
|
2749
|
-
NSRect measuredRect = [value boundingRectWithSize:NSMakeSize(textWidth, CGFLOAT_MAX)
|
|
2750
|
-
options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
|
|
2751
|
-
attributes:attributes];
|
|
2752
|
-
textHeight = MAX(textHeight, ceil(measuredRect.size.height + 1));
|
|
2753
|
-
[value drawWithRect:NSMakeRect(origin.x, origin.y - size, textWidth, textHeight)
|
|
2754
|
-
options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
|
|
2755
|
-
attributes:attributes];
|
|
2756
|
-
return YES;
|
|
2860
|
+
CGFloat textWidth = maxWidth > 0 ? maxWidth : CGFLOAT_MAX;
|
|
2861
|
+
return NativeSdkPacketDrawAttributedText(value, attributes, origin.x, origin.y, textWidth, CGFLOAT_MAX);
|
|
2757
2862
|
}
|
|
2758
2863
|
|
|
2759
2864
|
static BOOL NativeSdkPacketDrawEffect(NSDictionary *effect, CGFloat opacity, CGContextRef context, CGFloat scale, id transformValue, BOOL hasClip, NSRect clipRect) {
|
|
@@ -7840,9 +7945,17 @@ static float NativeSdkCaptureReadRemixedSample(const AudioBufferList *buffers, c
|
|
|
7840
7945
|
self.appVersion = version ?: @"";
|
|
7841
7946
|
self.aboutDescription = aboutDescription ?: @"";
|
|
7842
7947
|
self.hasWebContent = hasWebContent;
|
|
7843
|
-
|
|
7948
|
+
NSString *configuredBundleIdentifier = bundleIdentifier.length > 0 ? bundleIdentifier : @"dev.native_sdk.app";
|
|
7949
|
+
self.bundleIdentifier = NativeSdkPackagedBundleIdentifier() ?: configuredBundleIdentifier;
|
|
7844
7950
|
self.iconPath = iconPath ?: @"";
|
|
7845
7951
|
self.windowLabel = windowLabel.length > 0 ? windowLabel : @"main";
|
|
7952
|
+
self.updateFeedURL = @"";
|
|
7953
|
+
self.updatePublicKey = @"";
|
|
7954
|
+
self.updateTarget = @"";
|
|
7955
|
+
self.updateCheckOnStart = NO;
|
|
7956
|
+
self.updateCheckRunning = NO;
|
|
7957
|
+
self.updateDownloadCancelledByUser = NO;
|
|
7958
|
+
self.updateDownloadAlertIsAppModal = NO;
|
|
7846
7959
|
self.windows = [[NSMutableDictionary alloc] init];
|
|
7847
7960
|
self.webViews = [[NSMutableDictionary alloc] init];
|
|
7848
7961
|
self.delegates = [[NSMutableDictionary alloc] init];
|
|
@@ -8131,6 +8244,10 @@ static float NativeSdkCaptureReadRemixedSample(const AudioBufferList *buffers, c
|
|
|
8131
8244
|
NSUserNotificationCenter *notificationCenter = [NSUserNotificationCenter defaultUserNotificationCenter];
|
|
8132
8245
|
if (notificationCenter.delegate == self) notificationCenter.delegate = nil;
|
|
8133
8246
|
[self invalidateAppTimers];
|
|
8247
|
+
[self.updateFeedLimitTimer invalidate];
|
|
8248
|
+
[self.updateDownloadLimitTimer invalidate];
|
|
8249
|
+
[self.updateFeedTask cancel];
|
|
8250
|
+
[self.updateDownloadTask cancel];
|
|
8134
8251
|
[self audioCaptureStopSource:0];
|
|
8135
8252
|
[self audioCaptureStopSource:1];
|
|
8136
8253
|
[self audioStop];
|
|
@@ -9798,6 +9915,9 @@ static void NativeSdkApplyProcessDisplayName(NSString *displayName) {
|
|
|
9798
9915
|
NSMenu *appMenu = [[NSMenu alloc] initWithTitle:self.displayName];
|
|
9799
9916
|
[appMenuItem setSubmenu:appMenu];
|
|
9800
9917
|
[appMenu addItem:[self menuItem:[NSString stringWithFormat:@"About %@", self.displayName] action:@selector(showAboutPanel:) key:@"" modifiers:0]];
|
|
9918
|
+
if (self.updateFeedURL.length > 0 && self.updatePublicKey.length > 0) {
|
|
9919
|
+
[appMenu addItem:[self menuItem:@"Check for Updates…" action:@selector(checkForUpdatesMenuItem:) key:@"" modifiers:0]];
|
|
9920
|
+
}
|
|
9801
9921
|
[appMenu addItem:[NSMenuItem separatorItem]];
|
|
9802
9922
|
[appMenu addItem:[self menuItem:[NSString stringWithFormat:@"Hide %@", self.displayName] action:@selector(hide:) key:@"h" modifiers:NSEventModifierFlagCommand]];
|
|
9803
9923
|
[appMenu addItem:[self menuItem:@"Hide Others" action:@selector(hideOtherApplications:) key:@"h" modifiers:(NSEventModifierFlagCommand | NSEventModifierFlagOption)]];
|
|
@@ -9806,6 +9926,381 @@ static void NativeSdkApplyProcessDisplayName(NSString *displayName) {
|
|
|
9806
9926
|
[appMenu addItem:[self menuItem:[NSString stringWithFormat:@"Quit %@", self.displayName] action:@selector(terminate:) key:@"q" modifiers:NSEventModifierFlagCommand]];
|
|
9807
9927
|
}
|
|
9808
9928
|
|
|
9929
|
+
- (void)checkForUpdatesMenuItem:(id)sender {
|
|
9930
|
+
(void)sender;
|
|
9931
|
+
[self checkForUpdatesUserInitiated:YES];
|
|
9932
|
+
}
|
|
9933
|
+
|
|
9934
|
+
- (NSWindow *)updatePresentationWindow {
|
|
9935
|
+
NSWindow *keyWindow = NSApp.keyWindow;
|
|
9936
|
+
if (keyWindow.isVisible && !keyWindow.miniaturized) return keyWindow;
|
|
9937
|
+
for (NSWindow *window in NSApp.orderedWindows) {
|
|
9938
|
+
if (window.isVisible && !window.miniaturized) return window;
|
|
9939
|
+
}
|
|
9940
|
+
for (NSWindow *window in self.windows.allValues) {
|
|
9941
|
+
if (window.isVisible && !window.miniaturized) return window;
|
|
9942
|
+
}
|
|
9943
|
+
return nil;
|
|
9944
|
+
}
|
|
9945
|
+
|
|
9946
|
+
- (void)presentUpdateAlert:(NSAlert *)alert completionHandler:(void (^)(NSModalResponse responseCode))completionHandler {
|
|
9947
|
+
[NSApp activateIgnoringOtherApps:YES];
|
|
9948
|
+
NSWindow *window = [self updatePresentationWindow];
|
|
9949
|
+
if (window) {
|
|
9950
|
+
[alert beginSheetModalForWindow:window completionHandler:completionHandler];
|
|
9951
|
+
return;
|
|
9952
|
+
}
|
|
9953
|
+
const NSModalResponse response = [alert runModal];
|
|
9954
|
+
if (completionHandler) completionHandler(response);
|
|
9955
|
+
}
|
|
9956
|
+
|
|
9957
|
+
- (void)showUpdateError:(NSString *)message userInitiated:(BOOL)userInitiated {
|
|
9958
|
+
self.updateCheckRunning = NO;
|
|
9959
|
+
if (!userInitiated) {
|
|
9960
|
+
NSLog(@"Native SDK update check failed: %@", message ?: @"unknown error");
|
|
9961
|
+
return;
|
|
9962
|
+
}
|
|
9963
|
+
NSAlert *alert = [[NSAlert alloc] init];
|
|
9964
|
+
alert.alertStyle = NSAlertStyleWarning;
|
|
9965
|
+
alert.messageText = @"Unable to Check for Updates";
|
|
9966
|
+
alert.informativeText = message ?: @"The update could not be checked.";
|
|
9967
|
+
[alert addButtonWithTitle:@"OK"];
|
|
9968
|
+
[self presentUpdateAlert:alert completionHandler:nil];
|
|
9969
|
+
}
|
|
9970
|
+
|
|
9971
|
+
- (void)checkForUpdatesUserInitiated:(BOOL)userInitiated {
|
|
9972
|
+
if (self.updateFeedURL.length == 0 || self.updatePublicKey.length == 0 || self.updateCheckRunning) return;
|
|
9973
|
+
NSString *bundlePath = NSBundle.mainBundle.bundlePath;
|
|
9974
|
+
if (![[bundlePath pathExtension].lowercaseString isEqualToString:@"app"]) {
|
|
9975
|
+
[self showUpdateError:@"Update checks run only from a packaged .app bundle." userInitiated:userInitiated];
|
|
9976
|
+
return;
|
|
9977
|
+
}
|
|
9978
|
+
NSURL *feedURL = [NSURL URLWithString:self.updateFeedURL];
|
|
9979
|
+
if (!feedURL || ![feedURL.scheme.lowercaseString isEqualToString:@"https"]) {
|
|
9980
|
+
[self showUpdateError:@"The update feed URL is invalid." userInitiated:userInitiated];
|
|
9981
|
+
return;
|
|
9982
|
+
}
|
|
9983
|
+
self.updateCheckRunning = YES;
|
|
9984
|
+
__weak NativeSdkAppKitHost *weakSelf = self;
|
|
9985
|
+
NSMutableURLRequest *feedRequest = [NSMutableURLRequest requestWithURL:feedURL];
|
|
9986
|
+
[feedRequest setValue:@"bytes=0-65536" forHTTPHeaderField:@"Range"];
|
|
9987
|
+
self.updateFeedTask = [[NSURLSession sharedSession] downloadTaskWithRequest:feedRequest completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
|
|
9988
|
+
NativeSdkAppKitHost *strongSelf = weakSelf;
|
|
9989
|
+
if (!strongSelf) return;
|
|
9990
|
+
NSDictionary *attributes = location ? [[NSFileManager defaultManager] attributesOfItemAtPath:location.path error:nil] : nil;
|
|
9991
|
+
unsigned long long feedBytes = attributes.fileSize;
|
|
9992
|
+
NSData *data = feedBytes > 0 && feedBytes <= 64 * 1024 ? [NSData dataWithContentsOfURL:location options:NSDataReadingMappedIfSafe error:nil] : nil;
|
|
9993
|
+
if (error || data.length == 0 || ![response.URL.scheme.lowercaseString isEqualToString:@"https"] || ![response isKindOfClass:[NSHTTPURLResponse class]] || ((NSHTTPURLResponse *)response).statusCode < 200 || ((NSHTTPURLResponse *)response).statusCode >= 300) {
|
|
9994
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
9995
|
+
[strongSelf.updateFeedLimitTimer invalidate];
|
|
9996
|
+
strongSelf.updateFeedLimitTimer = nil;
|
|
9997
|
+
strongSelf.updateCheckRunning = NO;
|
|
9998
|
+
strongSelf.updateFeedTask = nil;
|
|
9999
|
+
[strongSelf showUpdateError:error.localizedDescription ?: @"The update feed could not be downloaded." userInitiated:userInitiated];
|
|
10000
|
+
});
|
|
10001
|
+
return;
|
|
10002
|
+
}
|
|
10003
|
+
NSMutableData *versionData = [NSMutableData dataWithLength:64];
|
|
10004
|
+
NSMutableData *archiveURLData = [NSMutableData dataWithLength:4096];
|
|
10005
|
+
NSMutableData *releaseNotesData = [NSMutableData dataWithLength:16 * 1024];
|
|
10006
|
+
native_sdk_update_verify_result_t verified = native_sdk_update_verify_feed(
|
|
10007
|
+
data.bytes, data.length,
|
|
10008
|
+
strongSelf.updatePublicKey.UTF8String, [strongSelf.updatePublicKey lengthOfBytesUsingEncoding:NSUTF8StringEncoding],
|
|
10009
|
+
strongSelf.bundleIdentifier.UTF8String, [strongSelf.bundleIdentifier lengthOfBytesUsingEncoding:NSUTF8StringEncoding],
|
|
10010
|
+
strongSelf.appVersion.UTF8String, [strongSelf.appVersion lengthOfBytesUsingEncoding:NSUTF8StringEncoding],
|
|
10011
|
+
strongSelf.updateTarget.UTF8String, [strongSelf.updateTarget lengthOfBytesUsingEncoding:NSUTF8StringEncoding],
|
|
10012
|
+
versionData.mutableBytes, versionData.length,
|
|
10013
|
+
archiveURLData.mutableBytes, archiveURLData.length,
|
|
10014
|
+
releaseNotesData.mutableBytes, releaseNotesData.length);
|
|
10015
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
10016
|
+
[strongSelf.updateFeedLimitTimer invalidate];
|
|
10017
|
+
strongSelf.updateFeedLimitTimer = nil;
|
|
10018
|
+
strongSelf.updateFeedTask = nil;
|
|
10019
|
+
if (verified.ok == 2) {
|
|
10020
|
+
strongSelf.updateCheckRunning = NO;
|
|
10021
|
+
if (userInitiated) {
|
|
10022
|
+
NSAlert *alert = [[NSAlert alloc] init];
|
|
10023
|
+
alert.messageText = [NSString stringWithFormat:@"%@ is up to date", strongSelf.displayName];
|
|
10024
|
+
alert.informativeText = [NSString stringWithFormat:@"You’re running version %@.", strongSelf.appVersion];
|
|
10025
|
+
[alert addButtonWithTitle:@"OK"];
|
|
10026
|
+
[strongSelf presentUpdateAlert:alert completionHandler:nil];
|
|
10027
|
+
}
|
|
10028
|
+
return;
|
|
10029
|
+
}
|
|
10030
|
+
if (verified.ok != 1) {
|
|
10031
|
+
NSString *message = verified.error_code == 3 ? @"The update feed signature is invalid."
|
|
10032
|
+
: verified.error_code == 5 ? @"The update feed belongs to another app."
|
|
10033
|
+
: verified.error_code == 6 ? @"The update feed does not contain a release for this Mac."
|
|
10034
|
+
: @"The update feed is malformed or cannot be verified.";
|
|
10035
|
+
[strongSelf showUpdateError:message userInitiated:userInitiated];
|
|
10036
|
+
return;
|
|
10037
|
+
}
|
|
10038
|
+
NSString *version = [[NSString alloc] initWithBytes:versionData.bytes length:verified.version_len encoding:NSUTF8StringEncoding];
|
|
10039
|
+
NSString *archiveURL = [[NSString alloc] initWithBytes:archiveURLData.bytes length:verified.archive_url_len encoding:NSUTF8StringEncoding];
|
|
10040
|
+
NSString *releaseNotes = [[NSString alloc] initWithBytes:releaseNotesData.bytes length:verified.release_notes_len encoding:NSUTF8StringEncoding] ?: @"";
|
|
10041
|
+
NSString *sha256 = [[NSString alloc] initWithBytes:verified.sha256 length:sizeof(verified.sha256) encoding:NSASCIIStringEncoding];
|
|
10042
|
+
if (!version || !archiveURL || !sha256) {
|
|
10043
|
+
[strongSelf showUpdateError:@"The verified update metadata could not be decoded." userInitiated:userInitiated];
|
|
10044
|
+
return;
|
|
10045
|
+
}
|
|
10046
|
+
NSAlert *alert = [[NSAlert alloc] init];
|
|
10047
|
+
alert.messageText = [NSString stringWithFormat:@"A new version of %@ is available", strongSelf.displayName];
|
|
10048
|
+
alert.informativeText = releaseNotes.length > 0
|
|
10049
|
+
? [NSString stringWithFormat:@"Version %@\n\n%@", version, releaseNotes]
|
|
10050
|
+
: [NSString stringWithFormat:@"Version %@ is ready to install.", version];
|
|
10051
|
+
[alert addButtonWithTitle:@"Install Update"];
|
|
10052
|
+
[alert addButtonWithTitle:@"Later"];
|
|
10053
|
+
[strongSelf presentUpdateAlert:alert completionHandler:^(NSModalResponse responseCode) {
|
|
10054
|
+
if (responseCode == NSAlertFirstButtonReturn) {
|
|
10055
|
+
[strongSelf downloadUpdateVersion:version archiveURL:archiveURL archiveBytes:verified.archive_bytes sha256:sha256 releaseNotes:releaseNotes];
|
|
10056
|
+
} else {
|
|
10057
|
+
strongSelf.updateCheckRunning = NO;
|
|
10058
|
+
}
|
|
10059
|
+
}];
|
|
10060
|
+
});
|
|
10061
|
+
}];
|
|
10062
|
+
[self.updateFeedTask resume];
|
|
10063
|
+
self.updateFeedLimitTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateFeedLimitTimerFired:) userInfo:nil repeats:YES];
|
|
10064
|
+
}
|
|
10065
|
+
|
|
10066
|
+
- (void)updateFeedLimitTimerFired:(NSTimer *)timer {
|
|
10067
|
+
(void)timer;
|
|
10068
|
+
if (!self.updateFeedTask) {
|
|
10069
|
+
[self.updateFeedLimitTimer invalidate];
|
|
10070
|
+
self.updateFeedLimitTimer = nil;
|
|
10071
|
+
return;
|
|
10072
|
+
}
|
|
10073
|
+
if (self.updateFeedTask.countOfBytesReceived > 64 * 1024) [self.updateFeedTask cancel];
|
|
10074
|
+
}
|
|
10075
|
+
|
|
10076
|
+
- (void)downloadUpdateVersion:(NSString *)version archiveURL:(NSString *)archiveURL archiveBytes:(uint64_t)archiveBytes sha256:(NSString *)sha256 releaseNotes:(NSString *)releaseNotes {
|
|
10077
|
+
(void)releaseNotes;
|
|
10078
|
+
NSURL *url = [NSURL URLWithString:archiveURL];
|
|
10079
|
+
if (!url || ![url.scheme.lowercaseString isEqualToString:@"https"]) {
|
|
10080
|
+
[self showUpdateError:@"The update archive URL is invalid." userInitiated:YES];
|
|
10081
|
+
return;
|
|
10082
|
+
}
|
|
10083
|
+
NSAlert *progress = [[NSAlert alloc] init];
|
|
10084
|
+
progress.messageText = [NSString stringWithFormat:@"Downloading %@ %@…", self.displayName, version];
|
|
10085
|
+
progress.informativeText = @"The update will be verified before it is installed.";
|
|
10086
|
+
NSProgressIndicator *indicator = [[NSProgressIndicator alloc] initWithFrame:NSMakeRect(0, 0, 320, 20)];
|
|
10087
|
+
indicator.indeterminate = YES;
|
|
10088
|
+
indicator.style = NSProgressIndicatorStyleBar;
|
|
10089
|
+
[indicator startAnimation:nil];
|
|
10090
|
+
progress.accessoryView = indicator;
|
|
10091
|
+
[progress addButtonWithTitle:@"Cancel"];
|
|
10092
|
+
self.updateDownloadAlert = progress;
|
|
10093
|
+
self.updateExpectedArchiveBytes = archiveBytes;
|
|
10094
|
+
self.updateDownloadCancelledByUser = NO;
|
|
10095
|
+
self.updateDownloadAlertIsAppModal = NO;
|
|
10096
|
+
__weak NativeSdkAppKitHost *weakSelf = self;
|
|
10097
|
+
self.updateDownloadTask = [[NSURLSession sharedSession] downloadTaskWithURL:url completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
|
|
10098
|
+
NativeSdkAppKitHost *strongSelf = weakSelf;
|
|
10099
|
+
if (!strongSelf) return;
|
|
10100
|
+
NSURL *copied = nil;
|
|
10101
|
+
NSError *copyError = error;
|
|
10102
|
+
if (!copyError && location && [response.URL.scheme.lowercaseString isEqualToString:@"https"] && [response isKindOfClass:[NSHTTPURLResponse class]] && ((NSHTTPURLResponse *)response).statusCode >= 200 && ((NSHTTPURLResponse *)response).statusCode < 300) {
|
|
10103
|
+
NSString *path = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"native-update-%@.zip", NSUUID.UUID.UUIDString]];
|
|
10104
|
+
copied = [NSURL fileURLWithPath:path];
|
|
10105
|
+
if (![[NSFileManager defaultManager] copyItemAtURL:location toURL:copied error:©Error]) copied = nil;
|
|
10106
|
+
}
|
|
10107
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
10108
|
+
const BOOL cancelledByUser = strongSelf.updateDownloadCancelledByUser;
|
|
10109
|
+
strongSelf.updateDownloadCancelledByUser = NO;
|
|
10110
|
+
[strongSelf.updateDownloadLimitTimer invalidate];
|
|
10111
|
+
strongSelf.updateDownloadLimitTimer = nil;
|
|
10112
|
+
strongSelf.updateDownloadTask = nil;
|
|
10113
|
+
NSAlert *downloadAlert = strongSelf.updateDownloadAlert;
|
|
10114
|
+
if (downloadAlert.window.sheetParent) {
|
|
10115
|
+
[downloadAlert.window.sheetParent endSheet:downloadAlert.window returnCode:NSModalResponseOK];
|
|
10116
|
+
} else if (strongSelf.updateDownloadAlertIsAppModal && NSApp.modalWindow == downloadAlert.window) {
|
|
10117
|
+
[NSApp abortModal];
|
|
10118
|
+
[downloadAlert.window orderOut:nil];
|
|
10119
|
+
}
|
|
10120
|
+
strongSelf.updateDownloadAlertIsAppModal = NO;
|
|
10121
|
+
strongSelf.updateDownloadAlert = nil;
|
|
10122
|
+
if (cancelledByUser) {
|
|
10123
|
+
if (copied) [[NSFileManager defaultManager] removeItemAtURL:copied error:nil];
|
|
10124
|
+
strongSelf.updateCheckRunning = NO;
|
|
10125
|
+
return;
|
|
10126
|
+
}
|
|
10127
|
+
if (!copied || copyError) {
|
|
10128
|
+
[strongSelf showUpdateError:copyError.localizedDescription ?: @"The update archive could not be downloaded." userInitiated:YES];
|
|
10129
|
+
return;
|
|
10130
|
+
}
|
|
10131
|
+
[strongSelf installDownloadedUpdate:copied version:version archiveBytes:archiveBytes sha256:sha256];
|
|
10132
|
+
});
|
|
10133
|
+
}];
|
|
10134
|
+
self.updateDownloadLimitTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateDownloadLimitTimerFired:) userInfo:nil repeats:YES];
|
|
10135
|
+
NSWindow *presentationWindow = [self updatePresentationWindow];
|
|
10136
|
+
if (presentationWindow) {
|
|
10137
|
+
[progress beginSheetModalForWindow:presentationWindow completionHandler:^(NSModalResponse responseCode) {
|
|
10138
|
+
NativeSdkAppKitHost *strongSelf = weakSelf;
|
|
10139
|
+
if (responseCode == NSAlertFirstButtonReturn && strongSelf.updateDownloadTask) {
|
|
10140
|
+
strongSelf.updateDownloadCancelledByUser = YES;
|
|
10141
|
+
[strongSelf.updateDownloadTask cancel];
|
|
10142
|
+
}
|
|
10143
|
+
}];
|
|
10144
|
+
[self.updateDownloadTask resume];
|
|
10145
|
+
} else {
|
|
10146
|
+
self.updateDownloadAlertIsAppModal = YES;
|
|
10147
|
+
[NSApp activateIgnoringOtherApps:YES];
|
|
10148
|
+
[self.updateDownloadTask resume];
|
|
10149
|
+
const NSModalResponse responseCode = [progress runModal];
|
|
10150
|
+
const BOOL completedByDownload = !self.updateDownloadAlertIsAppModal;
|
|
10151
|
+
self.updateDownloadAlertIsAppModal = NO;
|
|
10152
|
+
if (!completedByDownload && responseCode == NSAlertFirstButtonReturn && self.updateDownloadTask) {
|
|
10153
|
+
self.updateDownloadCancelledByUser = YES;
|
|
10154
|
+
[self.updateDownloadTask cancel];
|
|
10155
|
+
}
|
|
10156
|
+
}
|
|
10157
|
+
}
|
|
10158
|
+
|
|
10159
|
+
- (void)updateDownloadLimitTimerFired:(NSTimer *)timer {
|
|
10160
|
+
(void)timer;
|
|
10161
|
+
if (!self.updateDownloadTask) {
|
|
10162
|
+
[self.updateDownloadLimitTimer invalidate];
|
|
10163
|
+
self.updateDownloadLimitTimer = nil;
|
|
10164
|
+
return;
|
|
10165
|
+
}
|
|
10166
|
+
if ((uint64_t)self.updateDownloadTask.countOfBytesReceived > self.updateExpectedArchiveBytes) [self.updateDownloadTask cancel];
|
|
10167
|
+
}
|
|
10168
|
+
|
|
10169
|
+
static BOOL NativeSdkRunTask(NSString *launchPath, NSArray<NSString *> *arguments, NSString **output) {
|
|
10170
|
+
NSTask *task = [[NSTask alloc] init];
|
|
10171
|
+
task.executableURL = [NSURL fileURLWithPath:launchPath];
|
|
10172
|
+
task.arguments = arguments;
|
|
10173
|
+
NSPipe *pipe = [NSPipe pipe];
|
|
10174
|
+
task.standardOutput = pipe;
|
|
10175
|
+
task.standardError = pipe;
|
|
10176
|
+
NSError *error = nil;
|
|
10177
|
+
if (![task launchAndReturnError:&error]) {
|
|
10178
|
+
if (output) *output = error.localizedDescription;
|
|
10179
|
+
return NO;
|
|
10180
|
+
}
|
|
10181
|
+
NSData *data = [pipe.fileHandleForReading readDataToEndOfFile];
|
|
10182
|
+
[task waitUntilExit];
|
|
10183
|
+
if (output) *output = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] ?: @"";
|
|
10184
|
+
return task.terminationStatus == 0;
|
|
10185
|
+
}
|
|
10186
|
+
|
|
10187
|
+
static NSString *NativeSdkSigningTeamIdentifier(NSString *bundlePath) {
|
|
10188
|
+
SecStaticCodeRef code = NULL;
|
|
10189
|
+
CFDictionaryRef information = NULL;
|
|
10190
|
+
OSStatus status = SecStaticCodeCreateWithPath((__bridge CFURLRef)[NSURL fileURLWithPath:bundlePath], kSecCSDefaultFlags, &code);
|
|
10191
|
+
if (status == errSecSuccess) status = SecCodeCopySigningInformation(code, kSecCSSigningInformation, &information);
|
|
10192
|
+
NSString *result = nil;
|
|
10193
|
+
if (status == errSecSuccess && information) {
|
|
10194
|
+
CFTypeRef value = CFDictionaryGetValue(information, kSecCodeInfoTeamIdentifier);
|
|
10195
|
+
if (value && CFGetTypeID(value) == CFStringGetTypeID()) result = [(__bridge NSString *)value copy];
|
|
10196
|
+
}
|
|
10197
|
+
if (information) CFRelease(information);
|
|
10198
|
+
if (code) CFRelease(code);
|
|
10199
|
+
return result;
|
|
10200
|
+
}
|
|
10201
|
+
|
|
10202
|
+
- (void)installDownloadedUpdate:(NSURL *)downloadURL version:(NSString *)version archiveBytes:(uint64_t)archiveBytes sha256:(NSString *)sha256 {
|
|
10203
|
+
NSString *bundlePath = NSBundle.mainBundle.bundlePath.stringByStandardizingPath;
|
|
10204
|
+
NSString *parentPath = bundlePath.stringByDeletingLastPathComponent;
|
|
10205
|
+
__weak NativeSdkAppKitHost *weakSelf = self;
|
|
10206
|
+
dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
|
|
10207
|
+
NativeSdkAppKitHost *strongSelf = weakSelf;
|
|
10208
|
+
if (!strongSelf) return;
|
|
10209
|
+
NSFileManager *manager = [NSFileManager defaultManager];
|
|
10210
|
+
if (![manager isWritableFileAtPath:parentPath]) {
|
|
10211
|
+
[manager removeItemAtURL:downloadURL error:nil];
|
|
10212
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
10213
|
+
[strongSelf showUpdateError:@"The app’s containing folder is not writable. Move the app to a user-writable Applications folder or install the update manually." userInitiated:YES];
|
|
10214
|
+
});
|
|
10215
|
+
return;
|
|
10216
|
+
}
|
|
10217
|
+
if (!native_sdk_update_verify_archive(downloadURL.path.UTF8String, [downloadURL.path lengthOfBytesUsingEncoding:NSUTF8StringEncoding], archiveBytes, sha256.UTF8String, [sha256 lengthOfBytesUsingEncoding:NSUTF8StringEncoding])) {
|
|
10218
|
+
[manager removeItemAtURL:downloadURL error:nil];
|
|
10219
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
10220
|
+
[strongSelf showUpdateError:@"The downloaded update did not match its signed size and SHA-256 digest." userInitiated:YES];
|
|
10221
|
+
});
|
|
10222
|
+
return;
|
|
10223
|
+
}
|
|
10224
|
+
NSString *stagePath = [parentPath stringByAppendingPathComponent:[NSString stringWithFormat:@".%@.native-update-%@", bundlePath.lastPathComponent, NSUUID.UUID.UUIDString]];
|
|
10225
|
+
NSString *extractPath = [stagePath stringByAppendingPathComponent:@"extracted"];
|
|
10226
|
+
NSError *stageError = nil;
|
|
10227
|
+
if (![manager createDirectoryAtPath:extractPath withIntermediateDirectories:YES attributes:nil error:&stageError]) {
|
|
10228
|
+
[manager removeItemAtPath:stagePath error:nil];
|
|
10229
|
+
[manager removeItemAtURL:downloadURL error:nil];
|
|
10230
|
+
dispatch_async(dispatch_get_main_queue(), ^{ [strongSelf showUpdateError:stageError.localizedDescription userInitiated:YES]; });
|
|
10231
|
+
return;
|
|
10232
|
+
}
|
|
10233
|
+
NSString *taskOutput = nil;
|
|
10234
|
+
if (!NativeSdkRunTask(@"/usr/bin/ditto", @[ @"-x", @"-k", downloadURL.path, extractPath ], &taskOutput)) {
|
|
10235
|
+
[manager removeItemAtPath:stagePath error:nil];
|
|
10236
|
+
[manager removeItemAtURL:downloadURL error:nil];
|
|
10237
|
+
dispatch_async(dispatch_get_main_queue(), ^{ [strongSelf showUpdateError:taskOutput.length > 0 ? taskOutput : @"The update archive could not be extracted." userInitiated:YES]; });
|
|
10238
|
+
return;
|
|
10239
|
+
}
|
|
10240
|
+
[manager removeItemAtURL:downloadURL error:nil];
|
|
10241
|
+
NSError *listingError = nil;
|
|
10242
|
+
NSArray<NSString *> *entries = [manager contentsOfDirectoryAtPath:extractPath error:&listingError];
|
|
10243
|
+
NSMutableArray<NSString *> *appEntries = [NSMutableArray array];
|
|
10244
|
+
for (NSString *entry in entries ?: @[]) {
|
|
10245
|
+
if ([[entry pathExtension].lowercaseString isEqualToString:@"app"]) [appEntries addObject:entry];
|
|
10246
|
+
}
|
|
10247
|
+
if (listingError || entries.count != 1 || appEntries.count != 1) {
|
|
10248
|
+
[manager removeItemAtPath:stagePath error:nil];
|
|
10249
|
+
dispatch_async(dispatch_get_main_queue(), ^{ [strongSelf showUpdateError:@"The update archive must contain exactly one top-level .app bundle." userInitiated:YES]; });
|
|
10250
|
+
return;
|
|
10251
|
+
}
|
|
10252
|
+
NSString *candidatePath = [extractPath stringByAppendingPathComponent:appEntries.firstObject];
|
|
10253
|
+
NSNumber *candidateIsDirectory = nil;
|
|
10254
|
+
NSNumber *candidateIsSymlink = nil;
|
|
10255
|
+
[[NSURL fileURLWithPath:candidatePath] getResourceValue:&candidateIsDirectory forKey:NSURLIsDirectoryKey error:nil];
|
|
10256
|
+
[[NSURL fileURLWithPath:candidatePath] getResourceValue:&candidateIsSymlink forKey:NSURLIsSymbolicLinkKey error:nil];
|
|
10257
|
+
NSBundle *candidateBundle = [NSBundle bundleWithPath:candidatePath];
|
|
10258
|
+
if (!candidateIsDirectory.boolValue || candidateIsSymlink.boolValue || !candidateBundle || ![candidateBundle.bundleIdentifier isEqualToString:strongSelf.bundleIdentifier] || ![[candidateBundle objectForInfoDictionaryKey:@"CFBundleVersion"] isEqualToString:version]) {
|
|
10259
|
+
[manager removeItemAtPath:stagePath error:nil];
|
|
10260
|
+
dispatch_async(dispatch_get_main_queue(), ^{ [strongSelf showUpdateError:@"The update archive does not contain the expected app bundle and version." userInitiated:YES]; });
|
|
10261
|
+
return;
|
|
10262
|
+
}
|
|
10263
|
+
NSString *teamIdentifier = NativeSdkSigningTeamIdentifier(bundlePath);
|
|
10264
|
+
if (teamIdentifier.length > 0) {
|
|
10265
|
+
if ([teamIdentifier rangeOfCharacterFromSet:[[NSCharacterSet alphanumericCharacterSet] invertedSet]].location != NSNotFound ||
|
|
10266
|
+
[strongSelf.bundleIdentifier rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:@"\\\""]].location != NSNotFound) {
|
|
10267
|
+
[manager removeItemAtPath:stagePath error:nil];
|
|
10268
|
+
dispatch_async(dispatch_get_main_queue(), ^{ [strongSelf showUpdateError:@"The installed app’s signing identity cannot be represented safely." userInitiated:YES]; });
|
|
10269
|
+
return;
|
|
10270
|
+
}
|
|
10271
|
+
NSString *requirement = [NSString stringWithFormat:@"anchor apple generic and certificate leaf[subject.OU] = \"%@\" and identifier \"%@\"", teamIdentifier, strongSelf.bundleIdentifier];
|
|
10272
|
+
NSMutableArray<NSString *> *codesignArguments = [NSMutableArray arrayWithArray:@[ @"--verify", @"--deep", @"--strict" ]];
|
|
10273
|
+
if (requirement.length > 0) [codesignArguments addObject:[@"-R=" stringByAppendingString:requirement]];
|
|
10274
|
+
[codesignArguments addObject:candidatePath];
|
|
10275
|
+
if (!NativeSdkRunTask(@"/usr/bin/codesign", codesignArguments, &taskOutput)) {
|
|
10276
|
+
[manager removeItemAtPath:stagePath error:nil];
|
|
10277
|
+
dispatch_async(dispatch_get_main_queue(), ^{ [strongSelf showUpdateError:taskOutput.length > 0 ? taskOutput : @"The update’s Apple code signature does not match the installed app." userInitiated:YES]; });
|
|
10278
|
+
return;
|
|
10279
|
+
}
|
|
10280
|
+
}
|
|
10281
|
+
NSString *scriptPath = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"native-update-install-%@.sh", NSUUID.UUID.UUIDString]];
|
|
10282
|
+
NSString *backupPath = [parentPath stringByAppendingPathComponent:[NSString stringWithFormat:@".%@.native-update-backup-%@", bundlePath.lastPathComponent, NSUUID.UUID.UUIDString]];
|
|
10283
|
+
NSString *script = @"#!/bin/sh\nset -u\npid=\"$1\"\ncurrent=\"$2\"\ncandidate=\"$3\"\nbackup=\"$4\"\nstage=\"$5\"\ncount=0\nwhile /bin/kill -0 \"$pid\" 2>/dev/null; do\n count=$((count + 1))\n if [ \"$count\" -ge 600 ]; then /bin/rm -rf \"$stage\"; /bin/rm -f \"$0\"; exit 1; fi\n /bin/sleep 0.1\ndone\nif /bin/mv \"$current\" \"$backup\" && /bin/mv \"$candidate\" \"$current\"; then\n if /usr/bin/open \"$current\"; then\n /bin/rm -rf \"$backup\" \"$stage\"\n /bin/rm -f \"$0\"\n exit 0\n fi\n /bin/rm -rf \"$current\"\n /bin/mv \"$backup\" \"$current\"\nfi\nif [ -e \"$backup\" ] && [ ! -e \"$current\" ]; then /bin/mv \"$backup\" \"$current\"; fi\n/usr/bin/open \"$current\" >/dev/null 2>&1 || true\n/bin/rm -rf \"$stage\"\n/bin/rm -f \"$0\"\nexit 1\n";
|
|
10284
|
+
NSError *scriptError = nil;
|
|
10285
|
+
if (![script writeToFile:scriptPath atomically:YES encoding:NSUTF8StringEncoding error:&scriptError] || ![manager setAttributes:@{ NSFilePosixPermissions: @0700 } ofItemAtPath:scriptPath error:&scriptError]) {
|
|
10286
|
+
[manager removeItemAtPath:stagePath error:nil];
|
|
10287
|
+
dispatch_async(dispatch_get_main_queue(), ^{ [strongSelf showUpdateError:scriptError.localizedDescription userInitiated:YES]; });
|
|
10288
|
+
return;
|
|
10289
|
+
}
|
|
10290
|
+
NSTask *installer = [[NSTask alloc] init];
|
|
10291
|
+
installer.executableURL = [NSURL fileURLWithPath:@"/bin/sh"];
|
|
10292
|
+
installer.arguments = @[ scriptPath, [NSString stringWithFormat:@"%d", getpid()], bundlePath, candidatePath, backupPath, stagePath ];
|
|
10293
|
+
NSError *launchError = nil;
|
|
10294
|
+
if (![installer launchAndReturnError:&launchError]) {
|
|
10295
|
+
[manager removeItemAtPath:stagePath error:nil];
|
|
10296
|
+
[manager removeItemAtPath:scriptPath error:nil];
|
|
10297
|
+
dispatch_async(dispatch_get_main_queue(), ^{ [strongSelf showUpdateError:launchError.localizedDescription userInitiated:YES]; });
|
|
10298
|
+
return;
|
|
10299
|
+
}
|
|
10300
|
+
dispatch_async(dispatch_get_main_queue(), ^{ [NSApp terminate:nil]; });
|
|
10301
|
+
});
|
|
10302
|
+
}
|
|
10303
|
+
|
|
9809
10304
|
- (NSMenuItem *)menuItem:(NSString *)title action:(SEL)action key:(NSString *)key modifiers:(NSEventModifierFlags)modifiers {
|
|
9810
10305
|
NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:title action:action keyEquivalent:key ?: @""];
|
|
9811
10306
|
item.keyEquivalentModifierMask = modifiers;
|
|
@@ -10009,6 +10504,13 @@ static void NativeSdkApplyProcessDisplayName(NSString *displayName) {
|
|
|
10009
10504
|
dispatch_activate(sigterm_source);
|
|
10010
10505
|
self.sigtermSource = sigterm_source;
|
|
10011
10506
|
|
|
10507
|
+
if (self.updateCheckOnStart && self.updateFeedURL.length > 0 && self.updatePublicKey.length > 0) {
|
|
10508
|
+
__weak NativeSdkAppKitHost *weakUpdateSelf = self;
|
|
10509
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
10510
|
+
[weakUpdateSelf checkForUpdatesUserInitiated:NO];
|
|
10511
|
+
});
|
|
10512
|
+
}
|
|
10513
|
+
|
|
10012
10514
|
[self scheduleFrame];
|
|
10013
10515
|
[NSApp run];
|
|
10014
10516
|
}
|
|
@@ -12522,6 +13024,26 @@ void native_sdk_appkit_set_dock_icon_file(native_sdk_appkit_host_t *host, const
|
|
|
12522
13024
|
}
|
|
12523
13025
|
}
|
|
12524
13026
|
|
|
13027
|
+
void native_sdk_appkit_configure_updates(native_sdk_appkit_host_t *host, const char *feed_url, size_t feed_url_len, const char *public_key, size_t public_key_len, int check_on_start, const char *target, size_t target_len) {
|
|
13028
|
+
if (!host) return;
|
|
13029
|
+
NativeSdkAppKitHost *object = (__bridge NativeSdkAppKitHost *)host;
|
|
13030
|
+
@autoreleasepool {
|
|
13031
|
+
object.updateFeedURL = feed_url && feed_url_len > 0 ? ([[NSString alloc] initWithBytes:feed_url length:feed_url_len encoding:NSUTF8StringEncoding] ?: @"") : @"";
|
|
13032
|
+
object.updatePublicKey = public_key && public_key_len > 0 ? ([[NSString alloc] initWithBytes:public_key length:public_key_len encoding:NSUTF8StringEncoding] ?: @"") : @"";
|
|
13033
|
+
object.updateTarget = target && target_len > 0 ? ([[NSString alloc] initWithBytes:target length:target_len encoding:NSUTF8StringEncoding] ?: @"") : @"";
|
|
13034
|
+
object.updateCheckOnStart = check_on_start != 0;
|
|
13035
|
+
[object buildMenuBar];
|
|
13036
|
+
}
|
|
13037
|
+
}
|
|
13038
|
+
|
|
13039
|
+
int native_sdk_appkit_check_for_updates(native_sdk_appkit_host_t *host, int user_initiated) {
|
|
13040
|
+
if (!host) return 0;
|
|
13041
|
+
NativeSdkAppKitHost *object = (__bridge NativeSdkAppKitHost *)host;
|
|
13042
|
+
if (object.updateFeedURL.length == 0 || object.updatePublicKey.length == 0) return 0;
|
|
13043
|
+
[object checkForUpdatesUserInitiated:(user_initiated != 0)];
|
|
13044
|
+
return 1;
|
|
13045
|
+
}
|
|
13046
|
+
|
|
12525
13047
|
void native_sdk_appkit_run(native_sdk_appkit_host_t *host, native_sdk_appkit_event_callback_t callback, void *context) {
|
|
12526
13048
|
NativeSdkAppKitHost *object = (__bridge NativeSdkAppKitHost *)host;
|
|
12527
13049
|
[object runWithCallback:callback context:context];
|