Skip to content

Commit

Permalink
MacGui: improve management of security scoped resources lifetime from…
Browse files Browse the repository at this point in the history
… URL passed from open panels.
  • Loading branch information
galad87 committed Jun 1, 2023
1 parent 866aad6 commit 57ac2a3
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 25 deletions.
18 changes: 14 additions & 4 deletions macosx/HBController.m
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ @interface HBController () <HBPresetsViewControllerDelegate, HBTitleSelectionDel
@property (nonatomic, readonly, strong) HBCore *core;
@property (nonatomic, readonly, strong) HBAppDelegate *delegate;

@property (nonatomic, strong) HBSecurityAccessToken *fileToken;
@property (nonatomic, strong) NSURL *destinationFolderURL;
@property (nonatomic, strong) HBSecurityAccessToken *destinationFolderToken;


@property (nonatomic, weak) IBOutlet NSTextField *sourceLabel;
@property (nonatomic, weak) IBOutlet NSPopUpButton *titlePopUp;
@property (nonatomic, weak) IBOutlet NSPathControl *destinationPathControl;
Expand Down Expand Up @@ -105,8 +110,6 @@ @interface HBController () <HBPresetsViewControllerDelegate, HBTitleSelectionDel

#pragma mark - Job

@property (nonatomic, strong) NSURL *destinationFolderURL;

@property (nonatomic, nullable) HBJob *job;
@property (nonatomic, nullable) HBAutoNamer *autoNamer;

Expand Down Expand Up @@ -178,7 +181,7 @@ - (instancetype)initWithDelegate:(HBAppDelegate *)delegate queue:(HBQueue *)queu
}

#ifdef __SANDBOX_ENABLED__
[_destinationFolderURL startAccessingSecurityScopedResource];
_destinationFolderToken = [HBSecurityAccessToken tokenWithObject:_destinationFolderURL];
#endif
}

Expand Down Expand Up @@ -788,6 +791,7 @@ - (void)showOpenPanelForDestination:(NSURL *)destinationURL
if (result == NSModalResponseOK)
{
self.destinationFolderURL = panel.URL;
self.destinationFolderToken = [HBSecurityAccessToken tokenWithAlreadyAccessedObject:panel.URL];
}
}];
}
Expand All @@ -808,6 +812,8 @@ - (void)openURL:(NSURL *)fileURL titleIndex:(NSUInteger)index
{
[self showWindow:self];

self.fileToken = [HBSecurityAccessToken tokenWithAlreadyAccessedObject:fileURL];

[self scanURL:fileURL titleIndex:index completionHandler:^(NSArray<HBTitle *> *titles)
{
if (titles.count)
Expand All @@ -829,7 +835,7 @@ - (void)openURL:(NSURL *)fileURL titleIndex:(NSUInteger)index
self.job = job;
if (featuredTitle.isStream && [NSUserDefaults.standardUserDefaults boolForKey:HBUseSourceFolderDestination])
{
[self askForPermissionAndSetDestinationURL:job.title.url.URLByDeletingLastPathComponent];
[self askForPermissionAndSetDestinationURL:job.fileURL.URLByDeletingLastPathComponent];
}
}
else
Expand Down Expand Up @@ -858,6 +864,8 @@ - (void)openJob:(HBJob *)job completionHandler:(void (^)(BOOL result))handler
if (self.core.state != HBStateScanning)
{
[job refreshSecurityScopedResources];
self.fileToken = [HBSecurityAccessToken tokenWithObject:job.fileURL];

[self scanURL:job.fileURL titleIndex:job.titleIdx completionHandler:^(NSArray<HBTitle *> *titles)
{
if (titles.count)
Expand Down Expand Up @@ -1088,6 +1096,7 @@ - (IBAction)browseDestination:(id)sender
if (result == NSModalResponseOK)
{
self.destinationFolderURL = panel.URL;
self.destinationFolderToken = [HBSecurityAccessToken tokenWithAlreadyAccessedObject:panel.URL];
}
}];
}
Expand Down Expand Up @@ -1118,6 +1127,7 @@ - (BOOL)pathControl:(NSPathControl *)pathControl acceptDrop:(id <NSDraggingInfo>
if (URL.hasDirectoryPath)
{
self.destinationFolderURL = URL;
self.destinationFolderToken = [HBSecurityAccessToken tokenWithAlreadyAccessedObject:URL];
}
return YES;
}
Expand Down
26 changes: 18 additions & 8 deletions macosx/HBRemoteCore.m
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ - (void)handleInterruption
[self forwardError:@"XPC: Service did crash\n"];
}

- (void)updateState:(HBState)state {
- (void)updateState:(HBState)state
{
dispatch_sync(dispatch_get_main_queue(), ^{
self.state = state;
});
Expand Down Expand Up @@ -210,10 +211,16 @@ - (void)encodeJob:(HBJob *)job progressHandler:(HBCoreProgressHandler)progressHa
}
}

NSData *bookmark = [job.destinationFolderURL bookmarkDataWithOptions:0 includingResourceValuesForKeys:nil relativeToURL:nil error:NULL];
if (bookmark)
NSData *destinationBookmark = [job.destinationFolderURL bookmarkDataWithOptions:0 includingResourceValuesForKeys:nil relativeToURL:nil error:NULL];
if (destinationBookmark)
{
[bookmarks addObject:destinationBookmark];
}

NSData *sourceBookmark = [job.fileURL bookmarkDataWithOptions:0 includingResourceValuesForKeys:nil relativeToURL:nil error:NULL];
if (sourceBookmark)
{
[bookmarks addObject:bookmark];
[bookmarks addObject:sourceBookmark];
}

[_proxy provideResourceAccessWithBookmarks:bookmarks];
Expand Down Expand Up @@ -245,13 +252,16 @@ - (void)cancelEncode
[_proxy cancelEncode];
}

- (void)updateProgress:(double)currentProgress hours:(int)hours minutes:(int)minutes seconds:(int)seconds state:(HBState)state info:(NSString *)info {

- (void)updateProgress:(double)currentProgress hours:(int)hours minutes:(int)minutes seconds:(int)seconds state:(HBState)state info:(NSString *)info
{
__weak HBRemoteCore *weakSelf = self;

dispatch_sync(dispatch_get_main_queue(), ^{
HBProgress progress = {currentProgress , hours, minutes, seconds};
weakSelf.progressHandler(state, progress, info);
if (weakSelf.progressHandler)
{
HBProgress progress = {currentProgress , hours, minutes, seconds};
weakSelf.progressHandler(state, progress, info);
}
});
}

Expand Down
5 changes: 4 additions & 1 deletion macosx/HBSecurityAccessToken.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ NS_ASSUME_NONNULL_BEGIN

@interface HBSecurityAccessToken : NSObject

+ (instancetype)tokenWithObject:(id<HBSecurityScope>)object;
- (instancetype)initWithObject:(id<HBSecurityScope>)object;
- (instancetype)initWithAlreadyAccessedObject:(id<HBSecurityScope>)object;

+ (instancetype)tokenWithObject:(id<HBSecurityScope>)object;
+ (instancetype)tokenWithAlreadyAccessedObject:(id<HBSecurityScope>)object;

@end

Expand Down
16 changes: 16 additions & 0 deletions macosx/HBSecurityAccessToken.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,27 @@ - (instancetype)initWithObject:(id<HBSecurityScope>)object
return self;
}

- (instancetype)initWithAlreadyAccessedObject:(id<HBSecurityScope>)object
{
self = [super init];
if (self)
{
_object = object;
_accessed = YES;
}
return self;
}

+ (instancetype)tokenWithObject:(id<HBSecurityScope>)object
{
return [[self alloc] initWithObject:object];
}

+ (instancetype)tokenWithAlreadyAccessedObject:(id<HBSecurityScope>)object
{
return [[self alloc] initWithAlreadyAccessedObject:object];
}

- (void)dealloc
{
if (_accessed)
Expand Down
33 changes: 21 additions & 12 deletions macosx/HandBrakeXPCService/HandBrakeXPCService.m
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,20 @@ - (void)setUpWithLogLevel:(NSInteger)level name:(NSString *)name
_core = [[HBCore alloc] initWithLogLevel:level queue:_queue];
_core.name = name;

// Completion handler
void (^progressHandler)(HBState state, HBProgress progress, NSString *info) = ^(HBState state, HBProgress progress, NSString *info)
{
[self.connection.remoteObjectProxy updateProgress:progress.percent
hours:progress.hours
minutes:progress.minutes
seconds:progress.seconds
state:state
info:info];
};
_progressHandler = progressHandler;

void (^completionHandler)(HBCoreResult result) = ^(HBCoreResult result)
{
self->_progressHandler = nil;
[self stopAccessingSecurityScopedResources];
self.reply(result);
self.reply = nil;
};
Expand Down Expand Up @@ -102,6 +112,14 @@ - (void)provideResourceAccessWithBookmarks:(NSArray<NSData *> *)bookmarks
});
}

- (void)stopAccessingSecurityScopedResources
{
for (NSURL *url in self.urls)
{
[url stopAccessingSecurityScopedResource];
}
}

- (void)setAutomaticallyPreventSleep:(BOOL)automaticallyPreventSleep
{
dispatch_sync(_queue, ^{
Expand Down Expand Up @@ -138,11 +156,6 @@ - (void)allowSleep
- (void)scanURL:(NSURL *)url titleIndex:(NSUInteger)index previews:(NSUInteger)previewsNum minDuration:(NSUInteger)seconds keepPreviews:(BOOL)keepPreviews withReply:(void (^)(HBCoreResult))reply
{
dispatch_sync(_queue, ^{
void (^progressHandler)(HBState state, HBProgress progress, NSString *info) = ^(HBState state, HBProgress progress, NSString *info)
{
[self.connection.remoteObjectProxy updateProgress:progress.percent hours:progress.hours minutes:progress.minutes seconds:progress.seconds state:state info:info];
};
self->_progressHandler = progressHandler;
self.reply = reply;

[self.core scanURL:url titleIndex:index previews:previewsNum minDuration:seconds keepPreviews:keepPreviews
Expand All @@ -161,15 +174,11 @@ - (void)cancelScan
- (void)encodeJob:(HBJob *)job withReply:(void (^)(HBCoreResult))reply
{
dispatch_sync(_queue, ^{
void (^progressHandler)(HBState state, HBProgress progress, NSString *info) = ^(HBState state, HBProgress progress, NSString *info)
{
[self.connection.remoteObjectProxy updateProgress:progress.percent hours:progress.hours minutes:progress.minutes seconds:progress.seconds state:state info:info];
};
self->_progressHandler = progressHandler;
self.reply = reply;

// Reset the title in the job.
job.title = self.core.titles.firstObject;

[self.core encodeJob:job
progressHandler:self.progressHandler
completionHandler:self.completionHandler];
Expand Down

0 comments on commit 57ac2a3

Please sign in to comment.