Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge pir stabilization feature branch into main #2789

Merged
merged 11 commits into from
May 22, 2024
36 changes: 6 additions & 30 deletions DuckDuckGo.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion DuckDuckGo/DBP/DBPHomeViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ final class DBPHomeViewController: NSViewController {
featureToggles: features)

return DataBrokerProtectionViewController(
scheduler: dataBrokerProtectionManager.scheduler,
agentInterface: dataBrokerProtectionManager.loginItemInterface,
dataManager: dataBrokerProtectionManager.dataManager,
privacyConfig: privacyConfigurationManager,
prefs: prefs,
Expand Down
5 changes: 5 additions & 0 deletions DuckDuckGo/DBP/DataBrokerProtectionAppEvents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ struct DataBrokerProtectionAppEvents {
func applicationDidFinishLaunching() {
let loginItemsManager = LoginItemsManager()
let featureVisibility = DefaultDataBrokerProtectionFeatureVisibility()
let loginItemInterface = DataBrokerProtectionManager.shared.loginItemInterface

guard !featureVisibility.cleanUpDBPForPrivacyProIfNecessary() else { return }

Expand All @@ -52,6 +53,10 @@ struct DataBrokerProtectionAppEvents {
if let profileQueriesCount = try? DataBrokerProtectionManager.shared.dataManager.profileQueriesCount(),
profileQueriesCount > 0 {
restartBackgroundAgent(loginItemsManager: loginItemsManager)

// Wait to make sure the agent has had time to restart before attempting to call a method on it
try await Task.sleep(nanoseconds: 1_000_000_000)
loginItemInterface.appLaunched()
} else {
featureVisibility.disableAndDeleteForWaitlistUsers()
}
Expand Down
50 changes: 7 additions & 43 deletions DuckDuckGo/DBP/DataBrokerProtectionDebugMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ final class DataBrokerProtectionDebugMenu: NSMenu {
NSMenuItem(title: "Operations") {
NSMenuItem(title: "Hidden WebView") {
menuItem(withTitle: "Run queued operations",
action: #selector(DataBrokerProtectionDebugMenu.runQueuedOperations(_:)),
action: #selector(DataBrokerProtectionDebugMenu.startScheduledOperations(_:)),
representedObject: false)

menuItem(withTitle: "Run scan operations",
Expand All @@ -119,7 +119,7 @@ final class DataBrokerProtectionDebugMenu: NSMenu {

NSMenuItem(title: "Visible WebView") {
menuItem(withTitle: "Run queued operations",
action: #selector(DataBrokerProtectionDebugMenu.runQueuedOperations(_:)),
action: #selector(DataBrokerProtectionDebugMenu.startScheduledOperations(_:)),
representedObject: true)

menuItem(withTitle: "Run scan operations",
Expand Down Expand Up @@ -204,61 +204,25 @@ final class DataBrokerProtectionDebugMenu: NSMenu {
}
}

@objc private func runQueuedOperations(_ sender: NSMenuItem) {
@objc private func startScheduledOperations(_ sender: NSMenuItem) {
os_log("Running queued operations...", log: .dataBrokerProtection)
let showWebView = sender.representedObject as? Bool ?? false

DataBrokerProtectionManager.shared.scheduler.runQueuedOperations(showWebView: showWebView) { errors in
if let errors = errors {
if let oneTimeError = errors.oneTimeError {
os_log("Queued operations finished, error: %{public}@", log: .dataBrokerProtection, oneTimeError.localizedDescription)
}
if let operationErrors = errors.operationErrors,
operationErrors.count != 0 {
os_log("Queued operations finished, operation errors count: %{public}d", log: .dataBrokerProtection, operationErrors.count)
}
} else {
os_log("Queued operations finished", log: .dataBrokerProtection)
}
}
DataBrokerProtectionManager.shared.loginItemInterface.startScheduledOperations(showWebView: showWebView)
}

@objc private func runScanOperations(_ sender: NSMenuItem) {
os_log("Running scan operations...", log: .dataBrokerProtection)
let showWebView = sender.representedObject as? Bool ?? false

DataBrokerProtectionManager.shared.scheduler.startManualScan(showWebView: showWebView, startTime: Date()) { errors in
if let errors = errors {
if let oneTimeError = errors.oneTimeError {
os_log("scan operations finished, error: %{public}@", log: .dataBrokerProtection, oneTimeError.localizedDescription)
}
if let operationErrors = errors.operationErrors,
operationErrors.count != 0 {
os_log("scan operations finished, operation errors count: %{public}d", log: .dataBrokerProtection, operationErrors.count)
}
} else {
os_log("Scan operations finished", log: .dataBrokerProtection)
}
}
DataBrokerProtectionManager.shared.loginItemInterface.startImmediateOperations(showWebView: showWebView)
}

@objc private func runOptoutOperations(_ sender: NSMenuItem) {
os_log("Running Optout operations...", log: .dataBrokerProtection)
let showWebView = sender.representedObject as? Bool ?? false

DataBrokerProtectionManager.shared.scheduler.optOutAllBrokers(showWebView: showWebView) { errors in
if let errors = errors {
if let oneTimeError = errors.oneTimeError {
os_log("Optout operations finished, error: %{public}@", log: .dataBrokerProtection, oneTimeError.localizedDescription)
}
if let operationErrors = errors.operationErrors,
operationErrors.count != 0 {
os_log("Optout operations finished, operation errors count: %{public}d", log: .dataBrokerProtection, operationErrors.count)
}
} else {
os_log("Optout operations finished", log: .dataBrokerProtection)
}
}
DataBrokerProtectionManager.shared.loginItemInterface.runAllOptOuts(showWebView: showWebView)
}

@objc private func backgroundAgentRestart() {
Expand Down Expand Up @@ -332,7 +296,7 @@ final class DataBrokerProtectionDebugMenu: NSMenu {
}

@objc private func forceBrokerJSONFilesUpdate() {
if let updater = DataBrokerProtectionBrokerUpdater.provide() {
if let updater = DefaultDataBrokerProtectionBrokerUpdater.provideForDebug() {
updater.updateBrokers()
}
}
Expand Down
10 changes: 4 additions & 6 deletions DuckDuckGo/DBP/DataBrokerProtectionFeatureDisabler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,21 @@ protocol DataBrokerProtectionFeatureDisabling {
}

struct DataBrokerProtectionFeatureDisabler: DataBrokerProtectionFeatureDisabling {
private let scheduler: DataBrokerProtectionLoginItemScheduler
private let loginItemInterface: DataBrokerProtectionLoginItemInterface
private let dataManager: InMemoryDataCacheDelegate

init(scheduler: DataBrokerProtectionLoginItemScheduler = DataBrokerProtectionManager.shared.scheduler,
init(loginItemInterface: DataBrokerProtectionLoginItemInterface = DataBrokerProtectionManager.shared.loginItemInterface,
dataManager: InMemoryDataCacheDelegate = DataBrokerProtectionManager.shared.dataManager) {
self.dataManager = dataManager
self.scheduler = scheduler
self.loginItemInterface = loginItemInterface
}

func disableAndDelete() {
if !DefaultDataBrokerProtectionFeatureVisibility.bypassWaitlist {
scheduler.stopScheduler()

scheduler.disableLoginItem()

do {
try dataManager.removeAllData()
// the dataManagers delegate handles login item disabling
} catch {
os_log("DataBrokerProtectionFeatureDisabler error: disableAndDelete, error: %{public}@", log: .error, error.localizedDescription)
}
Expand Down
119 changes: 119 additions & 0 deletions DuckDuckGo/DBP/DataBrokerProtectionLoginItemInterface.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
//
// DataBrokerProtectionLoginItemInterface.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.
//

#if DBP

import Foundation
import DataBrokerProtection
import Common

protocol DataBrokerProtectionLoginItemInterface: DataBrokerProtectionAppToAgentInterface {
func dataDeleted()
}

/// Launches a login item and then communicates with it through IPC
///
final class DefaultDataBrokerProtectionLoginItemInterface {
private let ipcClient: DataBrokerProtectionIPCClient
private let loginItemsManager: LoginItemsManager
private let pixelHandler: EventMapping<DataBrokerProtectionPixels>

init(ipcClient: DataBrokerProtectionIPCClient,
loginItemsManager: LoginItemsManager = .init(),
pixelHandler: EventMapping<DataBrokerProtectionPixels>) {
self.ipcClient = ipcClient
self.loginItemsManager = loginItemsManager
self.pixelHandler = pixelHandler
}
}

extension DefaultDataBrokerProtectionLoginItemInterface: DataBrokerProtectionLoginItemInterface {

// MARK: - Login Item Management

private func disableLoginItem() {
DataBrokerProtectionLoginItemPixels.fire(pixel: GeneralPixel.dataBrokerDisableLoginItemDaily, frequency: .daily)
loginItemsManager.disableLoginItems([.dbpBackgroundAgent])
}

private func enableLoginItem() {
DataBrokerProtectionLoginItemPixels.fire(pixel: GeneralPixel.dataBrokerEnableLoginItemDaily, frequency: .daily)
loginItemsManager.enableLoginItems([.dbpBackgroundAgent], log: .dbp)
}

// MARK: - DataBrokerProtectionLoginItemInterface

func dataDeleted() {
disableLoginItem()
}

// MARK: - DataBrokerProtectionAppToAgentInterface
// MARK: - DataBrokerProtectionAgentAppEvents

func profileSaved() {
enableLoginItem()

Task {
// Wait to make sure the agent has had time to launch
try await Task.sleep(nanoseconds: 1_000_000_000)
pixelHandler.fire(.ipcServerProfileSavedCalledByApp)
ipcClient.profileSaved { error in
if let error = error {
self.pixelHandler.fire(.ipcServerProfileSavedXPCError(error: error))
} else {
self.pixelHandler.fire(.ipcServerProfileSavedReceivedByAgent)
}
}
}
}

func appLaunched() {
pixelHandler.fire(.ipcServerAppLaunchedCalledByApp)
ipcClient.appLaunched { error in
if let error = error {
self.pixelHandler.fire(.ipcServerAppLaunchedXPCError(error: error))
} else {
self.pixelHandler.fire(.ipcServerAppLaunchedReceivedByAgent)
}
}
}

// MARK: - DataBrokerProtectionAgentDebugCommands

func openBrowser(domain: String) {
ipcClient.openBrowser(domain: domain)
}

func startImmediateOperations(showWebView: Bool) {
ipcClient.startImmediateOperations(showWebView: showWebView)
}

func startScheduledOperations(showWebView: Bool) {
ipcClient.startScheduledOperations(showWebView: showWebView)
}

func runAllOptOuts(showWebView: Bool) {
ipcClient.runAllOptOuts(showWebView: showWebView)
}

func getDebugMetadata() async -> DataBrokerProtection.DBPBackgroundAgentMetadata? {
return await ipcClient.getDebugMetadata()
}
}

#endif
93 changes: 0 additions & 93 deletions DuckDuckGo/DBP/DataBrokerProtectionLoginItemScheduler.swift

This file was deleted.

11 changes: 4 additions & 7 deletions DuckDuckGo/DBP/DataBrokerProtectionManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,8 @@ public final class DataBrokerProtectionManager {
loginItemStatusChecker: loginItemStatusChecker)
}()

lazy var scheduler: DataBrokerProtectionLoginItemScheduler = {

let ipcScheduler = DataBrokerProtectionIPCScheduler(ipcClient: ipcClient)

return DataBrokerProtectionLoginItemScheduler(ipcScheduler: ipcScheduler)
lazy var loginItemInterface: DataBrokerProtectionLoginItemInterface = {
return DefaultDataBrokerProtectionLoginItemInterface(ipcClient: ipcClient, pixelHandler: pixelHandler)
}()

private init() {
Expand All @@ -70,14 +67,14 @@ public final class DataBrokerProtectionManager {

extension DataBrokerProtectionManager: DataBrokerProtectionDataManagerDelegate {
public func dataBrokerProtectionDataManagerDidUpdateData() {
scheduler.startScheduler()
loginItemInterface.profileSaved()

let dbpDateStore = DefaultWaitlistActivationDateStore(source: .dbp)
dbpDateStore.setActivationDateIfNecessary()
}

public func dataBrokerProtectionDataManagerDidDeleteData() {
scheduler.stopScheduler()
loginItemInterface.dataDeleted()
}
}

Expand Down
Loading
Loading