Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only register XPC interest when a delegate exists #37623

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/darwin/Framework/CHIP/MTRDevice.mm
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,10 @@ - (void)_addDelegate:(id<MTRDeviceDelegate>)delegate queue:(dispatch_queue_t)que
MTR_LOG("%@ added delegate info %@", self, newDelegateInfo);

// Call hook to allow subclasses to act on delegate addition.
[self _delegateAdded];
[self _delegateAdded:delegate];
}

- (void)_delegateAdded
- (void)_delegateAdded:(id<MTRDeviceDelegate>)delegate
{
os_unfair_lock_assert_owner(&self->_lock);

Expand Down Expand Up @@ -233,6 +233,16 @@ - (void)removeDelegate:(id<MTRDeviceDelegate>)delegate
[_delegates minusSet:delegatesToRemove];
MTR_LOG("%@ removeDelegate: removed %lu", self, static_cast<unsigned long>(_delegates.count - oldDelegatesCount));
}

// Call hook to allow subclasses to act on delegate addition.
[self _delegateRemoved:delegate];
}

- (void)_delegateRemoved:(id<MTRDeviceDelegate>)delegate
{
os_unfair_lock_assert_owner(&self->_lock);

// Nothing to do for now. At the moment this is a hook for subclasses.
}

- (void)invalidate
Expand Down
12 changes: 7 additions & 5 deletions src/darwin/Framework/CHIP/MTRDeviceController_XPC.mm
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#import "MTRDefines_Internal.h"
#import "MTRDeviceController_Internal.h"
#import "MTRDevice_Internal.h"
#import "MTRDevice_XPC.h"
#import "MTRDevice_XPC_Internal.h"
#import "MTRError_Internal.h"
Expand Down Expand Up @@ -77,10 +78,13 @@ - (void)_updateRegistrationInfo
NSMutableArray * nodeIDs = [NSMutableArray array];

for (NSNumber * nodeID in [self.nodeIDToDeviceMap keyEnumerator]) {
NSMutableDictionary * nodeDictionary = [NSMutableDictionary dictionary];
MTR_REQUIRED_ATTRIBUTE(MTRDeviceControllerRegistrationNodeIDKey, nodeID, nodeDictionary)
MTRDevice * device = [self _deviceForNodeID:nodeID createIfNeeded:NO];
if ([device _delegateExists]) {
NSMutableDictionary * nodeDictionary = [NSMutableDictionary dictionary];
MTR_REQUIRED_ATTRIBUTE(MTRDeviceControllerRegistrationNodeIDKey, nodeID, nodeDictionary)

[nodeIDs addObject:nodeDictionary];
[nodeIDs addObject:nodeDictionary];
}
}
MTR_REQUIRED_ATTRIBUTE(MTRDeviceControllerRegistrationNodeIDsKey, nodeIDs, registrationInfo)
MTR_REQUIRED_ATTRIBUTE(MTRDeviceControllerRegistrationControllerContextKey, controllerContext, registrationInfo)
Expand Down Expand Up @@ -386,8 +390,6 @@ - (MTRDevice *)_setupDeviceForNodeID:(NSNumber *)nodeID prefetchedClusterData:(N
[self.nodeIDToDeviceMap setObject:deviceToReturn forKey:nodeID];
MTR_LOG("%s: returning XPC device for node id %@", __PRETTY_FUNCTION__, nodeID);

[self _updateRegistrationInfo];

return deviceToReturn;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@
@interface MTRDeviceController_XPC (Internal)

- (id)initWithMachServiceName:(NSString *)machServiceName options:(NSXPCConnectionOptions)options;
- (void)_updateRegistrationInfo;

@end
4 changes: 2 additions & 2 deletions src/darwin/Framework/CHIP/MTRDevice_Concrete.mm
Original file line number Diff line number Diff line change
Expand Up @@ -884,11 +884,11 @@ - (BOOL)_subscriptionsAllowed
return self.suspended == NO && ![_deviceController isKindOfClass:MTRDeviceControllerOverXPC.class];
}

- (void)_delegateAdded
- (void)_delegateAdded:(id<MTRDeviceDelegate>)delegate
{
os_unfair_lock_assert_owner(&self->_lock);

[super _delegateAdded];
[super _delegateAdded:delegate];

[self _ensureSubscriptionForExistingDelegates:@"delegate is set"];
}
Expand Down
3 changes: 2 additions & 1 deletion src/darwin/Framework/CHIP/MTRDevice_Internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ MTR_DIRECT_MEMBERS
- (BOOL)_delegateExists;

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

#ifdef DEBUG
// Only used for unit test purposes - normal delegate should not expect or handle being called back synchronously
Expand Down
17 changes: 17 additions & 0 deletions src/darwin/Framework/CHIP/MTRDevice_XPC.mm
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#import <Matter/MTRDeviceControllerParameters.h>

#import "MTRDeviceController_Internal.h"
#import "MTRDeviceController_XPC_Internal.h"

#import "MTRAsyncWorkQueue.h"
#import "MTRAttestationTrustStoreBridge.h"
Expand Down Expand Up @@ -146,6 +147,22 @@ - (MTRNetworkCommissioningFeature)networkCommissioningFeatures
return [[self._internalState objectForKey:kMTRDeviceInternalPropertyNetworkFeatures] unsignedIntValue];
}

#pragma mark - Delegate added/removed callbacks

- (void)_delegateAdded:(id<MTRDeviceDelegate>)delegate
{
[super _delegateAdded:delegate];
MTR_LOG("%@ delegate added: %@", self, delegate);
[(MTRDeviceController_XPC *) [self deviceController] _updateRegistrationInfo];
}

- (void)_delegateRemoved:(id<MTRDeviceDelegate>)delegate
{
[super _delegateRemoved:delegate];
MTR_LOG("%@ delegate removed: %@", self, delegate);
[(MTRDeviceController_XPC *) [self deviceController] _updateRegistrationInfo];
}

#pragma mark - Client Callbacks (MTRDeviceDelegate)

// required methods for MTRDeviceDelegates
Expand Down
Loading