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