From f9ae28de1dff506e5a49a55f5ec539b37f8cafc9 Mon Sep 17 00:00:00 2001 From: Matt Czech Date: Thu, 6 Nov 2025 16:39:38 -0600 Subject: [PATCH] [PM-26063] Consolidate shared service protocols in BitwardenKit (#2111) --- .../Platform/Services/ServiceContainer.swift | 6 +-- .../Core/Platform/Services/Services.swift | 21 --------- .../Utilities/ExternalLinksConstants.swift | 4 +- AuthenticatorShared/UI/Auth/AuthRouter.swift | 1 + .../TestHelpers/ServiceContainer.swift | 46 +++++++++++++++++++ .../Core/Platform/Services/Services.swift | 29 ++++++++++++ .../Platform/Services/ServiceContainer.swift | 8 ++-- .../Core/Platform/Services/Services.swift | 28 ----------- BitwardenShared/UI/Auth/AuthRouter.swift | 1 + .../MasterPasswordRepromptHelper.swift | 1 + 10 files changed, 88 insertions(+), 57 deletions(-) create mode 100644 BitwardenKit/Application/TestHelpers/ServiceContainer.swift create mode 100644 BitwardenKit/Core/Platform/Services/Services.swift diff --git a/AuthenticatorShared/Core/Platform/Services/ServiceContainer.swift b/AuthenticatorShared/Core/Platform/Services/ServiceContainer.swift index e4ac326f0..ca3c106c5 100644 --- a/AuthenticatorShared/Core/Platform/Services/ServiceContainer.swift +++ b/AuthenticatorShared/Core/Platform/Services/ServiceContainer.swift @@ -42,13 +42,13 @@ public class ServiceContainer: Services { let clientService: ClientService /// The service to get locally-specified configuration - let configService: ConfigService + public let configService: ConfigService /// The service used by the application to encrypt and decrypt items let cryptographyService: CryptographyService /// The service used by the application to report non-fatal errors. - let errorReporter: ErrorReporter + public let errorReporter: ErrorReporter /// The service used to export items. let exportItemsService: ExportItemsService @@ -69,7 +69,7 @@ public class ServiceContainer: Services { let stateService: StateService /// Provides the present time for TOTP Code Calculation. - let timeProvider: TimeProvider + public let timeProvider: TimeProvider /// The factory to create TOTP expiration managers. let totpExpirationManagerFactory: TOTPExpirationManagerFactory diff --git a/AuthenticatorShared/Core/Platform/Services/Services.swift b/AuthenticatorShared/Core/Platform/Services/Services.swift index e55e044a7..30e603db8 100644 --- a/AuthenticatorShared/Core/Platform/Services/Services.swift +++ b/AuthenticatorShared/Core/Platform/Services/Services.swift @@ -54,13 +54,6 @@ protocol HasCameraService { var cameraService: CameraService { get } } -/// Protocol for an object that provides a `ConfigService`. -/// -protocol HasConfigService { - /// The service to get server-specified configuration. - var configService: ConfigService { get } -} - /// Protocol for an object that provides a `CryptographyService` /// protocol HasCryptographyService { @@ -68,13 +61,6 @@ protocol HasCryptographyService { var cryptographyService: CryptographyService { get } } -/// Protocol for an object that provides an `ErrorReporter`. -/// -protocol HasErrorReporter { - /// The service used by the application to report non-fatal errors. - var errorReporter: ErrorReporter { get } -} - /// Protocol for an object that provides an `ExportItemsService`. /// protocol HasExportItemsService { @@ -117,13 +103,6 @@ protocol HasTOTPService { var totpService: TOTPService { get } } -/// Protocol for an object that provides a `TimeProvider`. -/// -protocol HasTimeProvider { - /// Provides the present time for TOTP Code Calculation. - var timeProvider: TimeProvider { get } -} - /// Protocol for an object that provides a `TOTPExpirationManagerFactory`. /// protocol HasTOTPExpirationManagerFactory { diff --git a/AuthenticatorShared/Core/Platform/Utilities/ExternalLinksConstants.swift b/AuthenticatorShared/Core/Platform/Utilities/ExternalLinksConstants.swift index 4c0f49798..f5a0edbe6 100644 --- a/AuthenticatorShared/Core/Platform/Utilities/ExternalLinksConstants.swift +++ b/AuthenticatorShared/Core/Platform/Utilities/ExternalLinksConstants.swift @@ -9,7 +9,9 @@ extension ExternalLinksConstants { // MARK: Properties /// A link to Apple's guide on backing up iPhone. - static let backupInformation = URL(string: "https://support.apple.com/guide/iphone/back-up-iphone-iph3ecf67d29/ios")! + static let backupInformation = URL( + string: "https://support.apple.com/guide/iphone/back-up-iphone-iph3ecf67d29/ios", + )! /// A link to the password manager app within the app store. static let passwordManagerLink = URL(string: "https://itunes.apple.com/app/id1137397744?mt=8")! diff --git a/AuthenticatorShared/UI/Auth/AuthRouter.swift b/AuthenticatorShared/UI/Auth/AuthRouter.swift index 220edd005..0077c9f29 100644 --- a/AuthenticatorShared/UI/Auth/AuthRouter.swift +++ b/AuthenticatorShared/UI/Auth/AuthRouter.swift @@ -1,3 +1,4 @@ +import BitwardenKit import Foundation // MARK: - AuthRouter diff --git a/BitwardenKit/Application/TestHelpers/ServiceContainer.swift b/BitwardenKit/Application/TestHelpers/ServiceContainer.swift new file mode 100644 index 000000000..5ec668d71 --- /dev/null +++ b/BitwardenKit/Application/TestHelpers/ServiceContainer.swift @@ -0,0 +1,46 @@ +import BitwardenKit +import BitwardenKitMocks + +/// The services provided by the test `ServiceContainer`. +/// +typealias Services = HasConfigService + & HasEnvironmentService + & HasErrorReporter + & HasTimeProvider + +/// A service container used for testing processors within `BitwardenKitTests`. +/// +class ServiceContainer: Services { + let configService: ConfigService + let environmentService: EnvironmentService + let errorReporter: ErrorReporter + let timeProvider: TimeProvider + + required init( + configService: ConfigService, + environmentService: EnvironmentService, + errorReporter: ErrorReporter, + timeProvider: TimeProvider, + ) { + self.configService = configService + self.environmentService = environmentService + self.errorReporter = errorReporter + self.timeProvider = timeProvider + } +} + +extension ServiceContainer { + static func withMocks( + configService: ConfigService = MockConfigService(), + environmentService: EnvironmentService = MockEnvironmentService(), + errorReporter: ErrorReporter = MockErrorReporter(), + timeProvider: TimeProvider = MockTimeProvider(.currentTime), + ) -> ServiceContainer { + self.init( + configService: configService, + environmentService: environmentService, + errorReporter: errorReporter, + timeProvider: timeProvider, + ) + } +} diff --git a/BitwardenKit/Core/Platform/Services/Services.swift b/BitwardenKit/Core/Platform/Services/Services.swift new file mode 100644 index 000000000..111d4883c --- /dev/null +++ b/BitwardenKit/Core/Platform/Services/Services.swift @@ -0,0 +1,29 @@ +// swiftlint:disable:this file_name + +/// Protocol for an object that provides a `ConfigService`. +/// +public protocol HasConfigService { + /// The service to get server-specified configuration. + var configService: ConfigService { get } +} + +/// Protocol for an object that provides an `EnvironmentService`. +/// +public protocol HasEnvironmentService { + /// The service used by the application to manage the environment settings. + var environmentService: EnvironmentService { get } +} + +/// Protocol for an object that provides an `ErrorReporter`. +/// +public protocol HasErrorReporter { + /// The service used by the application to report non-fatal errors. + var errorReporter: ErrorReporter { get } +} + +/// Protocol for an object that provides a `TimeProvider`. +/// +public protocol HasTimeProvider { + /// Provides the present time for TOTP Code Calculation. + var timeProvider: TimeProvider { get } +} diff --git a/BitwardenShared/Core/Platform/Services/ServiceContainer.swift b/BitwardenShared/Core/Platform/Services/ServiceContainer.swift index d3db2ded5..9ddd2892c 100644 --- a/BitwardenShared/Core/Platform/Services/ServiceContainer.swift +++ b/BitwardenShared/Core/Platform/Services/ServiceContainer.swift @@ -70,16 +70,16 @@ public class ServiceContainer: Services { // swiftlint:disable:this type_body_le let clientService: ClientService /// The service to get server-specified configuration - let configService: ConfigService + public let configService: ConfigService /// The service used by the application to manage the environment settings. - let environmentService: EnvironmentService + public let environmentService: EnvironmentService /// A helper for building an error report containing the details of an error that occurred. let errorReportBuilder: ErrorReportBuilder /// The service used by the application to report non-fatal errors. - let errorReporter: ErrorReporter + public let errorReporter: ErrorReporter /// The service used to record and send events. let eventService: EventService @@ -164,7 +164,7 @@ public class ServiceContainer: Services { // swiftlint:disable:this type_body_le let textAutofillHelperFactory: TextAutofillHelperFactory /// Provides the present time for TOTP Code Calculation. - let timeProvider: TimeProvider + public let timeProvider: TimeProvider /// The service used by the application to manage account access tokens. let tokenService: TokenService diff --git a/BitwardenShared/Core/Platform/Services/Services.swift b/BitwardenShared/Core/Platform/Services/Services.swift index f1faff314..02deecb2e 100644 --- a/BitwardenShared/Core/Platform/Services/Services.swift +++ b/BitwardenShared/Core/Platform/Services/Services.swift @@ -159,13 +159,6 @@ protocol HasClientService { var clientService: ClientService { get } } -/// Protocol for an object that provides a `ConfigService`. -/// -protocol HasConfigService { - /// The service to get server-specified configuration. - var configService: ConfigService { get } -} - /// Protocol for an object that provides a `DeviceAPIService`. /// protocol HasDeviceAPIService { @@ -173,13 +166,6 @@ protocol HasDeviceAPIService { var deviceAPIService: DeviceAPIService { get } } -/// Protocol for an object that provides an `EnvironmentService`. -/// -protocol HasEnvironmentService { - /// The service used by the application to manage the environment settings. - var environmentService: EnvironmentService { get } -} - /// Protocol for an object that provides an `ErrorReportBuilder`. /// protocol HasErrorReportBuilder { @@ -187,13 +173,6 @@ protocol HasErrorReportBuilder { var errorReportBuilder: ErrorReportBuilder { get } } -/// Protocol for an object that provides an `ErrorReporter`. -/// -protocol HasErrorReporter { - /// The service used by the application to report non-fatal errors. - var errorReporter: ErrorReporter { get } -} - /// Protocol for an object that provides an `EventService`. /// protocol HasEventService { @@ -368,13 +347,6 @@ protocol HasTextAutofillHelperFactory { var textAutofillHelperFactory: TextAutofillHelperFactory { get } } -/// Protocol for an object that provides a `TimeProvider`. -/// -protocol HasTimeProvider { - /// Provides the present time for TOTP Code Calculation. - var timeProvider: TimeProvider { get } -} - /// Protocol for an object that provides a `TOTPExpirationManagerFactory`. /// protocol HasTOTPExpirationManagerFactory { diff --git a/BitwardenShared/UI/Auth/AuthRouter.swift b/BitwardenShared/UI/Auth/AuthRouter.swift index 7b41e35a4..7acc062c5 100644 --- a/BitwardenShared/UI/Auth/AuthRouter.swift +++ b/BitwardenShared/UI/Auth/AuthRouter.swift @@ -1,3 +1,4 @@ +import BitwardenKit import Foundation // MARK: - AuthManager diff --git a/BitwardenShared/UI/Vault/Helpers/MasterPasswordRepromptHelper.swift b/BitwardenShared/UI/Vault/Helpers/MasterPasswordRepromptHelper.swift index f7c14bcf6..bdb4bd46b 100644 --- a/BitwardenShared/UI/Vault/Helpers/MasterPasswordRepromptHelper.swift +++ b/BitwardenShared/UI/Vault/Helpers/MasterPasswordRepromptHelper.swift @@ -1,3 +1,4 @@ +import BitwardenKit import BitwardenSdk // MARK: - MasterPasswordRepromptHelper