diff --git a/Core/WebCacheManager.swift b/Core/WebCacheManager.swift index 90935915f1..d9d9c0a69f 100644 --- a/Core/WebCacheManager.swift +++ b/Core/WebCacheManager.swift @@ -246,20 +246,27 @@ 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 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