Bug 1371113 - Part 2:Show doorhanger and save/disable credit card. r=lchang draft
authorsteveck-chung <schung@mozilla.com>
Wed, 09 Aug 2017 15:43:28 +0800
changeset 653788 01954afe0698f5a2613f21c7bd28c226ca901bf9
parent 653787 fa2ffc5eec81907502772f5506c8f3a2b14b201c
child 653789 f8c57f8d6a39d8b8e087523cefefdb7943c9293c
push id76407
push userbmo:schung@mozilla.com
push dateMon, 28 Aug 2017 01:55:45 +0000
reviewerslchang
bugs1371113
milestone57.0a1
Bug 1371113 - Part 2:Show doorhanger and save/disable credit card. r=lchang MozReview-Commit-ID: EcAL3ANrrGb
browser/extensions/formautofill/FormAutofillHandler.jsm
browser/extensions/formautofill/FormAutofillParent.jsm
--- a/browser/extensions/formautofill/FormAutofillHandler.jsm
+++ b/browser/extensions/formautofill/FormAutofillHandler.jsm
@@ -560,16 +560,17 @@ FormAutofillHandler.prototype = {
     if (data.address &&
         Object.keys(data.address.record).length < FormAutofillUtils.AUTOFILL_FIELDS_THRESHOLD) {
       log.debug("No address record saving since there are only",
                      Object.keys(data.address.record).length,
                      "usable fields");
       delete data.address;
     }
 
-    if (data.creditCard && !data.creditCard.record["cc-number"]) {
-      log.debug("No credit card record saving since card number is empty");
+    if (data.creditCard && (!data.creditCard.record["cc-number"] ||
+        !FormAutofillUtils.isCCNumber(data.creditCard.record["cc-number"]))) {
+      log.debug("No credit card record saving since card number is invalid");
       delete data.creditCard;
     }
 
     return data;
   },
 };
--- a/browser/extensions/formautofill/FormAutofillParent.jsm
+++ b/browser/extensions/formautofill/FormAutofillParent.jsm
@@ -321,19 +321,17 @@ FormAutofillParent.prototype = {
       Services.ppmm.initialProcessData.autofillSavedFieldNames.delete(fieldName);
     });
 
     Services.ppmm.broadcastAsyncMessage("FormAutofill:savedFieldNames",
                                         Services.ppmm.initialProcessData.autofillSavedFieldNames);
     this._updateStatus();
   },
 
-  _onFormSubmit(data, target) {
-    let {address} = data;
-
+  _onAddressSubmit(address, target) {
     if (address.guid) {
       // Avoid updating the fields that users don't modify.
       let originalAddress = this.profileStorage.addresses.get(address.guid);
       for (let field in address.record) {
         if (address.untouchedFields.includes(field) && originalAddress[field]) {
           address.record[field] = originalAddress[field];
         }
       }
@@ -384,9 +382,43 @@ FormAutofillParent.prototype = {
                                              {origin: "autofillDoorhanger"});
         });
       } else {
         // We want to exclude the first time form filling.
         Services.telemetry.scalarAdd("formautofill.addresses.fill_type_manual", 1);
       }
     }
   },
+
+  async _onCreditCardSubmit(creditCard, target) {
+    // We'll show the credit card doorhanger if:
+    //   - User applys autofill and changed
+    //   - User fills form manually
+    if (creditCard.guid &&
+        Object.keys(creditCard.record).every(key => creditCard.untouchedFields.includes(key))) {
+      return;
+    }
+
+    let state = await FormAutofillDoorhanger.show(target, "creditCard");
+    if (state == "cancel") {
+      return;
+    }
+
+    if (state == "disable") {
+      Services.prefs.setBoolPref("extensions.formautofill.creditCards.enabled", false);
+      return;
+    }
+
+    await this.profileStorage.creditCards.normalizeCCNumberFields(creditCard.record);
+    this.profileStorage.creditCards.add(creditCard.record);
+  },
+
+  _onFormSubmit(data, target) {
+    let {address, creditCard} = data;
+
+    if (address) {
+      this._onAddressSubmit(address, target);
+    }
+    if (creditCard) {
+      this._onCreditCardSubmit(creditCard, target);
+    }
+  },
 };