From fcbdf7292fd372f7caea7e542d154e1c6613d0e7 Mon Sep 17 00:00:00 2001 From: Chris Brind Date: Fri, 22 Nov 2024 16:22:23 +0000 Subject: [PATCH] migration logic and refactoring --- Core/Fireproofing.swift | 4 +- Core/WebCacheManager.swift | 35 +++++++++----- DuckDuckGo/AppDelegate.swift | 15 +++++- DuckDuckGo/ContentBlockingUpdating.swift | 6 ++- DuckDuckGo/CookieDebugViewController.swift | 2 +- DuckDuckGo/Favicons.swift | 2 +- .../FireproofingSettingsViewController.swift | 10 ++-- .../ImageCacheDebugViewController.swift | 2 +- DuckDuckGo/MainViewController+Segues.swift | 7 ++- DuckDuckGo/MainViewController.swift | 13 +++-- ...otDebugViewController+VanillaBrowser.swift | 2 +- DuckDuckGo/RootDebugViewController.swift | 48 ++++++++----------- DuckDuckGo/ScriptSourceProviding.swift | 2 +- DuckDuckGo/SettingsLegacyViewProvider.swift | 35 ++++++++------ .../HeadlessWebView.swift | 3 +- .../PrivacyProDataReporting.swift | 2 +- DuckDuckGo/TabManager.swift | 16 +++++-- DuckDuckGo/TabViewController.swift | 15 ++++-- ...ViewControllerLongPressMenuExtension.swift | 4 +- 19 files changed, 140 insertions(+), 83 deletions(-) diff --git a/Core/Fireproofing.swift b/Core/Fireproofing.swift index 166c04c562..745787900d 100644 --- a/Core/Fireproofing.swift +++ b/Core/Fireproofing.swift @@ -35,7 +35,9 @@ public protocol Fireproofing { // This class is not final because we override allowed domains in WebCacheManagerTests public class UserDefaultsFireproofing: Fireproofing { - public static let shared: Fireproofing = UserDefaultsFireproofing() + /// This is only here because there are some places that don't support injection at this time. DO NOT USE IT. + /// If you find you really need to use it, ping Apple Devs channel first. + public static let xshared: Fireproofing = UserDefaultsFireproofing() public struct Notifications { public static let loginDetectionStateChanged = Foundation.Notification.Name("com.duckduckgo.ios.PreserveLogins.loginDetectionStateChanged") diff --git a/Core/WebCacheManager.swift b/Core/WebCacheManager.swift index 1abead7ddd..7e66ef0ed8 100644 --- a/Core/WebCacheManager.swift +++ b/Core/WebCacheManager.swift @@ -42,17 +42,31 @@ extension HTTPCookie { } @MainActor -public class WebCacheManager { +public protocol WebsiteDataManaging { - public static var shared = WebCacheManager() + func removeCookies(forDomains domains: [String], fromDataStore: WKWebsiteDataStore) async + func consumeCookies(intoHTTPCookieStore httpCookieStore: WKHTTPCookieStore) async + func clear(dataStore: WKWebsiteDataStore) async - private init() { } +} + +@MainActor +public class WebCacheManager: WebsiteDataManaging { + + let cookieStorage: MigratableCookieStorage + let fireproofing: Fireproofing + let dataStoreIdManager: DataStoreIdManaging + + public init(cookieStorage: MigratableCookieStorage, fireproofing: Fireproofing, dataStoreIdManager: DataStoreIdManaging) { + self.cookieStorage = cookieStorage + self.fireproofing = fireproofing + self.dataStoreIdManager = dataStoreIdManager + } /// We save cookies from the current container rather than copying them to a new container because /// the container only persists cookies to disk when the web view is used. If the user presses the fire button /// twice then the fire proofed cookies will be lost and the user will be logged out any sites they're logged in to. - public func consumeCookies(cookieStorage: MigratableCookieStorage = MigratableCookieStorage(), - httpCookieStore: WKHTTPCookieStore) async { + public func consumeCookies(intoHTTPCookieStore httpCookieStore: WKHTTPCookieStore) async { guard !cookieStorage.isConsumed else { return } let cookies = cookieStorage.cookies @@ -65,7 +79,7 @@ public class WebCacheManager { } public func removeCookies(forDomains domains: [String], - dataStore: WKWebsiteDataStore) async { + fromDataStore dataStore: WKWebsiteDataStore) async { let startTime = CACurrentMediaTime() let cookieStore = dataStore.httpCookieStore let cookies = await cookieStore.allCookies() @@ -76,13 +90,10 @@ public class WebCacheManager { Pixel.fire(pixel: .cookieDeletionTime(.init(number: totalTime))) } - public func clear(fromDataStore dataStore: WKWebsiteDataStore, - cookieStorage: MigratableCookieStorage = MigratableCookieStorage(), - fireproofing: Fireproofing = UserDefaultsFireproofing.shared, - dataStoreIdManager: DataStoreIdManaging = DataStoreIdManager.shared) async { + public func clear(dataStore: WKWebsiteDataStore) async { await performMigrationIfNeeded(dataStoreIdManager: dataStoreIdManager, destinationStore: dataStore) - await clearData(fromDataStore: dataStore, withFireproofing: fireproofing) + await clearData(inDataStore: dataStore, withFireproofing: fireproofing) removeContainersIfNeeded() } @@ -156,7 +167,7 @@ extension WebCacheManager { } } - private func clearData(fromDataStore dataStore: WKWebsiteDataStore, withFireproofing fireproofing: Fireproofing) async { + private func clearData(inDataStore dataStore: WKWebsiteDataStore, withFireproofing fireproofing: Fireproofing) async { let startTime = CACurrentMediaTime() // Start with all types diff --git a/DuckDuckGo/AppDelegate.swift b/DuckDuckGo/AppDelegate.swift index 8020b7ad4b..05bc844c0b 100644 --- a/DuckDuckGo/AppDelegate.swift +++ b/DuckDuckGo/AppDelegate.swift @@ -285,7 +285,8 @@ import os.log syncService.initializeIfNeeded() self.syncService = syncService - privacyProDataReporter = PrivacyProDataReporter() + let fireproofing = UserDefaultsFireproofing.xshared + privacyProDataReporter = PrivacyProDataReporter(fireproofing: fireproofing) isSyncInProgressCancellable = syncService.isSyncInProgressPublisher .filter { $0 } @@ -357,8 +358,11 @@ import os.log subscriptionFeatureAvailability: subscriptionFeatureAvailability, voiceSearchHelper: voiceSearchHelper, featureFlagger: AppDependencyProvider.shared.featureFlagger, + fireproofing: fireproofing, subscriptionCookieManager: subscriptionCookieManager, - textZoomCoordinator: makeTextZoomCoordinator()) + textZoomCoordinator: makeTextZoomCoordinator(), + websiteDataManager: makeWebsiteDataManager(fireproofing: fireproofing) + ) main.loadViewIfNeeded() syncErrorHandler.alertPresenter = main @@ -411,6 +415,13 @@ import os.log return true } + private func makeWebsiteDataManager(fireproofing: Fireproofing, + dataStoreIdManager: DataStoreIdManaging = DataStoreIdManager.shared) -> WebsiteDataManaging { + return WebCacheManager(cookieStorage: MigratableCookieStorage(), + fireproofing: fireproofing, + dataStoreIdManager: dataStoreIdManager) + } + private func makeTextZoomCoordinator() -> TextZoomCoordinator { let provider = AppDependencyProvider.shared let storage = TextZoomStorage() diff --git a/DuckDuckGo/ContentBlockingUpdating.swift b/DuckDuckGo/ContentBlockingUpdating.swift index dc3b8a720d..4147bbef44 100644 --- a/DuckDuckGo/ContentBlockingUpdating.swift +++ b/DuckDuckGo/ContentBlockingUpdating.swift @@ -57,12 +57,14 @@ public final class ContentBlockingUpdating { init(appSettings: AppSettings = AppUserDefaults(), contentBlockerRulesManager: ContentBlockerRulesManagerProtocol = ContentBlocking.shared.contentBlockingManager, - privacyConfigurationManager: PrivacyConfigurationManaging = ContentBlocking.shared.privacyConfigurationManager) { + privacyConfigurationManager: PrivacyConfigurationManaging = ContentBlocking.shared.privacyConfigurationManager, + fireproofing: Fireproofing = UserDefaultsFireproofing.xshared) { let makeValue: (Update) -> NewContent = { rulesUpdate in let sourceProvider = DefaultScriptSourceProvider(appSettings: appSettings, privacyConfigurationManager: privacyConfigurationManager, - contentBlockingManager: contentBlockerRulesManager) + contentBlockingManager: contentBlockerRulesManager, + fireproofing: fireproofing) return NewContent(rulesUpdate: rulesUpdate, sourceProvider: sourceProvider) } diff --git a/DuckDuckGo/CookieDebugViewController.swift b/DuckDuckGo/CookieDebugViewController.swift index 4efc382742..577bc50192 100644 --- a/DuckDuckGo/CookieDebugViewController.swift +++ b/DuckDuckGo/CookieDebugViewController.swift @@ -41,7 +41,7 @@ class CookieDebugViewController: UITableViewController { var loaded = false let fireproofing: Fireproofing - init(fireproofing: Fireproofing = UserDefaultsFireproofing.shared) { + init(fireproofing: Fireproofing) { self.fireproofing = fireproofing super.init() } diff --git a/DuckDuckGo/Favicons.swift b/DuckDuckGo/Favicons.swift index 719b1fa2cd..fafe8ef3a6 100644 --- a/DuckDuckGo/Favicons.swift +++ b/DuckDuckGo/Favicons.swift @@ -57,7 +57,7 @@ public class Favicons { init(sourcesProvider: FaviconSourcesProvider = DefaultFaviconSourcesProvider(), downloader: NotFoundCachingDownloader = NotFoundCachingDownloader(), - fireproofing: Fireproofing = UserDefaultsFireproofing.shared) { + fireproofing: Fireproofing = UserDefaultsFireproofing.xshared) { self.sourcesProvider = sourcesProvider self.downloader = downloader self.fireproofing = fireproofing diff --git a/DuckDuckGo/FireproofingSettingsViewController.swift b/DuckDuckGo/FireproofingSettingsViewController.swift index 08bc88b0c7..0fe2175144 100644 --- a/DuckDuckGo/FireproofingSettingsViewController.swift +++ b/DuckDuckGo/FireproofingSettingsViewController.swift @@ -37,9 +37,13 @@ class FireproofingSettingsViewController: UITableViewController { private var shouldShowRemoveAll = false private let fireproofing: Fireproofing + private let websiteDataManager: WebsiteDataManaging - init?(coder: NSCoder, fireproofing: Fireproofing) { + init?(coder: NSCoder, + fireproofing: Fireproofing, + websiteDataManager: WebsiteDataManaging) { self.fireproofing = fireproofing + self.websiteDataManager = websiteDataManager super.init(coder: coder) } @@ -166,7 +170,7 @@ class FireproofingSettingsViewController: UITableViewController { } Task { @MainActor in - await WebCacheManager.shared.removeCookies(forDomains: [domain], dataStore: WKWebsiteDataStore.current()) + await websiteDataManager.removeCookies(forDomains: [domain], fromDataStore: WKWebsiteDataStore.current()) } } @@ -225,7 +229,7 @@ class FireproofingSettingsViewController: UITableViewController { self?.refreshModel() }, confirmed: { [weak self] in Task { @MainActor in - await WebCacheManager.shared.removeCookies(forDomains: self?.model ?? [], dataStore: WKWebsiteDataStore.current()) + await self?.websiteDataManager.removeCookies(forDomains: self?.model ?? [], fromDataStore: WKWebsiteDataStore.current()) self?.fireproofing.clearAll() self?.refreshModel() self?.endEditing() diff --git a/DuckDuckGo/ImageCacheDebugViewController.swift b/DuckDuckGo/ImageCacheDebugViewController.swift index 37c6a838fe..f7be2275bd 100644 --- a/DuckDuckGo/ImageCacheDebugViewController.swift +++ b/DuckDuckGo/ImageCacheDebugViewController.swift @@ -61,7 +61,7 @@ class ImageCacheDebugViewController: UITableViewController { init?(coder: NSCoder, bookmarksDatabase: CoreDataDatabase, - fireproofing: Fireproofing = UserDefaultsFireproofing.shared) { + fireproofing: Fireproofing) { bookmarksContext = bookmarksDatabase.makeContext(concurrencyType: .mainQueueConcurrencyType) self.fireproofing = fireproofing diff --git a/DuckDuckGo/MainViewController+Segues.swift b/DuckDuckGo/MainViewController+Segues.swift index 3ab8722e7f..5eafe2cdea 100644 --- a/DuckDuckGo/MainViewController+Segues.swift +++ b/DuckDuckGo/MainViewController+Segues.swift @@ -292,7 +292,9 @@ extension MainViewController { appSettings: appSettings, bookmarksDatabase: bookmarksDatabase, tabManager: tabManager, - syncPausedStateManager: syncPausedStateManager) + syncPausedStateManager: syncPausedStateManager, + fireproofing: fireproofing, + websiteDataManager: websiteDataManager) let settingsViewModel = SettingsViewModel(legacyViewProvider: legacyViewProvider, subscriptionManager: AppDependencyProvider.shared.subscriptionManager, @@ -331,7 +333,8 @@ extension MainViewController { sync: self.syncService, bookmarksDatabase: self.bookmarksDatabase, internalUserDecider: AppDependencyProvider.shared.internalUserDecider, - tabManager: self.tabManager) + tabManager: self.tabManager, + fireproofing: self.fireproofing) } let controller = UINavigationController(rootViewController: settings) diff --git a/DuckDuckGo/MainViewController.swift b/DuckDuckGo/MainViewController.swift index a952094b60..b961dc0d78 100644 --- a/DuckDuckGo/MainViewController.swift +++ b/DuckDuckGo/MainViewController.swift @@ -178,6 +178,7 @@ class MainViewController: UIViewController { } let fireproofing: Fireproofing + let websiteDataManager: WebsiteDataManaging let textZoomCoordinator: TextZoomCoordinating var historyManager: HistoryManaging @@ -204,9 +205,10 @@ class MainViewController: UIViewController { subscriptionFeatureAvailability: SubscriptionFeatureAvailability, voiceSearchHelper: VoiceSearchHelperProtocol, featureFlagger: FeatureFlagger, - fireproofing: Fireproofing = UserDefaultsFireproofing.shared, + fireproofing: Fireproofing, subscriptionCookieManager: SubscriptionCookieManaging, - textZoomCoordinator: TextZoomCoordinating + textZoomCoordinator: TextZoomCoordinating, + websiteDataManager: WebsiteDataManaging ) { self.bookmarksDatabase = bookmarksDatabase self.bookmarksDatabaseCleaner = bookmarksDatabaseCleaner @@ -232,7 +234,9 @@ class MainViewController: UIViewController { featureFlagger: featureFlagger, subscriptionCookieManager: subscriptionCookieManager, appSettings: appSettings, - textZoomCoordinator: textZoomCoordinator) + textZoomCoordinator: textZoomCoordinator, + websiteDataManager: websiteDataManager, + fireproofing: fireproofing) self.syncPausedStateManager = syncPausedStateManager self.privacyProDataReporter = privacyProDataReporter self.homeTabManager = NewTabPageManager() @@ -246,6 +250,7 @@ class MainViewController: UIViewController { self.fireproofing = fireproofing self.subscriptionCookieManager = subscriptionCookieManager self.textZoomCoordinator = textZoomCoordinator + self.websiteDataManager = websiteDataManager super.init(nibName: nil, bundle: nil) @@ -2660,7 +2665,7 @@ extension MainViewController: AutoClearWorker { URLSession.shared.configuration.urlCache?.removeAllCachedResponses() let pixel = TimedPixel(.forgetAllDataCleared) - await WebCacheManager.shared.clear(fromDataStore: WKWebsiteDataStore.default()) + await websiteDataManager.clear(dataStore: WKWebsiteDataStore.default()) pixel.fire(withAdditionalParameters: [PixelParameters.tabCount: "\(self.tabManager.count)"]) AutoconsentManagement.shared.clearCache() diff --git a/DuckDuckGo/RootDebugViewController+VanillaBrowser.swift b/DuckDuckGo/RootDebugViewController+VanillaBrowser.swift index c2900f153e..1de4437194 100644 --- a/DuckDuckGo/RootDebugViewController+VanillaBrowser.swift +++ b/DuckDuckGo/RootDebugViewController+VanillaBrowser.swift @@ -29,7 +29,7 @@ extension RootDebugViewController { fileprivate static let ddgURL = URL(string: "https://duckduckgo.com/")! @objc func openVanillaBrowser(_ sender: Any?) { - let homeURL = tabManager?.current()?.tabModel.link?.url ?? RootDebugViewController.ddgURL + let homeURL = tabManager.current()?.tabModel.link?.url ?? RootDebugViewController.ddgURL openVanillaBrowser(url: homeURL) } diff --git a/DuckDuckGo/RootDebugViewController.swift b/DuckDuckGo/RootDebugViewController.swift index 58258a5a07..9c833739de 100644 --- a/DuckDuckGo/RootDebugViewController.swift +++ b/DuckDuckGo/RootDebugViewController.swift @@ -55,14 +55,15 @@ class RootDebugViewController: UITableViewController { weak var reportGatheringActivity: UIView? @IBAction func onShareTapped() { - presentShareSheet(withItems: [DiagnosticReportDataSource(delegate: self)], fromButtonItem: shareButton) + presentShareSheet(withItems: [DiagnosticReportDataSource(delegate: self, fireproofing: fireproofing)], fromButtonItem: shareButton) } - private var bookmarksDatabase: CoreDataDatabase? - private var sync: DDGSyncing? - private var internalUserDecider: DefaultInternalUserDecider? - var tabManager: TabManager? - private var tipKitUIActionHandler: TipKitDebugOptionsUIActionHandling? + private let bookmarksDatabase: CoreDataDatabase + private let sync: DDGSyncing + private let internalUserDecider: InternalUserDecider + let tabManager: TabManager + private let tipKitUIActionHandler: TipKitDebugOptionsUIActionHandling + private let fireproofing: Fireproofing @UserDefaultsWrapper(key: .lastConfigurationRefreshDate, defaultValue: .distantPast) private var lastConfigurationRefreshDate: Date @@ -72,33 +73,27 @@ class RootDebugViewController: UITableViewController { bookmarksDatabase: CoreDataDatabase, internalUserDecider: InternalUserDecider, tabManager: TabManager, - tipKitUIActionHandler: TipKitDebugOptionsUIActionHandling = TipKitDebugOptionsUIActionHandler()) { + tipKitUIActionHandler: TipKitDebugOptionsUIActionHandling = TipKitDebugOptionsUIActionHandler(), + fireproofing: Fireproofing) { self.sync = sync self.bookmarksDatabase = bookmarksDatabase - self.internalUserDecider = internalUserDecider as? DefaultInternalUserDecider + self.internalUserDecider = internalUserDecider self.tabManager = tabManager self.tipKitUIActionHandler = tipKitUIActionHandler + self.fireproofing = fireproofing super.init(coder: coder) } required init?(coder: NSCoder) { - super.init(coder: coder) - } - - func configure(sync: DDGSyncing, bookmarksDatabase: CoreDataDatabase, internalUserDecider: InternalUserDecider, tabManager: TabManager, tipKitUIActionHandler: TipKitDebugOptionsUIActionHandling = TipKitDebugOptionsUIActionHandler()) { - - self.sync = sync - self.bookmarksDatabase = bookmarksDatabase - self.internalUserDecider = internalUserDecider as? DefaultInternalUserDecider - self.tabManager = tabManager - self.tipKitUIActionHandler = tipKitUIActionHandler + fatalError("init not implemented") } @IBSegueAction func onCreateImageCacheDebugScreen(_ coder: NSCoder) -> ImageCacheDebugViewController? { guard let controller = ImageCacheDebugViewController(coder: coder, - bookmarksDatabase: self.bookmarksDatabase!) else { + bookmarksDatabase: self.bookmarksDatabase, + fireproofing: fireproofing) else { fatalError("Failed to create controller") } @@ -107,8 +102,8 @@ class RootDebugViewController: UITableViewController { @IBSegueAction func onCreateSyncDebugScreen(_ coder: NSCoder, sender: Any?, segueIdentifier: String?) -> SyncDebugViewController { guard let controller = SyncDebugViewController(coder: coder, - sync: self.sync!, - bookmarksDatabase: self.bookmarksDatabase!) else { + sync: self.sync, + bookmarksDatabase: self.bookmarksDatabase) else { fatalError("Failed to create controller") } @@ -127,7 +122,7 @@ class RootDebugViewController: UITableViewController { if cell.tag == Row.toggleInspectableWebViews.rawValue { cell.accessoryType = AppUserDefaults().inspectableWebViewEnabled ? .checkmark : .none } else if cell.tag == Row.toggleInternalUserState.rawValue { - cell.accessoryType = (internalUserDecider?.isInternalUser ?? false) ? .checkmark : .none + cell.accessoryType = (internalUserDecider.isInternalUser) ? .checkmark : .none } } @@ -163,8 +158,8 @@ class RootDebugViewController: UITableViewController { cell.accessoryType = defaults.inspectableWebViewEnabled ? .checkmark : .none NotificationCenter.default.post(Notification(name: AppUserDefaults.Notifications.inspectableWebViewsToggled)) case .toggleInternalUserState: - let newState = !(internalUserDecider?.isInternalUser ?? false) - internalUserDecider?.debugSetInternalUserState(newState) + let newState = !internalUserDecider.isInternalUser + (internalUserDecider as? DefaultInternalUserDecider)?.debugSetInternalUserState(newState) cell.accessoryType = newState ? .checkmark : .none NotificationCenter.default.post(Notification(name: AppUserDefaults.Notifications.inspectableWebViewsToggled)) case .openVanillaBrowser: @@ -184,12 +179,11 @@ class RootDebugViewController: UITableViewController { let controller = UIHostingController(rootView: OnboardingDebugView(onNewOnboardingIntroStartAction: action)) show(controller, sender: nil) case .resetSyncPromoPrompts: - guard let sync = sync else { return } let syncPromoPresenter = SyncPromoManager(syncService: sync) syncPromoPresenter.resetPromos() ActionMessageView.present(message: "Sync Promos reset") case .resetTipKit: - tipKitUIActionHandler?.resetTipKitTapped() + tipKitUIActionHandler.resetTipKitTapped() } } } @@ -255,7 +249,7 @@ class DiagnosticReportDataSource: UIActivityItemProvider { @UserDefaultsWrapper(key: .lastConfigurationRefreshDate, defaultValue: .distantPast) private var lastRefreshDate: Date - convenience init(delegate: DiagnosticReportDataSourceDelegate, fireproofing: Fireproofing = UserDefaultsFireproofing.shared) { + convenience init(delegate: DiagnosticReportDataSourceDelegate, fireproofing: Fireproofing) { self.init(placeholderItem: "") self.delegate = delegate self.fireproofing = fireproofing diff --git a/DuckDuckGo/ScriptSourceProviding.swift b/DuckDuckGo/ScriptSourceProviding.swift index 61a47e794f..549b538b45 100644 --- a/DuckDuckGo/ScriptSourceProviding.swift +++ b/DuckDuckGo/ScriptSourceProviding.swift @@ -55,7 +55,7 @@ struct DefaultScriptSourceProvider: ScriptSourceProviding { init(appSettings: AppSettings = AppDependencyProvider.shared.appSettings, privacyConfigurationManager: PrivacyConfigurationManaging = ContentBlocking.shared.privacyConfigurationManager, contentBlockingManager: ContentBlockerRulesManagerProtocol = ContentBlocking.shared.contentBlockingManager, - fireproofing: Fireproofing = UserDefaultsFireproofing.shared) { + fireproofing: Fireproofing) { sendDoNotSell = appSettings.sendDoNotSell diff --git a/DuckDuckGo/SettingsLegacyViewProvider.swift b/DuckDuckGo/SettingsLegacyViewProvider.swift index 3b74346f78..2ce76ef5d6 100644 --- a/DuckDuckGo/SettingsLegacyViewProvider.swift +++ b/DuckDuckGo/SettingsLegacyViewProvider.swift @@ -41,6 +41,7 @@ class SettingsLegacyViewProvider: ObservableObject { let tabManager: TabManager let syncPausedStateManager: any SyncPausedStateManaging let fireproofing: Fireproofing + let websiteDataManager: WebsiteDataManaging init(syncService: any DDGSyncing, syncDataProviders: SyncDataProviders, @@ -48,7 +49,8 @@ class SettingsLegacyViewProvider: ObservableObject { bookmarksDatabase: CoreDataDatabase, tabManager: TabManager, syncPausedStateManager: any SyncPausedStateManaging, - fireproofing: Fireproofing = UserDefaultsFireproofing.shared) { + fireproofing: Fireproofing, + websiteDataManager: WebsiteDataManaging) { self.syncService = syncService self.syncDataProviders = syncDataProviders self.appSettings = appSettings @@ -56,6 +58,7 @@ class SettingsLegacyViewProvider: ObservableObject { self.tabManager = tabManager self.syncPausedStateManager = syncPausedStateManager self.fireproofing = fireproofing + self.websiteDataManager = websiteDataManager } enum LegacyView { @@ -81,7 +84,7 @@ class SettingsLegacyViewProvider: ObservableObject { private func instantiateFireproofingController() -> UIViewController { let storyboard = UIStoryboard(name: StoryboardName.settings, bundle: nil) return storyboard.instantiateViewController(identifier: "FireProofSites") { coder in - return FireproofingSettingsViewController(coder: coder, fireproofing: self.fireproofing) + return FireproofingSettingsViewController(coder: coder, fireproofing: self.fireproofing, websiteDataManager: self.websiteDataManager) } } @@ -92,6 +95,18 @@ class SettingsLegacyViewProvider: ObservableObject { }) } + private func instantiateDebugController() -> UIViewController { + let storyboard = UIStoryboard(name: "Debug", bundle: nil) + return storyboard.instantiateViewController(identifier: "DebugMenu") { coder in + RootDebugViewController(coder: coder, + sync: self.syncService, + bookmarksDatabase: self.bookmarksDatabase, + internalUserDecider: AppDependencyProvider.shared.internalUserDecider, + tabManager: self.tabManager, + fireproofing: self.fireproofing) + } + } + // Legacy UIKit Views (Pushed unmodified) var addToDock: UIViewController { instantiate( "instructions", fromStoryboard: StoryboardName.homeRow) } var appIcon: UIViewController { instantiate("AppIcon", fromStoryboard: StoryboardName.settings) } @@ -102,6 +117,8 @@ class SettingsLegacyViewProvider: ObservableObject { var keyboard: UIViewController { instantiate("Keyboard", fromStoryboard: StoryboardName.settings) } var feedback: UIViewController { instantiate("Feedback", fromStoryboard: StoryboardName.feedback) } var autoclearData: UIViewController { instantiateAutoClearController() } + var debug: UIViewController { instantiateDebugController() } + @MainActor func syncSettings(source: String? = nil) -> SyncSettingsViewController { @@ -121,17 +138,5 @@ class SettingsLegacyViewProvider: ObservableObject { selectedAccount: selectedAccount, source: .settings) } - - var debug: UIViewController { - let storyboard = UIStoryboard(name: "Debug", bundle: nil) - if let viewController = storyboard.instantiateViewController(withIdentifier: "DebugMenu") as? RootDebugViewController { - viewController.configure(sync: syncService, - bookmarksDatabase: bookmarksDatabase, - internalUserDecider: AppDependencyProvider.shared.internalUserDecider, - tabManager: tabManager) - return viewController - } - return UIViewController() - } - + } diff --git a/DuckDuckGo/Subscription/AsyncHeadlessWebview/HeadlessWebView.swift b/DuckDuckGo/Subscription/AsyncHeadlessWebview/HeadlessWebView.swift index 5af147b4a5..f7246b44f0 100644 --- a/DuckDuckGo/Subscription/AsyncHeadlessWebview/HeadlessWebView.swift +++ b/DuckDuckGo/Subscription/AsyncHeadlessWebview/HeadlessWebView.swift @@ -22,6 +22,7 @@ import SwiftUI import WebKit import UserScript import BrowserServicesKit +import Core struct HeadlessWebView: UIViewRepresentable { weak var userScript: UserScriptMessaging? @@ -85,7 +86,7 @@ struct HeadlessWebView: UIViewRepresentable { // Enable content blocking rules if settings.contentBlocking { - let sourceProvider = DefaultScriptSourceProvider() + let sourceProvider = DefaultScriptSourceProvider(fireproofing: UserDefaultsFireproofing.xshared) let contentBlockerUserScript = ContentBlockerRulesUserScript(configuration: sourceProvider.contentBlockerRulesConfig) let contentScopeUserScript = ContentScopeUserScript(sourceProvider.privacyConfigurationManager, properties: sourceProvider.contentScopeProperties) diff --git a/DuckDuckGo/Subscription/PrivacyProDataReporting.swift b/DuckDuckGo/Subscription/PrivacyProDataReporting.swift index 43c2150504..86a65a29ed 100644 --- a/DuckDuckGo/Subscription/PrivacyProDataReporting.swift +++ b/DuckDuckGo/Subscription/PrivacyProDataReporting.swift @@ -127,7 +127,7 @@ final class PrivacyProDataReporter: PrivacyProDataReporting { secureVaultMaker: @escaping () -> (any AutofillSecureVault)? = { try? AutofillSecureVaultFactory.makeVault(reporter: SecureVaultReporter()) }, syncService: DDGSyncing? = nil, tabsModel: TabsModel? = nil, - fireproofing: Fireproofing = UserDefaultsFireproofing.shared, + fireproofing: Fireproofing, dateGenerator: @escaping () -> Date = Date.init) { self.configurationManager = configurationManager self.variantManager = variantManager diff --git a/DuckDuckGo/TabManager.swift b/DuckDuckGo/TabManager.swift index 2690c7b518..998ff5c25a 100644 --- a/DuckDuckGo/TabManager.swift +++ b/DuckDuckGo/TabManager.swift @@ -44,6 +44,8 @@ class TabManager { private let onboardingPixelReporter: OnboardingPixelReporting private let featureFlagger: FeatureFlagger private let textZoomCoordinator: TextZoomCoordinating + private let fireproofing: Fireproofing + private let websiteDataManager: WebsiteDataManaging private let subscriptionCookieManager: SubscriptionCookieManaging private let appSettings: AppSettings @@ -66,7 +68,9 @@ class TabManager { featureFlagger: FeatureFlagger, subscriptionCookieManager: SubscriptionCookieManaging, appSettings: AppSettings, - textZoomCoordinator: TextZoomCoordinating) { + textZoomCoordinator: TextZoomCoordinating, + websiteDataManager: WebsiteDataManaging, + fireproofing: Fireproofing) { self.model = model self.previewsSource = previewsSource self.bookmarksDatabase = bookmarksDatabase @@ -81,6 +85,8 @@ class TabManager { self.subscriptionCookieManager = subscriptionCookieManager self.appSettings = appSettings self.textZoomCoordinator = textZoomCoordinator + self.websiteDataManager = websiteDataManager + self.fireproofing = fireproofing registerForNotifications() } @@ -105,7 +111,9 @@ class TabManager { onboardingPixelReporter: onboardingPixelReporter, featureFlagger: featureFlagger, subscriptionCookieManager: subscriptionCookieManager, - textZoomCoordinator: textZoomCoordinator) + textZoomCoordinator: textZoomCoordinator, + websiteDataManager: websiteDataManager, + fireproofing: fireproofing) controller.applyInheritedAttribution(inheritedAttribution) controller.attachWebView(configuration: configuration, andLoadRequest: url == nil ? nil : URLRequest.userInitiated(url!), @@ -185,7 +193,9 @@ class TabManager { onboardingPixelReporter: onboardingPixelReporter, featureFlagger: featureFlagger, subscriptionCookieManager: subscriptionCookieManager, - textZoomCoordinator: textZoomCoordinator) + textZoomCoordinator: textZoomCoordinator, + websiteDataManager: websiteDataManager, + fireproofing: fireproofing) controller.attachWebView(configuration: configCopy, andLoadRequest: request, consumeCookies: !model.hasActiveTabs, diff --git a/DuckDuckGo/TabViewController.swift b/DuckDuckGo/TabViewController.swift index a5bbd41846..e96f54f302 100644 --- a/DuckDuckGo/TabViewController.swift +++ b/DuckDuckGo/TabViewController.swift @@ -332,7 +332,9 @@ class TabViewController: UIViewController { urlCredentialCreator: URLCredentialCreating = URLCredentialCreator(), featureFlagger: FeatureFlagger, subscriptionCookieManager: SubscriptionCookieManaging, - textZoomCoordinator: TextZoomCoordinating) -> TabViewController { + textZoomCoordinator: TextZoomCoordinating, + websiteDataManager: WebsiteDataManaging, + fireproofing: Fireproofing) -> TabViewController { let storyboard = UIStoryboard(name: "Tab", bundle: nil) let controller = storyboard.instantiateViewController(identifier: "TabViewController", creator: { coder in @@ -350,7 +352,9 @@ class TabViewController: UIViewController { urlCredentialCreator: urlCredentialCreator, featureFlagger: featureFlagger, subscriptionCookieManager: subscriptionCookieManager, - textZoomCoordinator: textZoomCoordinator + textZoomCoordinator: textZoomCoordinator, + fireproofing: fireproofing, + websiteDataManager: websiteDataManager ) }) return controller @@ -371,6 +375,7 @@ class TabViewController: UIViewController { let onboardingPixelReporter: OnboardingCustomInteractionPixelReporting let textZoomCoordinator: TextZoomCoordinating let fireproofing: Fireproofing + let websiteDataManager: WebsiteDataManaging required init?(coder aDecoder: NSCoder, tabModel: Tab, @@ -388,7 +393,8 @@ class TabViewController: UIViewController { featureFlagger: FeatureFlagger, subscriptionCookieManager: SubscriptionCookieManaging, textZoomCoordinator: TextZoomCoordinating, - fireproofing: Fireproofing = UserDefaultsFireproofing.shared) { + fireproofing: Fireproofing, + websiteDataManager: WebsiteDataManaging) { self.tabModel = tabModel self.appSettings = appSettings self.bookmarksDatabase = bookmarksDatabase @@ -410,6 +416,7 @@ class TabViewController: UIViewController { self.subscriptionCookieManager = subscriptionCookieManager self.textZoomCoordinator = textZoomCoordinator self.fireproofing = fireproofing + self.websiteDataManager = websiteDataManager super.init(coder: aDecoder) @@ -664,7 +671,7 @@ class TabViewController: UIViewController { Task { @MainActor in await webView.configuration.websiteDataStore.dataRecords(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes()) let cookieStore = webView.configuration.websiteDataStore.httpCookieStore - await WebCacheManager.shared.consumeCookies(httpCookieStore: cookieStore) + await websiteDataManager.consumeCookies(intoHTTPCookieStore: cookieStore) subscriptionCookieManager.resetLastRefreshDate() await subscriptionCookieManager.refreshSubscriptionCookie() doLoad() diff --git a/DuckDuckGo/TabViewControllerLongPressMenuExtension.swift b/DuckDuckGo/TabViewControllerLongPressMenuExtension.swift index 974fbbf895..0227757b55 100644 --- a/DuckDuckGo/TabViewControllerLongPressMenuExtension.swift +++ b/DuckDuckGo/TabViewControllerLongPressMenuExtension.swift @@ -114,7 +114,9 @@ extension TabViewController { onboardingPixelReporter: onboardingPixelReporter, featureFlagger: featureFlagger, subscriptionCookieManager: subscriptionCookieManager, - textZoomCoordinator: textZoomCoordinator) + textZoomCoordinator: textZoomCoordinator, + websiteDataManager: websiteDataManager, + fireproofing: fireproofing) tabController.isLinkPreview = true let configuration = WKWebViewConfiguration.nonPersistent()