FPRInstrument.m 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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/FPRInstrument.h"
  15. #import "FirebasePerformance/Sources/Instrumentation/FPRInstrument_Private.h"
  16. #import "FirebasePerformance/Sources/Common/FPRDiagnostics.h"
  17. #import "FirebasePerformance/Sources/Instrumentation/FPRClassInstrumentor.h"
  18. #import "FirebasePerformance/Sources/Instrumentation/FPRObjectInstrumentor.h"
  19. @implementation FPRInstrument {
  20. NSMutableArray<FPRClassInstrumentor *> *_classInstrumentors;
  21. NSMutableSet<Class> *_instrumentedClasses;
  22. }
  23. - (instancetype)init {
  24. self = [super init];
  25. if (self) {
  26. _classInstrumentors = [[NSMutableArray<FPRClassInstrumentor *> alloc] init];
  27. _instrumentedClasses = [[NSMutableSet<Class> alloc] init];
  28. }
  29. return self;
  30. }
  31. - (NSMutableArray<FPRClassInstrumentor *> *)classInstrumentors {
  32. return _classInstrumentors;
  33. }
  34. - (NSMutableSet<Class> *)instrumentedClasses {
  35. return _instrumentedClasses;
  36. }
  37. - (void)registerInstrumentors {
  38. FPRAssert(NO, @"registerInstrumentors should be implemented in a concrete subclass.");
  39. }
  40. - (BOOL)isObjectInstrumentable:(id)object {
  41. if ([object isKindOfClass:[NSOperation class]]) {
  42. return NO;
  43. }
  44. return YES;
  45. }
  46. - (BOOL)registerClassInstrumentor:(FPRClassInstrumentor *)instrumentor {
  47. @synchronized(self) {
  48. if ([_instrumentedClasses containsObject:instrumentor.instrumentedClass] ||
  49. [instrumentor.instrumentedClass instancesRespondToSelector:@selector(gul_class)]) {
  50. return NO;
  51. }
  52. [_instrumentedClasses addObject:instrumentor.instrumentedClass];
  53. [_classInstrumentors addObject:instrumentor];
  54. return YES;
  55. }
  56. }
  57. - (void)deregisterInstrumentors {
  58. @synchronized(self) {
  59. for (FPRClassInstrumentor *classInstrumentor in self.classInstrumentors) {
  60. [classInstrumentor unswizzle];
  61. }
  62. [_classInstrumentors removeAllObjects];
  63. [_instrumentedClasses removeAllObjects];
  64. }
  65. }
  66. @end