diff --git a/Autoupdate/AgentConnection.m b/Autoupdate/AgentConnection.m index 87e5e143..9396d95d 100644 --- a/Autoupdate/AgentConnection.m +++ b/Autoupdate/AgentConnection.m @@ -78,7 +78,9 @@ __weak AgentConnection *weakSelf = self; newConnection.interruptionHandler = ^{ - [weakSelf.activeConnection invalidate]; + dispatch_async(dispatch_get_main_queue(), ^{ + [weakSelf.activeConnection invalidate]; + }); }; newConnection.invalidationHandler = ^{ diff --git a/Autoupdate/AppInstaller.m b/Autoupdate/AppInstaller.m index e8e462c8..2223f389 100644 --- a/Autoupdate/AppInstaller.m +++ b/Autoupdate/AppInstaller.m @@ -147,7 +147,9 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7; __weak AppInstaller *weakSelf = self; newConnection.interruptionHandler = ^{ - [weakSelf.activeConnection invalidate]; + dispatch_async(dispatch_get_main_queue(), ^{ + [weakSelf.activeConnection invalidate]; + }); }; newConnection.invalidationHandler = ^{ diff --git a/InstallerConnection/SUInstallerConnection.h b/InstallerConnection/SUInstallerConnection.h index dfbcc5fc..2134c177 100644 --- a/InstallerConnection/SUInstallerConnection.h +++ b/InstallerConnection/SUInstallerConnection.h @@ -15,7 +15,9 @@ NS_ASSUME_NONNULL_BEGIN @interface SUInstallerConnection : NSObject // Due to XPC reasons, this delegate is strongly referenced, until it's invalidated -- (instancetype)initWithDelegate:(id)delegate; +- (instancetype)initWithDelegate:(id)delegate remote:(BOOL)remote; + +- (instancetype)init NS_UNAVAILABLE; @end diff --git a/InstallerConnection/SUInstallerConnection.m b/InstallerConnection/SUInstallerConnection.m index 89887aff..857fc4e4 100644 --- a/InstallerConnection/SUInstallerConnection.m +++ b/InstallerConnection/SUInstallerConnection.m @@ -25,21 +25,27 @@ static NSString *SUInstallerConnectionKeepAliveReason = @"Installer Connection K @end @implementation SUInstallerConnection +{ + BOOL _remote; +} @synthesize delegate = _delegate; @synthesize disabledAutomaticTermination = _disabledAutomaticTermination; @synthesize invalidationBlock = _invalidationBlock; @synthesize connection = _connection; -- (instancetype)initWithDelegate:(id)delegate +- (instancetype)initWithDelegate:(id)delegate remote:(BOOL)remote { self = [super init]; if (self != nil) { _delegate = delegate; + _remote = remote; - // If we are a XPC service, protect it from being terminated until the invalidation handler is set - _disabledAutomaticTermination = YES; - [[NSProcessInfo processInfo] disableAutomaticTermination:SUInstallerConnectionKeepAliveReason]; + if (remote) { + // If we are a XPC service, protect it from being terminated until the invalidation handler is set + _disabledAutomaticTermination = YES; + [[NSProcessInfo processInfo] disableAutomaticTermination:SUInstallerConnectionKeepAliveReason]; + } } return self; } @@ -52,7 +58,7 @@ static NSString *SUInstallerConnectionKeepAliveReason = @"Installer Connection K } } -- (void)setInvalidationHandler:(void (^)(void))invalidationHandler +- (void)_setInvalidationHandler:(void (^)(void))invalidationHandler { self.invalidationBlock = invalidationHandler; @@ -60,7 +66,18 @@ static NSString *SUInstallerConnectionKeepAliveReason = @"Installer Connection K [self enableAutomaticTermination]; } -- (void)setServiceName:(NSString *)serviceName systemDomain:(BOOL)systemDomain +- (void)setInvalidationHandler:(void (^)(void))invalidationHandler +{ + if (_remote) { + dispatch_async(dispatch_get_main_queue(), ^{ + [self _setInvalidationHandler:invalidationHandler]; + }); + } else { + [self _setInvalidationHandler:invalidationHandler]; + } +} + +- (void)_setServiceName:(NSString *)serviceName systemDomain:(BOOL)systemDomain { NSXPCConnectionOptions options = systemDomain ? NSXPCConnectionPrivileged : 0; NSXPCConnection *connection = [[NSXPCConnection alloc] initWithMachServiceName:serviceName options:options]; @@ -74,41 +91,67 @@ static NSString *SUInstallerConnectionKeepAliveReason = @"Installer Connection K __weak SUInstallerConnection *weakSelf = self; self.connection.interruptionHandler = ^{ - [weakSelf.connection invalidate]; + dispatch_async(dispatch_get_main_queue(), ^{ + [weakSelf.connection invalidate]; + }); }; self.connection.invalidationHandler = ^{ - SUInstallerConnection *strongSelf = weakSelf; - if (strongSelf != nil) { - strongSelf.connection = nil; - [strongSelf invalidate]; - } + dispatch_async(dispatch_get_main_queue(), ^{ + SUInstallerConnection *strongSelf = weakSelf; + if (strongSelf != nil) { + strongSelf.connection = nil; + [strongSelf _invalidate]; + } + }); }; [self.connection resume]; } -- (void)handleMessageWithIdentifier:(int32_t)identifier data:(NSData *)data +- (void)setServiceName:(NSString *)serviceName systemDomain:(BOOL)systemDomain { - [(id)self.connection.remoteObjectProxy handleMessageWithIdentifier:identifier data:data]; + if (_remote) { + dispatch_async(dispatch_get_main_queue(), ^{ + [self _setServiceName:serviceName systemDomain:systemDomain]; + }); + } else { + [self _setServiceName:serviceName systemDomain:systemDomain]; + } } -// This method can be called by us or from a remote +- (void)handleMessageWithIdentifier:(int32_t)identifier data:(NSData *)data +{ + if (_remote) { + dispatch_async(dispatch_get_main_queue(), ^{ + [(id)self.connection.remoteObjectProxy handleMessageWithIdentifier:identifier data:data]; + }); + } else { + [(id)self.connection.remoteObjectProxy handleMessageWithIdentifier:identifier data:data]; + } +} + +- (void)_invalidate +{ + if (self.invalidationBlock != nil) { + self.invalidationBlock(); + self.invalidationBlock = nil; + } + + // Break the retain cycle + self.delegate = nil; + + [self enableAutomaticTermination]; +} + +// This method can be called from us or a remote - (void)invalidate { dispatch_async(dispatch_get_main_queue(), ^{ [self.connection invalidate]; self.connection = nil; - if (self.invalidationBlock != nil) { - self.invalidationBlock(); - self.invalidationBlock = nil; - } - - // Break the retain cycle - self.delegate = nil; - - [self enableAutomaticTermination]; + [self _invalidate]; }); } diff --git a/InstallerConnection/main.m b/InstallerConnection/main.m index 56d9e4ef..b28b1489 100644 --- a/InstallerConnection/main.m +++ b/InstallerConnection/main.m @@ -26,7 +26,7 @@ newConnection.exportedInterface = [NSXPCInterface interfaceWithProtocol:@protocol(SUInstallerConnectionProtocol)]; newConnection.remoteObjectInterface = [NSXPCInterface interfaceWithProtocol:@protocol(SUInstallerCommunicationProtocol)]; - SUInstallerConnection *exportedObject = [[SUInstallerConnection alloc] initWithDelegate:newConnection.remoteObjectProxy]; + SUInstallerConnection *exportedObject = [[SUInstallerConnection alloc] initWithDelegate:newConnection.remoteObjectProxy remote:YES]; newConnection.exportedObject = exportedObject; diff --git a/InstallerStatus/SUInstallerStatus.h b/InstallerStatus/SUInstallerStatus.h index 445f8a2a..04f7aa8c 100644 --- a/InstallerStatus/SUInstallerStatus.h +++ b/InstallerStatus/SUInstallerStatus.h @@ -12,4 +12,8 @@ // This object implements the protocol which we have defined. It provides the actual behavior for the service. It is 'exported' by the service to make it available to the process hosting the service over an NSXPCConnection. @interface SUInstallerStatus : NSObject +- (instancetype)initWithRemote:(BOOL)remote; + +- (instancetype)init NS_UNAVAILABLE; + @end diff --git a/InstallerStatus/SUInstallerStatus.m b/InstallerStatus/SUInstallerStatus.m index bf513981..f12c56be 100644 --- a/InstallerStatus/SUInstallerStatus.m +++ b/InstallerStatus/SUInstallerStatus.m @@ -19,16 +19,34 @@ @end @implementation SUInstallerStatus +{ + BOOL _remote; +} @synthesize invalidationBlock = _invalidationBlock; @synthesize connection = _connection; -- (void)setInvalidationHandler:(void (^)(void))invalidationHandler +- (instancetype)initWithRemote:(BOOL)remote { - self.invalidationBlock = invalidationHandler; + self = [super init]; + if (self != nil) { + _remote = remote; + } + return self; } -- (void)setServiceName:(NSString *)serviceName +- (void)setInvalidationHandler:(void (^)(void))invalidationHandler +{ + if (_remote) { + dispatch_async(dispatch_get_main_queue(), ^{ + self.invalidationBlock = invalidationHandler; + }); + } else { + self.invalidationBlock = invalidationHandler; + } +} + +- (void)_setServiceName:(NSString *)serviceName { NSXPCConnection *connection = [[NSXPCConnection alloc] initWithMachServiceName:serviceName options:(NSXPCConnectionOptions)0]; @@ -38,41 +56,73 @@ __weak SUInstallerStatus *weakSelf = self; self.connection.interruptionHandler = ^{ - [weakSelf.connection invalidate]; + dispatch_async(dispatch_get_main_queue(), ^{ + [weakSelf.connection invalidate]; + }); }; self.connection.invalidationHandler = ^{ - SUInstallerStatus *strongSelf = weakSelf; - if (strongSelf != nil) { - strongSelf.connection = nil; - [strongSelf invalidate]; - } + dispatch_async(dispatch_get_main_queue(), ^{ + SUInstallerStatus *strongSelf = weakSelf; + if (strongSelf != nil) { + strongSelf.connection = nil; + [strongSelf _invokeInvalidationBlock]; + } + }); }; [self.connection resume]; } +- (void)setServiceName:(NSString *)serviceName +{ + if (_remote) { + dispatch_async(dispatch_get_main_queue(), ^{ + [self _setServiceName:serviceName]; + }); + } else { + [self _setServiceName:serviceName]; + } +} + - (void)probeStatusInfoWithReply:(void (^)(NSData * _Nullable installationInfoData))reply { - [(id)self.connection.remoteObjectProxy probeStatusInfoWithReply:reply]; + if (_remote) { + dispatch_async(dispatch_get_main_queue(), ^{ + [(id)self.connection.remoteObjectProxy probeStatusInfoWithReply:reply]; + }); + } else { + [(id)self.connection.remoteObjectProxy probeStatusInfoWithReply:reply]; + } } - (void)probeStatusConnectivityWithReply:(void (^)(void))reply { - [(id)self.connection.remoteObjectProxy probeStatusConnectivityWithReply:reply]; + if (_remote) { + dispatch_async(dispatch_get_main_queue(), ^{ + [(id)self.connection.remoteObjectProxy probeStatusConnectivityWithReply:reply]; + }); + } else { + [(id)self.connection.remoteObjectProxy probeStatusConnectivityWithReply:reply]; + } } -// This method can be called by us or from a remote +- (void)_invokeInvalidationBlock +{ + if (self.invalidationBlock != nil) { + self.invalidationBlock(); + self.invalidationBlock = nil; + } +} + +// This method can be called from us or a remote - (void)invalidate { dispatch_async(dispatch_get_main_queue(), ^{ [self.connection invalidate]; self.connection = nil; - if (self.invalidationBlock != nil) { - self.invalidationBlock(); - self.invalidationBlock = nil; - } + [self _invokeInvalidationBlock]; }); } diff --git a/InstallerStatus/main.m b/InstallerStatus/main.m index 95a044f4..4c1c6cde 100644 --- a/InstallerStatus/main.m +++ b/InstallerStatus/main.m @@ -25,7 +25,7 @@ newConnection.exportedInterface = [NSXPCInterface interfaceWithProtocol:@protocol(SUInstallerStatusProtocol)]; // Next, set the object that the connection exports. All messages sent on the connection to this service will be sent to the exported object to handle. The connection retains the exported object. - SUInstallerStatus *exportedObject = [SUInstallerStatus new]; + SUInstallerStatus *exportedObject = [[SUInstallerStatus alloc] initWithRemote:YES]; newConnection.exportedObject = exportedObject; // Resuming the connection allows the system to deliver more incoming messages. diff --git a/Sparkle/InstallerProgress/InstallerProgressAppController.m b/Sparkle/InstallerProgress/InstallerProgressAppController.m index 7c519870..d1119308 100644 --- a/Sparkle/InstallerProgress/InstallerProgressAppController.m +++ b/Sparkle/InstallerProgress/InstallerProgressAppController.m @@ -126,7 +126,9 @@ static const NSTimeInterval SUTerminationTimeDelay = 0.3; __weak InstallerProgressAppController *weakSelf = self; _connection.interruptionHandler = ^{ - [weakSelf.connection invalidate]; + dispatch_async(dispatch_get_main_queue(), ^{ + [weakSelf.connection invalidate]; + }); }; _connection.invalidationHandler = ^{ diff --git a/Sparkle/SPUInstallerDriver.m b/Sparkle/SPUInstallerDriver.m index 87bb7e19..b89fb54b 100644 --- a/Sparkle/SPUInstallerDriver.m +++ b/Sparkle/SPUInstallerDriver.m @@ -176,7 +176,7 @@ assert(hostBundleIdentifier != nil); if (!SPUXPCServiceIsEnabled(SUEnableInstallerConnectionServiceKey)) { - self.installerConnection = [[SUInstallerConnection alloc] initWithDelegate:self]; + self.installerConnection = [[SUInstallerConnection alloc] initWithDelegate:self remote:NO]; } else { self.installerConnection = [[SUXPCInstallerConnection alloc] initWithDelegate:self]; } diff --git a/Sparkle/SPUProbeInstallStatus.m b/Sparkle/SPUProbeInstallStatus.m index 574a297d..e1ff69f0 100644 --- a/Sparkle/SPUProbeInstallStatus.m +++ b/Sparkle/SPUProbeInstallStatus.m @@ -30,7 +30,7 @@ id installerStatus = nil; BOOL usesXPC = SPUXPCServiceIsEnabled(SUEnableInstallerStatusServiceKey); if (!usesXPC) { - installerStatus = [[SUInstallerStatus alloc] init]; + installerStatus = [[SUInstallerStatus alloc] initWithRemote:NO]; } else { installerStatus = [[SUXPCInstallerStatus alloc] init]; } @@ -93,7 +93,7 @@ id installerStatus = nil; BOOL usesXPC = SPUXPCServiceIsEnabled(SUEnableInstallerStatusServiceKey); if (!usesXPC) { - installerStatus = [[SUInstallerStatus alloc] init]; + installerStatus = [[SUInstallerStatus alloc] initWithRemote:NO]; } else { installerStatus = [[SUXPCInstallerStatus alloc] init]; }