diff --git a/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/Scheduler/DataBrokerProtectionProcessor.swift b/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/Scheduler/DataBrokerProtectionProcessor.swift deleted file mode 100644 index 189a796150..0000000000 --- a/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/Scheduler/DataBrokerProtectionProcessor.swift +++ /dev/null @@ -1,162 +0,0 @@ -// -// DataBrokerProtectionProcessor.swift -// -// Copyright © 2023 DuckDuckGo. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// - -import Foundation -import Common -import BrowserServicesKit - -final class DataBrokerProtectionProcessor { - private let database: DataBrokerProtectionRepository - private let config: DataBrokerProtectionProcessorConfiguration - private let jobRunnerProvider: JobRunnerProvider - private let notificationCenter: NotificationCenter - private let operationQueue: OperationQueue - private var pixelHandler: EventMapping - private let userNotificationService: DataBrokerProtectionUserNotificationService - private let engagementPixels: DataBrokerProtectionEngagementPixels - private let eventPixels: DataBrokerProtectionEventPixels - - init(database: DataBrokerProtectionRepository, - config: DataBrokerProtectionProcessorConfiguration = DataBrokerProtectionProcessorConfiguration(), - jobRunnerProvider: JobRunnerProvider, - notificationCenter: NotificationCenter = NotificationCenter.default, - pixelHandler: EventMapping, - userNotificationService: DataBrokerProtectionUserNotificationService) { - - self.database = database - self.config = config - self.jobRunnerProvider = jobRunnerProvider - self.notificationCenter = notificationCenter - self.operationQueue = OperationQueue() - self.pixelHandler = pixelHandler - self.userNotificationService = userNotificationService - self.engagementPixels = DataBrokerProtectionEngagementPixels(database: database, handler: pixelHandler) - self.eventPixels = DataBrokerProtectionEventPixels(database: database, handler: pixelHandler) - } - - // MARK: - Public functions - func startManualScans(showWebView: Bool = false, - completion: ((DataBrokerProtectionAgentErrorCollection?) -> Void)? = nil) { - - operationQueue.cancelAllOperations() - runOperations(operationType: .scan, - priorityDate: nil, - showWebView: showWebView) { errors in - os_log("Scans done", log: .dataBrokerProtection) - completion?(errors) - self.calculateMisMatches() - } - } - - private func calculateMisMatches() { - let mismatchUseCase = MismatchCalculatorUseCase(database: database, pixelHandler: pixelHandler) - mismatchUseCase.calculateMismatches() - } - - func runAllOptOutOperations(showWebView: Bool = false, - completion: ((DataBrokerProtectionAgentErrorCollection?) -> Void)? = nil) { - operationQueue.cancelAllOperations() - runOperations(operationType: .optOut, - priorityDate: nil, - showWebView: showWebView) { errors in - os_log("Optouts done", log: .dataBrokerProtection) - completion?(errors) - } - } - - func runQueuedOperations(showWebView: Bool = false, - completion: ((DataBrokerProtectionAgentErrorCollection?) -> Void)? = nil ) { - runOperations(operationType: .all, - priorityDate: Date(), - showWebView: showWebView) { errors in - os_log("Queued operations done", log: .dataBrokerProtection) - completion?(errors) - } - } - - func runAllOperations(showWebView: Bool = false, - completion: ((DataBrokerProtectionAgentErrorCollection?) -> Void)? = nil ) { - runOperations(operationType: .all, - priorityDate: nil, - showWebView: showWebView) { errors in - os_log("Queued operations done", log: .dataBrokerProtection) - completion?(errors) - } - } - - func stopAllOperations() { - operationQueue.cancelAllOperations() - } - - // MARK: - Private functions - private func runOperations(operationType: OperationType, - priorityDate: Date?, - showWebView: Bool, - completion: @escaping ((DataBrokerProtectionAgentErrorCollection?) -> Void)) { - - self.operationQueue.maxConcurrentOperationCount = config.concurrentOperationsFor(operationType) - // Before running new operations we check if there is any updates to the broker files. - if let vault = try? DataBrokerProtectionSecureVaultFactory.makeVault(reporter: DataBrokerProtectionSecureVaultErrorReporter.shared) { - let brokerUpdater = DefaultDataBrokerProtectionBrokerUpdater(vault: vault, pixelHandler: pixelHandler) - brokerUpdater.checkForUpdatesInBrokerJSONFiles() - } - - // This will fire the DAU/WAU/MAU pixels, - engagementPixels.fireEngagementPixel() - // This will try to fire the event weekly report pixels - eventPixels.tryToFireWeeklyPixels() - - let operations: [DataBrokerOperation] - - do { - // Note: The next task in this project will inject the dependencies & builder into our new 'QueueManager' type - - let dependencies = DefaultDataBrokerOperationDependencies(database: database, - brokerTimeInterval: config.intervalBetweenSameBrokerOperations, - runnerProvider: jobRunnerProvider, - notificationCenter: notificationCenter, - pixelHandler: pixelHandler, - userNotificationService: userNotificationService) - - operations = try DefaultDataBrokerOperationsCreator().operations(forOperationType: operationType, - withPriorityDate: priorityDate, - showWebView: showWebView, - operationDependencies: dependencies) - - for operation in operations { - operationQueue.addOperation(operation) - } - } catch { - os_log("DataBrokerProtectionProcessor error: runOperations, error: %{public}@", log: .error, error.localizedDescription) - operationQueue.addBarrierBlock { - completion(DataBrokerProtectionAgentErrorCollection(oneTimeError: error)) - } - return - } - - operationQueue.addBarrierBlock { - let operationErrors = operations.compactMap { $0.error } - let errorCollection = operationErrors.count != 0 ? DataBrokerProtectionAgentErrorCollection(operationErrors: operationErrors) : nil - completion(errorCollection) - } - } - - deinit { - os_log("Deinit DataBrokerProtectionProcessor", log: .dataBrokerProtection) - } -} diff --git a/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/Scheduler/DataBrokerProtectionQueueManager.swift b/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/Scheduler/DataBrokerProtectionQueueManager.swift index 4e53361018..5eed4ba23d 100644 --- a/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/Scheduler/DataBrokerProtectionQueueManager.swift +++ b/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/Scheduler/DataBrokerProtectionQueueManager.swift @@ -30,8 +30,8 @@ extension OperationQueue: DataBrokerProtectionOperationQueue {} enum DataBrokerProtectionQueueMode { case idle - case immediate(completion: ((DataBrokerProtectionSchedulerErrorCollection?) -> Void)?) - case scheduled(completion: ((DataBrokerProtectionSchedulerErrorCollection?) -> Void)?) + case immediate(completion: ((DataBrokerProtectionAgentErrorCollection?) -> Void)?) + case scheduled(completion: ((DataBrokerProtectionAgentErrorCollection?) -> Void)?) func canBeInterruptedBy(newMode: DataBrokerProtectionQueueMode) -> Bool { switch (self, newMode) { @@ -54,10 +54,10 @@ protocol DataBrokerProtectionQueueManager { func startImmediateOperationsIfPermitted(showWebView: Bool, operationDependencies: DataBrokerOperationDependencies, - completion: ((DataBrokerProtectionSchedulerErrorCollection?) -> Void)?) + completion: ((DataBrokerProtectionAgentErrorCollection?) -> Void)?) func startScheduledOperationsIfPermitted(showWebView: Bool, operationDependencies: DataBrokerOperationDependencies, - completion: ((DataBrokerProtectionSchedulerErrorCollection?) -> Void)?) + completion: ((DataBrokerProtectionAgentErrorCollection?) -> Void)?) func stopAllOperations() } @@ -85,7 +85,7 @@ final class DefaultDataBrokerProtectionQueueManager: DataBrokerProtectionQueueMa func startImmediateOperationsIfPermitted(showWebView: Bool, operationDependencies: DataBrokerOperationDependencies, - completion: ((DataBrokerProtectionSchedulerErrorCollection?) -> Void)?) { + completion: ((DataBrokerProtectionAgentErrorCollection?) -> Void)?) { let newMode = DataBrokerProtectionQueueMode.immediate(completion: completion) startOperationsIfPermitted(forNewMode: newMode, @@ -99,7 +99,7 @@ final class DefaultDataBrokerProtectionQueueManager: DataBrokerProtectionQueueMa func startScheduledOperationsIfPermitted(showWebView: Bool, operationDependencies: DataBrokerOperationDependencies, - completion: ((DataBrokerProtectionSchedulerErrorCollection?) -> Void)?) { + completion: ((DataBrokerProtectionAgentErrorCollection?) -> Void)?) { let newMode = DataBrokerProtectionQueueMode.scheduled(completion: completion) startOperationsIfPermitted(forNewMode: newMode, type: .all, @@ -119,7 +119,7 @@ private extension DefaultDataBrokerProtectionQueueManager { type: OperationType, showWebView: Bool, operationDependencies: DataBrokerOperationDependencies, - completion: ((DataBrokerProtectionSchedulerErrorCollection?) -> Void)?) { + completion: ((DataBrokerProtectionAgentErrorCollection?) -> Void)?) { guard mode.canBeInterruptedBy(newMode: newMode) else { completion?(nil) @@ -149,7 +149,7 @@ private extension DefaultDataBrokerProtectionQueueManager { priorityDate: Date? = nil, showWebView: Bool, operationDependencies: DataBrokerOperationDependencies, - completion: ((DataBrokerProtectionSchedulerErrorCollection?) -> Void)?) { + completion: ((DataBrokerProtectionAgentErrorCollection?) -> Void)?) { // Update broker files if applicable brokerUpdater?.checkForUpdatesInBrokerJSONFiles() @@ -173,7 +173,7 @@ private extension DefaultDataBrokerProtectionQueueManager { } } catch { os_log("DataBrokerProtectionProcessor error: addOperations, error: %{public}@", log: .error, error.localizedDescription) - completion?(DataBrokerProtectionSchedulerErrorCollection(oneTimeError: error)) + completion?(DataBrokerProtectionAgentErrorCollection(oneTimeError: error)) return } @@ -183,8 +183,8 @@ private extension DefaultDataBrokerProtectionQueueManager { } } - func errorCollection() -> DataBrokerProtectionSchedulerErrorCollection? { - return operationErrors.count != 0 ? DataBrokerProtectionSchedulerErrorCollection(operationErrors: operationErrors) : nil + func errorCollection() -> DataBrokerProtectionAgentErrorCollection? { + return operationErrors.count != 0 ? DataBrokerProtectionAgentErrorCollection(operationErrors: operationErrors) : nil } func firePixels(operationDependencies: DataBrokerOperationDependencies) { diff --git a/LocalPackages/DataBrokerProtection/Tests/DataBrokerProtectionTests/DataBrokerProtectionQueueManagerTests.swift b/LocalPackages/DataBrokerProtection/Tests/DataBrokerProtectionTests/DataBrokerProtectionQueueManagerTests.swift index e8b93fa0dd..5ff39c7108 100644 --- a/LocalPackages/DataBrokerProtection/Tests/DataBrokerProtectionTests/DataBrokerProtectionQueueManagerTests.swift +++ b/LocalPackages/DataBrokerProtection/Tests/DataBrokerProtectionTests/DataBrokerProtectionQueueManagerTests.swift @@ -63,7 +63,7 @@ final class DataBrokerProtectionQueueManagerTests: XCTestCase { let mockOperationsWithError = [3, 4].map { MockDataBrokerOperation(id: $0, operationType: .scan, errorDelegate: sut, shouldError: true, shouldSleep: false) } mockOperationsCreator.operationCollections = mockOperations + mockOperationsWithError let expectation = expectation(description: "Expected errors to be returned in completion") - var errorCollection: DataBrokerProtectionSchedulerErrorCollection! + var errorCollection: DataBrokerProtectionAgentErrorCollection! let expectedConcurrentOperations = DataBrokerProtectionProcessorConfiguration().concurrentOperationsFor(.scan) // When @@ -89,7 +89,7 @@ final class DataBrokerProtectionQueueManagerTests: XCTestCase { let mockOperationsWithError = [3, 4].map { MockDataBrokerOperation(id: $0, operationType: .scan, errorDelegate: sut, shouldError: true, shouldSleep: false) } mockOperationsCreator.operationCollections = mockOperations + mockOperationsWithError let expectation = expectation(description: "Expected errors to be returned in completion") - var errorCollection: DataBrokerProtectionSchedulerErrorCollection! + var errorCollection: DataBrokerProtectionAgentErrorCollection! let expectedConcurrentOperations = DataBrokerProtectionProcessorConfiguration().concurrentOperationsFor(.all) // When @@ -114,7 +114,7 @@ final class DataBrokerProtectionQueueManagerTests: XCTestCase { var mockOperations = (1...5).map { MockDataBrokerOperation(id: $0, operationType: .scan, errorDelegate: sut) } let mockOperationsWithError = (6...10).map { MockDataBrokerOperation(id: $0, operationType: .scan, errorDelegate: sut, shouldError: true) } mockOperationsCreator.operationCollections = mockOperations + mockOperationsWithError - var errorCollection: DataBrokerProtectionSchedulerErrorCollection! + var errorCollection: DataBrokerProtectionAgentErrorCollection! // When sut.startImmediateOperationsIfPermitted(showWebView: false, operationDependencies: mockDependencies) { errors in @@ -147,7 +147,7 @@ final class DataBrokerProtectionQueueManagerTests: XCTestCase { var mockOperations = (1...5).map { MockDataBrokerOperation(id: $0, operationType: .scan, errorDelegate: sut) } var mockOperationsWithError = (6...10).map { MockDataBrokerOperation(id: $0, operationType: .scan, errorDelegate: sut, shouldError: true) } mockOperationsCreator.operationCollections = mockOperations + mockOperationsWithError - var errorCollection: DataBrokerProtectionSchedulerErrorCollection! + var errorCollection: DataBrokerProtectionAgentErrorCollection! // When sut.startImmediateOperationsIfPermitted(showWebView: false, operationDependencies: mockDependencies) { _ in } @@ -183,7 +183,7 @@ final class DataBrokerProtectionQueueManagerTests: XCTestCase { mismatchCalculator: mockMismatchCalculator, brokerUpdater: mockUpdater) let expectation = expectation(description: "Expected completion to be called") - var errorCollection: DataBrokerProtectionSchedulerErrorCollection! + var errorCollection: DataBrokerProtectionAgentErrorCollection! // When sut.startImmediateOperationsIfPermitted(showWebView: false, @@ -207,7 +207,7 @@ final class DataBrokerProtectionQueueManagerTests: XCTestCase { let mockOperationsWithError = (6...10).map { MockDataBrokerOperation(id: $0, operationType: .scan, errorDelegate: sut, shouldError: true) } mockOperationsCreator.operationCollections = mockOperations + mockOperationsWithError let expectation = expectation(description: "Expected completion to be called") - var errorCollection: DataBrokerProtectionSchedulerErrorCollection! + var errorCollection: DataBrokerProtectionAgentErrorCollection! // When sut.startImmediateOperationsIfPermitted(showWebView: false,