Skip to content

Commit

Permalink
Merge branch 'main' into diego/add-vpn-tips
Browse files Browse the repository at this point in the history
  • Loading branch information
diegoreymendez committed Oct 15, 2024
2 parents 888b67a + c62f077 commit dcc360c
Show file tree
Hide file tree
Showing 22 changed files with 453 additions and 224 deletions.
2 changes: 1 addition & 1 deletion Configuration/Version.xcconfig
Original file line number Diff line number Diff line change
@@ -1 +1 @@
MARKETING_VERSION = 7.140.0
MARKETING_VERSION = 7.141.0
4 changes: 2 additions & 2 deletions Core/AppPrivacyConfigurationDataProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import BrowserServicesKit
final public class AppPrivacyConfigurationDataProvider: EmbeddedDataProvider {

public struct Constants {
public static let embeddedDataETag = "\"6330c36f7ff354d26f32ee951e0a972e\""
public static let embeddedDataSHA = "40f77d6db1db544f06740b3290c5a72e5f03f706d17c9c0e05b13cd9255f2778"
public static let embeddedDataETag = "\"4ffe7d2b6c8e252d0289b1398cc2685d\""
public static let embeddedDataSHA = "9795ade4fdbc474688250f2ecfa097e917feea21a54fd97c524b851245d170e8"
}

public var embeddedDataEtag: String {
Expand Down
35 changes: 20 additions & 15 deletions Core/DailyPixel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
//

import Foundation
import Persistence

/// A variant of pixel that is fired at most once per day.
///
Expand Down Expand Up @@ -52,6 +53,8 @@ public final class DailyPixel {
error: Swift.Error? = nil,
withAdditionalParameters params: [String: String] = [:],
includedParameters: [Pixel.QueryParameters] = [.appVersion],
pixelFiring: PixelFiring.Type = Pixel.self,
dailyPixelStore: KeyValueStoring = DailyPixel.storage,
onComplete: @escaping (Swift.Error?) -> Void = { _ in }) {
var key: String = pixel.name

Expand All @@ -61,13 +64,13 @@ public final class DailyPixel {
key.append(":\(createSortedStringOfValues(from: errorParams))")
}

if !hasBeenFiredToday(forKey: key, dailyPixelStorage: storage) {
Pixel.fire(pixel: pixel,
error: error,
includedParameters: includedParameters,
withAdditionalParameters: params,
onComplete: onComplete)
updatePixelLastFireDate(forKey: key)
if !hasBeenFiredToday(forKey: key, dailyPixelStore: dailyPixelStore) {
pixelFiring.fire(pixel: pixel,
error: error,
includedParameters: includedParameters,
withAdditionalParameters: params,
onComplete: onComplete)
updatePixelLastFireDate(forKey: key, dailyPixelStore: dailyPixelStore)
} else {
onComplete(Error.alreadyFired)
}
Expand All @@ -80,12 +83,14 @@ public final class DailyPixel {
error: Swift.Error? = nil,
withAdditionalParameters params: [String: String] = [:],
includedParameters: [Pixel.QueryParameters] = [.appVersion],
pixelFiring: PixelFiring.Type = Pixel.self,
dailyPixelStore: KeyValueStoring = DailyPixel.storage,
onDailyComplete: @escaping (Swift.Error?) -> Void = { _ in },
onCountComplete: @escaping (Swift.Error?) -> Void = { _ in }) {
let key: String = pixel.name

if !hasBeenFiredToday(forKey: key, dailyPixelStorage: storage) {
Pixel.fire(
if !hasBeenFiredToday(forKey: key, dailyPixelStore: dailyPixelStore) {
pixelFiring.fire(
pixelNamed: pixel.name + "_d",
withAdditionalParameters: params,
includedParameters: includedParameters,
Expand All @@ -94,25 +99,25 @@ public final class DailyPixel {
} else {
onDailyComplete(Error.alreadyFired)
}
updatePixelLastFireDate(forKey: key)
updatePixelLastFireDate(forKey: key, dailyPixelStore: dailyPixelStore)
var newParams = params
if let error {
newParams.appendErrorPixelParams(error: error)
}
Pixel.fire(
pixelFiring.fire(
pixelNamed: pixel.name + "_c",
withAdditionalParameters: newParams,
includedParameters: includedParameters,
onComplete: onCountComplete
)
}

private static func updatePixelLastFireDate(forKey key: String) {
storage.set(Date(), forKey: key)
private static func updatePixelLastFireDate(forKey key: String, dailyPixelStore: KeyValueStoring) {
dailyPixelStore.set(Date(), forKey: key)
}

private static func hasBeenFiredToday(forKey key: String, dailyPixelStorage: UserDefaults) -> Bool {
if let lastFireDate = dailyPixelStorage.object(forKey: key) as? Date {
private static func hasBeenFiredToday(forKey key: String, dailyPixelStore: KeyValueStoring) -> Bool {
if let lastFireDate = dailyPixelStore.object(forKey: key) as? Date {
return Date().isSameDay(lastFireDate)
}
return false
Expand Down
28 changes: 25 additions & 3 deletions Core/PixelFiring.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,46 @@ public protocol PixelFiring {
includedParameters: [Pixel.QueryParameters],
onComplete: @escaping (Error?) -> Void)

static func fire(pixel: Pixel.Event,
error: Error?,
includedParameters: [Pixel.QueryParameters],
withAdditionalParameters params: [String: String],
onComplete: @escaping (Error?) -> Void)

static func fire(_ pixel: Pixel.Event,
withAdditionalParameters params: [String: String])

static func fire(pixelNamed pixelName: String,
withAdditionalParameters params: [String: String],
includedParameters: [Pixel.QueryParameters],
onComplete: @escaping (Error?) -> Void)
}

extension Pixel: PixelFiring {
public static func fire(_ pixel: Pixel.Event,
withAdditionalParameters params: [String: String],
includedParameters: [Pixel.QueryParameters],
onComplete: @escaping (Error?) -> Void) {
Pixel.fire(pixel: pixel,

Self.fire(pixel: pixel,
withAdditionalParameters: params,
includedParameters: includedParameters,
onComplete: onComplete)
}

public static func fire(_ pixel: Pixel.Event,
withAdditionalParameters params: [String: String]) {
Pixel.fire(pixel: pixel, withAdditionalParameters: params)
Self.fire(pixel: pixel, withAdditionalParameters: params)
}

public static func fire(pixelNamed pixelName: String,
withAdditionalParameters params: [String: String],
includedParameters: [Pixel.QueryParameters],
onComplete: @escaping (Error?) -> Void) {
Self.fire(pixelNamed: pixelName,
withAdditionalParameters: params,
allowedQueryReservedCharacters: nil,
includedParameters: includedParameters,
onComplete: onComplete)
}
}
40 changes: 34 additions & 6 deletions Core/WebCacheManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,29 @@ public class WebCacheManager {
dataStoreIdManager: DataStoreIdManaging = DataStoreIdManager.shared) async {

var cookiesToUpdate = [HTTPCookie]()
var leftoverContainerIDs = [UUID]()
if #available(iOS 17, *) {
cookiesToUpdate += await containerBasedClearing(storeIdManager: dataStoreIdManager) ?? []
let result = await containerBasedClearing(storeIdManager: dataStoreIdManager)
cookiesToUpdate += result.cookies
leftoverContainerIDs = result.leftoverContainerIDs
}

// Perform legacy clearing to migrate to new container
cookiesToUpdate += await legacyDataClearing() ?? []

cookieStorage.updateCookies(cookiesToUpdate, keepingPreservedLogins: logins)

// Attempt to clean up leftover stores again after a delay
// This should not be a problem as these containers are not supposed to be used anymore.
// If this fails, we are going to still clean them next time as WebKit keeps track of all stores for us.
if #available(iOS 17, *), !leftoverContainerIDs.isEmpty {
Task {
try? await Task.sleep(for: .seconds(3))
for uuid in leftoverContainerIDs {
try? await WKWebsiteDataStore.remove(forIdentifier: uuid)
}
}
}
}

}
Expand All @@ -113,18 +128,27 @@ extension WebCacheManager {
}

@available(iOS 17, *)
private func containerBasedClearing(storeIdManager: DataStoreIdManaging) async -> [HTTPCookie]? {
private func containerBasedClearing(storeIdManager: DataStoreIdManaging) async -> (cookies: [HTTPCookie],
leftoverContainerIDs: [UUID]) {
guard let containerId = storeIdManager.currentId else {
storeIdManager.invalidateCurrentIdAndAllocateNew()
return []
return ([], [])
}
storeIdManager.invalidateCurrentIdAndAllocateNew()

var leftoverContainerIDs = [UUID]()

var dataStore: WKWebsiteDataStore? = WKWebsiteDataStore(forIdentifier: containerId)
let cookies = await dataStore?.httpCookieStore.allCookies()
let cookies = await dataStore?.httpCookieStore.allCookies() ?? []
dataStore = nil

var uuids = await WKWebsiteDataStore.allDataStoreIdentifiers

// There may be a timing issue related to fetching current container, so append previous UUID as a precaution
if uuids.firstIndex(of: containerId) == nil {
uuids.append(containerId)
}

if let newContainerID = storeIdManager.currentId,
let newIdIndex = uuids.firstIndex(of: newContainerID) {
assertionFailure("Attempted to cleanup current Data Store")
Expand All @@ -133,11 +157,15 @@ extension WebCacheManager {

let previousLeftOversCount = max(0, uuids.count - 1) // -1 because one store is expected to be cleared
for uuid in uuids {
try? await WKWebsiteDataStore.remove(forIdentifier: uuid)
do {
try await WKWebsiteDataStore.remove(forIdentifier: uuid)
} catch {
leftoverContainerIDs.append(uuid)
}
}
await checkForLeftBehindDataStores(previousLeftOversCount: previousLeftOversCount)

return cookies
return (cookies, leftoverContainerIDs)
}

private func legacyDataClearing() async -> [HTTPCookie]? {
Expand Down
Loading

0 comments on commit dcc360c

Please sign in to comment.