Bug 1402944: Part 6 - Optimize getBrowserInfo some more. r?mixedpuppy draft
authorKris Maglione <maglione.k@gmail.com>
Sat, 23 Sep 2017 00:50:46 -0700
changeset 670069 92bbd3a8aba4f4d21af2b10447fe8e3c9cb0786f
parent 670068 61f3015627d4afb0c6c7762039092052e1b9d71c
child 670070 0803beba75690dd034e0313fde8f2b7ac325d67f
push id81499
push usermaglione.k@gmail.com
push dateMon, 25 Sep 2017 19:05:45 +0000
reviewersmixedpuppy
bugs1402944
milestone58.0a1
Bug 1402944: Part 6 - Optimize getBrowserInfo some more. r?mixedpuppy MozReview-Commit-ID: 8kdeVqKb889
browser/components/extensions/ext-browser.js
--- a/browser/components/extensions/ext-browser.js
+++ b/browser/components/extensions/ext-browser.js
@@ -200,16 +200,17 @@ global.WindowEventManager = class extend
   }
 };
 
 class TabTracker extends TabTrackerBase {
   constructor() {
     super();
 
     this._tabs = new WeakMap();
+    this._browsers = new WeakMap();
     this._tabIds = new Map();
     this._nextId = 1;
 
     this._handleTabDestroyed = this._handleTabDestroyed.bind(this);
   }
 
   init() {
     if (this.initialized) {
@@ -230,29 +231,48 @@ class TabTracker extends TabTrackerBase 
 
     /* eslint-disable mozilla/balanced-listeners */
     this.on("tab-detached", this._handleTabDestroyed);
     this.on("tab-removed", this._handleTabDestroyed);
     /* eslint-enable mozilla/balanced-listeners */
   }
 
   getId(nativeTab) {
-    if (this._tabs.has(nativeTab)) {
-      return this._tabs.get(nativeTab);
+    let id = this._tabs.get(nativeTab);
+    if (id) {
+      return id;
     }
 
     this.init();
 
-    let id = this._nextId++;
+    id = this._nextId++;
     this.setId(nativeTab, id);
     return id;
   }
 
+  getBrowserTabId(browser) {
+    let id = this._browsers.get(browser);
+    if (id) {
+      return id;
+    }
+
+    let tab = browser.ownerGlobal.gBrowser.getTabForBrowser(browser);
+    if (tab) {
+      id = this.getId(tab);
+      this._browsers.set(browser, id);
+      return id;
+    }
+    return -1;
+  }
+
   setId(nativeTab, id) {
     this._tabs.set(nativeTab, id);
+    if (nativeTab.linkedBrowser) {
+      this._browsers.set(nativeTab.linkedBrowser, id);
+    }
     this._tabIds.set(id, nativeTab);
   }
 
   _handleTabDestroyed(event, {nativeTab}) {
     let id = this._tabs.get(nativeTab);
     if (id) {
       this._tabs.delete(nativeTab);
       if (this._tabIds.get(id) === nativeTab) {
@@ -498,40 +518,37 @@ class TabTracker extends TabTrackerBase 
   emitRemoved(nativeTab, isWindowClosing) {
     let windowId = windowTracker.getId(nativeTab.ownerGlobal);
     let tabId = this.getId(nativeTab);
 
     this.emit("tab-removed", {nativeTab, tabId, windowId, isWindowClosing});
   }
 
   getBrowserData(browser) {
-    if (browser.ownerDocument.documentURI === "about:addons") {
-      // When we're loaded into a <browser> inside about:addons, we need to go up
-      // one more level.
-      browser = browser.ownerDocument.docShell.chromeEventHandler;
-    }
+    let {gBrowser} = browser.ownerGlobal;
+    // Some non-browser windows have gBrowser but not getTabForBrowser!
+    if (!gBrowser || !gBrowser.getTabForBrowser) {
+      if (browser.ownerDocument.documentURI === "about:addons") {
+        // When we're loaded into a <browser> inside about:addons, we need to go up
+        // one more level.
+        browser = browser.ownerDocument.docShell.chromeEventHandler;
 
-    let result = {
-      tabId: -1,
-      windowId: -1,
-    };
-
-    let {gBrowser} = browser.ownerGlobal;
-    // Some non-browser windows have gBrowser but not
-    // getTabForBrowser!
-    if (gBrowser && gBrowser.getTabForBrowser) {
-      result.windowId = windowTracker.getId(browser.ownerGlobal);
-
-      let nativeTab = gBrowser.getTabForBrowser(browser);
-      if (nativeTab) {
-        result.tabId = this.getId(nativeTab);
+        ({gBrowser} = browser.ownerGlobal);
+      } else {
+        return {
+          tabId: -1,
+          windowId: -1,
+        };
       }
     }
 
-    return result;
+    return {
+      tabId: this.getBrowserTabId(browser),
+      windowId: windowTracker.getId(browser.ownerGlobal),
+    };
   }
 
   get activeTab() {
     let window = windowTracker.topWindow;
     if (window && window.gBrowser) {
       return window.gBrowser.selectedTab;
     }
     return null;