FPRGDTEvent.m 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. #import <nanopb/pb.h>
  15. #import <nanopb/pb_decode.h>
  16. #import <nanopb/pb_encode.h>
  17. #import "FirebasePerformance/Sources/FPRConsoleLogger.h"
  18. #import "FirebasePerformance/Sources/Loggers/FPRGDTEvent.h"
  19. #import "FirebasePerformance/Sources/Protogen/nanopb/perf_metric.nanopb.h"
  20. @interface FPRGDTEvent ()
  21. /** Perf metric that is going to be converted. */
  22. @property(nonatomic) firebase_perf_v1_PerfMetric metric;
  23. /**
  24. * Creates an instance of FPRGDTEvent.
  25. *
  26. * @param perfMetric Performance Event proto object that needs to be converted to FPRGDTEvent.
  27. * @return Instance of FPRGDTEvent.
  28. */
  29. - (instancetype)initForPerfMetric:(firebase_perf_v1_PerfMetric)perfMetric;
  30. @end
  31. @implementation FPRGDTEvent
  32. + (instancetype)gdtEventForPerfMetric:(firebase_perf_v1_PerfMetric)perfMetric {
  33. FPRGDTEvent *event = [[FPRGDTEvent alloc] initForPerfMetric:perfMetric];
  34. return event;
  35. }
  36. - (instancetype)initForPerfMetric:(firebase_perf_v1_PerfMetric)perfMetric {
  37. if (self = [super init]) {
  38. _metric = perfMetric;
  39. }
  40. return self;
  41. }
  42. #pragma mark - GDTCOREventDataObject protocol methods
  43. - (NSData *)transportBytes {
  44. pb_ostream_t sizestream = PB_OSTREAM_SIZING;
  45. // Encode 1 time to determine the size.
  46. if (!pb_encode(&sizestream, firebase_perf_v1_PerfMetric_fields, &_metric)) {
  47. FPRLogError(kFPRTransportBytesError, @"Error in nanopb encoding for size: %s",
  48. PB_GET_ERROR(&sizestream));
  49. }
  50. // Encode a 2nd time to actually get the bytes from it.
  51. size_t bufferSize = sizestream.bytes_written;
  52. CFMutableDataRef dataRef = CFDataCreateMutable(CFAllocatorGetDefault(), bufferSize);
  53. CFDataSetLength(dataRef, bufferSize);
  54. pb_ostream_t ostream = pb_ostream_from_buffer((void *)CFDataGetBytePtr(dataRef), bufferSize);
  55. if (!pb_encode(&ostream, firebase_perf_v1_PerfMetric_fields, &_metric)) {
  56. FPRLogError(kFPRTransportBytesError, @"Error in nanopb encoding for bytes: %s",
  57. PB_GET_ERROR(&ostream));
  58. }
  59. CFDataSetLength(dataRef, ostream.bytes_written);
  60. return CFBridgingRelease(dataRef);
  61. }
  62. - (void)dealloc {
  63. pb_release(firebase_perf_v1_PerfMetric_fields, &_metric);
  64. }
  65. @end