From 11bbf464a4243abba122cf5bb14428a56465bf49 Mon Sep 17 00:00:00 2001 From: Boris Zbarsky Date: Mon, 9 Sep 2024 17:35:56 -0400 Subject: [PATCH] Move management of uniqueIdentifier into MTRDeviceController base class. --- src/darwin/Framework/CHIP/MTRDeviceController.mm | 9 ++++++++- .../Framework/CHIP/MTRDeviceController_Concrete.mm | 3 +-- .../Framework/CHIP/MTRDeviceController_Internal.h | 5 +---- src/darwin/Framework/CHIP/MTRDeviceController_XPC.mm | 11 +++++------ 4 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/darwin/Framework/CHIP/MTRDeviceController.mm b/src/darwin/Framework/CHIP/MTRDeviceController.mm index 5cee77cb663bb8..b99c85b77fae80 100644 --- a/src/darwin/Framework/CHIP/MTRDeviceController.mm +++ b/src/darwin/Framework/CHIP/MTRDeviceController.mm @@ -87,6 +87,10 @@ - (instancetype)initWithDelegate:(id)delegate queue } @end +@interface MTRDeviceController () +@property (readwrite, nonatomic) NSUUID * uniqueIdentifier; +@end + @implementation MTRDeviceController { os_unfair_lock _underlyingDeviceMapLock; @@ -107,11 +111,14 @@ - (os_unfair_lock_t)deviceMapLock return &_underlyingDeviceMapLock; } -- (instancetype)initForSubclasses:(BOOL)startSuspended +- (instancetype)initForSubclasses:(BOOL)startSuspended uniqueIdentifier:(NSUUID *)uniqueIdentifier { if (self = [super init]) { // nothing, as superclass of MTRDeviceController is NSObject } + + self.uniqueIdentifier = uniqueIdentifier; + _underlyingDeviceMapLock = OS_UNFAIR_LOCK_INIT; _suspended = startSuspended; diff --git a/src/darwin/Framework/CHIP/MTRDeviceController_Concrete.mm b/src/darwin/Framework/CHIP/MTRDeviceController_Concrete.mm index 4dfffcbb8907b3..be0b93ed34ddaf 100644 --- a/src/darwin/Framework/CHIP/MTRDeviceController_Concrete.mm +++ b/src/darwin/Framework/CHIP/MTRDeviceController_Concrete.mm @@ -173,10 +173,9 @@ - (nullable instancetype)initWithFactory:(MTRDeviceControllerFactory *)factory storageBehaviorConfiguration:(MTRDeviceStorageBehaviorConfiguration *)storageBehaviorConfiguration startSuspended:(BOOL)startSuspended { - if (self = [super initForSubclasses:startSuspended]) { + if (self = [super initForSubclasses:startSuspended uniqueIdentifier:uniqueIdentifier]) { // Make sure our storage is all set up to work as early as possible, // before we start doing anything else with the controller. - self.uniqueIdentifier = uniqueIdentifier; // Setup assertion variables _keepRunningAssertionCounter = 0; diff --git a/src/darwin/Framework/CHIP/MTRDeviceController_Internal.h b/src/darwin/Framework/CHIP/MTRDeviceController_Internal.h index 8fc93b6235015c..7512e86f2f836b 100644 --- a/src/darwin/Framework/CHIP/MTRDeviceController_Internal.h +++ b/src/darwin/Framework/CHIP/MTRDeviceController_Internal.h @@ -51,10 +51,7 @@ NS_ASSUME_NONNULL_BEGIN @property (nonatomic, readonly) NSMapTable * nodeIDToDeviceMap; @property (readonly, assign) os_unfair_lock_t deviceMapLock; -@property (readwrite, nonatomic) NSUUID * uniqueIdentifier; -// (moved here so subclasses can initialize differently) - -- (instancetype)initForSubclasses:(BOOL)startSuspended; +- (instancetype)initForSubclasses:(BOOL)startSuspended uniqueIdentifier:(NSUUID *)uniqueIdentifier; #pragma mark - MTRDeviceControllerFactory methods diff --git a/src/darwin/Framework/CHIP/MTRDeviceController_XPC.mm b/src/darwin/Framework/CHIP/MTRDeviceController_XPC.mm index 9b9a87bb97a5cc..7770e63a5fd78b 100644 --- a/src/darwin/Framework/CHIP/MTRDeviceController_XPC.mm +++ b/src/darwin/Framework/CHIP/MTRDeviceController_XPC.mm @@ -288,10 +288,11 @@ - (nullable instancetype)initWithParameters:(MTRDeviceControllerAbstractParamete return nil; } - if (self = [super initForSubclasses:parameters.startSuspended]) { - auto * xpcParameters = static_cast(parameters); + auto * xpcParameters = static_cast(parameters); + auto * UUID = xpcParameters.uniqueIdentifier; + + if (self = [super initForSubclasses:parameters.startSuspended uniqueIdentifier:UUID]) { auto connectionBlock = xpcParameters.xpcConnectionBlock; - auto * UUID = xpcParameters.uniqueIdentifier; MTR_LOG("Setting up XPC Controller for UUID: %@ with connection block: %p", UUID, connectionBlock); @@ -304,7 +305,6 @@ - (nullable instancetype)initWithParameters:(MTRDeviceControllerAbstractParamete return nil; } - self.uniqueIdentifier = UUID; self.xpcParameters = xpcParameters; _workQueue = dispatch_queue_create("MTRDeviceController_XPC_queue", DISPATCH_QUEUE_SERIAL_WITH_AUTORELEASE_POOL); @@ -321,10 +321,9 @@ - (id)initWithUniqueIdentifier:(NSUUID *)UUID machServiceName:(NSString *)machSe { // TODO: Presumably this should end up doing some sort of // MTRDeviceControllerAbstractParameters thing eventually? - if (self = [super initForSubclasses:NO]) { + if (self = [super initForSubclasses:NO uniqueIdentifier:UUID]) { MTR_LOG("Setting up XPC Controller for UUID: %@ with machServiceName: %s options: %d", UUID, machServiceName, options); self.xpcConnection = [[NSXPCConnection alloc] initWithMachServiceName:machServiceName options:options]; - self.uniqueIdentifier = UUID; MTR_LOG("Set up XPC Connection: %@", self.xpcConnection); if (self.xpcConnection) {