Brak opisu

FIRInstanceIDCheckinPreferences.m 4.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright 2019 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 "FIRInstanceIDCheckinPreferences.h"
  17. #import <GoogleUtilities/GULUserDefaults.h>
  18. #import "FIRInstanceIDCheckinService.h"
  19. #import "FIRInstanceIDDefines.h"
  20. #import "FIRInstanceIDUtilities.h"
  21. const NSTimeInterval kFIRInstanceIDDefaultCheckinInterval = 7 * 24 * 60 * 60; // 7 days.
  22. @interface FIRInstanceIDCheckinPreferences ()
  23. @property(nonatomic, readwrite, copy) NSString *deviceID;
  24. @property(nonatomic, readwrite, copy) NSString *secretToken;
  25. @property(nonatomic, readwrite, copy) NSString *digest;
  26. @property(nonatomic, readwrite, copy) NSString *versionInfo;
  27. @property(nonatomic, readwrite, copy) NSString *deviceDataVersion;
  28. @property(nonatomic, readwrite, strong) NSMutableDictionary *gServicesData;
  29. @property(nonatomic, readwrite, assign) int64_t lastCheckinTimestampMillis;
  30. // This flag indicates that we have already saved the above deviceID and secret
  31. // to our keychain and hence we don't need to save again. This is helpful since
  32. // on checkin refresh we can avoid writing to the Keychain which can sometimes
  33. // be very buggy. For info check this https://forums.developer.apple.com/thread/4743
  34. @property(nonatomic, readwrite, assign) BOOL hasPreCachedAuthCredentials;
  35. @end
  36. @implementation FIRInstanceIDCheckinPreferences
  37. - (NSDictionary *)checkinPlistContents {
  38. NSMutableDictionary *checkinPlistContents = [NSMutableDictionary dictionary];
  39. checkinPlistContents[kFIRInstanceIDDigestStringKey] = self.digest ?: @"";
  40. checkinPlistContents[kFIRInstanceIDVersionInfoStringKey] = self.versionInfo ?: @"";
  41. checkinPlistContents[kFIRInstanceIDDeviceDataVersionKey] = self.deviceDataVersion ?: @"";
  42. checkinPlistContents[kFIRInstanceIDLastCheckinTimeKey] = @(self.lastCheckinTimestampMillis);
  43. checkinPlistContents[kFIRInstanceIDGServicesDictionaryKey] =
  44. [self.gServicesData count] ? self.gServicesData : @{};
  45. return checkinPlistContents;
  46. }
  47. - (BOOL)hasCheckinInfo {
  48. return (self.deviceID.length && self.secretToken.length);
  49. }
  50. - (BOOL)hasValidCheckinInfo {
  51. int64_t currentTimestampInMillis = FIRInstanceIDCurrentTimestampInMilliseconds();
  52. int64_t timeSinceLastCheckinInMillis = currentTimestampInMillis - self.lastCheckinTimestampMillis;
  53. _FIRInstanceIDDevAssert(timeSinceLastCheckinInMillis >= 0,
  54. @"FCM error: cannot have last checkin timestamp in future");
  55. BOOL hasCheckinInfo = [self hasCheckinInfo];
  56. NSString *lastLocale =
  57. [[GULUserDefaults standardUserDefaults] stringForKey:kFIRInstanceIDUserDefaultsKeyLocale];
  58. // If it's app's first time open and checkin is already fetched and no locale information is
  59. // stored, then checkin info is valid. We should not checkin again because locale is considered
  60. // "changed".
  61. if (hasCheckinInfo && !lastLocale) {
  62. NSString *currentLocale = FIRInstanceIDCurrentLocale();
  63. [[GULUserDefaults standardUserDefaults] setObject:currentLocale
  64. forKey:kFIRInstanceIDUserDefaultsKeyLocale];
  65. return YES;
  66. }
  67. // If locale has changed, checkin info is no longer valid.
  68. // Also update locale information if changed. (Only do it here not in token refresh)
  69. if (FIRInstanceIDHasLocaleChanged()) {
  70. NSString *currentLocale = FIRInstanceIDCurrentLocale();
  71. [[GULUserDefaults standardUserDefaults] setObject:currentLocale
  72. forKey:kFIRInstanceIDUserDefaultsKeyLocale];
  73. return NO;
  74. }
  75. return (hasCheckinInfo &&
  76. (timeSinceLastCheckinInMillis / 1000.0 < kFIRInstanceIDDefaultCheckinInterval));
  77. }
  78. - (void)setHasPreCachedAuthCredentials:(BOOL)hasPreCachedAuthCredentials {
  79. _hasPreCachedAuthCredentials = hasPreCachedAuthCredentials;
  80. }
  81. @end