No Description

FIRInstanceIDCheckinPreferences.m 4.0KB

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