Skip to content

Commit

Permalink
Refactor [vXXX] auto update credential provider script
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] authored May 1, 2024
1 parent 9b81a0c commit 152a034
Show file tree
Hide file tree
Showing 18 changed files with 568 additions and 447 deletions.
14 changes: 7 additions & 7 deletions Client/Assets/CC_Script/Constants.ios.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ const IOS_DEFAULT_PREFERENCES = {
"extensions.formautofill.creditCards.heuristics.fathom.testConfidence": 0,
"extensions.formautofill.creditCards.heuristics.fathom.types":
"cc-number,cc-name",
"extensions.formautofill.addresses.capture.requiredFields":
"street-address,postal-code,address-level1,address-level2",
"extensions.formautofill.loglevel": "Warn",
"extensions.formautofill.firstTimeUse": true,
"extensions.formautofill.addresses.supported": "off",
"extensions.formautofill.creditCards.supported": "detect",
"browser.search.region": "US",
"extensions.formautofill.creditCards.supportedCountries": "US,CA,GB,FR,DE",
"extensions.formautofill.addresses.enabled": false,
"extensions.formautofill.addresses.enabled": true,
"extensions.formautofill.addresses.experiments.enabled": true,
"extensions.formautofill.addresses.capture.enabled": false,
"extensions.formautofill.addresses.capture.v2.enabled": false,
"extensions.formautofill.addresses.supportedCountries": "",
"extensions.formautofill.creditCards.enabled": true,
"extensions.formautofill.reauth.enabled": true,
Expand All @@ -27,11 +28,10 @@ const IOS_DEFAULT_PREFERENCES = {
"extensions.formautofill.addresses.ignoreAutocompleteOff": true,
"extensions.formautofill.heuristics.enabled": true,
"extensions.formautofill.section.enabled": true,
// WebKit doesn't support the checkVisibility API, setting the threshold value to 0 to ensure
// `IsFieldVisible` function doesn't use it
"extensions.formautofill.heuristics.visibilityCheckThreshold": 0,
"extensions.formautofill.heuristics.interactivityCheckMode": "focusability",
"extensions.formautofill.heuristics.captureOnFormRemoval": false,
"extensions.formautofill.heuristics.captureOnPageNavigation": false,
"extensions.formautofill.focusOnAutofill": false,
"extensions.formautofill.test.ignoreVisibilityCheck": false,
};

// Used Mimic the behavior of .getAutocompleteInfo()
Expand Down
21 changes: 9 additions & 12 deletions Client/Assets/CC_Script/CreditCard.sys.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -454,10 +454,15 @@ export class CreditCard {
}

static formatMaskedNumber(maskedNumber) {
return {
affix: "****",
label: maskedNumber.replace(/^\**/, ""),
};
return "*".repeat(4) + maskedNumber.substr(-4);
}

static getMaskedNumber(number) {
return "*".repeat(4) + " " + number.substr(-4);
}

static getLongMaskedNumber(number) {
return "*".repeat(number.length - 4) + number.substr(-4);
}

static getCreditCardLogo(network) {
Expand Down Expand Up @@ -487,14 +492,6 @@ export class CreditCard {
}
}

static getMaskedNumber(number) {
return "*".repeat(4) + " " + number.substr(-4);
}

static getLongMaskedNumber(number) {
return "*".repeat(number.length - 4) + number.substr(-4);
}

/*
* Validates the number according to the Luhn algorithm. This
* method does not throw an exception if the number is invalid.
Expand Down
19 changes: 7 additions & 12 deletions Client/Assets/CC_Script/CreditCardRecord.sys.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,14 @@ export class CreditCardRecord {
}

static #normalizeCCNameFields(creditCard) {
if (
creditCard["cc-given-name"] ||
creditCard["cc-additional-name"] ||
creditCard["cc-family-name"]
) {
if (!creditCard["cc-name"]) {
creditCard["cc-name"] = FormAutofillNameUtils.joinNameParts({
given: creditCard["cc-given-name"],
middle: creditCard["cc-additional-name"],
family: creditCard["cc-family-name"],
});
}
if (!creditCard["cc-name"]) {
creditCard["cc-name"] = FormAutofillNameUtils.joinNameParts({
given: creditCard["cc-given-name"] ?? "",
middle: creditCard["cc-additional-name"] ?? "",
family: creditCard["cc-family-name"] ?? "",
});
}

delete creditCard["cc-given-name"];
delete creditCard["cc-additional-name"];
delete creditCard["cc-family-name"];
Expand Down
1 change: 1 addition & 0 deletions Client/Assets/CC_Script/CreditCardRuleset.sys.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ var FathomHeuristicsRegExp = {
"cc-name":
// Firefox-specific rules
"account.*holder.*name" +
"|^(credit[-\\s]?card|card).*name" +
// de-DE
"|^(kredit)?(karten|konto)inhaber" +
"|^(name).*karte" +
Expand Down
15 changes: 15 additions & 0 deletions Client/Assets/CC_Script/FieldScanner.sys.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
FormAutofillUtils: "resource://gre/modules/shared/FormAutofillUtils.sys.mjs",
});

/**
* Represents the detailed information about a form field, including
* the inferred field name, the approach used for inferring, and additional metadata.
Expand Down Expand Up @@ -32,6 +37,7 @@ export class FieldDetail {
section = "";
addressType = "";
contactType = "";
credentialType = "";

// When a field is split into N fields, we use part to record which field it is
// For example, a credit card number field is split into 4 fields, the value of
Expand All @@ -56,6 +62,7 @@ export class FieldDetail {
this.section = autocompleteInfo.section;
this.addressType = autocompleteInfo.addressType;
this.contactType = autocompleteInfo.contactType;
this.credentialType = autocompleteInfo.credentialType;
} else if (confidence) {
this.reason = "fathom";
this.confidence = confidence;
Expand All @@ -71,6 +78,14 @@ export class FieldDetail {
get sectionName() {
return this.section || this.addressType;
}

#isVisible = null;
get isVisible() {
if (this.#isVisible == null) {
this.#isVisible = lazy.FormAutofillUtils.isFieldVisible(this.element);
}
return this.#isVisible;
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion Client/Assets/CC_Script/FormAutofill.ios.sys.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import { FormAutofill } from "resource://autofill/FormAutofill.sys.mjs";

FormAutofill.defineLogGetter = (scope, logPrefix) => ({
FormAutofill.defineLogGetter = (_scope, _logPrefix) => ({
// TODO: Bug 1828405. Explore how logging should be handled.
// Maybe it makes more sense to do it on swift side and have JS just send messages.
info: () => {},
Expand Down
82 changes: 51 additions & 31 deletions Client/Assets/CC_Script/FormAutofill.sys.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";
import { Region } from "resource://gre/modules/Region.sys.mjs";
import { AddressMetaDataLoader } from "resource://gre/modules/shared/AddressMetaDataLoader.sys.mjs";

const ADDRESSES_FIRST_TIME_USE_PREF = "extensions.formautofill.firstTimeUse";
const AUTOFILL_ADDRESSES_AVAILABLE_PREF =
"extensions.formautofill.addresses.supported";
// This pref should be refactored after the migration of the old bool pref
Expand All @@ -18,8 +18,8 @@ const ENABLED_AUTOFILL_ADDRESSES_PREF =
"extensions.formautofill.addresses.enabled";
const ENABLED_AUTOFILL_ADDRESSES_CAPTURE_PREF =
"extensions.formautofill.addresses.capture.enabled";
const ENABLED_AUTOFILL_ADDRESSES_CAPTURE_V2_PREF =
"extensions.formautofill.addresses.capture.v2.enabled";
const ENABLED_AUTOFILL_ADDRESSES_CAPTURE_REQUIRED_FIELDS_PREF =
"extensions.formautofill.addresses.capture.requiredFields";
const ENABLED_AUTOFILL_ADDRESSES_SUPPORTED_COUNTRIES_PREF =
"extensions.formautofill.addresses.supportedCountries";
const ENABLED_AUTOFILL_CREDITCARDS_PREF =
Expand All @@ -33,14 +33,18 @@ const AUTOFILL_CREDITCARDS_AUTOCOMPLETE_OFF_PREF =
"extensions.formautofill.creditCards.ignoreAutocompleteOff";
const AUTOFILL_ADDRESSES_AUTOCOMPLETE_OFF_PREF =
"extensions.formautofill.addresses.ignoreAutocompleteOff";
const ENABLED_AUTOFILL_CAPTURE_ON_FORM_REMOVAL_PREF =
"extensions.formautofill.heuristics.captureOnFormRemoval";
const ENABLED_AUTOFILL_CAPTURE_ON_PAGE_NAVIGATION_PREF =
"extensions.formautofill.heuristics.captureOnPageNavigation";

export const FormAutofill = {
ENABLED_AUTOFILL_ADDRESSES_PREF,
ENABLED_AUTOFILL_ADDRESSES_CAPTURE_PREF,
ENABLED_AUTOFILL_ADDRESSES_CAPTURE_V2_PREF,
ENABLED_AUTOFILL_CAPTURE_ON_FORM_REMOVAL_PREF,
ENABLED_AUTOFILL_CAPTURE_ON_PAGE_NAVIGATION_PREF,
ENABLED_AUTOFILL_CREDITCARDS_PREF,
ENABLED_AUTOFILL_CREDITCARDS_REAUTH_PREF,
ADDRESSES_FIRST_TIME_USE_PREF,
AUTOFILL_CREDITCARDS_AUTOCOMPLETE_OFF_PREF,
AUTOFILL_ADDRESSES_AUTOCOMPLETE_OFF_PREF,

Expand Down Expand Up @@ -77,7 +81,9 @@ export const FormAutofill = {
return false;
},
isAutofillAddressesAvailableInCountry(country) {
return FormAutofill._addressAutofillSupportedCountries.includes(country);
return FormAutofill._addressAutofillSupportedCountries.includes(
country.toUpperCase()
);
},
get isAutofillEnabled() {
return this.isAutofillAddressesEnabled || this.isAutofillCreditCardsEnabled;
Expand All @@ -97,14 +103,25 @@ export const FormAutofill = {
/**
* Determines if the address autofill feature is available to use in the browser.
* If the feature is not available, then there are no user facing ways to enable it.
* Two conditions must be met for the autofill feature to be considered available:
* 1. Address autofill support is confirmed when:
* - `extensions.formautofill.addresses.supported` is set to `on`.
* - The user is located in a region supported by the feature
* (`extensions.formautofill.creditCards.supportedCountries`).
* 2. Address autofill is enabled through a Nimbus experiment:
* - The experiment pref `extensions.formautofill.addresses.experiments.enabled` is set to true.
*
* @returns {boolean} `true` if address autofill is available
*/
get isAutofillAddressesAvailable() {
return this._isSupportedRegion(
const isUserInSupportedRegion = this._isSupportedRegion(
FormAutofill._isAutofillAddressesAvailable,
FormAutofill._addressAutofillSupportedCountries
);
return (
isUserInSupportedRegion ||
FormAutofill._isAutofillAddressesAvailableInExperiment
);
},
/**
* Determines if the user has enabled or disabled credit card autofill.
Expand Down Expand Up @@ -204,11 +221,6 @@ XPCOMUtils.defineLazyPreferenceGetter(
"isAutofillAddressesCaptureEnabled",
ENABLED_AUTOFILL_ADDRESSES_CAPTURE_PREF
);
XPCOMUtils.defineLazyPreferenceGetter(
FormAutofill,
"isAutofillAddressesCaptureV2Enabled",
ENABLED_AUTOFILL_ADDRESSES_CAPTURE_V2_PREF
);
XPCOMUtils.defineLazyPreferenceGetter(
FormAutofill,
"_isAutofillCreditCardsAvailable",
Expand All @@ -224,11 +236,6 @@ XPCOMUtils.defineLazyPreferenceGetter(
"isAutofillCreditCardsHideUI",
AUTOFILL_CREDITCARDS_HIDE_UI_PREF
);
XPCOMUtils.defineLazyPreferenceGetter(
FormAutofill,
"isAutofillAddressesFirstTimeUse",
ADDRESSES_FIRST_TIME_USE_PREF
);
XPCOMUtils.defineLazyPreferenceGetter(
FormAutofill,
"_addressAutofillSupportedCountries",
Expand Down Expand Up @@ -259,18 +266,31 @@ XPCOMUtils.defineLazyPreferenceGetter(
"addressesAutocompleteOff",
AUTOFILL_ADDRESSES_AUTOCOMPLETE_OFF_PREF
);
XPCOMUtils.defineLazyPreferenceGetter(
FormAutofill,
"captureOnFormRemoval",
ENABLED_AUTOFILL_CAPTURE_ON_FORM_REMOVAL_PREF
);
XPCOMUtils.defineLazyPreferenceGetter(
FormAutofill,
"captureOnPageNavigation",
ENABLED_AUTOFILL_CAPTURE_ON_PAGE_NAVIGATION_PREF
);
XPCOMUtils.defineLazyPreferenceGetter(
FormAutofill,
"addressCaptureRequiredFields",
ENABLED_AUTOFILL_ADDRESSES_CAPTURE_REQUIRED_FIELDS_PREF,
null,
null,
val => val?.split(",").filter(v => !!v)
);

XPCOMUtils.defineLazyPreferenceGetter(
FormAutofill,
"_isAutofillAddressesAvailableInExperiment",
"extensions.formautofill.addresses.experiments.enabled"
);

// XXX: This should be invalidated on intl:app-locales-changed.
ChromeUtils.defineLazyGetter(FormAutofill, "countries", () => {
let availableRegionCodes =
Services.intl.getAvailableLocaleDisplayNames("region");
let displayNames = Services.intl.getRegionDisplayNames(
undefined,
availableRegionCodes
);
let result = new Map();
for (let i = 0; i < availableRegionCodes.length; i++) {
result.set(availableRegionCodes[i].toUpperCase(), displayNames[i]);
}
return result;
});
ChromeUtils.defineLazyGetter(FormAutofill, "countries", () =>
AddressMetaDataLoader.getCountries()
);
Loading

0 comments on commit 152a034

Please sign in to comment.