RCNUserDefaultsManager.m 9.4 KB

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