Skip to content

Commit ee341ad

Browse files
Only register XPC interest when a delegate exists (#37623)
* Only registering when a delegate is added * Restyled by whitespace * Restyled by clang-format --------- Co-authored-by: Restyled.io <commits@restyled.io>
1 parent dd9bb34 commit ee341ad

6 files changed

+41
-10
lines changed

src/darwin/Framework/CHIP/MTRDevice.mm

+12-2
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

@@ -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

+7-5
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)
81+
MTRDevice * device = [self _deviceForNodeID:nodeID createIfNeeded:NO];
82+
if ([device _delegateExists]) {
83+
NSMutableDictionary * nodeDictionary = [NSMutableDictionary dictionary];
84+
MTR_REQUIRED_ATTRIBUTE(MTRDeviceControllerRegistrationNodeIDKey, nodeID, nodeDictionary)
8285

83-
[nodeIDs addObject:nodeDictionary];
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

+17
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,22 @@ - (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+
{
154+
[super _delegateAdded:delegate];
155+
MTR_LOG("%@ delegate added: %@", self, delegate);
156+
[(MTRDeviceController_XPC *) [self deviceController] _updateRegistrationInfo];
157+
}
158+
159+
- (void)_delegateRemoved:(id<MTRDeviceDelegate>)delegate
160+
{
161+
[super _delegateRemoved:delegate];
162+
MTR_LOG("%@ delegate removed: %@", self, delegate);
163+
[(MTRDeviceController_XPC *) [self deviceController] _updateRegistrationInfo];
164+
}
165+
149166
#pragma mark - Client Callbacks (MTRDeviceDelegate)
150167

151168
// required methods for MTRDeviceDelegates

0 commit comments

Comments
 (0)