FPRNSURLConnectionDelegateInstrument.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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/FPRNSURLConnectionDelegateInstrument.h"
  15. #import "FirebasePerformance/Sources/Instrumentation/FPRClassInstrumentor.h"
  16. #import "FirebasePerformance/Sources/Instrumentation/FPRInstrument_Private.h"
  17. #import "FirebasePerformance/Sources/Instrumentation/FPRNetworkTrace.h"
  18. #import "FirebasePerformance/Sources/Instrumentation/FPRSelectorInstrumentor.h"
  19. #import "FirebasePerformance/Sources/Instrumentation/Network/Delegates/FPRNSURLConnectionDelegate.h"
  20. #import "FirebasePerformance/Sources/Instrumentation/Network/FPRNetworkInstrumentHelpers.h"
  21. #pragma mark - NSURLConnectionDelegate methods
  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.FPRNSURLConnectionDelegateInstrument",
  28. DISPATCH_QUEUE_SERIAL);
  29. });
  30. return queue;
  31. }
  32. /** Instruments connection:didFailWithError:.
  33. *
  34. * @param instrumentor The FPRClassInstrumentor to add the selector instrumentor to.
  35. */
  36. FOUNDATION_STATIC_INLINE
  37. NS_EXTENSION_UNAVAILABLE("Firebase Performance is not supported for extensions.")
  38. void InstrumentConnectionDidFailWithError(FPRClassInstrumentor *instrumentor) {
  39. SEL selector = @selector(connection:didFailWithError:);
  40. FPRSelectorInstrumentor *selectorInstrumentor =
  41. [instrumentor instrumentorForInstanceSelector:selector];
  42. if (selectorInstrumentor) {
  43. IMP currentIMP = selectorInstrumentor.currentIMP;
  44. [selectorInstrumentor
  45. setReplacingBlock:^(id object, NSURLConnection *connection, NSError *error) {
  46. FPRNetworkTrace *trace = [FPRNetworkTrace networkTraceFromObject:connection];
  47. [trace didCompleteRequestWithResponse:nil error:error];
  48. [FPRNetworkTrace removeNetworkTraceFromObject:connection];
  49. typedef void (*OriginalImp)(id, SEL, NSURLConnection *, NSError *);
  50. ((OriginalImp)currentIMP)(object, selector, connection, error);
  51. }];
  52. }
  53. }
  54. #pragma mark - NSURLConnectionDataDelegate methods
  55. /** Instruments connection:willSendRequest:redirectResponse:.
  56. *
  57. * @param instrumentor The FPRClassInstrumentor to add the selector instrumentor to.
  58. */
  59. FOUNDATION_STATIC_INLINE
  60. NS_EXTENSION_UNAVAILABLE("Firebase Performance is not supported for extensions.")
  61. void InstrumentConnectionWillSendRequestRedirectResponse(FPRClassInstrumentor *instrumentor) {
  62. SEL selector = @selector(connection:willSendRequest:redirectResponse:);
  63. FPRSelectorInstrumentor *selectorInstrumentor =
  64. [instrumentor instrumentorForInstanceSelector:selector];
  65. if (selectorInstrumentor) {
  66. IMP currentIMP = selectorInstrumentor.currentIMP;
  67. [selectorInstrumentor setReplacingBlock:^(id object, NSURLConnection *connection,
  68. NSURLRequest *request, NSURLResponse *response) {
  69. FPRNetworkTrace *trace = [FPRNetworkTrace networkTraceFromObject:connection];
  70. [trace checkpointState:FPRNetworkTraceCheckpointStateResponseReceived];
  71. typedef NSURLRequest *(*OriginalImp)(id, SEL, NSURLConnection *, NSURLRequest *,
  72. NSURLResponse *);
  73. return ((OriginalImp)currentIMP)(object, selector, connection, request, response);
  74. }];
  75. }
  76. }
  77. /** Instruments connection:didReceiveResponse:.
  78. *
  79. * @param instrumentor The FPRClassInstrumentor to add the selector instrumentor to.
  80. */
  81. FOUNDATION_STATIC_INLINE
  82. NS_EXTENSION_UNAVAILABLE("Firebase Performance is not supported for extensions.")
  83. void InstrumentConnectionDidReceiveResponse(FPRClassInstrumentor *instrumentor) {
  84. SEL selector = @selector(connection:didReceiveResponse:);
  85. FPRSelectorInstrumentor *selectorInstrumentor =
  86. [instrumentor instrumentorForInstanceSelector:selector];
  87. if (selectorInstrumentor) {
  88. IMP currentIMP = selectorInstrumentor.currentIMP;
  89. [selectorInstrumentor
  90. setReplacingBlock:^(id object, NSURLConnection *connection, NSURLResponse *response) {
  91. FPRNetworkTrace *trace = [FPRNetworkTrace networkTraceFromObject:connection];
  92. if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
  93. trace.responseCode = (int32_t)((NSHTTPURLResponse *)response).statusCode;
  94. }
  95. [trace checkpointState:FPRNetworkTraceCheckpointStateResponseReceived];
  96. typedef void (*OriginalImp)(id, SEL, NSURLConnection *, NSURLResponse *);
  97. ((OriginalImp)currentIMP)(object, selector, connection, response);
  98. }];
  99. }
  100. }
  101. /** Instruments connection:didReceiveData:.
  102. *
  103. * @param instrumentor The FPRClassInstrumentor to add the selector instrumentor to.
  104. */
  105. FOUNDATION_STATIC_INLINE
  106. NS_EXTENSION_UNAVAILABLE("Firebase Performance is not supported for extensions.")
  107. void InstrumentConnectionDidReceiveData(FPRClassInstrumentor *instrumentor) {
  108. SEL selector = @selector(connection:didReceiveData:);
  109. FPRSelectorInstrumentor *selectorInstrumentor =
  110. [instrumentor instrumentorForInstanceSelector:selector];
  111. if (selectorInstrumentor) {
  112. IMP currentIMP = selectorInstrumentor.currentIMP;
  113. [selectorInstrumentor
  114. setReplacingBlock:^(id object, NSURLConnection *connection, NSData *data) {
  115. FPRNetworkTrace *trace = [FPRNetworkTrace networkTraceFromObject:connection];
  116. [trace checkpointState:FPRNetworkTraceCheckpointStateResponseReceived];
  117. trace.responseSize += data.length;
  118. typedef void (*OriginalImp)(id, SEL, NSURLConnection *, NSData *);
  119. ((OriginalImp)currentIMP)(object, selector, connection, data);
  120. }];
  121. }
  122. }
  123. /** Instruments connection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite:.
  124. *
  125. * @param instrumentor The FPRClassInstrumentor to add the selector instrumentor to.
  126. */
  127. FOUNDATION_STATIC_INLINE
  128. NS_EXTENSION_UNAVAILABLE("Firebase Performance is not supported for extensions.")
  129. void InstrumentConnectionAllTheTotals(FPRClassInstrumentor *instrumentor) {
  130. SEL selector = @selector(connection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite:);
  131. FPRSelectorInstrumentor *selectorInstrumentor =
  132. [instrumentor instrumentorForInstanceSelector:selector];
  133. if (selectorInstrumentor) {
  134. IMP currentIMP = selectorInstrumentor.currentIMP;
  135. [selectorInstrumentor
  136. setReplacingBlock:^(id object, NSURLConnection *connection, NSInteger bytesWritten,
  137. NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite) {
  138. FPRNetworkTrace *trace = [FPRNetworkTrace networkTraceFromObject:connection];
  139. trace.requestSize = totalBytesWritten;
  140. if (totalBytesWritten >= totalBytesExpectedToWrite) {
  141. [trace checkpointState:FPRNetworkTraceCheckpointStateRequestCompleted];
  142. }
  143. typedef void (*OriginalImp)(id, SEL, NSURLConnection *, NSInteger, NSInteger, NSInteger);
  144. ((OriginalImp)currentIMP)(object, selector, connection, bytesWritten, totalBytesWritten,
  145. totalBytesExpectedToWrite);
  146. }];
  147. }
  148. }
  149. /** Instruments connectionDidFinishLoading:.
  150. *
  151. * @param instrumentor The FPRClassInstrumentor to add the selector instrumentor to.
  152. */
  153. FOUNDATION_STATIC_INLINE
  154. NS_EXTENSION_UNAVAILABLE("Firebase Performance is not supported for extensions.")
  155. void InstrumentConnectionDidFinishLoading(FPRClassInstrumentor *instrumentor) {
  156. SEL selector = @selector(connectionDidFinishLoading:);
  157. FPRSelectorInstrumentor *selectorInstrumentor =
  158. [instrumentor instrumentorForInstanceSelector:selector];
  159. if (selectorInstrumentor) {
  160. IMP currentIMP = selectorInstrumentor.currentIMP;
  161. [selectorInstrumentor setReplacingBlock:^(id object, NSURLConnection *connection) {
  162. FPRNetworkTrace *trace = [FPRNetworkTrace networkTraceFromObject:connection];
  163. [trace didCompleteRequestWithResponse:nil error:nil];
  164. [FPRNetworkTrace removeNetworkTraceFromObject:connection];
  165. typedef void (*OriginalImp)(id, SEL, NSURLConnection *);
  166. ((OriginalImp)currentIMP)(object, selector, connection);
  167. }];
  168. }
  169. }
  170. /** Instruments connection:didWriteData:totalBytesWritten:expectedTotalBytes:.
  171. *
  172. * @param instrumentor The FPRClassInstrumentor to add the selector instrumentor to.
  173. */
  174. FOUNDATION_STATIC_INLINE
  175. NS_EXTENSION_UNAVAILABLE("Firebase Performance is not supported for extensions.")
  176. void InstrumentConnectionDidWriteDataTotalBytesWrittenExpectedTotalBytes(
  177. FPRClassInstrumentor *instrumentor) {
  178. SEL selector = @selector(connection:didWriteData:totalBytesWritten:expectedTotalBytes:);
  179. FPRSelectorInstrumentor *selectorInstrumentor =
  180. [instrumentor instrumentorForInstanceSelector:selector];
  181. if (selectorInstrumentor) {
  182. IMP currentIMP = selectorInstrumentor.currentIMP;
  183. [selectorInstrumentor
  184. setReplacingBlock:^(id object, NSURLConnection *connection, long long bytesWritten,
  185. long long totalBytesWritten, long long expectedTotalBytes) {
  186. FPRNetworkTrace *trace = [FPRNetworkTrace networkTraceFromObject:connection];
  187. trace.requestSize = totalBytesWritten;
  188. typedef void (*OriginalImp)(id, SEL, NSURLConnection *, long long, long long, long long);
  189. ((OriginalImp)currentIMP)(object, selector, connection, bytesWritten, totalBytesWritten,
  190. expectedTotalBytes);
  191. }];
  192. }
  193. }
  194. /** Instruments connectionDidFinishDownloading:destinationURL:.
  195. *
  196. * @param instrumentor The FPRClassInstrumentor to add the selector instrumentor to.
  197. */
  198. FOUNDATION_STATIC_INLINE
  199. NS_EXTENSION_UNAVAILABLE("Firebase Performance is not supported for extensions.")
  200. void InstrumentConnectionDidFinishDownloadingDestinationURL(FPRClassInstrumentor *instrumentor) {
  201. SEL selector = @selector(connectionDidFinishDownloading:destinationURL:);
  202. FPRSelectorInstrumentor *selectorInstrumentor =
  203. [instrumentor instrumentorForInstanceSelector:selector];
  204. if (selectorInstrumentor) {
  205. IMP currentIMP = selectorInstrumentor.currentIMP;
  206. [selectorInstrumentor
  207. setReplacingBlock:^(id object, NSURLConnection *connection, NSURL *destinationURL) {
  208. FPRNetworkTrace *trace = [FPRNetworkTrace networkTraceFromObject:connection];
  209. [trace didReceiveFileURL:destinationURL];
  210. [trace didCompleteRequestWithResponse:nil error:nil];
  211. [FPRNetworkTrace removeNetworkTraceFromObject:connection];
  212. typedef void (*OriginalImp)(id, SEL, NSURLConnection *, NSURL *);
  213. ((OriginalImp)currentIMP)(object, selector, connection, destinationURL);
  214. }];
  215. }
  216. }
  217. #pragma mark - Helper functions
  218. FOUNDATION_STATIC_INLINE
  219. NS_EXTENSION_UNAVAILABLE("Firebase Performance is not supported for extensions.")
  220. void CopySelector(SEL selector, FPRObjectInstrumentor *instrumentor) {
  221. static Class fromClass = Nil;
  222. static dispatch_once_t onceToken;
  223. dispatch_once(&onceToken, ^{
  224. fromClass = [FPRNSURLConnectionDelegate class];
  225. });
  226. if (![instrumentor.instrumentedObject respondsToSelector:selector]) {
  227. [instrumentor copySelector:selector fromClass:fromClass isClassSelector:NO];
  228. }
  229. }
  230. #pragma mark - FPRNSURLConnectionDelegateInstrument
  231. @implementation FPRNSURLConnectionDelegateInstrument
  232. - (void)registerInstrumentors {
  233. // Do nothing by default. classes will be instrumented on-demand upon discovery.
  234. }
  235. - (void)registerClass:(Class)aClass {
  236. dispatch_sync(GetInstrumentationQueue(), ^{
  237. // If this class has already been instrumented, just return.
  238. FPRClassInstrumentor *instrumentor = [[FPRClassInstrumentor alloc] initWithClass:aClass];
  239. if (![self registerClassInstrumentor:instrumentor]) {
  240. return;
  241. }
  242. InstrumentConnectionDidFailWithError(instrumentor);
  243. InstrumentConnectionWillSendRequestRedirectResponse(instrumentor);
  244. InstrumentConnectionDidReceiveResponse(instrumentor);
  245. InstrumentConnectionDidReceiveData(instrumentor);
  246. InstrumentConnectionAllTheTotals(instrumentor);
  247. InstrumentConnectionDidFinishLoading(instrumentor);
  248. InstrumentConnectionDidWriteDataTotalBytesWrittenExpectedTotalBytes(instrumentor);
  249. InstrumentConnectionDidFinishDownloadingDestinationURL(instrumentor);
  250. [instrumentor swizzle];
  251. });
  252. }
  253. - (void)registerObject:(id)object {
  254. dispatch_sync(GetInstrumentationQueue(), ^{
  255. if ([object respondsToSelector:@selector(gul_class)]) {
  256. return;
  257. }
  258. if (![self isObjectInstrumentable:object]) {
  259. return;
  260. }
  261. FPRObjectInstrumentor *instrumentor = [[FPRObjectInstrumentor alloc] initWithObject:object];
  262. // Register the non-swizzled versions of these methods.
  263. CopySelector(@selector(connection:didFailWithError:), instrumentor);
  264. CopySelector(@selector(connection:willSendRequest:redirectResponse:), instrumentor);
  265. CopySelector(@selector(connection:didReceiveResponse:), instrumentor);
  266. CopySelector(@selector(connection:didReceiveData:), instrumentor);
  267. CopySelector(@selector(connection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite:),
  268. instrumentor);
  269. if (![object respondsToSelector:@selector(connectionDidFinishDownloading:destinationURL:)]) {
  270. CopySelector(@selector(connectionDidFinishLoading:), instrumentor);
  271. }
  272. CopySelector(@selector(connection:didWriteData:totalBytesWritten:expectedTotalBytes:),
  273. instrumentor);
  274. if (![object respondsToSelector:@selector(connectionDidFinishLoading:)]) {
  275. CopySelector(@selector(connectionDidFinishDownloading:destinationURL:), instrumentor);
  276. }
  277. [instrumentor swizzle];
  278. });
  279. }
  280. @end