FPRUIViewControllerInstrument.m 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. if (FPRSharedApplication() && ((UIViewController *)_self).view.window.keyWindow) {
  65. [[FPRScreenTraceTracker sharedInstance] viewControllerDidAppear:_self];
  66. }
  67. }];
  68. }
  69. /** Wraps -viewDidDisappear:
  70. *
  71. * @param instrument The FPRUIViewController instance.
  72. * @param instrumentor The FPRClassInstrumentor to add the selector instrumentor to.
  73. */
  74. FOUNDATION_STATIC_INLINE
  75. NS_EXTENSION_UNAVAILABLE("Firebase Performance is not supported for extensions.")
  76. void InstrumentViewDidDisappear(FPRUIViewControllerInstrument *instrument,
  77. FPRClassInstrumentor *instrumentor) {
  78. SEL selector = @selector(viewDidDisappear:);
  79. FPRSelectorInstrumentor *selectorInstrumentor = SelectorInstrumentor(selector, instrumentor, NO);
  80. IMP oldViewDidDisappearIMP = [selectorInstrumentor currentIMP];
  81. [selectorInstrumentor setReplacingBlock:^void(id _self, BOOL animated) {
  82. if (oldViewDidDisappearIMP) {
  83. GUL_INVOKE_ORIGINAL_IMP1(_self, selector, void, oldViewDidDisappearIMP, animated);
  84. }
  85. [[FPRScreenTraceTracker sharedInstance] viewControllerDidDisappear:_self];
  86. }];
  87. }
  88. - (void)registerInstrumentors {
  89. dispatch_sync(GetInstrumentationQueue(), ^{
  90. FPRClassInstrumentor *instrumentor =
  91. [[FPRClassInstrumentor alloc] initWithClass:[UIViewController class]];
  92. if (![self registerClassInstrumentor:instrumentor]) {
  93. FPRAssert(NO, @"UIViewController should only be instrumented once.");
  94. }
  95. InstrumentViewDidAppear(self, instrumentor);
  96. InstrumentViewDidDisappear(self, instrumentor);
  97. [instrumentor swizzle];
  98. });
  99. }
  100. @end