Skip to content

Commit 9d7cbdb

Browse files
committed
ref: Dependency container in Swift
1 parent 3319d58 commit 9d7cbdb

File tree

61 files changed

+700
-938
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+700
-938
lines changed

Sentry.xcodeproj/project.pbxproj

Lines changed: 38 additions & 26 deletions
Large diffs are not rendered by default.

Sources/Sentry/Processors/SentryWatchdogTerminationBreadcrumbProcessor.m

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@ @interface SentryWatchdogTerminationBreadcrumbProcessor ()
1818
@implementation SentryWatchdogTerminationBreadcrumbProcessor
1919

2020
- (instancetype)initWithMaxBreadcrumbs:(NSInteger)maxBreadcrumbs
21-
fileManager:(SentryFileManager *_Nullable)fileManager
21+
{
22+
return [self initWithMaxBreadcrumbs:maxBreadcrumbs
23+
fileManager:SentryDependencyContainer.sharedInstance.fileManager];
24+
}
25+
26+
- (instancetype)initWithMaxBreadcrumbs:(NSInteger)maxBreadcrumbs
27+
fileManager:(nullable SentryFileManager *)fileManager
2228
{
2329
if (self = [super init]) {
2430
self.fileManager = fileManager;

Sources/Sentry/Profiling/SentryContinuousProfiler.mm

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#if SENTRY_TARGET_PROFILING_SUPPORTED
44

55
# import "SentryDependencyContainer.h"
6+
# import "SentryDependencyContainerSwiftHelper.h"
67
# import "SentryInternalDefines.h"
78
# import "SentryLogC.h"
89
# import "SentryMetricProfiler.h"
@@ -83,7 +84,7 @@
8384

8485
// Move the serialization work to a background queue to avoid potentially
8586
// blocking the main thread. The serialization can take several milliseconds.
86-
sentry_dispatchAsync(SentryDependencyContainer.sharedInstance.dispatchQueueWrapper, ^{
87+
sentry_dispatchAsync(SentryDependencyContainerSwiftHelper.dispatchQueueWrapper, ^{
8788
NSDictionary *_Nonnull serializedMetrics
8889
= serializeContinuousProfileMetrics(metricProfilerState);
8990
SentryEnvelope *_Nullable envelope
@@ -199,7 +200,7 @@ + (nullable SentryId *)currentProfilerID
199200
+ (void)scheduleTimer
200201
{
201202
sentry_dispatchAsyncOnMainIfNotMainThread(
202-
SentryDependencyContainer.sharedInstance.dispatchQueueWrapper, ^{
203+
SentryDependencyContainerSwiftHelper.dispatchQueueWrapper, ^{
203204
std::lock_guard<std::mutex> l(_threadUnsafe_gContinuousProfilerLock);
204205
if (_chunkTimer != nil) {
205206
SENTRY_LOG_WARN(

Sources/Sentry/Profiling/SentryProfilerState.mm

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# import "SentryAsyncSafeLog.h"
44
# import "SentryBacktrace.hpp"
55
# import "SentryDependencyContainer.h"
6+
# import "SentryDependencyContainerSwiftHelper.h"
67
# import "SentryFormatter.h"
78
# import "SentryInternalDefines.h"
89
# import "SentryProfileTimeseries.h"
@@ -73,7 +74,7 @@ - (instancetype)init
7374
_mutableState = [[SentryProfilerMutableState alloc] init];
7475
_mainThreadID = 0;
7576
sentry_dispatchAsyncOnMainIfNotMainThread(
76-
SentryDependencyContainer.sharedInstance.dispatchQueueWrapper,
77+
SentryDependencyContainerSwiftHelper.dispatchQueueWrapper,
7778
^{ [self cacheMainThreadID]; });
7879
}
7980
return self;

Sources/Sentry/Profiling/SentryTraceProfiler.mm

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
# import "SentryDependencyContainer.h"
66

7+
# import "SentryDependencyContainerSwiftHelper.h"
78
# import "SentryInternalDefines.h"
89
# import "SentryLogC.h"
910
# import "SentryMetricProfiler.h"
@@ -85,7 +86,7 @@ + (void)recordMetrics
8586
+ (void)scheduleTimeoutTimer
8687
{
8788
sentry_dispatchAsyncOnMainIfNotMainThread(
88-
SentryDependencyContainer.sharedInstance.dispatchQueueWrapper, ^{
89+
SentryDependencyContainerSwiftHelper.dispatchQueueWrapper, ^{
8990
std::lock_guard<std::mutex> l(_threadUnsafe_gTraceProfilerLock);
9091
if (_sentry_threadUnsafe_traceProfileTimeoutTimer != nil) {
9192
return;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#import <Foundation/Foundation.h>
2+
3+
@interface SentryANRStoppedResultInternal : NSObject
4+
5+
@property (nonatomic, readonly) NSTimeInterval minDuration;
6+
7+
@property (nonatomic, readonly) NSTimeInterval maxDuration;
8+
9+
- (instancetype)initWithMinDuration:(NSTimeInterval)minDuration
10+
maxDuration:(NSTimeInterval)maxDuration;
11+
12+
@end
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#import "SentryANRStoppedResultInternal.h"
2+
3+
@implementation SentryANRStoppedResultInternal
4+
5+
- (instancetype)initWithMinDuration:(NSTimeInterval)minDuration
6+
maxDuration:(NSTimeInterval)maxDuration
7+
{
8+
if (self = [super init]) {
9+
_minDuration = minDuration;
10+
_maxDuration = maxDuration;
11+
return self;
12+
}
13+
return nil;
14+
}
15+
16+
@end
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#import <Foundation/Foundation.h>
2+
3+
@class SentryANRStoppedResultInternal;
4+
5+
typedef NS_ENUM(NSInteger, SentryANRTypeInternal) {
6+
kSentryANRTypeFatalFullyBlocking,
7+
kSentryANRTypeFatalNonFullyBlocking,
8+
kSentryANRTypeFullyBlocking,
9+
kSentryANRTypeNonFullyBlocking,
10+
kSentryANRTypeUnknown
11+
};
12+
13+
NS_ASSUME_NONNULL_BEGIN
14+
15+
@protocol SentryANRTrackerInternalDelegate <NSObject>
16+
17+
- (void)anrDetected:(SentryANRTypeInternal)type;
18+
19+
- (void)anrStopped:(nullable SentryANRStoppedResultInternal *)result;
20+
21+
@end
22+
23+
NS_ASSUME_NONNULL_END

Sources/Sentry/SentryANRTrackerV1.m

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#import "SentryANRTrackerV1.h"
2+
#import "SentryANRTrackerInternalDelegate.h"
23
#import "SentryDependencyContainer.h"
34
#import "SentryLogC.h"
45
#import "SentrySwift.h"
@@ -13,12 +14,12 @@ typedef NS_ENUM(NSInteger, SentryANRTrackerState) {
1314
kSentryANRTrackerStopping
1415
};
1516

16-
@interface SentryANRTrackerV1 () <SentryANRTracker>
17+
@interface SentryANRTrackerV1 ()
1718

1819
@property (nonatomic, strong) SentryCrashWrapper *crashWrapper;
1920
@property (nonatomic, strong) SentryDispatchQueueWrapper *dispatchQueueWrapper;
2021
@property (nonatomic, strong) SentryThreadWrapper *threadWrapper;
21-
@property (nonatomic, strong) NSHashTable<id<SentryANRTrackerDelegate>> *listeners;
22+
@property (nonatomic, strong) NSHashTable<id<SentryANRTrackerInternalDelegate>> *listeners;
2223
@property (nonatomic, assign) NSTimeInterval timeoutInterval;
2324

2425
@end
@@ -28,28 +29,32 @@ @implementation SentryANRTrackerV1 {
2829
SentryANRTrackerState state;
2930
}
3031

32+
- (instancetype)initWithTimeoutInterval:(NSTimeInterval)timeoutInterval
33+
{
34+
return
35+
[self initWithTimeoutInterval:timeoutInterval
36+
crashWrapper:SentryDependencyContainer.sharedInstance.crashWrapper
37+
dispatchQueueWrapper:SentryDependencyContainer.sharedInstance.dispatchQueueWrapper
38+
threadWrapper:SentryDependencyContainer.sharedInstance.threadWrapper];
39+
}
40+
3141
- (instancetype)initWithTimeoutInterval:(NSTimeInterval)timeoutInterval
3242
crashWrapper:(SentryCrashWrapper *)crashWrapper
3343
dispatchQueueWrapper:(SentryDispatchQueueWrapper *)dispatchQueueWrapper
3444
threadWrapper:(SentryThreadWrapper *)threadWrapper
3545
{
3646
if (self = [super init]) {
3747
self.timeoutInterval = timeoutInterval;
38-
self.crashWrapper = crashWrapper;
39-
self.dispatchQueueWrapper = dispatchQueueWrapper;
40-
self.threadWrapper = threadWrapper;
48+
self.crashWrapper = SentryDependencyContainer.sharedInstance.crashWrapper;
49+
self.dispatchQueueWrapper = SentryDependencyContainer.sharedInstance.dispatchQueueWrapper;
50+
self.threadWrapper = SentryDependencyContainer.sharedInstance.threadWrapper;
4151
self.listeners = [NSHashTable weakObjectsHashTable];
4252
threadLock = [[NSObject alloc] init];
4353
state = kSentryANRTrackerNotRunning;
4454
}
4555
return self;
4656
}
4757

48-
- (id<SentryANRTracker>)asProtocol
49-
{
50-
return self;
51-
}
52-
5358
- (void)detectANRs
5459
{
5560
NSUUID *threadID = [NSUUID UUID];
@@ -149,8 +154,8 @@ - (void)ANRDetected
149154
localListeners = [self.listeners allObjects];
150155
}
151156

152-
for (id<SentryANRTrackerDelegate> target in localListeners) {
153-
[target anrDetectedWithType:SentryANRTypeUnknown];
157+
for (id<SentryANRTrackerInternalDelegate> target in localListeners) {
158+
[target anrDetected:kSentryANRTypeUnknown];
154159
}
155160
}
156161

@@ -161,14 +166,14 @@ - (void)ANRStopped
161166
targets = [self.listeners allObjects];
162167
}
163168

164-
for (id<SentryANRTrackerDelegate> target in targets) {
169+
for (id<SentryANRTrackerInternalDelegate> target in targets) {
165170
// We intentionally don't measure the ANR duration, because V2 will replace V1, so it's not
166171
// worth the effort.
167-
[target anrStoppedWithResult:nil];
172+
[target anrStopped:nil];
168173
}
169174
}
170175

171-
- (void)addListener:(id<SentryANRTrackerDelegate>)listener
176+
- (void)addListener:(id<SentryANRTrackerInternalDelegate>)listener
172177
{
173178
@synchronized(self.listeners) {
174179
[self.listeners addObject:listener];
@@ -186,7 +191,7 @@ - (void)addListener:(id<SentryANRTrackerDelegate>)listener
186191
}
187192
}
188193

189-
- (void)removeListener:(id<SentryANRTrackerDelegate>)listener
194+
- (void)removeListener:(id<SentryANRTrackerInternalDelegate>)listener
190195
{
191196
@synchronized(self.listeners) {
192197
[self.listeners removeObject:listener];

Sources/Sentry/SentryANRTrackerV2.m

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#if SENTRY_HAS_UIKIT
44

5+
# import "SentryANRStoppedResultInternal.h"
6+
# import "SentryANRTrackerInternalDelegate.h"
57
# import "SentryDependencyContainer.h"
68
# import "SentryLogC.h"
79
# import "SentrySwift.h"
@@ -17,12 +19,12 @@ typedef NS_ENUM(NSInteger, SentryANRTrackerState) {
1719
kSentryANRTrackerStopping
1820
};
1921

20-
@interface SentryANRTrackerV2 () <SentryANRTracker>
22+
@interface SentryANRTrackerV2 ()
2123

2224
@property (nonatomic, strong) SentryCrashWrapper *crashWrapper;
2325
@property (nonatomic, strong) SentryDispatchQueueWrapper *dispatchQueueWrapper;
2426
@property (nonatomic, strong) SentryThreadWrapper *threadWrapper;
25-
@property (nonatomic, strong) NSHashTable<id<SentryANRTrackerDelegate>> *listeners;
27+
@property (nonatomic, strong) NSHashTable<id<SentryANRTrackerInternalDelegate>> *listeners;
2628
@property (nonatomic, strong) SentryFramesTracker *framesTracker;
2729
@property (nonatomic, assign) NSTimeInterval timeoutInterval;
2830

@@ -33,6 +35,16 @@ @implementation SentryANRTrackerV2 {
3335
SentryANRTrackerState state;
3436
}
3537

38+
- (instancetype)initWithTimeoutInterval:(NSTimeInterval)timeoutInterval
39+
{
40+
return
41+
[self initWithTimeoutInterval:timeoutInterval
42+
crashWrapper:SentryDependencyContainer.sharedInstance.crashWrapper
43+
dispatchQueueWrapper:SentryDependencyContainer.sharedInstance.dispatchQueueWrapper
44+
threadWrapper:SentryDependencyContainer.sharedInstance.threadWrapper
45+
framesTracker:SentryDependencyContainer.sharedInstance.framesTracker];
46+
}
47+
3648
- (instancetype)initWithTimeoutInterval:(NSTimeInterval)timeoutInterval
3749
crashWrapper:(SentryCrashWrapper *)crashWrapper
3850
dispatchQueueWrapper:(SentryDispatchQueueWrapper *)dispatchQueueWrapper
@@ -52,11 +64,6 @@ - (instancetype)initWithTimeoutInterval:(NSTimeInterval)timeoutInterval
5264
return self;
5365
}
5466

55-
- (id<SentryANRTracker>)asProtocol
56-
{
57-
return self;
58-
}
59-
6067
- (void)detectANRs
6168
{
6269
NSUUID *threadID = [NSUUID UUID];
@@ -125,9 +132,9 @@ - (void)detectANRs
125132

126133
uint64_t framesDelayStartSystemTime = nowSystemTime - appHangStoppedInterval;
127134

128-
SentryFramesDelayResult *framesDelay =
129-
[self.framesTracker getFramesDelay:framesDelayStartSystemTime
130-
endSystemTimestamp:nowSystemTime];
135+
SentryFramesDelayResultSPI *framesDelay =
136+
[self.framesTracker getFramesDelaySPI:framesDelayStartSystemTime
137+
endSystemTimestamp:nowSystemTime];
131138

132139
if (framesDelay.delayDuration == -1) {
133140
continue;
@@ -171,9 +178,9 @@ - (void)detectANRs
171178

172179
uint64_t frameDelayStartSystemTime = nowSystemTime - timeoutIntervalInNanos;
173180

174-
SentryFramesDelayResult *framesDelayForTimeInterval =
175-
[self.framesTracker getFramesDelay:frameDelayStartSystemTime
176-
endSystemTimestamp:nowSystemTime];
181+
SentryFramesDelayResultSPI *framesDelayForTimeInterval =
182+
[self.framesTracker getFramesDelaySPI:frameDelayStartSystemTime
183+
endSystemTimestamp:nowSystemTime];
177184

178185
if (framesDelayForTimeInterval.delayDuration == -1) {
179186
continue;
@@ -189,7 +196,7 @@ - (void)detectANRs
189196

190197
reported = YES;
191198
lastAppHangStartedSystemTime = dateProvider.systemTime;
192-
[self ANRDetected:SentryANRTypeFullyBlocking];
199+
[self ANRDetected:kSentryANRTypeFullyBlocking];
193200
}
194201

195202
NSTimeInterval nonFullyBlockingFramesDelayThreshold = self.timeoutInterval * 0.99;
@@ -200,7 +207,7 @@ - (void)detectANRs
200207

201208
reported = YES;
202209
lastAppHangStartedSystemTime = dateProvider.systemTime;
203-
[self ANRDetected:SentryANRTypeNonFullyBlocking];
210+
[self ANRDetected:kSentryANRTypeNonFullyBlocking];
204211
}
205212
}
206213

@@ -210,15 +217,15 @@ - (void)detectANRs
210217
}
211218
}
212219

213-
- (void)ANRDetected:(enum SentryANRType)type
220+
- (void)ANRDetected:(SentryANRTypeInternal)type
214221
{
215222
NSArray *localListeners;
216223
@synchronized(self.listeners) {
217224
localListeners = [self.listeners allObjects];
218225
}
219226

220-
for (id<SentryANRTrackerDelegate> target in localListeners) {
221-
[target anrDetectedWithType:type];
227+
for (id<SentryANRTrackerInternalDelegate> target in localListeners) {
228+
[target anrDetected:type];
222229
}
223230
}
224231

@@ -229,15 +236,15 @@ - (void)ANRStopped:(NSTimeInterval)hangDurationMinimum to:(NSTimeInterval)hangDu
229236
targets = [self.listeners allObjects];
230237
}
231238

232-
SentryANRStoppedResult *result =
233-
[[SentryANRStoppedResult alloc] initWithMinDuration:hangDurationMinimum
234-
maxDuration:hangDurationMaximum];
235-
for (id<SentryANRTrackerDelegate> target in targets) {
236-
[target anrStoppedWithResult:result];
239+
SentryANRStoppedResultInternal *result =
240+
[[SentryANRStoppedResultInternal alloc] initWithMinDuration:hangDurationMinimum
241+
maxDuration:hangDurationMaximum];
242+
for (id<SentryANRTrackerInternalDelegate> target in targets) {
243+
[target anrStopped:result];
237244
}
238245
}
239246

240-
- (void)addListener:(id<SentryANRTrackerDelegate>)listener
247+
- (void)addListener:(id<SentryANRTrackerInternalDelegate>)listener
241248
{
242249
@synchronized(self.listeners) {
243250
[self.listeners addObject:listener];
@@ -255,7 +262,7 @@ - (void)addListener:(id<SentryANRTrackerDelegate>)listener
255262
}
256263
}
257264

258-
- (void)removeListener:(id<SentryANRTrackerDelegate>)listener
265+
- (void)removeListener:(id<SentryANRTrackerInternalDelegate>)listener
259266
{
260267
@synchronized(self.listeners) {
261268
[self.listeners removeObject:listener];

0 commit comments

Comments
 (0)