Skip to content

Commit

Permalink
Merge branch 'release-8.0' into ah/oslog-migration
Browse files Browse the repository at this point in the history
# Conflicts:
#	GoogleUtilities/UserDefaults/GULUserDefaults.m
  • Loading branch information
andrewheard committed Jul 2, 2024
2 parents 34f11f2 + 83fa5a9 commit 7f34d22
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 115 deletions.
6 changes: 2 additions & 4 deletions GoogleUtilities/AppDelegateSwizzler/GULAppDelegateSwizzler.m
Original file line number Diff line number Diff line change
Expand Up @@ -702,9 +702,7 @@ - (BOOL)application:(GULApplication *)application

#endif // TARGET_OS_IOS || TARGET_OS_TV

// TODO(Xcode 15): When Xcode 15 is the minimum supported Xcode version,
// it will be unnecessary to check if `TARGET_OS_VISION` is defined.
#if TARGET_OS_IOS && (!defined(TARGET_OS_VISION) || !TARGET_OS_VISION)
#if TARGET_OS_IOS

- (BOOL)application:(GULApplication *)application
openURL:(NSURL *)url
Expand Down Expand Up @@ -737,7 +735,7 @@ - (BOOL)application:(GULApplication *)application
return returnedValue;
}

#endif // TARGET_OS_IOS && (!defined(TARGET_OS_VISION) || !TARGET_OS_VISION)
#endif // TARGET_OS_IOS

#pragma mark - [Donor Methods] Network overridden handler methods

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

#import <Foundation/Foundation.h>

#if TARGET_OS_IOS || TARGET_OS_TV || (defined(TARGET_OS_VISION) && TARGET_OS_VISION)
#if TARGET_OS_IOS || TARGET_OS_TV || TARGET_OS_VISION

#import <UIKit/UIKit.h>

Expand Down
9 changes: 4 additions & 5 deletions GoogleUtilities/Environment/GULAppEnvironmentUtil.m
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ + (NSString *)deviceSimulatorModel {
model = @"watchOS Simulator";
#elif TARGET_OS_TV
model = @"tvOS Simulator";
#elif defined(TARGET_OS_VISION) && TARGET_OS_VISION
#elif TARGET_OS_VISION
model = @"visionOS Simulator";
#elif TARGET_OS_IOS
switch ([[UIDevice currentDevice] userInterfaceIdiom]) {
Expand Down Expand Up @@ -195,8 +195,7 @@ + (NSString *)deviceSimulatorModel {
+ (NSString *)systemVersion {
#if TARGET_OS_IOS
return [UIDevice currentDevice].systemVersion;
#elif TARGET_OS_OSX || TARGET_OS_TV || TARGET_OS_WATCH || \
(defined(TARGET_OS_VISION) && TARGET_OS_VISION)
#elif TARGET_OS_OSX || TARGET_OS_TV || TARGET_OS_WATCH || TARGET_OS_VISION
// Assemble the systemVersion, excluding the patch version if it's 0.
NSOperatingSystemVersion osVersion = [NSProcessInfo processInfo].operatingSystemVersion;
NSMutableString *versionString = [[NSMutableString alloc]
Expand Down Expand Up @@ -225,7 +224,7 @@ + (NSString *)applePlatform {
// `true`, which means the condition list is order-sensitive.
#if TARGET_OS_MACCATALYST
applePlatform = @"maccatalyst";
#elif TARGET_OS_IOS && (!defined(TARGET_OS_VISION) || !TARGET_OS_VISION)
#elif TARGET_OS_IOS
if (@available(iOS 14.0, *)) {
// Early iOS 14 betas do not include isiOSAppOnMac (#6969)
applePlatform = ([[NSProcessInfo processInfo] respondsToSelector:@selector(isiOSAppOnMac)] &&
Expand All @@ -241,7 +240,7 @@ + (NSString *)applePlatform {
applePlatform = @"macos";
#elif TARGET_OS_WATCH
applePlatform = @"watchos";
#elif defined(TARGET_OS_VISION) && TARGET_OS_VISION
#elif TARGET_OS_VISION
applePlatform = @"visionos";
#endif // TARGET_OS_MACCATALYST

Expand Down
10 changes: 4 additions & 6 deletions GoogleUtilities/Environment/NetworkInfo/GULNetworkInfo.m
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,11 @@ + (GULNetworkType)getNetworkType {
+ (NSString *)getNetworkRadioType {
#ifdef TARGET_HAS_MOBILE_CONNECTIVITY
CTTelephonyNetworkInfo *networkInfo = [GULNetworkInfo getNetworkInfo];
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
return networkInfo.currentRadioAccessTechnology;
#pragma clang diagnostic pop
#else
return @"";
if (networkInfo.serviceCurrentRadioAccessTechnology.count) {
return networkInfo.serviceCurrentRadioAccessTechnology.allValues[0] ?: @"";
}
#endif
return @"";
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
<string>NSPrivacyAccessedAPICategoryUserDefaults</string>
<key>NSPrivacyAccessedAPITypeReasons</key>
<array>
<string>1C8F.1</string>
<string>C56D.1</string>
</array>
</dict>
Expand Down
26 changes: 9 additions & 17 deletions GoogleUtilities/Network/GULNetworkURLSession.m
Original file line number Diff line number Diff line change
Expand Up @@ -387,28 +387,20 @@ - (void)URLSession:(NSURLSession *)session
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

dispatch_async(evaluateBackgroundQueue, ^{
SecTrustResultType trustEval = kSecTrustResultInvalid;
BOOL shouldAllow;
OSStatus trustError;
CFErrorRef errorRef = NULL;

@synchronized([GULNetworkURLSession class]) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
trustError = SecTrustEvaluate(serverTrust, &trustEval);
#pragma clang diagnostic pop
shouldAllow = SecTrustEvaluateWithError(serverTrust, &errorRef);
}

if (trustError != errSecSuccess) {
[self->_loggerDelegate GULNetwork_logWithLevel:kGULNetworkLogLevelError
messageCode:kGULNetworkMessageCodeURLSession008
message:@"Cannot evaluate server trust. Error, host"
contexts:@[ @(trustError), self->_request.URL ]];
shouldAllow = NO;
} else {
// Having a trust level "unspecified" by the user is the usual result, described at
// https://developer.apple.com/library/mac/qa/qa1360
shouldAllow =
(trustEval == kSecTrustResultUnspecified || trustEval == kSecTrustResultProceed);
if (errorRef) {
[self->_loggerDelegate
GULNetwork_logWithLevel:kGULNetworkLogLevelError
messageCode:kGULNetworkMessageCodeURLSession008
message:@"Cannot evaluate server trust. Error, host"
contexts:@[ @((int)CFErrorGetCode(errorRef)), self->_request.URL ]];
CFRelease(errorRef);
}

// Call the call back with the permission.
Expand Down
1 change: 0 additions & 1 deletion GoogleUtilities/Privacy/Resources/PrivacyInfo.xcprivacy
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
<string>NSPrivacyAccessedAPICategoryUserDefaults</string>
<key>NSPrivacyAccessedAPITypeReasons</key>
<array>
<string>1C8F.1</string>
<string>C56D.1</string>
</array>
</dict>
Expand Down
4 changes: 2 additions & 2 deletions GoogleUtilities/Reachability/GULReachabilityChecker.m
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ - (GULReachabilityStatus)statusForFlags:(SCNetworkReachabilityFlags)flags {
// Reachable flag is set. Check further flags.
if (!(flags & kSCNetworkReachabilityFlagsConnectionRequired)) {
// Connection required flag is not set, so we have connectivity.
#if TARGET_OS_IOS || TARGET_OS_TV || (defined(TARGET_OS_VISION) && TARGET_OS_VISION)
#if TARGET_OS_IOS || TARGET_OS_TV || TARGET_OS_VISION
status = (flags & kSCNetworkReachabilityFlagsIsWWAN) ? kGULReachabilityViaCellular
: kGULReachabilityViaWifi;
#elif TARGET_OS_OSX
Expand All @@ -191,7 +191,7 @@ - (GULReachabilityStatus)statusForFlags:(SCNetworkReachabilityFlags)flags {
!(flags & kSCNetworkReachabilityFlagsInterventionRequired)) {
// If the connection on demand or connection on traffic flag is set, and user intervention
// is not required, we have connectivity.
#if TARGET_OS_IOS || TARGET_OS_TV || (defined(TARGET_OS_VISION) && TARGET_OS_VISION)
#if TARGET_OS_IOS || TARGET_OS_TV || TARGET_OS_VISION
status = (flags & kSCNetworkReachabilityFlagsIsWWAN) ? kGULReachabilityViaCellular
: kGULReachabilityViaWifi;
#elif TARGET_OS_OSX
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ - (void)testApplePlatform {
// and `TARGET_OS_IOS` are `true` when building a macCatalyst app.
#if TARGET_OS_MACCATALYST
NSString *expectedPlatform = @"maccatalyst";
#elif TARGET_OS_IOS && (!defined(TARGET_OS_VISION) || !TARGET_OS_VISION)
#elif TARGET_OS_IOS
NSString *expectedPlatform = @"ios";
#elif TARGET_OS_TV
NSString *expectedPlatform = @"tvos";
Expand All @@ -106,9 +106,9 @@ - (void)testApplePlatform {
NSString *expectedPlatform = @"watchos";
#endif // TARGET_OS_MACCATALYST

#if defined(TARGET_OS_VISION) && TARGET_OS_VISION
#if TARGET_OS_VISION
NSString *expectedPlatform = @"visionos";
#endif // defined(TARGET_OS_VISION) && TARGET_OS_VISION
#endif // TARGET_OS_VISION

XCTAssertEqualObjects([GULAppEnvironmentUtil applePlatform], expectedPlatform);
}
Expand All @@ -118,7 +118,7 @@ - (void)testAppleDevicePlatform {
// `true`.
#if TARGET_OS_MACCATALYST
NSString *expectedPlatform = @"maccatalyst";
#elif TARGET_OS_IOS && (!defined(TARGET_OS_VISION) || !TARGET_OS_VISION)
#elif TARGET_OS_IOS
NSString *expectedPlatform = @"ios";

if ([[UIDevice currentDevice].model.lowercaseString containsString:@"ipad"] ||
Expand All @@ -139,9 +139,9 @@ - (void)testAppleDevicePlatform {
NSString *expectedPlatform = @"watchos";
#endif // TARGET_OS_WATCH

#if defined(TARGET_OS_VISION) && TARGET_OS_VISION
#if TARGET_OS_VISION
NSString *expectedPlatform = @"visionos";
#endif // defined(TARGET_OS_VISION) && TARGET_OS_VISION
#endif // TARGET_OS_VISION

XCTAssertEqualObjects([GULAppEnvironmentUtil appleDevicePlatform], expectedPlatform);
}
Expand Down
26 changes: 0 additions & 26 deletions GoogleUtilities/Tests/Unit/UserDefaults/GULUserDefaultsTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -363,30 +363,6 @@ - (void)testNewSharedUserDefaultsWithStandardUserDefaults {
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];
}

- (void)testUserDefaultNotifications {
// Test to ensure no notifications are sent with our implementation.
void (^callBlock)(NSNotification *) = ^(NSNotification *_Nonnull notification) {
XCTFail(@"A notification must not be sent for GULUserDefaults!");
};

id observer =
[[NSNotificationCenter defaultCenter] addObserverForName:NSUserDefaultsDidChangeNotification
object:nil
queue:nil
usingBlock:callBlock];
NSString *suiteName = @"test_suite_notification";
GULUserDefaults *newUserDefaults = [[GULUserDefaults alloc] initWithSuiteName:suiteName];
[newUserDefaults setObject:@"134" forKey:@"test-another"];
XCTAssertEqualObjects([newUserDefaults objectForKey:@"test-another"], @"134");
[newUserDefaults setObject:nil forKey:@"test-another"];
XCTAssertNil([newUserDefaults objectForKey:@"test-another"]);
[newUserDefaults synchronize];
[[NSNotificationCenter defaultCenter] removeObserver:observer];

// Remove the underlying reference file.
[self removePreferenceFileWithSuiteName:suiteName];
}

- (void)testSynchronizeToDisk {
#if TARGET_OS_OSX || TARGET_OS_MACCATALYST || SWIFT_PACKAGE
// `NSFileManager` has trouble reading the files in `~/Library` even though the
Expand All @@ -406,14 +382,12 @@ - (void)testSynchronizeToDisk {

GULUserDefaults *newUserDefaults = [[GULUserDefaults alloc] initWithSuiteName:suiteName];
[newUserDefaults setObject:@"134" forKey:@"test-another"];
[newUserDefaults synchronize];

XCTAssertTrue([fileManager fileExistsAtPath:filePath],
@"The user defaults file was not synchronized to disk.");

// Now get the file directly from disk.
XCTAssertTrue([fileManager fileExistsAtPath:filePath]);
[newUserDefaults synchronize];

[self removePreferenceFileWithSuiteName:suiteName];
#endif // TARGET_OS_OSX || TARGET_OS_MACCATALYST || SWIFT_PACKAGE
Expand Down
49 changes: 8 additions & 41 deletions GoogleUtilities/UserDefaults/GULUserDefaults.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,11 @@ typedef NS_ENUM(NSInteger, GULUDMessageCode) {

@interface GULUserDefaults ()

/// Equivalent to the suite name for NSUserDefaults.
@property(readonly) CFStringRef appNameRef;

@property(atomic) BOOL isPreferenceFileExcluded;
@property(nonatomic, readonly) NSUserDefaults *userDefaults;

@end

@implementation GULUserDefaults {
// The application name is the same with the suite name of the NSUserDefaults, and it is used for
// preferences.
CFStringRef _appNameRef;
}
@implementation GULUserDefaults

+ (GULUserDefaults *)standardUserDefaults {
static GULUserDefaults *standardUserDefaults;
Expand All @@ -63,27 +56,13 @@ - (instancetype)initWithSuiteName:(nullable NSString *)suiteName {
NSString *name = [suiteName copy];

if (self) {
// `kCFPreferencesCurrentApplication` maps to the same defaults database as
// `[NSUserDefaults standardUserDefaults]`.
_appNameRef =
name.length ? (__bridge_retained CFStringRef)name : kCFPreferencesCurrentApplication;
_userDefaults = name.length ? [[NSUserDefaults alloc] initWithSuiteName:name]
: [NSUserDefaults standardUserDefaults];
}

return self;
}

- (void)dealloc {
// If we're using a custom `_appNameRef` it needs to be released. If it's a constant, it shouldn't
// need to be released since we don't own it.
if (CFStringCompare(_appNameRef, kCFPreferencesCurrentApplication, 0) != kCFCompareEqualTo) {
CFRelease(_appNameRef);
}

[NSObject cancelPreviousPerformRequestsWithTarget:self
selector:@selector(synchronize)
object:nil];
}

- (nullable id)objectForKey:(NSString *)defaultName {
NSString *key = [defaultName copy];
if (![key isKindOfClass:[NSString class]] || !key.length) {
Expand All @@ -92,7 +71,8 @@ - (nullable id)objectForKey:(NSString *)defaultName {
@"Cannot get object for invalid user default key.");
return nil;
}
return (__bridge_transfer id)CFPreferencesCopyAppValue((__bridge CFStringRef)key, _appNameRef);

return [self.userDefaults objectForKey:key];
}

- (void)setObject:(nullable id)value forKey:(NSString *)defaultName {
Expand All @@ -104,8 +84,7 @@ - (void)setObject:(nullable id)value forKey:(NSString *)defaultName {
return;
}
if (!value) {
CFPreferencesSetAppValue((__bridge CFStringRef)key, NULL, _appNameRef);
[self synchronize];
[self.userDefaults removeObjectForKey:key];
return;
}
BOOL isAcceptableValue =
Expand All @@ -122,8 +101,7 @@ - (void)setObject:(nullable id)value forKey:(NSString *)defaultName {
return;
}

CFPreferencesSetAppValue((__bridge CFStringRef)key, (__bridge CFStringRef)value, _appNameRef);
[self synchronize];
[self.userDefaults setObject:value forKey:key];
}

- (void)removeObjectForKey:(NSString *)key {
Expand Down Expand Up @@ -182,17 +160,6 @@ - (void)setBool:(BOOL)boolValue forKey:(NSString *)defaultName {
[self setObject:@(boolValue) forKey:defaultName];
}

#pragma mark - Save data

- (void)synchronize {
if (!CFPreferencesAppSynchronize(_appNameRef)) {
GULOSLogError(
kGULLogSubsystem, kGULLogUserDefaultsService, NO,
[NSString stringWithFormat:kGULLogFormat, (long)GULUDMessageCodeSynchronizeFailed],
@"Cannot synchronize user defaults to disk");
}
}

@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,6 @@ NS_ASSUME_NONNULL_BEGIN
/// Equivalent to -[... setObject:nil forKey:defaultName]
- (void)removeObjectForKey:(NSString *)defaultName;

#pragma mark - Save data

/// Blocks the calling thread until all in-progress set operations have completed.
- (void)synchronize;

@end

NS_ASSUME_NONNULL_END

0 comments on commit 7f34d22

Please sign in to comment.