Skip to content

FCM: Fix crash on iOS 14 when multiple app delegate completion methods are called #6863

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

Merged
merged 6 commits into from
Nov 5, 2020
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
1 change: 1 addition & 0 deletions FirebaseAuth/Sources/Auth/FIRAuth.m
Original file line number Diff line number Diff line change
Expand Up @@ -1499,6 +1499,7 @@ - (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
[self canHandleNotification:userInfo];
completionHandler(UIBackgroundFetchResultNoData);
}

// iOS 10 deprecation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@ - (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
[[FIRMessaging messaging] appDidReceiveMessage:userInfo];
completionHandler(UIBackgroundFetchResultNoData);
}

- (void)application:(UIApplication *)application
Expand Down
53 changes: 51 additions & 2 deletions GoogleUtilities/AppDelegateSwizzler/GULAppDelegateSwizzler.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#import "GoogleUtilities/Logger/Public/GoogleUtilities/GULLogger.h"
#import "GoogleUtilities/Network/Public/GoogleUtilities/GULMutableDictionary.h"

#import <dispatch/group.h>
#import <objc/runtime.h>

// Implementations need to be typed before calling the implementation directly to cast the
Expand Down Expand Up @@ -882,24 +883,72 @@ - (void)application:(GULApplication *)application
didReceiveRemoteNotificationWithCompletionIMP =
[didReceiveRemoteNotificationWithCompletionIMPPointer pointerValue];

dispatch_group_t __block callbackGroup = dispatch_group_create();
NSMutableArray<NSNumber *> *__block fetchResults = [NSMutableArray array];

void (^localCompletionHandler)(UIBackgroundFetchResult) =
^void(UIBackgroundFetchResult fetchResult) {
[fetchResults addObject:[NSNumber numberWithInt:(int)fetchResult]];
dispatch_group_leave(callbackGroup);
};

// Notify interceptors.
[GULAppDelegateSwizzler
notifyInterceptorsWithMethodSelector:methodSelector
callback:^(id<GULApplicationDelegate> interceptor) {
dispatch_group_enter(callbackGroup);

NSInvocation *invocation = [GULAppDelegateSwizzler
appDelegateInvocationForSelector:methodSelector];
[invocation setTarget:interceptor];
[invocation setSelector:methodSelector];
[invocation setArgument:(void *)(&application) atIndex:2];
[invocation setArgument:(void *)(&userInfo) atIndex:3];
[invocation setArgument:(void *)(&completionHandler) atIndex:4];
[invocation setArgument:(void *)(&localCompletionHandler)
atIndex:4];
[invocation invoke];
}];
// Call the real implementation if the real App Delegate has any.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current implementation doesn't add the originally implemented method to the group. I think we should do the same for it: add enter/leave group calls and wrap the completion. Potentially we may use a single completion handler wrapper to be passed to all interceptors and the original implementations.

if (didReceiveRemoteNotificationWithCompletionIMP) {
dispatch_group_enter(callbackGroup);

didReceiveRemoteNotificationWithCompletionIMP(self, methodSelector, application, userInfo,
completionHandler);
localCompletionHandler);
}

dispatch_group_notify(callbackGroup, dispatch_get_main_queue(), ^() {
BOOL allFetchesFailed = YES;
BOOL anyFetchHasNewData = NO;

for (NSNumber *oneResult in fetchResults) {
UIBackgroundFetchResult result = oneResult.intValue;

switch (result) {
case UIBackgroundFetchResultNoData:
allFetchesFailed = NO;
break;
case UIBackgroundFetchResultNewData:
allFetchesFailed = NO;
anyFetchHasNewData = YES;
break;
case UIBackgroundFetchResultFailed:

break;
}
}

UIBackgroundFetchResult finalFetchResult = UIBackgroundFetchResultNoData;

if (allFetchesFailed) {
finalFetchResult = UIBackgroundFetchResultFailed;
} else if (anyFetchHasNewData) {
finalFetchResult = UIBackgroundFetchResultNewData;
} else {
finalFetchResult = UIBackgroundFetchResultNoData;
}

completionHandler(finalFetchResult);
});
}
#endif // !TARGET_OS_WATCH && !TARGET_OS_OSX

Expand Down
107 changes: 104 additions & 3 deletions GoogleUtilities/Tests/Unit/Swizzler/GULAppDelegateSwizzlerTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -1065,12 +1065,12 @@ - (void)testApplicationDidReceiveRemoteNotificationWithCompletionIsInvokedOnInte
id interceptor = OCMProtocolMock(@protocol(GULApplicationDelegate));
OCMExpect([interceptor application:application
didReceiveRemoteNotification:notification
fetchCompletionHandler:completion]);
fetchCompletionHandler:[OCMArg isNotNil]]);

id interceptor2 = OCMProtocolMock(@protocol(GULApplicationDelegate));
OCMExpect([interceptor2 application:application
didReceiveRemoteNotification:notification
fetchCompletionHandler:completion]);
fetchCompletionHandler:[OCMArg isNotNil]]);

GULTestAppDelegate *testAppDelegate = [[GULTestAppDelegate alloc] init];
OCMStub([self.mockSharedApplication delegate]).andReturn(testAppDelegate);
Expand All @@ -1087,7 +1087,108 @@ - (void)testApplicationDidReceiveRemoteNotificationWithCompletionIsInvokedOnInte

XCTAssertEqual(testAppDelegate.application, application);
XCTAssertEqual(testAppDelegate.remoteNotification, notification);
XCTAssertEqual(testAppDelegate.remoteNotificationCompletionHandler, completion);
}

- (void)verifyCompletionCalledForObserverResult:(UIBackgroundFetchResult)observerResult1
anotherObserverResult:(UIBackgroundFetchResult)observerResult2
swizzledResult:(UIBackgroundFetchResult)swizzledResult
expecredResult:(UIBackgroundFetchResult)expectedResult {
NSDictionary *notification = @{};
GULApplication *application = [GULApplication sharedApplication];

XCTestExpectation *completionExpectation =
[[XCTestExpectation alloc] initWithDescription:@"Completion called once"];

void (^completion)(UIBackgroundFetchResult) = ^(UIBackgroundFetchResult result) {
XCTAssertEqual(result, expectedResult);
[completionExpectation fulfill];
};

void (^onDidReceiveRemoteNotification1)(NSInvocation *invocation) = ^(NSInvocation *invocation) {
void __unsafe_unretained (^localCompletionHandler)(UIBackgroundFetchResult) = nil;
[invocation getArgument:(void *)(&localCompletionHandler) atIndex:4];
XCTAssertNotNil(localCompletionHandler);
localCompletionHandler(observerResult1);
};

id interceptor = OCMProtocolMock(@protocol(GULApplicationDelegate));
OCMExpect([interceptor application:application
didReceiveRemoteNotification:notification
fetchCompletionHandler:[OCMArg isNotNil]])
.andDo(onDidReceiveRemoteNotification1);

void (^onDidReceiveRemoteNotification2)(NSInvocation *invocation) = ^(NSInvocation *invocation) {
void __unsafe_unretained (^localCompletionHandler)(UIBackgroundFetchResult) = nil;
[invocation getArgument:(void *)(&localCompletionHandler) atIndex:4];
XCTAssertNotNil(localCompletionHandler);
localCompletionHandler(observerResult2);
};

id interceptor2 = OCMProtocolMock(@protocol(GULApplicationDelegate));
OCMExpect([interceptor2 application:application
didReceiveRemoteNotification:notification
fetchCompletionHandler:[OCMArg isNotNil]])
.andDo(onDidReceiveRemoteNotification2);

GULTestAppDelegate *testAppDelegate = [[GULTestAppDelegate alloc] init];
OCMStub([self.mockSharedApplication delegate]).andReturn(testAppDelegate);
[GULAppDelegateSwizzler proxyOriginalDelegateIncludingAPNSMethods];

[GULAppDelegateSwizzler registerAppDelegateInterceptor:interceptor];
[GULAppDelegateSwizzler registerAppDelegateInterceptor:interceptor2];

[testAppDelegate application:application
didReceiveRemoteNotification:notification
fetchCompletionHandler:completion];
testAppDelegate.remoteNotificationCompletionHandler(swizzledResult);
OCMVerifyAll(interceptor);
OCMVerifyAll(interceptor2);
[self waitForExpectations:@[ completionExpectation ] timeout:0.1];
}

- (void)testApplicationDidReceiveRemoteNotificationWithCompletionCompletionIsCalledOnce {
[self verifyCompletionCalledForObserverResult:UIBackgroundFetchResultNoData
anotherObserverResult:UIBackgroundFetchResultNoData
swizzledResult:UIBackgroundFetchResultNoData
expecredResult:UIBackgroundFetchResultNoData];
}

- (void)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to add tests for cases with different fetch results or at least add a TODO to add them.

testApplicationDidReceiveRemoteNotificationWithCompletionCompletionIsCalledOnce_HandleFailedState {
[self verifyCompletionCalledForObserverResult:UIBackgroundFetchResultFailed
anotherObserverResult:UIBackgroundFetchResultFailed
swizzledResult:UIBackgroundFetchResultFailed
expecredResult:UIBackgroundFetchResultFailed];
}

- (void)testApplicationDidReceiveRemoteNotificationWithCompletionCompletionIsCalledOnce_NoData {
[self verifyCompletionCalledForObserverResult:UIBackgroundFetchResultNoData
anotherObserverResult:UIBackgroundFetchResultFailed
swizzledResult:UIBackgroundFetchResultFailed
expecredResult:UIBackgroundFetchResultNoData];
}
- (void)
testApplicationDidReceiveRemoteNotificationWithCompletionCompletionIsCalledOnce_HandleNewDataState_OthersFailed {
[self verifyCompletionCalledForObserverResult:UIBackgroundFetchResultNewData
anotherObserverResult:UIBackgroundFetchResultFailed
swizzledResult:UIBackgroundFetchResultFailed
expecredResult:UIBackgroundFetchResultNewData];
}

- (void)
testApplicationDidReceiveRemoteNotificationWithCompletionCompletionIsCalledOnce_HandleNewDataState_OthersNoData {
[self verifyCompletionCalledForObserverResult:UIBackgroundFetchResultNewData
anotherObserverResult:UIBackgroundFetchResultNoData
swizzledResult:UIBackgroundFetchResultNoData
expecredResult:UIBackgroundFetchResultNewData];
}

- (void)
testApplicationDidReceiveRemoteNotificationWithCompletionCompletionIsCalledOnce_HandleNewDataState_OthersNoDataFailed {
[self verifyCompletionCalledForObserverResult:UIBackgroundFetchResultNewData
anotherObserverResult:UIBackgroundFetchResultNoData
swizzledResult:UIBackgroundFetchResultFailed
expecredResult:UIBackgroundFetchResultNewData];
}

- (void)testApplicationDidReceiveRemoteNotificationWithCompletionImplementationIsNotAdded {
Expand Down