From f83e4fea98516c69f1f815ed19462063e454e2a0 Mon Sep 17 00:00:00 2001 From: amddg44 Date: Thu, 14 Sep 2023 13:15:56 +0200 Subject: [PATCH 1/3] Added alternate url path to observations.db --- Core/WebCacheManager.swift | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/Core/WebCacheManager.swift b/Core/WebCacheManager.swift index 90935915f1..faeb75de9c 100644 --- a/Core/WebCacheManager.swift +++ b/Core/WebCacheManager.swift @@ -246,20 +246,28 @@ public class WebCacheManager { } private func removeObservationsData() { - guard let bundleID = Bundle.main.bundleIdentifier else { - return + if let pool = getValidDatabasePool() { + removeObservationsData(from: pool) + } else { + os_log("Could not find valid pool to clear observations data", log: .generalLog, type: .debug) } + } - let databaseURL = FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask)[0] - .appendingPathComponent("WebKit/\(bundleID)/WebsiteData/ResourceLoadStatistics/observations.db") + func getValidDatabasePool() -> DatabasePool? { + let bundleID = Bundle.main.bundleIdentifier ?? "" - guard let pool = try? DatabasePool(path: databaseURL.absoluteString) else { - return - } + let databaseURLs = [ + FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask)[0] + .appendingPathComponent("WebKit/WebsiteData/ResourceLoadStatistics/observations.db", + FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask)[0] + .appendingPathComponent("WebKit/\(bundleID)/WebsiteData/ResourceLoadStatistics/observations.db"), + ) + ] - removeObservationsData(from: pool) + return databaseURLs.lazy.compactMap({ try? DatabasePool(path: $0.absoluteString) }).first } + private func removeObservationsData(from pool: DatabasePool) { do { try pool.write { database in From 3fa53c894c2ea27ed3de6727fa5ab98f4c336397 Mon Sep 17 00:00:00 2001 From: amddg44 Date: Thu, 14 Sep 2023 13:16:38 +0200 Subject: [PATCH 2/3] Added test for accessing observations.db --- DuckDuckGoTests/WebCacheManagerTests.swift | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/DuckDuckGoTests/WebCacheManagerTests.swift b/DuckDuckGoTests/WebCacheManagerTests.swift index dd33061bae..e3d2aac34a 100644 --- a/DuckDuckGoTests/WebCacheManagerTests.swift +++ b/DuckDuckGoTests/WebCacheManagerTests.swift @@ -149,6 +149,11 @@ class WebCacheManagerTests: XCTestCase { XCTAssertEqual(dataStore.removeAllDataCalledCount, 1) } + + func testWhenAccessingObservationsDbThenValidDatabasePoolIsReturned() { + let pool = WebCacheManager.shared.getValidDatabasePool() + XCTAssertNotNil(pool, "DatabasePool should not be nil") + } // MARK: Mocks From e9f055259d3c53c65cdecae6ec9017d309c2c189 Mon Sep 17 00:00:00 2001 From: amddg44 Date: Thu, 14 Sep 2023 13:35:42 +0200 Subject: [PATCH 3/3] reorderd the urls --- Core/WebCacheManager.swift | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Core/WebCacheManager.swift b/Core/WebCacheManager.swift index faeb75de9c..d9d9c0a69f 100644 --- a/Core/WebCacheManager.swift +++ b/Core/WebCacheManager.swift @@ -258,10 +258,9 @@ public class WebCacheManager { let databaseURLs = [ FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask)[0] - .appendingPathComponent("WebKit/WebsiteData/ResourceLoadStatistics/observations.db", - FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask)[0] - .appendingPathComponent("WebKit/\(bundleID)/WebsiteData/ResourceLoadStatistics/observations.db"), - ) + .appendingPathComponent("WebKit/WebsiteData/ResourceLoadStatistics/observations.db"), + FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask)[0] + .appendingPathComponent("WebKit/\(bundleID)/WebsiteData/ResourceLoadStatistics/observations.db") ] return databaseURLs.lazy.compactMap({ try? DatabasePool(path: $0.absoluteString) }).first