Skip to content

Commit

Permalink
migration logic and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
brindy committed Nov 22, 2024
1 parent 1e73bca commit fcbdf72
Show file tree
Hide file tree
Showing 19 changed files with 140 additions and 83 deletions.
4 changes: 3 additions & 1 deletion Core/Fireproofing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
35 changes: 23 additions & 12 deletions Core/WebCacheManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand All @@ -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()

}
Expand Down Expand Up @@ -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
Expand Down
15 changes: 13 additions & 2 deletions DuckDuckGo/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
6 changes: 4 additions & 2 deletions DuckDuckGo/ContentBlockingUpdating.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion DuckDuckGo/CookieDebugViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
2 changes: 1 addition & 1 deletion DuckDuckGo/Favicons.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 7 additions & 3 deletions DuckDuckGo/FireproofingSettingsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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())
}
}

Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion DuckDuckGo/ImageCacheDebugViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions DuckDuckGo/MainViewController+Segues.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
13 changes: 9 additions & 4 deletions DuckDuckGo/MainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ class MainViewController: UIViewController {
}

let fireproofing: Fireproofing
let websiteDataManager: WebsiteDataManaging
let textZoomCoordinator: TextZoomCoordinating

var historyManager: HistoryManaging
Expand All @@ -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
Expand All @@ -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()
Expand All @@ -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)

Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion DuckDuckGo/RootDebugViewController+VanillaBrowser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
Loading

0 comments on commit fcbdf72

Please sign in to comment.