FPRClassInstrumentor.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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/FPRClassInstrumentor.h"
  15. #import "FirebasePerformance/Sources/Instrumentation/FPRClassInstrumentor_Private.h"
  16. #import "FirebasePerformance/Sources/Common/FPRDiagnostics.h"
  17. #import "FirebasePerformance/Sources/Instrumentation/FPRSelectorInstrumentor.h"
  18. /** Use ivars instead of properties to reduce message sending overhead. */
  19. @interface FPRClassInstrumentor () {
  20. // The selector instrumentors associated with this class.
  21. NSMutableSet<FPRSelectorInstrumentor *> *_selectorInstrumentors;
  22. }
  23. @end
  24. @implementation FPRClassInstrumentor
  25. #pragma mark - Public methods
  26. - (instancetype)init {
  27. FPRAssert(NO, @"%@: please use the designated initializer.", NSStringFromClass([self class]));
  28. return nil;
  29. }
  30. - (instancetype)initWithClass:(Class)aClass {
  31. self = [super init];
  32. if (self) {
  33. FPRAssert(aClass, @"You must supply a class in order to instrument its methods");
  34. _instrumentedClass = aClass;
  35. _selectorInstrumentors = [[NSMutableSet<FPRSelectorInstrumentor *> alloc] init];
  36. }
  37. return self;
  38. }
  39. - (nullable FPRSelectorInstrumentor *)instrumentorForClassSelector:(SEL)selector {
  40. return [self buildAndAddSelectorInstrumentorForSelector:selector isClassSelector:YES];
  41. }
  42. - (nullable FPRSelectorInstrumentor *)instrumentorForInstanceSelector:(SEL)selector {
  43. return [self buildAndAddSelectorInstrumentorForSelector:selector isClassSelector:NO];
  44. }
  45. - (void)swizzle {
  46. for (FPRSelectorInstrumentor *selectorInstrumentor in _selectorInstrumentors) {
  47. [selectorInstrumentor swizzle];
  48. }
  49. }
  50. - (BOOL)unswizzle {
  51. for (FPRSelectorInstrumentor *selectorInstrumentor in _selectorInstrumentors) {
  52. [selectorInstrumentor unswizzle];
  53. }
  54. [_selectorInstrumentors removeAllObjects];
  55. return _selectorInstrumentors.count == 0;
  56. }
  57. #pragma mark - Private methods
  58. /** Creates and adds a selector instrumentor to this class instrumentor.
  59. *
  60. * @param selector The selector to build and add to this class instrumentor;
  61. * @param isClassSelector If YES, then the selector is a class selector.
  62. * @return An FPRSelectorInstrumentor if the class/selector combination exists, nil otherwise.
  63. */
  64. - (nullable FPRSelectorInstrumentor *)buildAndAddSelectorInstrumentorForSelector:(SEL)selector
  65. isClassSelector:
  66. (BOOL)isClassSelector {
  67. FPRSelectorInstrumentor *selectorInstrumentor =
  68. [[FPRSelectorInstrumentor alloc] initWithSelector:selector
  69. class:_instrumentedClass
  70. isClassSelector:isClassSelector];
  71. if (selectorInstrumentor) {
  72. [self addSelectorInstrumentor:selectorInstrumentor];
  73. }
  74. return selectorInstrumentor;
  75. }
  76. /** Adds a selector instrumentors to an existing running list of instrumented selectors.
  77. *
  78. * @param selectorInstrumentor A non-nil selector instrumentor, whose SEL objects will be swizzled.
  79. */
  80. - (void)addSelectorInstrumentor:(nonnull FPRSelectorInstrumentor *)selectorInstrumentor {
  81. if ([_selectorInstrumentors containsObject:selectorInstrumentor]) {
  82. FPRAssert(NO, @"You cannot instrument the same selector (%@) twice",
  83. NSStringFromSelector(selectorInstrumentor.selector));
  84. }
  85. [_selectorInstrumentors addObject:selectorInstrumentor];
  86. }
  87. @end