Skip to content
This repository has been archived by the owner on Jul 18, 2019. It is now read-only.

Commit

Permalink
Change runningApplication observing codes.
Browse files Browse the repository at this point in the history
- Stop observing `isTerminated`. It's not happen sometime.
- Use `NSKeyValueObservingOptionInitial` for simplify codes.
  • Loading branch information
norio-nomura committed Nov 26, 2014
1 parent b994e6a commit 524be0c
Showing 1 changed file with 9 additions and 32 deletions.
41 changes: 9 additions & 32 deletions SIMBL Agent/SIMBLAgent.m
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,9 @@ - (void) applicationDidFinishLaunching:(NSNotification*)notificaion
NSWorkspace *workspace = [NSWorkspace sharedWorkspace];
[workspace addObserver:self
forKeyPath:@"runningApplications"
options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld
options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld|NSKeyValueObservingOptionInitial
context:NULL];

// inject into resumed applications
for (NSRunningApplication *runningApp in [workspace runningApplications]) {
[self injectSIMBL:runningApp];
}

// previous minus running, it should be uninject
[previousInjectedSandboxBundleIdentifierSet minusSet:[NSMutableSet setWithArray:[defaults objectForKey:kInjectedSandboxBundleIdentifiers]]];
if ([previousInjectedSandboxBundleIdentifierSet count]) {
Expand All @@ -90,34 +85,19 @@ - (id) eventDidFail:(const AppleEvent *)event withError:(NSError *)error;

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:@"isTerminated"]) {
SIMBLLogDebug(@"runningApp %@ isTerminated.", object);
[object removeObserver:self forKeyPath:keyPath];

[self injectContainerForApplication:(NSRunningApplication*)object enabled:NO];
} else if ([keyPath isEqualToString:@"runningApplications"]) {
// for apps which will be terminated without called @"isFinishedLaunching"
static NSMutableSet *appsObservingFinishedLaunching = nil;
if (!appsObservingFinishedLaunching) {
appsObservingFinishedLaunching = [NSMutableSet set];
}

if ([keyPath isEqualToString:@"runningApplications"]) {
for (NSRunningApplication *app in [change objectForKey:NSKeyValueChangeNewKey]) {
if (app.isFinishedLaunching) {
SIMBLLogDebug(@"runningApp %@ is already isFinishedLaunching", app);
[self injectSIMBL:app];
} else {
[app addObserver:self forKeyPath:@"isFinishedLaunching" options:NSKeyValueObservingOptionNew context:NULL];
[appsObservingFinishedLaunching addObject:app];
}
SIMBLLogDebug(@"add runningApp %@.", app);
[app addObserver:self forKeyPath:@"isFinishedLaunching" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionInitial context:NULL];
}
for (NSRunningApplication *app in [change objectForKey:NSKeyValueChangeOldKey]) {
if ([appsObservingFinishedLaunching containsObject:app]) {
[app removeObserver:self forKeyPath:@"isFinishedLaunching"];
[appsObservingFinishedLaunching removeObject:app];
SIMBLLogDebug(@"remove runningApp %@.", app);
[app removeObserver:self forKeyPath:@"isFinishedLaunching"];
if ([self.runningSandboxedApplications containsObject:app]) {
[self injectContainerForApplication:app enabled:NO];
}
}
} else if ([keyPath isEqualToString:@"isFinishedLaunching"]) {
} else if ([keyPath isEqualToString:@"isFinishedLaunching"] && [change[NSKeyValueChangeNewKey]boolValue]) {
SIMBLLogDebug(@"runningApp %@ isFinishedLaunching.", object);
[self injectSIMBL:(NSRunningApplication*)object];
}
Expand Down Expand Up @@ -275,9 +255,6 @@ - (void)injectContainerForApplication:(NSRunningApplication*)runningApp enabled:
NSString *identifier = [runningApp bundleIdentifier];
if (bEnabled) {
if ([self injectContainerBundleIdentifier:identifier enabled:YES]) {
SIMBLLogDebug(@"Start observing %@'s 'isTerminated'.", identifier);

[runningApp addObserver:self forKeyPath:@"isTerminated" options:NSKeyValueObservingOptionNew context:NULL];
[self.runningSandboxedApplications addObject:runningApp];

NSMutableSet *injectedSandboxBundleIdentifierSet = [NSMutableSet set];
Expand Down

0 comments on commit 524be0c

Please sign in to comment.