diff --git a/apps/browser/src/autofill/enums/autofill-field.enums.ts b/apps/browser/src/autofill/enums/autofill-field.enums.ts index 68408f2b671..197e88259fb 100644 --- a/apps/browser/src/autofill/enums/autofill-field.enums.ts +++ b/apps/browser/src/autofill/enums/autofill-field.enums.ts @@ -1,3 +1,4 @@ +/** @deprecated use `AutofillFieldQualifier` in `libs/common/src/autofill/constants` instead */ export const AutofillFieldQualifier = { password: "password", newPassword: "newPassword", @@ -26,5 +27,6 @@ export const AutofillFieldQualifier = { identityUsername: "identityUsername", } as const; +/** @deprecated use `AutofillFieldQualifierType` in `libs/common/src/autofill/types` instead */ export type AutofillFieldQualifierType = (typeof AutofillFieldQualifier)[keyof typeof AutofillFieldQualifier]; diff --git a/libs/common/src/autofill/constants/index.ts b/libs/common/src/autofill/constants/index.ts index dc79e27b6aa..7d14ac7245d 100644 --- a/libs/common/src/autofill/constants/index.ts +++ b/libs/common/src/autofill/constants/index.ts @@ -72,6 +72,34 @@ export const AutofillOverlayVisibility = { OnFieldFocus: 2, } as const; +export const AutofillFieldQualifier = { + password: "password", + newPassword: "newPassword", + username: "username", + cardholderName: "cardholderName", + cardNumber: "cardNumber", + cardExpirationMonth: "cardExpirationMonth", + cardExpirationYear: "cardExpirationYear", + cardExpirationDate: "cardExpirationDate", + cardCvv: "cardCvv", + identityTitle: "identityTitle", + identityFirstName: "identityFirstName", + identityMiddleName: "identityMiddleName", + identityLastName: "identityLastName", + identityFullName: "identityFullName", + identityAddress1: "identityAddress1", + identityAddress2: "identityAddress2", + identityAddress3: "identityAddress3", + identityCity: "identityCity", + identityState: "identityState", + identityPostalCode: "identityPostalCode", + identityCountry: "identityCountry", + identityCompany: "identityCompany", + identityPhone: "identityPhone", + identityEmail: "identityEmail", + identityUsername: "identityUsername", +} as const; + export const BrowserClientVendors = { Chrome: "Chrome", Opera: "Opera", diff --git a/libs/common/src/autofill/services/domain-settings.service.ts b/libs/common/src/autofill/services/domain-settings.service.ts index d6ab8851ad7..f4345885b22 100644 --- a/libs/common/src/autofill/services/domain-settings.service.ts +++ b/libs/common/src/autofill/services/domain-settings.service.ts @@ -24,6 +24,7 @@ import { UserKeyDefinition, } from "../../platform/state"; import { UserId } from "../../types/guid"; +import { AutofillTargetingRulesByDomain, AutofillTargetingRules } from "../types"; const SHOW_FAVICONS = new KeyDefinition(DOMAIN_SETTINGS_DISK, "showFavicons", { deserializer: (value: boolean) => value ?? true, @@ -57,10 +58,23 @@ const DEFAULT_URI_MATCH_STRATEGY = new UserKeyDefinition( }, ); +const AUTOFILL_TARGETING_RULES = new UserKeyDefinition( + DOMAIN_SETTINGS_DISK, + "autofillTargetingRules", + { + deserializer: (value: AutofillTargetingRulesByDomain) => value ?? {}, + clearOn: [], + }, +); + /** * The Domain Settings service; provides client settings state for "active client view" URI concerns */ export abstract class DomainSettingsService { + autofillTargetingRules$: Observable; + setAutofillTargetingRules: (newValue: AutofillTargetingRulesByDomain) => Promise; + getUrlAutofillTargetingRules$: (url: string) => Observable; + /** * Indicates if the favicons for ciphers' URIs should be shown instead of a placeholder */ @@ -112,6 +126,9 @@ export abstract class DomainSettingsService { } export class DefaultDomainSettingsService implements DomainSettingsService { + private autofillTargetingRulesState: ActiveUserState; + readonly autofillTargetingRules$: Observable; + private showFaviconsState: GlobalState; readonly showFavicons$: Observable; @@ -136,6 +153,9 @@ export class DefaultDomainSettingsService implements DomainSettingsService { private policyService: PolicyService, private accountService: AccountService, ) { + this.autofillTargetingRulesState = this.stateProvider.getActive(AUTOFILL_TARGETING_RULES); + this.autofillTargetingRules$ = this.autofillTargetingRulesState.state$.pipe(map((x) => (x && Object.keys(x).length) ? x : {})); + this.showFaviconsState = this.stateProvider.getGlobal(SHOW_FAVICONS); this.showFavicons$ = this.showFaviconsState.state$.pipe(map((x) => x ?? true)); @@ -182,6 +202,10 @@ export class DefaultDomainSettingsService implements DomainSettingsService { ); } + async setAutofillTargetingRules(newValue: AutofillTargetingRulesByDomain): Promise { + await this.autofillTargetingRulesState.update(() => newValue); + } + async setShowFavicons(newValue: boolean): Promise { await this.showFaviconsState.update(() => newValue); } @@ -218,4 +242,18 @@ export class DefaultDomainSettingsService implements DomainSettingsService { return domains$; } + + getUrlAutofillTargetingRules$(url: string): Observable { + return this.autofillTargetingRules$.pipe( + map((autofillTargetingRules) => { + const domain = Utils.getHostname(url); + + if (domain == null) { + return {}; + } + + return autofillTargetingRules?.[domain] || {}; + }), + ); + } } diff --git a/libs/common/src/autofill/types/index.ts b/libs/common/src/autofill/types/index.ts index 9a5a434d9e2..5affa75c190 100644 --- a/libs/common/src/autofill/types/index.ts +++ b/libs/common/src/autofill/types/index.ts @@ -1,4 +1,5 @@ import { + AutofillFieldQualifier, AutofillOverlayVisibility, BrowserClientVendors, BrowserShortcutsUris, @@ -16,3 +17,14 @@ export type BrowserClientVendor = (typeof BrowserClientVendors)[keyof typeof Bro export type BrowserShortcutsUri = (typeof BrowserShortcutsUris)[keyof typeof BrowserShortcutsUris]; export type DisablePasswordManagerUri = (typeof DisablePasswordManagerUris)[keyof typeof DisablePasswordManagerUris]; + +export type AutofillFieldQualifierType = + (typeof AutofillFieldQualifier)[keyof typeof AutofillFieldQualifier]; + +export type AutofillTargetingRules = { + [type in AutofillFieldQualifierType]?: string; +}; + +export type AutofillTargetingRulesByDomain = { + [key: string]: AutofillTargetingRules +};