diff --git a/Package.resolved b/Package.resolved index 491a98c2c..dadbd9a5c 100644 --- a/Package.resolved +++ b/Package.resolved @@ -23,8 +23,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/duckduckgo/duckduckgo-autofill.git", "state" : { - "revision" : "6053999d6af384a716ab0ce7205dbab5d70ed1b3", - "version" : "11.0.1" + "revision" : "10aeff1ec7f533d1705233a9b14f9393a699b1c0", + "version" : "11.0.2" } }, { diff --git a/Package.swift b/Package.swift index 725131db6..8cd51ddc5 100644 --- a/Package.swift +++ b/Package.swift @@ -38,7 +38,7 @@ let package = Package( .library(name: "PixelKitTestingUtilities", targets: ["PixelKitTestingUtilities"]), ], dependencies: [ - .package(url: "https://github.com/duckduckgo/duckduckgo-autofill.git", exact: "11.0.1"), + .package(url: "https://github.com/duckduckgo/duckduckgo-autofill.git", exact: "11.0.2"), .package(url: "https://github.com/duckduckgo/GRDB.swift.git", exact: "2.3.0"), .package(url: "https://github.com/duckduckgo/TrackerRadarKit", exact: "2.0.0"), .package(url: "https://github.com/duckduckgo/sync_crypto", exact: "0.2.0"), diff --git a/Sources/BrowserServicesKit/SecureVault/AutofillDatabaseProvider.swift b/Sources/BrowserServicesKit/SecureVault/AutofillDatabaseProvider.swift index a03e551f8..73470a359 100644 --- a/Sources/BrowserServicesKit/SecureVault/AutofillDatabaseProvider.swift +++ b/Sources/BrowserServicesKit/SecureVault/AutofillDatabaseProvider.swift @@ -26,6 +26,7 @@ import SecureStorage public protocol AutofillDatabaseProvider: SecureStorageDatabaseProvider { func accounts() throws -> [SecureVaultModels.WebsiteAccount] + func accountsCount() throws -> Int func hasAccountFor(username: String?, domain: String?) throws -> Bool @discardableResult @@ -119,6 +120,13 @@ public final class DefaultAutofillDatabaseProvider: GRDBSecureStorageDatabasePro } } + public func accountsCount() throws -> Int { + let count = try db.read { + try SecureVaultModels.WebsiteAccount.fetchCount($0) + } + return count + } + public func hasAccountFor(username: String?, domain: String?) throws -> Bool { let account = try db.read { try SecureVaultModels.WebsiteAccount diff --git a/Sources/BrowserServicesKit/SecureVault/AutofillSecureVault.swift b/Sources/BrowserServicesKit/SecureVault/AutofillSecureVault.swift index 865c54268..498a7a2b7 100644 --- a/Sources/BrowserServicesKit/SecureVault/AutofillSecureVault.swift +++ b/Sources/BrowserServicesKit/SecureVault/AutofillSecureVault.swift @@ -54,6 +54,8 @@ public protocol AutofillSecureVault: SecureVault { func resetL2Password(oldPassword: Data?, newPassword: Data) throws func accounts() throws -> [SecureVaultModels.WebsiteAccount] + func accountsCount() throws -> Int + func accountsCountBucket() throws -> String func accountsFor(domain: String) throws -> [SecureVaultModels.WebsiteAccount] func accountsWithPartialMatchesFor(eTLDplus1: String) throws -> [SecureVaultModels.WebsiteAccount] func hasAccountFor(username: String?, domain: String?) throws -> Bool @@ -228,6 +230,33 @@ public class DefaultAutofillSecureVault: AutofillSe } } + public func accountsCount() throws -> Int { + lock.lock() + defer { + lock.unlock() + } + + do { + return try self.providers.database.accountsCount() + } catch { + throw SecureStorageError.databaseError(cause: error) + } + } + + public func accountsCountBucket() throws -> String { + let accountsCount = try accountsCount() + + if accountsCount < 3 { + return "none" + } else if accountsCount < 11 { + return "some" + } else if accountsCount < 50 { + return "many" + } else { + return "lots" + } + } + public func accountsFor(domain: String) throws -> [SecureVaultModels.WebsiteAccount] { lock.lock() defer { diff --git a/Tests/BrowserServicesKitTests/SecureVault/MockAutofillDatabaseProvider.swift b/Tests/BrowserServicesKitTests/SecureVault/MockAutofillDatabaseProvider.swift index de4613553..7e674a1bf 100644 --- a/Tests/BrowserServicesKitTests/SecureVault/MockAutofillDatabaseProvider.swift +++ b/Tests/BrowserServicesKitTests/SecureVault/MockAutofillDatabaseProvider.swift @@ -96,6 +96,10 @@ internal class MockAutofillDatabaseProvider: AutofillDatabaseProvider { return _accounts } + func accountsCount() throws -> Int { + return _accounts.count + } + func notes() throws -> [SecureVaultModels.Note] { return _notes }