FPRUIViewControllerInstrument.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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/UIKit/FPRUIViewControllerInstrument.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 <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() {
  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. static UIApplication *FPRSharedApplication() {
  38. if ([GULAppEnvironmentUtil isAppExtension]) {
  39. return nil;
  40. }
  41. return [UIApplication sharedApplication];
  42. }
  43. @implementation FPRUIViewControllerInstrument
  44. /** Wraps -viewDidAppear:
  45. *
  46. * @param instrument The FPRUIViewController instance.
  47. * @param instrumentor The FPRClassInstrumentor to add the selector instrumentor to.
  48. */
  49. FOUNDATION_STATIC_INLINE
  50. void InstrumentViewDidAppear(FPRUIViewControllerInstrument *instrument,
  51. FPRClassInstrumentor *instrumentor) {
  52. SEL selector = @selector(viewDidAppear:);
  53. FPRSelectorInstrumentor *selectorInstrumentor = SelectorInstrumentor(selector, instrumentor, NO);
  54. IMP oldViewDidAppearIMP = [selectorInstrumentor currentIMP];
  55. [selectorInstrumentor setReplacingBlock:^void(id _self, BOOL animated) {
  56. if (oldViewDidAppearIMP) {
  57. GUL_INVOKE_ORIGINAL_IMP1(_self, selector, void, oldViewDidAppearIMP, animated);
  58. }
  59. // This has to be called on the main thread and so it's done here instead of in
  60. // FPRScreenTraceTracker.
  61. if ([((UIViewController *)_self).view isDescendantOfView:FPRSharedApplication().keyWindow]) {
  62. [[FPRScreenTraceTracker sharedInstance] viewControllerDidAppear:_self];
  63. }
  64. }];
  65. }
  66. /** Wraps -viewDidDisappear:
  67. *
  68. * @param instrument The FPRUIViewController instance.
  69. * @param instrumentor The FPRClassInstrumentor to add the selector instrumentor to.
  70. */
  71. FOUNDATION_STATIC_INLINE
  72. void InstrumentViewDidDisappear(FPRUIViewControllerInstrument *instrument,
  73. FPRClassInstrumentor *instrumentor) {
  74. SEL selector = @selector(viewDidDisappear:);
  75. FPRSelectorInstrumentor *selectorInstrumentor = SelectorInstrumentor(selector, instrumentor, NO);
  76. IMP oldViewDidDisappearIMP = [selectorInstrumentor currentIMP];
  77. [selectorInstrumentor setReplacingBlock:^void(id _self, BOOL animated) {
  78. if (oldViewDidDisappearIMP) {
  79. GUL_INVOKE_ORIGINAL_IMP1(_self, selector, void, oldViewDidDisappearIMP, animated);
  80. }
  81. [[FPRScreenTraceTracker sharedInstance] viewControllerDidDisappear:_self];
  82. }];
  83. }
  84. - (void)registerInstrumentors {
  85. dispatch_sync(GetInstrumentationQueue(), ^{
  86. FPRClassInstrumentor *instrumentor =
  87. [[FPRClassInstrumentor alloc] initWithClass:[UIViewController class]];
  88. if (![self registerClassInstrumentor:instrumentor]) {
  89. FPRAssert(NO, @"UIViewController should only be instrumented once.");
  90. }
  91. InstrumentViewDidAppear(self, instrumentor);
  92. InstrumentViewDidDisappear(self, instrumentor);
  93. [instrumentor swizzle];
  94. });
  95. }
  96. @end