RCNUserDefaultsManager.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. static NSString *const kRCNUserDefaultsKeyNameRealtimeThrottleEndTime = @"throttleRealtimeEndTime";
  32. static NSString *const kRCNUserDefaultsKeyNameCurrentRealtimeThrottlingRetryInterval =
  33. @"currentRealtimeThrottlingRetryInterval";
  34. static NSString *const kRCNUserDefaultsKeyNameRealtimeRetryCount = @"realtimeRetryCount";
  35. @interface RCNUserDefaultsManager () {
  36. /// User Defaults instance for this bundleID. NSUserDefaults is guaranteed to be thread-safe.
  37. NSUserDefaults *_userDefaults;
  38. /// The suite name for this user defaults instance. It is a combination of a prefix and the
  39. /// bundleID. This is because you cannot use just the bundleID of the current app as the suite
  40. /// name when initializing user defaults.
  41. NSString *_userDefaultsSuiteName;
  42. /// The FIRApp that this instance is scoped within.
  43. NSString *_firebaseAppName;
  44. /// The Firebase Namespace that this instance is scoped within.
  45. NSString *_firebaseNamespace;
  46. /// The bundleID of the app. In case of an extension, this will be the bundleID of the parent app.
  47. NSString *_bundleIdentifier;
  48. }
  49. @end
  50. @implementation RCNUserDefaultsManager
  51. #pragma mark Initializers.
  52. /// Designated initializer.
  53. - (instancetype)initWithAppName:(NSString *)appName
  54. bundleID:(NSString *)bundleIdentifier
  55. namespace:(NSString *)firebaseNamespace {
  56. self = [super init];
  57. if (self) {
  58. _firebaseAppName = appName;
  59. _bundleIdentifier = bundleIdentifier;
  60. NSInteger location = [firebaseNamespace rangeOfString:@":"].location;
  61. if (location == NSNotFound) {
  62. FIRLogError(kFIRLoggerRemoteConfig, @"I-RCN000064",
  63. @"Error: Namespace %@ is not fully qualified app:namespace.", firebaseNamespace);
  64. _firebaseNamespace = firebaseNamespace;
  65. } else {
  66. _firebaseNamespace = [firebaseNamespace substringToIndex:location];
  67. }
  68. // Initialize the user defaults with a prefix and the bundleID. For app extensions, this will be
  69. // the bundleID of the app extension.
  70. _userDefaults =
  71. [RCNUserDefaultsManager sharedUserDefaultsForBundleIdentifier:_bundleIdentifier];
  72. }
  73. return self;
  74. }
  75. + (NSUserDefaults *)sharedUserDefaultsForBundleIdentifier:(NSString *)bundleIdentifier {
  76. static dispatch_once_t onceToken;
  77. static NSUserDefaults *sharedInstance;
  78. dispatch_once(&onceToken, ^{
  79. NSString *userDefaultsSuiteName =
  80. [RCNUserDefaultsManager userDefaultsSuiteNameForBundleIdentifier:bundleIdentifier];
  81. sharedInstance = [[NSUserDefaults alloc] initWithSuiteName:userDefaultsSuiteName];
  82. });
  83. return sharedInstance;
  84. }
  85. + (NSString *)userDefaultsSuiteNameForBundleIdentifier:(NSString *)bundleIdentifier {
  86. NSString *suiteName =
  87. [NSString stringWithFormat:@"%@.%@.%@", kRCNGroupPrefix, bundleIdentifier, kRCNGroupSuffix];
  88. return suiteName;
  89. }
  90. #pragma mark Public properties.
  91. - (NSString *)lastETag {
  92. return [[self instanceUserDefaults] objectForKey:kRCNUserDefaultsKeyNamelastETag];
  93. }
  94. - (void)setLastETag:(NSString *)lastETag {
  95. if (lastETag) {
  96. [self setInstanceUserDefaultsValue:lastETag forKey:kRCNUserDefaultsKeyNamelastETag];
  97. }
  98. }
  99. - (NSString *)lastFetchedTemplateVersion {
  100. NSDictionary *userDefaults = [self instanceUserDefaults];
  101. if ([userDefaults objectForKey:RCNFetchResponseKeyTemplateVersion]) {
  102. return [userDefaults objectForKey:RCNFetchResponseKeyTemplateVersion];
  103. }
  104. return @"0";
  105. }
  106. - (void)setLastFetchedTemplateVersion:(NSString *)templateVersion {
  107. if (templateVersion) {
  108. [self setInstanceUserDefaultsValue:templateVersion forKey:RCNFetchResponseKeyTemplateVersion];
  109. }
  110. }
  111. - (NSString *)lastActiveTemplateVersion {
  112. NSDictionary *userDefaults = [self instanceUserDefaults];
  113. if ([userDefaults objectForKey:RCNActiveKeyTemplateVersion]) {
  114. return [userDefaults objectForKey:RCNActiveKeyTemplateVersion];
  115. }
  116. return @"0";
  117. }
  118. - (void)setLastActiveTemplateVersion:(NSString *)templateVersion {
  119. if (templateVersion) {
  120. [self setInstanceUserDefaultsValue:templateVersion forKey:RCNActiveKeyTemplateVersion];
  121. }
  122. }
  123. - (NSTimeInterval)lastETagUpdateTime {
  124. NSNumber *lastETagUpdateTime =
  125. [[self instanceUserDefaults] objectForKey:kRCNUserDefaultsKeyNamelastETagUpdateTime];
  126. return lastETagUpdateTime.doubleValue;
  127. }
  128. - (void)setLastETagUpdateTime:(NSTimeInterval)lastETagUpdateTime {
  129. if (lastETagUpdateTime) {
  130. [self setInstanceUserDefaultsValue:@(lastETagUpdateTime)
  131. forKey:kRCNUserDefaultsKeyNamelastETagUpdateTime];
  132. }
  133. }
  134. - (NSTimeInterval)lastFetchTime {
  135. NSNumber *lastFetchTime =
  136. [[self instanceUserDefaults] objectForKey:kRCNUserDefaultsKeyNameLastSuccessfulFetchTime];
  137. return lastFetchTime.doubleValue;
  138. }
  139. - (void)setLastFetchTime:(NSTimeInterval)lastFetchTime {
  140. [self setInstanceUserDefaultsValue:@(lastFetchTime)
  141. forKey:kRCNUserDefaultsKeyNameLastSuccessfulFetchTime];
  142. }
  143. - (NSString *)lastFetchStatus {
  144. return [[self instanceUserDefaults] objectForKey:kRCNUserDefaultsKeyNamelastFetchStatus];
  145. }
  146. - (void)setLastFetchStatus:(NSString *)lastFetchStatus {
  147. if (lastFetchStatus) {
  148. [self setInstanceUserDefaultsValue:lastFetchStatus
  149. forKey:kRCNUserDefaultsKeyNamelastFetchStatus];
  150. }
  151. }
  152. - (BOOL)isClientThrottledWithExponentialBackoff {
  153. NSNumber *isClientThrottled =
  154. [[self instanceUserDefaults] objectForKey:kRCNUserDefaultsKeyNameIsClientThrottled];
  155. return isClientThrottled.boolValue;
  156. }
  157. - (void)setIsClientThrottledWithExponentialBackoff:(BOOL)isClientThrottled {
  158. [self setInstanceUserDefaultsValue:@(isClientThrottled)
  159. forKey:kRCNUserDefaultsKeyNameIsClientThrottled];
  160. }
  161. - (NSTimeInterval)throttleEndTime {
  162. NSNumber *throttleEndTime =
  163. [[self instanceUserDefaults] objectForKey:kRCNUserDefaultsKeyNameThrottleEndTime];
  164. return throttleEndTime.doubleValue;
  165. }
  166. - (void)setThrottleEndTime:(NSTimeInterval)throttleEndTime {
  167. [self setInstanceUserDefaultsValue:@(throttleEndTime)
  168. forKey:kRCNUserDefaultsKeyNameThrottleEndTime];
  169. }
  170. - (NSTimeInterval)currentThrottlingRetryIntervalSeconds {
  171. NSNumber *throttleEndTime = [[self instanceUserDefaults]
  172. objectForKey:kRCNUserDefaultsKeyNamecurrentThrottlingRetryInterval];
  173. return throttleEndTime.doubleValue;
  174. }
  175. - (void)setCurrentThrottlingRetryIntervalSeconds:(NSTimeInterval)throttlingRetryIntervalSeconds {
  176. [self setInstanceUserDefaultsValue:@(throttlingRetryIntervalSeconds)
  177. forKey:kRCNUserDefaultsKeyNamecurrentThrottlingRetryInterval];
  178. }
  179. - (int)realtimeRetryCount {
  180. int realtimeRetryCount = 0;
  181. if ([[self instanceUserDefaults] objectForKey:kRCNUserDefaultsKeyNameRealtimeRetryCount]) {
  182. realtimeRetryCount = [[[self instanceUserDefaults]
  183. objectForKey:kRCNUserDefaultsKeyNameRealtimeRetryCount] intValue];
  184. }
  185. return realtimeRetryCount;
  186. }
  187. - (void)setRealtimeRetryCount:(int)realtimeRetryCount {
  188. [self setInstanceUserDefaultsValue:[NSNumber numberWithInt:realtimeRetryCount]
  189. forKey:kRCNUserDefaultsKeyNameRealtimeRetryCount];
  190. }
  191. - (NSTimeInterval)realtimeThrottleEndTime {
  192. NSNumber *realtimeThrottleEndTime = 0;
  193. if ([[self instanceUserDefaults] objectForKey:kRCNUserDefaultsKeyNameRealtimeThrottleEndTime]) {
  194. realtimeThrottleEndTime =
  195. [[self instanceUserDefaults] objectForKey:kRCNUserDefaultsKeyNameRealtimeThrottleEndTime];
  196. }
  197. return realtimeThrottleEndTime.doubleValue;
  198. }
  199. - (void)setRealtimeThrottleEndTime:(NSTimeInterval)throttleEndTime {
  200. [self setInstanceUserDefaultsValue:@(throttleEndTime)
  201. forKey:kRCNUserDefaultsKeyNameRealtimeThrottleEndTime];
  202. }
  203. - (NSTimeInterval)currentRealtimeThrottlingRetryIntervalSeconds {
  204. NSNumber *realtimeThrottleEndTime = 0;
  205. if ([[self instanceUserDefaults]
  206. objectForKey:kRCNUserDefaultsKeyNameCurrentRealtimeThrottlingRetryInterval]) {
  207. realtimeThrottleEndTime = [[self instanceUserDefaults]
  208. objectForKey:kRCNUserDefaultsKeyNameCurrentRealtimeThrottlingRetryInterval];
  209. }
  210. return realtimeThrottleEndTime.doubleValue;
  211. }
  212. - (void)setCurrentRealtimeThrottlingRetryIntervalSeconds:
  213. (NSTimeInterval)throttlingRetryIntervalSeconds {
  214. [self setInstanceUserDefaultsValue:@(throttlingRetryIntervalSeconds)
  215. forKey:kRCNUserDefaultsKeyNameCurrentRealtimeThrottlingRetryInterval];
  216. }
  217. #pragma mark Public methods.
  218. - (void)resetUserDefaults {
  219. [self resetInstanceUserDefaults];
  220. }
  221. #pragma mark Private methods.
  222. // There is a nested hierarchy for the userdefaults as follows:
  223. // [FIRAppName][FIRNamespaceName][Key]
  224. - (nonnull NSDictionary *)appUserDefaults {
  225. NSString *appPath = _firebaseAppName;
  226. NSDictionary *appDict = [_userDefaults valueForKeyPath:appPath];
  227. if (!appDict) {
  228. appDict = [[NSDictionary alloc] init];
  229. }
  230. return appDict;
  231. }
  232. // Search for the user defaults for this (app, namespace) instance using the valueForKeyPath method.
  233. - (nonnull NSDictionary *)instanceUserDefaults {
  234. NSString *appNamespacePath =
  235. [NSString stringWithFormat:@"%@.%@", _firebaseAppName, _firebaseNamespace];
  236. NSDictionary *appNamespaceDict = [_userDefaults valueForKeyPath:appNamespacePath];
  237. if (!appNamespaceDict) {
  238. appNamespaceDict = [[NSMutableDictionary alloc] init];
  239. }
  240. return appNamespaceDict;
  241. }
  242. // Update users defaults for just this (app, namespace) instance.
  243. - (void)setInstanceUserDefaultsValue:(NSObject *)value forKey:(NSString *)key {
  244. @synchronized(_userDefaults) {
  245. NSMutableDictionary *appUserDefaults = [[self appUserDefaults] mutableCopy];
  246. NSMutableDictionary *appNamespaceUserDefaults = [[self instanceUserDefaults] mutableCopy];
  247. [appNamespaceUserDefaults setObject:value forKey:key];
  248. [appUserDefaults setObject:appNamespaceUserDefaults forKey:_firebaseNamespace];
  249. [_userDefaults setObject:appUserDefaults forKey:_firebaseAppName];
  250. // We need to synchronize to have this value updated for the extension.
  251. [_userDefaults synchronize];
  252. }
  253. }
  254. // Delete any existing userdefaults for this instance.
  255. - (void)resetInstanceUserDefaults {
  256. @synchronized(_userDefaults) {
  257. NSMutableDictionary *appUserDefaults = [[self appUserDefaults] mutableCopy];
  258. NSMutableDictionary *appNamespaceUserDefaults = [[self instanceUserDefaults] mutableCopy];
  259. [appNamespaceUserDefaults removeAllObjects];
  260. [appUserDefaults setObject:appNamespaceUserDefaults forKey:_firebaseNamespace];
  261. [_userDefaults setObject:appUserDefaults forKey:_firebaseAppName];
  262. // We need to synchronize to have this value updated for the extension.
  263. [_userDefaults synchronize];
  264. }
  265. }
  266. @end