Skip to content

Commit

Permalink
Run opt outs when profile is saved
Browse files Browse the repository at this point in the history
  • Loading branch information
quanganhdo committed Oct 8, 2024
1 parent 49a67a6 commit bdb228d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public final class DataBrokerProtectionAgentManager {
activityScheduler.startScheduler()
didStartActivityScheduler = true
fireMonitoringPixels()
queueManager.startScheduledOperationsIfPermitted(showWebView: false, operationDependencies: operationDependencies, completion: nil)
queueManager.startScheduledOperationsIfPermitted(showWebView: false, operationDependencies: operationDependencies, errorHandler: nil, completion: nil)

/// Monitors entitlement changes every 60 minutes to optimize system performance and resource utilization by avoiding unnecessary operations when entitlement is invalid.
/// While keeping the agent active with invalid entitlement has no significant risk, setting the monitoring interval at 60 minutes is a good balance to minimize backend checks.
Expand Down Expand Up @@ -207,15 +207,20 @@ extension DataBrokerProtectionAgentManager {
extension DataBrokerProtectionAgentManager: DataBrokerProtectionBackgroundActivitySchedulerDelegate {

public func dataBrokerProtectionBackgroundActivitySchedulerDidTrigger(_ activityScheduler: DataBrokerProtection.DataBrokerProtectionBackgroundActivityScheduler, completion: (() -> Void)?) {
startScheduledOperations(completion: completion)
}

func startScheduledOperations(completion: (() -> Void)?) {
fireMonitoringPixels()
queueManager.startScheduledOperationsIfPermitted(showWebView: false, operationDependencies: operationDependencies) { _ in
// no-op
} completion: {
completion?()
}
}
}

extension DataBrokerProtectionAgentManager: DataBrokerProtectionAgentAppEvents {

public func profileSaved() {
let backgroundAgentInitialScanStartTime = Date()

Expand Down Expand Up @@ -245,21 +250,24 @@ extension DataBrokerProtectionAgentManager: DataBrokerProtectionAgentAppEvents {
self.pixelHandler.fire(.ipcServerImmediateScansFinishedWithoutError)
self.userNotificationService.sendFirstScanCompletedNotification()
}
} completion: { [weak self] in
guard let self else { return }

if let hasMatches = try? self.dataManager.hasMatches(),
hasMatches {
hasMatches {
self.userNotificationService.scheduleCheckInNotificationIfPossible()
}

fireImmediateScansCompletionPixel(startTime: backgroundAgentInitialScanStartTime)
self.startScheduledOperations(completion: nil)
}
}

public func appLaunched() {
fireMonitoringPixels()
queueManager.startScheduledOperationsIfPermitted(showWebView: false,
operationDependencies:
operationDependencies) { [weak self] errors in
operationDependencies: operationDependencies,
errorHandler: { [weak self] errors in
guard let self = self else { return }

if let errors = errors {
Expand All @@ -285,7 +293,7 @@ extension DataBrokerProtectionAgentManager: DataBrokerProtectionAgentAppEvents {
if errors?.oneTimeError == nil {
self.pixelHandler.fire(.ipcServerAppLaunchedScheduledScansFinishedWithoutError)
}
}
}, completion: nil)
}

private func fireImmediateScansCompletionPixel(startTime: Date) {
Expand All @@ -310,18 +318,21 @@ extension DataBrokerProtectionAgentManager: DataBrokerProtectionAgentDebugComman
public func startImmediateOperations(showWebView: Bool) {
queueManager.startImmediateOperationsIfPermitted(showWebView: showWebView,
operationDependencies: operationDependencies,
errorHandler: nil,
completion: nil)
}

public func startScheduledOperations(showWebView: Bool) {
queueManager.startScheduledOperationsIfPermitted(showWebView: showWebView,
operationDependencies: operationDependencies,
errorHandler: nil,
completion: nil)
}

public func runAllOptOuts(showWebView: Bool) {
queueManager.execute(.startOptOutOperations(showWebView: showWebView,
operationDependencies: operationDependencies,
errorHandler: nil,
completion: nil))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ protocol DataBrokerProtectionOperationQueue {

extension OperationQueue: DataBrokerProtectionOperationQueue {
/*
An unfortunute necessarity due to an issue with xcode 16 and building for Product Review Release
An unfortunate necessarity due to an issue with xcode 16 and building for Product Review Release
Originally we just used addBarrierBlock directly, but now it won't build as it says
the extension doesn't conform to the protocol
The docs however say the method signiture is unchanged, so we've decided to blame the build system
Expand All @@ -41,8 +41,8 @@ extension OperationQueue: DataBrokerProtectionOperationQueue {

enum DataBrokerProtectionQueueMode {
case idle
case immediate(completion: ((DataBrokerProtectionAgentErrorCollection?) -> Void)?)
case scheduled(completion: ((DataBrokerProtectionAgentErrorCollection?) -> Void)?)
case immediate(errorHandler: ((DataBrokerProtectionAgentErrorCollection?) -> Void)?, completion: (() -> Void)?)
case scheduled(errorHandler: ((DataBrokerProtectionAgentErrorCollection?) -> Void)?, completion: (() -> Void)?)

var priorityDate: Date? {
switch self {
Expand Down Expand Up @@ -73,7 +73,8 @@ enum DataBrokerProtectionQueueError: Error {
enum DataBrokerProtectionQueueManagerDebugCommand {
case startOptOutOperations(showWebView: Bool,
operationDependencies: DataBrokerOperationDependencies,
completion: ((DataBrokerProtectionAgentErrorCollection?) -> Void)?)
errorHandler: ((DataBrokerProtectionAgentErrorCollection?) -> Void)?,
completion: (() -> Void)?)
}

protocol DataBrokerProtectionQueueManager {
Expand All @@ -86,10 +87,12 @@ protocol DataBrokerProtectionQueueManager {

func startImmediateOperationsIfPermitted(showWebView: Bool,
operationDependencies: DataBrokerOperationDependencies,
completion: ((DataBrokerProtectionAgentErrorCollection?) -> Void)?)
errorHandler: ((DataBrokerProtectionAgentErrorCollection?) -> Void)?,
completion: (() -> Void)?)
func startScheduledOperationsIfPermitted(showWebView: Bool,
operationDependencies: DataBrokerOperationDependencies,
completion: ((DataBrokerProtectionAgentErrorCollection?) -> Void)?)
errorHandler: ((DataBrokerProtectionAgentErrorCollection?) -> Void)?,
completion: (() -> Void)?)

func execute(_ command: DataBrokerProtectionQueueManagerDebugCommand)
var debugRunningStatusString: String { get }
Expand Down Expand Up @@ -131,37 +134,44 @@ final class DefaultDataBrokerProtectionQueueManager: DataBrokerProtectionQueueMa

func startImmediateOperationsIfPermitted(showWebView: Bool,
operationDependencies: DataBrokerOperationDependencies,
completion: ((DataBrokerProtectionAgentErrorCollection?) -> Void)?) {
errorHandler: ((DataBrokerProtectionAgentErrorCollection?) -> Void)?,
completion: (() -> Void)?) {

let newMode = DataBrokerProtectionQueueMode.immediate(completion: completion)
let newMode = DataBrokerProtectionQueueMode.immediate(errorHandler: errorHandler, completion: completion)
startOperationsIfPermitted(forNewMode: newMode,
type: .scan,
showWebView: showWebView,
operationDependencies: operationDependencies) { [weak self] errors in
completion?(errors)
self?.mismatchCalculator.calculateMismatches()
errorHandler?(errors)
} completion: {
completion?()
}
}

func startScheduledOperationsIfPermitted(showWebView: Bool,
operationDependencies: DataBrokerOperationDependencies,
completion: ((DataBrokerProtectionAgentErrorCollection?) -> Void)?) {
let newMode = DataBrokerProtectionQueueMode.scheduled(completion: completion)
errorHandler: ((DataBrokerProtectionAgentErrorCollection?) -> Void)?,
completion: (() -> Void)?) {
let newMode = DataBrokerProtectionQueueMode.scheduled(errorHandler: errorHandler, completion: completion)
startOperationsIfPermitted(forNewMode: newMode,
type: .all,
showWebView: showWebView,
operationDependencies: operationDependencies,
errorHandler: errorHandler,
completion: completion)
}

func execute(_ command: DataBrokerProtectionQueueManagerDebugCommand) {
guard case .startOptOutOperations(let showWebView,
let operationDependencies,
let errorHandler,
let completion) = command else { return }

addOperations(withType: .optOut,
showWebView: showWebView,
operationDependencies: operationDependencies,
errorHandler: errorHandler,
completion: completion)
}
}
Expand All @@ -172,12 +182,14 @@ private extension DefaultDataBrokerProtectionQueueManager {
type: OperationType,
showWebView: Bool,
operationDependencies: DataBrokerOperationDependencies,
completion: ((DataBrokerProtectionAgentErrorCollection?) -> Void)?) {
errorHandler: ((DataBrokerProtectionAgentErrorCollection?) -> Void)?,
completion: (() -> Void)?) {

guard mode.canBeInterruptedBy(newMode: newMode) else {
let error = DataBrokerProtectionQueueError.cannotInterrupt
let errorCollection = DataBrokerProtectionAgentErrorCollection(oneTimeError: error)
completion?(errorCollection)
errorHandler?(errorCollection)
completion?()
return
}

Expand All @@ -191,16 +203,18 @@ private extension DefaultDataBrokerProtectionQueueManager {
priorityDate: mode.priorityDate,
showWebView: showWebView,
operationDependencies: operationDependencies,
errorHandler: errorHandler,
completion: completion)
}

func cancelCurrentModeAndResetIfNeeded() {
switch mode {
case .immediate(let completion), .scheduled(let completion):
case .immediate(let errorHandler, let completion), .scheduled(let errorHandler, let completion):
operationQueue.cancelAllOperations()
let errorCollection = DataBrokerProtectionAgentErrorCollection(oneTimeError: DataBrokerProtectionQueueError.interrupted, operationErrors: operationErrorsForCurrentOperations())
completion?(errorCollection)
errorHandler?(errorCollection)
resetModeAndClearErrors()
completion?()
default:
break
}
Expand All @@ -220,7 +234,8 @@ private extension DefaultDataBrokerProtectionQueueManager {
priorityDate: Date? = nil,
showWebView: Bool,
operationDependencies: DataBrokerOperationDependencies,
completion: ((DataBrokerProtectionAgentErrorCollection?) -> Void)?) {
errorHandler: ((DataBrokerProtectionAgentErrorCollection?) -> Void)?,
completion: (() -> Void)?) {

operationQueue.maxConcurrentOperationCount = operationDependencies.config.concurrentOperationsFor(type)

Expand All @@ -238,14 +253,16 @@ private extension DefaultDataBrokerProtectionQueueManager {
}
} catch {
Logger.dataBrokerProtection.error("DataBrokerProtectionProcessor error: addOperations, error: \(error.localizedDescription, privacy: .public)")
completion?(DataBrokerProtectionAgentErrorCollection(oneTimeError: error))
errorHandler?(DataBrokerProtectionAgentErrorCollection(oneTimeError: error))
completion?()
return
}

operationQueue.addBarrierCompletionBlock { [weak self] in
let errorCollection = DataBrokerProtectionAgentErrorCollection(oneTimeError: nil, operationErrors: self?.operationErrorsForCurrentOperations())
completion?(errorCollection)
errorHandler?(errorCollection)
self?.resetModeAndClearErrors()
completion?()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import os.log
public extension Logger {
fileprivate static let subsystem = "com.duckduckgo.macos.browser.databroker-protection"

static var dataBrokerProtection = { Logger(subsystem: subsystem, category: "") }()
static var dataBrokerProtection = { Logger(subsystem: subsystem, category: "Data Broker Protection") }()
static var action = { Logger(subsystem: subsystem, category: "Action") }()
static var service = { Logger(subsystem: subsystem, category: "Service") }()
static var backgroundAgent = { Logger(subsystem: subsystem, category: "Background Agent") }()
Expand Down

0 comments on commit bdb228d

Please sign in to comment.