Skip to content

Commit

Permalink
Refactor - auto update credential provider script
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] authored Nov 30, 2024
1 parent ab4bdbb commit 4a931e8
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 85 deletions.
49 changes: 1 addition & 48 deletions firefox-ios/Client/Assets/CC_Script/AutofillTelemetry.sys.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ class AutofillTelemetryBase {
EVENT_CATEGORY = null;
EVENT_OBJECT_FORM_INTERACTION = null;

SCALAR_DETECTED_SECTION_COUNT = null;
SCALAR_SUBMITTED_SECTION_COUNT = null;

HISTOGRAM_NUM_USES = null;
HISTOGRAM_PROFILE_NUM_USES = null;
HISTOGRAM_PROFILE_NUM_USES_KEY = null;
Expand Down Expand Up @@ -233,22 +230,6 @@ class AutofillTelemetryBase {
throw new Error("Not implemented.");
}

recordDetectedSectionCount() {
if (!this.SCALAR_DETECTED_SECTION_COUNT) {
return;
}

Services.telemetry.scalarAdd(this.SCALAR_DETECTED_SECTION_COUNT, 1);
}

recordSubmittedSectionCount(count) {
if (!this.SCALAR_SUBMITTED_SECTION_COUNT || !count) {
return;
}

Services.telemetry.scalarAdd(this.SCALAR_SUBMITTED_SECTION_COUNT, count);
}

recordNumberOfUse(records) {
let histogram = Services.telemetry.getKeyedHistogramById(
this.HISTOGRAM_PROFILE_NUM_USES
Expand Down Expand Up @@ -304,13 +285,6 @@ export class AddressTelemetry extends AutofillTelemetryBase {
EVENT_OBJECT_FORM_INTERACTION = "AddressForm";
EVENT_OBJECT_FORM_INTERACTION_EXT = "AddressFormExt";

SCALAR_DETECTED_SECTION_COUNT =
"formautofill.addresses.detected_sections_count";
SCALAR_SUBMITTED_SECTION_COUNT =
"formautofill.addresses.submitted_sections_count";
SCALAR_AUTOFILL_PROFILE_COUNT =
"formautofill.addresses.autofill_profiles_count";

HISTOGRAM_PROFILE_NUM_USES = "AUTOFILL_PROFILE_NUM_USES";
HISTOGRAM_PROFILE_NUM_USES_KEY = "address";

Expand Down Expand Up @@ -385,19 +359,14 @@ export class AddressTelemetry extends AutofillTelemetryBase {
}

recordAutofillProfileCount(count) {
Services.telemetry.scalarSet(this.SCALAR_AUTOFILL_PROFILE_COUNT, count);
Glean.formautofillAddresses.autofillProfilesCount.set(count);
}
}

class CreditCardTelemetry extends AutofillTelemetryBase {
EVENT_CATEGORY = "creditcard";
EVENT_OBJECT_FORM_INTERACTION = "CcFormV2";

SCALAR_DETECTED_SECTION_COUNT =
"formautofill.creditCards.detected_sections_count";
SCALAR_SUBMITTED_SECTION_COUNT =
"formautofill.creditCards.submitted_sections_count";

HISTOGRAM_NUM_USES = "CREDITCARD_NUM_USES";
HISTOGRAM_PROFILE_NUM_USES = "AUTOFILL_PROFILE_NUM_USES";
HISTOGRAM_PROFILE_NUM_USES_KEY = "credit_card";
Expand Down Expand Up @@ -511,22 +480,6 @@ export class AutofillTelemetry {
telemetry.recordFormInteractionEvent(method, flowId, fieldDetails, data);
}

/**
* Utility functions for submitted section count scalar (defined in Scalars.yaml)
*
* Category: formautofill.creditCards or formautofill.addresses
* Scalar name: submitted_sections_count
*/
static recordDetectedSectionCount(fieldDetails) {
const telemetry = this.#getTelemetryByFieldDetail(fieldDetails[0]);
telemetry.recordDetectedSectionCount();
}

static recordSubmittedSectionCount(fieldDetails, count) {
const telemetry = this.#getTelemetryByFieldDetail(fieldDetails[0]);
telemetry.recordSubmittedSectionCount(count);
}

static recordManageEvent(type, method) {
const telemetry = this.#getTelemetryByType(type);
telemetry.recordManageEvent(method);
Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,6 @@ export class FormAutofillSection {
return;
}

lazy.AutofillTelemetry.recordDetectedSectionCount(this.fieldDetails);
lazy.AutofillTelemetry.recordFormInteractionEvent(
"detected",
this.flowId,
Expand Down Expand Up @@ -388,7 +387,6 @@ export class FormAutofillSection {
onSubmitted(formFilledData) {
this.submitted = true;

lazy.AutofillTelemetry.recordSubmittedSectionCount(this.fieldDetails, 1);
lazy.AutofillTelemetry.recordFormInteractionEvent(
"submitted",
this.flowId,
Expand Down
51 changes: 21 additions & 30 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 @@ -163,30 +173,6 @@ export const Services = withNotImplementedError({
formatStringFromName: () => "",
}),
}),
telemetry: withNotImplementedError({
scalarAdd: (scalarName, scalarValue) => {
// For now, we only care about the address form telemetry
// TODO(FXCM-935): move address telemetry to Glean so we can remove this
// Data format of the sent message is:
// {
// type: "scalar",
// name: "formautofill.addresses.detected_sections_count",
// value: Number,
// }
if (scalarName !== "formautofill.addresses.detected_sections_count") {
return;
}

// eslint-disable-next-line no-undef
webkit.messageHandlers.addressFormTelemetryMessageHandler.postMessage(
JSON.stringify({
type: "scalar",
object: scalarName,
value: scalarValue,
})
);
},
}),
// TODO(FXCM-936): we should use crypto.randomUUID() instead of Services.uuid.generateUUID() in our codebase
// Underneath crypto.randomUUID() uses the same implementation as generateUUID()
// https://searchfox.org/mozilla-central/rev/d405168c4d3c0fb900a7354ae17bb34e939af996/dom/base/Crypto.cpp#96
Expand All @@ -205,6 +191,8 @@ 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 = {
// While moving away from Legacy Telemetry to Glean, the automated script generated the additional categories
// `creditcard` and `address`. After bug 1933961 all probes will have moved to category formautofillCreditcards and formautofillAddresses.
formautofillCreditcards: undefinedProxy(),
formautofill: undefinedProxy(),
creditcard: undefinedProxy(),
Expand Down Expand Up @@ -247,6 +235,9 @@ window.Glean = {
},
}
),
// Keeping unused category formautofillAddresses here, because Bug 1933961
// will move probes from the glean category address to formautofillAddresses
formautofillAddresses: undefinedProxy(),
};

const genericLogger = () =>
Expand Down
4 changes: 2 additions & 2 deletions firefox-ios/Client/Assets/CC_Script/addressFormLayout.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ function* convertLayoutToUI(fields, l10nStrings) {
const fieldTag = item.options
? "select"
: item.multiline
? "textarea"
: "input";
? "textarea"
: "input";

const fieldUI = {
label: {
Expand Down

0 comments on commit 4a931e8

Please sign in to comment.