FPRNanoPbUtils.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. /** Creates a NSData object by copying the given bytes array and returns the reference.
  49. *
  50. * @param pbData The pbData to dedoded as NSData
  51. * @return A reference to NSData
  52. */
  53. extern NSData* _Nullable FPRDecodeData(pb_bytes_array_t* _Nonnull pbData);
  54. /** Creates a NSString object by copying the given bytes array and returns the reference.
  55. *
  56. * @param pbData The pbData to dedoded as NSString
  57. * @return A reference to the NSString
  58. */
  59. extern NSString* _Nullable FPRDecodeString(pb_bytes_array_t* _Nonnull pbData);
  60. /** Creates a NSDictionary by copying the given bytes from the StringToStringMap object and returns
  61. * the reference.
  62. *
  63. * @param map The reference to a StringToStringMap object to be decoded.
  64. * @param count The number of entries in the dictionary.
  65. * @return A reference to the dictionary
  66. */
  67. extern NSDictionary<NSString*, NSString*>* _Nullable FPRDecodeStringToStringMap(
  68. StringToStringMap* _Nullable map, NSInteger count);
  69. /** Callocs a nanopb StringToStringMap and copies the given NSDictionary bytes into the
  70. * StringToStringMap.
  71. *
  72. * @param dict The dict to copy into the new StringToStringMap.
  73. * @return A reference to StringToStringMap
  74. */
  75. extern StringToStringMap* _Nullable FPREncodeStringToStringMap(NSDictionary* _Nullable dict);
  76. /** Creates a NSDictionary by copying the given bytes from the StringToNumberMap object and returns
  77. * the reference.
  78. *
  79. * @param map The reference to a StringToNumberMap object to be decoded.
  80. * @param count The number of entries in the dictionary.
  81. * @return A reference to the dictionary
  82. */
  83. extern NSDictionary<NSString*, NSNumber*>* _Nullable FPRDecodeStringToNumberMap(
  84. StringToNumberMap* _Nullable map, NSInteger count);
  85. /** Callocs a nanopb StringToNumberMap and copies the given NSDictionary bytes into the
  86. * StringToStringMap.
  87. *
  88. * @param dict The dict to copy into the new StringToNumberMap.
  89. * @return A reference to StringToNumberMap
  90. */
  91. extern StringToNumberMap* _Nullable FPREncodeStringToNumberMap(NSDictionary* _Nullable dict);
  92. /** Creates a new firebase_perf_v1_PerfMetric struct populated with system metadata.
  93. * @param appID The Google app id to put into the message
  94. * @return A firebase_perf_v1_PerfMetric struct.
  95. */
  96. extern firebase_perf_v1_PerfMetric FPRGetPerfMetricMessage(NSString* _Nonnull appID);
  97. /** Creates a new firebase_perf_v1_ApplicationInfo struct populated with system metadata.
  98. * @return A firebase_perf_v1_ApplicationInfo struct.
  99. */
  100. extern firebase_perf_v1_ApplicationInfo FPRGetApplicationInfoMessage(void);
  101. /** Converts the FIRTrace object to a firebase_perf_v1_TraceMetric struct.
  102. * @return A firebase_perf_v1_TraceMetric struct.
  103. */
  104. extern firebase_perf_v1_TraceMetric FPRGetTraceMetric(FIRTrace* _Nonnull trace);
  105. /** Converts the FPRNetworkTrace object to a firebase_perf_v1_NetworkRequestMetric struct.
  106. * @return A firebase_perf_v1_NetworkRequestMetric struct.
  107. */
  108. extern firebase_perf_v1_NetworkRequestMetric FPRGetNetworkRequestMetric(
  109. FPRNetworkTrace* _Nonnull trace);
  110. /** Converts the gaugeData array object to a firebase_perf_v1_GaugeMetric struct.
  111. * @return A firebase_perf_v1_GaugeMetric struct.
  112. */
  113. extern firebase_perf_v1_GaugeMetric FPRGetGaugeMetric(NSArray* _Nonnull gaugeData,
  114. NSString* _Nonnull sessionId);
  115. /** Converts the FPRTraceState to a firebase_perf_v1_ApplicationProcessState struct.
  116. * @return A firebase_perf_v1_ApplicationProcessState struct.
  117. */
  118. extern firebase_perf_v1_ApplicationProcessState FPRApplicationProcessState(FPRTraceState state);
  119. /** Populate a firebase_perf_v1_PerfMetric object with the given firebase_perf_v1_ApplicationInfo.
  120. *
  121. * @param perfMetric The reference to a firebase_perf_v1_PerfMetric object to be populated.
  122. * @param appInfo The firebase_perf_v1_ApplicationInfo object that will be added to
  123. * firebase_perf_v1_PerfMetric.
  124. */
  125. extern void FPRSetApplicationInfo(firebase_perf_v1_PerfMetric* _Nonnull perfMetric,
  126. firebase_perf_v1_ApplicationInfo appInfo);
  127. /** Populate a firebase_perf_v1_PerfMetric object with the given firebase_perf_v1_TraceMetric.
  128. *
  129. * @param perfMetric The reference to firebase_perf_v1_PerfMetric to be populated.
  130. * @param traceMetric The firebase_perf_v1_TraceMetric object that will be added to
  131. * firebase_perf_v1_PerfMetric.
  132. */
  133. extern void FPRSetTraceMetric(firebase_perf_v1_PerfMetric* _Nonnull perfMetric,
  134. firebase_perf_v1_TraceMetric traceMetric);
  135. /** Populate a firebase_perf_v1_PerfMetric object with the given
  136. * firebase_perf_v1_NetworkRequestMetric.
  137. *
  138. * @param perfMetric The reference to a firebase_perf_v1_PerfMetric object to be populated.
  139. * @param networkMetric The firebase_perf_v1_NetworkRequestMetric object that will be added to
  140. * firebase_perf_v1_PerfMetric.
  141. */
  142. extern void FPRSetNetworkRequestMetric(firebase_perf_v1_PerfMetric* _Nonnull perfMetric,
  143. firebase_perf_v1_NetworkRequestMetric networkMetric);
  144. /** Populate a firebase_perf_v1_PerfMetric object with the given firebase_perf_v1_GaugeMetric.
  145. *
  146. * @param perfMetric The reference to a firebase_perf_v1_PerfMetric object to be populated.
  147. * @param gaugeMetric The firebase_perf_v1_GaugeMetric object that will be added to
  148. * firebase_perf_v1_PerfMetric.
  149. */
  150. extern void FPRSetGaugeMetric(firebase_perf_v1_PerfMetric* _Nonnull perfMetric,
  151. firebase_perf_v1_GaugeMetric gaugeMetric);
  152. /** Populate a firebase_perf_v1_PerfMetric object with the given
  153. * firebase_perf_v1_ApplicationProcessState.
  154. *
  155. * @param perfMetric The reference to a firebase_perf_v1_PerfMetric object to be populated.
  156. * @param state The firebase_perf_v1_ApplicationProcessState object that will be added to
  157. * firebase_perf_v1_PerfMetric.
  158. */
  159. extern void FPRSetApplicationProcessState(firebase_perf_v1_PerfMetric* _Nonnull perfMetric,
  160. firebase_perf_v1_ApplicationProcessState state);
  161. #ifdef TARGET_HAS_MOBILE_CONNECTIVITY
  162. /** Obtain a CTTelephonyNetworkInfo object to determine device network attributes.
  163. * @return CTTelephonyNetworkInfo object.
  164. */
  165. extern CTTelephonyNetworkInfo* _Nullable FPRNetworkInfo(void);
  166. #endif