forked from blinksh/blink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SessionRegistry.swift
274 lines (231 loc) · 7.22 KB
/
SessionRegistry.swift
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
//////////////////////////////////////////////////////////////////////////////////
//
// B L I N K
//
// Copyright (C) 2016-2019 Blink Mobile Shell Project
//
// This file is part of Blink.
//
// Blink is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Blink is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Blink. If not, see <http://www.gnu.org/licenses/>.
//
// In addition, Blink is also subject to certain additional terms under
// GNU GPL version 3 section 7.
//
// You should have received a copy of these additional terms immediately
// following the terms and conditions of the GNU General Public License
// which accompanied the Blink Source Code. If not, see
// <http://www.github.com/blinksh/blink>.
//
////////////////////////////////////////////////////////////////////////////////
import Foundation
import UIKit
class SessionMeta: Codable {
fileprivate(set) var key: UUID = UUID()
fileprivate(set) var isSuspended: Bool = false
}
protocol SuspendableSession: class {
var meta: SessionMeta { get }
init(meta: SessionMeta?)
func resume(with unarchiver: NSKeyedUnarchiver)
func suspendedSession(with archiver: NSKeyedArchiver)
}
extension SuspendableSession {
func suspendIfNeeded() {
SessionRegistry.shared.suspendIfNeeded(session: self)
}
func resumeIfNeeded() {
SessionRegistry.shared.resumeIfNeeded(session: self)
}
}
@objc class SessionRegistry: NSObject {
private var _sessionsIndex: [UUID: SuspendableSession] = [:]
private var _metaIndex: [UUID: SessionMeta] = [:]
@objc public static let shared = SessionRegistry()
override init() {
super.init()
_fsReadMetaIndex()
DispatchQueue.main.asyncAfter(wallDeadline: DispatchWallTime.now() + TimeInterval(10)) {
self._cleanLostSessions()
}
}
func _cleanLostSessions() {
guard UIApplication.shared.isProtectedDataAvailable
else {
return
}
var keysSet = Set(_metaIndex.keys)
for key in _sessionsIndex.keys {
keysSet.remove(key)
}
for key in keysSet {
if _fsStateExists(forKey: key) == false {
_metaIndex.removeValue(forKey: key)
}
}
// Enumerate files and delete missed in index
_fsWriteMetaIndex()
}
func track(session: SuspendableSession) {
let meta = session.meta
let key = meta.key
_metaIndex[key] = meta
_sessionsIndex[key] = session
}
subscript<T: SuspendableSession>(key: UUID) -> T {
// 1. we already have it (same type)
if let session = _sessionsIndex[key] as? T {
return session
}
// 2. we have it only in meta index
if let meta = _metaIndex[key] {
meta.isSuspended = true
let session = T(meta: meta)
track(session: session)
return session
}
// 3. creating new one
let meta = SessionMeta()
meta.key = key
meta.isSuspended = true
let session = T(meta: meta)
track(session: session)
return session
}
func remove(forKey key: UUID) {
_metaIndex.removeValue(forKey: key)
_fsRemove(forKey: key)
_sessionsIndex.removeValue(forKey: key)
}
func remove(session: SuspendableSession?) {
if let key = session?.meta.key {
remove(forKey: key)
}
}
@objc func suspend() {
_sessionsIndex.forEach { self.suspendIfNeeded(session: $1) }
_fsWriteMetaIndex()
}
func suspendIfNeeded(session: SuspendableSession) {
guard !session.meta.isSuspended else {
return
}
let archiver = NSKeyedArchiver(requiringSecureCoding: true)
session.suspendedSession(with: archiver)
_fsWrite(archiver.encodedData, forKey: session.meta.key)
session.meta.isSuspended = true
}
func resumeIfNeeded(session: SuspendableSession) {
guard session.meta.isSuspended else {
return
}
_resume(forKey: session.meta.key)
session.meta.isSuspended = false
}
private func _resume(forKey key: UUID) {
guard
let session = _sessionsIndex[key],
let data = _fsRead(forKey: key),
let unarchiver = try? NSKeyedUnarchiver(forReadingFrom: data)
else {
return
}
session.resume(with: unarchiver)
session.meta.isSuspended = false
}
private var _fsSessionsFolderURL: URL? = nil
private func _fsSessionsFolder() throws -> URL {
if let fsSessionFolderURL = _fsSessionsFolderURL {
return fsSessionFolderURL
}
let fm = FileManager.default
var supporDirUrl = try fm.url(
for: .applicationSupportDirectory,
in: .userDomainMask,
appropriateFor: nil,
create: true
)
supporDirUrl.appendPathComponent("sessions")
var isDir:ObjCBool = false
if !fm.fileExists(atPath: supporDirUrl.path, isDirectory: &isDir) {
try fm.createDirectory(at: supporDirUrl, withIntermediateDirectories: true, attributes: nil)
}
_fsSessionsFolderURL = supporDirUrl
return supporDirUrl
}
private func _fsSessionURL(_ key: UUID) throws -> URL {
let sessionsFolderURL = try _fsSessionsFolder()
var fileURL = sessionsFolderURL
fileURL.appendPathComponent(key.uuidString)
return fileURL
}
private func _fsRemove(forKey key: UUID) {
let fm = FileManager.default
do {
let sessionURL = try _fsSessionURL(key)
if fm.fileExists(atPath: sessionURL.path) {
try fm.removeItem(at: sessionURL)
}
} catch let e {
debugPrint(e)
}
}
private func _fsWrite(_ data: Data, forKey key: UUID) {
do {
let sessionURL = try _fsSessionURL(key)
try data.write(to: sessionURL, options: [.atomic, .completeFileProtection])
} catch let e {
debugPrint(e)
}
}
private func _fsStateExists(forKey key: UUID) -> Bool? {
do {
let sessionURL = try _fsSessionURL(key)
return FileManager.default.fileExists(atPath: sessionURL.path)
} catch {
return nil
}
}
private func _fsRead(forKey key: UUID) -> Data? {
do {
let sessionURL = try _fsSessionURL(key)
let data = try Data(contentsOf: sessionURL)
return data
} catch let e {
// debugPrint(e)
return nil
}
}
private func _fsWriteMetaIndex() {
let jsonEncoder = JSONEncoder()
do {
let data = try jsonEncoder.encode(_metaIndex)
let sessionsFolder = try _fsSessionsFolder()
let indexURL = sessionsFolder.appendingPathComponent("index.json")
try data.write(to: indexURL, options: [.atomic])
} catch let e {
debugPrint(e)
}
}
private func _fsReadMetaIndex() {
do {
let sessionsFolder = try _fsSessionsFolder()
let indexURL = sessionsFolder.appendingPathComponent("index.json")
let data = try Data(contentsOf: indexURL)
let jsonDecoder = JSONDecoder()
_metaIndex = try jsonDecoder.decode(type(of: _metaIndex), from: data)
} catch let e {
debugPrint(e)
}
}
}