diff --git a/Configurations/ConfigCommon.xcconfig b/Configurations/ConfigCommon.xcconfig index b129d3a1..b1b5298f 100644 --- a/Configurations/ConfigCommon.xcconfig +++ b/Configurations/ConfigCommon.xcconfig @@ -39,13 +39,15 @@ INSTALLER_LAUNCHER_PRODUCT_NAME = SparkleInstallerLauncher TEMPORARY_DOWNLOADER_PRODUCT_NAME = SparkleTemporaryDownloader PERSISTENT_DOWNLOADER_PRODUCT_NAME = SparklePersistentDownloader REMOTE_MESSAGE_PORT_PRODUCT_NAME = SparkleRemoteMessagePort +LOCAL_MESSAGE_PORT_PRODUCT_NAME = SparkleLocalMessagePort INSTALLER_LAUNCHER_BUNDLE_ID = org.sparkle-project.${INSTALLER_LAUNCHER_PRODUCT_NAME} TEMPORARY_DOWNLOADER_BUNDLE_ID = org.sparkle-project.${TEMPORARY_DOWNLOADER_PRODUCT_NAME} PERSISTENT_DOWNLOADER_BUNDLE_ID = org.sparkle-project.${PERSISTENT_DOWNLOADER_PRODUCT_NAME} REMOTE_MESSAGE_PORT_BUNDLE_ID = org.sparkle-project.${REMOTE_MESSAGE_PORT_PRODUCT_NAME} +LOCAL_MESSAGE_PORT_BUNDLE_ID = org.sparkle-project.${LOCAL_MESSAGE_PORT_PRODUCT_NAME} -GCC_PREPROCESSOR_DEFINITIONS_COMMON = SPARKLE_NORMALIZE_INSTALLED_APPLICATION_NAME=$(SPARKLE_NORMALIZE_INSTALLED_APPLICATION_NAME) SPARKLE_AUTOMATED_DOWNGRADES=$(SPARKLE_AUTOMATED_DOWNGRADES) SPARKLE_APPEND_VERSION_NUMBER=$(SPARKLE_APPEND_VERSION_NUMBER) SPARKLE_BUNDLE_IDENTIFIER=\"$(SPARKLE_BUNDLE_IDENTIFIER)\" CURRENT_PROJECT_VERSION=\"$(CURRENT_PROJECT_VERSION)\" SPARKLE_RELAUNCH_TOOL_NAME=\"$(SPARKLE_RELAUNCH_TOOL_NAME)\" SPARKLE_INSTALLER_PROGRESS_TOOL_NAME=\"$(SPARKLE_INSTALLER_PROGRESS_TOOL_NAME)\" SPARKLE_ICON_NAME=\"$(SPARKLE_ICON_NAME)\" INSTALLER_LAUNCHER_BUNDLE_ID=\"${INSTALLER_LAUNCHER_BUNDLE_ID}\" TEMPORARY_DOWNLOADER_BUNDLE_ID=\"${TEMPORARY_DOWNLOADER_BUNDLE_ID}\" PERSISTENT_DOWNLOADER_BUNDLE_ID=\"${PERSISTENT_DOWNLOADER_BUNDLE_ID}\" REMOTE_MESSAGE_PORT_BUNDLE_ID=\"${REMOTE_MESSAGE_PORT_BUNDLE_ID}\" INSTALLER_LAUNCHER_PRODUCT_NAME=\"${INSTALLER_LAUNCHER_PRODUCT_NAME}\" TEMPORARY_DOWNLOADER_PRODUCT_NAME=\"${TEMPORARY_DOWNLOADER_PRODUCT_NAME}\" PERSISTENT_DOWNLOADER_PRODUCT_NAME=\"${PERSISTENT_DOWNLOADER_PRODUCT_NAME}\" REMOTE_MESSAGE_PORT_PRODUCT_NAME=\"${REMOTE_MESSAGE_PORT_PRODUCT_NAME}\" SPARKLE_FILEOP_TOOL_NAME=\"$(SPARKLE_FILEOP_TOOL_NAME)\" +GCC_PREPROCESSOR_DEFINITIONS_COMMON = SPARKLE_NORMALIZE_INSTALLED_APPLICATION_NAME=$(SPARKLE_NORMALIZE_INSTALLED_APPLICATION_NAME) SPARKLE_AUTOMATED_DOWNGRADES=$(SPARKLE_AUTOMATED_DOWNGRADES) SPARKLE_APPEND_VERSION_NUMBER=$(SPARKLE_APPEND_VERSION_NUMBER) SPARKLE_BUNDLE_IDENTIFIER=\"$(SPARKLE_BUNDLE_IDENTIFIER)\" CURRENT_PROJECT_VERSION=\"$(CURRENT_PROJECT_VERSION)\" SPARKLE_RELAUNCH_TOOL_NAME=\"$(SPARKLE_RELAUNCH_TOOL_NAME)\" SPARKLE_INSTALLER_PROGRESS_TOOL_NAME=\"$(SPARKLE_INSTALLER_PROGRESS_TOOL_NAME)\" SPARKLE_ICON_NAME=\"$(SPARKLE_ICON_NAME)\" INSTALLER_LAUNCHER_BUNDLE_ID=\"${INSTALLER_LAUNCHER_BUNDLE_ID}\" TEMPORARY_DOWNLOADER_BUNDLE_ID=\"${TEMPORARY_DOWNLOADER_BUNDLE_ID}\" PERSISTENT_DOWNLOADER_BUNDLE_ID=\"${PERSISTENT_DOWNLOADER_BUNDLE_ID}\" REMOTE_MESSAGE_PORT_BUNDLE_ID=\"${REMOTE_MESSAGE_PORT_BUNDLE_ID}\" LOCAL_MESSAGE_PORT_BUNDLE_ID=\"${LOCAL_MESSAGE_PORT_BUNDLE_ID}\" INSTALLER_LAUNCHER_PRODUCT_NAME=\"${INSTALLER_LAUNCHER_PRODUCT_NAME}\" TEMPORARY_DOWNLOADER_PRODUCT_NAME=\"${TEMPORARY_DOWNLOADER_PRODUCT_NAME}\" PERSISTENT_DOWNLOADER_PRODUCT_NAME=\"${PERSISTENT_DOWNLOADER_PRODUCT_NAME}\" REMOTE_MESSAGE_PORT_PRODUCT_NAME=\"${REMOTE_MESSAGE_PORT_PRODUCT_NAME}\" LOCAL_MESSAGE_PORT_PRODUCT_NAME=\"${LOCAL_MESSAGE_PORT_PRODUCT_NAME}\" SPARKLE_FILEOP_TOOL_NAME=\"$(SPARKLE_FILEOP_TOOL_NAME)\" CLANG_ENABLE_OBJC_ARC = YES GCC_ENABLE_PASCAL_STRINGS = NO diff --git a/Configurations/ConfigLocalMessagePort.xcconfig b/Configurations/ConfigLocalMessagePort.xcconfig new file mode 100644 index 00000000..e4557056 --- /dev/null +++ b/Configurations/ConfigLocalMessagePort.xcconfig @@ -0,0 +1,6 @@ +// LocalMessagePort + +INFOPLIST_FILE = LocalMessagePort/Info.plist +WRAPPER_EXTENSION = xpc +PRODUCT_BUNDLE_IDENTIFIER = ${LOCAL_MESSAGE_PORT_BUNDLE_ID} +PRODUCT_NAME = ${LOCAL_MESSAGE_PORT_PRODUCT_NAME} diff --git a/Configurations/ConfigLocalMessagePortDebug.xcconfig b/Configurations/ConfigLocalMessagePortDebug.xcconfig new file mode 100644 index 00000000..6b9fa638 --- /dev/null +++ b/Configurations/ConfigLocalMessagePortDebug.xcconfig @@ -0,0 +1,5 @@ +// LocalMessagePort Debug + +#include "ConfigLocalMessagePort.xcconfig" + +CODE_SIGN_IDENTITY = - diff --git a/LocalMessagePort/Info.plist b/LocalMessagePort/Info.plist new file mode 100644 index 00000000..5bdbec87 --- /dev/null +++ b/LocalMessagePort/Info.plist @@ -0,0 +1,31 @@ + + + + + CFBundleDevelopmentRegion + en + CFBundleExecutable + $(EXECUTABLE_NAME) + CFBundleIdentifier + $(PRODUCT_BUNDLE_IDENTIFIER) + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + $(PRODUCT_NAME) + CFBundlePackageType + XPC! + CFBundleShortVersionString + ${CURRENT_PROJECT_VERSION} + CFBundleSignature + ???? + CFBundleVersion + ${CURRENT_PROJECT_VERSION} + NSHumanReadableCopyright + Copyright © 2016 Sparkle Project. All rights reserved. + XPCService + + ServiceType + Application + + + diff --git a/LocalMessagePort/main.m b/LocalMessagePort/main.m new file mode 100644 index 00000000..0ca4cd8d --- /dev/null +++ b/LocalMessagePort/main.m @@ -0,0 +1,47 @@ +// +// main.m +// LocalMessagePort +// +// Created by Mayur Pawashe on 6/14/16. +// Copyright © 2016 Sparkle Project. All rights reserved. +// + +#import +#import "SULocalMessagePort.h" +#import "SULocalMessagePortDelegate.h" + +@interface ServiceDelegate : NSObject +@end + +@implementation ServiceDelegate + +- (BOOL)listener:(NSXPCListener *)__unused listener shouldAcceptNewConnection:(NSXPCConnection *)newConnection { + // This method is where the NSXPCListener configures, accepts, and resumes a new incoming NSXPCConnection. + + // Configure the connection. + newConnection.exportedInterface = [NSXPCInterface interfaceWithProtocol:@protocol(SULocalMessagePortProtocol)]; + newConnection.remoteObjectInterface = [NSXPCInterface interfaceWithProtocol:@protocol(SULocalMessagePortDelegate)]; + + newConnection.exportedObject = [[SULocalMessagePort alloc] initWithDelegate:newConnection.remoteObjectProxy]; + + // Resuming the connection allows the system to deliver more incoming messages. + [newConnection resume]; + + return YES; +} + +@end + +int main(int argc, const char *argv[]) +{ + // Create the delegate for the service. + ServiceDelegate *delegate = [ServiceDelegate new]; + + // Set up the one NSXPCListener for this service. It will handle all incoming connections. + NSXPCListener *listener = [NSXPCListener serviceListener]; + listener.delegate = delegate; + + // Resuming the serviceListener starts this service. This method does not return. + [listener resume]; + return 0; +} diff --git a/Sparkle.xcodeproj/project.pbxproj b/Sparkle.xcodeproj/project.pbxproj index 3b3aba40..1d64a953 100644 --- a/Sparkle.xcodeproj/project.pbxproj +++ b/Sparkle.xcodeproj/project.pbxproj @@ -124,6 +124,13 @@ 720E21781D0CED2B003A311C /* SUFileOperationConstants.m in Sources */ = {isa = PBXBuildFile; fileRef = 72E641A61D090F51000404CA /* SUFileOperationConstants.m */; }; 720E217B1D0D00BF003A311C /* SUUpdaterCycle.h in Headers */ = {isa = PBXBuildFile; fileRef = 720E21791D0D00BF003A311C /* SUUpdaterCycle.h */; }; 720E217C1D0D00BF003A311C /* SUUpdaterCycle.m in Sources */ = {isa = PBXBuildFile; fileRef = 720E217A1D0D00BF003A311C /* SUUpdaterCycle.m */; }; + 720E217E1D10EB04003A311C /* SULocalMessagePortProtocol.h in Headers */ = {isa = PBXBuildFile; fileRef = 720E217D1D10EB04003A311C /* SULocalMessagePortProtocol.h */; }; + 720E218A1D10EDB4003A311C /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 720E21891D10EDB4003A311C /* main.m */; }; + 720E218E1D10EDB4003A311C /* SparkleLocalMessagePort.xpc in Embed XPC Services */ = {isa = PBXBuildFile; fileRef = 720E21831D10EDB4003A311C /* SparkleLocalMessagePort.xpc */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; }; + 720E21941D10EE1F003A311C /* SULocalMessagePort.m in Sources */ = {isa = PBXBuildFile; fileRef = 7297463F1C9119F300FC134E /* SULocalMessagePort.m */; }; + 720E21991D10F4FC003A311C /* SUXPCLocalMessagePort.h in Headers */ = {isa = PBXBuildFile; fileRef = 720E21971D10F4FC003A311C /* SUXPCLocalMessagePort.h */; }; + 720E219A1D10F4FC003A311C /* SUXPCLocalMessagePort.m in Sources */ = {isa = PBXBuildFile; fileRef = 720E21981D10F4FC003A311C /* SUXPCLocalMessagePort.m */; }; + 720E219C1D11ED83003A311C /* SULocalMessagePortDelegate.h in Headers */ = {isa = PBXBuildFile; fileRef = 720E219B1D11ED83003A311C /* SULocalMessagePortDelegate.h */; }; 7210C7681B9A9A1500EB90AC /* SUUnarchiverTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7210C7671B9A9A1500EB90AC /* SUUnarchiverTest.swift */; }; 72162B061C82C8B30013C1C5 /* SUParameterAssert.h in Headers */ = {isa = PBXBuildFile; fileRef = 72162B051C82C8B30013C1C5 /* SUParameterAssert.h */; }; 72162B081C82C9600013C1C5 /* SULocalizations.h in Headers */ = {isa = PBXBuildFile; fileRef = 72162B071C82C9600013C1C5 /* SULocalizations.h */; }; @@ -413,6 +420,13 @@ remoteGlobalIDString = 8DC2EF4F0486A6940098B216; remoteInfo = Sparkle; }; + 720E218C1D10EDB4003A311C /* PBXContainerItemProxy */ = { + isa = PBXContainerItemProxy; + containerPortal = 0867D690FE84028FC02AAC07 /* Project object */; + proxyType = 1; + remoteGlobalIDString = 720E21821D10EDB3003A311C; + remoteInfo = LocalMessagePort; + }; 726B2B621C645FC900388755 /* PBXContainerItemProxy */ = { isa = PBXContainerItemProxy; containerPortal = 0867D690FE84028FC02AAC07 /* Project object */; @@ -557,6 +571,7 @@ 72F94F5E1CC455B4002DEE68 /* SparklePersistentDownloader.xpc in Embed XPC Services */, 72F94F5F1CC455B4002DEE68 /* SparkleTemporaryDownloader.xpc in Embed XPC Services */, 72F94F601CC455B4002DEE68 /* SparkleRemoteMessagePort.xpc in Embed XPC Services */, + 720E218E1D10EDB4003A311C /* SparkleLocalMessagePort.xpc in Embed XPC Services */, 726E4A211C86C88F00C57C6A /* TestAppHelper.xpc in Embed XPC Services */, ); name = "Embed XPC Services"; @@ -825,6 +840,15 @@ 720B16431C66433D006985FB /* SUTestApplicationTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SUTestApplicationTest.swift; path = UITests/SUTestApplicationTest.swift; sourceTree = SOURCE_ROOT; }; 720E21791D0D00BF003A311C /* SUUpdaterCycle.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SUUpdaterCycle.h; sourceTree = ""; }; 720E217A1D0D00BF003A311C /* SUUpdaterCycle.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SUUpdaterCycle.m; sourceTree = ""; }; + 720E217D1D10EB04003A311C /* SULocalMessagePortProtocol.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SULocalMessagePortProtocol.h; path = Sparkle/SULocalMessagePortProtocol.h; sourceTree = SOURCE_ROOT; }; + 720E21831D10EDB4003A311C /* SparkleLocalMessagePort.xpc */ = {isa = PBXFileReference; explicitFileType = "wrapper.xpc-service"; includeInIndex = 0; path = SparkleLocalMessagePort.xpc; sourceTree = BUILT_PRODUCTS_DIR; }; + 720E21891D10EDB4003A311C /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; + 720E218B1D10EDB4003A311C /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + 720E21951D10F1C4003A311C /* ConfigLocalMessagePort.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = ConfigLocalMessagePort.xcconfig; sourceTree = ""; }; + 720E21961D10F1C4003A311C /* ConfigLocalMessagePortDebug.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = ConfigLocalMessagePortDebug.xcconfig; sourceTree = ""; }; + 720E21971D10F4FC003A311C /* SUXPCLocalMessagePort.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SUXPCLocalMessagePort.h; path = Sparkle/SUXPCLocalMessagePort.h; sourceTree = SOURCE_ROOT; }; + 720E21981D10F4FC003A311C /* SUXPCLocalMessagePort.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = SUXPCLocalMessagePort.m; path = Sparkle/SUXPCLocalMessagePort.m; sourceTree = SOURCE_ROOT; }; + 720E219B1D11ED83003A311C /* SULocalMessagePortDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SULocalMessagePortDelegate.h; path = Sparkle/SULocalMessagePortDelegate.h; sourceTree = SOURCE_ROOT; }; 7210C7671B9A9A1500EB90AC /* SUUnarchiverTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SUUnarchiverTest.swift; sourceTree = ""; }; 72162B051C82C8B30013C1C5 /* SUParameterAssert.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SUParameterAssert.h; sourceTree = ""; }; 72162B071C82C9600013C1C5 /* SULocalizations.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SULocalizations.h; sourceTree = ""; }; @@ -1075,6 +1099,13 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + 720E21801D10EDB3003A311C /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; 721C24421CB753E6005440CB /* Frameworks */ = { isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; @@ -1173,6 +1204,7 @@ 721C24451CB753E6005440CB /* Updater.app */, 72D9549E1CBB415B006F28BD /* sparkle.app */, 72E6419B1D090F1C000404CA /* fileop */, + 720E21831D10EDB4003A311C /* SparkleLocalMessagePort.xpc */, ); name = Products; sourceTree = ""; @@ -1557,6 +1589,16 @@ name = "Update Control"; sourceTree = ""; }; + 720E21841D10EDB4003A311C /* LocalMessagePort */ = { + isa = PBXGroup; + children = ( + 720E21891D10EDB4003A311C /* main.m */, + 720E218B1D10EDB4003A311C /* Info.plist */, + ); + name = LocalMessagePort; + path = ../LocalMessagePort; + sourceTree = ""; + }; 721C24461CB753E6005440CB /* Installer Progress */ = { isa = PBXGroup; children = ( @@ -1658,8 +1700,12 @@ 729746481C91483B00FC134E /* IPC */ = { isa = PBXGroup; children = ( + 720E217D1D10EB04003A311C /* SULocalMessagePortProtocol.h */, 7297463E1C9119F300FC134E /* SULocalMessagePort.h */, 7297463F1C9119F300FC134E /* SULocalMessagePort.m */, + 720E21971D10F4FC003A311C /* SUXPCLocalMessagePort.h */, + 720E21981D10F4FC003A311C /* SUXPCLocalMessagePort.m */, + 720E219B1D11ED83003A311C /* SULocalMessagePortDelegate.h */, 72E41A381CB209CA003D47E0 /* SURemoteMessagePortProtocol.h */, 729746431C91212400FC134E /* SURemoteMessagePort.h */, 729746441C91212400FC134E /* SURemoteMessagePort.m */, @@ -1734,6 +1780,7 @@ 728638F41CAF5A5F00783084 /* TemporaryDownloader */, 726E07AE1CAF08D6001A286B /* InstallerLauncher */, 726E07F01CAF37BD001A286B /* PersistentDownloader */, + 720E21841D10EDB4003A311C /* LocalMessagePort */, 72E41A3F1CB20BA7003D47E0 /* RemoteMessagePort */, ); name = "XPC Services"; @@ -1766,6 +1813,8 @@ 72F94ED51CC3441A002DEE68 /* ConfigInstallerLauncherDebug.xcconfig */, 728638EE1CAF589C00783084 /* ConfigPersistentDownloader.xcconfig */, 72F94ED61CC344A7002DEE68 /* ConfigPersistentDownloaderDebug.xcconfig */, + 720E21951D10F1C4003A311C /* ConfigLocalMessagePort.xcconfig */, + 720E21961D10F1C4003A311C /* ConfigLocalMessagePortDebug.xcconfig */, 728639041CAF614000783084 /* ConfigTemporaryDownloader.xcconfig */, 72F94ED71CC344E4002DEE68 /* ConfigTemporaryDownloaderDebug.xcconfig */, 72E41A551CB22F44003D47E0 /* ConfigRemoteMessagePort.xcconfig */, @@ -1805,6 +1854,8 @@ 7229E1B61C97C91100CB50D0 /* SUUpdateDriver.h in Headers */, 729746401C9119F300FC134E /* SULocalMessagePort.h in Headers */, 61299B3609CB04E000B7442F /* Sparkle.h in Headers */, + 720E219C1D11ED83003A311C /* SULocalMessagePortDelegate.h in Headers */, + 720E217E1D10EB04003A311C /* SULocalMessagePortProtocol.h in Headers */, 61CFB3290E385186007A1735 /* Sparkle.pch in Headers */, 72F94F581CC44DE1002DEE68 /* SUXPCServiceInfo.h in Headers */, 72162B081C82C9600013C1C5 /* SULocalizations.h in Headers */, @@ -1832,6 +1883,7 @@ 72B767E61C9CFD7200A07552 /* SUAutomaticUpdateDriver.h in Headers */, 618FA5220DAE8E8A0026945C /* SUPackageInstaller.h in Headers */, 728337A61C9E6FF40085AA99 /* SUProbeInstallStatus.h in Headers */, + 720E21991D10F4FC003A311C /* SUXPCLocalMessagePort.h in Headers */, 72B767CA1C9B707000A07552 /* SUAppcastDriver.h in Headers */, 6102FE460E077FCE00F85D09 /* SUPipedUnarchiver.h in Headers */, 726E075C1CA3A6D6001A286B /* SUSecureCoding.h in Headers */, @@ -1972,6 +2024,7 @@ 72F94F621CC48669002DEE68 /* PBXTargetDependency */, 72F94F641CC48669002DEE68 /* PBXTargetDependency */, 72F94F661CC48669002DEE68 /* PBXTargetDependency */, + 720E218D1D10EDB4003A311C /* PBXTargetDependency */, 72F94F681CC48669002DEE68 /* PBXTargetDependency */, 726E4A201C86C88F00C57C6A /* PBXTargetDependency */, ); @@ -1980,6 +2033,23 @@ productReference = 61B5F90209C4CEE200B25A18 /* Sparkle Test App.app */; productType = "com.apple.product-type.application"; }; + 720E21821D10EDB3003A311C /* SparkleLocalMessagePort */ = { + isa = PBXNativeTarget; + buildConfigurationList = 720E218F1D10EDB4003A311C /* Build configuration list for PBXNativeTarget "SparkleLocalMessagePort" */; + buildPhases = ( + 720E217F1D10EDB3003A311C /* Sources */, + 720E21801D10EDB3003A311C /* Frameworks */, + 720E21811D10EDB3003A311C /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = SparkleLocalMessagePort; + productName = LocalMessagePort; + productReference = 720E21831D10EDB4003A311C /* SparkleLocalMessagePort.xpc */; + productType = "com.apple.product-type.xpc-service"; + }; 721C24441CB753E6005440CB /* Installer Progress */ = { isa = PBXNativeTarget; buildConfigurationList = 721C24561CB753E7005440CB /* Build configuration list for PBXNativeTarget "Installer Progress" */; @@ -2184,6 +2254,9 @@ }; }; }; + 720E21821D10EDB3003A311C = { + CreatedOnToolsVersion = 7.3.1; + }; 721C24441CB753E6005440CB = { CreatedOnToolsVersion = 7.3; }; @@ -2292,6 +2365,7 @@ 726E07EE1CAF37BD001A286B /* SparklePersistentDownloader */, 728638F21CAF5A5F00783084 /* SparkleTemporaryDownloader */, 72E41A3D1CB20BA7003D47E0 /* SparkleRemoteMessagePort */, + 720E21821D10EDB3003A311C /* SparkleLocalMessagePort */, 61B5F90109C4CEE200B25A18 /* Sparkle Test App */, 726E4A151C86C88F00C57C6A /* TestAppHelper */, 612279D80DB5470200AB99EA /* Sparkle Unit Tests */, @@ -2354,6 +2428,13 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + 720E21811D10EDB3003A311C /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; 721C24431CB753E6005440CB /* Resources */ = { isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; @@ -2628,6 +2709,15 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + 720E217F1D10EDB3003A311C /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 720E21941D10EE1F003A311C /* SULocalMessagePort.m in Sources */, + 720E218A1D10EDB4003A311C /* main.m in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; 721C24411CB753E6005440CB /* Sources */ = { isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; @@ -2757,6 +2847,7 @@ 726E07611CA4E9A5001A286B /* SUInstallationInputData.m in Sources */, 7275F9C21B5F1F2900B1D19E /* SUFileManager.m in Sources */, 72E41A541CB21125003D47E0 /* SUXPCRemoteMessagePort.m in Sources */, + 720E219A1D10F4FC003A311C /* SUXPCLocalMessagePort.m in Sources */, 725CB95B1C7121830064365A /* SUStandardUserDriver.m in Sources */, 61EF67560E25B58D00F754E0 /* SUHost.m in Sources */, 55C14F07136EF6DB00649790 /* SULog.m in Sources */, @@ -2844,6 +2935,11 @@ target = 8DC2EF4F0486A6940098B216 /* Sparkle */; targetProxy = 61FA528C0E2D9EB200EF58AD /* PBXContainerItemProxy */; }; + 720E218D1D10EDB4003A311C /* PBXTargetDependency */ = { + isa = PBXTargetDependency; + target = 720E21821D10EDB3003A311C /* SparkleLocalMessagePort */; + targetProxy = 720E218C1D10EDB4003A311C /* PBXContainerItemProxy */; + }; 726B2B631C645FC900388755 /* PBXTargetDependency */ = { isa = PBXTargetDependency; target = 61B5F90109C4CEE200B25A18 /* Sparkle Test App */; @@ -3129,6 +3225,7 @@ isa = XCBuildConfiguration; baseConfigurationReference = 72F94EDA1CC36C37002DEE68 /* ConfigTestAppDebug.xcconfig */; buildSettings = { + CODE_SIGN_IDENTITY = "-"; }; name = Coverage; }; @@ -3245,6 +3342,7 @@ isa = XCBuildConfiguration; baseConfigurationReference = 72F94EDA1CC36C37002DEE68 /* ConfigTestAppDebug.xcconfig */; buildSettings = { + CODE_SIGN_IDENTITY = "-"; }; name = Debug; }; @@ -3252,9 +3350,38 @@ isa = XCBuildConfiguration; baseConfigurationReference = FA1941CD0D94A70100DD942E /* ConfigTestApp.xcconfig */; buildSettings = { + CODE_SIGN_IDENTITY = "-"; }; name = Release; }; + 720E21901D10EDB4003A311C /* Debug */ = { + isa = XCBuildConfiguration; + baseConfigurationReference = 720E21961D10F1C4003A311C /* ConfigLocalMessagePortDebug.xcconfig */; + buildSettings = { + }; + name = Debug; + }; + 720E21911D10EDB4003A311C /* Coverage */ = { + isa = XCBuildConfiguration; + baseConfigurationReference = 720E21951D10F1C4003A311C /* ConfigLocalMessagePort.xcconfig */; + buildSettings = { + }; + name = Coverage; + }; + 720E21921D10EDB4003A311C /* Release */ = { + isa = XCBuildConfiguration; + baseConfigurationReference = 720E21951D10F1C4003A311C /* ConfigLocalMessagePort.xcconfig */; + buildSettings = { + }; + name = Release; + }; + 720E21931D10EDB4003A311C /* Release-HTTP */ = { + isa = XCBuildConfiguration; + baseConfigurationReference = 720E21951D10F1C4003A311C /* ConfigLocalMessagePort.xcconfig */; + buildSettings = { + }; + name = "Release-HTTP"; + }; 721C24531CB753E7005440CB /* Debug */ = { isa = XCBuildConfiguration; baseConfigurationReference = 721C24571CB754E8005440CB /* ConfigInstallerProgress.xcconfig */; @@ -3651,6 +3778,7 @@ isa = XCBuildConfiguration; baseConfigurationReference = FA1941CD0D94A70100DD942E /* ConfigTestApp.xcconfig */; buildSettings = { + CODE_SIGN_IDENTITY = "-"; }; name = "Release-HTTP"; }; @@ -3826,6 +3954,16 @@ defaultConfigurationIsVisible = 0; defaultConfigurationName = Release; }; + 720E218F1D10EDB4003A311C /* Build configuration list for PBXNativeTarget "SparkleLocalMessagePort" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 720E21901D10EDB4003A311C /* Debug */, + 720E21911D10EDB4003A311C /* Coverage */, + 720E21921D10EDB4003A311C /* Release */, + 720E21931D10EDB4003A311C /* Release-HTTP */, + ); + defaultConfigurationIsVisible = 0; + }; 721C24561CB753E7005440CB /* Build configuration list for PBXNativeTarget "Installer Progress" */ = { isa = XCConfigurationList; buildConfigurations = ( diff --git a/Sparkle/Autoupdate/AppInstaller.m b/Sparkle/Autoupdate/AppInstaller.m index 648c9ec7..030e1c3e 100644 --- a/Sparkle/Autoupdate/AppInstaller.m +++ b/Sparkle/Autoupdate/AppInstaller.m @@ -105,24 +105,23 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7; _inheritsPrivileges = inheritsPrivileges; - self.localPort = - [[SULocalMessagePort alloc] - initWithServiceName:SUAutoUpdateServiceNameForBundleIdentifier(hostBundleIdentifier) - messageCallback:^NSData *(int32_t identifier, NSData * _Nonnull data) { - return [self handleMessageWithIdentifier:identifier data:data]; - } - invalidationCallback:^{ - dispatch_async(dispatch_get_main_queue(), ^{ - if (self.localPort != nil) { - [self cleanupAndExitWithStatus:EXIT_FAILURE]; - } - }); - }]; + self.localPort = [[SULocalMessagePort alloc] init]; - if (self.localPort == nil) { - SULog(@"Failed creating local message port from installer"); - [self cleanupAndExitWithStatus:EXIT_FAILURE]; - } + [self.localPort setServiceName:SUAutoUpdateServiceNameForBundleIdentifier(hostBundleIdentifier)]; + + __weak AppInstaller *weakSelf = self; + [self.localPort setMessageCallback:^NSData * _Nullable(int32_t identifier, NSData * _Nonnull data) { + return [weakSelf handleMessageWithIdentifier:identifier data:data]; + }]; + + [self.localPort setInvalidationCallback:^{ + dispatch_async(dispatch_get_main_queue(), ^{ + AppInstaller *strongSelf = weakSelf; + if (strongSelf.localPort != nil) { + [strongSelf cleanupAndExitWithStatus:EXIT_FAILURE]; + } + }); + }]; [self startRemotePortWithCompletion:^(BOOL success) { if (!success) { diff --git a/Sparkle/SUInstallerDriver.m b/Sparkle/SUInstallerDriver.m index 44298c3f..7df8f98a 100644 --- a/Sparkle/SUInstallerDriver.m +++ b/Sparkle/SUInstallerDriver.m @@ -10,6 +10,8 @@ #import "SULog.h" #import "SUMessageTypes.h" #import "SULocalMessagePort.h" +#import "SUXPCLocalMessagePort.h" +#import "SULocalMessagePortDelegate.h" #import "SURemoteMessagePort.h" #import "SUXPCRemoteMessagePort.h" #import "SUXPCServiceInfo.h" @@ -30,7 +32,7 @@ #error This is a "core" class and should NOT import AppKit #endif -@interface SUInstallerDriver () +@interface SUInstallerDriver () @property (nonatomic, readonly) SUHost *host; @property (nonatomic, readonly, copy) NSString *cachePath; @@ -38,7 +40,7 @@ @property (nonatomic, weak, readonly) id delegate; @property (nonatomic) SUInstallerMessageType currentStage; @property (nonatomic) BOOL startedInstalling; -@property (nonatomic) SULocalMessagePort *localPort; +@property (nonatomic) id localPort; @property (nonatomic) id remotePort; @property (nonatomic) BOOL postponedOnce; @property (nonatomic, weak, readonly) id updater; @@ -83,57 +85,50 @@ return self; } -- (BOOL)setUpLocalPort:(NSError * __autoreleasing *)error +- (void)localMessagePortReceivedMessageWithIdentifier:(int32_t)identifier data:(NSData *)data +{ + dispatch_async(dispatch_get_main_queue(), ^{ + [self handleMessageWithIdentifier:identifier data:data]; + }); +} + +- (void)setUpLocalPort { if (self.localPort != nil) { - return YES; + return; } NSString *hostBundleIdentifier = self.host.bundle.bundleIdentifier; assert(hostBundleIdentifier != nil); - self.localPort = - [[SULocalMessagePort alloc] - initWithServiceName:SUUpdateDriverServiceNameForBundleIdentifier(hostBundleIdentifier) - messageCallback:^NSData *(int32_t identifier, NSData * _Nonnull data) { - dispatch_async(dispatch_get_main_queue(), ^{ - [self handleMessageWithIdentifier:identifier data:data]; - }); - return nil; - } - invalidationCallback:^{ - dispatch_async(dispatch_get_main_queue(), ^{ - if (self.localPort != nil) { - NSError *invalidationError = - [NSError - errorWithDomain:SUSparkleErrorDomain - code:SUInstallationError - userInfo:@{ - NSLocalizedDescriptionKey: SULocalizedString(@"An error occurred while running the updater. Please try again later.", nil), - NSLocalizedFailureReasonErrorKey:@"The local port connection from the updater was invalidated" - } - ]; - - [self.delegate installerIsRequestingAbortInstallWithError:invalidationError]; - } - }); - }]; + __weak SUInstallerDriver *weakSelf = self; - if (self.localPort == nil) { - if (error != NULL) { - *error = - [NSError - errorWithDomain:SUSparkleErrorDomain - code:SUInstallationError - userInfo:@{ - NSLocalizedDescriptionKey: SULocalizedString(@"An error occurred while running the updater. Please try again later.", nil), - NSLocalizedFailureReasonErrorKey:@"The local port connection failed being created" - } - ]; - } - return NO; + if (SUXPCServiceExists(@LOCAL_MESSAGE_PORT_PRODUCT_NAME)) { + self.localPort = [[SUXPCLocalMessagePort alloc] initWithDelegate:self]; + } else { + self.localPort = [[SULocalMessagePort alloc] initWithDelegate:self]; } - return YES; + + [self.localPort setServiceName:SUUpdateDriverServiceNameForBundleIdentifier(hostBundleIdentifier)]; + + [self.localPort setInvalidationCallback:^{ + dispatch_async(dispatch_get_main_queue(), ^{ + SUInstallerDriver *strongSelf = weakSelf; + if (strongSelf.localPort != nil) { + NSError *invalidationError = + [NSError + errorWithDomain:SUSparkleErrorDomain + code:SUInstallationError + userInfo:@{ + NSLocalizedDescriptionKey: SULocalizedString(@"An error occurred while running the updater. Please try again later.", nil), + NSLocalizedFailureReasonErrorKey:@"The local port connection from the updater was invalidated" + } + ]; + + [strongSelf.delegate installerIsRequestingAbortInstallWithError:invalidationError]; + } + }); + }]; } - (void)setUpRemotePortWithCompletion:(void (^)(BOOL))completionHandler @@ -184,11 +179,7 @@ - (void)launchInstallToolWithCompletion:(void (^)(NSError * _Nullable ))completionHandler { - NSError *localPortError = nil; - if (![self setUpLocalPort:&localPortError]) { - completionHandler(localPortError); - } - + [self setUpLocalPort]; [self launchAutoUpdateWithCompletion:completionHandler]; } @@ -580,12 +571,7 @@ } // Set up local and remote ports to the installer if they are not set up already - - NSError *localPortError = nil; - if (![self setUpLocalPort:&localPortError]) { - [self.delegate installerIsRequestingAbortInstallWithError:[NSError errorWithDomain:SUSparkleErrorDomain code:SUInstallationError userInfo:@{ NSLocalizedDescriptionKey:SULocalizedString(@"An error occurred while starting the local port for the installer. Please try again later.", nil) }]]; - return; - } + [self setUpLocalPort]; __weak SUInstallerDriver *weakSelf = self; [self setUpRemotePortWithCompletion:^(BOOL setupSuccess) { diff --git a/Sparkle/SULocalMessagePort.h b/Sparkle/SULocalMessagePort.h index 427ef6ad..f3d18519 100644 --- a/Sparkle/SULocalMessagePort.h +++ b/Sparkle/SULocalMessagePort.h @@ -7,15 +7,20 @@ // #import +#import "SULocalMessagePortProtocol.h" +#import "SULocalMessagePortDelegate.h" NS_ASSUME_NONNULL_BEGIN -@interface SULocalMessagePort : NSObject +@interface SULocalMessagePort : NSObject -// Note: messageCallback may not be called on the main thread -- (nullable instancetype)initWithServiceName:(NSString *)serviceName messageCallback:(NSData *(^)(int32_t identifier, NSData *data))messageCallback invalidationCallback:(void (^)(void))invalidationCallback; +- (instancetype)init; -- (void)invalidate; +// Due to XPC reasons, this delegate is strongly referenced. Make sure to -invalidate when done with this instance. +- (instancetype)initWithDelegate:(id)delegate; + +// messageCallback may be invoked on non-main thread +- (void)setMessageCallback:(NSData * _Nullable(^)(int32_t identifier, NSData *data))messageCallback; @end diff --git a/Sparkle/SULocalMessagePort.m b/Sparkle/SULocalMessagePort.m index 66faa5a7..4a6b0b1b 100644 --- a/Sparkle/SULocalMessagePort.m +++ b/Sparkle/SULocalMessagePort.m @@ -11,8 +11,9 @@ @interface SULocalMessagePort () @property (nonatomic) CFMessagePortRef messagePort; -@property (nonatomic, copy) NSData *(^messageCallback)(int32_t, NSData *); -@property (nonatomic, copy) void (^invalidationCallback)(void); +@property (nonatomic, nullable) id delegate; +@property (nonatomic, copy) NSData * _Nullable(^messageHandler)(int32_t, NSData *); +@property (nonatomic, copy) void (^invalidationBlock)(void); @property (nonatomic, readonly) dispatch_queue_t messageQueue; @end @@ -20,44 +21,83 @@ @implementation SULocalMessagePort @synthesize messagePort = _messagePort; -@synthesize messageCallback = _messageCallback; -@synthesize invalidationCallback = _invalidationCallback; +@synthesize delegate = _delegate; +@synthesize messageHandler = _messageHandler; +@synthesize invalidationBlock = _invalidationBlock; @synthesize messageQueue = _messageQueue; -- (nullable instancetype)initWithServiceName:(NSString *)serviceName messageCallback:(NSData *(^)(int32_t identifier, NSData *data))messageCallback invalidationCallback:(void (^)(void))invalidationCallback +- (instancetype)init { self = [super init]; if (self != nil) { - CFMessagePortContext context = {.version = 0, .info = (void *)CFBridgingRetain(self), .retain = NULL, .release = NULL, .copyDescription = NULL}; - CFMessagePortRef messagePort = CFMessagePortCreateLocal(kCFAllocatorDefault, (CFStringRef)serviceName, messagePortCallback, &context, NULL); - - if (messagePort == NULL) { - CFRelease((__bridge CFTypeRef)(self)); - return nil; - } - - _messagePort = messagePort; - _messageCallback = [messageCallback copy]; - _invalidationCallback = [invalidationCallback copy]; - _messageQueue = dispatch_queue_create("org.sparkle-project.sparkle-local-port", DISPATCH_QUEUE_SERIAL); - - CFMessagePortSetDispatchQueue(messagePort, _messageQueue); - CFMessagePortSetInvalidationCallBack(messagePort, messageInvalidationCallback); } return self; } +- (instancetype)initWithDelegate:(id)delegate +{ + self = [self init]; + if (self != nil) { + _delegate = delegate; + } + return self; +} + +- (void)setServiceName:(NSString *)serviceName +{ + assert(self.messagePort == NULL); + + CFMessagePortContext context = {.version = 0, .info = (void *)CFBridgingRetain(self), .retain = NULL, .release = NULL, .copyDescription = NULL}; + CFMessagePortRef messagePort = CFMessagePortCreateLocal(kCFAllocatorDefault, (CFStringRef)serviceName, messagePortCallback, &context, NULL); + + if (messagePort == NULL) { + CFRelease((__bridge CFTypeRef)(self)); + } else { + self.messagePort = messagePort; + CFMessagePortSetDispatchQueue(messagePort, self.messageQueue); + } +} + +- (void)setMessageCallback:(NSData * _Nullable(^)(int32_t identifier, NSData *data))messageCallback +{ + self.messageHandler = messageCallback; +} + +- (void)setInvalidationCallback:(void (^)(void))invalidationCallback +{ + if (self.messagePort == NULL) { + invalidationCallback(); + } else { + self.invalidationBlock = [invalidationCallback copy]; + CFMessagePortSetInvalidationCallBack(self.messagePort, messageInvalidationCallback); + } +} + - (void)invalidate { dispatch_async(self.messageQueue, ^{ - if (self.invalidationCallback != nil) { - self.invalidationCallback = nil; + if (self.invalidationBlock != nil) { + self.invalidationBlock = nil; CFMessagePortInvalidate(self.messagePort); + } else { + [self removeDelegate]; } }); } +- (void)removeDelegate +{ + // Remove our delegate right away but make sure it is released on main thread + __block id delegate = self.delegate; + if (delegate != nil) { + self.delegate = nil; + dispatch_async(dispatch_get_main_queue(), ^{ + delegate = nil; + }); + } +} + // Called on non-main thread static CFDataRef messagePortCallback(CFMessagePortRef __unused messagePort, SInt32 messageID, CFDataRef dataRef, void *info) { @@ -69,8 +109,10 @@ static CFDataRef messagePortCallback(CFMessagePortRef __unused messagePort, SInt NSData *data = [NSData dataWithBytes:CFDataGetBytePtr(dataRef) length:(NSUInteger)CFDataGetLength(dataRef)]; NSData *replyData = nil; - if (self.messageCallback != nil) { - replyData = self.messageCallback(messageID, data); + if (self.delegate != nil) { + [self.delegate localMessagePortReceivedMessageWithIdentifier:messageID data:data]; + } else if (self.messageHandler != nil) { + replyData = self.messageHandler(messageID, data); } return (CFDataRef)CFBridgingRetain(replyData); @@ -84,12 +126,14 @@ static void messageInvalidationCallback(CFMessagePortRef messagePort, void *info SULocalMessagePort *self = (__bridge SULocalMessagePort *)info; dispatch_async(self.messageQueue, ^{ - // note that messageCallback is deallocated on same queue that we're receiving messages from, which is good - self.messageCallback = nil; + [self removeDelegate]; - if (self.invalidationCallback != nil) { - self.invalidationCallback(); - self.invalidationCallback = nil; + // note that messageHandler is deallocated on same queue that we're receiving messages from, which is good + self.messageHandler = nil; + + if (self.invalidationBlock != nil) { + self.invalidationBlock(); + self.invalidationBlock = nil; } self.messagePort = NULL; diff --git a/Sparkle/SULocalMessagePortDelegate.h b/Sparkle/SULocalMessagePortDelegate.h new file mode 100644 index 00000000..818bf486 --- /dev/null +++ b/Sparkle/SULocalMessagePortDelegate.h @@ -0,0 +1,21 @@ +// +// SULocalMessagePortDelegate.h +// Sparkle +// +// Created by Mayur Pawashe on 6/15/16. +// Copyright © 2016 Sparkle Project. All rights reserved. +// + +#import + +NS_ASSUME_NONNULL_BEGIN + +@protocol SULocalMessagePortDelegate + +// Note: this method may not be called on the main thread +// Due to XPC/asynchronous reasons, this cannot return any reply data back +- (void)localMessagePortReceivedMessageWithIdentifier:(int32_t)identifier data:(NSData *)data; + +@end + +NS_ASSUME_NONNULL_END diff --git a/Sparkle/SULocalMessagePortProtocol.h b/Sparkle/SULocalMessagePortProtocol.h new file mode 100644 index 00000000..1236b01b --- /dev/null +++ b/Sparkle/SULocalMessagePortProtocol.h @@ -0,0 +1,23 @@ +// +// SULocalMessagePortProtocol.h +// Sparkle +// +// Created by Mayur Pawashe on 6/14/16. +// Copyright © 2016 Sparkle Project. All rights reserved. +// + +#import + +NS_ASSUME_NONNULL_BEGIN + +@protocol SULocalMessagePortProtocol + +- (void)setServiceName:(NSString *)serviceName; + +- (void)setInvalidationCallback:(void (^)(void))invalidationCallback; + +- (void)invalidate; + +@end + +NS_ASSUME_NONNULL_END diff --git a/Sparkle/SUXPCLocalMessagePort.h b/Sparkle/SUXPCLocalMessagePort.h new file mode 100644 index 00000000..bc4464a3 --- /dev/null +++ b/Sparkle/SUXPCLocalMessagePort.h @@ -0,0 +1,22 @@ +// +// SUXPCLocalMessagePort.h +// Sparkle +// +// Created by Mayur Pawashe on 6/14/16. +// Copyright © 2016 Sparkle Project. All rights reserved. +// + +#import +#import "SULocalMessagePortProtocol.h" +#import "SULocalMessagePortDelegate.h" + +NS_ASSUME_NONNULL_BEGIN + +@interface SUXPCLocalMessagePort : NSObject + +// Due to XPC reasons, this delegate is strongly referenced. Make sure to -invalidate when done with this instance. +- (instancetype)initWithDelegate:(id)delegate; + +@end + +NS_ASSUME_NONNULL_END diff --git a/Sparkle/SUXPCLocalMessagePort.m b/Sparkle/SUXPCLocalMessagePort.m new file mode 100644 index 00000000..a05ecc8c --- /dev/null +++ b/Sparkle/SUXPCLocalMessagePort.m @@ -0,0 +1,84 @@ +// +// SUXPCLocalMessagePort.m +// Sparkle +// +// Created by Mayur Pawashe on 6/14/16. +// Copyright © 2016 Sparkle Project. All rights reserved. +// + +#import "SUXPCLocalMessagePort.h" +#import "SULog.h" + +@interface SUXPCLocalMessagePort () + +@property (nonatomic, readonly) NSXPCConnection *connection; +@property (nonatomic) id delegate; +@property (nonatomic, copy) void (^invalidationHandler)(void); + +@end + +@implementation SUXPCLocalMessagePort + +@synthesize connection = _connection; +@synthesize delegate = _delegate; +@synthesize invalidationHandler = _invalidationHandler; + +- (instancetype)initWithDelegate:(id)delegate +{ + self = [self init]; + if (self != nil) { + _connection = [[NSXPCConnection alloc] initWithServiceName:@LOCAL_MESSAGE_PORT_BUNDLE_ID]; + _connection.remoteObjectInterface = [NSXPCInterface interfaceWithProtocol:@protocol(SULocalMessagePortProtocol)]; + + __weak SUXPCLocalMessagePort *weakSelf = self; + _connection.invalidationHandler = ^{ + [weakSelf invokeInvalidation]; + }; + + _connection.interruptionHandler = ^{ + SULog(@"LocalMessagePort service interrupted"); + [weakSelf invokeInvalidation]; + [weakSelf.connection invalidate]; + }; + + _delegate = delegate; + + _connection.exportedInterface = [NSXPCInterface interfaceWithProtocol:@protocol(SULocalMessagePortDelegate)]; + _connection.exportedObject = _delegate; + + [_connection resume]; + } + return self; +} + +- (void)setServiceName:(NSString *)serviceName +{ + [self.connection.remoteObjectProxy setServiceName:serviceName]; +} + +- (void)invokeInvalidation +{ + if (self.invalidationHandler != nil) { + self.invalidationHandler(); + self.invalidationHandler = nil; + } + self.delegate = nil; +} + +- (void)setInvalidationCallback:(void (^)(void))invalidationCallback +{ + self.invalidationHandler = [invalidationCallback copy]; + + __weak SUXPCLocalMessagePort *weakSelf = self; + [self.connection.remoteObjectProxy setInvalidationCallback:^{ + [weakSelf invokeInvalidation]; + }]; +} + +- (void)invalidate +{ + [self.connection.remoteObjectProxy invalidate]; + [self.connection invalidate]; +} + +@end