Skip to content
Merged
Changes from 2 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
76 changes: 38 additions & 38 deletions Firebase/Messaging/FIRMessagingRemoteNotificationsProxy.m
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ - (void)swizzleMethodsIfPossible {
Class notificationCenterClass = NSClassFromString(@"UNUserNotificationCenter");
if (notificationCenterClass) {
// We are linked against iOS 10 SDK or above
id notificationCenter = getNamedPropertyFromObject(notificationCenterClass,
id notificationCenter = FIRMessagingPropertyNameFromObject(notificationCenterClass,
@"currentNotificationCenter",
notificationCenterClass);
if (notificationCenter) {
Expand Down Expand Up @@ -125,7 +125,7 @@ - (void)listenForDelegateChangesInUserNotificationCenter:(id)notificationCenter
if (![notificationCenter isKindOfClass:notificationCenterClass]) {
return;
}
id delegate = getNamedPropertyFromObject(notificationCenter, @"delegate", nil);
id delegate = FIRMessagingPropertyNameFromObject(notificationCenter, @"delegate", nil);
Protocol *delegateProtocol = NSProtocolFromString(@"UNUserNotificationCenterDelegate");
if ([delegate conformsToProtocol:delegateProtocol]) {
// Swizzle this object now, if available
Expand Down Expand Up @@ -156,15 +156,15 @@ - (void)swizzleUserNotificationCenterDelegate:(id _Nonnull)delegate {
if ([delegate respondsToSelector:willPresentNotificationSelector]) {
[self swizzleSelector:willPresentNotificationSelector
inClass:[delegate class]
withImplementation:(IMP)FCM_swizzle_willPresentNotificationWithHandler
withImplementation:(IMP)FCMSwizzleWillPresentNotificationWithHandler
inProtocol:userNotificationCenterProtocol];
}
SEL didReceiveNotificationResponseSelector =
NSSelectorFromString(kUserNotificationDidReceiveResponseSelectorString);
if ([delegate respondsToSelector:didReceiveNotificationResponseSelector]) {
[self swizzleSelector:didReceiveNotificationResponseSelector
inClass:[delegate class]
withImplementation:(IMP)FCM_swizzle_didReceiveNotificationResponseWithHandler
withImplementation:(IMP)FCMSwizzleDidReceiveNotificationResponseWithHandler
inProtocol:userNotificationCenterProtocol];
}
self.currentUserNotificationCenterDelegate = delegate;
Expand Down Expand Up @@ -263,13 +263,13 @@ - (void)saveOriginalImplementation:(IMP)imp forSelector:(SEL)selector {

- (IMP)originalImplementationForSelector:(SEL)selector {
NSString *selectorString = NSStringFromSelector(selector);
NSValue *implementation_value = self.originalAppDelegateImps[selectorString];
if (!implementation_value) {
NSValue *implementationValue = self.originalAppDelegateImps[selectorString];
if (!implementationValue) {
return nil;
}

IMP imp;
[implementation_value getValue:&imp];
[implementationValue getValue:&imp];
return imp;
}

Expand Down Expand Up @@ -300,27 +300,27 @@ - (void)swizzleSelector:(SEL)originalSelector
// This class implements this method, so replace the original implementation
// with our new implementation and save the old implementation.

IMP __original_method_implementation =
IMP originalMethodImplementation =
method_setImplementation(originalMethod, swizzledImplementation);

IMP __nonexistant_method_implementation = [self nonExistantMethodImplementationForClass:klass];
IMP nonexistantMethodImplementation = [self nonExistantMethodImplementationForClass:klass];

if (__original_method_implementation &&
__original_method_implementation != __nonexistant_method_implementation &&
__original_method_implementation != swizzledImplementation) {
[self saveOriginalImplementation:__original_method_implementation
if (originalMethodImplementation &&
originalMethodImplementation != nonexistantMethodImplementation &&
originalMethodImplementation != swizzledImplementation) {
[self saveOriginalImplementation:originalMethodImplementation
forSelector:originalSelector];
}
} else {
// The class doesn't have this method, so add our swizzled implementation as the
// original implementation of the original method.
struct objc_method_description method_description =
struct objc_method_description methodDescription =
protocol_getMethodDescription(protocol, originalSelector, NO, YES);

BOOL methodAdded = class_addMethod(klass,
originalSelector,
swizzledImplementation,
method_description.types);
methodDescription.types);
if (!methodAdded) {
FIRMessagingLoggerError(kFIRMessagingMessageCodeRemoteNotificationsProxyMethodNotAdded,
@"Could not add method for %@ to class %@",
Expand All @@ -339,10 +339,10 @@ - (void)unswizzleSelector:(SEL)selector inClass:(Class)klass {
return;
}

IMP original_imp = [self originalImplementationForSelector:selector];
if (original_imp) {
IMP originalImp = [self originalImplementationForSelector:selector];
if (originalImp) {
// Restore the original implementation as the current implementation
method_setImplementation(swizzledMethod, original_imp);
method_setImplementation(swizzledMethod, originalImp);
[self removeImplementationForSelector:selector];
} else {
// This class originally did not have an implementation for this selector.
Expand All @@ -369,7 +369,7 @@ - (IMP)nonExistantMethodImplementationForClass:(Class)klass {
}

// A safe, non-leaky way return a property object by its name
id getNamedPropertyFromObject(id object, NSString *propertyName, Class klass) {
id FIRMessagingPropertyNameFromObject(id object, NSString *propertyName, Class klass) {
SEL selector = NSSelectorFromString(propertyName);
if (![object respondsToSelector:selector]) {
return nil;
Expand Down Expand Up @@ -433,16 +433,16 @@ - (void)application:(GULApplication *)application
* In order to make FCM SDK compile and compatible with iOS SDKs before iOS 10, hide the
* parameter types from the swizzling implementation.
*/
void FCM_swizzle_willPresentNotificationWithHandler(
id self, SEL _cmd, id center, id notification, void (^handler)(NSUInteger)) {
void FCMSwizzleWillPresentNotificationWithHandler(
id self, SEL cmd, id center, id notification, void (^handler)(NSUInteger)) {

FIRMessagingRemoteNotificationsProxy *proxy = [FIRMessagingRemoteNotificationsProxy sharedProxy];
IMP original_imp = [proxy originalImplementationForSelector:_cmd];
IMP originalImp = [proxy originalImplementationForSelector:cmd];

void (^callOriginalMethodIfAvailable)(void) = ^{
if (original_imp) {
((void (*)(id, SEL, id, id, void (^)(NSUInteger)))original_imp)(
self, _cmd, center, notification, handler);
if (originalImp) {
((void (*)(id, SEL, id, id, void (^)(NSUInteger)))originalImp)(
self, cmd, center, notification, handler);
}
return;
};
Expand Down Expand Up @@ -476,7 +476,7 @@ void FCM_swizzle_willPresentNotificationWithHandler(
}

// Attempt to access the user info
id notificationUserInfo = userInfoFromNotification(notification);
id notificationUserInfo = FIRMessagingUserInfoFromNotification(notification);

if (!notificationUserInfo) {
// Could not access notification.request.content.userInfo.
Expand All @@ -498,16 +498,16 @@ void FCM_swizzle_willPresentNotificationWithHandler(
* In order to make FCM SDK compile and compatible with iOS SDKs before iOS 10, hide the
* parameter types from the swizzling implementation.
*/
void FCM_swizzle_didReceiveNotificationResponseWithHandler(
id self, SEL _cmd, id center, id response, void (^handler)(void)) {
void FCMSwizzleDidReceiveNotificationResponseWithHandler(
id self, SEL cmd, id center, id response, void (^handler)(void)) {

FIRMessagingRemoteNotificationsProxy *proxy = [FIRMessagingRemoteNotificationsProxy sharedProxy];
IMP original_imp = [proxy originalImplementationForSelector:_cmd];
IMP originalImp = [proxy originalImplementationForSelector:cmd];

void (^callOriginalMethodIfAvailable)(void) = ^{
if (original_imp) {
((void (*)(id, SEL, id, id, void (^)(void)))original_imp)(
self, _cmd, center, response, handler);
if (originalImp) {
((void (*)(id, SEL, id, id, void (^)(void)))originalImp)(
self, cmd, center, response, handler);
}
return;
};
Expand Down Expand Up @@ -543,11 +543,11 @@ void FCM_swizzle_didReceiveNotificationResponseWithHandler(
return;
}
id notificationClass = NSClassFromString(@"UNNotification");
id notification = getNamedPropertyFromObject(response, @"notification", notificationClass);
id notification = FIRMessagingPropertyNameFromObject(response, @"notification", notificationClass);

// With a notification object, use the common code to reach deep into notification
// (notification.request.content.userInfo)
id notificationUserInfo = userInfoFromNotification(notification);
id notificationUserInfo = FIRMessagingUserInfoFromNotification(notification);
if (!notificationUserInfo) {
// Could not access notification.request.content.userInfo.
callOriginalMethodIfAvailable();
Expand All @@ -559,7 +559,7 @@ void FCM_swizzle_didReceiveNotificationResponseWithHandler(
callOriginalMethodIfAvailable();
}

id userInfoFromNotification(id notification) {
id FIRMessagingUserInfoFromNotification(id notification) {

// Select the userInfo field from UNNotification.request.content.userInfo.
SEL requestSelector = NSSelectorFromString(@"request");
Expand All @@ -568,7 +568,7 @@ id userInfoFromNotification(id notification) {
return nil;
}
Class requestClass = NSClassFromString(@"UNNotificationRequest");
id notificationRequest = getNamedPropertyFromObject(notification, @"request", requestClass);
id notificationRequest = FIRMessagingPropertyNameFromObject(notification, @"request", requestClass);

SEL notificationContentSelector = NSSelectorFromString(@"content");
if (!notificationRequest
Expand All @@ -577,7 +577,7 @@ id userInfoFromNotification(id notification) {
return nil;
}
Class contentClass = NSClassFromString(@"UNNotificationContent");
id notificationContent = getNamedPropertyFromObject(notificationRequest,
id notificationContent = FIRMessagingPropertyNameFromObject(notificationRequest,
@"content",
contentClass);

Expand All @@ -587,7 +587,7 @@ id userInfoFromNotification(id notification) {
// Cannot access the userInfo property.
return nil;
}
id notificationUserInfo = getNamedPropertyFromObject(notificationContent,
id notificationUserInfo = FIRMessagingPropertyNameFromObject(notificationContent,
@"userInfo",
[NSDictionary class]);

Expand Down