Skip to content

Commit 6efc1f2

Browse files
max-signalsashaweiss-signal
authored andcommitted
Fix crash after using device transfer
1 parent d7cb1f8 commit 6efc1f2

File tree

7 files changed

+37
-30
lines changed

7 files changed

+37
-30
lines changed

Signal/AppLaunch/AppDelegate.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,13 @@ final class AppDelegate: UIResponder, UIApplicationDelegate {
178178

179179
MessageFetchBGRefreshTask.register()
180180

181+
let keychainStorage = KeychainStorageImpl(isUsingProductionService: TSConstants.isUsingProductionService)
182+
let deviceTransferService = DeviceTransferService(keychainStorage: keychainStorage)
183+
184+
AppEnvironment.setSharedEnvironment(AppEnvironment(
185+
deviceTransferService: deviceTransferService
186+
))
187+
181188
// This *must* happen before we try and access or verify the database,
182189
// since we may be in a state where the database has been partially
183190
// restored from transfer (e.g. the key was replaced, but the database
@@ -186,10 +193,9 @@ final class AppDelegate: UIResponder, UIApplicationDelegate {
186193
title: "Slow device transfer service launch",
187194
logIfLongerThan: 0.01,
188195
logInProduction: true,
189-
block: { DeviceTransferService.shared.launchCleanup() }
196+
block: { deviceTransferService.launchCleanup() }
190197
)
191198

192-
let keychainStorage = KeychainStorageImpl(isUsingProductionService: TSConstants.isUsingProductionService)
193199
let databaseStorage: SDSDatabaseStorage
194200
do {
195201
databaseStorage = try SDSDatabaseStorage(

Signal/AppLaunch/AppEnvironment.swift

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,21 @@ import SignalServiceKit
77

88
public class AppEnvironment: NSObject {
99

10-
private static var _shared: AppEnvironment = AppEnvironment()
10+
private static var _shared: AppEnvironment?
1111

12-
@objc
13-
public class var shared: AppEnvironment {
14-
get {
15-
return _shared
16-
}
17-
set {
18-
guard CurrentAppContext().isRunningTests else {
19-
owsFailDebug("Can only switch environments in tests.")
20-
return
21-
}
22-
23-
_shared = newValue
24-
}
12+
static func setSharedEnvironment(_ appEnvironment: AppEnvironment) {
13+
owsAssert(self._shared == nil)
14+
self._shared = appEnvironment
2515
}
2616

27-
public var pushRegistrationManagerRef: PushRegistrationManager
17+
@objc
18+
public class var shared: AppEnvironment { _shared! }
19+
20+
let pushRegistrationManagerRef: PushRegistrationManager
2821

2922
var callService: CallService!
3023

31-
let deviceTransferServiceRef = DeviceTransferService()
24+
let deviceTransferServiceRef: DeviceTransferService
3225

3326
let avatarHistorManagerRef = AvatarHistoryManager()
3427

@@ -42,7 +35,8 @@ public class AppEnvironment: NSObject {
4235
private(set) var badgeManager: BadgeManager!
4336
private var usernameValidationObserverRef: UsernameValidationObserver?
4437

45-
private override init() {
38+
init(deviceTransferService: DeviceTransferService) {
39+
self.deviceTransferServiceRef = deviceTransferService
4640
self.pushRegistrationManagerRef = PushRegistrationManager()
4741

4842
super.init()

Signal/DeviceTransfer/DeviceTransferService+Restore.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ extension DeviceTransferService {
145145
}
146146

147147
do {
148-
try databaseStorage.keyFetcher.store(data: database.key)
148+
try GRDBKeyFetcher(keychainStorage: keychainStorage).store(data: database.key)
149149
} catch {
150150
owsFailDebug("failed to restore database key")
151151
return false
@@ -547,7 +547,7 @@ extension DeviceTransferService {
547547
throw OWSAssertionError("No manifest database available")
548548
}
549549

550-
try databaseStorage.keyFetcher.store(data: database.key)
550+
try GRDBKeyFetcher(keychainStorage: keychainStorage).store(data: database.key)
551551
GRDBDatabaseStorageAdapter.promoteTransferDirectoryToPrimary()
552552
}
553553

Signal/DeviceTransfer/DeviceTransferService.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,11 @@ class DeviceTransferService: NSObject {
123123

124124
// MARK: -
125125

126-
override init() {
126+
let keychainStorage: any KeychainStorage
127+
128+
init(keychainStorage: any KeychainStorage) {
129+
self.keychainStorage = keychainStorage
130+
127131
super.init()
128132

129133
SwiftSingletons.register(self)

SignalNSE/NSEEnvironment.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ class NSEEnvironment: Dependencies {
131131
databaseFileUrl: SDSDatabaseStorage.grdbDatabaseFileUrl,
132132
keychainStorage: keychainStorage
133133
)
134+
databaseStorage.grdbStorage.setUpDatabasePathKVO()
134135

135136
didFinishDatabaseSetup = true
136137

SignalServiceKit/Storage/Database/GRDBDatabaseStorageAdapter.swift

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public class GRDBDatabaseStorageAdapter: NSObject {
8787
return primaryFolderName
8888
}
8989
if let primaryFolderName = CurrentAppContext().appUserDefaults().string(forKey: DirectoryMode.primaryFolderNameKey) {
90-
// Make sure its also written to the file.
90+
// Make sure it's also written to the file.
9191
OWSFileSystem.ensureDirectoryExists(fileUrl.deletingLastPathComponent().path)
9292
try? primaryFolderName.write(toFile: fileUrl.pathExtension, atomically: true, encoding: .utf8)
9393
return primaryFolderName
@@ -97,7 +97,7 @@ public class GRDBDatabaseStorageAdapter: NSObject {
9797

9898
fileprivate static func writeStoredPrimaryFolderName(_ newPrimaryFolderName: String) {
9999
CurrentAppContext().appUserDefaults().set(newPrimaryFolderName, forKey: DirectoryMode.primaryFolderNameKey)
100-
// Make sure its also written to the file.
100+
// Make sure it's also written to the file.
101101
let fileUrl = storedPrimaryFolderNameFileUrl()
102102
OWSFileSystem.ensureDirectoryExists(fileUrl.deletingLastPathComponent().path)
103103
try? newPrimaryFolderName.write(toFile: fileUrl.pathExtension, atomically: true, encoding: .utf8)
@@ -139,9 +139,6 @@ public class GRDBDatabaseStorageAdapter: NSObject {
139139
} catch {
140140
throw error
141141
}
142-
143-
super.init()
144-
setUpDatabasePathKVO()
145142
}
146143

147144
deinit {
@@ -207,7 +204,7 @@ public class GRDBDatabaseStorageAdapter: NSObject {
207204

208205
private var darwinToken: Int32?
209206

210-
func setUpDatabasePathKVO() {
207+
public func setUpDatabasePathKVO() {
211208
darwinToken = DarwinNotificationCenter.addObserver(
212209
for: .primaryDBFolderNameDidChange,
213210
queue: .main,
@@ -351,7 +348,7 @@ extension GRDBDatabaseStorageAdapter {
351348
}
352349

353350
public static func promoteTransferDirectoryToPrimary() {
354-
owsAssert(CurrentAppContext().isMainApp, "Only the main app can't swap databases")
351+
owsAssert(CurrentAppContext().isMainApp, "Only the main app can swap databases")
355352

356353
// Ordering matters here. We should be able to crash and recover without issue
357354
// A prior run may have already performed the swap but crashed, so we should not expect a transfer folder
@@ -863,7 +860,11 @@ public struct GRDBKeyFetcher {
863860
public static let kSQLCipherKeySpecLength: Int32 = 48
864861
}
865862

866-
let keychainStorage: any KeychainStorage
863+
private let keychainStorage: any KeychainStorage
864+
865+
public init(keychainStorage: any KeychainStorage) {
866+
self.keychainStorage = keychainStorage
867+
}
867868

868869
func fetchString() throws -> String {
869870
// Use a raw key spec, where the 96 hexadecimal digits are provided

SignalShareExtension/ShareViewController.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public class ShareViewController: UIViewController, ShareViewDelegate, SAEFailed
7979
self.showNotRegisteredView()
8080
return
8181
}
82+
databaseStorage.grdbStorage.setUpDatabasePathKVO()
8283

8384
// We shouldn't set up our environment until after we've consulted isReadyForAppExtensions.
8485
let databaseContinuation = AppSetup().start(

0 commit comments

Comments
 (0)