FIRMessagingInstanceIDProxy.m 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "FIRMessagingInstanceIDProxy.h"
  17. @implementation FIRMessagingInstanceIDProxy
  18. + (nonnull instancetype)instanceIDProxy {
  19. static id proxyInstanceID = nil;
  20. static dispatch_once_t onceToken;
  21. dispatch_once(&onceToken, ^{
  22. Class instanceIDClass = NSClassFromString(@"FIRInstanceID");
  23. if (!instanceIDClass) {
  24. proxyInstanceID = nil;
  25. return;
  26. }
  27. SEL instanceIDSelector = NSSelectorFromString(@"instanceID");
  28. if (![instanceIDClass respondsToSelector:instanceIDSelector]) {
  29. proxyInstanceID = nil;
  30. return;
  31. }
  32. #pragma clang diagnostic push
  33. #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
  34. proxyInstanceID = [instanceIDClass performSelector:instanceIDSelector];
  35. #pragma clang diagnostic pop
  36. });
  37. return (FIRMessagingInstanceIDProxy *)proxyInstanceID;
  38. }
  39. - (void)setAPNSToken:(nonnull NSData *)token
  40. type:(FIRMessagingInstanceIDProxyAPNSTokenType)type {
  41. id proxy = [[self class] instanceIDProxy];
  42. SEL setAPNSTokenSelector = NSSelectorFromString(@"setAPNSToken:type:");
  43. if (![proxy respondsToSelector:setAPNSTokenSelector]) {
  44. return;
  45. }
  46. // Since setAPNSToken takes a scalar value, use NSInvocation
  47. NSMethodSignature *methodSignature =
  48. [[proxy class] instanceMethodSignatureForSelector:setAPNSTokenSelector];
  49. NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature];
  50. invocation.selector = setAPNSTokenSelector;
  51. invocation.target = proxy;
  52. [invocation setArgument:&token atIndex:2];
  53. [invocation setArgument:&type atIndex:3];
  54. [invocation invoke];
  55. }
  56. #pragma mark - Tokens
  57. - (nullable NSString *)token {
  58. id proxy = [[self class] instanceIDProxy];
  59. SEL getTokenSelector = NSSelectorFromString(@"token");
  60. if (![proxy respondsToSelector:getTokenSelector]) {
  61. return nil;
  62. }
  63. #pragma clang diagnostic push
  64. #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
  65. return [proxy performSelector:getTokenSelector];
  66. #pragma clang diagnostic pop
  67. }
  68. - (void)tokenWithAuthorizedEntity:(nonnull NSString *)authorizedEntity
  69. scope:(nonnull NSString *)scope
  70. options:(nullable NSDictionary *)options
  71. handler:(nonnull FIRMessagingInstanceIDProxyTokenHandler)handler {
  72. id proxy = [[self class] instanceIDProxy];
  73. SEL getTokenSelector = NSSelectorFromString(@"tokenWithAuthorizedEntity:scope:options:handler:");
  74. if (![proxy respondsToSelector:getTokenSelector]) {
  75. return;
  76. }
  77. // Since there are >2 arguments, use NSInvocation
  78. NSMethodSignature *methodSignature =
  79. [[proxy class] instanceMethodSignatureForSelector:getTokenSelector];
  80. NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature];
  81. invocation.selector = getTokenSelector;
  82. invocation.target = proxy;
  83. [invocation setArgument:&authorizedEntity atIndex:2];
  84. [invocation setArgument:&scope atIndex:3];
  85. [invocation setArgument:&options atIndex:4];
  86. [invocation setArgument:&handler atIndex:5];
  87. [invocation invoke];
  88. }
  89. - (void)deleteTokenWithAuthorizedEntity:(nonnull NSString *)authorizedEntity
  90. scope:(nonnull NSString *)scope
  91. handler:
  92. (nonnull FIRMessagingInstanceIDProxyDeleteTokenHandler)handler {
  93. id proxy = [[self class] instanceIDProxy];
  94. SEL deleteTokenSelector = NSSelectorFromString(@"deleteTokenWithAuthorizedEntity:scope:handler:");
  95. if (![proxy respondsToSelector:deleteTokenSelector]) {
  96. return;
  97. }
  98. // Since there are >2 arguments, use NSInvocation
  99. NSMethodSignature *methodSignature =
  100. [[proxy class] instanceMethodSignatureForSelector:deleteTokenSelector];
  101. NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature];
  102. invocation.selector = deleteTokenSelector;
  103. invocation.target = proxy;
  104. [invocation setArgument:&authorizedEntity atIndex:2];
  105. [invocation setArgument:&scope atIndex:3];
  106. [invocation setArgument:&handler atIndex:4];
  107. [invocation invoke];
  108. }
  109. @end