FPRNSURLSessionDelegateInstrument.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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/Instrumentation/Network/Delegates/FPRNSURLSessionDelegateInstrument.h"
  15. #import "FirebasePerformance/Sources/FPRConsoleLogger.h"
  16. #import "FirebasePerformance/Sources/Instrumentation/FPRClassInstrumentor.h"
  17. #import "FirebasePerformance/Sources/Instrumentation/FPRInstrument_Private.h"
  18. #import "FirebasePerformance/Sources/Instrumentation/FPRNetworkTrace.h"
  19. #import "FirebasePerformance/Sources/Instrumentation/FPRSelectorInstrumentor.h"
  20. #import "FirebasePerformance/Sources/Instrumentation/Network/Delegates/FPRNSURLSessionDelegate.h"
  21. #import "FirebasePerformance/Sources/Instrumentation/Network/FPRNetworkInstrumentHelpers.h"
  22. /** Returns the dispatch queue for all instrumentation to occur on. */
  23. static dispatch_queue_t GetInstrumentationQueue(void) {
  24. static dispatch_queue_t queue;
  25. static dispatch_once_t token;
  26. dispatch_once(&token, ^{
  27. queue = dispatch_queue_create("com.google.FPRNSURLSessionDelegateInstrument",
  28. DISPATCH_QUEUE_SERIAL);
  29. });
  30. return queue;
  31. }
  32. #pragma mark - NSURLSessionTaskDelegate methods
  33. /** Instruments URLSession:task:didCompleteWithError:.
  34. *
  35. * @param instrumentor The FPRClassInstrumentor to add the selector instrumentor to.
  36. */
  37. FOUNDATION_STATIC_INLINE
  38. NS_EXTENSION_UNAVAILABLE("Firebase Performance is not supported for extensions.")
  39. void InstrumentURLSessionTaskDidCompleteWithError(FPRClassInstrumentor *instrumentor) {
  40. SEL selector = @selector(URLSession:task:didCompleteWithError:);
  41. FPRSelectorInstrumentor *selectorInstrumentor =
  42. [instrumentor instrumentorForInstanceSelector:selector];
  43. if (selectorInstrumentor) {
  44. IMP currentIMP = selectorInstrumentor.currentIMP;
  45. [selectorInstrumentor setReplacingBlock:^(id object, NSURLSession *session,
  46. NSURLSessionTask *task, NSError *error) {
  47. @try {
  48. FPRNetworkTrace *trace = [FPRNetworkTrace networkTraceFromObject:task];
  49. [trace didCompleteRequestWithResponse:task.response error:error];
  50. [FPRNetworkTrace removeNetworkTraceFromObject:task];
  51. } @catch (NSException *exception) {
  52. FPRLogWarning(kFPRNetworkTraceNotTrackable, @"Unable to track network request.");
  53. } @finally {
  54. typedef void (*OriginalImp)(id, SEL, NSURLSession *, NSURLSessionTask *, NSError *);
  55. ((OriginalImp)currentIMP)(object, selector, session, task, error);
  56. }
  57. }];
  58. }
  59. }
  60. /** Instruments URLSession:task:didSendBodyData:totalBytesSent:totalBytesExpectedToSend:.
  61. *
  62. * @param instrumentor The FPRClassInstrumentor to add the selector instrumentor to.
  63. */
  64. FOUNDATION_STATIC_INLINE
  65. NS_EXTENSION_UNAVAILABLE("Firebase Performance is not supported for extensions.")
  66. void InstrumentURLSessionTaskDidSendBodyDataTotalBytesSentTotalBytesExpectedToSend(
  67. FPRClassInstrumentor *instrumentor) {
  68. SEL selector = @selector(URLSession:
  69. task:didSendBodyData:totalBytesSent:totalBytesExpectedToSend:);
  70. FPRSelectorInstrumentor *selectorInstrumentor =
  71. [instrumentor instrumentorForInstanceSelector:selector];
  72. if (selectorInstrumentor) {
  73. IMP currentIMP = selectorInstrumentor.currentIMP;
  74. [selectorInstrumentor
  75. setReplacingBlock:^(id object, NSURLSession *session, NSURLSessionTask *task,
  76. int64_t bytesSent, int64_t totalBytesSent,
  77. int64_t totalBytesExpectedToSend) {
  78. @try {
  79. FPRNetworkTrace *trace = [FPRNetworkTrace networkTraceFromObject:task];
  80. trace.requestSize = totalBytesSent;
  81. if (totalBytesSent >= totalBytesExpectedToSend) {
  82. [trace checkpointState:FPRNetworkTraceCheckpointStateRequestCompleted];
  83. }
  84. } @catch (NSException *exception) {
  85. FPRLogWarning(kFPRNetworkTraceNotTrackable, @"Unable to track network request.");
  86. } @finally {
  87. typedef void (*OriginalImp)(id, SEL, NSURLSession *, NSURLSessionTask *, int64_t,
  88. int64_t, int64_t);
  89. ((OriginalImp)currentIMP)(object, selector, session, task, bytesSent, totalBytesSent,
  90. totalBytesExpectedToSend);
  91. }
  92. }];
  93. }
  94. }
  95. #pragma mark - NSURLSessionDataDelegate methods
  96. /** Instruments URLSession:dataTask:didReceiveData:.
  97. *
  98. * @param instrumentor The FPRClassInstrumentor to add the selector instrumentor to.
  99. */
  100. FOUNDATION_STATIC_INLINE
  101. NS_EXTENSION_UNAVAILABLE("Firebase Performance is not supported for extensions.")
  102. void InstrumentURLSessionDataTaskDidReceiveData(FPRClassInstrumentor *instrumentor) {
  103. SEL selector = @selector(URLSession:dataTask:didReceiveData:);
  104. FPRSelectorInstrumentor *selectorInstrumentor =
  105. [instrumentor instrumentorForInstanceSelector:selector];
  106. if (selectorInstrumentor) {
  107. IMP currentIMP = selectorInstrumentor.currentIMP;
  108. [selectorInstrumentor setReplacingBlock:^(id object, NSURLSession *session,
  109. NSURLSessionDataTask *dataTask, NSData *data) {
  110. FPRNetworkTrace *trace = [FPRNetworkTrace networkTraceFromObject:dataTask];
  111. [trace didReceiveData:data];
  112. [trace checkpointState:FPRNetworkTraceCheckpointStateResponseReceived];
  113. typedef void (*OriginalImp)(id, SEL, NSURLSession *, NSURLSessionDataTask *, NSData *);
  114. ((OriginalImp)currentIMP)(object, selector, session, dataTask, data);
  115. }];
  116. }
  117. }
  118. #pragma mark - NSURLSessionDownloadDelegate methods.
  119. /** Instruments URLSession:downloadTask:didFinishDownloadingToURL:.
  120. *
  121. * @param instrumentor The FPRClassInstrumentor to add the selector instrumentor to.
  122. */
  123. FOUNDATION_STATIC_INLINE
  124. NS_EXTENSION_UNAVAILABLE("Firebase Performance is not supported for extensions.")
  125. void InstrumentURLSessionDownloadTaskDidFinishDownloadToURL(FPRClassInstrumentor *instrumentor) {
  126. SEL selector = @selector(URLSession:downloadTask:didFinishDownloadingToURL:);
  127. FPRSelectorInstrumentor *selectorInstrumentor =
  128. [instrumentor instrumentorForInstanceSelector:selector];
  129. if (selectorInstrumentor) {
  130. IMP currentIMP = selectorInstrumentor.currentIMP;
  131. [selectorInstrumentor
  132. setReplacingBlock:^(id object, NSURLSession *session,
  133. NSURLSessionDownloadTask *downloadTask, NSURL *location) {
  134. FPRNetworkTrace *trace = [FPRNetworkTrace networkTraceFromObject:downloadTask];
  135. [trace didReceiveFileURL:location];
  136. [trace didCompleteRequestWithResponse:downloadTask.response error:downloadTask.error];
  137. [FPRNetworkTrace removeNetworkTraceFromObject:downloadTask];
  138. typedef void (*OriginalImp)(id, SEL, NSURLSession *, NSURLSessionDownloadTask *, NSURL *);
  139. ((OriginalImp)currentIMP)(object, selector, session, downloadTask, location);
  140. }];
  141. }
  142. }
  143. /** Instruments URLSession:downloadTask:didWriteData:totalBytesWritten:totalBytesExpectedToWrite:.
  144. *
  145. * @param instrumentor The FPRClassInstrumentor to add the selector instrumentor to.
  146. */
  147. FOUNDATION_STATIC_INLINE
  148. NS_EXTENSION_UNAVAILABLE("Firebase Performance is not supported for extensions.")
  149. void InstrumentURLSessionDownloadTaskDidWriteDataTotalBytesWrittenTotalBytesExpectedToWrite(
  150. FPRClassInstrumentor *instrumentor) {
  151. SEL selector = @selector(URLSession:
  152. downloadTask:didWriteData:totalBytesWritten:totalBytesExpectedToWrite:);
  153. FPRSelectorInstrumentor *selectorInstrumentor =
  154. [instrumentor instrumentorForInstanceSelector:selector];
  155. if (selectorInstrumentor) {
  156. IMP currentIMP = selectorInstrumentor.currentIMP;
  157. [selectorInstrumentor
  158. setReplacingBlock:^(id object, NSURLSession *session,
  159. NSURLSessionDownloadTask *downloadTask, int64_t bytesWritten,
  160. int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {
  161. FPRNetworkTrace *trace = [FPRNetworkTrace networkTraceFromObject:downloadTask];
  162. [trace checkpointState:FPRNetworkTraceCheckpointStateResponseReceived];
  163. trace.responseSize = totalBytesWritten;
  164. if (totalBytesWritten >= totalBytesExpectedToWrite) {
  165. if ([downloadTask.response isKindOfClass:[NSHTTPURLResponse class]]) {
  166. NSHTTPURLResponse *response = (NSHTTPURLResponse *)downloadTask.response;
  167. [trace didCompleteRequestWithResponse:response error:downloadTask.error];
  168. [FPRNetworkTrace removeNetworkTraceFromObject:downloadTask];
  169. }
  170. }
  171. typedef void (*OriginalImp)(id, SEL, NSURLSession *, NSURLSessionDownloadTask *, int64_t,
  172. int64_t, int64_t);
  173. ((OriginalImp)currentIMP)(object, selector, session, downloadTask, bytesWritten,
  174. totalBytesWritten, totalBytesExpectedToWrite);
  175. }];
  176. }
  177. }
  178. #pragma mark - Helper functions
  179. FOUNDATION_STATIC_INLINE
  180. NS_EXTENSION_UNAVAILABLE("Firebase Performance is not supported for extensions.")
  181. void CopySelector(SEL selector, FPRObjectInstrumentor *instrumentor) {
  182. static Class fromClass = Nil;
  183. static dispatch_once_t onceToken;
  184. dispatch_once(&onceToken, ^{
  185. fromClass = [FPRNSURLSessionDelegate class];
  186. });
  187. if (![instrumentor.instrumentedObject respondsToSelector:selector]) {
  188. [instrumentor copySelector:selector fromClass:fromClass isClassSelector:NO];
  189. }
  190. }
  191. #pragma mark - FPRNSURLSessionDelegateInstrument
  192. @implementation FPRNSURLSessionDelegateInstrument
  193. - (void)registerInstrumentors {
  194. // Do nothing by default; classes will be instrumented on-demand upon discovery.
  195. }
  196. - (void)registerClass:(Class)aClass {
  197. dispatch_sync(GetInstrumentationQueue(), ^{
  198. // If this class has already been instrumented, just return.
  199. FPRClassInstrumentor *instrumentor = [[FPRClassInstrumentor alloc] initWithClass:aClass];
  200. if (![self registerClassInstrumentor:instrumentor]) {
  201. return;
  202. }
  203. // NSURLSessionTaskDelegate methods.
  204. InstrumentURLSessionTaskDidCompleteWithError(instrumentor);
  205. InstrumentURLSessionTaskDidSendBodyDataTotalBytesSentTotalBytesExpectedToSend(instrumentor);
  206. // NSURLSessionDataDelegate methods.
  207. InstrumentURLSessionDataTaskDidReceiveData(instrumentor);
  208. // NSURLSessionDownloadDelegate methods.
  209. InstrumentURLSessionDownloadTaskDidFinishDownloadToURL(instrumentor);
  210. InstrumentURLSessionDownloadTaskDidWriteDataTotalBytesWrittenTotalBytesExpectedToWrite(
  211. instrumentor);
  212. [instrumentor swizzle];
  213. });
  214. }
  215. - (void)registerObject:(id)object {
  216. dispatch_sync(GetInstrumentationQueue(), ^{
  217. if ([object respondsToSelector:@selector(gul_class)]) {
  218. return;
  219. }
  220. FPRObjectInstrumentor *instrumentor = [[FPRObjectInstrumentor alloc] initWithObject:object];
  221. // Register the non-swizzled versions of these methods.
  222. // NSURLSessionTaskDelegate methods.
  223. CopySelector(@selector(URLSession:task:didCompleteWithError:), instrumentor);
  224. CopySelector(@selector(URLSession:
  225. task:didSendBodyData:totalBytesSent:totalBytesExpectedToSend:),
  226. instrumentor);
  227. // NSURLSessionDataDelegate methods.
  228. CopySelector(@selector(URLSession:dataTask:didReceiveData:), instrumentor);
  229. // NSURLSessionDownloadDelegate methods.
  230. CopySelector(@selector(URLSession:downloadTask:didFinishDownloadingToURL:), instrumentor);
  231. CopySelector(@selector(URLSession:downloadTask:didResumeAtOffset:expectedTotalBytes:),
  232. instrumentor);
  233. CopySelector(@selector(URLSession:
  234. downloadTask:didWriteData:totalBytesWritten:totalBytesExpectedToWrite:),
  235. instrumentor);
  236. [instrumentor swizzle];
  237. });
  238. }
  239. @end