|
| 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 |
0 commit comments