Bug 1322869: Part 2 - Memoize some relatively expensive-to-compute tab attributes. r?aswan
MozReview-Commit-ID: EpjsLhlNzvG
--- a/browser/components/extensions/ext-utils.js
+++ b/browser/components/extensions/ext-utils.js
@@ -670,17 +670,17 @@ ExtensionTabManager.prototype = {
id: TabManager.getId(tab),
index: tab._tPos,
windowId: WindowManager.getId(window),
selected: tab.selected,
highlighted: tab.selected,
active: tab.selected,
pinned: tab.pinned,
status: TabManager.getStatus(tab),
- incognito: PrivateBrowsingUtils.isBrowserPrivate(browser),
+ incognito: WindowManager.isBrowserPrivate(browser),
width: browser.frameLoader.lazyWidth || browser.clientWidth,
height: browser.frameLoader.lazyHeight || browser.clientHeight,
audible: tab.soundPlaying,
mutedInfo,
};
if (this.extension.hasPermission("cookies")) {
result.cookieStoreId = getCookieStoreIdForTab(result, tab);
}
@@ -920,16 +920,21 @@ TabManager.for = function(extension) {
};
/* eslint-disable mozilla/balanced-listeners */
extensions.on("shutdown", (type, extension) => {
tabManagers.delete(extension);
});
/* eslint-enable mozilla/balanced-listeners */
+function memoize(fn) {
+ let weakMap = new DefaultWeakMap(fn);
+ return weakMap.get.bind(weakMap);
+}
+
// Manages mapping between XUL windows and extension window IDs.
global.WindowManager = {
// Note: These must match the values in windows.json.
WINDOW_ID_NONE: -1,
WINDOW_ID_CURRENT: -2,
get topWindow() {
return Services.wm.getMostRecentWindow("navigator:browser");
@@ -959,23 +964,26 @@ global.WindowManager = {
if (options.width !== null || options.height !== null) {
let width = options.width !== null ? options.width : window.outerWidth;
let height = options.height !== null ? options.height : window.outerHeight;
window.resizeTo(width, height);
}
},
- getId(window) {
- if (!window.QueryInterface) {
- return null;
+ isBrowserPrivate: memoize(browser => {
+ return PrivateBrowsingUtils.isBrowserPrivate(browser);
+ }),
+
+ getId: memoize(window => {
+ if (window instanceof Ci.nsIInterfaceRequestor) {
+ return window.getInterface(Ci.nsIDOMWindowUtils).outerWindowID;
}
- return window.QueryInterface(Ci.nsIInterfaceRequestor)
- .getInterface(Ci.nsIDOMWindowUtils).outerWindowID;
- },
+ return null;
+ }),
getWindow(id, context) {
if (id == this.WINDOW_ID_CURRENT) {
return currentWindow(context);
}
for (let window of WindowListManager.browserWindows(true)) {
if (this.getId(window) == id) {
--- a/toolkit/components/extensions/ExtensionUtils.jsm
+++ b/toolkit/components/extensions/ExtensionUtils.jsm
@@ -154,17 +154,17 @@ function extend(obj, ...args) {
class DefaultWeakMap extends WeakMap {
constructor(defaultConstructor, init) {
super(init);
this.defaultConstructor = defaultConstructor;
}
get(key) {
if (!this.has(key)) {
- this.set(key, this.defaultConstructor());
+ this.set(key, this.defaultConstructor(key));
}
return super.get(key);
}
}
class DefaultMap extends Map {
constructor(defaultConstructor, init) {
super(init);