1 // Copyright 2018 Google
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 #import "Private/GULUserDefaults.h"
17 #import <GoogleUtilities/GULLogger.h>
19 NS_ASSUME_NONNULL_BEGIN
21 static NSTimeInterval const kGULSynchronizeInterval = 1.0;
23 static NSString *const kGULLogFormat = @"I-GUL%06ld";
25 static GULLoggerService kGULLogUserDefaultsService = @"[GoogleUtilities/UserDefaults]";
27 typedef NS_ENUM(NSInteger, GULUDMessageCode) {
28 GULUDMessageCodeInvalidKeyGet = 1,
29 GULUDMessageCodeInvalidKeySet = 2,
30 GULUDMessageCodeInvalidObjectSet = 3,
31 GULUDMessageCodeSynchronizeFailed = 4,
34 @interface GULUserDefaults ()
36 /// Equivalent to the suite name for NSUserDefaults.
37 @property(readonly) CFStringRef appNameRef;
39 @property(atomic) BOOL isPreferenceFileExcluded;
43 @implementation GULUserDefaults {
44 // The application name is the same with the suite name of the NSUserDefaults, and it is used for
46 CFStringRef _appNameRef;
49 + (GULUserDefaults *)standardUserDefaults {
50 static GULUserDefaults *standardUserDefaults;
51 static dispatch_once_t onceToken;
52 dispatch_once(&onceToken, ^{
53 standardUserDefaults = [[GULUserDefaults alloc] init];
55 return standardUserDefaults;
58 - (instancetype)init {
59 return [self initWithSuiteName:nil];
62 - (instancetype)initWithSuiteName:(nullable NSString *)suiteName {
65 NSString *name = [suiteName copy];
68 // `kCFPreferencesCurrentApplication` maps to the same defaults database as
69 // `[NSUserDefaults standardUserDefaults]`.
71 name.length ? (__bridge_retained CFStringRef)name : kCFPreferencesCurrentApplication;
78 // If we're using a custom `_appNameRef` it needs to be released. If it's a constant, it shouldn't
79 // need to be released since we don't own it.
80 if (CFStringCompare(_appNameRef, kCFPreferencesCurrentApplication, 0) != kCFCompareEqualTo) {
81 CFRelease(_appNameRef);
84 [NSObject cancelPreviousPerformRequestsWithTarget:self
85 selector:@selector(synchronize)
89 - (nullable id)objectForKey:(NSString *)defaultName {
90 NSString *key = [defaultName copy];
91 if (![key isKindOfClass:[NSString class]] || !key.length) {
92 GULLogWarning(@"<GoogleUtilities>", NO,
93 [NSString stringWithFormat:kGULLogFormat, (long)GULUDMessageCodeInvalidKeyGet],
94 @"Cannot get object for invalid user default key.");
97 return (__bridge_transfer id)CFPreferencesCopyAppValue((__bridge CFStringRef)key, _appNameRef);
100 - (void)setObject:(nullable id)value forKey:(NSString *)defaultName {
101 NSString *key = [defaultName copy];
102 if (![key isKindOfClass:[NSString class]] || !key.length) {
103 GULLogWarning(kGULLogUserDefaultsService, NO,
104 [NSString stringWithFormat:kGULLogFormat, (long)GULUDMessageCodeInvalidKeySet],
105 @"Cannot set object for invalid user default key.");
109 CFPreferencesSetAppValue((__bridge CFStringRef)key, NULL, _appNameRef);
110 [self scheduleSynchronize];
113 BOOL isAcceptableValue =
114 [value isKindOfClass:[NSString class]] || [value isKindOfClass:[NSNumber class]] ||
115 [value isKindOfClass:[NSArray class]] || [value isKindOfClass:[NSDictionary class]] ||
116 [value isKindOfClass:[NSDate class]] || [value isKindOfClass:[NSData class]];
117 if (!isAcceptableValue) {
118 GULLogWarning(kGULLogUserDefaultsService, NO,
119 [NSString stringWithFormat:kGULLogFormat, (long)GULUDMessageCodeInvalidObjectSet],
120 @"Cannot set invalid object to user defaults. Must be a string, number, array, "
121 @"dictionary, date, or data. Value: %@",
126 CFPreferencesSetAppValue((__bridge CFStringRef)key, (__bridge CFStringRef)value, _appNameRef);
127 [self scheduleSynchronize];
130 - (void)removeObjectForKey:(NSString *)key {
131 [self setObject:nil forKey:key];
134 #pragma mark - Getters
136 - (NSInteger)integerForKey:(NSString *)defaultName {
137 NSNumber *object = [self objectForKey:defaultName];
138 return object.integerValue;
141 - (float)floatForKey:(NSString *)defaultName {
142 NSNumber *object = [self objectForKey:defaultName];
143 return object.floatValue;
146 - (double)doubleForKey:(NSString *)defaultName {
147 NSNumber *object = [self objectForKey:defaultName];
148 return object.doubleValue;
151 - (BOOL)boolForKey:(NSString *)defaultName {
152 NSNumber *object = [self objectForKey:defaultName];
153 return object.boolValue;
156 - (nullable NSString *)stringForKey:(NSString *)defaultName {
157 return [self objectForKey:defaultName];
160 - (nullable NSArray *)arrayForKey:(NSString *)defaultName {
161 return [self objectForKey:defaultName];
164 - (nullable NSDictionary<NSString *, id> *)dictionaryForKey:(NSString *)defaultName {
165 return [self objectForKey:defaultName];
168 #pragma mark - Setters
170 - (void)setInteger:(NSInteger)integer forKey:(NSString *)defaultName {
171 [self setObject:@(integer) forKey:defaultName];
174 - (void)setFloat:(float)value forKey:(NSString *)defaultName {
175 [self setObject:@(value) forKey:defaultName];
178 - (void)setDouble:(double)doubleNumber forKey:(NSString *)defaultName {
179 [self setObject:@(doubleNumber) forKey:defaultName];
182 - (void)setBool:(BOOL)boolValue forKey:(NSString *)defaultName {
183 [self setObject:@(boolValue) forKey:defaultName];
186 #pragma mark - Save data
188 - (void)synchronize {
189 if (!CFPreferencesAppSynchronize(_appNameRef)) {
190 GULLogError(kGULLogUserDefaultsService, NO,
191 [NSString stringWithFormat:kGULLogFormat, (long)GULUDMessageCodeSynchronizeFailed],
192 @"Cannot synchronize user defaults to disk");
196 #pragma mark - Private methods
198 /// Removes all values from the search list entry specified by 'domainName', the current user, and
199 /// any host. The change is persistent. Equivalent to -removePersistentDomainForName: of
201 - (void)clearAllData {
202 // On macOS, using `kCFPreferencesCurrentHost` will not set all the keys necessary to match
205 CFStringRef host = kCFPreferencesAnyHost;
207 CFStringRef host = kCFPreferencesCurrentHost;
208 #endif // TARGET_OS_OSX
210 CFArrayRef keyList = CFPreferencesCopyKeyList(_appNameRef, kCFPreferencesCurrentUser, host);
215 CFPreferencesSetMultiple(NULL, keyList, _appNameRef, kCFPreferencesCurrentUser, host);
217 [self scheduleSynchronize];
220 - (void)scheduleSynchronize {
221 // Synchronize data using a timer so that multiple set... calls can be coalesced under one
223 [NSObject cancelPreviousPerformRequestsWithTarget:self
224 selector:@selector(synchronize)
226 // This method may be called on multiple queues (due to set... methods can be called on any queue)
227 // synchronize can be scheduled on different queues, so make sure that it does not crash. If this
228 // instance goes away, self will be released also, no one will retain it and the schedule won't be
230 [self performSelector:@selector(synchronize) withObject:nil afterDelay:kGULSynchronizeInterval];
235 NS_ASSUME_NONNULL_END