Skip to content

Commit 3127b8c

Browse files
thatjiaoziJuan Jose Lopez Jaimez
andauthored
Add support for CCT EventCode field (#5767)
* Add support for eventCode property to GDTCCTEvent * Add support for eventCode property to GDTCCTEvent * Add support for eventCode property to GDTCCTEvent * Format Code * resolve the first round of comments * address last round of comments * Make GDTCOREvent+GDTCCTSupport a public category so EventCode can be set by developers Co-authored-by: Juan Jose Lopez Jaimez <[email protected]>
1 parent 538bae0 commit 3127b8c

File tree

8 files changed

+284
-106
lines changed

8 files changed

+284
-106
lines changed

GoogleDataTransportCCTSupport.podspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Support library to provide event prioritization and uploading for the GoogleData
3030

3131
s.source_files = 'GoogleDataTransportCCTSupport/GDTCCTLibrary/**/*'
3232
s.private_header_files = 'GoogleDataTransportCCTSupport/GDTCCTLibrary/Private/*.h'
33+
s.public_header_files = 'GoogleDataTransportCCTSupport/GDTCCTLibrary/Public/*.h'
3334
s.ios.frameworks = 'SystemConfiguration', 'CoreTelephony'
3435
s.osx.frameworks = 'SystemConfiguration', 'CoreTelephony'
3536
s.tvos.frameworks = 'SystemConfiguration'

GoogleDataTransportCCTSupport/GDTCCTLibrary/GDTCCTNanopbHelpers.m

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
#import <nanopb/pb_decode.h>
3232
#import <nanopb/pb_encode.h>
3333

34-
#import "GDTCCTLibrary/Private/GDTCOREvent+NetworkConnectionInfo.h"
34+
#import "GDTCCTLibrary/Public/GDTCOREvent+GDTCCTSupport.h"
3535

3636
#pragma mark - General purpose encoders
3737

@@ -145,6 +145,11 @@ gdt_cct_LogEvent GDTCCTConstructLogEvent(GDTCOREvent *event) {
145145
length:networkConnectionInfoData.length];
146146
logEvent.has_network_connection_info = 1;
147147
}
148+
NSNumber *eventCode = event.eventCode;
149+
if (eventCode != nil) {
150+
logEvent.has_event_code = 1;
151+
logEvent.event_code = [eventCode intValue];
152+
}
148153
}
149154
NSError *error;
150155
NSData *extensionBytes;

GoogleDataTransportCCTSupport/GDTCCTLibrary/GDTCCTPrioritizer.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#import <GoogleDataTransport/GDTCORTargets.h>
2424

2525
#import "GDTCCTLibrary/Private/GDTCCTNanopbHelpers.h"
26-
#import "GDTCCTLibrary/Private/GDTCOREvent+NetworkConnectionInfo.h"
26+
#import "GDTCCTLibrary/Public/GDTCOREvent+GDTCCTSupport.h"
2727

2828
const static int64_t kMillisPerDay = 8.64e+7;
2929

Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
/*
2+
* Copyright 2020 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+
17+
#import "GDTCCTLibrary/Public/GDTCOREvent+GDTCCTSupport.h"
18+
19+
#import <GoogleDataTransport/GDTCORConsoleLogger.h>
20+
21+
NSString *const GDTCCTNeedsNetworkConnectionInfo = @"needs_network_connection_info";
22+
23+
NSString *const GDTCCTNetworkConnectionInfo = @"network_connection_info";
24+
25+
NSString *const GDTCCTEventCodeInfo = @"event_code_info";
26+
27+
@implementation GDTCOREvent (GDTCCTSupport)
28+
29+
- (void)setNeedsNetworkConnectionInfoPopulated:(BOOL)needsNetworkConnectionInfoPopulated {
30+
if (!needsNetworkConnectionInfoPopulated) {
31+
if (!self.customBytes) {
32+
return;
33+
}
34+
35+
// Make sure we don't destroy the eventCode data, if any is present.
36+
@try {
37+
NSError *error;
38+
NSMutableDictionary *bytesDict =
39+
[[NSJSONSerialization JSONObjectWithData:self.customBytes options:0
40+
error:&error] mutableCopy];
41+
if (error) {
42+
GDTCORLogDebug(@"Error when setting an event's event_code: %@", error);
43+
return;
44+
}
45+
NSNumber *eventCode = bytesDict[GDTCCTEventCodeInfo];
46+
if (eventCode != nil) {
47+
self.customBytes =
48+
[NSJSONSerialization dataWithJSONObject:@{GDTCCTEventCodeInfo : eventCode}
49+
options:0
50+
error:&error];
51+
}
52+
} @catch (NSException *exception) {
53+
GDTCORLogDebug(@"Error when setting the event for needs_network_connection_info: %@",
54+
exception);
55+
}
56+
} else {
57+
@try {
58+
NSError *error;
59+
NSMutableDictionary *bytesDict;
60+
if (self.customBytes) {
61+
bytesDict = [[NSJSONSerialization JSONObjectWithData:self.customBytes
62+
options:0
63+
error:&error] mutableCopy];
64+
if (error) {
65+
GDTCORLogDebug(@"Error when setting an even'ts event_code: %@", error);
66+
return;
67+
}
68+
} else {
69+
bytesDict = [[NSMutableDictionary alloc] init];
70+
}
71+
[bytesDict setObject:@YES forKey:GDTCCTNeedsNetworkConnectionInfo];
72+
self.customBytes = [NSJSONSerialization dataWithJSONObject:bytesDict options:0 error:&error];
73+
} @catch (NSException *exception) {
74+
GDTCORLogDebug(@"Error when setting the event for needs_network_connection_info: %@",
75+
exception);
76+
}
77+
}
78+
}
79+
80+
- (BOOL)needsNetworkConnectionInfoPopulated {
81+
if (self.customBytes) {
82+
@try {
83+
NSError *error;
84+
NSDictionary *bytesDict = [NSJSONSerialization JSONObjectWithData:self.customBytes
85+
options:0
86+
error:&error];
87+
return bytesDict && !error && [bytesDict[GDTCCTNeedsNetworkConnectionInfo] boolValue];
88+
} @catch (NSException *exception) {
89+
GDTCORLogDebug(@"Error when checking the event for needs_network_connection_info: %@",
90+
exception);
91+
}
92+
}
93+
return NO;
94+
}
95+
96+
- (void)setNetworkConnectionInfoData:(NSData *)networkConnectionInfoData {
97+
@try {
98+
NSError *error;
99+
NSString *dataString = [networkConnectionInfoData base64EncodedStringWithOptions:0];
100+
if (dataString != nil) {
101+
NSMutableDictionary *bytesDict;
102+
if (self.customBytes) {
103+
bytesDict = [[NSJSONSerialization JSONObjectWithData:self.customBytes
104+
options:0
105+
error:&error] mutableCopy];
106+
if (error) {
107+
GDTCORLogDebug(@"Error when setting an even'ts event_code: %@", error);
108+
return;
109+
}
110+
} else {
111+
bytesDict = [[NSMutableDictionary alloc] init];
112+
}
113+
[bytesDict setObject:dataString forKey:GDTCCTNetworkConnectionInfo];
114+
self.customBytes = [NSJSONSerialization dataWithJSONObject:bytesDict options:0 error:&error];
115+
if (error) {
116+
self.customBytes = nil;
117+
GDTCORLogDebug(@"Error when setting an event's network_connection_info: %@", error);
118+
}
119+
}
120+
} @catch (NSException *exception) {
121+
GDTCORLogDebug(@"Error when setting an event's network_connection_info: %@", exception);
122+
}
123+
}
124+
125+
- (nullable NSData *)networkConnectionInfoData {
126+
if (self.customBytes) {
127+
@try {
128+
NSError *error;
129+
NSDictionary *bytesDict = [NSJSONSerialization JSONObjectWithData:self.customBytes
130+
options:0
131+
error:&error];
132+
NSString *base64Data = bytesDict[GDTCCTNetworkConnectionInfo];
133+
NSData *networkConnectionInfoData = [[NSData alloc] initWithBase64EncodedString:base64Data
134+
options:0];
135+
if (error) {
136+
GDTCORLogDebug(@"Error when getting an event's network_connection_info: %@", error);
137+
return nil;
138+
} else {
139+
return networkConnectionInfoData;
140+
}
141+
} @catch (NSException *exception) {
142+
GDTCORLogDebug(@"Error when getting an event's network_connection_info: %@", exception);
143+
}
144+
}
145+
return nil;
146+
}
147+
148+
- (NSNumber *)eventCode {
149+
if (self.customBytes) {
150+
@try {
151+
NSError *error;
152+
NSDictionary *bytesDict = [NSJSONSerialization JSONObjectWithData:self.customBytes
153+
options:0
154+
error:&error];
155+
NSString *eventCodeString = bytesDict[GDTCCTEventCodeInfo];
156+
157+
if (!eventCodeString) {
158+
return nil;
159+
}
160+
161+
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
162+
formatter.numberStyle = NSNumberFormatterDecimalStyle;
163+
NSNumber *eventCode = [formatter numberFromString:eventCodeString];
164+
165+
if (error) {
166+
GDTCORLogDebug(@"Error when getting an event's network_connection_info: %@", error);
167+
return nil;
168+
} else {
169+
return eventCode;
170+
}
171+
} @catch (NSException *exception) {
172+
GDTCORLogDebug(@"Error when getting an event's event_code: %@", exception);
173+
}
174+
}
175+
return nil;
176+
}
177+
178+
- (void)setEventCode:(NSNumber *)eventCode {
179+
if (eventCode == nil) {
180+
if (!self.customBytes) {
181+
return;
182+
}
183+
184+
NSError *error;
185+
NSMutableDictionary *bytesDict = [[NSJSONSerialization JSONObjectWithData:self.customBytes
186+
options:0
187+
error:&error] mutableCopy];
188+
if (error) {
189+
GDTCORLogDebug(@"Error when setting an event's event_code: %@", error);
190+
return;
191+
}
192+
193+
[bytesDict removeObjectForKey:GDTCCTEventCodeInfo];
194+
self.customBytes = [NSJSONSerialization dataWithJSONObject:bytesDict options:0 error:&error];
195+
if (error) {
196+
self.customBytes = nil;
197+
GDTCORLogDebug(@"Error when setting an event's event_code: %@", error);
198+
return;
199+
}
200+
return;
201+
}
202+
203+
@try {
204+
NSMutableDictionary *bytesDict;
205+
NSError *error;
206+
if (self.customBytes) {
207+
bytesDict = [[NSJSONSerialization JSONObjectWithData:self.customBytes options:0
208+
error:&error] mutableCopy];
209+
if (error) {
210+
GDTCORLogDebug(@"Error when setting an event's event_code: %@", error);
211+
return;
212+
}
213+
} else {
214+
bytesDict = [[NSMutableDictionary alloc] init];
215+
}
216+
217+
NSString *eventCodeString = [eventCode stringValue];
218+
if (eventCodeString == nil) {
219+
return;
220+
}
221+
222+
[bytesDict setObject:eventCodeString forKey:GDTCCTEventCodeInfo];
223+
224+
self.customBytes = [NSJSONSerialization dataWithJSONObject:bytesDict options:0 error:&error];
225+
if (error) {
226+
self.customBytes = nil;
227+
GDTCORLogDebug(@"Error when setting an event's network_connection_info: %@", error);
228+
return;
229+
}
230+
231+
} @catch (NSException *exception) {
232+
GDTCORLogDebug(@"Error when getting an event's network_connection_info: %@", exception);
233+
}
234+
}
235+
236+
@end

GoogleDataTransportCCTSupport/GDTCCTLibrary/GDTCOREvent+NetworkConnectionInfo.m

Lines changed: 0 additions & 102 deletions
This file was deleted.

GoogleDataTransportCCTSupport/GDTCCTLibrary/Private/GDTCOREvent+NetworkConnectionInfo.h renamed to GoogleDataTransportCCTSupport/GDTCCTLibrary/Public/GDTCOREvent+GDTCCTSupport.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ FOUNDATION_EXPORT NSString *const GDTCCTNetworkConnectionInfo;
3030

3131
/** A category that uses the customBytes property of a GDTCOREvent to store network connection info.
3232
*/
33-
@interface GDTCOREvent (CCTNetworkConnectionInfo)
33+
@interface GDTCOREvent (GDTCCTSupport)
3434

3535
/** If YES, needs the network connection info field set during prioritization.
3636
* @note Uses the GDTCOREvent customBytes property.
@@ -42,6 +42,10 @@ FOUNDATION_EXPORT NSString *const GDTCCTNetworkConnectionInfo;
4242
*/
4343
@property(nullable, nonatomic) NSData *networkConnectionInfoData;
4444

45+
/** Code that identifies the event to be sent to the CCT backend.
46+
*/
47+
@property(nullable, nonatomic) NSNumber *eventCode;
48+
4549
@end
4650

4751
NS_ASSUME_NONNULL_END

0 commit comments

Comments
 (0)