You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
4.3 KiB

6 years ago
  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 "FIRMessagingCheckinService.h"
  17. #import "FIRMessagingUtilities.h"
  18. #import "NSError+FIRMessaging.h"
  19. @interface FIRMessagingCheckinService ()
  20. // This property is of type FIRInstanceIDCheckinPreferences, if InstanceID was directly linkable
  21. @property(nonatomic, readwrite, strong) id checkinPreferences;
  22. @end
  23. @implementation FIRMessagingCheckinService;
  24. #pragma mark - Reflection-Based Getter Functions
  25. // Encapsulates the -hasValidCheckinInfo method of FIRInstanceIDCheckinPreferences
  26. BOOL FIRMessagingCheckinService_hasValidCheckinInfo(id checkinPreferences) {
  27. SEL hasValidCheckinInfoSelector = NSSelectorFromString(@"hasValidCheckinInfo");
  28. if (![checkinPreferences respondsToSelector:hasValidCheckinInfoSelector]) {
  29. // Can't check hasValidCheckinInfo
  30. return NO;
  31. }
  32. // Since hasValidCheckinInfo returns a BOOL, use NSInvocation
  33. NSMethodSignature *methodSignature =
  34. [[checkinPreferences class] instanceMethodSignatureForSelector:hasValidCheckinInfoSelector];
  35. NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature];
  36. invocation.selector = hasValidCheckinInfoSelector;
  37. invocation.target = checkinPreferences;
  38. [invocation invoke];
  39. BOOL returnValue;
  40. [invocation getReturnValue:&returnValue];
  41. return returnValue;
  42. }
  43. // Returns a non-scalar (id) object based on the property name
  44. id FIRMessagingCheckinService_propertyNamed(id checkinPreferences, NSString *propertyName) {
  45. SEL propertyGetterSelector = NSSelectorFromString(propertyName);
  46. if ([checkinPreferences respondsToSelector:propertyGetterSelector]) {
  47. #pragma clang diagnostic push
  48. #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
  49. return [checkinPreferences performSelector:propertyGetterSelector];
  50. #pragma clang diagnostic pop
  51. }
  52. return nil;
  53. }
  54. #pragma mark - Methods
  55. - (BOOL)tryToLoadPrefetchedCheckinPreferences {
  56. Class instanceIDClass = NSClassFromString(@"FIRInstanceID");
  57. if (!instanceIDClass) {
  58. // InstanceID is not linked
  59. return NO;
  60. }
  61. // [FIRInstanceID instanceID]
  62. SEL instanceIDSelector = NSSelectorFromString(@"instanceID");
  63. if (![instanceIDClass respondsToSelector:instanceIDSelector]) {
  64. return NO;
  65. }
  66. #pragma clang diagnostic push
  67. #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
  68. id instanceID = [instanceIDClass performSelector:instanceIDSelector];
  69. #pragma clang diagnostic pop
  70. if (!instanceID) {
  71. // Instance ID singleton not available
  72. return NO;
  73. }
  74. // [[FIRInstanceID instanceID] cachedCheckinPreferences]
  75. SEL cachedCheckinPrefsSelector = NSSelectorFromString(@"cachedCheckinPreferences");
  76. if (![instanceID respondsToSelector:cachedCheckinPrefsSelector]) {
  77. // cachedCheckinPreferences is not accessible
  78. return NO;
  79. }
  80. #pragma clang diagnostic push
  81. #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
  82. id checkinPreferences = [instanceID performSelector:cachedCheckinPrefsSelector];
  83. #pragma clang diagnostic pop
  84. if (!checkinPreferences) {
  85. // No cached checkin prefs
  86. return NO;
  87. }
  88. BOOL hasValidInfo = FIRMessagingCheckinService_hasValidCheckinInfo(checkinPreferences);
  89. if (hasValidInfo) {
  90. self.checkinPreferences = checkinPreferences;
  91. }
  92. return hasValidInfo;
  93. }
  94. #pragma mark - API
  95. - (NSString *)deviceAuthID {
  96. return FIRMessagingCheckinService_propertyNamed(self.checkinPreferences, @"deviceID");
  97. }
  98. - (NSString *)secretToken {
  99. return FIRMessagingCheckinService_propertyNamed(self.checkinPreferences, @"secretToken");
  100. }
  101. - (NSString *)versionInfo {
  102. return FIRMessagingCheckinService_propertyNamed(self.checkinPreferences, @"versionInfo");
  103. }
  104. - (NSString *)digest {
  105. return FIRMessagingCheckinService_propertyNamed(self.checkinPreferences, @"digest");
  106. }
  107. - (BOOL)hasValidCheckinInfo {
  108. return FIRMessagingCheckinService_hasValidCheckinInfo(self.checkinPreferences);
  109. }
  110. @end