Bug 1322622 - Move handler as content script and load at bootstrap, r=MattN
MozReview-Commit-ID: An0t7pdBKtX
--- a/browser/extensions/formautofill/bootstrap.js
+++ b/browser/extensions/formautofill/bootstrap.js
@@ -1,12 +1,31 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
/* exported startup, shutdown, install, uninstall */
-function startup() {}
+const {classes: Cc, interfaces: Ci, results: Cr, utils: Cu} = Components;
+
+Cu.import("resource://gre/modules/Services.jsm");
+Cu.import("resource://gre/modules/XPCOMUtils.jsm");
+
+XPCOMUtils.defineLazyModuleGetter(this, "FormAutofillParent",
+ "resource://formautofill/FormAutofillParent.jsm");
+
+function startup() {
+ // Besides this pref, we'll need dom.forms.autocomplete.experimental enabled
+ // as well to make sure form autocomplete works correctly.
+ if (!Services.prefs.getBoolPref("dom.forms.autocomplete.enabled")) {
+ return;
+ }
+
+ FormAutofillParent.init();
+ Services.mm.loadFrameScript(
+ "resource://formautofill/FormAutofillContent.js", true);
+}
+
function shutdown() {}
function install() {}
function uninstall() {}
rename from browser/extensions/formautofill/content/FormAutofillContent.jsm
rename to browser/extensions/formautofill/content/FormAutofillContent.js
--- a/browser/extensions/formautofill/content/FormAutofillContent.jsm
+++ b/browser/extensions/formautofill/content/FormAutofillContent.js
@@ -1,20 +1,21 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*
- * Implements a service used by DOM content to request Form Autofill.
+ * Implements Form Autofill content process script.
*/
"use strict";
const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
+
/**
* Handles profile autofill for a DOM Form element.
* @param {HTMLFormElement} form Form that need to be auto filled
*/
function FormAutofillHandler(form) {
this.form = form;
this.fieldDetails = [];
}
@@ -125,10 +126,8 @@ FormAutofillHandler.prototype = {
Cu.reportError("Autocomplete tokens mismatched");
continue;
}
fieldDetail.element.setUserInput(field.value);
}
},
};
-
-this.EXPORTED_SYMBOLS = ["FormAutofillHandler"];
--- a/browser/extensions/formautofill/test/unit/head.js
+++ b/browser/extensions/formautofill/test/unit/head.js
@@ -1,13 +1,13 @@
/**
* Provides infrastructure for automated formautofill components tests.
*/
-/* exported importAutofillModule, getTempFile */
+/* exported FormAutofillHandler, getTempFile */
"use strict";
const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/NetUtil.jsm");
@@ -19,22 +19,41 @@ XPCOMUtils.defineLazyModuleGetter(this,
"resource://gre/modules/FileUtils.jsm");
// Register the resource path of formautofill
let resHandler = Services.io.getProtocolHandler("resource")
.QueryInterface(Ci.nsISubstitutingProtocolHandler);
let dataURI = NetUtil.newURI(do_get_file(".", true));
resHandler.setSubstitution("formautofill", dataURI);
+do_register_cleanup(() => {
+ resHandler.setSubstitution("formautofill", null);
+});
+
// While the previous test file should have deleted all the temporary files it
// used, on Windows these might still be pending deletion on the physical file
// system. Thus, start from a new base number every time, to make a collision
// with a file that is still pending deletion highly unlikely.
let gFileCounter = Math.floor(Math.random() * 1000000);
+let FormAutofillHandler;
+
+// Load content script by using loadSubScriptWithOptions
+function loadFormAutofillContent() {
+ let facGlobal = {};
+ let loader = Cc["@mozilla.org/moz/jssubscript-loader;1"]
+ .getService(Ci.mozIJSSubScriptLoader);
+ loader.loadSubScriptWithOptions("resource://formautofill/FormAutofillContent.js", {
+ target: facGlobal,
+ });
+
+ FormAutofillHandler = facGlobal.FormAutofillHandler;
+}
+
+
/**
* Returns a reference to a temporary file, that is guaranteed not to exist, and
* to have never been created before.
*
* @param {string} leafName
* Suggested leaf name for the file to be created.
*
* @returns {nsIFile} pointing to a non-existent file in a temporary directory.
@@ -59,15 +78,18 @@ function getTempFile(leafName) {
file.remove(false);
}
});
return file;
}
add_task(function* test_common_initialize() {
+ Services.prefs.setBoolPref("dom.forms.autocomplete.enabled", true);
Services.prefs.setBoolPref("dom.forms.autocomplete.experimental", true);
+ loadFormAutofillContent();
// Clean up after every test.
do_register_cleanup(() => {
+ Services.prefs.setBoolPref("dom.forms.autocomplete.enabled", false);
Services.prefs.setBoolPref("dom.forms.autocomplete.experimental", false);
});
});
deleted file mode 100644
--- a/browser/extensions/formautofill/test/unit/tail.js
+++ /dev/null
@@ -1,10 +0,0 @@
-/**
- * Cleans up the testing environment.
- */
-
-/* global resHandler */
-
-"use strict";
-
-// Unregister the resource path of formautofill.
-resHandler.setSubstitution("formautofill", null);
--- a/browser/extensions/formautofill/test/unit/test_autofillFormFields.js
+++ b/browser/extensions/formautofill/test/unit/test_autofillFormFields.js
@@ -1,16 +1,14 @@
/*
* Test for form auto fill content helper fill all inputs function.
*/
"use strict";
-let {FormAutofillHandler} = Cu.import("resource://formautofill/FormAutofillContent.jsm");
-
const TESTCASES = [
{
description: "Form without autocomplete property",
document: `<form><input id="given-name"><input id="family-name">
<input id="street-addr"><input id="city"><input id="country">
<input id='email'><input id="tel"></form>`,
fieldDetails: [],
profileData: [],
--- a/browser/extensions/formautofill/test/unit/test_collectFormFields.js
+++ b/browser/extensions/formautofill/test/unit/test_collectFormFields.js
@@ -1,15 +1,14 @@
/*
* Test for form auto fill content helper collectFormFields functions.
*/
"use strict";
-let {FormAutofillHandler} = Cu.import("resource://formautofill/FormAutofillContent.jsm");
const TESTCASES = [
{
description: "Form without autocomplete property",
document: `<form><input id="given-name"><input id="family-name">
<input id="street-addr"><input id="city"><input id="country">
<input id='email'><input id="tel"></form>`,
returnedFormat: [],
--- a/browser/extensions/formautofill/test/unit/xpcshell.ini
+++ b/browser/extensions/formautofill/test/unit/xpcshell.ini
@@ -1,12 +1,12 @@
[DEFAULT]
head = head.js
-tail = tail.js
+tail =
support-files =
- ../../content/FormAutofillContent.jsm
+ ../../content/FormAutofillContent.js
../../content/FormAutofillParent.jsm
../../content/ProfileStorage.jsm
[test_autofillFormFields.js]
[test_collectFormFields.js]
[test_populateFieldValues.js]
[test_profileStorage.js]
--- a/modules/libpref/init/all.js
+++ b/modules/libpref/init/all.js
@@ -1184,16 +1184,19 @@ pref("dom.forms.color", true);
pref("dom.forms.datetime", false);
// Enable time picker UI. By default, disabled.
pref("dom.forms.datetime.timepicker", false);
// Support for new @autocomplete values
pref("dom.forms.autocomplete.experimental", false);
+// Enable from autocomplete system addon. Disabled before feature completed.
+pref("dom.forms.autocomplete.enabled", false);
+
// Enables requestAutocomplete DOM API on forms.
pref("dom.forms.requestAutocomplete", false);
// Enable Directory API. By default, disabled.
pref("dom.input.dirpicker", false);
// Enables system messages and activities
pref("dom.sysmsg.enabled", false);