-
Notifications
You must be signed in to change notification settings - Fork 427
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
abstract datastore removal to make it testable
Showing
4 changed files
with
115 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// | ||
// WebsiteDataStoreCleaning.swift | ||
// DuckDuckGo | ||
// | ||
// Copyright © 2024 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import WebKit | ||
|
||
public protocol WebsiteDataStoreCleaning { | ||
|
||
func countContainers() async -> Int | ||
func removeAllContainersAfterDelay(previousCount: Int) | ||
|
||
} | ||
|
||
public class DefaultWebsiteDataStoreCleaner: WebsiteDataStoreCleaning { | ||
|
||
public init() { } | ||
|
||
public func countContainers() async -> Int { | ||
guard #available(iOS 17, *) else { return 0 } | ||
return await WKWebsiteDataStore.allDataStoreIdentifiers.count | ||
} | ||
|
||
public func removeAllContainersAfterDelay(previousCount: Int) { | ||
guard #available(iOS 17, *) else { return } | ||
|
||
// Attempt to clean up all previous stores, but wait for a few seconds. | ||
// If this fails, we are going to still clean them next time as WebKit keeps track of all stores for us. | ||
Task { | ||
try? await Task.sleep(interval: 3.0) | ||
for uuid in await WKWebsiteDataStore.allDataStoreIdentifiers { | ||
try? await WKWebsiteDataStore.remove(forIdentifier: uuid) | ||
} | ||
|
||
await checkForLeftBehindDataStores(previousLeftOversCount: previousCount) | ||
} | ||
} | ||
|
||
private func checkForLeftBehindDataStores(previousLeftOversCount: Int) async { | ||
guard #available(iOS 17, *) else { return } | ||
|
||
let params = [ | ||
"left_overs_count": "\(previousLeftOversCount)" | ||
] | ||
|
||
let ids = await WKWebsiteDataStore.allDataStoreIdentifiers | ||
if ids.count > 1 { | ||
Pixel.fire(pixel: .debugWebsiteDataStoresNotClearedMultiple, withAdditionalParameters: params) | ||
} else if ids.count > 0 { | ||
Pixel.fire(pixel: .debugWebsiteDataStoresNotClearedOne, withAdditionalParameters: params) | ||
} else if previousLeftOversCount > 0 { | ||
Pixel.fire(pixel: .debugWebsiteDataStoresCleared, withAdditionalParameters: params) | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters