-
-
Notifications
You must be signed in to change notification settings - Fork 316
/
SentryAppStateManager.m
204 lines (170 loc) · 6.82 KB
/
SentryAppStateManager.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#import "SentryCrashSysCtl.h"
#import "SentryDependencyContainer.h"
#import "SentrySysctl.h"
#import <Foundation/Foundation.h>
#import <SentryAppState.h>
#import <SentryAppStateManager.h>
#import <SentryCrashWrapper.h>
#import <SentryDispatchQueueWrapper.h>
#import <SentryFileManager.h>
#import <SentryNSNotificationCenterWrapper.h>
#import <SentryOptions.h>
#import <SentrySwift.h>
#if SENTRY_HAS_UIKIT
# import <SentryInternalNotificationNames.h>
# import <SentryNSNotificationCenterWrapper.h>
# import <UIKit/UIKit.h>
#endif
@interface SentryAppStateManager ()
@property (nonatomic, strong) SentryOptions *options;
@property (nonatomic, strong) SentryCrashWrapper *crashWrapper;
@property (nonatomic, strong) SentryFileManager *fileManager;
@property (nonatomic, strong) SentryDispatchQueueWrapper *dispatchQueue;
@property (nonatomic, strong) SentryNSNotificationCenterWrapper *notificationCenterWrapper;
@property (nonatomic) NSInteger startCount;
@end
@implementation SentryAppStateManager
- (instancetype)initWithOptions:(SentryOptions *)options
crashWrapper:(SentryCrashWrapper *)crashWrapper
fileManager:(SentryFileManager *)fileManager
dispatchQueueWrapper:(SentryDispatchQueueWrapper *)dispatchQueueWrapper
notificationCenterWrapper:(SentryNSNotificationCenterWrapper *)notificationCenterWrapper
{
if (self = [super init]) {
self.options = options;
self.crashWrapper = crashWrapper;
self.fileManager = fileManager;
self.dispatchQueue = dispatchQueueWrapper;
self.notificationCenterWrapper = notificationCenterWrapper;
self.startCount = 0;
}
return self;
}
#if SENTRY_HAS_UIKIT
- (void)start
{
if (self.startCount == 0) {
[self.notificationCenterWrapper
addObserver:self
selector:@selector(didBecomeActive)
name:SentryNSNotificationCenterWrapper.didBecomeActiveNotificationName];
[self.notificationCenterWrapper addObserver:self
selector:@selector(didBecomeActive)
name:SentryHybridSdkDidBecomeActiveNotificationName];
[self.notificationCenterWrapper
addObserver:self
selector:@selector(willResignActive)
name:SentryNSNotificationCenterWrapper.willResignActiveNotificationName];
[self.notificationCenterWrapper
addObserver:self
selector:@selector(willTerminate)
name:SentryNSNotificationCenterWrapper.willTerminateNotificationName];
[self storeCurrentAppState];
}
self.startCount += 1;
}
- (void)stop
{
[self stopWithForce:NO];
}
// forceStop is YES when the SDK gets closed
- (void)stopWithForce:(BOOL)forceStop
{
if (self.startCount <= 0) {
return;
}
if (forceStop) {
[self
updateAppStateInBackground:^(SentryAppState *appState) { appState.isSDKRunning = NO; }];
self.startCount = 0;
} else {
self.startCount -= 1;
}
if (self.startCount == 0) {
// Remove the observers with the most specific detail possible, see
// https://developer.apple.com/documentation/foundation/nsnotificationcenter/1413994-removeobserver
[self.notificationCenterWrapper
removeObserver:self
name:SentryNSNotificationCenterWrapper.didBecomeActiveNotificationName];
[self.notificationCenterWrapper
removeObserver:self
name:SentryHybridSdkDidBecomeActiveNotificationName];
[self.notificationCenterWrapper
removeObserver:self
name:SentryNSNotificationCenterWrapper.willResignActiveNotificationName];
[self.notificationCenterWrapper
removeObserver:self
name:SentryNSNotificationCenterWrapper.willTerminateNotificationName];
}
}
- (void)dealloc
{
// In dealloc it's safe to unsubscribe for all, see
// https://developer.apple.com/documentation/foundation/nsnotificationcenter/1413994-removeobserver
[self.notificationCenterWrapper removeObserver:self];
}
/**
* It is called when an app is receiving events / it is in the foreground and when we receive a
* @c SentryHybridSdkDidBecomeActiveNotification.
* @discussion This also works when using SwiftUI or Scenes, as UIKit posts a
* @c didBecomeActiveNotification regardless of whether your app uses scenes, see
* https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1622956-applicationdidbecomeactive.
*/
- (void)didBecomeActive
{
[self updateAppStateInBackground:^(SentryAppState *appState) { appState.isActive = YES; }];
}
/**
* The app is about to lose focus / going to the background. This is only called when an app was
* receiving events / was is in the foreground.
*/
- (void)willResignActive
{
[self updateAppStateInBackground:^(SentryAppState *appState) { appState.isActive = NO; }];
}
- (void)willTerminate
{
// The app is terminating so it is fine to do this on the main thread.
// Furthermore, so users can manually post UIApplicationWillTerminateNotification and then call
// exit(0), to avoid getting false OOM when using exit(0), see GH-1252.
[self updateAppState:^(SentryAppState *appState) { appState.wasTerminated = YES; }];
}
- (void)updateAppStateInBackground:(void (^)(SentryAppState *))block
{
// We accept the tradeoff that the app state might not be 100% up to date over blocking the main
// thread.
[self.dispatchQueue dispatchAsyncWithBlock:^{ [self updateAppState:block]; }];
}
- (void)updateAppState:(void (^)(SentryAppState *))block
{
@synchronized(self) {
SentryAppState *appState = [self.fileManager readAppState];
if (appState != nil) {
block(appState);
[self.fileManager storeAppState:appState];
}
}
}
- (SentryAppState *)buildCurrentAppState
{
// Is the current process being traced or not? If it is a debugger is attached.
bool isDebugging = self.crashWrapper.isBeingTraced;
UIDevice *device = [UIDevice currentDevice];
NSString *vendorId = [device.identifierForVendor UUIDString];
return [[SentryAppState alloc] initWithReleaseName:self.options.releaseName
osVersion:device.systemVersion
vendorId:vendorId
isDebugging:isDebugging
systemBootTimestamp:SentryDependencyContainer.sharedInstance
.sysctlWrapper.systemBootTimestamp];
}
- (SentryAppState *)loadPreviousAppState
{
return [self.fileManager readPreviousAppState];
}
- (void)storeCurrentAppState
{
[self.fileManager storeAppState:[self buildCurrentAppState]];
}
#endif
@end