Bug 1759418 - P2. Use Fathom to detect cc-number field r=sgalich

Differential Revision: https://phabricator.services.mozilla.com/D146792
This commit is contained in:
Dimi 2022-05-23 20:39:57 +00:00
Родитель 417adfdbd3
Коммит f58608ebb4
5 изменённых файлов: 56 добавлений и 36 удалений

Просмотреть файл

@ -2,7 +2,7 @@
* By default this test only tests 1 sample. This is to avoid publishing all samples we have
* to the codebase. If you update the Fathom CC model, please follow the instruction below
* and run the test. Doing this makes sure the optimized (Native implementation) CC fathom model produces
* exactly the same result as the non-optimized model (JS implementation, See creditCardRuleset.jsm).
* exactly the same result as the non-optimized model (JS implementation, See CreditCardRuleset.jsm).
*
* To test this:
* 1. Run the test setup script (fathom/test-setup.sh) to download all samples to the local
@ -101,14 +101,23 @@ async function run_test(path, dirs) {
);
}
// This value should sync with the number of supported types in
// CreditCardRuleset.jsm (See `get types()` in `this.creditCardRulesets`).
const EXPECTED_NUM_OF_CONFIDENCE = 1;
for (let i = 0; i < eligibleFields.length; i++) {
if (Object.keys(nativeConfidencesKeyedByType[i]).length != 6) {
if (
Object.keys(nativeConfidencesKeyedByType[i]).length !=
EXPECTED_NUM_OF_CONFIDENCE
) {
ok(
false,
`Native CC model doesn't get confidence value for all types`
);
}
if (Object.keys(jsConfidencesKeyedByType[i]).length != 6) {
if (
Object.keys(jsConfidencesKeyedByType[i]).length !=
EXPECTED_NUM_OF_CONFIDENCE
) {
ok(
false,
`JS CC model doesn't get confidence value for all types`

Просмотреть файл

@ -61,13 +61,12 @@ dictionary InteractionData {
unsigned long scrollingDistanceInPixels = 0;
};
/**
* Confidence value of credit card fields. This is used by the native
* Fathom Credit Card model to return the score to JS.
*/
dictionary FormAutofillConfidences {
double ccNumber = 0;
double ccName = 0;
double ccType = 0;
double ccExp = 0;
double ccExpMonth = 0;
double ccExpYear = 0;
};
/**

Просмотреть файл

@ -1199,12 +1199,13 @@ var creditCardRulesets = {
get types() {
return [
"cc-name",
// Only use Fathom to detect cc-number fields for now.
"cc-number",
"cc-exp-month",
"cc-exp-year",
"cc-exp",
"cc-type",
//"cc-name",
//"cc-exp-month",
//"cc-exp-year",
//"cc-exp",
//"cc-type",
];
},
};

Просмотреть файл

@ -881,7 +881,10 @@ this.FormAutofillHeuristics = {
// The heuristic below should be covered by fathom rules, so we can skip doing
// it.
if (FormAutofillUtils.isFathomCreditCardsEnabled()) {
if (
FormAutofillUtils.isFathomCreditCardsEnabled() &&
creditCardRulesets.types.includes(detail.fieldName)
) {
fieldScanner.parsingIndex++;
return true;
}
@ -1151,10 +1154,10 @@ this.FormAutofillHeuristics = {
let fathomFields = fields.filter(r =>
creditCardRulesets.types.includes(r)
);
let fathomField = scanner.getFathomField(element, fathomFields);
let matchedFieldName = scanner.getFathomField(element, fathomFields);
// At this point, use fathom's recommendation if it has one
if (fathomField) {
return infoRecordWithFieldName(fathomField);
if (matchedFieldName) {
return infoRecordWithFieldName(matchedFieldName);
}
// TODO: Do we want to run old heuristics for fields that fathom isn't confident?

Просмотреть файл

@ -1171,27 +1171,16 @@ void FormAutofillImpl::GetFormAutofillConfidences(
auto& params = paramSet[i];
const auto& element = aElements[i];
Element* nextFillableField = NextField(aElements, i);
Element* prevFillableField = PrevField(aElements, i);
const nsTArray<nsCString>* labelStrings = GetLabelStrings(
element, elementsToLabelStrings, elementsIdToLabelStrings);
const nsTArray<nsCString>* nextLabelStrings = GetLabelStrings(
nextFillableField, elementsToLabelStrings, elementsIdToLabelStrings);
const nsTArray<nsCString>* prevLabelStrings = GetLabelStrings(
prevFillableField, elementsToLabelStrings, elementsIdToLabelStrings);
bool idOrNameMatchDwfrmAndBml =
IdOrNameMatchRegExp(element, RegexKey::DWFRM) &&
IdOrNameMatchRegExp(element, RegexKey::BML);
bool idOrNameMatchFirstAndLast =
IdOrNameMatchRegExp(element, RegexKey::FIRST) &&
IdOrNameMatchRegExp(element, RegexKey::LAST);
bool hasTemplatedValue = HasTemplatedValue(element);
bool inputTypeNotNumbery = InputTypeNotNumbery(element);
bool idOrNameMatchSubscription =
IdOrNameMatchRegExp(element, RegexKey::SUBSCRIPTION);
bool roleIsMenu = RoleIsMenu(element);
#define RULE_IMPL2(rule, type) params.m##type##Params[type##Params::rule]
#define RULE_IMPL(rule, type) RULE_IMPL2(rule, type)
@ -1222,6 +1211,22 @@ void FormAutofillImpl::GetFormAutofillConfidences(
RULE(inputTypeNotNumbery) = inputTypeNotNumbery;
#undef RULE_TYPE
// We only use Fathom to detect credit card number field for now.
// Comment out code below instead of removing them to make it clear that
// the current design is to support multiple rules.
/*
Element* nextFillableField = NextField(aElements, i);
Element* prevFillableField = PrevField(aElements, i);
const nsTArray<nsCString>* nextLabelStrings = GetLabelStrings(
nextFillableField, elementsToLabelStrings, elementsIdToLabelStrings);
const nsTArray<nsCString>* prevLabelStrings = GetLabelStrings(
prevFillableField, elementsToLabelStrings, elementsIdToLabelStrings);
bool idOrNameMatchFirstAndLast =
IdOrNameMatchRegExp(element, RegexKey::FIRST) &&
IdOrNameMatchRegExp(element, RegexKey::LAST);
bool roleIsMenu = RoleIsMenu(element);
// cc-name
#define RULE_TYPE CCName
RULE(idOrNameMatchNameRegExp) =
@ -1425,25 +1430,28 @@ void FormAutofillImpl::GetFormAutofillConfidences(
RULE(idOrNameMatchDwfrmAndBml) = idOrNameMatchDwfrmAndBml;
RULE(hasTemplatedValue) = hasTemplatedValue;
#undef RULE_TYPE
*/
#undef RULE_IMPL2
#undef RULE_IMPL
#undef RULE
// Calculating the final score of each rule
FormAutofillConfidences score;
#define CALCULATE_SCORE(type, score) \
for (auto i : MakeEnumeratedRange(type##Params::Count)) { \
(score) += params.m##type##Params[i] * kCoefficents.m##type##Params[i]; \
} \
(score) = Sigmoid(score + k##type##Bias);
// Calculating the final score of each rule
FormAutofillConfidences score;
CALCULATE_SCORE(CCNumber, score.mCcNumber)
CALCULATE_SCORE(CCName, score.mCcName)
CALCULATE_SCORE(CCType, score.mCcType)
CALCULATE_SCORE(CCExp, score.mCcExp)
CALCULATE_SCORE(CCExpMonth, score.mCcExpMonth)
CALCULATE_SCORE(CCExpYear, score.mCcExpYear)
// Comment out code that are used for other rules.
// CALCULATE_SCORE(CCName, score.mCcName)
// CALCULATE_SCORE(CCType, score.mCcType)
// CALCULATE_SCORE(CCExp, score.mCcExp)
// CALCULATE_SCORE(CCExpMonth, score.mCcExpMonth)
// CALCULATE_SCORE(CCExpYear, score.mCcExpYear)
#undef CALCULATE_SCORE