Skip to content

Commit 4ee3288

Browse files
authored
Fix deprecation warnings by consolidating coding calls (#5131)
* Fix deprecation warnings by consolidating coding calls Fixes #5086 * Fix all NSSecureCoding references * Fix changed API usage
1 parent 98aba2e commit 4ee3288

File tree

9 files changed

+188
-161
lines changed

9 files changed

+188
-161
lines changed

GoogleDataTransport/GDTCORLibrary/GDTCORPlatform.m

Lines changed: 80 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ GDTCORNetworkMobileSubtype GDTCORNetworkMobileSubTypeMessage() {
9393
if (networkCurrentRadioAccessTechnologyDict.count) {
9494
networkCurrentRadioAccessTechnology = networkCurrentRadioAccessTechnologyDict.allValues[0];
9595
}
96-
#else
96+
#else // TARGET_OS_MACCATALYST
97+
#if defined(__IPHONE_12_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 120000
9798
if (@available(iOS 12.0, *)) {
9899
NSDictionary<NSString *, NSString *> *networkCurrentRadioAccessTechnologyDict =
99100
networkInfo.serviceCurrentRadioAccessTechnology;
@@ -103,9 +104,11 @@ GDTCORNetworkMobileSubtype GDTCORNetworkMobileSubTypeMessage() {
103104
networkCurrentRadioAccessTechnology = networkCurrentRadioAccessTechnologyDict.allValues[0];
104105
}
105106
} else {
106-
networkCurrentRadioAccessTechnology = networkInfo.currentRadioAccessTechnology;
107+
#else // defined(__IPHONE_12_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 120000
108+
networkCurrentRadioAccessTechnology = networkInfo.currentRadioAccessTechnology;
109+
#endif // // defined(__IPHONE_12_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 120000
107110
}
108-
#endif
111+
#endif // TARGET_OS_MACCATALYST
109112
if (networkCurrentRadioAccessTechnology) {
110113
NSNumber *networkMobileSubtype =
111114
CTRadioAccessTechnologyToNetworkSubTypeMessage[networkCurrentRadioAccessTechnology];
@@ -118,6 +121,80 @@ GDTCORNetworkMobileSubtype GDTCORNetworkMobileSubTypeMessage() {
118121
#endif
119122
}
120123

124+
NSData *_Nullable GDTCOREncodeArchive(id<NSSecureCoding> obj,
125+
NSString *archivePath,
126+
NSError *_Nullable *error) {
127+
NSData *resultData;
128+
#if (defined(__IPHONE_11_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000) || \
129+
(defined(__MAC_10_13) && MAC_OS_X_VERSION_MAX_ALLOWED >= 101300) || \
130+
(defined(__TVOS_11_0) && __TV_OS_VERSION_MAX_ALLOWED >= 110000) || \
131+
(defined(__WATCHOS_4_0) && __WATCH_OS_VERSION_MAX_ALLOWED >= 040000) || \
132+
(defined(TARGET_OS_MACCATALYST) && TARGET_OS_MACCATALYST)
133+
if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, watchOS 4, *)) {
134+
resultData = [NSKeyedArchiver archivedDataWithRootObject:obj
135+
requiringSecureCoding:YES
136+
error:error];
137+
BOOL result = [resultData writeToFile:archivePath atomically:YES];
138+
result = result; // To get rid of the warning.
139+
GDTCORLogDebug(@"Attempt to write archive. successful:%@ path:%@", result, archivePath);
140+
} else {
141+
#endif
142+
BOOL result = NO;
143+
@try {
144+
#pragma clang diagnostic push
145+
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
146+
resultData = [NSKeyedArchiver archivedDataWithRootObject:obj];
147+
#pragma clang diagnostic pop
148+
result = [resultData writeToFile:archivePath atomically:YES];
149+
} @catch (NSException *exception) {
150+
NSString *errorString =
151+
[NSString stringWithFormat:@"An exception was thrown during encoding: %@", exception];
152+
*error = [NSError errorWithDomain:NSCocoaErrorDomain
153+
code:-1
154+
userInfo:@{NSLocalizedFailureReasonErrorKey : errorString}];
155+
}
156+
GDTCORLogDebug(@"Attempt to write archive. successful:%@ path:%@", result, archivePath);
157+
}
158+
return resultData;
159+
}
160+
161+
id<NSSecureCoding> _Nullable GDTCORDecodeArchive(Class archiveClass,
162+
NSString *_Nullable archivePath,
163+
NSData *_Nullable archiveData,
164+
NSError *_Nullable *error) {
165+
id<NSSecureCoding> unarchivedObject = nil;
166+
#if (defined(__IPHONE_11_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000) || \
167+
(defined(__MAC_10_13) && MAC_OS_X_VERSION_MAX_ALLOWED >= 101300) || \
168+
(defined(__TVOS_11_0) && __TV_OS_VERSION_MAX_ALLOWED >= 110000) || \
169+
(defined(__WATCHOS_4_0) && __WATCH_OS_VERSION_MAX_ALLOWED >= 040000) || \
170+
(defined(TARGET_OS_MACCATALYST) && TARGET_OS_MACCATALYST)
171+
if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, watchOS 4, *)) {
172+
NSData *data = archiveData ? archiveData : [NSData dataWithContentsOfFile:archivePath];
173+
if (data) {
174+
unarchivedObject = [NSKeyedUnarchiver unarchivedObjectOfClass:archiveClass
175+
fromData:data
176+
error:error];
177+
}
178+
} else {
179+
#endif
180+
@try {
181+
NSData *archivedData =
182+
archiveData ? archiveData : [NSData dataWithContentsOfFile:archivePath];
183+
#pragma clang diagnostic push
184+
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
185+
unarchivedObject = [NSKeyedUnarchiver unarchiveObjectWithData:archivedData];
186+
#pragma clang diagnostic pop
187+
} @catch (NSException *exception) {
188+
NSString *errorString =
189+
[NSString stringWithFormat:@"An exception was thrown during encoding: %@", exception];
190+
*error = [NSError errorWithDomain:NSCocoaErrorDomain
191+
code:-1
192+
userInfo:@{NSLocalizedFailureReasonErrorKey : errorString}];
193+
}
194+
}
195+
return unarchivedObject;
196+
}
197+
121198
@interface GDTCORApplication ()
122199
/**
123200
Private flag to match the existing `readonly` public flag. This will be accurate for all platforms,

GoogleDataTransport/GDTCORLibrary/GDTCORStorage.m

Lines changed: 16 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -129,16 +129,10 @@ - (void)storeEvent:(GDTCOREvent *)event
129129
// Write state to disk if we're in the background.
130130
if ([[GDTCORApplication sharedApplication] isRunningInBackground]) {
131131
GDTCORLogDebug("%@", @"Saving storage state because the app is running in the background");
132-
if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) {
133-
NSError *error;
134-
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:self
135-
requiringSecureCoding:YES
136-
error:&error];
137-
[data writeToFile:[GDTCORStorage archivePath] atomically:YES];
138-
} else {
139-
#if !TARGET_OS_MACCATALYST && !TARGET_OS_WATCH
140-
[NSKeyedArchiver archiveRootObject:self toFile:[GDTCORStorage archivePath]];
141-
#endif
132+
NSError *error;
133+
GDTCOREncodeArchive(self, [GDTCORStorage archivePath], &error);
134+
if (error) {
135+
GDTCORLogDebug(@"Serializing GDTCORStorage to an archive failed: %@", error);
142136
}
143137
}
144138

@@ -227,16 +221,10 @@ - (void)addEventToTrackingCollections:(GDTCOREvent *)event {
227221
#pragma mark - GDTCORLifecycleProtocol
228222

229223
- (void)appWillForeground:(GDTCORApplication *)app {
230-
if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) {
231-
NSError *error;
232-
NSData *data = [NSData dataWithContentsOfFile:[GDTCORStorage archivePath]];
233-
if (data) {
234-
[NSKeyedUnarchiver unarchivedObjectOfClass:[GDTCORStorage class] fromData:data error:&error];
235-
}
236-
} else {
237-
#if !TARGET_OS_MACCATALYST && !TARGET_OS_WATCH
238-
[NSKeyedUnarchiver unarchiveObjectWithFile:[GDTCORStorage archivePath]];
239-
#endif
224+
NSError *error;
225+
GDTCORDecodeArchive([GDTCORStorage class], [GDTCORStorage archivePath], nil, &error);
226+
if (error) {
227+
GDTCORLogDebug(@"Deserializing GDTCORStorage from an archive failed: %@", error);
240228
}
241229
}
242230

@@ -250,17 +238,10 @@ - (void)appWillBackground:(GDTCORApplication *)app {
250238
[app endBackgroundTask:bgID];
251239
bgID = GDTCORBackgroundIdentifierInvalid;
252240
}];
253-
254-
if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) {
255-
NSError *error;
256-
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:self
257-
requiringSecureCoding:YES
258-
error:&error];
259-
[data writeToFile:[GDTCORStorage archivePath] atomically:YES];
260-
} else {
261-
#if !TARGET_OS_MACCATALYST && !TARGET_OS_WATCH
262-
[NSKeyedArchiver archiveRootObject:self toFile:[GDTCORStorage archivePath]];
263-
#endif
241+
NSError *error;
242+
GDTCOREncodeArchive(self, [GDTCORStorage archivePath], &error);
243+
if (error) {
244+
GDTCORLogDebug(@"Serializing GDTCORStorage to an archive failed: %@", error);
264245
}
265246

266247
// End the background task if it's still valid.
@@ -271,16 +252,10 @@ - (void)appWillBackground:(GDTCORApplication *)app {
271252

272253
- (void)appWillTerminate:(GDTCORApplication *)application {
273254
dispatch_sync(_storageQueue, ^{
274-
if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) {
275-
NSError *error;
276-
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:self
277-
requiringSecureCoding:YES
278-
error:&error];
279-
[data writeToFile:[GDTCORStorage archivePath] atomically:YES];
280-
} else {
281-
#if !TARGET_OS_MACCATALYST && !TARGET_OS_WATCH
282-
[NSKeyedArchiver archiveRootObject:self toFile:[GDTCORStorage archivePath]];
283-
#endif
255+
NSError *error;
256+
GDTCOREncodeArchive(self, [GDTCORStorage archivePath], &error);
257+
if (error) {
258+
GDTCORLogDebug(@"Serializing GDTCORStorage to an archive failed: %@", error);
284259
}
285260
});
286261
}

GoogleDataTransport/GDTCORLibrary/GDTCORUploadCoordinator.m

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,10 @@ - (instancetype)initWithCoder:(NSCoder *)aDecoder {
177177
GDTCORUploadCoordinator *sharedCoordinator = [GDTCORUploadCoordinator sharedInstance];
178178
dispatch_sync(sharedCoordinator->_coordinationQueue, ^{
179179
@try {
180+
NSSet *classes =
181+
[NSSet setWithObjects:[NSMutableDictionary class], [GDTCORUploadPackage class], nil];
180182
sharedCoordinator->_targetToInFlightPackages =
181-
[aDecoder decodeObjectOfClass:[NSMutableDictionary class]
182-
forKey:ktargetToInFlightPackagesKey];
183+
[aDecoder decodeObjectOfClasses:classes forKey:ktargetToInFlightPackagesKey];
183184

184185
} @catch (NSException *exception) {
185186
sharedCoordinator->_targetToInFlightPackages = [NSMutableDictionary dictionary];

GoogleDataTransport/GDTCORLibrary/Public/GDTCORPlatform.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,30 @@ GDTCORNetworkType GDTCORNetworkTypeMessage(void);
8888
*/
8989
GDTCORNetworkMobileSubtype GDTCORNetworkMobileSubTypeMessage(void);
9090

91+
/** Writes the given object to the given fileURL and populates the given error if it fails.
92+
*
93+
* @param obj The object to encode.
94+
* @param filePath The path to write the object to. Can be nil if you just need the data.
95+
* @param error The error to populate if something goes wrong.
96+
* @return The data of the archive. If error is nil, it's been written to disk.
97+
*/
98+
NSData *_Nullable GDTCOREncodeArchive(id<NSSecureCoding> obj,
99+
NSString *_Nullable filePath,
100+
NSError *_Nullable *error);
101+
102+
/** Decodes an object of the given class from the given archive path or data and populates the given
103+
* error if it fails.
104+
*
105+
* @param archiveClass The class of the archive's root object.
106+
* @param archivePath The path to the archived data. Don't use with the archiveData param.
107+
* @param archiveData The data to decode. Don't use with the archivePath param.
108+
* @param error The error to populate if something goes wrong.
109+
*/
110+
id<NSSecureCoding> _Nullable GDTCORDecodeArchive(Class archiveClass,
111+
NSString *_Nullable archivePath,
112+
NSData *_Nullable archiveData,
113+
NSError *_Nullable *error);
114+
91115
/** A typedef identify background identifiers. */
92116
typedef volatile NSUInteger GDTCORBackgroundIdentifier;
93117

GoogleDataTransport/GDTCORTests/Unit/GDTCORClockTest.m

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#import "GDTCORTests/Unit/GDTCORTestCase.h"
1818

1919
#import "GDTCORLibrary/Public/GDTCORClock.h"
20+
#import "GDTCORLibrary/Public/GDTCORPlatform.h"
2021

2122
@interface GDTCORClockTest : GDTCORTestCase
2223

@@ -51,20 +52,16 @@ - (void)testSupportsSecureEncoding {
5152
/** Tests encoding and decoding a clock using a keyed archiver. */
5253
- (void)testEncoding {
5354
GDTCORClock *clock = [GDTCORClock snapshot];
54-
GDTCORClock *unarchivedClock;
55-
if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) {
56-
NSData *clockData = [NSKeyedArchiver archivedDataWithRootObject:clock
57-
requiringSecureCoding:YES
58-
error:nil];
59-
unarchivedClock = [NSKeyedUnarchiver unarchivedObjectOfClass:[GDTCORClock class]
60-
fromData:clockData
61-
error:nil];
62-
} else {
63-
#if !TARGET_OS_MACCATALYST
64-
NSData *clockData = [NSKeyedArchiver archivedDataWithRootObject:clock];
65-
unarchivedClock = [NSKeyedUnarchiver unarchiveObjectWithData:clockData];
66-
#endif
67-
}
55+
NSError *error;
56+
NSData *clockData = GDTCOREncodeArchive(clock, nil, &error);
57+
XCTAssertNil(error);
58+
XCTAssertNotNil(clockData);
59+
60+
error = nil;
61+
GDTCORClock *unarchivedClock =
62+
(GDTCORClock *)GDTCORDecodeArchive([GDTCORClock class], nil, clockData, &error);
63+
XCTAssertNil(error);
64+
XCTAssertNotNil(unarchivedClock);
6865
XCTAssertEqual([clock hash], [unarchivedClock hash]);
6966
XCTAssertEqualObjects(clock, unarchivedClock);
7067
}

GoogleDataTransport/GDTCORTests/Unit/GDTCOREventTest.m

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
1414
* limitations under the License.
1515
*/
1616

17-
#import <GoogleDataTransport/GDTCORClock.h>
1817
#import <GoogleDataTransport/GDTCOREvent.h>
1918

19+
#import <GoogleDataTransport/GDTCORClock.h>
20+
#import <GoogleDataTransport/GDTCORPlatform.h>
21+
2022
#import "GDTCORTests/Unit/GDTCORTestCase.h"
2123
#import "GDTCORTests/Unit/Helpers/GDTCORDataObjectTesterClasses.h"
2224

@@ -45,28 +47,18 @@ - (void)testArchiving {
4547
event.qosTier = GDTCOREventQoSTelemetry;
4648
event.clockSnapshot = clockSnapshot;
4749

48-
NSData *archiveData;
49-
if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) {
50-
archiveData = [NSKeyedArchiver archivedDataWithRootObject:event
51-
requiringSecureCoding:YES
52-
error:nil];
53-
} else {
54-
#if !TARGET_OS_MACCATALYST
55-
archiveData = [NSKeyedArchiver archivedDataWithRootObject:event];
56-
#endif
57-
}
50+
NSError *error;
51+
NSData *archiveData = GDTCOREncodeArchive(event, nil, &error);
52+
XCTAssertNil(error);
53+
XCTAssertNotNil(archiveData);
54+
5855
// To ensure that all the objects being retained by the original event are dealloc'd.
5956
event = nil;
60-
GDTCOREvent *decodedEvent;
61-
if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) {
62-
decodedEvent = [NSKeyedUnarchiver unarchivedObjectOfClass:[GDTCOREvent class]
63-
fromData:archiveData
64-
error:nil];
65-
} else {
66-
#if !TARGET_OS_MACCATALYST
67-
decodedEvent = [NSKeyedUnarchiver unarchiveObjectWithData:archiveData];
68-
#endif
69-
}
57+
error = nil;
58+
GDTCOREvent *decodedEvent =
59+
(GDTCOREvent *)GDTCORDecodeArchive([GDTCOREvent class], nil, archiveData, &error);
60+
XCTAssertNil(error);
61+
XCTAssertNotNil(decodedEvent);
7062
XCTAssertEqualObjects(decodedEvent.mappingID, @"testID");
7163
XCTAssertEqual(decodedEvent.target, 42);
7264
event.dataObject = [[GDTCORDataObjectTesterSimple alloc] initWithString:@"someData"];

0 commit comments

Comments
 (0)