FPRUIViewControllerInstrument.m 4.6 KB

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