RCNUserDefaultsManager.m 8.2 KB

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