Bug 1348193 - Handle submit action with earlyformsubmit. r?MattN
MozReview-Commit-ID: Dit9d71GGUw
--- a/browser/extensions/formautofill/FormAutofillContent.jsm
+++ b/browser/extensions/formautofill/FormAutofillContent.jsm
@@ -247,40 +247,59 @@ let ProfileAutocomplete = {
};
/**
* Handles content's interactions for the process.
*
* NOTE: Declares it by "var" to make it accessible in unit tests.
*/
var FormAutofillContent = {
+ QueryInterface: XPCOMUtils.generateQI([Ci.nsIFormSubmitObserver]),
/**
* @type {WeakMap} mapping FormLike root HTML elements to FormAutofillHandler objects.
*/
_formsDetails: new WeakMap(),
/**
* @type {Set} Set of the fields with usable values in any saved profile.
*/
savedFieldNames: null,
init() {
FormAutofillUtils.defineLazyLogGetter(this, "FormAutofillContent");
Services.cpmm.addMessageListener("FormAutofill:enabledStatus", this);
Services.cpmm.addMessageListener("FormAutofill:savedFieldNames", this);
+ Services.obs.addObserver(this, "earlyformsubmit", false);
if (Services.cpmm.initialProcessData.autofillEnabled) {
ProfileAutocomplete.ensureRegistered();
}
this.savedFieldNames =
Services.cpmm.initialProcessData.autofillSavedFieldNames || new Set();
},
+ _onFormSubmit(handler) {
+ // TODO: Handle form submit event for profile saving(bug 990219) and metrics(bug 1341569).
+ },
+
+ notify(formElement) {
+ this.log.debug("notified for form early submission");
+
+ let handler = this._formsDetails.get(formElement);
+ if (!handler) {
+ this.log.debug("Form element could not map to an existing handler");
+ } else {
+ this._onFormSubmit(handler);
+ }
+
+ return true;
+ },
+
receiveMessage({name, data}) {
switch (name) {
case "FormAutofill:enabledStatus": {
if (data) {
ProfileAutocomplete.ensureRegistered();
} else {
ProfileAutocomplete.ensureUnregistered();
}
new file mode 100644
--- /dev/null
+++ b/browser/extensions/formautofill/test/unit/test_onFormSubmitted.js
@@ -0,0 +1,30 @@
+"use strict";
+
+Cu.import("resource://formautofill/FormAutofillContent.jsm");
+
+const MOCK_DOC = MockDocument.createTestDocument("http://localhost:8080/test/",
+ `<form id="form1">
+ <input id="street-addr" autocomplete="street-address">
+ <input id="city" autocomplete="address-level2">
+ <input id="country" autocomplete="country">
+ <input id="email" autocomplete="email">
+ <input id="tel" autocomplete="tel">
+ <input id="submit" type="submit">
+ </form>`);
+
+add_task(function* () {
+ do_print("Starting testcase: Make sure content handle earlyformsubmit correctly");
+
+ let form = MOCK_DOC.getElementById("form1");
+ FormAutofillContent.identifyAutofillFields(MOCK_DOC);
+ sinon.spy(FormAutofillContent, "_onFormSubmit");
+
+ do_check_eq(FormAutofillContent.notify(form), true);
+ do_check_eq(FormAutofillContent._onFormSubmit.called, true);
+
+ let fakeForm = MOCK_DOC.createElement("form");
+ FormAutofillContent._onFormSubmit.reset();
+
+ do_check_eq(FormAutofillContent.notify(fakeForm), true);
+ do_check_eq(FormAutofillContent._onFormSubmit.called, false);
+});
--- a/browser/extensions/formautofill/test/unit/xpcshell.ini
+++ b/browser/extensions/formautofill/test/unit/xpcshell.ini
@@ -3,12 +3,13 @@ firefox-appdir = browser
head = head.js
support-files =
[test_autofillFormFields.js]
[test_collectFormFields.js]
[test_enabledStatus.js]
[test_getFormInputDetails.js]
[test_markAsAutofillField.js]
+[test_onFormSubmitted.js]
[test_profileAutocompleteResult.js]
[test_profileStorage.js]
[test_savedFieldNames.js]