FIRAppCheckSettings.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright 2021 Google LLC
  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 "FirebaseAppCheck/Sources/Core/FIRAppCheckSettings.h"
  17. #import <GoogleUtilities/GULUserDefaults.h>
  18. #import "FirebaseCore/Extension/FirebaseCoreInternal.h"
  19. NS_ASSUME_NONNULL_BEGIN
  20. NSString *const kFIRAppCheckTokenAutoRefreshEnabledUserDefaultsPrefix =
  21. @"FIRAppCheckTokenAutoRefreshEnabled_";
  22. NSString *const kFIRAppCheckTokenAutoRefreshEnabledInfoPlistKey =
  23. @"FirebaseAppCheckTokenAutoRefreshEnabled";
  24. @interface FIRAppCheckSettings ()
  25. @property(nonatomic, weak, readonly) FIRApp *firebaseApp;
  26. @property(nonatomic, readonly) GULUserDefaults *userDefaults;
  27. @property(nonatomic, readonly) NSBundle *mainBundle;
  28. @property(nonatomic, readonly) NSString *userDefaultKey;
  29. @property(nonatomic, assign) BOOL isTokenAutoRefreshConfigured;
  30. @end
  31. @implementation FIRAppCheckSettings
  32. - (instancetype)initWithApp:(FIRApp *)firebaseApp
  33. userDefault:(GULUserDefaults *)userDefaults
  34. mainBundle:(NSBundle *)mainBundle {
  35. self = [super init];
  36. if (self) {
  37. _firebaseApp = firebaseApp;
  38. _userDefaults = userDefaults;
  39. _mainBundle = mainBundle;
  40. _userDefaultKey = [kFIRAppCheckTokenAutoRefreshEnabledUserDefaultsPrefix
  41. stringByAppendingString:firebaseApp.name];
  42. [super setIsTokenAutoRefreshEnabled:NO];
  43. _isTokenAutoRefreshConfigured = NO;
  44. }
  45. return self;
  46. }
  47. - (BOOL)isTokenAutoRefreshEnabled {
  48. @synchronized(self) {
  49. if (self.isTokenAutoRefreshConfigured) {
  50. // Return value form the in-memory cache to avoid accessing the user default or bundle when
  51. // not required.
  52. return [super isTokenAutoRefreshEnabled];
  53. }
  54. // Check user defaults for a value set during the previous launch.
  55. NSNumber *isTokenAutoRefreshEnabledNumber =
  56. [self.userDefaults objectForKey:self.userDefaultKey];
  57. // Check Info.plist if no user defaults value found.
  58. if (isTokenAutoRefreshEnabledNumber == nil) {
  59. isTokenAutoRefreshEnabledNumber = [self.mainBundle
  60. objectForInfoDictionaryKey:kFIRAppCheckTokenAutoRefreshEnabledInfoPlistKey];
  61. }
  62. if (isTokenAutoRefreshEnabledNumber != nil) {
  63. // Update in-memory cache.
  64. self.isTokenAutoRefreshConfigured = YES;
  65. self.isTokenAutoRefreshEnabled = isTokenAutoRefreshEnabledNumber.boolValue;
  66. // Return the value.
  67. return [super isTokenAutoRefreshEnabled];
  68. }
  69. // Fallback to the global data collection flag.
  70. if (self.firebaseApp) {
  71. return self.firebaseApp.isDataCollectionDefaultEnabled;
  72. } else {
  73. // If `self.firebaseApp == nil`, then the app has been de-initialized. No auto-refresh in this
  74. // case.
  75. return NO;
  76. }
  77. }
  78. }
  79. - (void)setIsTokenAutoRefreshEnabled:(BOOL)isTokenAutoRefreshEnabled {
  80. @synchronized(self) {
  81. self.isTokenAutoRefreshConfigured = YES;
  82. [super setIsTokenAutoRefreshEnabled:isTokenAutoRefreshEnabled];
  83. [self.userDefaults setBool:isTokenAutoRefreshEnabled forKey:self.userDefaultKey];
  84. }
  85. }
  86. @end
  87. NS_ASSUME_NONNULL_END