FPRSelectorInstrumentor.m 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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/FPRSelectorInstrumentor.h"
  15. #import "FirebasePerformance/Sources/Common/FPRDiagnostics.h"
  16. #import <GoogleUtilities/GULSwizzler.h>
  17. #ifdef UNSWIZZLE_AVAILABLE
  18. #import <GoogleUtilities/GULSwizzler+Unswizzle.h>
  19. #endif
  20. @implementation FPRSelectorInstrumentor {
  21. // The class this instrumentor operates on.
  22. Class _class;
  23. // The selector this instrumentor operates on.
  24. SEL _selector;
  25. // YES indicates the selector swizzled is a class selector, as opposed to an instance selector.
  26. BOOL _isClassSelector;
  27. // YES indicates that this selector instrumentor has been swizzled.
  28. BOOL _swizzled;
  29. // A block to replace the original implementation. Can't be used with before/after blocks.
  30. id _replacingBlock;
  31. }
  32. - (instancetype)init {
  33. FPRAssert(NO, @"%@: Please use the designated initializer", NSStringFromClass([self class]));
  34. return nil;
  35. }
  36. - (instancetype)initWithSelector:(SEL)selector
  37. class:(Class)aClass
  38. isClassSelector:(BOOL)isClassSelector {
  39. if (![GULSwizzler selector:selector existsInClass:aClass isClassSelector:isClassSelector]) {
  40. return nil;
  41. }
  42. self = [super init];
  43. if (self) {
  44. _selector = selector;
  45. _class = aClass;
  46. _isClassSelector = isClassSelector;
  47. FPRAssert(_selector, @"A selector to swizzle must be provided.");
  48. FPRAssert(_class, @"You can't swizzle a class that doesn't exist");
  49. }
  50. return self;
  51. }
  52. - (void)setReplacingBlock:(id)block {
  53. _replacingBlock = [block copy];
  54. }
  55. - (void)swizzle {
  56. _swizzled = YES;
  57. FPRAssert(_replacingBlock, @"A replacingBlock needs to be set.");
  58. [GULSwizzler swizzleClass:_class
  59. selector:_selector
  60. isClassSelector:_isClassSelector
  61. withBlock:_replacingBlock];
  62. }
  63. - (void)unswizzle {
  64. _swizzled = NO;
  65. #ifdef UNSWIZZLE_AVAILABLE
  66. [GULSwizzler unswizzleClass:_class selector:_selector isClassSelector:_isClassSelector];
  67. #else
  68. NSAssert(NO, @"Unswizzling is disabled.");
  69. #endif
  70. }
  71. - (IMP)currentIMP {
  72. return [GULSwizzler currentImplementationForClass:_class
  73. selector:_selector
  74. isClassSelector:_isClassSelector];
  75. }
  76. - (BOOL)isEqual:(id)object {
  77. if ([object isKindOfClass:[FPRSelectorInstrumentor class]]) {
  78. FPRSelectorInstrumentor *otherObject = object;
  79. return otherObject->_class == _class && otherObject->_selector == _selector &&
  80. otherObject->_isClassSelector == _isClassSelector && otherObject->_swizzled == _swizzled;
  81. }
  82. return NO;
  83. }
  84. - (NSUInteger)hash {
  85. return [[NSString stringWithFormat:@"%@%@%d%d", NSStringFromClass(_class),
  86. NSStringFromSelector(_selector), _isClassSelector, _swizzled]
  87. hash];
  88. }
  89. @end