2  * Copyright 2017 Google
 
   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
 
   8  *      http://www.apache.org/licenses/LICENSE-2.0
 
  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.
 
  17 #import "FIRMessagingCheckinService.h"
 
  19 #import "FIRMessagingUtilities.h"
 
  20 #import "NSError+FIRMessaging.h"
 
  22 @interface FIRMessagingCheckinService ()
 
  24 // This property is of type FIRInstanceIDCheckinPreferences, if InstanceID was directly linkable
 
  25 @property(nonatomic, readwrite, strong) id checkinPreferences;
 
  29 @implementation FIRMessagingCheckinService;
 
  31 #pragma mark - Reflection-Based Getter Functions
 
  33 // Encapsulates the -hasValidCheckinInfo method of FIRInstanceIDCheckinPreferences
 
  34 BOOL FIRMessagingCheckinService_hasValidCheckinInfo(id checkinPreferences) {
 
  35   SEL hasValidCheckinInfoSelector = NSSelectorFromString(@"hasValidCheckinInfo");
 
  36   if (![checkinPreferences respondsToSelector:hasValidCheckinInfoSelector]) {
 
  37     // Can't check hasValidCheckinInfo
 
  41   // Since hasValidCheckinInfo returns a BOOL, use NSInvocation
 
  42   NSMethodSignature *methodSignature =
 
  43       [[checkinPreferences class] instanceMethodSignatureForSelector:hasValidCheckinInfoSelector];
 
  44   NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature];
 
  45   invocation.selector = hasValidCheckinInfoSelector;
 
  46   invocation.target = checkinPreferences;
 
  49   [invocation getReturnValue:&returnValue];
 
  53 // Returns a non-scalar (id) object based on the property name
 
  54 id FIRMessagingCheckinService_propertyNamed(id checkinPreferences, NSString *propertyName) {
 
  55   SEL propertyGetterSelector = NSSelectorFromString(propertyName);
 
  56   if ([checkinPreferences respondsToSelector:propertyGetterSelector]) {
 
  57 #pragma clang diagnostic push
 
  58 #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
 
  59     return [checkinPreferences performSelector:propertyGetterSelector];
 
  60 #pragma clang diagnostic pop
 
  65 #pragma mark - Methods
 
  67 - (BOOL)tryToLoadPrefetchedCheckinPreferences {
 
  68   Class instanceIDClass = NSClassFromString(@"FIRInstanceID");
 
  69   if (!instanceIDClass) {
 
  70     // InstanceID is not linked
 
  74   // [FIRInstanceID instanceID]
 
  75   SEL instanceIDSelector = NSSelectorFromString(@"instanceID");
 
  76   if (![instanceIDClass respondsToSelector:instanceIDSelector]) {
 
  79 #pragma clang diagnostic push
 
  80 #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
 
  81   id instanceID = [instanceIDClass performSelector:instanceIDSelector];
 
  82 #pragma clang diagnostic pop
 
  84     // Instance ID singleton not available
 
  88   // [[FIRInstanceID instanceID] cachedCheckinPreferences]
 
  89   SEL cachedCheckinPrefsSelector = NSSelectorFromString(@"cachedCheckinPreferences");
 
  90   if (![instanceID respondsToSelector:cachedCheckinPrefsSelector]) {
 
  91     // cachedCheckinPreferences is not accessible
 
  94 #pragma clang diagnostic push
 
  95 #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
 
  96   id checkinPreferences = [instanceID performSelector:cachedCheckinPrefsSelector];
 
  97 #pragma clang diagnostic pop
 
  98   if (!checkinPreferences) {
 
  99     // No cached checkin prefs
 
 103   BOOL hasValidInfo = FIRMessagingCheckinService_hasValidCheckinInfo(checkinPreferences);
 
 105     self.checkinPreferences = checkinPreferences;
 
 112 - (NSString *)deviceAuthID {
 
 113   return FIRMessagingCheckinService_propertyNamed(self.checkinPreferences, @"deviceID");
 
 116 - (NSString *)secretToken {
 
 117   return FIRMessagingCheckinService_propertyNamed(self.checkinPreferences, @"secretToken");
 
 120 - (NSString *)versionInfo {
 
 121   return FIRMessagingCheckinService_propertyNamed(self.checkinPreferences, @"versionInfo");
 
 124 - (NSString *)digest {
 
 125   return FIRMessagingCheckinService_propertyNamed(self.checkinPreferences, @"digest");
 
 128 - (BOOL)hasValidCheckinInfo {
 
 129   return FIRMessagingCheckinService_hasValidCheckinInfo(self.checkinPreferences);