FPRObjectInstrumentor.m 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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/FPRObjectInstrumentor.h"
  15. #import "FirebasePerformance/Sources/Common/FPRDiagnostics.h"
  16. #import "FirebasePerformance/Sources/ISASwizzler/FPRObjectSwizzler.h"
  17. #import "FirebasePerformance/Sources/Instrumentation/FPRInstrument_Private.h"
  18. #import "FirebasePerformance/Sources/Instrumentation/FPRSelectorInstrumentor.h"
  19. @interface FPRObjectInstrumentor () {
  20. // The object swizzler instance this instrumentor will use.
  21. FPRObjectSwizzler *_objectSwizzler;
  22. }
  23. @end
  24. @implementation FPRObjectInstrumentor
  25. - (instancetype)init {
  26. FPRAssert(NO, @"%@: Please use the designated initializer.", NSStringFromClass([self class]));
  27. return nil;
  28. }
  29. - (instancetype)initWithObject:(id)object {
  30. self = [super init];
  31. if (self) {
  32. _objectSwizzler = [[FPRObjectSwizzler alloc] initWithObject:object];
  33. _instrumentedObject = object;
  34. }
  35. return self;
  36. }
  37. - (void)copySelector:(SEL)selector fromClass:(Class)aClass isClassSelector:(BOOL)isClassSelector {
  38. __strong id instrumentedObject = _instrumentedObject;
  39. if (instrumentedObject && ![instrumentedObject respondsToSelector:selector]) {
  40. _hasModifications = YES;
  41. [_objectSwizzler copySelector:selector fromClass:aClass isClassSelector:isClassSelector];
  42. }
  43. }
  44. - (void)swizzle {
  45. if (_hasModifications) {
  46. [_objectSwizzler swizzle];
  47. }
  48. }
  49. @end