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/FPRInstrumentation.h"
|
|
|
16 |
|
|
|
17 |
#import "FirebasePerformance/Sources/Common/FPRDiagnostics.h"
|
|
|
18 |
#import "FirebasePerformance/Sources/Instrumentation/FPRInstrument.h"
|
|
|
19 |
#import "FirebasePerformance/Sources/Instrumentation/Network/FPRNSURLConnectionInstrument.h"
|
|
|
20 |
#import "FirebasePerformance/Sources/Instrumentation/Network/FPRNSURLSessionInstrument.h"
|
|
|
21 |
#import "FirebasePerformance/Sources/Instrumentation/UIKit/FPRUIViewControllerInstrument.h"
|
|
|
22 |
|
|
|
23 |
#import "FirebasePerformance/Sources/Configurations/FPRConfigurations.h"
|
|
|
24 |
|
|
|
25 |
// The instrumentation group keys.
|
|
|
26 |
NSString *const kFPRInstrumentationGroupNetworkKey = @"network";
|
|
|
27 |
NSString *const kFPRInstrumentationGroupUIKitKey = @"uikit";
|
|
|
28 |
|
|
|
29 |
/** Use ivars instead of properties to reduce message sending overhead. */
|
|
|
30 |
@interface FPRInstrumentation () {
|
|
|
31 |
// A dictionary of the instrument groups.
|
|
|
32 |
NSDictionary<NSString *, NSMutableArray *> *_instrumentGroups;
|
|
|
33 |
}
|
|
|
34 |
|
|
|
35 |
/** Registers an instrument in the given group.
|
|
|
36 |
*
|
|
|
37 |
* @param instrument The instrument to register.
|
|
|
38 |
* @param group The group to register the instrument in.
|
|
|
39 |
*/
|
|
|
40 |
- (void)registerInstrument:(FPRInstrument *)instrument group:(NSString *)group;
|
|
|
41 |
|
|
|
42 |
@end
|
|
|
43 |
|
|
|
44 |
@implementation FPRInstrumentation
|
|
|
45 |
|
|
|
46 |
- (instancetype)init {
|
|
|
47 |
self = [super init];
|
|
|
48 |
if (self) {
|
|
|
49 |
_instrumentGroups = @{
|
|
|
50 |
kFPRInstrumentationGroupNetworkKey : [[NSMutableArray alloc] init],
|
|
|
51 |
kFPRInstrumentationGroupUIKitKey : [[NSMutableArray alloc] init]
|
|
|
52 |
};
|
|
|
53 |
}
|
|
|
54 |
return self;
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
- (void)registerInstrument:(FPRInstrument *)instrument group:(NSString *)group {
|
|
|
58 |
FPRAssert(instrument, @"Instrument must be non-nil.");
|
|
|
59 |
FPRAssert(_instrumentGroups[group], @"groups and group must be non-nil, and groups[group] must be"
|
|
|
60 |
"non-nil.");
|
|
|
61 |
if (instrument != nil) {
|
|
|
62 |
[_instrumentGroups[group] addObject:instrument];
|
|
|
63 |
}
|
|
|
64 |
[instrument registerInstrumentors];
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
- (NSUInteger)registerInstrumentGroup:(NSString *)group {
|
|
|
68 |
FPRAssert(_instrumentGroups[group], @"The group key does not exist", group);
|
|
|
69 |
FPRAssert(_instrumentGroups[group].count == 0, @"This group is already instrumented");
|
|
|
70 |
|
|
|
71 |
if ([group isEqualToString:kFPRInstrumentationGroupNetworkKey]) {
|
|
|
72 |
[self registerInstrument:[[FPRNSURLSessionInstrument alloc] init] group:group];
|
|
|
73 |
[self registerInstrument:[[FPRNSURLConnectionInstrument alloc] init] group:group];
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
if ([group isEqualToString:kFPRInstrumentationGroupUIKitKey]) {
|
|
|
77 |
[self registerInstrument:[[FPRUIViewControllerInstrument alloc] init] group:group];
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
return _instrumentGroups[group].count;
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
- (BOOL)deregisterInstrumentGroup:(NSString *)group {
|
|
|
84 |
FPRAssert(_instrumentGroups[group], @"You're attempting to deregister an invalid group key.");
|
|
|
85 |
for (FPRInstrument *instrument in _instrumentGroups[group]) {
|
|
|
86 |
[instrument deregisterInstrumentors];
|
|
|
87 |
}
|
|
|
88 |
[_instrumentGroups[group] removeAllObjects];
|
|
|
89 |
return _instrumentGroups[group].count == 0;
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
@end
|