FPRSelectorInstrumentor.m 3.2 KB

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