browser/modules/CustomizationTabPreloader.jsm

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 "use strict";
michael@0 6
michael@0 7 this.EXPORTED_SYMBOLS = ["CustomizationTabPreloader"];
michael@0 8
michael@0 9 const Cu = Components.utils;
michael@0 10 const Cc = Components.classes;
michael@0 11 const Ci = Components.interfaces;
michael@0 12
michael@0 13 Cu.import("resource://gre/modules/Services.jsm");
michael@0 14 Cu.import("resource://gre/modules/Promise.jsm");
michael@0 15
michael@0 16 const HTML_NS = "http://www.w3.org/1999/xhtml";
michael@0 17 const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
michael@0 18 const XUL_PAGE = "data:application/vnd.mozilla.xul+xml;charset=utf-8,<window%20id='win'/>";
michael@0 19 const CUSTOMIZATION_URL = "about:customizing";
michael@0 20
michael@0 21 // The interval between swapping in a preload docShell and kicking off the
michael@0 22 // next preload in the background.
michael@0 23 const PRELOADER_INTERVAL_MS = 600;
michael@0 24
michael@0 25 function createTimer(obj, delay) {
michael@0 26 let timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
michael@0 27 timer.init(obj, delay, Ci.nsITimer.TYPE_ONE_SHOT);
michael@0 28 return timer;
michael@0 29 }
michael@0 30
michael@0 31 function clearTimer(timer) {
michael@0 32 if (timer) {
michael@0 33 timer.cancel();
michael@0 34 }
michael@0 35 return null;
michael@0 36 }
michael@0 37
michael@0 38 this.CustomizationTabPreloader = {
michael@0 39 uninit: function () {
michael@0 40 CustomizationTabPreloaderInternal.uninit();
michael@0 41 },
michael@0 42
michael@0 43 newTab: function (aTab) {
michael@0 44 return CustomizationTabPreloaderInternal.newTab(aTab);
michael@0 45 },
michael@0 46
michael@0 47 /**
michael@0 48 * ensurePreloading starts the preloading of the about:customizing
michael@0 49 * content page. This function is idempotent (until a call to uninit),
michael@0 50 * so multiple calls to it are fine.
michael@0 51 */
michael@0 52 ensurePreloading: function() {
michael@0 53 CustomizationTabPreloaderInternal.ensurePreloading();
michael@0 54 },
michael@0 55 };
michael@0 56
michael@0 57 Object.freeze(CustomizationTabPreloader);
michael@0 58
michael@0 59 this.CustomizationTabPreloaderInternal = {
michael@0 60 _browser: null,
michael@0 61
michael@0 62 uninit: function () {
michael@0 63 HostFrame.destroy();
michael@0 64
michael@0 65 if (this._browser) {
michael@0 66 this._browser.destroy();
michael@0 67 this._browser = null;
michael@0 68 }
michael@0 69 },
michael@0 70
michael@0 71 newTab: function (aTab) {
michael@0 72 let win = aTab.ownerDocument.defaultView;
michael@0 73 if (win.gBrowser && this._browser) {
michael@0 74 return this._browser.swapWithNewTab(aTab);
michael@0 75 }
michael@0 76
michael@0 77 return false;
michael@0 78 },
michael@0 79
michael@0 80 ensurePreloading: function () {
michael@0 81 if (!this._browser) {
michael@0 82 this._browser = new HiddenBrowser();
michael@0 83 }
michael@0 84 }
michael@0 85 };
michael@0 86
michael@0 87 function HiddenBrowser() {
michael@0 88 this._createBrowser();
michael@0 89 }
michael@0 90
michael@0 91 HiddenBrowser.prototype = {
michael@0 92 _timer: null,
michael@0 93
michael@0 94 get isPreloaded() {
michael@0 95 return this._browser &&
michael@0 96 this._browser.contentDocument &&
michael@0 97 this._browser.contentDocument.readyState === "complete" &&
michael@0 98 this._browser.currentURI.spec === CUSTOMIZATION_URL;
michael@0 99 },
michael@0 100
michael@0 101 swapWithNewTab: function (aTab) {
michael@0 102 if (!this.isPreloaded || this._timer) {
michael@0 103 return false;
michael@0 104 }
michael@0 105
michael@0 106 let win = aTab.ownerDocument.defaultView;
michael@0 107 let tabbrowser = win.gBrowser;
michael@0 108
michael@0 109 if (!tabbrowser) {
michael@0 110 return false;
michael@0 111 }
michael@0 112
michael@0 113 // Swap docShells.
michael@0 114 tabbrowser.swapNewTabWithBrowser(aTab, this._browser);
michael@0 115
michael@0 116 // Load all default frame scripts attached to the target window.
michael@0 117 let mm = aTab.linkedBrowser.messageManager;
michael@0 118 let scripts = win.messageManager.getDelayedFrameScripts();
michael@0 119 Array.forEach(scripts, ([script, runGlobal]) => mm.loadFrameScript(script, true, runGlobal));
michael@0 120
michael@0 121 // Remove the browser, it will be recreated by a timer.
michael@0 122 this._removeBrowser();
michael@0 123
michael@0 124 // Start a timer that will kick off preloading the next page.
michael@0 125 this._timer = createTimer(this, PRELOADER_INTERVAL_MS);
michael@0 126
michael@0 127 // Signal that we swapped docShells.
michael@0 128 return true;
michael@0 129 },
michael@0 130
michael@0 131 observe: function () {
michael@0 132 this._timer = null;
michael@0 133
michael@0 134 // Start pre-loading the customization page.
michael@0 135 this._createBrowser();
michael@0 136 },
michael@0 137
michael@0 138 destroy: function () {
michael@0 139 this._removeBrowser();
michael@0 140 this._timer = clearTimer(this._timer);
michael@0 141 },
michael@0 142
michael@0 143 _createBrowser: function () {
michael@0 144 HostFrame.get().then(aFrame => {
michael@0 145 let doc = aFrame.document;
michael@0 146 this._browser = doc.createElementNS(XUL_NS, "browser");
michael@0 147 this._browser.setAttribute("type", "content");
michael@0 148 this._browser.setAttribute("src", CUSTOMIZATION_URL);
michael@0 149 this._browser.style.width = "400px";
michael@0 150 this._browser.style.height = "400px";
michael@0 151 doc.getElementById("win").appendChild(this._browser);
michael@0 152 });
michael@0 153 },
michael@0 154
michael@0 155 _removeBrowser: function () {
michael@0 156 if (this._browser) {
michael@0 157 this._browser.remove();
michael@0 158 this._browser = null;
michael@0 159 }
michael@0 160 }
michael@0 161 };
michael@0 162
michael@0 163 let HostFrame = {
michael@0 164 _frame: null,
michael@0 165 _deferred: null,
michael@0 166
michael@0 167 get hiddenDOMDocument() {
michael@0 168 return Services.appShell.hiddenDOMWindow.document;
michael@0 169 },
michael@0 170
michael@0 171 get isReady() {
michael@0 172 return this.hiddenDOMDocument.readyState === "complete";
michael@0 173 },
michael@0 174
michael@0 175 get: function () {
michael@0 176 if (!this._deferred) {
michael@0 177 this._deferred = Promise.defer();
michael@0 178 this._create();
michael@0 179 }
michael@0 180
michael@0 181 return this._deferred.promise;
michael@0 182 },
michael@0 183
michael@0 184 destroy: function () {
michael@0 185 if (this._frame) {
michael@0 186 if (!Cu.isDeadWrapper(this._frame)) {
michael@0 187 this._frame.removeEventListener("load", this, true);
michael@0 188 this._frame.remove();
michael@0 189 }
michael@0 190
michael@0 191 this._frame = null;
michael@0 192 this._deferred = null;
michael@0 193 }
michael@0 194 },
michael@0 195
michael@0 196 handleEvent: function () {
michael@0 197 let contentWindow = this._frame.contentWindow;
michael@0 198 if (contentWindow.location.href === XUL_PAGE) {
michael@0 199 this._frame.removeEventListener("load", this, true);
michael@0 200 this._deferred.resolve(contentWindow);
michael@0 201 } else {
michael@0 202 contentWindow.location = XUL_PAGE;
michael@0 203 }
michael@0 204 },
michael@0 205
michael@0 206 _create: function () {
michael@0 207 if (this.isReady) {
michael@0 208 let doc = this.hiddenDOMDocument;
michael@0 209 this._frame = doc.createElementNS(HTML_NS, "iframe");
michael@0 210 this._frame.addEventListener("load", this, true);
michael@0 211 doc.documentElement.appendChild(this._frame);
michael@0 212 } else {
michael@0 213 let flags = Ci.nsIThread.DISPATCH_NORMAL;
michael@0 214 Services.tm.currentThread.dispatch(() => this._create(), flags);
michael@0 215 }
michael@0 216 }
michael@0 217 };

mercurial