FPRNanoPbUtils.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Copyright 2021 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 <TargetConditionals.h>
  15. #if __has_include("CoreTelephony/CTTelephonyNetworkInfo.h") && !TARGET_OS_MACCATALYST
  16. #define TARGET_HAS_MOBILE_CONNECTIVITY
  17. #import <CoreTelephony/CTTelephonyNetworkInfo.h>
  18. #endif
  19. #import "FirebasePerformance/Sources/AppActivity/FPRTraceBackgroundActivityTracker.h"
  20. #import "FirebasePerformance/Sources/Instrumentation/FPRNetworkTrace.h"
  21. #import "FirebasePerformance/Sources/Public/FirebasePerformance/FIRTrace.h"
  22. #import "FirebasePerformance/Sources/Protogen/nanopb/perf_metric.nanopb.h"
  23. /**nanopb struct of encoded NSDictionary<NSString *, NSString *>.*/
  24. typedef struct {
  25. pb_bytes_array_t* _Nonnull key;
  26. pb_bytes_array_t* _Nonnull value;
  27. } StringToStringMap;
  28. /**nanopb struct of encoded NSDictionary<NSString *, NSNumber *>.*/
  29. typedef struct {
  30. pb_bytes_array_t* _Nonnull key;
  31. bool has_value;
  32. int64_t value;
  33. } StringToNumberMap;
  34. /** Callocs a pb_bytes_array and copies the given NSData bytes into the bytes array.
  35. *
  36. * @note Memory needs to be free manually, through pb_free or pb_release.
  37. * @param data The data to copy into the new bytes array.
  38. * @return pb_byte array
  39. */
  40. extern pb_bytes_array_t* _Nullable FPREncodeData(NSData* _Nonnull data);
  41. /** Callocs a pb_bytes_array and copies the given NSString's bytes into the bytes array.
  42. *
  43. * @note Memory needs to be free manually, through pb_free or pb_release.
  44. * @param string The string to encode as pb_bytes.
  45. * @return pb_byte array
  46. */
  47. extern pb_bytes_array_t* _Nullable FPREncodeString(NSString* _Nonnull string);
  48. /** Callocs a nanopb StringToStringMap and copies the given NSDictionary bytes into the
  49. * StringToStringMap.
  50. *
  51. * @param dict The dict to copy into the new StringToStringMap.
  52. * @return A reference to StringToStringMap
  53. */
  54. extern StringToStringMap* _Nullable FPREncodeStringToStringMap(NSDictionary* _Nullable dict);
  55. /** Callocs a nanopb StringToNumberMap and copies the given NSDictionary bytes into the
  56. * StringToStringMap.
  57. *
  58. * @param dict The dict to copy into the new StringToNumberMap.
  59. * @return A reference to StringToNumberMap
  60. */
  61. extern StringToNumberMap* _Nullable FPREncodeStringToNumberMap(NSDictionary* _Nullable dict);
  62. /** Creates a new firebase_perf_v1_PerfMetric struct populated with system metadata.
  63. * @param appID The Google app id to put into the message
  64. * @return A firebase_perf_v1_PerfMetric struct.
  65. */
  66. NS_EXTENSION_UNAVAILABLE("Firebase Performance is not supported for extensions.")
  67. extern firebase_perf_v1_PerfMetric FPRGetPerfMetricMessage(NSString* _Nonnull appID);
  68. /** Creates a new firebase_perf_v1_ApplicationInfo struct populated with system metadata.
  69. * @return A firebase_perf_v1_ApplicationInfo struct.
  70. */
  71. NS_EXTENSION_UNAVAILABLE("Firebase Performance is not supported for extensions.")
  72. extern firebase_perf_v1_ApplicationInfo FPRGetApplicationInfoMessage(void);
  73. /** Converts the FIRTrace object to a firebase_perf_v1_TraceMetric struct.
  74. * @return A firebase_perf_v1_TraceMetric struct.
  75. */
  76. NS_EXTENSION_UNAVAILABLE("Firebase Performance is not supported for extensions.")
  77. extern firebase_perf_v1_TraceMetric FPRGetTraceMetric(FIRTrace* _Nonnull trace);
  78. /** Converts the FPRNetworkTrace object to a firebase_perf_v1_NetworkRequestMetric struct.
  79. * @return A firebase_perf_v1_NetworkRequestMetric struct.
  80. */
  81. NS_EXTENSION_UNAVAILABLE("Firebase Performance is not supported for extensions.")
  82. extern firebase_perf_v1_NetworkRequestMetric FPRGetNetworkRequestMetric(
  83. FPRNetworkTrace* _Nonnull trace);
  84. /** Converts the gaugeData array object to a firebase_perf_v1_GaugeMetric struct.
  85. * @return A firebase_perf_v1_GaugeMetric struct.
  86. */
  87. extern firebase_perf_v1_GaugeMetric FPRGetGaugeMetric(NSArray* _Nonnull gaugeData,
  88. NSString* _Nonnull sessionId);
  89. /** Converts the FPRTraceState to a firebase_perf_v1_ApplicationProcessState struct.
  90. * @return A firebase_perf_v1_ApplicationProcessState struct.
  91. */
  92. extern firebase_perf_v1_ApplicationProcessState FPRApplicationProcessState(FPRTraceState state);
  93. /** Populate a firebase_perf_v1_PerfMetric object with the given firebase_perf_v1_ApplicationInfo.
  94. *
  95. * @param perfMetric The reference to a firebase_perf_v1_PerfMetric object to be populated.
  96. * @param appInfo The firebase_perf_v1_ApplicationInfo object that will be added to
  97. * firebase_perf_v1_PerfMetric.
  98. */
  99. extern void FPRSetApplicationInfo(firebase_perf_v1_PerfMetric* _Nonnull perfMetric,
  100. firebase_perf_v1_ApplicationInfo appInfo);
  101. /** Populate a firebase_perf_v1_PerfMetric object with the given firebase_perf_v1_TraceMetric.
  102. *
  103. * @param perfMetric The reference to firebase_perf_v1_PerfMetric to be populated.
  104. * @param traceMetric The firebase_perf_v1_TraceMetric object that will be added to
  105. * firebase_perf_v1_PerfMetric.
  106. */
  107. extern void FPRSetTraceMetric(firebase_perf_v1_PerfMetric* _Nonnull perfMetric,
  108. firebase_perf_v1_TraceMetric traceMetric);
  109. /** Populate a firebase_perf_v1_PerfMetric object with the given
  110. * firebase_perf_v1_NetworkRequestMetric.
  111. *
  112. * @param perfMetric The reference to a firebase_perf_v1_PerfMetric object to be populated.
  113. * @param networkMetric The firebase_perf_v1_NetworkRequestMetric object that will be added to
  114. * firebase_perf_v1_PerfMetric.
  115. */
  116. extern void FPRSetNetworkRequestMetric(firebase_perf_v1_PerfMetric* _Nonnull perfMetric,
  117. firebase_perf_v1_NetworkRequestMetric networkMetric);
  118. /** Populate a firebase_perf_v1_PerfMetric object with the given firebase_perf_v1_GaugeMetric.
  119. *
  120. * @param perfMetric The reference to a firebase_perf_v1_PerfMetric object to be populated.
  121. * @param gaugeMetric The firebase_perf_v1_GaugeMetric object that will be added to
  122. * firebase_perf_v1_PerfMetric.
  123. */
  124. extern void FPRSetGaugeMetric(firebase_perf_v1_PerfMetric* _Nonnull perfMetric,
  125. firebase_perf_v1_GaugeMetric gaugeMetric);
  126. /** Populate a firebase_perf_v1_PerfMetric object with the given
  127. * firebase_perf_v1_ApplicationProcessState.
  128. *
  129. * @param perfMetric The reference to a firebase_perf_v1_PerfMetric object to be populated.
  130. * @param state The firebase_perf_v1_ApplicationProcessState object that will be added to
  131. * firebase_perf_v1_PerfMetric.
  132. */
  133. extern void FPRSetApplicationProcessState(firebase_perf_v1_PerfMetric* _Nonnull perfMetric,
  134. firebase_perf_v1_ApplicationProcessState state);
  135. #ifdef TARGET_HAS_MOBILE_CONNECTIVITY
  136. /** Obtain a CTTelephonyNetworkInfo object to determine device network attributes.
  137. * @return CTTelephonyNetworkInfo object.
  138. */
  139. extern CTTelephonyNetworkInfo* _Nullable FPRNetworkInfo(void);
  140. #endif