RCNUserDefaultsManager.m 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 "FirebaseRemoteConfig/Sources/RCNUserDefaultsManager.h"
  17. #import <FirebaseCore/FIRLogger.h>
  18. #import <FirebaseRemoteConfig/FIRRemoteConfig.h>
  19. static NSString *const kRCNGroupPrefix = @"group";
  20. static NSString *const kRCNGroupSuffix = @"firebase";
  21. static NSString *const kRCNUserDefaultsKeyNamelastETag = @"lastETag";
  22. static NSString *const kRCNUserDefaultsKeyNameLastSuccessfulFetchTime = @"lastSuccessfulFetchTime";
  23. static NSString *const kRCNUserDefaultsKeyNamelastFetchStatus = @"lastFetchStatus";
  24. static NSString *const kRCNUserDefaultsKeyNameIsClientThrottled =
  25. @"isClientThrottledWithExponentialBackoff";
  26. static NSString *const kRCNUserDefaultsKeyNameThrottleEndTime = @"throttleEndTime";
  27. static NSString *const kRCNUserDefaultsKeyNamecurrentThrottlingRetryInterval =
  28. @"currentThrottlingRetryInterval";
  29. @interface RCNUserDefaultsManager () {
  30. /// User Defaults instance for this bundleID. NSUserDefaults is guaranteed to be thread-safe.
  31. NSUserDefaults *_userDefaults;
  32. /// The suite name for this user defaults instance. It is a combination of a prefix and the
  33. /// bundleID. This is because you cannot use just the bundleID of the current app as the suite
  34. /// name when initializing user defaults.
  35. NSString *_userDefaultsSuiteName;
  36. /// The FIRApp that this instance is scoped within.
  37. NSString *_firebaseAppName;
  38. /// The Firebase Namespace that this instance is scoped within.
  39. NSString *_firebaseNamespace;
  40. /// The bundleID of the app. In case of an extension, this will be the bundleID of the parent app.
  41. NSString *_bundleIdentifier;
  42. }
  43. @end
  44. @implementation RCNUserDefaultsManager
  45. #pragma mark Initializers.
  46. /// Designated initializer.
  47. - (instancetype)initWithAppName:(NSString *)appName
  48. bundleID:(NSString *)bundleIdentifier
  49. namespace:(NSString *)firebaseNamespace {
  50. self = [super init];
  51. if (self) {
  52. _firebaseAppName = appName;
  53. _bundleIdentifier = bundleIdentifier;
  54. NSInteger location = [firebaseNamespace rangeOfString:@":"].location;
  55. if (location == NSNotFound) {
  56. FIRLogError(kFIRLoggerRemoteConfig, @"I-RCN000064",
  57. @"Error: Namespace %@ is not fully qualified app:namespace.", firebaseNamespace);
  58. _firebaseNamespace = firebaseNamespace;
  59. } else {
  60. _firebaseNamespace = [firebaseNamespace substringToIndex:location];
  61. }
  62. // Initialize the user defaults with a prefix and the bundleID. For app extensions, this will be
  63. // the bundleID of the app extension.
  64. _userDefaults =
  65. [RCNUserDefaultsManager sharedUserDefaultsForBundleIdentifier:_bundleIdentifier];
  66. }
  67. return self;
  68. }
  69. + (NSUserDefaults *)sharedUserDefaultsForBundleIdentifier:(NSString *)bundleIdentifier {
  70. static dispatch_once_t onceToken;
  71. static NSUserDefaults *sharedInstance;
  72. dispatch_once(&onceToken, ^{
  73. NSString *userDefaultsSuiteName =
  74. [RCNUserDefaultsManager userDefaultsSuiteNameForBundleIdentifier:bundleIdentifier];
  75. sharedInstance = [[NSUserDefaults alloc] initWithSuiteName:userDefaultsSuiteName];
  76. });
  77. return sharedInstance;
  78. }
  79. + (NSString *)userDefaultsSuiteNameForBundleIdentifier:(NSString *)bundleIdentifier {
  80. NSString *suiteName =
  81. [NSString stringWithFormat:@"%@.%@.%@", kRCNGroupPrefix, bundleIdentifier, kRCNGroupSuffix];
  82. return suiteName;
  83. }
  84. #pragma mark Public properties.
  85. - (NSString *)lastETag {
  86. return [[self instanceUserDefaults] objectForKey:kRCNUserDefaultsKeyNamelastETag];
  87. }
  88. - (void)setLastETag:(NSString *)lastETag {
  89. if (lastETag) {
  90. [self setInstanceUserDefaultsValue:lastETag forKey:kRCNUserDefaultsKeyNamelastETag];
  91. }
  92. }
  93. - (NSTimeInterval)lastFetchTime {
  94. NSNumber *lastFetchTime =
  95. [[self instanceUserDefaults] objectForKey:kRCNUserDefaultsKeyNameLastSuccessfulFetchTime];
  96. return lastFetchTime.doubleValue;
  97. }
  98. - (void)setLastFetchTime:(NSTimeInterval)lastFetchTime {
  99. [self setInstanceUserDefaultsValue:@(lastFetchTime)
  100. forKey:kRCNUserDefaultsKeyNameLastSuccessfulFetchTime];
  101. }
  102. - (NSString *)lastFetchStatus {
  103. return [[self instanceUserDefaults] objectForKey:kRCNUserDefaultsKeyNamelastFetchStatus];
  104. }
  105. - (void)setLastFetchStatus:(NSString *)lastFetchStatus {
  106. if (lastFetchStatus) {
  107. [self setInstanceUserDefaultsValue:lastFetchStatus
  108. forKey:kRCNUserDefaultsKeyNamelastFetchStatus];
  109. }
  110. }
  111. - (BOOL)isClientThrottledWithExponentialBackoff {
  112. NSNumber *isClientThrottled =
  113. [[self instanceUserDefaults] objectForKey:kRCNUserDefaultsKeyNameIsClientThrottled];
  114. return isClientThrottled.boolValue;
  115. }
  116. - (void)setIsClientThrottledWithExponentialBackoff:(BOOL)isClientThrottled {
  117. [self setInstanceUserDefaultsValue:@(isClientThrottled)
  118. forKey:kRCNUserDefaultsKeyNameIsClientThrottled];
  119. }
  120. - (NSTimeInterval)throttleEndTime {
  121. NSNumber *throttleEndTime =
  122. [[self instanceUserDefaults] objectForKey:kRCNUserDefaultsKeyNameThrottleEndTime];
  123. return throttleEndTime.doubleValue;
  124. }
  125. - (void)setThrottleEndTime:(NSTimeInterval)throttleEndTime {
  126. [self setInstanceUserDefaultsValue:@(throttleEndTime)
  127. forKey:kRCNUserDefaultsKeyNameThrottleEndTime];
  128. }
  129. - (NSTimeInterval)currentThrottlingRetryIntervalSeconds {
  130. NSNumber *throttleEndTime = [[self instanceUserDefaults]
  131. objectForKey:kRCNUserDefaultsKeyNamecurrentThrottlingRetryInterval];
  132. return throttleEndTime.doubleValue;
  133. }
  134. - (void)setCurrentThrottlingRetryIntervalSeconds:(NSTimeInterval)throttlingRetryIntervalSeconds {
  135. [self setInstanceUserDefaultsValue:@(throttlingRetryIntervalSeconds)
  136. forKey:kRCNUserDefaultsKeyNamecurrentThrottlingRetryInterval];
  137. }
  138. #pragma mark Private methods.
  139. // There is a nested hierarchy for the userdefaults as follows:
  140. // [FIRAppName][FIRNamespaceName][Key]
  141. - (nonnull NSDictionary *)appUserDefaults {
  142. NSString *appPath = _firebaseAppName;
  143. NSDictionary *appDict = [_userDefaults valueForKeyPath:appPath];
  144. if (!appDict) {
  145. appDict = [[NSDictionary alloc] init];
  146. }
  147. return appDict;
  148. }
  149. // Search for the user defaults for this (app, namespace) instance using the valueForKeyPath method.
  150. - (nonnull NSDictionary *)instanceUserDefaults {
  151. NSString *appNamespacePath =
  152. [NSString stringWithFormat:@"%@.%@", _firebaseAppName, _firebaseNamespace];
  153. NSDictionary *appNamespaceDict = [_userDefaults valueForKeyPath:appNamespacePath];
  154. if (!appNamespaceDict) {
  155. appNamespaceDict = [[NSMutableDictionary alloc] init];
  156. }
  157. return appNamespaceDict;
  158. }
  159. // Update users defaults for just this (app, namespace) instance.
  160. - (void)setInstanceUserDefaultsValue:(NSObject *)value forKey:(NSString *)key {
  161. @synchronized(_userDefaults) {
  162. NSMutableDictionary *appUserDefaults = [[self appUserDefaults] mutableCopy];
  163. NSMutableDictionary *appNamespaceUserDefaults = [[self instanceUserDefaults] mutableCopy];
  164. [appNamespaceUserDefaults setObject:value forKey:key];
  165. [appUserDefaults setObject:appNamespaceUserDefaults forKey:_firebaseNamespace];
  166. [_userDefaults setObject:appUserDefaults forKey:_firebaseAppName];
  167. // We need to synchronize to have this value updated for the extension.
  168. [_userDefaults synchronize];
  169. }
  170. }
  171. @end