1 |
efrain |
1 |
// Copyright 2020 Google LLC
|
|
|
2 |
//
|
|
|
3 |
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
4 |
// you may not use this file except in compliance with the License.
|
|
|
5 |
// You may obtain a copy of the License at
|
|
|
6 |
//
|
|
|
7 |
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
8 |
//
|
|
|
9 |
// Unless required by applicable law or agreed to in writing, software
|
|
|
10 |
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
12 |
// See the License for the specific language governing permissions and
|
|
|
13 |
// limitations under the License.
|
|
|
14 |
|
|
|
15 |
#import <nanopb/pb.h>
|
|
|
16 |
#import <nanopb/pb_decode.h>
|
|
|
17 |
#import <nanopb/pb_encode.h>
|
|
|
18 |
|
|
|
19 |
#import "FirebasePerformance/Sources/FPRConsoleLogger.h"
|
|
|
20 |
#import "FirebasePerformance/Sources/Loggers/FPRGDTEvent.h"
|
|
|
21 |
|
|
|
22 |
#import "FirebasePerformance/Sources/Protogen/nanopb/perf_metric.nanopb.h"
|
|
|
23 |
|
|
|
24 |
@interface FPRGDTEvent ()
|
|
|
25 |
|
|
|
26 |
/** Perf metric that is going to be converted. */
|
|
|
27 |
@property(nonatomic) firebase_perf_v1_PerfMetric metric;
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* Creates an instance of FPRGDTEvent.
|
|
|
31 |
*
|
|
|
32 |
* @param perfMetric Performance Event proto object that needs to be converted to FPRGDTEvent.
|
|
|
33 |
* @return Instance of FPRGDTEvent.
|
|
|
34 |
*/
|
|
|
35 |
- (instancetype)initForPerfMetric:(firebase_perf_v1_PerfMetric)perfMetric;
|
|
|
36 |
|
|
|
37 |
@end
|
|
|
38 |
|
|
|
39 |
@implementation FPRGDTEvent
|
|
|
40 |
|
|
|
41 |
+ (instancetype)gdtEventForPerfMetric:(firebase_perf_v1_PerfMetric)perfMetric {
|
|
|
42 |
FPRGDTEvent *event = [[FPRGDTEvent alloc] initForPerfMetric:perfMetric];
|
|
|
43 |
return event;
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
- (instancetype)initForPerfMetric:(firebase_perf_v1_PerfMetric)perfMetric {
|
|
|
47 |
if (self = [super init]) {
|
|
|
48 |
_metric = perfMetric;
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
return self;
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
#pragma mark - GDTCOREventDataObject protocol methods
|
|
|
55 |
|
|
|
56 |
- (NSData *)transportBytes {
|
|
|
57 |
pb_ostream_t sizestream = PB_OSTREAM_SIZING;
|
|
|
58 |
|
|
|
59 |
// Encode 1 time to determine the size.
|
|
|
60 |
if (!pb_encode(&sizestream, firebase_perf_v1_PerfMetric_fields, &_metric)) {
|
|
|
61 |
FPRLogError(kFPRTransportBytesError, @"Error in nanopb encoding for size: %s",
|
|
|
62 |
PB_GET_ERROR(&sizestream));
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
// Encode a 2nd time to actually get the bytes from it.
|
|
|
66 |
size_t bufferSize = sizestream.bytes_written;
|
|
|
67 |
CFMutableDataRef dataRef = CFDataCreateMutable(CFAllocatorGetDefault(), bufferSize);
|
|
|
68 |
CFDataSetLength(dataRef, bufferSize);
|
|
|
69 |
pb_ostream_t ostream = pb_ostream_from_buffer((void *)CFDataGetBytePtr(dataRef), bufferSize);
|
|
|
70 |
if (!pb_encode(&ostream, firebase_perf_v1_PerfMetric_fields, &_metric)) {
|
|
|
71 |
FPRLogError(kFPRTransportBytesError, @"Error in nanopb encoding for bytes: %s",
|
|
|
72 |
PB_GET_ERROR(&ostream));
|
|
|
73 |
}
|
|
|
74 |
CFDataSetLength(dataRef, ostream.bytes_written);
|
|
|
75 |
|
|
|
76 |
return CFBridgingRelease(dataRef);
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
- (void)dealloc {
|
|
|
80 |
pb_release(firebase_perf_v1_PerfMetric_fields, &_metric);
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
@end
|