FPRNetworkTrace.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 "FirebasePerformance/Sources/AppActivity/FPRTraceBackgroundActivityTracker.h"
  15. #import "FirebasePerformance/Sources/AppActivity/FPRSessionDetails.h"
  16. #import "FirebasePerformance/Sources/FIRPerformance+Internal.h"
  17. /** Possible checkpoint states of network trace */
  18. typedef NS_ENUM(NSInteger, FPRNetworkTraceCheckpointState) {
  19. FPRNetworkTraceCheckpointStateUnknown,
  20. // Network request has been initiated.
  21. FPRNetworkTraceCheckpointStateInitiated,
  22. // Network request is completed (All necessary uploads for the request is complete).
  23. FPRNetworkTraceCheckpointStateRequestCompleted,
  24. // Network request has received its first response. There could be more.
  25. FPRNetworkTraceCheckpointStateResponseReceived,
  26. // Network request has completed (Could be network error/request successful completion).
  27. FPRNetworkTraceCheckpointStateResponseCompleted
  28. };
  29. @protocol FPRNetworkResponseHandler <NSObject>
  30. /**
  31. * Records the size of the file that is uploaded during the request.
  32. *
  33. * @param URL The URL object that is being used for uploading from the network request.
  34. */
  35. - (void)didUploadFileWithURL:(nullable NSURL *)URL;
  36. /**
  37. * Records the amount of data that is fetched during the request. This can be called multiple times
  38. * when the network delegate comes back with some data.
  39. *
  40. * @param data The data object as received from the network request.
  41. */
  42. - (void)didReceiveData:(nullable NSData *)data;
  43. /**
  44. * Records the size of the file that is fetched during the request. This can be called multiple
  45. * times when the network delegate comes back with some data.
  46. *
  47. * @param URL The URL object as received from the network request.
  48. */
  49. - (void)didReceiveFileURL:(nullable NSURL *)URL;
  50. /**
  51. * Records the end state of the network request. This is usually called at the end of the network
  52. * request with a valid response or an error.
  53. *
  54. * @param response Response of the network request.
  55. * @param error Error with the network request.
  56. */
  57. - (void)didCompleteRequestWithResponse:(nullable NSURLResponse *)response
  58. error:(nullable NSError *)error;
  59. @end
  60. /**
  61. * FPRNetworkTrace object contains information about an NSURLRequest. Every object contains
  62. * information about the URL, type of request, and details of the response.
  63. */
  64. @interface FPRNetworkTrace : NSObject <FPRNetworkResponseHandler, FIRPerformanceAttributable>
  65. /** @brief Start time of the trace since epoch. */
  66. @property(nonatomic, assign, readonly) NSTimeInterval startTimeSinceEpoch;
  67. /** @brief The size of the request. The value is in bytes. */
  68. @property(nonatomic) int64_t requestSize;
  69. /** @brief The response size for the request. The value is in bytes. */
  70. @property(nonatomic) int64_t responseSize;
  71. /** @brief The HTTP response code for the request. */
  72. @property(nonatomic) int32_t responseCode;
  73. /** @brief Yes if a valid response code is set, NO otherwise. */
  74. @property(nonatomic) BOOL hasValidResponseCode;
  75. /** @brief The content type of the request as received from the server. */
  76. @property(nonatomic, copy, nullable) NSString *responseContentType;
  77. /** @brief The checkpoint states for the request. The key to the dictionary is the value referred in
  78. * enum FPRNetworkTraceCheckpointState mentioned above. The value is the number of seconds since the
  79. * reference date.
  80. */
  81. @property(nonatomic, readonly, nullable) NSDictionary<NSString *, NSNumber *> *checkpointStates;
  82. /** @brief The network request object. */
  83. @property(nonatomic, readonly, nullable) NSURLRequest *URLRequest;
  84. /** @brief The URL string with all the query params cleaned. The URL string will be of the format:
  85. * scheme:[//[user:password@]host[:port]][/]path.
  86. */
  87. @property(nonatomic, readonly, nullable) NSString *trimmedURLString;
  88. /** @brief Error object received with the network response. */
  89. @property(nonatomic, readonly, nullable) NSError *responseError;
  90. /** Background state of the trace. */
  91. @property(nonatomic, readonly) FPRTraceState backgroundTraceState;
  92. /** @brief List of sessions the trace is associated with. */
  93. @property(nonnull, atomic, readonly) NSArray<FPRSessionDetails *> *sessions;
  94. /** @brief Serial queue to manage usage of session Ids. */
  95. @property(nonatomic, readonly, nonnull) dispatch_queue_t sessionIdSerialQueue;
  96. /**
  97. * Associate a network trace to an object project. This uses ObjC runtime to associate the network
  98. * trace with the object provided.
  99. *
  100. * @param networkTrace Network trace object to be associated with the provided object.
  101. * @param object The provided object to whom the network trace object will be associated with.
  102. */
  103. + (void)addNetworkTrace:(nonnull FPRNetworkTrace *)networkTrace toObject:(nonnull id)object;
  104. /**
  105. * Gets the network trace associated with the provided object. If the network trace is not
  106. * associated with the object, return nil. This uses ObjC runtime to fetch the object.
  107. *
  108. * @param object The provided object from which the network object would be fetched.
  109. * @return The network trace object associated with the provided object.
  110. */
  111. + (nullable FPRNetworkTrace *)networkTraceFromObject:(nonnull id)object;
  112. /**
  113. * Remove the network trace associated with the provided object. If the network trace is not
  114. * associated with the object, does nothing. This uses ObjC runtime to remove the object.
  115. *
  116. * @param object The provided object from which the network object would be removed.
  117. */
  118. + (void)removeNetworkTraceFromObject:(nonnull id)object;
  119. /**
  120. * Creates an instance of the FPRNetworkTrace with the provided URL and the HTTP method.
  121. *
  122. * @param URLRequest NSURLRequest object.
  123. * @return An instance of FPRNetworkTrace.
  124. */
  125. - (nullable instancetype)initWithURLRequest:(nonnull NSURLRequest *)URLRequest
  126. NS_DESIGNATED_INITIALIZER;
  127. - (nullable instancetype)init NS_UNAVAILABLE;
  128. /**
  129. * Records the begining of the network request. This is usually called just before initiating the
  130. * request.
  131. */
  132. - (void)start;
  133. /**
  134. * Checkpoints a particular state of the network request. Checkpoint states are listed in the enum
  135. * FPRNetworkTraceCheckpointState mentioned above.
  136. *
  137. * @param state A state as mentioned in enum FPRNetworkTraceCheckpointState.
  138. */
  139. - (void)checkpointState:(FPRNetworkTraceCheckpointState)state;
  140. /**
  141. * Provides the time difference between the provided checkpoint states in seconds. If the starting
  142. * checkpoint state is greater than the ending checkpoint state, the return value will be negative.
  143. * If either of the states does not exist, returns 0.
  144. *
  145. * @param startState The starting checkpoint state.
  146. * @param endState The ending checkpoint state.
  147. * @return Difference between the ending checkpoint state and starting checkpoint state in seconds.
  148. */
  149. - (NSTimeInterval)timeIntervalBetweenCheckpointState:(FPRNetworkTraceCheckpointState)startState
  150. andState:(FPRNetworkTraceCheckpointState)endState;
  151. @end