| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- /*
- * Copyright 2019 Google
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- #import "FirebaseRemoteConfig/Sources/RCNUserDefaultsManager.h"
- #import <FirebaseCore/FIRLogger.h>
- #import <FirebaseRemoteConfig/FIRRemoteConfig.h>
- static NSString *const kRCNGroupPrefix = @"group";
- static NSString *const kRCNGroupSuffix = @"firebase";
- static NSString *const kRCNUserDefaultsKeyNamelastETag = @"lastETag";
- static NSString *const kRCNUserDefaultsKeyNamelastETagUpdateTime = @"lastETagUpdateTime";
- static NSString *const kRCNUserDefaultsKeyNameLastSuccessfulFetchTime = @"lastSuccessfulFetchTime";
- static NSString *const kRCNUserDefaultsKeyNamelastFetchStatus = @"lastFetchStatus";
- static NSString *const kRCNUserDefaultsKeyNameIsClientThrottled =
- @"isClientThrottledWithExponentialBackoff";
- static NSString *const kRCNUserDefaultsKeyNameThrottleEndTime = @"throttleEndTime";
- static NSString *const kRCNUserDefaultsKeyNamecurrentThrottlingRetryInterval =
- @"currentThrottlingRetryInterval";
- @interface RCNUserDefaultsManager () {
- /// User Defaults instance for this bundleID. NSUserDefaults is guaranteed to be thread-safe.
- NSUserDefaults *_userDefaults;
- /// The suite name for this user defaults instance. It is a combination of a prefix and the
- /// bundleID. This is because you cannot use just the bundleID of the current app as the suite
- /// name when initializing user defaults.
- NSString *_userDefaultsSuiteName;
- /// The FIRApp that this instance is scoped within.
- NSString *_firebaseAppName;
- /// The Firebase Namespace that this instance is scoped within.
- NSString *_firebaseNamespace;
- /// The bundleID of the app. In case of an extension, this will be the bundleID of the parent app.
- NSString *_bundleIdentifier;
- }
- @end
- @implementation RCNUserDefaultsManager
- #pragma mark Initializers.
- /// Designated initializer.
- - (instancetype)initWithAppName:(NSString *)appName
- bundleID:(NSString *)bundleIdentifier
- namespace:(NSString *)firebaseNamespace {
- self = [super init];
- if (self) {
- _firebaseAppName = appName;
- _bundleIdentifier = bundleIdentifier;
- NSInteger location = [firebaseNamespace rangeOfString:@":"].location;
- if (location == NSNotFound) {
- FIRLogError(kFIRLoggerRemoteConfig, @"I-RCN000064",
- @"Error: Namespace %@ is not fully qualified app:namespace.", firebaseNamespace);
- _firebaseNamespace = firebaseNamespace;
- } else {
- _firebaseNamespace = [firebaseNamespace substringToIndex:location];
- }
- // Initialize the user defaults with a prefix and the bundleID. For app extensions, this will be
- // the bundleID of the app extension.
- _userDefaults =
- [RCNUserDefaultsManager sharedUserDefaultsForBundleIdentifier:_bundleIdentifier];
- }
- return self;
- }
- + (NSUserDefaults *)sharedUserDefaultsForBundleIdentifier:(NSString *)bundleIdentifier {
- static dispatch_once_t onceToken;
- static NSUserDefaults *sharedInstance;
- dispatch_once(&onceToken, ^{
- NSString *userDefaultsSuiteName =
- [RCNUserDefaultsManager userDefaultsSuiteNameForBundleIdentifier:bundleIdentifier];
- sharedInstance = [[NSUserDefaults alloc] initWithSuiteName:userDefaultsSuiteName];
- });
- return sharedInstance;
- }
- + (NSString *)userDefaultsSuiteNameForBundleIdentifier:(NSString *)bundleIdentifier {
- NSString *suiteName =
- [NSString stringWithFormat:@"%@.%@.%@", kRCNGroupPrefix, bundleIdentifier, kRCNGroupSuffix];
- return suiteName;
- }
- #pragma mark Public properties.
- - (NSString *)lastETag {
- return [[self instanceUserDefaults] objectForKey:kRCNUserDefaultsKeyNamelastETag];
- }
- - (void)setLastETag:(NSString *)lastETag {
- if (lastETag) {
- [self setInstanceUserDefaultsValue:lastETag forKey:kRCNUserDefaultsKeyNamelastETag];
- }
- }
- - (NSTimeInterval)lastETagUpdateTime {
- NSNumber *lastETagUpdateTime =
- [[self instanceUserDefaults] objectForKey:kRCNUserDefaultsKeyNamelastETagUpdateTime];
- return lastETagUpdateTime.doubleValue;
- }
- - (void)setLastETagUpdateTime:(NSTimeInterval)lastETagUpdateTime {
- if (lastETagUpdateTime) {
- [self setInstanceUserDefaultsValue:@(lastETagUpdateTime)
- forKey:kRCNUserDefaultsKeyNamelastETagUpdateTime];
- }
- }
- - (NSTimeInterval)lastFetchTime {
- NSNumber *lastFetchTime =
- [[self instanceUserDefaults] objectForKey:kRCNUserDefaultsKeyNameLastSuccessfulFetchTime];
- return lastFetchTime.doubleValue;
- }
- - (void)setLastFetchTime:(NSTimeInterval)lastFetchTime {
- [self setInstanceUserDefaultsValue:@(lastFetchTime)
- forKey:kRCNUserDefaultsKeyNameLastSuccessfulFetchTime];
- }
- - (NSString *)lastFetchStatus {
- return [[self instanceUserDefaults] objectForKey:kRCNUserDefaultsKeyNamelastFetchStatus];
- }
- - (void)setLastFetchStatus:(NSString *)lastFetchStatus {
- if (lastFetchStatus) {
- [self setInstanceUserDefaultsValue:lastFetchStatus
- forKey:kRCNUserDefaultsKeyNamelastFetchStatus];
- }
- }
- - (BOOL)isClientThrottledWithExponentialBackoff {
- NSNumber *isClientThrottled =
- [[self instanceUserDefaults] objectForKey:kRCNUserDefaultsKeyNameIsClientThrottled];
- return isClientThrottled.boolValue;
- }
- - (void)setIsClientThrottledWithExponentialBackoff:(BOOL)isClientThrottled {
- [self setInstanceUserDefaultsValue:@(isClientThrottled)
- forKey:kRCNUserDefaultsKeyNameIsClientThrottled];
- }
- - (NSTimeInterval)throttleEndTime {
- NSNumber *throttleEndTime =
- [[self instanceUserDefaults] objectForKey:kRCNUserDefaultsKeyNameThrottleEndTime];
- return throttleEndTime.doubleValue;
- }
- - (void)setThrottleEndTime:(NSTimeInterval)throttleEndTime {
- [self setInstanceUserDefaultsValue:@(throttleEndTime)
- forKey:kRCNUserDefaultsKeyNameThrottleEndTime];
- }
- - (NSTimeInterval)currentThrottlingRetryIntervalSeconds {
- NSNumber *throttleEndTime = [[self instanceUserDefaults]
- objectForKey:kRCNUserDefaultsKeyNamecurrentThrottlingRetryInterval];
- return throttleEndTime.doubleValue;
- }
- - (void)setCurrentThrottlingRetryIntervalSeconds:(NSTimeInterval)throttlingRetryIntervalSeconds {
- [self setInstanceUserDefaultsValue:@(throttlingRetryIntervalSeconds)
- forKey:kRCNUserDefaultsKeyNamecurrentThrottlingRetryInterval];
- }
- #pragma mark Public methods.
- - (void)resetUserDefaults {
- [self resetInstanceUserDefaults];
- }
- #pragma mark Private methods.
- // There is a nested hierarchy for the userdefaults as follows:
- // [FIRAppName][FIRNamespaceName][Key]
- - (nonnull NSDictionary *)appUserDefaults {
- NSString *appPath = _firebaseAppName;
- NSDictionary *appDict = [_userDefaults valueForKeyPath:appPath];
- if (!appDict) {
- appDict = [[NSDictionary alloc] init];
- }
- return appDict;
- }
- // Search for the user defaults for this (app, namespace) instance using the valueForKeyPath method.
- - (nonnull NSDictionary *)instanceUserDefaults {
- NSString *appNamespacePath =
- [NSString stringWithFormat:@"%@.%@", _firebaseAppName, _firebaseNamespace];
- NSDictionary *appNamespaceDict = [_userDefaults valueForKeyPath:appNamespacePath];
- if (!appNamespaceDict) {
- appNamespaceDict = [[NSMutableDictionary alloc] init];
- }
- return appNamespaceDict;
- }
- // Update users defaults for just this (app, namespace) instance.
- - (void)setInstanceUserDefaultsValue:(NSObject *)value forKey:(NSString *)key {
- @synchronized(_userDefaults) {
- NSMutableDictionary *appUserDefaults = [[self appUserDefaults] mutableCopy];
- NSMutableDictionary *appNamespaceUserDefaults = [[self instanceUserDefaults] mutableCopy];
- [appNamespaceUserDefaults setObject:value forKey:key];
- [appUserDefaults setObject:appNamespaceUserDefaults forKey:_firebaseNamespace];
- [_userDefaults setObject:appUserDefaults forKey:_firebaseAppName];
- // We need to synchronize to have this value updated for the extension.
- [_userDefaults synchronize];
- }
- }
- // Delete any existing userdefaults for this instance.
- - (void)resetInstanceUserDefaults {
- @synchronized(_userDefaults) {
- NSMutableDictionary *appUserDefaults = [[self appUserDefaults] mutableCopy];
- NSMutableDictionary *appNamespaceUserDefaults = [[self instanceUserDefaults] mutableCopy];
- [appNamespaceUserDefaults removeAllObjects];
- [appUserDefaults setObject:appNamespaceUserDefaults forKey:_firebaseNamespace];
- [_userDefaults setObject:appUserDefaults forKey:_firebaseAppName];
- // We need to synchronize to have this value updated for the extension.
- [_userDefaults synchronize];
- }
- }
- @end
|