From f58608ebb49bf803e51019696d90bc608469e3a5 Mon Sep 17 00:00:00 2001 From: Dimi Date: Mon, 23 May 2022 20:39:57 +0000 Subject: [PATCH] Bug 1759418 - P2. Use Fathom to detect cc-number field r=sgalich Differential Revision: https://phabricator.services.mozilla.com/D146792 --- .../test/browser/browser_fathom_cc.js | 15 ++++-- dom/chrome-webidl/ChromeUtils.webidl | 9 ++-- .../formautofill/CreditCardRuleset.jsm | 11 +++-- .../formautofill/FormAutofillHeuristics.jsm | 11 +++-- .../formautofill/FormAutofillNative.cpp | 46 +++++++++++-------- 5 files changed, 56 insertions(+), 36 deletions(-) diff --git a/browser/extensions/formautofill/test/browser/browser_fathom_cc.js b/browser/extensions/formautofill/test/browser/browser_fathom_cc.js index 91e194549ff6..dc7af1a15ea7 100644 --- a/browser/extensions/formautofill/test/browser/browser_fathom_cc.js +++ b/browser/extensions/formautofill/test/browser/browser_fathom_cc.js @@ -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` diff --git a/dom/chrome-webidl/ChromeUtils.webidl b/dom/chrome-webidl/ChromeUtils.webidl index 7c7696d97311..f5877ec59588 100644 --- a/dom/chrome-webidl/ChromeUtils.webidl +++ b/dom/chrome-webidl/ChromeUtils.webidl @@ -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; }; /** diff --git a/toolkit/components/formautofill/CreditCardRuleset.jsm b/toolkit/components/formautofill/CreditCardRuleset.jsm index e12360a17cfd..0d2165a5eccc 100644 --- a/toolkit/components/formautofill/CreditCardRuleset.jsm +++ b/toolkit/components/formautofill/CreditCardRuleset.jsm @@ -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", ]; }, }; diff --git a/toolkit/components/formautofill/FormAutofillHeuristics.jsm b/toolkit/components/formautofill/FormAutofillHeuristics.jsm index e404ed2fc2de..bf828ee07184 100644 --- a/toolkit/components/formautofill/FormAutofillHeuristics.jsm +++ b/toolkit/components/formautofill/FormAutofillHeuristics.jsm @@ -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? diff --git a/toolkit/components/formautofill/FormAutofillNative.cpp b/toolkit/components/formautofill/FormAutofillNative.cpp index 41110c6bc3b1..5a48b96c47c3 100644 --- a/toolkit/components/formautofill/FormAutofillNative.cpp +++ b/toolkit/components/formautofill/FormAutofillNative.cpp @@ -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* labelStrings = GetLabelStrings( element, elementsToLabelStrings, elementsIdToLabelStrings); - const nsTArray* nextLabelStrings = GetLabelStrings( - nextFillableField, elementsToLabelStrings, elementsIdToLabelStrings); - const nsTArray* 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* nextLabelStrings = GetLabelStrings( + nextFillableField, elementsToLabelStrings, elementsIdToLabelStrings); + const nsTArray* 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