Skip to content

Commit 546a3ca

Browse files
committedFeb 17, 2025
Only registering when a delegate is added
1 parent 7c1d6f7 commit 546a3ca

6 files changed

+42
-13
lines changed
 

‎src/darwin/Framework/CHIP/MTRDevice.mm

+14-4
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,10 @@ - (void)_addDelegate:(id<MTRDeviceDelegate>)delegate queue:(dispatch_queue_t)que
201201
MTR_LOG("%@ added delegate info %@", self, newDelegateInfo);
202202

203203
// Call hook to allow subclasses to act on delegate addition.
204-
[self _delegateAdded];
204+
[self _delegateAdded: delegate];
205205
}
206206

207-
- (void)_delegateAdded
207+
- (void)_delegateAdded:(id<MTRDeviceDelegate>)delegate
208208
{
209209
os_unfair_lock_assert_owner(&self->_lock);
210210

@@ -214,9 +214,9 @@ - (void)_delegateAdded
214214
- (void)removeDelegate:(id<MTRDeviceDelegate>)delegate
215215
{
216216
MTR_LOG("%@ removeDelegate %@", self, delegate);
217-
217+
218218
std::lock_guard lock(_lock);
219-
219+
220220
NSMutableSet<MTRDeviceDelegateInfo *> * delegatesToRemove = [NSMutableSet set];
221221
[self _iterateDelegatesWithBlock:^(MTRDeviceDelegateInfo * delegateInfo) {
222222
id<MTRDeviceDelegate> strongDelegate = delegateInfo.delegate;
@@ -233,6 +233,16 @@ - (void)removeDelegate:(id<MTRDeviceDelegate>)delegate
233233
[_delegates minusSet:delegatesToRemove];
234234
MTR_LOG("%@ removeDelegate: removed %lu", self, static_cast<unsigned long>(_delegates.count - oldDelegatesCount));
235235
}
236+
237+
// Call hook to allow subclasses to act on delegate addition.
238+
[self _delegateRemoved: delegate];
239+
}
240+
241+
- (void)_delegateRemoved:(id<MTRDeviceDelegate>)delegate
242+
{
243+
os_unfair_lock_assert_owner(&self->_lock);
244+
245+
// Nothing to do for now. At the moment this is a hook for subclasses.
236246
}
237247

238248
- (void)invalidate

‎src/darwin/Framework/CHIP/MTRDeviceController_XPC.mm

+8-6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#import "MTRDefines_Internal.h"
2020
#import "MTRDeviceController_Internal.h"
21+
#import "MTRDevice_Internal.h"
2122
#import "MTRDevice_XPC.h"
2223
#import "MTRDevice_XPC_Internal.h"
2324
#import "MTRError_Internal.h"
@@ -77,10 +78,13 @@ - (void)_updateRegistrationInfo
7778
NSMutableArray * nodeIDs = [NSMutableArray array];
7879

7980
for (NSNumber * nodeID in [self.nodeIDToDeviceMap keyEnumerator]) {
80-
NSMutableDictionary * nodeDictionary = [NSMutableDictionary dictionary];
81-
MTR_REQUIRED_ATTRIBUTE(MTRDeviceControllerRegistrationNodeIDKey, nodeID, nodeDictionary)
82-
83-
[nodeIDs addObject:nodeDictionary];
81+
MTRDevice * device = [self _deviceForNodeID: nodeID createIfNeeded: NO];
82+
if ( [device _delegateExists] ) {
83+
NSMutableDictionary * nodeDictionary = [NSMutableDictionary dictionary];
84+
MTR_REQUIRED_ATTRIBUTE(MTRDeviceControllerRegistrationNodeIDKey, nodeID, nodeDictionary)
85+
86+
[nodeIDs addObject:nodeDictionary];
87+
}
8488
}
8589
MTR_REQUIRED_ATTRIBUTE(MTRDeviceControllerRegistrationNodeIDsKey, nodeIDs, registrationInfo)
8690
MTR_REQUIRED_ATTRIBUTE(MTRDeviceControllerRegistrationControllerContextKey, controllerContext, registrationInfo)
@@ -386,8 +390,6 @@ - (MTRDevice *)_setupDeviceForNodeID:(NSNumber *)nodeID prefetchedClusterData:(N
386390
[self.nodeIDToDeviceMap setObject:deviceToReturn forKey:nodeID];
387391
MTR_LOG("%s: returning XPC device for node id %@", __PRETTY_FUNCTION__, nodeID);
388392

389-
[self _updateRegistrationInfo];
390-
391393
return deviceToReturn;
392394
}
393395

‎src/darwin/Framework/CHIP/MTRDeviceController_XPC_Internal.h

+1
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@
1919
@interface MTRDeviceController_XPC (Internal)
2020

2121
- (id)initWithMachServiceName:(NSString *)machServiceName options:(NSXPCConnectionOptions)options;
22+
- (void)_updateRegistrationInfo;
2223

2324
@end

‎src/darwin/Framework/CHIP/MTRDevice_Concrete.mm

+2-2
Original file line numberDiff line numberDiff line change
@@ -884,11 +884,11 @@ - (BOOL)_subscriptionsAllowed
884884
return self.suspended == NO && ![_deviceController isKindOfClass:MTRDeviceControllerOverXPC.class];
885885
}
886886

887-
- (void)_delegateAdded
887+
- (void)_delegateAdded:(id<MTRDeviceDelegate>)delegate
888888
{
889889
os_unfair_lock_assert_owner(&self->_lock);
890890

891-
[super _delegateAdded];
891+
[super _delegateAdded: delegate];
892892

893893
[self _ensureSubscriptionForExistingDelegates:@"delegate is set"];
894894
}

‎src/darwin/Framework/CHIP/MTRDevice_Internal.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ MTR_DIRECT_MEMBERS
149149
- (BOOL)_delegateExists;
150150

151151
// Must be called by subclasses or MTRDevice implementation only.
152-
- (void)_delegateAdded;
152+
- (void)_delegateAdded:(id<MTRDeviceDelegate>)delegate;
153+
- (void)_delegateRemoved:(id<MTRDeviceDelegate>)delegate;
153154

154155
#ifdef DEBUG
155156
// Only used for unit test purposes - normal delegate should not expect or handle being called back synchronously

‎src/darwin/Framework/CHIP/MTRDevice_XPC.mm

+15
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#import <Matter/MTRDeviceControllerParameters.h>
2323

2424
#import "MTRDeviceController_Internal.h"
25+
#import "MTRDeviceController_XPC_Internal.h"
2526

2627
#import "MTRAsyncWorkQueue.h"
2728
#import "MTRAttestationTrustStoreBridge.h"
@@ -146,6 +147,20 @@ - (MTRNetworkCommissioningFeature)networkCommissioningFeatures
146147
return [[self._internalState objectForKey:kMTRDeviceInternalPropertyNetworkFeatures] unsignedIntValue];
147148
}
148149

150+
#pragma mark - Delegate added/removed callbacks
151+
152+
- (void)_delegateAdded:(id<MTRDeviceDelegate>)delegate {
153+
[super _delegateAdded: delegate];
154+
MTR_LOG("%@ delegate added: %@", self, delegate);
155+
[(MTRDeviceController_XPC *)[self deviceController] _updateRegistrationInfo];
156+
}
157+
158+
- (void)_delegateRemoved:(id<MTRDeviceDelegate>)delegate {
159+
[super _delegateRemoved: delegate];
160+
MTR_LOG("%@ delegate removed: %@", self, delegate);
161+
[(MTRDeviceController_XPC *)[self deviceController] _updateRegistrationInfo];
162+
}
163+
149164
#pragma mark - Client Callbacks (MTRDeviceDelegate)
150165

151166
// required methods for MTRDeviceDelegates

0 commit comments

Comments
 (0)