|
| 1 | +/* |
| 2 | + * Copyright 2019 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 "GDTCCTNanopbHelpers.h" |
| 18 | + |
| 19 | +#import <UIKit/UIKit.h> |
| 20 | +#import <nanopb/pb.h> |
| 21 | +#import <nanopb/pb_decode.h> |
| 22 | +#import <nanopb/pb_encode.h> |
| 23 | + |
| 24 | +#import "GDTCCTPrioritizer.h" |
| 25 | + |
| 26 | +#pragma mark - General purpose encoders |
| 27 | + |
| 28 | +pb_bytes_array_t *GDTCCTEncodeString(NSString *string) { |
| 29 | + NSData *stringBytes = [string dataUsingEncoding:NSUTF8StringEncoding]; |
| 30 | + return GDTCCTEncodeData(stringBytes); |
| 31 | +} |
| 32 | + |
| 33 | +pb_bytes_array_t *GDTCCTEncodeData(NSData *data) { |
| 34 | + pb_bytes_array_t *pbBytes = malloc(PB_BYTES_ARRAY_T_ALLOCSIZE(data.length)); |
| 35 | + memcpy(pbBytes->bytes, [data bytes], data.length); |
| 36 | + pbBytes->size = (pb_size_t)data.length; |
| 37 | + return pbBytes; |
| 38 | +} |
| 39 | + |
| 40 | +#pragma mark - CCT object constructors |
| 41 | + |
| 42 | +NSData *_Nullable GDTCCTEncodeBatchedLogRequest(gdt_cct_BatchedLogRequest *batchedLogRequest) { |
| 43 | + pb_ostream_t sizestream = PB_OSTREAM_SIZING; |
| 44 | + // Encode 1 time to determine the size. |
| 45 | + if (!pb_encode(&sizestream, gdt_cct_BatchedLogRequest_fields, batchedLogRequest)) { |
| 46 | + NSCAssert(NO, @"Error in nanopb encoding for size: %s", PB_GET_ERROR(&sizestream)); |
| 47 | + } |
| 48 | + |
| 49 | + // Encode a 2nd time to actually get the bytes from it. |
| 50 | + size_t bufferSize = sizestream.bytes_written; |
| 51 | + CFMutableDataRef dataRef = CFDataCreateMutable(CFAllocatorGetDefault(), bufferSize); |
| 52 | + pb_ostream_t ostream = pb_ostream_from_buffer((void *)CFDataGetBytePtr(dataRef), bufferSize); |
| 53 | + if (!pb_encode(&ostream, gdt_cct_BatchedLogRequest_fields, batchedLogRequest)) { |
| 54 | + NSCAssert(NO, @"Error in nanopb encoding for bytes: %s", PB_GET_ERROR(&ostream)); |
| 55 | + } |
| 56 | + CFDataSetLength(dataRef, ostream.bytes_written); |
| 57 | + |
| 58 | + return CFBridgingRelease(dataRef); |
| 59 | +} |
| 60 | + |
| 61 | +gdt_cct_BatchedLogRequest GDTCCTConstructBatchedLogRequest( |
| 62 | + NSDictionary<NSString *, NSSet<GDTStoredEvent *> *> *logMappingIDToLogSet) { |
| 63 | + gdt_cct_BatchedLogRequest batchedLogRequest = gdt_cct_BatchedLogRequest_init_default; |
| 64 | + NSUInteger numberOfLogRequests = logMappingIDToLogSet.count; |
| 65 | + gdt_cct_LogRequest *logRequests = malloc(sizeof(gdt_cct_LogRequest) * numberOfLogRequests); |
| 66 | + |
| 67 | + __block int i = 0; |
| 68 | + [logMappingIDToLogSet enumerateKeysAndObjectsUsingBlock:^( |
| 69 | + NSString *_Nonnull logMappingID, |
| 70 | + NSSet<GDTStoredEvent *> *_Nonnull logSet, BOOL *_Nonnull stop) { |
| 71 | + int32_t logSource = [logMappingID intValue]; |
| 72 | + gdt_cct_LogRequest logRequest = GDTCCTConstructLogRequest(logSource, logSet); |
| 73 | + logRequests[i] = logRequest; |
| 74 | + i++; |
| 75 | + }]; |
| 76 | + |
| 77 | + batchedLogRequest.log_request = logRequests; |
| 78 | + batchedLogRequest.log_request_count = (pb_size_t)numberOfLogRequests; |
| 79 | + return batchedLogRequest; |
| 80 | +} |
| 81 | + |
| 82 | +gdt_cct_LogRequest GDTCCTConstructLogRequest(int32_t logSource, |
| 83 | + NSSet<GDTStoredEvent *> *_Nonnull logSet) { |
| 84 | + NSCAssert(logSet.count, @"An empty event set can't be serialized to proto."); |
| 85 | + gdt_cct_LogRequest logRequest = gdt_cct_LogRequest_init_default; |
| 86 | + logRequest.log_source = logSource; |
| 87 | + logRequest.has_log_source = 1; |
| 88 | + logRequest.client_info = GDTCCTConstructClientInfo(); |
| 89 | + logRequest.has_client_info = 1; |
| 90 | + logRequest.log_event = malloc(sizeof(gdt_cct_LogEvent) * logSet.count); |
| 91 | + int i = 0; |
| 92 | + for (GDTStoredEvent *log in logSet) { |
| 93 | + gdt_cct_LogEvent logEvent = GDTCCTConstructLogEvent(log); |
| 94 | + logRequest.log_event[i] = logEvent; |
| 95 | + i++; |
| 96 | + } |
| 97 | + logRequest.log_event_count = (pb_size_t)logSet.count; |
| 98 | + |
| 99 | + return logRequest; |
| 100 | +} |
| 101 | + |
| 102 | +gdt_cct_LogEvent GDTCCTConstructLogEvent(GDTStoredEvent *event) { |
| 103 | + gdt_cct_LogEvent logEvent = gdt_cct_LogEvent_init_default; |
| 104 | + logEvent.event_time_ms = event.clockSnapshot.timeMillis; |
| 105 | + logEvent.has_event_time_ms = 1; |
| 106 | + logEvent.event_uptime_ms = event.clockSnapshot.uptime; |
| 107 | + logEvent.has_event_uptime_ms = 1; |
| 108 | + logEvent.timezone_offset_seconds = event.clockSnapshot.timezoneOffsetSeconds; |
| 109 | + logEvent.has_timezone_offset_seconds = 1; |
| 110 | + // TODO: Read network_connection_info from the custom params dict. |
| 111 | + |
| 112 | + NSError *error; |
| 113 | + NSData *extensionBytes = [NSData dataWithContentsOfURL:event.eventFileURL options:0 error:&error]; |
| 114 | + NSCAssert(error == nil, @"There was an error reading extension bytes from disk: %@", error); |
| 115 | + logEvent.source_extension = GDTCCTEncodeData(extensionBytes); // read bytes from the file. |
| 116 | + return logEvent; |
| 117 | +} |
| 118 | + |
| 119 | +gdt_cct_ClientInfo GDTCCTConstructClientInfo() { |
| 120 | + gdt_cct_ClientInfo clientInfo = gdt_cct_ClientInfo_init_default; |
| 121 | + clientInfo.client_type = gdt_cct_ClientInfo_ClientType_IOS_FIREBASE; |
| 122 | + clientInfo.has_client_type = 1; |
| 123 | + clientInfo.ios_client_info = GDTCCTConstructiOSClientInfo(); |
| 124 | + clientInfo.has_ios_client_info = 1; |
| 125 | + return clientInfo; |
| 126 | +} |
| 127 | + |
| 128 | +gdt_cct_IosClientInfo GDTCCTConstructiOSClientInfo() { |
| 129 | + gdt_cct_IosClientInfo iOSClientInfo = gdt_cct_IosClientInfo_init_default; |
| 130 | + UIDevice *device = [UIDevice currentDevice]; |
| 131 | + NSBundle *bundle = [NSBundle mainBundle]; |
| 132 | + NSLocale *locale = [NSLocale currentLocale]; |
| 133 | + iOSClientInfo.os_full_version = GDTCCTEncodeString(device.systemVersion); |
| 134 | + NSArray *versionComponents = [device.systemVersion componentsSeparatedByString:@"."]; |
| 135 | + iOSClientInfo.os_major_version = GDTCCTEncodeString(versionComponents[0]); |
| 136 | + NSString *version = [bundle objectForInfoDictionaryKey:(NSString *)kCFBundleVersionKey]; |
| 137 | + if (version) { |
| 138 | + iOSClientInfo.application_build = GDTCCTEncodeString(version); |
| 139 | + } |
| 140 | + iOSClientInfo.country = GDTCCTEncodeString([locale objectForKey:NSLocaleCountryCode]); |
| 141 | + iOSClientInfo.model = GDTCCTEncodeString(device.model); |
| 142 | + NSString *languageCode = bundle.preferredLocalizations.firstObject; |
| 143 | + iOSClientInfo.language_code = |
| 144 | + languageCode ? GDTCCTEncodeString(languageCode) : GDTCCTEncodeString(@"en"); |
| 145 | + iOSClientInfo.application_bundle_id = GDTCCTEncodeString(bundle.bundleIdentifier); |
| 146 | + return iOSClientInfo; |
| 147 | +} |
| 148 | + |
| 149 | +#pragma mark - CCT Object decoders |
| 150 | + |
| 151 | +gdt_cct_LogResponse GDTCCTDecodeLogResponse(NSData *data, NSError **error) { |
| 152 | + gdt_cct_LogResponse response = gdt_cct_LogResponse_init_default; |
| 153 | + pb_istream_t istream = pb_istream_from_buffer([data bytes], [data length]); |
| 154 | + if (!pb_decode(&istream, gdt_cct_LogResponse_fields, &response)) { |
| 155 | + NSCAssert(NO, @"Error in nanopb decoding for bytes: %s", PB_GET_ERROR(&istream)); |
| 156 | + *error = [NSError errorWithDomain:NSURLErrorDomain code:-1 userInfo:nil]; |
| 157 | + response = (gdt_cct_LogResponse)gdt_cct_LogResponse_init_default; |
| 158 | + } |
| 159 | + return response; |
| 160 | +} |
0 commit comments