Skip to content

Commit

Permalink
Fix crash after using device transfer
Browse files Browse the repository at this point in the history
  • Loading branch information
max-signal authored and sashaweiss-signal committed May 16, 2024
1 parent d7cb1f8 commit 6efc1f2
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 30 deletions.
10 changes: 8 additions & 2 deletions Signal/AppLaunch/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,13 @@ final class AppDelegate: UIResponder, UIApplicationDelegate {

MessageFetchBGRefreshTask.register()

let keychainStorage = KeychainStorageImpl(isUsingProductionService: TSConstants.isUsingProductionService)
let deviceTransferService = DeviceTransferService(keychainStorage: keychainStorage)

AppEnvironment.setSharedEnvironment(AppEnvironment(
deviceTransferService: deviceTransferService
))

// This *must* happen before we try and access or verify the database,
// since we may be in a state where the database has been partially
// restored from transfer (e.g. the key was replaced, but the database
Expand All @@ -186,10 +193,9 @@ final class AppDelegate: UIResponder, UIApplicationDelegate {
title: "Slow device transfer service launch",
logIfLongerThan: 0.01,
logInProduction: true,
block: { DeviceTransferService.shared.launchCleanup() }
block: { deviceTransferService.launchCleanup() }
)

let keychainStorage = KeychainStorageImpl(isUsingProductionService: TSConstants.isUsingProductionService)
let databaseStorage: SDSDatabaseStorage
do {
databaseStorage = try SDSDatabaseStorage(
Expand Down
28 changes: 11 additions & 17 deletions Signal/AppLaunch/AppEnvironment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,21 @@ import SignalServiceKit

public class AppEnvironment: NSObject {

private static var _shared: AppEnvironment = AppEnvironment()
private static var _shared: AppEnvironment?

@objc
public class var shared: AppEnvironment {
get {
return _shared
}
set {
guard CurrentAppContext().isRunningTests else {
owsFailDebug("Can only switch environments in tests.")
return
}

_shared = newValue
}
static func setSharedEnvironment(_ appEnvironment: AppEnvironment) {
owsAssert(self._shared == nil)
self._shared = appEnvironment
}

public var pushRegistrationManagerRef: PushRegistrationManager
@objc
public class var shared: AppEnvironment { _shared! }

let pushRegistrationManagerRef: PushRegistrationManager

var callService: CallService!

let deviceTransferServiceRef = DeviceTransferService()
let deviceTransferServiceRef: DeviceTransferService

let avatarHistorManagerRef = AvatarHistoryManager()

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

private override init() {
init(deviceTransferService: DeviceTransferService) {
self.deviceTransferServiceRef = deviceTransferService
self.pushRegistrationManagerRef = PushRegistrationManager()

super.init()
Expand Down
4 changes: 2 additions & 2 deletions Signal/DeviceTransfer/DeviceTransferService+Restore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ extension DeviceTransferService {
}

do {
try databaseStorage.keyFetcher.store(data: database.key)
try GRDBKeyFetcher(keychainStorage: keychainStorage).store(data: database.key)
} catch {
owsFailDebug("failed to restore database key")
return false
Expand Down Expand Up @@ -547,7 +547,7 @@ extension DeviceTransferService {
throw OWSAssertionError("No manifest database available")
}

try databaseStorage.keyFetcher.store(data: database.key)
try GRDBKeyFetcher(keychainStorage: keychainStorage).store(data: database.key)
GRDBDatabaseStorageAdapter.promoteTransferDirectoryToPrimary()
}

Expand Down
6 changes: 5 additions & 1 deletion Signal/DeviceTransfer/DeviceTransferService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,11 @@ class DeviceTransferService: NSObject {

// MARK: -

override init() {
let keychainStorage: any KeychainStorage

init(keychainStorage: any KeychainStorage) {
self.keychainStorage = keychainStorage

super.init()

SwiftSingletons.register(self)
Expand Down
1 change: 1 addition & 0 deletions SignalNSE/NSEEnvironment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ class NSEEnvironment: Dependencies {
databaseFileUrl: SDSDatabaseStorage.grdbDatabaseFileUrl,
keychainStorage: keychainStorage
)
databaseStorage.grdbStorage.setUpDatabasePathKVO()

didFinishDatabaseSetup = true

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public class GRDBDatabaseStorageAdapter: NSObject {
return primaryFolderName
}
if let primaryFolderName = CurrentAppContext().appUserDefaults().string(forKey: DirectoryMode.primaryFolderNameKey) {
// Make sure its also written to the file.
// Make sure it's also written to the file.
OWSFileSystem.ensureDirectoryExists(fileUrl.deletingLastPathComponent().path)
try? primaryFolderName.write(toFile: fileUrl.pathExtension, atomically: true, encoding: .utf8)
return primaryFolderName
Expand All @@ -97,7 +97,7 @@ public class GRDBDatabaseStorageAdapter: NSObject {

fileprivate static func writeStoredPrimaryFolderName(_ newPrimaryFolderName: String) {
CurrentAppContext().appUserDefaults().set(newPrimaryFolderName, forKey: DirectoryMode.primaryFolderNameKey)
// Make sure its also written to the file.
// Make sure it's also written to the file.
let fileUrl = storedPrimaryFolderNameFileUrl()
OWSFileSystem.ensureDirectoryExists(fileUrl.deletingLastPathComponent().path)
try? newPrimaryFolderName.write(toFile: fileUrl.pathExtension, atomically: true, encoding: .utf8)
Expand Down Expand Up @@ -139,9 +139,6 @@ public class GRDBDatabaseStorageAdapter: NSObject {
} catch {
throw error
}

super.init()
setUpDatabasePathKVO()
}

deinit {
Expand Down Expand Up @@ -207,7 +204,7 @@ public class GRDBDatabaseStorageAdapter: NSObject {

private var darwinToken: Int32?

func setUpDatabasePathKVO() {
public func setUpDatabasePathKVO() {
darwinToken = DarwinNotificationCenter.addObserver(
for: .primaryDBFolderNameDidChange,
queue: .main,
Expand Down Expand Up @@ -351,7 +348,7 @@ extension GRDBDatabaseStorageAdapter {
}

public static func promoteTransferDirectoryToPrimary() {
owsAssert(CurrentAppContext().isMainApp, "Only the main app can't swap databases")
owsAssert(CurrentAppContext().isMainApp, "Only the main app can swap databases")

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

let keychainStorage: any KeychainStorage
private let keychainStorage: any KeychainStorage

public init(keychainStorage: any KeychainStorage) {
self.keychainStorage = keychainStorage
}

func fetchString() throws -> String {
// Use a raw key spec, where the 96 hexadecimal digits are provided
Expand Down
1 change: 1 addition & 0 deletions SignalShareExtension/ShareViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public class ShareViewController: UIViewController, ShareViewDelegate, SAEFailed
self.showNotRegisteredView()
return
}
databaseStorage.grdbStorage.setUpDatabasePathKVO()

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

0 comments on commit 6efc1f2

Please sign in to comment.