Bug 1421551 - Make FormAutofillParent singleton and expose the initialized status. r=MattN
MozReview-Commit-ID: IMd25HcNTMa
--- a/browser/extensions/formautofill/FormAutofillParent.jsm
+++ b/browser/extensions/formautofill/FormAutofillParent.jsm
@@ -22,17 +22,19 @@
* {
* // ...
* }
* ]
*/
"use strict";
-this.EXPORTED_SYMBOLS = ["FormAutofillParent"];
+// We expose a singleton from this module. Some tests may import the
+// constructor via a backstage pass.
+this.EXPORTED_SYMBOLS = ["formAutofillParent"];
const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://formautofill/FormAutofillUtils.jsm");
@@ -74,19 +76,39 @@ FormAutofillParent.prototype = {
QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports, Ci.nsIObserver]),
/**
* Cache of the Form Autofill status (considering preferences and storage).
*/
_active: null,
/**
+ * The status of Form Autofill's initialization.
+ */
+ _initialized: false,
+
+ /**
+ * Exposes the status of Form Autofill's initialization. It can be used to
+ * determine whether Form Autofill is available for current users.
+ *
+ * @returns {boolean} Whether FormAutofillParent is initialized.
+ */
+ get initialized() {
+ return this._initialized;
+ },
+
+ /**
* Initializes ProfileStorage and registers the message handler.
*/
async init() {
+ if (this._initialized) {
+ return;
+ }
+ this._initialized = true;
+
Services.obs.addObserver(this, "sync-pane-loaded");
Services.ppmm.addMessageListener("FormAutofill:InitStorage", this);
Services.ppmm.addMessageListener("FormAutofill:GetRecords", this);
Services.ppmm.addMessageListener("FormAutofill:SaveAddress", this);
Services.ppmm.addMessageListener("FormAutofill:RemoveAddresses", this);
Services.ppmm.addMessageListener("FormAutofill:OpenPreferences", this);
Services.mm.addMessageListener("FormAutofill:OnFormSubmit", this);
@@ -547,8 +569,10 @@ FormAutofillParent.prototype = {
_recordFormFillingTime(formType, fillingType, startedFillingMS) {
if (!startedFillingMS) {
return;
}
let histogram = Services.telemetry.getKeyedHistogramById("FORM_FILLING_REQUIRED_TIME_MS");
histogram.add(`${formType}-${fillingType}`, Date.now() - startedFillingMS);
},
};
+
+this.formAutofillParent = new FormAutofillParent();
--- a/browser/extensions/formautofill/bootstrap.js
+++ b/browser/extensions/formautofill/bootstrap.js
@@ -11,17 +11,17 @@ const STYLESHEET_URI = "chrome://formaut
const CACHED_STYLESHEETS = new WeakMap();
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "AddonManager", "resource://gre/modules/AddonManager.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "AddonManagerPrivate",
"resource://gre/modules/AddonManager.jsm");
-XPCOMUtils.defineLazyModuleGetter(this, "FormAutofillParent",
+XPCOMUtils.defineLazyModuleGetter(this, "formAutofillParent",
"resource://formautofill/FormAutofillParent.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "FormAutofillUtils",
"resource://formautofill/FormAutofillUtils.jsm");
function insertStyleSheet(domWindow, url) {
let doc = domWindow.document;
let styleSheetAttr = `href="${url}" type="text/css"`;
let styleSheet = doc.createProcessingInstruction("xml-stylesheet", styleSheetAttr);
@@ -105,18 +105,17 @@ function startup(data) {
Services.prefs.setBoolPref("services.sync.engine.creditcards.available", true);
} else {
Services.prefs.clearUserPref("services.sync.engine.creditcards.available");
}
// Listen for the autocomplete popup message to lazily append our stylesheet related to the popup.
Services.mm.addMessageListener("FormAutoComplete:MaybeOpenPopup", onMaybeOpenPopup);
- let parent = new FormAutofillParent();
- parent.init().catch(Cu.reportError);
+ formAutofillParent.init().catch(Cu.reportError);
Services.ppmm.loadProcessScript("data:,new " + function() {
Components.utils.import("resource://formautofill/FormAutofillContent.jsm");
}, true);
Services.mm.loadFrameScript("chrome://formautofill/content/FormAutofillFrameScript.js", true);
}
function shutdown() {
Services.mm.removeMessageListener("FormAutoComplete:MaybeOpenPopup", onMaybeOpenPopup);
--- a/browser/extensions/formautofill/test/unit/test_activeStatus.js
+++ b/browser/extensions/formautofill/test/unit/test_activeStatus.js
@@ -1,15 +1,15 @@
/*
* Test for status handling in Form Autofill Parent.
*/
"use strict";
-Cu.import("resource://formautofill/FormAutofillParent.jsm");
+let {FormAutofillParent} = Cu.import("resource://formautofill/FormAutofillParent.jsm", {});
Cu.import("resource://formautofill/ProfileStorage.jsm");
add_task(async function test_activeStatus_init() {
let formAutofillParent = new FormAutofillParent();
sinon.spy(formAutofillParent, "_updateStatus");
// Default status is null before initialization
do_check_eq(formAutofillParent._active, null);
--- a/browser/extensions/formautofill/test/unit/test_getRecords.js
+++ b/browser/extensions/formautofill/test/unit/test_getRecords.js
@@ -1,15 +1,15 @@
/*
* Test for make sure getRecords can retrieve right collection from storage.
*/
"use strict";
-Cu.import("resource://formautofill/FormAutofillParent.jsm");
+let {FormAutofillParent} = Cu.import("resource://formautofill/FormAutofillParent.jsm", {});
Cu.import("resource://formautofill/MasterPassword.jsm");
Cu.import("resource://formautofill/ProfileStorage.jsm");
const TEST_ADDRESS_1 = {
"given-name": "Timothy",
"additional-name": "John",
"family-name": "Berners-Lee",
organization: "World Wide Web Consortium",
--- a/browser/extensions/formautofill/test/unit/test_savedFieldNames.js
+++ b/browser/extensions/formautofill/test/unit/test_savedFieldNames.js
@@ -1,15 +1,15 @@
/*
* Test for keeping the valid fields information in initialProcessData.
*/
"use strict";
-Cu.import("resource://formautofill/FormAutofillParent.jsm");
+let {FormAutofillParent} = Cu.import("resource://formautofill/FormAutofillParent.jsm", {});
Cu.import("resource://formautofill/ProfileStorage.jsm");
add_task(async function test_profileSavedFieldNames_init() {
let formAutofillParent = new FormAutofillParent();
sinon.stub(formAutofillParent, "_updateSavedFieldNames");
await formAutofillParent.init();
await formAutofillParent.profileStorage.initialize();