2017-07-28 00:41:00 +03: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/. */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This module tracks each browser window and informs network module
|
|
|
|
* the current selected tab's content outer window ID.
|
|
|
|
*/
|
|
|
|
|
2018-04-11 13:05:56 +03:00
|
|
|
var EXPORTED_SYMBOLS = ["BrowserWindowTracker"];
|
2017-07-28 00:41:00 +03:00
|
|
|
|
2019-01-17 21:18:31 +03:00
|
|
|
const {XPCOMUtils} = ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
|
2017-07-28 00:41:00 +03:00
|
|
|
|
|
|
|
// Lazy getters
|
2018-04-11 13:05:56 +03:00
|
|
|
XPCOMUtils.defineLazyModuleGetters(this, {
|
2018-08-31 08:59:17 +03:00
|
|
|
PrivateBrowsingUtils: "resource://gre/modules/PrivateBrowsingUtils.jsm",
|
2018-04-11 13:05:56 +03:00
|
|
|
});
|
2017-07-28 00:41:00 +03:00
|
|
|
|
|
|
|
// Constants
|
|
|
|
const TAB_EVENTS = ["TabBrowserInserted", "TabSelect"];
|
2018-04-11 13:06:03 +03:00
|
|
|
const WINDOW_EVENTS = ["activate", "sizemodechange", "unload"];
|
2017-07-28 00:41:00 +03:00
|
|
|
const DEBUG = false;
|
|
|
|
|
|
|
|
// Variables
|
|
|
|
var _lastTopLevelWindowID = 0;
|
2018-04-11 13:06:03 +03:00
|
|
|
var _trackedWindows = [];
|
2017-07-28 00:41:00 +03:00
|
|
|
|
|
|
|
// Global methods
|
|
|
|
function debug(s) {
|
|
|
|
if (DEBUG) {
|
|
|
|
dump("-*- UpdateTopLevelContentWindowIDHelper: " + s + "\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-11 13:05:59 +03:00
|
|
|
function _updateCurrentContentOuterWindowID(browser) {
|
|
|
|
if (!browser.outerWindowID ||
|
2018-09-26 14:57:07 +03:00
|
|
|
browser.outerWindowID === _lastTopLevelWindowID ||
|
|
|
|
browser.ownerGlobal != _trackedWindows[0]) {
|
2017-07-28 00:41:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-06 12:48:46 +03:00
|
|
|
debug("Current window uri=" + (browser.currentURI && browser.currentURI.spec) +
|
2018-04-11 13:05:59 +03:00
|
|
|
" id=" + browser.outerWindowID);
|
2017-07-28 00:41:00 +03:00
|
|
|
|
2018-04-11 13:05:59 +03:00
|
|
|
_lastTopLevelWindowID = browser.outerWindowID;
|
2018-02-28 20:51:33 +03:00
|
|
|
let windowIDWrapper = Cc["@mozilla.org/supports-PRUint64;1"]
|
2017-07-28 00:41:00 +03:00
|
|
|
.createInstance(Ci.nsISupportsPRUint64);
|
|
|
|
windowIDWrapper.data = _lastTopLevelWindowID;
|
|
|
|
Services.obs.notifyObservers(windowIDWrapper,
|
|
|
|
"net:current-toplevel-outer-content-windowid");
|
|
|
|
}
|
|
|
|
|
2018-04-11 13:06:03 +03:00
|
|
|
function _handleEvent(event) {
|
|
|
|
switch (event.type) {
|
2017-07-28 00:41:00 +03:00
|
|
|
case "TabBrowserInserted":
|
2018-04-11 13:06:03 +03:00
|
|
|
if (event.target.ownerGlobal.gBrowser.selectedBrowser === event.target.linkedBrowser) {
|
|
|
|
_updateCurrentContentOuterWindowID(event.target.linkedBrowser);
|
2017-07-28 00:41:00 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "TabSelect":
|
2018-04-11 13:06:03 +03:00
|
|
|
_updateCurrentContentOuterWindowID(event.target.linkedBrowser);
|
2017-07-28 00:41:00 +03:00
|
|
|
break;
|
|
|
|
case "activate":
|
2018-04-11 13:06:03 +03:00
|
|
|
WindowHelper.onActivate(event.target);
|
|
|
|
break;
|
|
|
|
case "sizemodechange":
|
|
|
|
WindowHelper.onSizemodeChange(event.target);
|
2017-07-28 00:41:00 +03:00
|
|
|
break;
|
|
|
|
case "unload":
|
2018-04-11 13:06:03 +03:00
|
|
|
WindowHelper.removeWindow(event.currentTarget);
|
2017-07-28 00:41:00 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-11 13:05:59 +03:00
|
|
|
function _handleMessage(message) {
|
|
|
|
let browser = message.target;
|
|
|
|
if (message.name === "Browser:Init" &&
|
2017-08-09 00:46:00 +03:00
|
|
|
browser === browser.ownerGlobal.gBrowser.selectedBrowser) {
|
|
|
|
_updateCurrentContentOuterWindowID(browser);
|
2017-07-28 00:41:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-11 13:06:03 +03:00
|
|
|
function _trackWindowOrder(window) {
|
|
|
|
_trackedWindows.splice(window.windowState == window.STATE_MINIMIZED ?
|
|
|
|
_trackedWindows.length - 1 : 0, 0, window);
|
|
|
|
}
|
|
|
|
|
|
|
|
function _untrackWindowOrder(window) {
|
|
|
|
let idx = _trackedWindows.indexOf(window);
|
|
|
|
if (idx >= 0)
|
|
|
|
_trackedWindows.splice(idx, 1);
|
|
|
|
}
|
|
|
|
|
2017-07-28 00:41:00 +03:00
|
|
|
// Methods that impact a window. Put into single object for organization.
|
|
|
|
var WindowHelper = {
|
2018-04-11 13:05:59 +03:00
|
|
|
addWindow(window) {
|
2017-07-28 00:41:00 +03:00
|
|
|
// Add event listeners
|
|
|
|
TAB_EVENTS.forEach(function(event) {
|
2018-04-11 13:05:59 +03:00
|
|
|
window.gBrowser.tabContainer.addEventListener(event, _handleEvent);
|
2017-07-28 00:41:00 +03:00
|
|
|
});
|
|
|
|
WINDOW_EVENTS.forEach(function(event) {
|
2018-04-11 13:05:59 +03:00
|
|
|
window.addEventListener(event, _handleEvent);
|
2017-07-28 00:41:00 +03:00
|
|
|
});
|
|
|
|
|
2018-04-11 13:05:59 +03:00
|
|
|
let messageManager = window.getGroupMessageManager("browsers");
|
2017-07-28 00:41:00 +03:00
|
|
|
messageManager.addMessageListener("Browser:Init", _handleMessage);
|
|
|
|
|
2018-04-11 13:06:03 +03:00
|
|
|
_trackWindowOrder(window);
|
2017-07-28 00:41:00 +03:00
|
|
|
|
|
|
|
// Update the selected tab's content outer window ID.
|
2018-04-11 13:05:59 +03:00
|
|
|
_updateCurrentContentOuterWindowID(window.gBrowser.selectedBrowser);
|
2017-07-28 00:41:00 +03:00
|
|
|
},
|
|
|
|
|
2018-04-11 13:05:59 +03:00
|
|
|
removeWindow(window) {
|
2018-04-11 13:06:03 +03:00
|
|
|
_untrackWindowOrder(window);
|
2017-07-28 00:41:00 +03:00
|
|
|
|
|
|
|
// Remove the event listeners
|
|
|
|
TAB_EVENTS.forEach(function(event) {
|
2018-04-11 13:05:59 +03:00
|
|
|
window.gBrowser.tabContainer.removeEventListener(event, _handleEvent);
|
2017-07-28 00:41:00 +03:00
|
|
|
});
|
|
|
|
WINDOW_EVENTS.forEach(function(event) {
|
2018-04-11 13:05:59 +03:00
|
|
|
window.removeEventListener(event, _handleEvent);
|
2017-07-28 00:41:00 +03:00
|
|
|
});
|
|
|
|
|
2018-04-11 13:05:59 +03:00
|
|
|
let messageManager = window.getGroupMessageManager("browsers");
|
2017-07-28 00:41:00 +03:00
|
|
|
messageManager.removeMessageListener("Browser:Init", _handleMessage);
|
|
|
|
},
|
|
|
|
|
2018-09-29 12:39:57 +03:00
|
|
|
onActivate(window) {
|
2017-07-28 00:41:00 +03:00
|
|
|
// If this window was the last focused window, we don't need to do anything
|
2018-04-11 13:06:03 +03:00
|
|
|
if (window == _trackedWindows[0])
|
2017-07-28 00:41:00 +03:00
|
|
|
return;
|
|
|
|
|
2018-04-11 13:06:03 +03:00
|
|
|
_untrackWindowOrder(window);
|
|
|
|
_trackWindowOrder(window);
|
2017-07-28 00:41:00 +03:00
|
|
|
|
2018-04-11 13:05:59 +03:00
|
|
|
_updateCurrentContentOuterWindowID(window.gBrowser.selectedBrowser);
|
2017-07-28 00:41:00 +03:00
|
|
|
},
|
|
|
|
|
2018-04-11 13:06:03 +03:00
|
|
|
onSizemodeChange(window) {
|
|
|
|
if (window.windowState == window.STATE_MINIMIZED) {
|
|
|
|
// Make sure to have the minimized window at the end of the list.
|
|
|
|
_untrackWindowOrder(window);
|
|
|
|
_trackedWindows.push(window);
|
|
|
|
}
|
2017-07-28 00:41:00 +03:00
|
|
|
},
|
2018-04-11 13:05:56 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
this.BrowserWindowTracker = {
|
|
|
|
/**
|
|
|
|
* Get the most recent browser window.
|
|
|
|
*
|
2018-04-11 13:05:59 +03:00
|
|
|
* @param options an object accepting the arguments for the search.
|
2018-04-11 13:05:56 +03:00
|
|
|
* * private: true to restrict the search to private windows
|
|
|
|
* only, false to restrict the search to non-private only.
|
|
|
|
* Omit the property to search in both groups.
|
|
|
|
* * allowPopups: true if popup windows are permissable.
|
|
|
|
*/
|
2018-09-10 23:27:38 +03:00
|
|
|
getTopWindow(options = {}) {
|
|
|
|
for (let win of _trackedWindows) {
|
|
|
|
if (!win.closed &&
|
|
|
|
(options.allowPopups || win.toolbar.visible) &&
|
|
|
|
(!("private" in options) ||
|
|
|
|
PrivateBrowsingUtils.permanentPrivateBrowsing ||
|
|
|
|
PrivateBrowsingUtils.isWindowPrivate(win) == options.private)) {
|
|
|
|
return win;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Number of currently open browser windows.
|
|
|
|
*/
|
|
|
|
get windowCount() {
|
|
|
|
return _trackedWindows.length;
|
2018-04-11 13:05:56 +03:00
|
|
|
},
|
|
|
|
|
2018-04-11 13:06:03 +03:00
|
|
|
/**
|
2018-09-10 23:27:38 +03:00
|
|
|
* Array of browser windows ordered by z-index, in reverse order.
|
|
|
|
* This means that the top-most browser window will be the first item.
|
2018-04-11 13:06:03 +03:00
|
|
|
*/
|
2018-09-10 23:27:38 +03:00
|
|
|
get orderedWindows() {
|
|
|
|
// Clone the windows array immediately as it may change during iteration,
|
|
|
|
// we'd rather have an outdated order than skip/revisit windows.
|
|
|
|
return [..._trackedWindows];
|
2018-04-11 13:06:03 +03:00
|
|
|
},
|
|
|
|
|
2018-04-11 13:05:56 +03:00
|
|
|
track(window) {
|
|
|
|
return WindowHelper.addWindow(window);
|
2018-08-31 08:59:17 +03:00
|
|
|
},
|
2017-07-28 00:41:00 +03:00
|
|
|
};
|