-
Notifications
You must be signed in to change notification settings - Fork 148
/
Copy pathGYBootingProtection.m
180 lines (153 loc) · 6.78 KB
/
GYBootingProtection.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
//
// GYBootingProtection.m
// GYMonitor
//
// Created by jasenhuang on 15/12/22.
//
#import "GYBootingProtection.h"
#import <QuartzCore/QuartzCore.h>
void (^Logger)(NSString *log);
ReportBlock reportBlock;
RepairBlock repairBlock;
BoolCompletionBlock boolCompletionBlock;
static NSString *const kStartupCrashForTest = @"StartupCrashForTest"; // 尝试制造启动 crash 彩蛋
static NSString *const kContinuousCrashOnLaunchCounterKey = @"ContinuousCrashOnLaunchCounter";
static NSString *const kContinuousCrashFixingKey = @"ContinuousCrashFixing"; // 是否正在修复
static NSInteger const kContinuousCrashOnLaunchNeedToReport = 5;
static NSInteger const kContinuousCrashOnLaunchNeedToFix = 5;
static CFTimeInterval const kCrashOnLaunchTimeIntervalThreshold = 5.0;
static CFTimeInterval g_startTick; // 记录启动时刻
@implementation GYBootingProtection
+ (BOOL)launchContinuousCrashProtect
{
NSAssert(repairBlock, @"repairBlock is nil!");
if (Logger) Logger(@"GYBootingProtection: Launch continuous crash report");
[self setIsFixing:NO];
NSInteger launchCrashes = [self crashCount];
// 上报
if (launchCrashes >= kContinuousCrashOnLaunchNeedToReport) {
if (Logger) Logger([NSString stringWithFormat:@"GYBootingProtection: App has continuously crashed for %@ times. Now synchronize uploading crash report and begin fixing procedure.", @(launchCrashes)]);
if (reportBlock) reportBlock(launchCrashes);
}
[self setCrashCount:[self crashCount]+1];
// 记录启动时刻,用于计算启动连续 crash
g_startTick = CACurrentMediaTime();
// 重置启动 crash 计数
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(kCrashOnLaunchTimeIntervalThreshold * NSEC_PER_SEC)), dispatch_get_main_queue(), ^(void){
// APP活过了阈值时间,重置崩溃计数
if (Logger) Logger([NSString stringWithFormat:@"GYBootingProtection: long live the app ( more than %@ seconds ), now reset crash counts", @(kCrashOnLaunchTimeIntervalThreshold)]);
[self setCrashCount:0];
});
// 修复
if (launchCrashes >= kContinuousCrashOnLaunchNeedToFix) {
if (Logger) Logger(@"need to repair");
[self setIsFixing:YES];
if (repairBlock) {
repairBlock(^BOOL(){
[self setCrashCount:0];
[self setIsFixing:NO];
if (boolCompletionBlock) {
if (Logger) Logger(@"repairBlock will execute completion block");
return boolCompletionBlock();
} else {
if (Logger) Logger(@"repairBlock will not execute completion block (nil)");
return NO;
}
});
}
} else {
// 正常流程,无需修复
if (Logger) Logger(@"need no repair");
if (boolCompletionBlock) {
if (Logger) Logger(@"will execute completion block");
return boolCompletionBlock();
}
}
return NO;
}
+ (void)setIsFixing:(BOOL)isFixingCrash
{
if (Logger) Logger([NSString stringWithFormat:@"setisFixingCrash:{%@}",@(isFixingCrash)]);
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:isFixingCrash forKey:kContinuousCrashFixingKey];
[defaults synchronize];
}
+ (void)setCrashCount:(NSInteger)count
{
if (Logger) Logger([NSString stringWithFormat:@"setCrashCount:%@", @(count)]);
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:count forKey:kContinuousCrashOnLaunchCounterKey];
[defaults synchronize];
}
+ (BOOL)isFixingCrash
{
BOOL isFixingCrash = [[NSUserDefaults standardUserDefaults] boolForKey:kContinuousCrashFixingKey];
if (Logger) Logger([NSString stringWithFormat:@"isFixingCrash:%@", @(isFixingCrash)]);
return isFixingCrash;
}
+ (NSInteger)crashCount
{
NSInteger crashCount = [[NSUserDefaults standardUserDefaults] integerForKey:kContinuousCrashOnLaunchCounterKey];
if (Logger) Logger([NSString stringWithFormat:@"crashCount:%@", @(crashCount)]);
return crashCount;
}
+ (void)setLogger:(void (^)(NSString *))logger
{
Logger = [logger copy];
}
+ (void)setReportBlock:(ReportBlock)block
{
reportBlock = block;
}
+ (void)setRepairBlock:(RepairBlock)block
{
repairBlock = block;
}
+ (void)setBoolCompletionBlock:(BoolCompletionBlock)block
{
boolCompletionBlock = block;
}
+ (void)setStartupCrashForTest:(BOOL)isOn
{
if (Logger) Logger([NSString stringWithFormat:@"setStartupCrashForTest:%@", @(isOn)]);
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:isOn forKey:kStartupCrashForTest];
[defaults synchronize];
}
+ (BOOL)startupCrashForTest
{
if ([GYBootingProtection crashCount] >= kContinuousCrashOnLaunchNeedToFix) {
return NO;
}
BOOL ret = [[NSUserDefaults standardUserDefaults] boolForKey:kStartupCrashForTest];
if (Logger) Logger([NSString stringWithFormat:@"startupCrashForTest:%@", @(ret)]);
return ret;
}
+ (void)deleteAllFilesUnderDocumentsLibraryCaches {
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *libraryDirectory = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject];
NSString *cachesDirectory = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
NSArray *filePathsToRemove = @[documentsDirectory, libraryDirectory, cachesDirectory];
NSFileManager *fileMgr = [NSFileManager defaultManager];
for (NSString *filePath in filePathsToRemove) {
if ([fileMgr fileExistsAtPath:filePath]) {
NSArray *subFileArray = [fileMgr contentsOfDirectoryAtPath:filePath error:nil];
for (NSString *subFileName in subFileArray) {
NSString *subFilePath = [filePath stringByAppendingPathComponent:subFileName];
if ([fileMgr removeItemAtPath:subFilePath error:nil]) {
NSLog(@"removed file path:%@", subFilePath);
} else {
NSLog(@"failed to remove file path:%@", subFilePath);
}
}
} else {
NSLog(@"failed to remove non-existing file path:%@", filePath);
}
}
NSLog(@"recoverFromContinuousCrash finished, files at home:[%@]\nDocuments:[%@]\nLibrary:[%@]\nCaches:[%@]",
[[NSFileManager defaultManager] contentsOfDirectoryAtPath:NSHomeDirectory() error:nil],
[[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:nil],
[[NSFileManager defaultManager] contentsOfDirectoryAtPath:libraryDirectory error:nil],
[[NSFileManager defaultManager] contentsOfDirectoryAtPath:cachesDirectory error:nil]);
}
@end