-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathRegistrationCoodinatorShims.swift
483 lines (364 loc) · 17.5 KB
/
RegistrationCoodinatorShims.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
//
// Copyright 2023 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
//
import Contacts
import Foundation
import LibSignalClient
public import SignalServiceKit
extension RegistrationCoordinatorImpl {
public enum Shims {
public typealias ContactsManager = _RegistrationCoordinator_ContactsManagerShim
public typealias ContactsStore = _RegistrationCoordinator_CNContactsStoreShim
public typealias ExperienceManager = _RegistrationCoordinator_ExperienceManagerShim
public typealias FeatureFlags = _RegistrationCoordinator_FeatureFlagsShim
public typealias MessagePipelineSupervisor = _RegistrationCoordinator_MessagePipelineSupervisorShim
public typealias MessageProcessor = _RegistrationCoordinator_MessageProcessorShim
public typealias OWS2FAManager = _RegistrationCoordinator_OWS2FAManagerShim
public typealias PreKeyManager = _RegistrationCoordinator_PreKeyManagerShim
public typealias ProfileManager = _RegistrationCoordinator_ProfileManagerShim
public typealias PushRegistrationManager = _RegistrationCoordinator_PushRegistrationManagerShim
public typealias ReceiptManager = _RegistrationCoordinator_ReceiptManagerShim
public typealias StorageServiceManager = _RegistrationCoordinator_StorageServiceManagerShim
public typealias UDManager = _RegistrationCoordinator_UDManagerShim
}
public enum Wrappers {
public typealias ContactsManager = _RegistrationCoordinator_ContactsManagerWrapper
public typealias ContactsStore = _RegistrationCoordinator_CNContactsStoreWrapper
public typealias ExperienceManager = _RegistrationCoordinator_ExperienceManagerWrapper
public typealias FeatureFlags = _RegistrationCoordinator_FeatureFlagsWrapper
public typealias MessagePipelineSupervisor = _RegistrationCoordinator_MessagePipelineSupervisorWrapper
public typealias MessageProcessor = _RegistrationCoordinator_MessageProcessorWrapper
public typealias OWS2FAManager = _RegistrationCoordinator_OWS2FAManagerWrapper
public typealias PreKeyManager = _RegistrationCoordinator_PreKeyManagerWrapper
public typealias ProfileManager = _RegistrationCoordinator_ProfileManagerWrapper
public typealias PushRegistrationManager = _RegistrationCoordinator_PushRegistrationManagerWrapper
public typealias ReceiptManager = _RegistrationCoordinator_ReceiptManagerWrapper
public typealias StorageServiceManager = _RegistrationCoordinator_StorageServiceManagerWrapper
public typealias UDManager = _RegistrationCoordinator_UDManagerWrapper
}
}
// MARK: OWSContactsManager
public protocol _RegistrationCoordinator_ContactsManagerShim {
func fetchSystemContactsOnceIfAlreadyAuthorized()
}
public class _RegistrationCoordinator_ContactsManagerWrapper: _RegistrationCoordinator_ContactsManagerShim {
private let manager: OWSContactsManager
public init(_ manager: OWSContactsManager) { self.manager = manager }
public func fetchSystemContactsOnceIfAlreadyAuthorized() {
manager.fetchSystemContactsOnceIfAlreadyAuthorized()
}
}
// MARK: CNContacts
public protocol _RegistrationCoordinator_CNContactsStoreShim {
func needsContactsAuthorization() -> Guarantee<Bool>
func requestContactsAuthorization() -> Guarantee<Void>
}
public class _RegistrationCoordinator_CNContactsStoreWrapper: _RegistrationCoordinator_CNContactsStoreShim {
public init() {}
public func needsContactsAuthorization() -> Guarantee<Bool> {
return .value(CNContactStore.authorizationStatus(for: .contacts) == .notDetermined)
}
public func requestContactsAuthorization() -> Guarantee<Void> {
let (guarantee, future) = Guarantee<Void>.pending()
CNContactStore().requestAccess(for: CNEntityType.contacts) { (granted, error) -> Void in
if granted {
Logger.info("User granted contacts permission")
} else {
// Unfortunately, we can't easily disambiguate "not granted" and
// "other error".
Logger.warn("User denied contacts permission or there was an error. Error: \(String(describing: error))")
}
future.resolve()
}
return guarantee
}
}
// MARK: - ExperienceManager
public protocol _RegistrationCoordinator_ExperienceManagerShim {
func clearIntroducingPinsExperience(_ tx: DBWriteTransaction)
func enableAllGetStartedCards(_ tx: DBWriteTransaction)
}
public class _RegistrationCoordinator_ExperienceManagerWrapper: _RegistrationCoordinator_ExperienceManagerShim {
public init() {}
public func clearIntroducingPinsExperience(_ tx: DBWriteTransaction) {
ExperienceUpgradeManager.clearExperienceUpgrade(.introducingPins, transaction: SDSDB.shimOnlyBridge(tx))
}
public func enableAllGetStartedCards(_ tx: DBWriteTransaction) {
GetStartedBannerViewController.enableAllCards(writeTx: SDSDB.shimOnlyBridge(tx))
}
}
// MARK: - FeatureFlags
public protocol _RegistrationCoordinator_FeatureFlagsShim {
var enableAccountEntropyPool: Bool { get }
var messageBackupFileAlphaRegistrationFlow: Bool { get }
}
public class _RegistrationCoordinator_FeatureFlagsWrapper: _RegistrationCoordinator_FeatureFlagsShim {
public init() {}
public var enableAccountEntropyPool: Bool { FeatureFlags.enableAccountEntropyPool }
public var messageBackupFileAlphaRegistrationFlow: Bool { FeatureFlags.MessageBackup.fileAlphaRegistrationFlow }
}
// MARK: - MessagePipelineSupervisor
public protocol _RegistrationCoordinator_MessagePipelineSupervisorShim {
func suspendMessageProcessingWithoutHandle(for: MessagePipelineSupervisor.Suspension)
func unsuspendMessageProcessing(for: MessagePipelineSupervisor.Suspension)
}
public class _RegistrationCoordinator_MessagePipelineSupervisorWrapper: _RegistrationCoordinator_MessagePipelineSupervisorShim {
private let supervisor: MessagePipelineSupervisor
public init(_ supervisor: MessagePipelineSupervisor) {
self.supervisor = supervisor
}
public func suspendMessageProcessingWithoutHandle(for suspension: MessagePipelineSupervisor.Suspension) {
supervisor.suspendMessageProcessingWithoutHandle(for: suspension)
}
public func unsuspendMessageProcessing(for suspension: MessagePipelineSupervisor.Suspension) {
supervisor.unsuspendMessageProcessing(for: suspension)
}
}
// MARK: - MessageProcessor
public protocol _RegistrationCoordinator_MessageProcessorShim {
func waitForFetchingAndProcessing() -> Guarantee<Void>
}
public class _RegistrationCoordinator_MessageProcessorWrapper: _RegistrationCoordinator_MessageProcessorShim {
private let processor: MessageProcessor
public init(_ processor: MessageProcessor) {
self.processor = processor
}
public func waitForFetchingAndProcessing() -> Guarantee<Void> {
return processor.waitForFetchingAndProcessing()
}
}
// MARK: - OWS2FAManager
public protocol _RegistrationCoordinator_OWS2FAManagerShim {
func pinCode(_ tx: DBReadTransaction) -> String?
func clearLocalPinCode(_ tx: DBWriteTransaction)
func isReglockEnabled(_ tx: DBReadTransaction) -> Bool
func markPinEnabled(_ pin: String, _ tx: DBWriteTransaction)
func markRegistrationLockEnabled(_ tx: DBWriteTransaction)
}
public class _RegistrationCoordinator_OWS2FAManagerWrapper: _RegistrationCoordinator_OWS2FAManagerShim {
private let manager: OWS2FAManager
public init(_ manager: OWS2FAManager) { self.manager = manager }
public func pinCode(_ tx: DBReadTransaction) -> String? {
return manager.pinCode(transaction: SDSDB.shimOnlyBridge(tx))
}
public func clearLocalPinCode(_ tx: DBWriteTransaction) {
return manager.clearLocalPinCode(transaction: SDSDB.shimOnlyBridge(tx))
}
public func isReglockEnabled(_ tx: DBReadTransaction) -> Bool {
return manager.isRegistrationLockV2Enabled(transaction: SDSDB.shimOnlyBridge(tx))
}
public func markPinEnabled(_ pin: String, _ tx: DBWriteTransaction) {
manager.markEnabled(pin: pin, transaction: SDSDB.shimOnlyBridge(tx))
}
public func markRegistrationLockEnabled(_ tx: DBWriteTransaction) {
manager.markRegistrationLockV2Enabled(transaction: SDSDB.shimOnlyBridge(tx))
}
}
// MARK: - PreKeyManager
// Unfortunately this shim needs to exist because of tests; Promise.wrapAsync starts a Task,
// which is unavoidably asynchronous, which does not play nice with TestScheduler and general
// synchronous, single-threaded test setups. To avoid this, we need to push down the
// Promise.wrapAsync call into the shim, so that we can avoid the call entirely in tests.
public protocol _RegistrationCoordinator_PreKeyManagerShim {
func createPreKeysForRegistration() -> Promise<RegistrationPreKeyUploadBundles>
func finalizeRegistrationPreKeys(
_ prekeyBundles: RegistrationPreKeyUploadBundles,
uploadDidSucceed: Bool
) -> Promise<Void>
func rotateOneTimePreKeysForRegistration(auth: ChatServiceAuth) -> Promise<Void>
}
public class _RegistrationCoordinator_PreKeyManagerWrapper: _RegistrationCoordinator_PreKeyManagerShim {
private let preKeyManager: PreKeyManager
public init(_ preKeyManager: PreKeyManager) {
self.preKeyManager = preKeyManager
}
public func createPreKeysForRegistration() -> Promise<RegistrationPreKeyUploadBundles> {
let preKeyManager = self.preKeyManager
return Promise.wrapAsync {
return try await preKeyManager.createPreKeysForRegistration().value
}
}
public func finalizeRegistrationPreKeys(
_ prekeyBundles: RegistrationPreKeyUploadBundles,
uploadDidSucceed: Bool
) -> Promise<Void> {
let preKeyManager = self.preKeyManager
return Promise.wrapAsync {
return try await preKeyManager.finalizeRegistrationPreKeys(
prekeyBundles,
uploadDidSucceed: uploadDidSucceed
).value
}
}
public func rotateOneTimePreKeysForRegistration(auth: ChatServiceAuth) -> Promise<Void> {
let preKeyManager = self.preKeyManager
return Promise.wrapAsync {
return try await preKeyManager.rotateOneTimePreKeysForRegistration(auth: auth).value
}
}
}
// MARK: - ProfileManager
public protocol _RegistrationCoordinator_ProfileManagerShim {
func localUserProfile(tx: DBReadTransaction) -> OWSUserProfile?
// NOTE: non-optional because OWSProfileManager generates a random key
// if one doesn't already exist.
func updateLocalProfile(
givenName: OWSUserProfile.NameComponent,
familyName: OWSUserProfile.NameComponent?,
avatarData: Data?,
authedAccount: AuthedAccount,
tx: DBWriteTransaction
) -> Promise<Void>
func scheduleReuploadLocalProfile(authedAccount: AuthedAccount)
}
public class _RegistrationCoordinator_ProfileManagerWrapper: _RegistrationCoordinator_ProfileManagerShim {
private let manager: ProfileManager
public init(_ manager: ProfileManager) { self.manager = manager }
public func localUserProfile(tx: DBReadTransaction) -> OWSUserProfile? {
return manager.localUserProfile(tx: SDSDB.shimOnlyBridge(tx))
}
public func updateLocalProfile(
givenName: OWSUserProfile.NameComponent,
familyName: OWSUserProfile.NameComponent?,
avatarData: Data?,
authedAccount: AuthedAccount,
tx: DBWriteTransaction
) -> Promise<Void> {
return manager.updateLocalProfile(
profileGivenName: .setTo(givenName),
profileFamilyName: .setTo(familyName),
profileBio: .setTo(nil),
profileBioEmoji: .setTo(nil),
profileAvatarData: .setTo(avatarData),
visibleBadgeIds: .setTo([]),
unsavedRotatedProfileKey: nil,
userProfileWriter: .registration,
authedAccount: authedAccount,
tx: SDSDB.shimOnlyBridge(tx)
)
}
public func scheduleReuploadLocalProfile(authedAccount: AuthedAccount) {
Task {
await DependenciesBridge.shared.db.awaitableWrite { tx in
_ = manager.reuploadLocalProfile(
unsavedRotatedProfileKey: nil,
mustReuploadAvatar: false,
authedAccount: authedAccount,
tx: tx
)
}
}
}
}
// MARK: - PushRegistrationManager
extension Registration {
public enum RequestPushTokensResult {
case success(RegistrationRequestFactory.ApnRegistrationId)
case pushUnsupported(description: String)
case timeout
case genericError(Error)
}
public enum SyncPushTokensResult {
case success
case pushUnsupported(description: String)
case networkError
case genericError(Error)
}
}
public protocol _RegistrationCoordinator_PushRegistrationManagerShim {
func needsNotificationAuthorization() -> Guarantee<Bool>
func registerUserNotificationSettings() -> Guarantee<Void>
func requestPushToken() -> Guarantee<Registration.RequestPushTokensResult>
func receivePreAuthChallengeToken() -> Guarantee<String>
func clearPreAuthChallengeToken()
}
public class _RegistrationCoordinator_PushRegistrationManagerWrapper: _RegistrationCoordinator_PushRegistrationManagerShim {
private let manager: PushRegistrationManager
public init(_ manager: PushRegistrationManager) { self.manager = manager }
public func needsNotificationAuthorization() -> Guarantee<Bool> {
return manager.needsNotificationAuthorization()
}
public func registerUserNotificationSettings() -> Guarantee<Void> {
return Guarantee.wrapAsync { [manager] in
await manager.registerUserNotificationSettings()
}
}
public func requestPushToken() -> Guarantee<Registration.RequestPushTokensResult> {
return manager.requestPushTokens(forceRotation: false, timeOutEventually: true)
.map(on: SyncScheduler()) { .success($0) }
.recover(on: SyncScheduler()) { error in
switch error {
case PushRegistrationError.pushNotSupported(let description):
return .value(.pushUnsupported(description: description))
case PushRegistrationError.timeout:
return .value(.timeout)
default:
return .value(.genericError(error))
}
}
}
public func receivePreAuthChallengeToken() -> Guarantee<String> {
return manager.receivePreAuthChallengeToken()
}
public func clearPreAuthChallengeToken() {
manager.clearPreAuthChallengeToken()
}
}
// MARK: - ReceiptManager
public protocol _RegistrationCoordinator_ReceiptManagerShim {
func setAreReadReceiptsEnabled(_ areEnabled: Bool, _ tx: DBWriteTransaction)
func setAreStoryViewedReceiptsEnabled(_ areEnabled: Bool, _ tx: DBWriteTransaction)
}
public class _RegistrationCoordinator_ReceiptManagerWrapper: _RegistrationCoordinator_ReceiptManagerShim {
private let manager: OWSReceiptManager
public init(_ manager: OWSReceiptManager) { self.manager = manager }
public func setAreReadReceiptsEnabled(_ areEnabled: Bool, _ tx: DBWriteTransaction) {
manager.setAreReadReceiptsEnabled(areEnabled, transaction: SDSDB.shimOnlyBridge(tx))
}
public func setAreStoryViewedReceiptsEnabled(_ areEnabled: Bool, _ tx: DBWriteTransaction) {
StoryManager.setAreViewReceiptsEnabled(areEnabled, transaction: SDSDB.shimOnlyBridge(tx))
}
}
// MARK: - StorageService
public protocol _RegistrationCoordinator_StorageServiceManagerShim {
func rotateManifest(mode: StorageServiceManagerManifestRotationMode, authedDevice: AuthedDevice) -> Promise<Void>
func restoreOrCreateManifestIfNecessary(authedDevice: AuthedDevice, masterKeySource: StorageService.MasterKeySource) -> Promise<Void>
func backupPendingChanges(authedDevice: AuthedDevice)
func recordPendingLocalAccountUpdates()
}
public class _RegistrationCoordinator_StorageServiceManagerWrapper: _RegistrationCoordinator_StorageServiceManagerShim {
private let manager: StorageServiceManager
public init(_ manager: StorageServiceManager) { self.manager = manager }
public func rotateManifest(
mode: StorageServiceManagerManifestRotationMode,
authedDevice: AuthedDevice
) -> Promise<Void> {
Promise.wrapAsync {
try await self.manager.rotateManifest(mode: mode, authedDevice: authedDevice)
}
}
public func restoreOrCreateManifestIfNecessary(
authedDevice: AuthedDevice,
masterKeySource: StorageService.MasterKeySource
) -> Promise<Void> {
manager.restoreOrCreateManifestIfNecessary(authedDevice: authedDevice, masterKeySource: masterKeySource)
}
public func backupPendingChanges(authedDevice: AuthedDevice) {
manager.backupPendingChanges(authedDevice: authedDevice)
}
public func recordPendingLocalAccountUpdates() {
manager.recordPendingLocalAccountUpdates()
}
}
// MARK: - UDManager
public protocol _RegistrationCoordinator_UDManagerShim {
func shouldAllowUnrestrictedAccessLocal(transaction: DBReadTransaction) -> Bool
}
public class _RegistrationCoordinator_UDManagerWrapper: _RegistrationCoordinator_UDManagerShim {
private let manager: OWSUDManager
public init(_ manager: OWSUDManager) { self.manager = manager }
public func shouldAllowUnrestrictedAccessLocal(transaction: DBReadTransaction) -> Bool {
return manager.shouldAllowUnrestrictedAccessLocal(transaction: SDSDB.shimOnlyBridge(transaction))
}
}