Skip to content

Commit 198f700

Browse files
Refactor - auto update credential provider script
1 parent 3a57bd0 commit 198f700

7 files changed

+45
-225
lines changed

firefox-ios/Client/Assets/CC_Script/AutofillTelemetry.sys.mjs

-47
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ class AutofillTelemetryBase {
1212
EVENT_CATEGORY = null;
1313
EVENT_OBJECT_FORM_INTERACTION = null;
1414

15-
HISTOGRAM_NUM_USES = null;
16-
HISTOGRAM_PROFILE_NUM_USES = null;
17-
HISTOGRAM_PROFILE_NUM_USES_KEY = null;
18-
1915
#initFormEventExtra(value) {
2016
let extra = {};
2117
for (const field of Object.values(this.SUPPORTED_FIELDS)) {
@@ -183,17 +179,6 @@ class AutofillTelemetryBase {
183179
throw new Error("Not implemented.");
184180
}
185181

186-
recordNumberOfUse(records) {
187-
let histogram = Services.telemetry.getKeyedHistogramById(
188-
this.HISTOGRAM_PROFILE_NUM_USES
189-
);
190-
histogram.clear();
191-
192-
for (let record of records) {
193-
histogram.add(this.HISTOGRAM_PROFILE_NUM_USES_KEY, record.timesUsed);
194-
}
195-
}
196-
197182
recordIframeLayoutDetection(flowId, fieldDetails) {
198183
const fieldsInMainFrame = [];
199184
const fieldsInIframe = [];
@@ -238,9 +223,6 @@ export class AddressTelemetry extends AutofillTelemetryBase {
238223
EVENT_OBJECT_FORM_INTERACTION = "AddressForm";
239224
EVENT_OBJECT_FORM_INTERACTION_EXT = "AddressFormExt";
240225

241-
HISTOGRAM_PROFILE_NUM_USES = "AUTOFILL_PROFILE_NUM_USES";
242-
HISTOGRAM_PROFILE_NUM_USES_KEY = "address";
243-
244226
// Fields that are recorded in `address_form` and `address_form_ext` telemetry
245227
SUPPORTED_FIELDS = {
246228
"street-address": "street_address",
@@ -316,10 +298,6 @@ class CreditCardTelemetry extends AutofillTelemetryBase {
316298
EVENT_CATEGORY = "creditcard";
317299
EVENT_OBJECT_FORM_INTERACTION = "CcFormV2";
318300

319-
HISTOGRAM_NUM_USES = "CREDITCARD_NUM_USES";
320-
HISTOGRAM_PROFILE_NUM_USES = "AUTOFILL_PROFILE_NUM_USES";
321-
HISTOGRAM_PROFILE_NUM_USES_KEY = "credit_card";
322-
323301
// Mapping of field name used in formautofill code to the field name
324302
// used in the telemetry.
325303
SUPPORTED_FIELDS = {
@@ -369,23 +347,6 @@ class CreditCardTelemetry extends AutofillTelemetryBase {
369347
}
370348
}
371349

372-
recordNumberOfUse(records) {
373-
super.recordNumberOfUse(records);
374-
375-
if (!this.HISTOGRAM_NUM_USES) {
376-
return;
377-
}
378-
379-
let histogram = Services.telemetry.getHistogramById(
380-
this.HISTOGRAM_NUM_USES
381-
);
382-
histogram.clear();
383-
384-
for (let record of records) {
385-
histogram.add(record.timesUsed);
386-
}
387-
}
388-
389350
recordAutofillProfileCount(count) {
390351
Glean.formautofillCreditcards.autofillProfilesCount.set(count);
391352
}
@@ -463,14 +424,6 @@ export class AutofillTelemetry {
463424
telemetry.recordAutofillProfileCount(count);
464425
}
465426

466-
/**
467-
* Utility functions for address/credit card number of use
468-
*/
469-
static recordNumberOfUse(type, records) {
470-
const telemetry = this.#getTelemetryByType(type);
471-
telemetry.recordNumberOfUse(records);
472-
}
473-
474427
static recordFormSubmissionHeuristicCount(label) {
475428
Glean.formautofill.formSubmissionHeuristic[label].add(1);
476429
}

firefox-ios/Client/Assets/CC_Script/FormAutofillSection.sys.mjs

+22-17
Original file line numberDiff line numberDiff line change
@@ -333,27 +333,32 @@ export class FormAutofillSection {
333333
return data;
334334
}
335335

336+
shouldAutofillField(fieldDetail) {
337+
// We don't save security code, but if somehow the profile has securty code,
338+
// make sure we don't autofill it.
339+
if (fieldDetail.fieldName == "cc-csc") {
340+
return false;
341+
}
342+
343+
// When both visible and invisible elements exist, we only autofill the
344+
// visible element.
345+
if (!fieldDetail.isVisible) {
346+
return !this.fieldDetails.some(
347+
field => field.fieldName == fieldDetail.fieldName && field.isVisible
348+
);
349+
}
350+
351+
return true;
352+
}
353+
336354
/**
337355
* Heuristics to determine which fields to autofill when a section contains
338356
* multiple fields of the same type.
339357
*/
340358
getAutofillFields() {
341-
return this.fieldDetails.filter(fieldDetail => {
342-
// We don't save security code, but if somehow the profile has securty code,
343-
// make sure we don't autofill it.
344-
if (fieldDetail.fieldName == "cc-csc") {
345-
return false;
346-
}
347-
348-
// When both visible and invisible elements exist, we only autofill the
349-
// visible element.
350-
if (!fieldDetail.isVisible) {
351-
return !this.fieldDetails.some(
352-
field => field.fieldName == fieldDetail.fieldName && field.isVisible
353-
);
354-
}
355-
return true;
356-
});
359+
return this.fieldDetails.filter(fieldDetail =>
360+
this.shouldAutofillField(fieldDetail)
361+
);
357362
}
358363

359364
/*
@@ -645,7 +650,6 @@ export class FormAutofillCreditCardSection extends FormAutofillSection {
645650
result = decrypted ? "success" : "fail_user_canceled";
646651
} catch (ex) {
647652
result = "fail_error";
648-
throw ex;
649653
} finally {
650654
Glean.formautofill.promptShownOsReauth.record({
651655
trigger: "autofill",
@@ -677,6 +681,7 @@ export class FormAutofillCreditCardSection extends FormAutofillSection {
677681
} catch (e) {
678682
errorResult = e.result;
679683
if (e.result != Cr.NS_ERROR_ABORT) {
684+
this.log.warn(`Decryption failed with result: ${e.result}`);
680685
throw e;
681686
}
682687
this.log.warn("User canceled encryption login");

firefox-ios/Client/Assets/CC_Script/Helpers.ios.mjs

+3-14
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
import { IOSAppConstants } from "resource://gre/modules/shared/Constants.ios.mjs";
66
import Overrides from "resource://gre/modules/Overrides.ios.js";
77

8-
const EMPTY_MODULE_PATH = "EmptyModule.sys.mjs";
9-
108
/* eslint mozilla/use-isInstance: 0 */
119
HTMLSelectElement.isInstance = element => element instanceof HTMLSelectElement;
1210
HTMLInputElement.isInstance = element => element instanceof HTMLInputElement;
@@ -30,13 +28,6 @@ Object.defineProperty(HTMLInputElement.prototype, "hasBeenTypePassword", {
3028
configurable: true,
3129
});
3230

33-
Object.defineProperty(HTMLInputElement.prototype, "nodePrincipal", {
34-
get() {
35-
return { isNullPrincipal: false };
36-
},
37-
configurable: true,
38-
});
39-
4031
function setUserInput(value) {
4132
this.value = value;
4233

@@ -109,15 +100,13 @@ const internalModuleResolvers = {
109100
const moduleName = moduleURI.split("/").pop();
110101
const modulePath =
111102
"./" + (Overrides.ModuleOverrides[moduleName] ?? moduleName);
112-
return { module: moduleResolver(modulePath), path: modulePath };
103+
return moduleResolver(modulePath);
113104
},
114105

115106
resolveModules(obj, modules) {
116107
for (const [exportName, moduleURI] of Object.entries(modules)) {
117108
const resolvedModule = this.resolveModule(moduleURI);
118-
obj[exportName] = resolvedModule.path.includes(EMPTY_MODULE_PATH)
119-
? resolvedModule.module?.default
120-
: resolvedModule.module?.[exportName];
109+
obj[exportName] = resolvedModule?.[exportName];
121110
}
122111
},
123112
};
@@ -158,7 +147,7 @@ export const ChromeUtils = withNotImplementedError({
158147
internalModuleResolvers.resolveModules(obj, modules);
159148
},
160149
importESModule(moduleURI) {
161-
return internalModuleResolvers.resolveModule(moduleURI)?.module;
150+
return internalModuleResolvers.resolveModule(moduleURI);
162151
},
163152
});
164153
window.ChromeUtils = ChromeUtils;

firefox-ios/Client/Assets/CC_Script/HeuristicsRegExp.sys.mjs

+5-2
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ export const HeuristicsRegExp = {
5656
"address-line3": "addrline3|address_3|addl3",
5757
"address-level2": "città", // it-IT
5858
"address-housenumber":
59-
"house\\s*number|hausnummer|haus|house[a-z\-]*n(r|o)",
59+
"(house|building)\\s*number|hausnummer|haus|house[a-z\-]*n(r|o)" +
60+
"|n[úu]mero",
6061
"address-level3":
6162
"(^address-?level-?3$)" +
6263
"|neighbou*rhood|barrio|bairro|colonia|suburb", // en/es/pt/mx/au/nz
@@ -653,10 +654,12 @@ export const HeuristicsRegExp = {
653654
"address-line2":
654655
"address|line" +
655656
"|house|building|apartment|floor" + // de-DE
657+
"|apartamento|" + // pt
656658
"|adresse" + // fr-FR
657659
"|indirizzo" + // it-IT
658660
"|地址" + // zh-CN
659-
"|주소", // ko-KR
661+
"|주소" + // ko-KR
662+
"|mieszkan(ie|ia)", // pl-PL
660663
},
661664
],
662665

firefox-ios/Client/Assets/CC_Script/LabelUtils.sys.mjs

+15-43
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@ export const LabelUtils = {
2323
// @type {Map<string, array>}
2424
_mappedLabels: null,
2525

26-
// An array consisting of label elements whose correponding form field doesn't
27-
// have an id attribute.
28-
// @type {Array<[HTMLLabelElement, HTMLElement]>}
29-
_unmappedLabelControls: null,
30-
3126
// A weak map consisting of label element and extracted strings pairs.
3227
// @type {WeakMap<HTMLLabelElement, array>}
3328
_labelStrings: null,
@@ -178,41 +173,29 @@ export const LabelUtils = {
178173

179174
generateLabelMap(doc) {
180175
this._mappedLabels = new Map();
181-
this._unmappedLabelControls = [];
182176
this._labelStrings = new WeakMap();
183177

184178
// A map of potential label -> control for labels that don't have an id or
185179
// control associated with them. Labels that have ids or associated controls
186180
// will be placed in _mappedLabels.
187181
let potentialLabels = new Map();
188-
189182
for (let label of doc.querySelectorAll("label")) {
190-
let id = label.htmlFor;
191-
let control;
192-
if (!id) {
193-
control = label.control;
194-
if (!control) {
195-
// If the label has no control, look for the next input or select
196-
// element in the document and add that to the potentialLabels list.
197-
control = this.findAdjacentControl(label, potentialLabels);
198-
if (control) {
199-
potentialLabels.set(control, label);
200-
} else {
201-
continue;
202-
}
203-
}
204-
id = control.id;
205-
}
206-
if (id) {
207-
let labels = this._mappedLabels.get(id);
183+
let control = label.control;
184+
if (control) {
185+
const controlId = lazy.FormAutofillUtils.getElementIdentifier(control);
186+
let labels = this._mappedLabels.get(controlId);
208187
if (labels) {
209188
labels.push(label);
210189
} else {
211-
this._mappedLabels.set(id, [label]);
190+
this._mappedLabels.set(controlId, [label]);
212191
}
213192
} else {
214-
// control must be non-empty here
215-
this._unmappedLabelControls.push({ label, control });
193+
// If the label has no control, look for the next input or select
194+
// element in the document and add that to the potentialLabels list.
195+
control = this.findAdjacentControl(label, potentialLabels);
196+
if (control) {
197+
potentialLabels.set(control, label);
198+
}
216199
}
217200
}
218201

@@ -221,22 +204,16 @@ export const LabelUtils = {
221204
// control that is nearby even when it has no for attribute or doesn't match an id.
222205
if (potentialLabels.size) {
223206
for (let label of potentialLabels) {
224-
if (
225-
!this._unmappedLabelControls.some(e => e.control == label[0]) &&
226-
(!label[1].id || !this._mappedLabels.has(label[1].id))
227-
) {
228-
this._unmappedLabelControls.push({
229-
label: label[1],
230-
control: label[0],
231-
});
207+
const elementId = lazy.FormAutofillUtils.getElementIdentifier(label[0]);
208+
if (!this._mappedLabels.has(elementId)) {
209+
this._mappedLabels.set(elementId, [label[1]]);
232210
}
233211
}
234212
}
235213
},
236214

237215
clearLabelMap() {
238216
this._mappedLabels = null;
239-
this._unmappedLabelControls = null;
240217
this._labelStrings = null;
241218
},
242219

@@ -245,12 +222,7 @@ export const LabelUtils = {
245222
this.generateLabelMap(element.ownerDocument);
246223
}
247224

248-
let id = element.id;
249-
if (!id) {
250-
return this._unmappedLabelControls
251-
.filter(lc => lc.control == element)
252-
.map(lc => lc.label);
253-
}
225+
let id = lazy.FormAutofillUtils.getElementIdentifier(element);
254226
return this._mappedLabels.get(id) || [];
255227
},
256228
};

0 commit comments

Comments
 (0)