2017-07-27 21:41:00 +00: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 10:05:56 +00:00
|
|
|
var EXPORTED_SYMBOLS = ["BrowserWindowTracker"];
|
2017-07-27 21:41:00 +00:00
|
|
|
|
2019-01-17 18:18:31 +00:00
|
|
|
const {XPCOMUtils} = ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
|
2017-07-27 21:41:00 +00:00
|
|
|
|
|
|
|
// Lazy getters
|
2018-04-11 10:05:56 +00:00
|
|
|
XPCOMUtils.defineLazyModuleGetters(this, {
|
2018-08-31 05:59:17 +00:00
|
|
|
PrivateBrowsingUtils: "resource://gre/modules/PrivateBrowsingUtils.jsm",
|
2018-04-11 10:05:56 +00:00
|
|
|
});
|
2017-07-27 21:41:00 +00:00
|
|
|
|
|
|
|
// Constants
|
|
|
|
const TAB_EVENTS = ["TabBrowserInserted", "TabSelect"];
|
2019-02-08 16:34:41 +00:00
|
|
|
const WINDOW_EVENTS = ["activate", "unload"];
|
2017-07-27 21:41:00 +00:00
|
|
|
const DEBUG = false;
|
|
|
|
|
|
|
|
// Variables
|
|
|
|
var _lastTopLevelWindowID = 0;
|
2018-04-11 10:06:03 +00:00
|
|
|
var _trackedWindows = [];
|
2017-07-27 21:41:00 +00:00
|
|
|
|
|
|
|
// Global methods
|
|
|
|
function debug(s) {
|
|
|
|
if (DEBUG) {
|
|
|
|
dump("-*- UpdateTopLevelContentWindowIDHelper: " + s + "\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-11 10:05:59 +00:00
|
|
|
function _updateCurrentContentOuterWindowID(browser) {
|
|
|
|
if (!browser.outerWindowID ||
|
2018-09-26 11:57:07 +00:00
|
|
|
browser.outerWindowID === _lastTopLevelWindowID ||
|
|
|
|
browser.ownerGlobal != _trackedWindows[0]) {
|
2017-07-27 21:41:00 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-06 09:48:46 +00:00
|
|
|
debug("Current window uri=" + (browser.currentURI && browser.currentURI.spec) +
|
2018-04-11 10:05:59 +00:00
|
|
|
" id=" + browser.outerWindowID);
|
2017-07-27 21:41:00 +00:00
|
|
|
|
2018-04-11 10:05:59 +00:00
|
|
|
_lastTopLevelWindowID = browser.outerWindowID;
|
2018-02-28 17:51:33 +00:00
|
|
|
let windowIDWrapper = Cc["@mozilla.org/supports-PRUint64;1"]
|
2017-07-27 21:41:00 +00:00
|
|
|
.createInstance(Ci.nsISupportsPRUint64);
|
|
|
|
windowIDWrapper.data = _lastTopLevelWindowID;
|
|
|
|
Services.obs.notifyObservers(windowIDWrapper,
|
|
|
|
"net:current-toplevel-outer-content-windowid");
|
|
|
|
}
|
|
|
|
|
2018-04-11 10:06:03 +00:00
|
|
|
function _handleEvent(event) {
|
|
|
|
switch (event.type) {
|
2017-07-27 21:41:00 +00:00
|
|
|
case "TabBrowserInserted":
|
2018-04-11 10:06:03 +00:00
|
|
|
if (event.target.ownerGlobal.gBrowser.selectedBrowser === event.target.linkedBrowser) {
|
|
|
|
_updateCurrentContentOuterWindowID(event.target.linkedBrowser);
|
2017-07-27 21:41:00 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "TabSelect":
|
2018-04-11 10:06:03 +00:00
|
|
|
_updateCurrentContentOuterWindowID(event.target.linkedBrowser);
|
2017-07-27 21:41:00 +00:00
|
|
|
break;
|
|
|
|
case "activate":
|
2018-04-11 10:06:03 +00:00
|
|
|
WindowHelper.onActivate(event.target);
|
|
|
|
break;
|
2017-07-27 21:41:00 +00:00
|
|
|
case "unload":
|
2018-04-11 10:06:03 +00:00
|
|
|
WindowHelper.removeWindow(event.currentTarget);
|
2017-07-27 21:41:00 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-11 10:05:59 +00:00
|
|
|
function _handleMessage(message) {
|
|
|
|
let browser = message.target;
|
|
|
|
if (message.name === "Browser:Init" &&
|
2017-08-08 21:46:00 +00:00
|
|
|
browser === browser.ownerGlobal.gBrowser.selectedBrowser) {
|
|
|
|
_updateCurrentContentOuterWindowID(browser);
|
2017-07-27 21:41:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-11 10:06:03 +00:00
|
|
|
function _trackWindowOrder(window) {
|
2019-02-18 07:04:43 +00:00
|
|
|
if (window.windowState == window.STATE_MINIMIZED) {
|
|
|
|
let firstMinimizedWindow = _trackedWindows.findIndex(w => w.windowState == w.STATE_MINIMIZED);
|
|
|
|
if (firstMinimizedWindow == -1) {
|
|
|
|
firstMinimizedWindow = _trackedWindows.length;
|
|
|
|
}
|
|
|
|
_trackedWindows.splice(firstMinimizedWindow, 0, window);
|
|
|
|
} else {
|
|
|
|
_trackedWindows.unshift(window);
|
|
|
|
}
|
2018-04-11 10:06:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function _untrackWindowOrder(window) {
|
|
|
|
let idx = _trackedWindows.indexOf(window);
|
|
|
|
if (idx >= 0)
|
|
|
|
_trackedWindows.splice(idx, 1);
|
|
|
|
}
|
|
|
|
|
2017-07-27 21:41:00 +00:00
|
|
|
// Methods that impact a window. Put into single object for organization.
|
|
|
|
var WindowHelper = {
|
2018-04-11 10:05:59 +00:00
|
|
|
addWindow(window) {
|
2017-07-27 21:41:00 +00:00
|
|
|
// Add event listeners
|
|
|
|
TAB_EVENTS.forEach(function(event) {
|
2018-04-11 10:05:59 +00:00
|
|
|
window.gBrowser.tabContainer.addEventListener(event, _handleEvent);
|
2017-07-27 21:41:00 +00:00
|
|
|
});
|
|
|
|
WINDOW_EVENTS.forEach(function(event) {
|
2018-04-11 10:05:59 +00:00
|
|
|
window.addEventListener(event, _handleEvent);
|
2017-07-27 21:41:00 +00:00
|
|
|
});
|
|
|
|
|
2018-04-11 10:05:59 +00:00
|
|
|
let messageManager = window.getGroupMessageManager("browsers");
|
2017-07-27 21:41:00 +00:00
|
|
|
messageManager.addMessageListener("Browser:Init", _handleMessage);
|
|
|
|
|
2018-04-11 10:06:03 +00:00
|
|
|
_trackWindowOrder(window);
|
2017-07-27 21:41:00 +00:00
|
|
|
|
|
|
|
// Update the selected tab's content outer window ID.
|
2018-04-11 10:05:59 +00:00
|
|
|
_updateCurrentContentOuterWindowID(window.gBrowser.selectedBrowser);
|
2017-07-27 21:41:00 +00:00
|
|
|
},
|
|
|
|
|
2018-04-11 10:05:59 +00:00
|
|
|
removeWindow(window) {
|
2018-04-11 10:06:03 +00:00
|
|
|
_untrackWindowOrder(window);
|
2017-07-27 21:41:00 +00:00
|
|
|
|
|
|
|
// Remove the event listeners
|
|
|
|
TAB_EVENTS.forEach(function(event) {
|
2018-04-11 10:05:59 +00:00
|
|
|
window.gBrowser.tabContainer.removeEventListener(event, _handleEvent);
|
2017-07-27 21:41:00 +00:00
|
|
|
});
|
|
|
|
WINDOW_EVENTS.forEach(function(event) {
|
2018-04-11 10:05:59 +00:00
|
|
|
window.removeEventListener(event, _handleEvent);
|
2017-07-27 21:41:00 +00:00
|
|
|
});
|
|
|
|
|
2018-04-11 10:05:59 +00:00
|
|
|
let messageManager = window.getGroupMessageManager("browsers");
|
2017-07-27 21:41:00 +00:00
|
|
|
messageManager.removeMessageListener("Browser:Init", _handleMessage);
|
|
|
|
},
|
|
|
|
|
2018-09-29 09:39:57 +00:00
|
|
|
onActivate(window) {
|
2017-07-27 21:41:00 +00:00
|
|
|
// If this window was the last focused window, we don't need to do anything
|
2018-04-11 10:06:03 +00:00
|
|
|
if (window == _trackedWindows[0])
|
2017-07-27 21:41:00 +00:00
|
|
|
return;
|
|
|
|
|
2018-04-11 10:06:03 +00:00
|
|
|
_untrackWindowOrder(window);
|
|
|
|
_trackWindowOrder(window);
|
2017-07-27 21:41:00 +00:00
|
|
|
|
2018-04-11 10:05:59 +00:00
|
|
|
_updateCurrentContentOuterWindowID(window.gBrowser.selectedBrowser);
|
2017-07-27 21:41:00 +00:00
|
|
|
},
|
2018-04-11 10:05:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this.BrowserWindowTracker = {
|
|
|
|
/**
|
|
|
|
* Get the most recent browser window.
|
|
|
|
*
|
2018-04-11 10:05:59 +00:00
|
|
|
* @param options an object accepting the arguments for the search.
|
2018-04-11 10:05:56 +00: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 20:27:38 +00: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 10:05:56 +00:00
|
|
|
},
|
|
|
|
|
2018-04-11 10:06:03 +00:00
|
|
|
/**
|
2018-09-10 20:27:38 +00: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 10:06:03 +00:00
|
|
|
*/
|
2018-09-10 20:27:38 +00: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 10:06:03 +00:00
|
|
|
},
|
|
|
|
|
2018-04-11 10:05:56 +00:00
|
|
|
track(window) {
|
|
|
|
return WindowHelper.addWindow(window);
|
2018-08-31 05:59:17 +00:00
|
|
|
},
|
2017-07-27 21:41:00 +00:00
|
|
|
};
|