Add a hook point to update a restored Client. (#6441)

* Add a hook point to update a restored Client.

* Add a ClientBuilderFactory to test the ClientFactory.

* Update the Enterprise submodule.
This commit is contained in:
Doug
2026-10-29 19:17:02 +00:00
committed by GitHub
parent 296ceba684
commit 0050c56f61
12 changed files with 102 additions and 22 deletions
+6
View File
@@ -745,6 +745,7 @@
71C1347F23868324A4F43940 /* NavigationModule.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A22A05E472533ED3C5A31B3 /* NavigationModule.swift */; };
723382158BFD148984201266 /* HomeserverHistoryManagerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB9292E3516E65DED7130684 /* HomeserverHistoryManagerTests.swift */; };
7254FB2EFDD43BC8BB7A1213 /* SecurityAndPrivacyScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = B4AE42C19EDE64B7CB7BE4D0 /* SecurityAndPrivacyScreen.swift */; };
726493FDB59E0D509844F364 /* ClientBuilderFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = 41DBFF447FC30BEAACD61182 /* ClientBuilderFactory.swift */; };
726AA74DF4E5EFCEBD78CE3F /* RoomMembersFlowCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5A70B03471F6027C90EE868C /* RoomMembersFlowCoordinator.swift */; };
72D2298DE695A6797CDA1A2A /* SpaceScreenViewModelTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 18B223FA339BF53085328DEE /* SpaceScreenViewModelTests.swift */; };
733E2B19AB1FDA3B93293A28 /* AppLockSetupPINScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = D3F275432954C8C6B1B7D966 /* AppLockSetupPINScreen.swift */; };
@@ -1585,6 +1586,7 @@
F118DD449066E594F63C697D /* RoomMemberProxyProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 32B5E17028C02DFA7DDA3931 /* RoomMemberProxyProtocol.swift */; };
F12F6BED7B6D7EE4BEE55039 /* PlainMentionBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8AE78FA0011E07920AE83135 /* PlainMentionBuilder.swift */; };
F138A807F969044AB0E41AFD /* LocationSharingSettingsScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4F59B8EEE7A060438C96BB77 /* LocationSharingSettingsScreen.swift */; };
F1503120EC935FD654DAF4D2 /* ClientBuilderFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = 41DBFF447FC30BEAACD61182 /* ClientBuilderFactory.swift */; };
F16EED85E30B83878FBC6629 /* JoinedMembersBadgeView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F320003F490B11F808ECC5E9 /* JoinedMembersBadgeView.swift */; };
F18CA61A58C77C84F551B8E7 /* GeneratedMocks.swift in Sources */ = {isa = PBXBuildFile; fileRef = 57916A1578D8043BB0795441 /* GeneratedMocks.swift */; };
F1C68F64FC8A66B6B9510BF7 /* ManageRoomMemberSheetView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1B30CD19ED6243FEDFBA8400 /* ManageRoomMemberSheetView.swift */; };
@@ -2226,6 +2228,7 @@
41A8571A8A071FB41778C016 /* ExtensionLogger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExtensionLogger.swift; sourceTree = "<group>"; };
41BB37D96C3EA18F3CE8675D /* RoomDirectorySearchScreenModels.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RoomDirectorySearchScreenModels.swift; sourceTree = "<group>"; };
41D041A857614A9AE13C7795 /* RoomChangePermissionsScreenViewModelTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RoomChangePermissionsScreenViewModelTests.swift; sourceTree = "<group>"; };
41DBFF447FC30BEAACD61182 /* ClientBuilderFactory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClientBuilderFactory.swift; sourceTree = "<group>"; };
41F69820F717FF11C6309916 /* AuthenticationStartScreenCoordinator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthenticationStartScreenCoordinator.swift; sourceTree = "<group>"; };
421E716C521F96D24ECE69B3 /* NoticeRoomTimelineItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NoticeRoomTimelineItem.swift; sourceTree = "<group>"; };
421FA93BCC2840E66E4F306F /* NotificationSettingsScreenViewModelProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationSettingsScreenViewModelProtocol.swift; sourceTree = "<group>"; };
@@ -5779,6 +5782,7 @@
isa = PBXGroup;
children = (
0724EBDFE8BB4C9E5547C57D /* Client.swift */,
41DBFF447FC30BEAACD61182 /* ClientBuilderFactory.swift */,
996AF8CF3FCF19B62F005C57 /* ClientFactory.swift */,
176A581FDC61737676782607 /* ClientFactoryProtocol.swift */,
18F2958E6D247AE2516BEEE8 /* ClientProxy.swift */,
@@ -8463,6 +8467,7 @@
A8E324E700E596E36B0A311B /* BootDetectionManager.swift in Sources */,
9A3B0CDF097E3838FB1B9595 /* Bundle.swift in Sources */,
9295F1F5E04484E10780BCE8 /* CharacterSet.swift in Sources */,
F1503120EC935FD654DAF4D2 /* ClientBuilderFactory.swift in Sources */,
DA6E57CB533213D438001A31 /* ClientFactory.swift in Sources */,
9AF73FA23C86B45801EE2949 /* ClientFactoryHook.swift in Sources */,
039FE1757084092146C0B616 /* ClientFactoryProtocol.swift in Sources */,
@@ -8993,6 +8998,7 @@
2A4F8B76E1F99DE95D5867E6 /* ClassicAppManagerMock.swift in Sources */,
696D34FAA9A4E00435DDAF39 /* ClassicAppMediaLoader.swift in Sources */,
A52090A4FE0DB826578DFC03 /* Client.swift in Sources */,
726493FDB59E0D509844F364 /* ClientBuilderFactory.swift in Sources */,
2252975DECF9B72F83709C4A /* ClientBuilderSDKMock.swift in Sources */,
4AC0B3634BDA6817EC7DF539 /* ClientFactory.swift in Sources */,
4FBC9E416582352432EBAB7F /* ClientFactoryHook.swift in Sources */,
@@ -11,7 +11,8 @@ import MatrixRustSDK
nonisolated protocol ClientFactoryHookProtocol: Sendable {
func configure(_ builder: ClientBuilder, toRestore session: Session?) -> ClientBuilder
func updateAuthenticationClient(_ client: ClientProtocol) -> ClientProtocol
func updateAuthenticationClient(_ client: ClientProtocol) async -> ClientProtocol
func updateRestoredClient(_ client: ClientProtocol) async -> ClientProtocol
}
nonisolated struct DefaultClientFactoryHook: ClientFactoryHookProtocol {
@@ -19,7 +20,11 @@ nonisolated struct DefaultClientFactoryHook: ClientFactoryHookProtocol {
builder
}
func updateAuthenticationClient(_ client: ClientProtocol) -> ClientProtocol {
func updateAuthenticationClient(_ client: ClientProtocol) async -> ClientProtocol {
client
}
func updateRestoredClient(_ client: ClientProtocol) async -> ClientProtocol {
client
}
}
@@ -260,12 +260,6 @@ final nonisolated class AppSettings: @unchecked Sendable {
/// The name allocated by the bug report server
private(set) var bugReportApplicationID = "element-x-ios"
// MARK: - Content scanner
/// The base URL of the content scanner server used to scan media before it is downloaded.
/// `nil` when content scanning is disabled.
let contentScannerURL: RemotePreference<URL?> = .init(nil)
// MARK: - Analytics
/// The configuration to use for analytics. Set to `nil` to disable analytics.
@@ -2209,6 +2209,37 @@ nonisolated class ClassicAppManagerMock: ClassicAppManagerProtocol, @unchecked S
}
}
}
nonisolated class ClientBuilderFactoryMock: ClientBuilderFactoryProtocol, @unchecked Sendable {
//MARK: - makeBuilder
private let makeBuilderCallsCountLock = NSLock()
private nonisolated(unsafe) var makeBuilderUnderlyingCallsCount = 0
var makeBuilderCallsCount: Int {
get { makeBuilderCallsCountLock.withLock { makeBuilderUnderlyingCallsCount } }
set { makeBuilderCallsCountLock.withLock { makeBuilderUnderlyingCallsCount = newValue } }
}
var makeBuilderCalled: Bool {
return makeBuilderCallsCount > 0
}
private let makeBuilderReturnValueLock = NSLock()
private nonisolated(unsafe) var makeBuilderUnderlyingReturnValue: ClientBuilder!
var makeBuilderReturnValue: ClientBuilder! {
get { makeBuilderReturnValueLock.withLock { makeBuilderUnderlyingReturnValue } }
set { makeBuilderReturnValueLock.withLock { makeBuilderUnderlyingReturnValue = newValue } }
}
nonisolated(unsafe) var makeBuilderClosure: (() -> ClientBuilder)?
func makeBuilder() -> ClientBuilder {
makeBuilderCallsCountLock.withLock { makeBuilderUnderlyingCallsCount += 1 }
if let makeBuilderClosure = makeBuilderClosure {
return makeBuilderClosure()
} else {
return makeBuilderReturnValue
}
}
}
nonisolated class ClientFactoryMock: ClientFactoryProtocol, @unchecked Sendable {
//MARK: - makeAuthenticationClient
@@ -11,7 +11,7 @@ import MatrixRustSDKMocks
nonisolated extension ClientBuilderSDKMock {
struct Configuration {
var disableWellKnownLookup = false
var client = ClientSDKMock(.init())
}
enum MockError: Error { case generic }
@@ -19,6 +19,29 @@ nonisolated extension ClientBuilderSDKMock {
convenience init(_ configuration: Configuration) {
self.init()
autoEnableBackupsAutoEnableBackupsClosure = { [unowned self] _ in self }
autoEnableCrossSigningAutoEnableCrossSigningClosure = { [unowned self] _ in self }
backupDownloadStrategyBackupDownloadStrategyClosure = { [unowned self] _ in self }
crossProcessLockConfigCrossProcessLockConfigClosure = { [unowned self] _ in self }
decryptionSettingsDecryptionSettingsClosure = { [unowned self] _ in self }
disableWellKnownLookupDisableWellKnownLookupClosure = { [unowned self] _ in self }
dmRoomDefinitionDmRoomDefinitionClosure = { [unowned self] _ in self }
enableAutomaticBackPaginationEnableAutomaticBackPaginationClosure = { [unowned self] _ in self }
enableShareHistoryOnInviteEnableShareHistoryOnInviteClosure = { [unowned self] _ in self }
homeserverUrlUrlClosure = { [unowned self] _ in self }
inMemoryStoreClosure = { [unowned self] in self }
proxyUrlClosure = { [unowned self] _ in self }
requestConfigConfigClosure = { [unowned self] _ in self }
roomKeyRecipientStrategyStrategyClosure = { [unowned self] _ in self }
serverNameOrHomeserverUrlServerNameOrUrlClosure = { [unowned self] _ in self }
setSessionDelegateSessionDelegateClosure = { [unowned self] _ in self }
slidingSyncVersionBuilderVersionBuilderClosure = { [unowned self] _ in self }
sqliteStoreConfigClosure = { [unowned self] _ in self }
systemIsMemoryConstrainedClosure = { [unowned self] in self }
threadsEnabledEnabledThreadSubscriptionsClosure = { [unowned self] _, _ in self }
userAgentUserAgentClosure = { [unowned self] _ in self }
withSearchIndexStorePathKeyClosure = { [unowned self] _, _ in self }
buildClosure = { configuration.client }
}
}
@@ -276,7 +276,7 @@ class AuthenticationService: AuthenticationServiceProtocol {
appHooks: appHooks)
try await appHooks.remoteSettingsHook.initializeCache(using: client, applyingTo: appSettings).get()
return appHooks.clientFactoryHook.updateAuthenticationClient(client)
return await appHooks.clientFactoryHook.updateAuthenticationClient(client)
}
private func rotateSessionDirectory() {
@@ -0,0 +1,20 @@
//
// Copyright 2026 Element Creations Ltd.
//
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
// Please see LICENSE files in the repository root for full details.
//
import MatrixRustSDK
// sourcery: AutoMockable
nonisolated protocol ClientBuilderFactoryProtocol {
func makeBuilder() -> ClientBuilder
}
/// A wrapper around `ClientBuilder` to allow for mocked builders to be injected into tests.
nonisolated struct ClientBuilderFactory: ClientBuilderFactoryProtocol {
func makeBuilder() -> ClientBuilder {
ClientBuilder()
}
}
@@ -11,6 +11,12 @@ import MatrixRustSDK
/// A wrapper around `ClientBuilder` to allow for mocked clients to be injected into tests.
nonisolated struct ClientFactory: ClientFactoryProtocol {
private let clientBuilderFactory: ClientBuilderFactoryProtocol
init(clientBuilderFactory: ClientBuilderFactoryProtocol = ClientBuilderFactory()) {
self.clientBuilderFactory = clientBuilderFactory
}
// MARK: Authentication
#if IS_MAIN_APP
@@ -115,7 +121,7 @@ nonisolated struct ClientFactory: ClientFactoryProtocol {
requestTimeout: UInt64? = 30000,
maxRequestRetryTime: UInt64? = nil,
threadsEnabled: Bool) -> ClientBuilder {
var builder = ClientBuilder()
var builder = clientBuilderFactory.makeBuilder()
.crossProcessLockConfig(crossProcessLockConfig: .multiProcess(holderName: InfoPlistReader.main.bundleIdentifier))
.setSessionDelegate(sessionDelegate: sessionDelegate)
.userAgent(userAgent: UserAgentBuilder.makeASCIIUserAgent())
@@ -161,13 +167,15 @@ nonisolated struct ClientFactory: ClientFactoryProtocol {
private func build(_ builder: ClientBuilder,
for mode: BuildMode,
appHooks: AppHooks) async throws -> ClientProtocol {
let client: ClientProtocol = try await appHooks.clientFactoryHook.configure(builder, toRestore: mode.session).build()
var client: ClientProtocol = try await appHooks.clientFactoryHook.configure(builder, toRestore: mode.session).build()
switch mode {
case .authentication:
// The hook is run in the authentication service.
break
case .restoration(let session, let roomLoadSettings):
try await client.restoreSessionWith(session: session, roomLoadSettings: roomLoadSettings)
client = await appHooks.clientFactoryHook.updateRestoredClient(client)
case .classicAppAccount:
break
}
@@ -163,9 +163,7 @@ class ClientProxy: ClientProxyProtocol {
// Route media downloads through a content scanner when one has been configured for the server,
// and expose a proxy for the active scanning of content in the timeline.
if let contentScannerURL = userSettings.app.contentScannerURL.publisher.value, let client = client as? Client {
let scanner = ContentScanner(scannerUrl: contentScannerURL.absoluteString)
await client.setContentScanner(contentScanner: scanner)
if let client = client as? Client, let scanner = await client.contentScanner() {
contentScanner = ContentScannerProxy(contentScanner: scanner, client: client)
} else {
contentScanner = nil
-6
View File
@@ -76,12 +76,6 @@ final nonisolated class NSEUserSession: NSEUserSessionProtocol {
}
delegateHandle = try baseClient.setDelegate(delegate: ClientDelegateWrapper())
// Inject the content scanner so the SDK gates the media it downloads whilst building the notification.
if let contentScannerURL = userSettings.app.contentScannerURL.publisher.value {
let contentScanner = ContentScanner(scannerUrl: contentScannerURL.absoluteString)
await baseClient.setContentScanner(contentScanner: contentScanner)
}
notificationClient = try await baseClient.notificationClient(processSetup: .multipleProcesses)
}
+1
View File
@@ -125,6 +125,7 @@ targets:
- path: ../../ElementX/Sources/Services/AlertTones/NotificationTone.swift
- path: ../../ElementX/Sources/Services/AlertTones/NotificationToneManager.swift
- path: ../../ElementX/Sources/Services/AlertTones/NotificationToneManagerProtocol.swift
- path: ../../ElementX/Sources/Services/Client/ClientBuilderFactory.swift
- path: ../../ElementX/Sources/Services/Client/ClientFactory.swift
- path: ../../ElementX/Sources/Services/Client/ClientFactoryProtocol.swift
- path: ../../ElementX/Sources/Services/ElementCall/ElementCallServiceConstants.swift