Skip to content

Commit

Permalink
Merge branch 'main' into fcappelli/prev_sub_breakage
Browse files Browse the repository at this point in the history
  • Loading branch information
federicocappelli committed Jan 11, 2024
2 parents 924da96 + bee5c1b commit 357e216
Show file tree
Hide file tree
Showing 20 changed files with 458 additions and 273 deletions.
41 changes: 27 additions & 14 deletions Core/CookieStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,36 @@ public class CookieStorage {
private var userDefaults: UserDefaults

var cookies: [HTTPCookie] {

var storedCookies = [HTTPCookie]()
if let cookies = userDefaults.object(forKey: Constants.key) as? [[String: Any?]] {
for cookieData in cookies {
var properties = [HTTPCookiePropertyKey: Any]()
cookieData.forEach({
properties[HTTPCookiePropertyKey(rawValue: $0.key)] = $0.value
})

if let cookie = HTTPCookie(properties: properties) {
os_log("read cookie %s %s %s", log: .generalLog, type: .debug, cookie.domain, cookie.name, cookie.value)
storedCookies.append(cookie)
get {
var storedCookies = [HTTPCookie]()
if let cookies = userDefaults.object(forKey: Constants.key) as? [[String: Any?]] {
for cookieData in cookies {
var properties = [HTTPCookiePropertyKey: Any]()
cookieData.forEach({
properties[HTTPCookiePropertyKey(rawValue: $0.key)] = $0.value
})

if let cookie = HTTPCookie(properties: properties) {
os_log("read cookie %s %s %s", log: .generalLog, type: .debug, cookie.domain, cookie.name, cookie.value)
storedCookies.append(cookie)
}
}
}

return storedCookies
}

set {
var cookies = [[String: Any?]]()
newValue.forEach { cookie in
var mappedCookie = [String: Any?]()
cookie.properties?.forEach {
mappedCookie[$0.key.rawValue] = $0.value
}
cookies.append(mappedCookie)
}
userDefaults.setValue(cookies, forKey: Constants.key)
}

return storedCookies
}

public init(userDefaults: UserDefaults = UserDefaults.app) {
Expand Down
14 changes: 8 additions & 6 deletions Core/PixelEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -399,8 +399,6 @@ extension Pixel {

case cookieDeletionTimedOut
case cookieDeletionLeftovers
case legacyCookieMigration
case legacyCookieCleanupError

case cachedTabPreviewsExceedsTabCount
case cachedTabPreviewRemovalError
Expand Down Expand Up @@ -439,6 +437,10 @@ extension Pixel {
case debugCantSaveBookmarkFix

case debugCannotClearObservationsDatabase
case debugWebsiteDataStoresNotClearedMultiple
case debugWebsiteDataStoresNotClearedOne
case debugCookieCleanupError

case debugBookmarksMigratedMoreThanOnce

// Return user measurement
Expand Down Expand Up @@ -882,8 +884,6 @@ extension Pixel.Event {

case .cookieDeletionTimedOut: return "m_d_csto"
case .cookieDeletionLeftovers: return "m_cookie_deletion_leftovers"
case .legacyCookieMigration: return "m_legacy_cookie_migration"
case .legacyCookieCleanupError: return "m_legacy_cookie_cleanup_error"

case .cachedTabPreviewsExceedsTabCount: return "m_d_tpetc"
case .cachedTabPreviewRemovalError: return "m_d_tpre"
Expand All @@ -909,8 +909,10 @@ extension Pixel.Event {
case .debugCantSaveBookmarkFix: return "m_d_cant_save_bookmark_fix"

case .debugCannotClearObservationsDatabase: return "m_d_cannot_clear_observations_database"


case .debugWebsiteDataStoresNotClearedMultiple: return "m_d_wkwebsitedatastoresnotcleared_multiple"
case .debugWebsiteDataStoresNotClearedOne: return "m_d_wkwebsitedatastoresnotcleared_one"
case .debugCookieCleanupError: return "m_cookie_cleanup_error"

// MARK: Ad Attribution

case .adAttributionGlobalAttributedRulesDoNotExist: return "m_attribution_global_attributed_rules_do_not_exist"
Expand Down
21 changes: 0 additions & 21 deletions Core/PreserveLogins.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,18 @@ public class PreserveLogins {
public struct Notifications {
public static let loginDetectionStateChanged = Foundation.Notification.Name("com.duckduckgo.ios.PreserveLogins.loginDetectionStateChanged")
}

struct Keys {
static let legacyDetectedDomains = "com.duckduckgo.ios.PreserveLogins.userDecision.detectedDomains"
static let legacyUserDecision = "com.duckduckgo.ios.PreserveLogins.userDecision"
static let legacyUserPrompted = "com.duckduckgo.ios.PreserveLogins.userPrompted"
static let legacyAllowedDomains = UserDefaultsWrapper<Any>.Key.preserveLoginsLegacyAllowedDomains.rawValue
}

public static let shared = PreserveLogins()

@UserDefaultsWrapper(key: .preserveLoginsAllowedDomains, defaultValue: [])
private(set) public var allowedDomains: [String]

@UserDefaultsWrapper(key: .preserveLoginsLegacyAllowedDomains, defaultValue: [])
private(set) public var legacyAllowedDomains: [String]

@UserDefaultsWrapper(key: .preserveLoginsDetectionEnabled, defaultValue: false)
public var loginDetectionEnabled: Bool {
didSet {
NotificationCenter.default.post(name: Notifications.loginDetectionStateChanged, object: nil)
}
}

init() {
UserDefaults.app.removeObject(forKey: Keys.legacyUserDecision)
UserDefaults.app.removeObject(forKey: Keys.legacyUserPrompted)
UserDefaults.app.removeObject(forKey: Keys.legacyDetectedDomains)
}

public func addToAllowed(domain: String) {
allowedDomains += [domain]
Expand All @@ -73,11 +57,6 @@ public class PreserveLogins {
allowedDomains = []
}

public func clearLegacyAllowedDomains() {
// This doesn't get cleared in init because it might need to be migrated
UserDefaults.app.removeObject(forKey: Keys.legacyAllowedDomains)
}

public func isAllowed(fireproofDomain domain: String) -> Bool {
return allowedDomains.contains(domain)
}
Expand Down
2 changes: 2 additions & 0 deletions Core/UserDefaultsPropertyWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ public struct UserDefaultsWrapper<T> {
case addressBarPosition = "com.duckduckgo.ios.addressbarposition"
case showFullSiteAddress = "com.duckduckgo.ios.showfullsiteaddress"

case webContainerId = "com.duckduckgo.ios.webcontainer.id"

case bookmarksLastGoodVersion = "com.duckduckgo.ios.bookmarksLastGoodVersion"
case bookmarksMigrationVersion = "com.duckduckgo.ios.bookmarksMigrationVersion"

Expand Down
35 changes: 33 additions & 2 deletions Core/WKWebViewConfigurationExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,15 @@ import WebKit

extension WKWebViewConfiguration {

public static func persistent() -> WKWebViewConfiguration {
return configuration(persistsData: true)
@MainActor
public static func persistent(idManager: DataStoreIdManager = .shared) -> WKWebViewConfiguration {
let config = configuration(persistsData: true)

// Only use a container if there's an id which will be allocated next time the fire button is used.
if #available(iOS 17, *), let containerId = idManager.id {
config.websiteDataStore = WKWebsiteDataStore(forIdentifier: containerId)
}
return config
}

public static func nonPersistent() -> WKWebViewConfiguration {
Expand All @@ -48,3 +55,27 @@ extension WKWebViewConfiguration {
}

}

public class DataStoreIdManager {

public static let shared = DataStoreIdManager()

@UserDefaultsWrapper(key: .webContainerId, defaultValue: nil)
private var containerId: String?

var id: UUID? {
if let containerId {
return UUID(uuidString: containerId)
}
return nil
}

var hasId: Bool {
return containerId != nil
}

public func allocateNewContainerId() {
self.containerId = UUID().uuidString
}

}
Loading

0 comments on commit 357e216

Please sign in to comment.