2012-08-13 20:30:18 +04:00
|
|
|
/* 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";
|
|
|
|
|
2012-10-31 20:13:28 +04:00
|
|
|
this.EXPORTED_SYMBOLS = ["BrowserNewTabPreloader"];
|
2012-08-13 20:30:18 +04:00
|
|
|
|
|
|
|
const Cu = Components.utils;
|
2012-09-19 18:20:44 +04:00
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
2012-08-13 20:30:18 +04:00
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
2012-09-19 18:20:44 +04:00
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
2012-08-13 20:30:18 +04:00
|
|
|
|
2012-09-19 18:20:44 +04:00
|
|
|
const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
|
|
|
|
const PREF_BRANCH = "browser.newtab.";
|
2012-09-21 13:28:06 +04:00
|
|
|
const TOPIC_DELAYED_STARTUP = "browser-delayed-startup-finished";
|
|
|
|
const PRELOADER_INIT_DELAY_MS = 5000;
|
2012-08-13 20:30:18 +04:00
|
|
|
|
2012-10-31 20:13:28 +04:00
|
|
|
this.BrowserNewTabPreloader = {
|
2012-09-19 18:20:44 +04:00
|
|
|
init: function Preloader_init() {
|
2012-09-21 13:28:06 +04:00
|
|
|
Initializer.start();
|
2012-09-19 18:20:44 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
uninit: function Preloader_uninit() {
|
2012-09-21 13:28:06 +04:00
|
|
|
Initializer.stop();
|
2012-09-19 18:20:44 +04:00
|
|
|
HostFrame.destroy();
|
|
|
|
Preferences.uninit();
|
|
|
|
HiddenBrowser.destroy();
|
|
|
|
},
|
2012-08-13 20:30:18 +04:00
|
|
|
|
2012-09-19 18:20:44 +04:00
|
|
|
newTab: function Preloader_newTab(aTab) {
|
|
|
|
HiddenBrowser.swapWithNewTab(aTab);
|
|
|
|
}
|
|
|
|
};
|
2012-08-13 20:30:18 +04:00
|
|
|
|
2012-09-19 18:20:44 +04:00
|
|
|
Object.freeze(BrowserNewTabPreloader);
|
|
|
|
|
2012-09-21 13:28:06 +04:00
|
|
|
let Initializer = {
|
|
|
|
_timer: null,
|
|
|
|
_observing: false,
|
|
|
|
|
|
|
|
start: function Initializer_start() {
|
|
|
|
Services.obs.addObserver(this, TOPIC_DELAYED_STARTUP, false);
|
|
|
|
this._observing = true;
|
|
|
|
},
|
|
|
|
|
|
|
|
stop: function Initializer_stop() {
|
|
|
|
if (this._timer) {
|
|
|
|
this._timer.cancel();
|
|
|
|
this._timer = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._observing) {
|
|
|
|
Services.obs.removeObserver(this, TOPIC_DELAYED_STARTUP);
|
|
|
|
this._observing = false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
observe: function Initializer_observe(aSubject, aTopic, aData) {
|
|
|
|
if (aTopic == TOPIC_DELAYED_STARTUP) {
|
|
|
|
Services.obs.removeObserver(this, TOPIC_DELAYED_STARTUP);
|
|
|
|
this._observing = false;
|
|
|
|
this._startTimer();
|
|
|
|
} else if (aTopic == "timer-callback") {
|
|
|
|
this._timer = null;
|
|
|
|
this._startPreloader();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_startTimer: function Initializer_startTimer() {
|
|
|
|
this._timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
|
|
|
|
this._timer.init(this, PRELOADER_INIT_DELAY_MS, Ci.nsITimer.TYPE_ONE_SHOT);
|
|
|
|
},
|
|
|
|
|
|
|
|
_startPreloader: function Initializer_startPreloader() {
|
|
|
|
Preferences.init();
|
|
|
|
if (Preferences.enabled) {
|
|
|
|
HiddenBrowser.create();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-09-19 18:20:44 +04:00
|
|
|
let Preferences = {
|
|
|
|
_enabled: null,
|
|
|
|
_branch: null,
|
|
|
|
_url: null,
|
|
|
|
|
|
|
|
get enabled() {
|
|
|
|
if (this._enabled === null) {
|
|
|
|
this._enabled = this._branch.getBoolPref("preload") &&
|
|
|
|
!this._branch.prefHasUserValue("url") &&
|
|
|
|
this.url && this.url != "about:blank";
|
2012-08-13 20:30:18 +04:00
|
|
|
}
|
2012-09-19 18:20:44 +04:00
|
|
|
|
|
|
|
return this._enabled;
|
2012-08-13 20:30:18 +04:00
|
|
|
},
|
|
|
|
|
2012-09-19 18:20:44 +04:00
|
|
|
get url() {
|
|
|
|
if (this._url === null) {
|
|
|
|
this._url = this._branch.getCharPref("url");
|
2012-08-13 20:30:18 +04:00
|
|
|
}
|
|
|
|
|
2012-09-19 18:20:44 +04:00
|
|
|
return this._url;
|
|
|
|
},
|
2012-08-13 20:30:18 +04:00
|
|
|
|
2012-09-19 18:20:44 +04:00
|
|
|
init: function Preferences_init() {
|
|
|
|
this._branch = Services.prefs.getBranch(PREF_BRANCH);
|
|
|
|
this._branch.addObserver("", this, false);
|
2012-08-13 20:30:18 +04:00
|
|
|
},
|
|
|
|
|
2012-09-19 18:20:44 +04:00
|
|
|
uninit: function Preferences_uninit() {
|
2012-09-19 19:33:55 +04:00
|
|
|
if (this._branch) {
|
|
|
|
this._branch.removeObserver("", this);
|
|
|
|
this._branch = null;
|
|
|
|
}
|
2012-09-19 18:20:44 +04:00
|
|
|
},
|
2012-08-13 20:30:18 +04:00
|
|
|
|
2012-09-19 18:20:44 +04:00
|
|
|
observe: function Preferences_observe(aSubject, aTopic, aData) {
|
|
|
|
let {url, enabled} = this;
|
|
|
|
this._url = this._enabled = null;
|
|
|
|
|
|
|
|
if (enabled && !this.enabled) {
|
|
|
|
HiddenBrowser.destroy();
|
|
|
|
} else if (!enabled && this.enabled) {
|
|
|
|
HiddenBrowser.create();
|
|
|
|
} else if (this._browser && url != this.url) {
|
|
|
|
HiddenBrowser.update(this.url);
|
2012-08-13 20:30:18 +04:00
|
|
|
}
|
|
|
|
},
|
2012-09-19 18:20:44 +04:00
|
|
|
};
|
2012-08-13 20:30:18 +04:00
|
|
|
|
2012-09-19 18:20:44 +04:00
|
|
|
let HiddenBrowser = {
|
|
|
|
get isPreloaded() {
|
|
|
|
return this._browser &&
|
|
|
|
this._browser.contentDocument &&
|
|
|
|
this._browser.contentDocument.readyState == "complete" &&
|
|
|
|
this._browser.currentURI.spec == Preferences.url;
|
|
|
|
},
|
2012-08-13 20:30:18 +04:00
|
|
|
|
2012-09-19 18:20:44 +04:00
|
|
|
swapWithNewTab: function HiddenBrowser_swapWithNewTab(aTab) {
|
|
|
|
if (this.isPreloaded) {
|
|
|
|
let tabbrowser = aTab.ownerDocument.defaultView.gBrowser;
|
|
|
|
if (tabbrowser) {
|
|
|
|
tabbrowser.swapNewTabWithBrowser(aTab, this._browser);
|
2012-08-13 20:30:18 +04:00
|
|
|
}
|
|
|
|
}
|
2012-09-19 18:20:44 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
create: function HiddenBrowser_create() {
|
|
|
|
HostFrame.getFrame(function (aFrame) {
|
|
|
|
let doc = aFrame.document;
|
|
|
|
this._browser = doc.createElementNS(XUL_NS, "browser");
|
|
|
|
this._browser.setAttribute("type", "content");
|
|
|
|
this._browser.setAttribute("src", Preferences.url);
|
|
|
|
doc.documentElement.appendChild(this._browser);
|
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
|
|
|
|
update: function HiddenBrowser_update(aURL) {
|
|
|
|
this._browser.setAttribute("src", aURL);
|
|
|
|
},
|
2012-08-13 20:30:18 +04:00
|
|
|
|
2012-09-19 18:20:44 +04:00
|
|
|
destroy: function HiddenBrowser_destroy() {
|
|
|
|
if (this._browser) {
|
2012-08-13 20:30:18 +04:00
|
|
|
this._browser.parentNode.removeChild(this._browser);
|
|
|
|
this._browser = null;
|
|
|
|
}
|
2012-09-19 18:20:44 +04:00
|
|
|
}
|
|
|
|
};
|
2012-08-13 20:30:18 +04:00
|
|
|
|
2012-09-19 18:20:44 +04:00
|
|
|
let HostFrame = {
|
|
|
|
_listener: null,
|
|
|
|
_privilegedFrame: null,
|
2012-08-13 20:30:18 +04:00
|
|
|
|
2012-09-19 18:20:44 +04:00
|
|
|
_privilegedContentTypes: {
|
|
|
|
"application/vnd.mozilla.xul+xml": true,
|
|
|
|
"application/xhtml+xml": true
|
2012-08-13 20:30:18 +04:00
|
|
|
},
|
|
|
|
|
2012-09-19 18:20:44 +04:00
|
|
|
get _frame() {
|
|
|
|
delete this._frame;
|
|
|
|
return this._frame = Services.appShell.hiddenDOMWindow;
|
|
|
|
},
|
2012-08-13 20:30:18 +04:00
|
|
|
|
2012-09-19 18:20:44 +04:00
|
|
|
get _isReady() {
|
|
|
|
let readyState = this._frame.document.readyState;
|
|
|
|
return (readyState == "complete" || readyState == "interactive");
|
|
|
|
},
|
2012-08-13 20:30:18 +04:00
|
|
|
|
2012-09-19 18:20:44 +04:00
|
|
|
get _isPrivileged() {
|
|
|
|
return (this._frame.location.protocol == "chrome:" &&
|
|
|
|
this._frame.document.contentType in this._privilegedContentTypes);
|
2012-08-13 20:30:18 +04:00
|
|
|
},
|
|
|
|
|
2012-09-19 18:20:44 +04:00
|
|
|
getFrame: function HostFrame_getFrame(aCallback) {
|
|
|
|
if (this._isReady && !this._isPrivileged) {
|
|
|
|
this._createPrivilegedFrame();
|
2012-08-13 20:30:18 +04:00
|
|
|
}
|
|
|
|
|
2012-09-19 18:20:44 +04:00
|
|
|
if (this._isReady) {
|
|
|
|
aCallback(this._frame);
|
|
|
|
} else {
|
|
|
|
this._waitUntilLoaded(aCallback);
|
2012-08-13 20:30:18 +04:00
|
|
|
}
|
2012-09-19 18:20:44 +04:00
|
|
|
},
|
2012-08-13 20:30:18 +04:00
|
|
|
|
2012-09-19 18:20:44 +04:00
|
|
|
destroy: function HostFrame_destroy() {
|
|
|
|
delete this._frame;
|
|
|
|
this._listener = null;
|
2012-08-13 20:30:18 +04:00
|
|
|
},
|
|
|
|
|
2012-09-19 18:20:44 +04:00
|
|
|
_createPrivilegedFrame: function HostFrame_createPrivilegedFrame() {
|
|
|
|
let doc = this._frame.document;
|
|
|
|
let iframe = doc.createElement("iframe");
|
|
|
|
iframe.setAttribute("src", "chrome://browser/content/newtab/preload.xhtml");
|
|
|
|
doc.documentElement.appendChild(iframe);
|
|
|
|
this._frame = iframe.contentWindow;
|
2012-08-13 20:30:18 +04:00
|
|
|
},
|
|
|
|
|
2012-09-19 18:20:44 +04:00
|
|
|
_waitUntilLoaded: function HostFrame_waitUntilLoaded(aCallback) {
|
|
|
|
this._listener = new HiddenWindowLoadListener(this._frame, function () {
|
|
|
|
HostFrame.getFrame(aCallback);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
function HiddenWindowLoadListener(aWindow, aCallback) {
|
|
|
|
this._window = aWindow;
|
|
|
|
this._callback = aCallback;
|
|
|
|
|
|
|
|
let docShell = Services.appShell.hiddenWindow.docShell;
|
|
|
|
this._webProgress = docShell.QueryInterface(Ci.nsIWebProgress);
|
|
|
|
this._webProgress.addProgressListener(this, Ci.nsIWebProgress.NOTIFY_STATE_ALL);
|
|
|
|
}
|
|
|
|
|
|
|
|
HiddenWindowLoadListener.prototype = {
|
|
|
|
_window: null,
|
|
|
|
_callback: null,
|
|
|
|
_webProgress: null,
|
|
|
|
|
|
|
|
_destroy: function HiddenWindowLoadListener_destroy() {
|
|
|
|
this._webProgress.removeProgressListener(this);
|
|
|
|
this._window = null;
|
|
|
|
this._callback = null;
|
|
|
|
this._webProgress = null;
|
2012-08-13 20:30:18 +04:00
|
|
|
},
|
|
|
|
|
2012-09-19 18:20:44 +04:00
|
|
|
onStateChange:
|
|
|
|
function HiddenWindowLoadListener_onStateChange(aWebProgress, aRequest,
|
|
|
|
aStateFlags, aStatus) {
|
|
|
|
if (aStateFlags & Ci.nsIWebProgressListener.STATE_STOP &&
|
|
|
|
aStateFlags & Ci.nsIWebProgressListener.STATE_IS_NETWORK &&
|
|
|
|
aStateFlags & Ci.nsIWebProgressListener.STATE_IS_WINDOW &&
|
|
|
|
this._window == aWebProgress.DOMWindow) {
|
|
|
|
this._callback();
|
|
|
|
this._destroy();
|
2012-08-13 20:30:18 +04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-09-19 18:20:44 +04:00
|
|
|
onStatusChange: function () {},
|
|
|
|
onLocationChange: function () {},
|
|
|
|
onProgressChange: function () {},
|
|
|
|
onSecurityChange: function () {},
|
2012-08-13 20:30:18 +04:00
|
|
|
|
2012-09-19 18:20:44 +04:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIWebProgressListener,
|
|
|
|
Ci.nsISupportsWeakReference])
|
2012-08-13 20:30:18 +04:00
|
|
|
};
|