Bug 1128500 - Make CustomizationTabPreloader.jsm use HiddenFrame.jsm. r=ttaubert, a=lmandel
authorAlessio Placitelli <alessio.placitelli@gmail.com>
Wed, 04 Feb 2015 06:34:00 +0100
changeset 250109 cae785163094
parent 250108 3d6eaf96ca69
child 250110 8ac02f8d22e5
push id4503
push userryanvm@gmail.com
push date2015-02-27 21:34 +0000
treeherdermozilla-beta@87d76aead804 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersttaubert, lmandel
bugs1128500
milestone37.0
Bug 1128500 - Make CustomizationTabPreloader.jsm use HiddenFrame.jsm. r=ttaubert, a=lmandel
browser/modules/CustomizationTabPreloader.jsm
--- a/browser/modules/CustomizationTabPreloader.jsm
+++ b/browser/modules/CustomizationTabPreloader.jsm
@@ -7,16 +7,20 @@
 this.EXPORTED_SYMBOLS = ["CustomizationTabPreloader"];
 
 const Cu = Components.utils;
 const Cc = Components.classes;
 const Ci = Components.interfaces;
 
 Cu.import("resource://gre/modules/Services.jsm");
 Cu.import("resource://gre/modules/Promise.jsm");
+Cu.import("resource://gre/modules/XPCOMUtils.jsm");
+
+XPCOMUtils.defineLazyModuleGetter(this, "HiddenFrame",
+  "resource:///modules/HiddenFrame.jsm");
 
 const HTML_NS = "http://www.w3.org/1999/xhtml";
 const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
 const XUL_PAGE = "data:application/vnd.mozilla.xul+xml;charset=utf-8,<window%20id='win'/>";
 const CUSTOMIZATION_URL = "about:customizing";
 
 // The interval between swapping in a preload docShell and kicking off the
 // next preload in the background.
@@ -55,18 +59,16 @@ this.CustomizationTabPreloader = {
 };
 
 Object.freeze(CustomizationTabPreloader);
 
 this.CustomizationTabPreloaderInternal = {
   _browser: null,
 
   uninit: function () {
-    HostFrame.destroy();
-
     if (this._browser) {
       this._browser.destroy();
       this._browser = null;
     }
   },
 
   newTab: function (aTab) {
     let win = aTab.ownerDocument.defaultView;
@@ -85,16 +87,17 @@ this.CustomizationTabPreloaderInternal =
 };
 
 function HiddenBrowser() {
   this._createBrowser();
 }
 
 HiddenBrowser.prototype = {
   _timer: null,
+  _hiddenFrame: null,
 
   get isPreloaded() {
     return this._browser &&
            this._browser.contentDocument &&
            this._browser.contentDocument.readyState === "complete" &&
            this._browser.currentURI.spec === CUSTOMIZATION_URL;
   },
 
@@ -132,21 +135,29 @@ HiddenBrowser.prototype = {
     this._timer = null;
 
     // Start pre-loading the customization page.
     this._createBrowser();
   },
 
   destroy: function () {
     this._removeBrowser();
+    if (this._hiddenFrame) {
+      this._hiddenFrame.destroy();
+      this._hiddenFrame = null;
+    }
     this._timer = clearTimer(this._timer);
   },
 
   _createBrowser: function () {
-    HostFrame.get().then(aFrame => {
+    if (!this._hiddenFrame) {
+      this._hiddenFrame = new HiddenFrame();
+    }
+
+    this._hiddenFrame.get().then(aFrame => {
       let doc = aFrame.document;
       this._browser = doc.createElementNS(XUL_NS, "browser");
       this._browser.setAttribute("type", "content");
       this._browser.setAttribute("src", CUSTOMIZATION_URL);
       this._browser.style.width = "400px";
       this._browser.style.height = "400px";
       doc.getElementById("win").appendChild(this._browser);
     });
@@ -154,64 +165,8 @@ HiddenBrowser.prototype = {
 
   _removeBrowser: function () {
     if (this._browser) {
       this._browser.remove();
       this._browser = null;
     }
   }
 };
-
-let HostFrame = {
-  _frame: null,
-  _deferred: null,
-
-  get hiddenDOMDocument() {
-    return Services.appShell.hiddenDOMWindow.document;
-  },
-
-  get isReady() {
-    return this.hiddenDOMDocument.readyState === "complete";
-  },
-
-  get: function () {
-    if (!this._deferred) {
-      this._deferred = Promise.defer();
-      this._create();
-    }
-
-    return this._deferred.promise;
-  },
-
-  destroy: function () {
-    if (this._frame) {
-      if (!Cu.isDeadWrapper(this._frame)) {
-        this._frame.removeEventListener("load", this, true);
-        this._frame.remove();
-      }
-
-      this._frame = null;
-      this._deferred = null;
-    }
-  },
-
-  handleEvent: function () {
-    let contentWindow = this._frame.contentWindow;
-    if (contentWindow.location.href === XUL_PAGE) {
-      this._frame.removeEventListener("load", this, true);
-      this._deferred.resolve(contentWindow);
-    } else {
-      contentWindow.location = XUL_PAGE;
-    }
-  },
-
-  _create: function () {
-    if (this.isReady) {
-      let doc = this.hiddenDOMDocument;
-      this._frame = doc.createElementNS(HTML_NS, "iframe");
-      this._frame.addEventListener("load", this, true);
-      doc.documentElement.appendChild(this._frame);
-    } else {
-      let flags = Ci.nsIThread.DISPATCH_NORMAL;
-      Services.tm.currentThread.dispatch(() => this._create(), flags);
-    }
-  }
-};