FPRUIViewControllerInstrument.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 <TargetConditionals.h>
  15. #import <UIKit/UIKit.h>
  16. #import "FirebasePerformance/Sources/AppActivity/FPRScreenTraceTracker+Private.h"
  17. #import "FirebasePerformance/Sources/Common/FPRDiagnostics.h"
  18. #import "FirebasePerformance/Sources/Configurations/FPRConfigurations.h"
  19. #import "FirebasePerformance/Sources/Instrumentation/FPRClassInstrumentor.h"
  20. #import "FirebasePerformance/Sources/Instrumentation/FPRInstrument_Private.h"
  21. #import "FirebasePerformance/Sources/Instrumentation/FPRSelectorInstrumentor.h"
  22. #import "FirebasePerformance/Sources/Instrumentation/Network/FPRNetworkInstrumentHelpers.h"
  23. #import "FirebasePerformance/Sources/Instrumentation/UIKit/FPRUIViewControllerInstrument.h"
  24. #import <GoogleUtilities/GULAppEnvironmentUtil.h>
  25. #import <GoogleUtilities/GULOriginalIMPConvenienceMacros.h>
  26. /** Returns the dispatch queue for all instrumentation to occur on. */
  27. static dispatch_queue_t GetInstrumentationQueue(void) {
  28. static dispatch_queue_t queue = nil;
  29. static dispatch_once_t token = 0;
  30. dispatch_once(&token, ^{
  31. queue = dispatch_queue_create("com.google.FPRUIViewControllerInstrumentation",
  32. DISPATCH_QUEUE_SERIAL);
  33. });
  34. return queue;
  35. }
  36. // Returns the singleton UIApplication of the application this is currently running in or nil if
  37. // it's in an app extension.
  38. NS_EXTENSION_UNAVAILABLE("Firebase Performance is not supported for extensions.")
  39. static UIApplication *FPRSharedApplication(void) {
  40. if ([GULAppEnvironmentUtil isAppExtension]) {
  41. return nil;
  42. }
  43. return [UIApplication sharedApplication];
  44. }
  45. @implementation FPRUIViewControllerInstrument
  46. /** Wraps -viewDidAppear:
  47. *
  48. * @param instrument The FPRUIViewController instance.
  49. * @param instrumentor The FPRClassInstrumentor to add the selector instrumentor to.
  50. */
  51. FOUNDATION_STATIC_INLINE
  52. NS_EXTENSION_UNAVAILABLE("Firebase Performance is not supported for extensions.")
  53. void InstrumentViewDidAppear(FPRUIViewControllerInstrument *instrument,
  54. FPRClassInstrumentor *instrumentor) {
  55. SEL selector = @selector(viewDidAppear:);
  56. FPRSelectorInstrumentor *selectorInstrumentor = SelectorInstrumentor(selector, instrumentor, NO);
  57. IMP oldViewDidAppearIMP = [selectorInstrumentor currentIMP];
  58. [selectorInstrumentor setReplacingBlock:^void(id _self, BOOL animated) {
  59. if (oldViewDidAppearIMP) {
  60. GUL_INVOKE_ORIGINAL_IMP1(_self, selector, void, oldViewDidAppearIMP, animated);
  61. }
  62. // This has to be called on the main thread and so it's done here instead of in
  63. // FPRScreenTraceTracker.
  64. // TODO(#13067): Replace keyWindow usage (deprecated in iOS and unavailable in visionOS).
  65. #if !defined(TARGET_OS_VISION) || !TARGET_OS_VISION
  66. #pragma clang diagnostic push
  67. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  68. if ([((UIViewController *)_self).view isDescendantOfView:FPRSharedApplication().keyWindow]) {
  69. #pragma clang diagnostic pop
  70. [[FPRScreenTraceTracker sharedInstance] viewControllerDidAppear:_self];
  71. }
  72. #endif
  73. }];
  74. }
  75. /** Wraps -viewDidDisappear:
  76. *
  77. * @param instrument The FPRUIViewController instance.
  78. * @param instrumentor The FPRClassInstrumentor to add the selector instrumentor to.
  79. */
  80. FOUNDATION_STATIC_INLINE
  81. NS_EXTENSION_UNAVAILABLE("Firebase Performance is not supported for extensions.")
  82. void InstrumentViewDidDisappear(FPRUIViewControllerInstrument *instrument,
  83. FPRClassInstrumentor *instrumentor) {
  84. SEL selector = @selector(viewDidDisappear:);
  85. FPRSelectorInstrumentor *selectorInstrumentor = SelectorInstrumentor(selector, instrumentor, NO);
  86. IMP oldViewDidDisappearIMP = [selectorInstrumentor currentIMP];
  87. [selectorInstrumentor setReplacingBlock:^void(id _self, BOOL animated) {
  88. if (oldViewDidDisappearIMP) {
  89. GUL_INVOKE_ORIGINAL_IMP1(_self, selector, void, oldViewDidDisappearIMP, animated);
  90. }
  91. [[FPRScreenTraceTracker sharedInstance] viewControllerDidDisappear:_self];
  92. }];
  93. }
  94. - (void)registerInstrumentors {
  95. dispatch_sync(GetInstrumentationQueue(), ^{
  96. FPRClassInstrumentor *instrumentor =
  97. [[FPRClassInstrumentor alloc] initWithClass:[UIViewController class]];
  98. if (![self registerClassInstrumentor:instrumentor]) {
  99. FPRAssert(NO, @"UIViewController should only be instrumented once.");
  100. }
  101. InstrumentViewDidAppear(self, instrumentor);
  102. InstrumentViewDidDisappear(self, instrumentor);
  103. [instrumentor swizzle];
  104. });
  105. }
  106. @end