FPRNSURLConnectionDelegateInstrument.m 13 KB

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