Skip to content
This repository has been archived by the owner on Feb 24, 2025. It is now read-only.

Improve cleanup of observations.db #2010

Merged
merged 3 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions Core/WebCacheManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions DuckDuckGoTests/WebCacheManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down