Skip to content

Commit

Permalink
Refactor - auto update credential provider script (#23304)
Browse files Browse the repository at this point in the history
* Refactor - auto update credential provider script

* fix: pull fix from central

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Issam Mani <[email protected]>
  • Loading branch information
github-actions[bot] and issammani authored Nov 22, 2024
1 parent 97d6161 commit 4f73569
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 9 deletions.
1 change: 1 addition & 0 deletions firefox-ios/Client/Assets/CC_Script/Constants.ios.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const IOS_DEFAULT_PREFERENCES = {
"extensions.formautofill.test.ignoreVisibilityCheck": false,
"extensions.formautofill.heuristics.autofillSameOriginWithTop": false,
"signon.generation.confidenceThreshold": 0.75,
"extensions.formautofill.ml.experiment.enabled": false,
};

// Used Mimic the behavior of .getAutocompleteInfo()
Expand Down
8 changes: 8 additions & 0 deletions firefox-ios/Client/Assets/CC_Script/FieldScanner.sys.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

Expand Down Expand Up @@ -159,6 +160,13 @@ export class FieldDetail {
// Info required by heuristics
fieldDetail.maxLength = element.maxLength;

if (
lazy.FormAutofill.isMLExperimentEnabled &&
["input", "select"].includes(element.localName)
) {
fieldDetail.htmlMarkup = element.outerHTML.substring(0, 512);
}

return fieldDetail;
}
}
Expand Down
18 changes: 18 additions & 0 deletions firefox-ios/Client/Assets/CC_Script/FormAutofill.sys.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ export const FormAutofill = {
prefix: logPrefix,
});
},

get isMLExperimentEnabled() {
return FormAutofill._isMLEnabled && FormAutofill._isMLExperimentEnabled;
},
};

// TODO: Bug 1747284. Use Region.home instead of reading "browser.serach.region"
Expand Down Expand Up @@ -299,6 +303,20 @@ XPCOMUtils.defineLazyPreferenceGetter(
"extensions.formautofill.addresses.experiments.enabled"
);

XPCOMUtils.defineLazyPreferenceGetter(
FormAutofill,
"_isMLEnabled",
"browser.ml.enable",
false
);

XPCOMUtils.defineLazyPreferenceGetter(
FormAutofill,
"_isMLExperimentEnabled",
"extensions.formautofill.ml.experiment.enabled",
false
);

ChromeUtils.defineLazyGetter(FormAutofill, "countries", () =>
AddressMetaDataLoader.getCountries()
);
13 changes: 10 additions & 3 deletions firefox-ios/Client/Assets/CC_Script/FormAutofillHeuristics.sys.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -633,17 +633,24 @@ export const FormAutofillHeuristics = {
prevCCFields.add(detail.fieldName);
}

const isLastField =
scanner.getFieldDetailByIndex(scanner.parsingIndex + 1) === null;

// We update the "name" fields to "cc-name" fields when the following
// conditions are met:
// 1. The preceding fields are identified as credit card fields and
// contain the "cc-number" field.
// 2. No "cc-name-*" field is found among the preceding credit card fields.
// 3. The "cc-csc" field is not present among the preceding credit card fields.
// 3. The "cc-csc" field is either not present among the preceding credit card fields,
// or the current field is the last field in the form. This condition is in place
// because "cc-csc" is often the last field in a credit card form, and we want to
// avoid mistakenly updating fields in subsequent address forms.
if (
["cc-number"].some(f => prevCCFields.has(f)) &&
!["cc-name", "cc-given-name", "cc-family-name", "cc-csc"].some(f =>
!["cc-name", "cc-given-name", "cc-family-name"].some(f =>
prevCCFields.has(f)
)
) &&
(isLastField || !prevCCFields.has("cc-csc"))
) {
// If there is only one field, assume the name field a `cc-name` field
if (fields.length == 1) {
Expand Down
26 changes: 20 additions & 6 deletions firefox-ios/Client/Assets/CC_Script/Helpers.ios.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@ HTMLElement.prototype.ownerGlobal = window;

// We cannot mock this in WebKit because we lack access to low-level APIs.
// For completeness, we simply return true when the input type is "password".
HTMLInputElement.prototype.hasBeenTypePassword = function () {
return this.type === "password";
};
// NOTE: Since now we also include this file for password generator, it might be included multiple times
// which causes the defineProperty to throw. Allowing it to be overwritten for now is fine, since
// our code runs in a sandbox and only firefox code can overwrite it.
Object.defineProperty(HTMLInputElement.prototype, "hasBeenTypePassword", {
get() {
return this.type === "password";
},
configurable: true,
});

HTMLInputElement.prototype.setUserInput = function (value) {
this.value = value;
Expand Down Expand Up @@ -110,10 +116,14 @@ export const XPCOMUtils = withNotImplementedError({
onUpdate,
transform = val => val
) => {
if (!Object.keys(IOSAppConstants.prefs).includes(pref)) {
throw Error(`Pref ${pref} is not defined.`);
const value = IOSAppConstants.prefs[pref] ?? defaultValue;
// Explicitly check for null since false, "" and 0 are valid values
if (value === null) {
throw Error(
`Pref ${pref} is not defined and no valid default value was provided.`
);
}
obj[prop] = transform(IOSAppConstants.prefs[pref] ?? defaultValue);
obj[prop] = transform(value);
},
defineLazyModuleGetters(obj, modules) {
internalModuleResolvers.resolveModules(obj, modules);
Expand Down Expand Up @@ -181,7 +191,11 @@ window.Localization = function () {
// dispatches telemetry messages to the iOS, we need to modify typedefs in swift. For now, we map the telemetry events
// to the expected shape. FXCM-935 will tackle cleaning this up.
window.Glean = {
// TODO(FXCM-1453): While moving away from legacy scalars to glean ones, the automated script generated
// an additional category `formautofill.credit_cards`. This resulted into two different methods in our code
// with different casing for the c in Credit(c|C)ards. This is a temp fix until FXCM-1453 happens.
formautofillCreditcards: undefinedProxy(),
formautofillCreditCards: undefinedProxy(),
formautofill: undefinedProxy(),
creditcard: undefinedProxy(),
_mapGleanToLegacy: (eventName, { value, ...extra }) => {
Expand Down

0 comments on commit 4f73569

Please sign in to comment.