Bug 1404531 - [Form Autofill] Features on submission should be disabled when the prefs are set to false. r=steveck draft
authorLuke Chang <lchang@mozilla.com>
Sat, 30 Sep 2017 18:36:10 +0800
changeset 673023 31f41e5bc03cfd1b969c59c292879b87d49e51be
parent 672978 57f68296c350469d73d788eb3695a898947b4acb
child 733982 78a542e1cdc69be1cd990680164bbf252d4f5e4e
push id82440
push userbmo:lchang@mozilla.com
push dateSat, 30 Sep 2017 10:39:34 +0000
reviewerssteveck
bugs1404531
milestone58.0a1
Bug 1404531 - [Form Autofill] Features on submission should be disabled when the prefs are set to false. r=steveck MozReview-Commit-ID: F1DfnpFUVYg
browser/extensions/formautofill/FormAutofillContent.jsm
browser/extensions/formautofill/FormAutofillHandler.jsm
browser/extensions/formautofill/test/unit/test_onFormSubmitted.js
--- a/browser/extensions/formautofill/FormAutofillContent.jsm
+++ b/browser/extensions/formautofill/FormAutofillContent.jsm
@@ -388,16 +388,21 @@ var FormAutofillContent = {
    *
    * @param {HTMLElement} formElement Root element which receives earlyformsubmit event.
    * @param {Object} domWin Content window
    * @returns {boolean} Should always return true so form submission isn't canceled.
    */
   notify(formElement, domWin) {
     this.log.debug("Notifying form early submission");
 
+    if (!FormAutofillUtils.isAutofillEnabled) {
+      this.log.debug("Form Autofill is disabled");
+      return true;
+    }
+
     if (domWin && PrivateBrowsingUtils.isContentWindowPrivate(domWin)) {
       this.log.debug("Ignoring submission in a private window");
       return true;
     }
 
     let handler = this._formsDetails.get(formElement);
     if (!handler) {
       this.log.debug("Form element could not map to an existing handler");
--- a/browser/extensions/formautofill/FormAutofillHandler.jsm
+++ b/browser/extensions/formautofill/FormAutofillHandler.jsm
@@ -545,18 +545,26 @@ FormAutofillHandler.prototype = {
    *          be omitted if there's no valid fields. A record object consists of
    *          three properties:
    *            - guid: The id of the previously-filled profile or null if omitted.
    *            - record: A valid record converted from details with trimmed result.
    *            - untouchedFields: Fields that aren't touched after autofilling.
    */
   createRecords() {
     let data = {};
+    let target = [];
 
-    ["address", "creditCard"].forEach(type => {
+    if (FormAutofillUtils.isAutofillAddressesEnabled) {
+      target.push("address");
+    }
+    if (FormAutofillUtils.isAutofillCreditCardsEnabled) {
+      target.push("creditCard");
+    }
+
+    target.forEach(type => {
       let details = this[type].fieldDetails;
       if (!details || details.length == 0) {
         return;
       }
 
       data[type] = {
         guid: this[type].filledRecordGUID,
         record: {},
--- a/browser/extensions/formautofill/test/unit/test_onFormSubmitted.js
+++ b/browser/extensions/formautofill/test/unit/test_onFormSubmitted.js
@@ -478,16 +478,74 @@ add_task(async function handle_earlyform
   let fakeForm = MOCK_DOC.createElement("form");
   sinon.spy(FormAutofillContent, "_onFormSubmit");
 
   do_check_eq(FormAutofillContent.notify(fakeForm), true);
   do_check_eq(FormAutofillContent._onFormSubmit.called, false);
   FormAutofillContent._onFormSubmit.restore();
 });
 
+add_task(async function autofill_disabled() {
+  let form = MOCK_DOC.getElementById("form1");
+  form.reset();
+
+  let testcase = {
+    "street-addr": "331 E. Evelyn Avenue",
+    "country": "US",
+    "tel": "+16509030800",
+    "cc-number": "1111222233334444",
+  };
+  for (let key in testcase) {
+    let input = MOCK_DOC.getElementById(key);
+    input.value = testcase[key];
+  }
+
+  let element = MOCK_DOC.getElementById(TARGET_ELEMENT_ID);
+  FormAutofillContent.identifyAutofillFields(element);
+
+  sinon.stub(FormAutofillContent, "_onFormSubmit");
+
+  // "_onFormSubmit" shouldn't be called if both "addresses" and "creditCards"
+  // are disabled.
+  Services.prefs.setBoolPref("extensions.formautofill.addresses.enabled", false);
+  Services.prefs.setBoolPref("extensions.formautofill.creditCards.enabled", false);
+  FormAutofillContent.notify(form);
+  do_check_eq(FormAutofillContent._onFormSubmit.called, false);
+  FormAutofillContent._onFormSubmit.reset();
+
+  // "_onFormSubmit" should be called as usual.
+  Services.prefs.clearUserPref("extensions.formautofill.addresses.enabled");
+  Services.prefs.clearUserPref("extensions.formautofill.creditCards.enabled");
+  FormAutofillContent.notify(form);
+  do_check_eq(FormAutofillContent._onFormSubmit.called, true);
+  do_check_neq(FormAutofillContent._onFormSubmit.args[0][0].address, undefined);
+  do_check_neq(FormAutofillContent._onFormSubmit.args[0][0].creditCard, undefined);
+  FormAutofillContent._onFormSubmit.reset();
+
+  // "address" should be empty if "addresses" pref is disabled.
+  Services.prefs.setBoolPref("extensions.formautofill.addresses.enabled", false);
+  FormAutofillContent.notify(form);
+  do_check_eq(FormAutofillContent._onFormSubmit.called, true);
+  do_check_eq(FormAutofillContent._onFormSubmit.args[0][0].address, undefined);
+  do_check_neq(FormAutofillContent._onFormSubmit.args[0][0].creditCard, undefined);
+  FormAutofillContent._onFormSubmit.reset();
+  Services.prefs.clearUserPref("extensions.formautofill.addresses.enabled");
+
+  // "creditCard" should be empty if "creditCards" pref is disabled.
+  Services.prefs.setBoolPref("extensions.formautofill.creditCards.enabled", false);
+  FormAutofillContent.notify(form);
+  do_check_eq(FormAutofillContent._onFormSubmit.called, true);
+  do_check_neq(FormAutofillContent._onFormSubmit.args[0][0].address, undefined);
+  do_check_eq(FormAutofillContent._onFormSubmit.args[0][0].creditCard, undefined);
+  FormAutofillContent._onFormSubmit.reset();
+  Services.prefs.clearUserPref("extensions.formautofill.creditCards.enabled");
+
+  FormAutofillContent._onFormSubmit.restore();
+});
+
 TESTCASES.forEach(testcase => {
   add_task(async function check_records_saving_is_called_correctly() {
     do_print("Starting testcase: " + testcase.description);
 
     let form = MOCK_DOC.getElementById("form1");
     form.reset();
     for (let key in testcase.formValue) {
       let input = MOCK_DOC.getElementById(key);