FPRNSURLConnectionInstrument.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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/FPRNSURLConnectionInstrument.h"
  15. #import "FirebasePerformance/Sources/Instrumentation/Network/FPRNSURLConnectionInstrument_Private.h"
  16. #import "FirebasePerformance/Sources/Common/FPRDiagnostics.h"
  17. #import "FirebasePerformance/Sources/Instrumentation/FPRClassInstrumentor.h"
  18. #import "FirebasePerformance/Sources/Instrumentation/FPRInstrument_Private.h"
  19. #import "FirebasePerformance/Sources/Instrumentation/FPRNetworkTrace.h"
  20. #import "FirebasePerformance/Sources/Instrumentation/FPRObjectInstrumentor.h"
  21. #import "FirebasePerformance/Sources/Instrumentation/FPRSelectorInstrumentor.h"
  22. #import "FirebasePerformance/Sources/Instrumentation/Network/Delegates/FPRNSURLConnectionDelegate.h"
  23. #import "FirebasePerformance/Sources/Instrumentation/Network/FPRNetworkInstrumentHelpers.h"
  24. #import <GoogleUtilities/GULObjectSwizzler.h>
  25. #import "FirebasePerformance/Sources/Configurations/FPRConfigurations.h"
  26. static NSString *const kFPRDelegateKey = @"kFPRDelegateKey";
  27. typedef void (^FPRNSURLConnectionCompletionHandler)(NSURLResponse *_Nullable response,
  28. NSData *_Nullable data,
  29. NSError *_Nullable connectionError);
  30. /** Returns the dispatch queue for all instrumentation to occur on. */
  31. static dispatch_queue_t GetInstrumentationQueue() {
  32. static dispatch_queue_t queue = nil;
  33. static dispatch_once_t token = 0;
  34. dispatch_once(&token, ^{
  35. queue = dispatch_queue_create("com.google.FPRNSURLConnectionInstrumentation",
  36. DISPATCH_QUEUE_SERIAL);
  37. });
  38. return queue;
  39. }
  40. /** Instruments +sendAsynchronousRequest:queue:completionHandler:.
  41. *
  42. * @param instrumentor The FPRClassInstrumentor to add the selector instrumentor to.
  43. */
  44. FOUNDATION_STATIC_INLINE
  45. NS_EXTENSION_UNAVAILABLE("Firebase Performance is not supported for extensions.")
  46. void InstrumentSendAsynchronousRequestQueueCompletionHandler(FPRClassInstrumentor *instrumentor) {
  47. SEL selector = @selector(sendAsynchronousRequest:queue:completionHandler:);
  48. FPRSelectorInstrumentor *selectorInstrumentor = SelectorInstrumentor(selector, instrumentor, YES);
  49. IMP currentIMP = selectorInstrumentor.currentIMP;
  50. [selectorInstrumentor
  51. setReplacingBlock:^(id connection, NSURLRequest *request, NSOperationQueue *queue,
  52. FPRNSURLConnectionCompletionHandler completionHandler) {
  53. FPRNetworkTrace *trace = [[FPRNetworkTrace alloc] initWithURLRequest:request];
  54. [trace start];
  55. [trace checkpointState:FPRNetworkTraceCheckpointStateInitiated];
  56. // The completionHandler needs to be there for FPRNetworkTrace purposes, even if originally
  57. // nil.
  58. FPRNSURLConnectionCompletionHandler wrappedCompletionHandler =
  59. ^(NSURLResponse *_Nullable response, NSData *_Nullable data,
  60. NSError *_Nullable connectionError) {
  61. [trace didReceiveData:data];
  62. [trace didCompleteRequestWithResponse:response error:connectionError];
  63. if (completionHandler) {
  64. completionHandler(response, data, connectionError);
  65. }
  66. };
  67. typedef void (*OriginalImp)(id, SEL, NSURLRequest *, NSOperationQueue *,
  68. FPRNSURLConnectionCompletionHandler);
  69. ((OriginalImp)currentIMP)(connection, selector, request, queue, wrappedCompletionHandler);
  70. }];
  71. }
  72. /** Instruments -initWithRequest:delegate:.
  73. *
  74. * @param instrumentor The FPRClassInstrumentor to add the selector instrumentor to.
  75. * @param delegateInstrument The FPRNSURLConnectionDelegateInstrument to potentially add a new
  76. * class to.
  77. */
  78. FOUNDATION_STATIC_INLINE
  79. NS_EXTENSION_UNAVAILABLE("Firebase Performance is not supported for extensions.")
  80. void InstrumentInitWithRequestDelegate(FPRClassInstrumentor *instrumentor,
  81. FPRNSURLConnectionDelegateInstrument *delegateInstrument) {
  82. SEL selector = @selector(initWithRequest:delegate:);
  83. FPRSelectorInstrumentor *selectorInstrumentor = SelectorInstrumentor(selector, instrumentor, NO);
  84. IMP currentIMP = selectorInstrumentor.currentIMP;
  85. [selectorInstrumentor setReplacingBlock:^(id connection, NSURLRequest *request, id delegate) {
  86. if (delegate) {
  87. [delegateInstrument registerClass:[delegate class]];
  88. [delegateInstrument registerObject:delegate];
  89. [GULObjectSwizzler setAssociatedObject:connection
  90. key:kFPRDelegateKey
  91. value:delegate
  92. association:GUL_ASSOCIATION_ASSIGN];
  93. } else {
  94. delegate = [[FPRNSURLConnectionDelegate alloc] init];
  95. [GULObjectSwizzler setAssociatedObject:connection
  96. key:kFPRDelegateKey
  97. value:delegate
  98. association:GUL_ASSOCIATION_ASSIGN];
  99. }
  100. typedef NSURLConnection *(*OriginalImp)(id, SEL, NSURLRequest *, id);
  101. return ((OriginalImp)currentIMP)(connection, selector, request, delegate);
  102. }];
  103. }
  104. /** Instruments -initWithRequest:delegate:startImmediately:.
  105. *
  106. * @param instrumentor The FPRClassInstrumentor to add the selector instrumentor to.
  107. * @param delegateInstrument The FPRNSURLConnectionDelegateInstrument to potentially add a new
  108. * class to.
  109. */
  110. FOUNDATION_STATIC_INLINE
  111. NS_EXTENSION_UNAVAILABLE("Firebase Performance is not supported for extensions.")
  112. void InstrumentInitWithRequestDelegateStartImmediately(
  113. FPRClassInstrumentor *instrumentor, FPRNSURLConnectionDelegateInstrument *delegateInstrument) {
  114. SEL selector = @selector(initWithRequest:delegate:startImmediately:);
  115. FPRSelectorInstrumentor *selectorInstrumentor = SelectorInstrumentor(selector, instrumentor, NO);
  116. IMP currentIMP = selectorInstrumentor.currentIMP;
  117. [selectorInstrumentor setReplacingBlock:^(id connection, NSURLRequest *request, id delegate,
  118. BOOL startImmediately) {
  119. if (delegate) {
  120. [delegateInstrument registerClass:[delegate class]];
  121. [delegateInstrument registerObject:delegate];
  122. [GULObjectSwizzler setAssociatedObject:connection
  123. key:kFPRDelegateKey
  124. value:delegate
  125. association:GUL_ASSOCIATION_ASSIGN];
  126. } else {
  127. delegate = [[FPRNSURLConnectionDelegate alloc] init];
  128. [GULObjectSwizzler setAssociatedObject:connection
  129. key:kFPRDelegateKey
  130. value:delegate
  131. association:GUL_ASSOCIATION_ASSIGN];
  132. }
  133. typedef NSURLConnection *(*OriginalImp)(id, SEL, NSURLRequest *, id, BOOL);
  134. return ((OriginalImp)currentIMP)(connection, selector, request, delegate, startImmediately);
  135. }];
  136. }
  137. /** Instruments -start.
  138. *
  139. * @param instrumentor The FPRClassInstrumentor to add the selector instrumentor to.
  140. */
  141. FOUNDATION_STATIC_INLINE
  142. NS_EXTENSION_UNAVAILABLE("Firebase Performance is not supported for extensions.")
  143. void InstrumentConnectionStart(FPRClassInstrumentor *instrumentor) {
  144. SEL selector = @selector(start);
  145. FPRSelectorInstrumentor *selectorInstrumentor = SelectorInstrumentor(selector, instrumentor, NO);
  146. IMP currentIMP = selectorInstrumentor.currentIMP;
  147. [selectorInstrumentor setReplacingBlock:^(id object) {
  148. typedef void (*OriginalImp)(id, SEL);
  149. NSURLConnection *connection = (NSURLConnection *)object;
  150. if ([GULObjectSwizzler getAssociatedObject:connection key:kFPRDelegateKey]) {
  151. FPRNetworkTrace *trace =
  152. [[FPRNetworkTrace alloc] initWithURLRequest:connection.originalRequest];
  153. [trace start];
  154. [trace checkpointState:FPRNetworkTraceCheckpointStateInitiated];
  155. [FPRNetworkTrace addNetworkTrace:trace toObject:connection];
  156. }
  157. ((OriginalImp)currentIMP)(connection, selector);
  158. }];
  159. }
  160. /** Instruments -cancel.
  161. *
  162. * @param instrumentor The FPRClassInstrumentor to add the selector instrumentor to.
  163. */
  164. FOUNDATION_STATIC_INLINE
  165. NS_EXTENSION_UNAVAILABLE("Firebase Performance is not supported for extensions.")
  166. void InstrumentConnectionCancel(FPRClassInstrumentor *instrumentor) {
  167. SEL selector = @selector(cancel);
  168. FPRSelectorInstrumentor *selectorInstrumentor = SelectorInstrumentor(selector, instrumentor, NO);
  169. IMP currentIMP = selectorInstrumentor.currentIMP;
  170. [selectorInstrumentor setReplacingBlock:^(id object) {
  171. typedef void (*OriginalImp)(id, SEL);
  172. NSURLConnection *connection = (NSURLConnection *)object;
  173. FPRNetworkTrace *trace = [FPRNetworkTrace networkTraceFromObject:connection];
  174. [trace didCompleteRequestWithResponse:nil error:nil];
  175. [FPRNetworkTrace removeNetworkTraceFromObject:connection];
  176. ((OriginalImp)currentIMP)(connection, selector);
  177. }];
  178. }
  179. @implementation FPRNSURLConnectionInstrument
  180. - (instancetype)init {
  181. self = [super init];
  182. if (self) {
  183. _delegateInstrument = [[FPRNSURLConnectionDelegateInstrument alloc] init];
  184. [_delegateInstrument registerInstrumentors];
  185. }
  186. return self;
  187. }
  188. - (void)dealloc {
  189. [_delegateInstrument deregisterInstrumentors];
  190. }
  191. - (void)registerInstrumentors {
  192. dispatch_sync(GetInstrumentationQueue(), ^{
  193. FPRClassInstrumentor *instrumentor =
  194. [[FPRClassInstrumentor alloc] initWithClass:[NSURLConnection class]];
  195. if (![self registerClassInstrumentor:instrumentor]) {
  196. FPRAssert(NO, @"NSURLConnection should only be instrumented once.");
  197. }
  198. InstrumentSendAsynchronousRequestQueueCompletionHandler(instrumentor);
  199. InstrumentInitWithRequestDelegate(instrumentor, _delegateInstrument);
  200. InstrumentInitWithRequestDelegateStartImmediately(instrumentor, _delegateInstrument);
  201. InstrumentConnectionStart(instrumentor);
  202. InstrumentConnectionCancel(instrumentor);
  203. [instrumentor swizzle];
  204. });
  205. }
  206. - (void)deregisterInstrumentors {
  207. [_delegateInstrument deregisterInstrumentors];
  208. [super deregisterInstrumentors];
  209. }
  210. @end