Bug 1361855 - Record TAB_COUNT histogram during TabClose event; r?bsmedberg,Gijs draft
authorLie Ryan <lie.1296@gmail.com>
Sat, 27 May 2017 17:17:16 +0000
changeset 600357 7811e6d3aae498cbfc4c8526d5bbad30a0fcc824
parent 600356 fdb23de10f5d9a6388481724fe76871c4a39b9d4
child 600358 e5b134204ea0ccbb0c9f234338e8e8509216162f
push id65723
push userbmo:lie.1296@gmail.com
push dateMon, 26 Jun 2017 02:03:28 +0000
reviewersbsmedberg, Gijs
bugs1361855
milestone55.0a1
Bug 1361855 - Record TAB_COUNT histogram during TabClose event; r?bsmedberg,Gijs MozReview-Commit-ID: 3e67l80GlGw
browser/modules/BrowserUsageTelemetry.jsm
browser/modules/test/browser/browser_UsageTelemetry.js
--- a/browser/modules/BrowserUsageTelemetry.jsm
+++ b/browser/modules/BrowserUsageTelemetry.jsm
@@ -358,16 +358,20 @@ let BrowserUsageTelemetry = {
     }
   },
 
   handleEvent(event) {
     switch (event.type) {
       case "TabOpen":
         this._onTabOpen();
         break;
+      case "TabClose":
+        let tabCount = getOpenTabsAndWinsCounts().tabCount - 1;
+        this._recordTabCount(tabCount);
+        break;
       case "unload":
         this._unregisterWindow(event.target);
         break;
       case TAB_RESTORING_TOPIC:
         // We're restoring a new tab from a previous or crashed session.
         // We don't want to track the URIs from these tabs, so let
         // |URICountListener| know about them.
         let browser = event.target.linkedBrowser;
@@ -575,31 +579,33 @@ let BrowserUsageTelemetry = {
   },
 
   /**
    * Adds listeners to a single chrome window.
    */
   _registerWindow(win) {
     win.addEventListener("unload", this);
     win.addEventListener("TabOpen", this, true);
+    win.addEventListener("TabClose", this, true);
 
     // Don't include URI and domain counts when in private mode.
     if (PrivateBrowsingUtils.isWindowPrivate(win)) {
       return;
     }
     win.gBrowser.tabContainer.addEventListener(TAB_RESTORING_TOPIC, this);
     win.gBrowser.addTabsProgressListener(URICountListener);
   },
 
   /**
    * Removes listeners from a single chrome window.
    */
   _unregisterWindow(win) {
     win.removeEventListener("unload", this);
     win.removeEventListener("TabOpen", this, true);
+    win.removeEventListener("TabClose", this, true);
 
     // Don't include URI and domain counts when in private mode.
     if (PrivateBrowsingUtils.isWindowPrivate(win.defaultView)) {
       return;
     }
     win.defaultView.gBrowser.tabContainer.removeEventListener(TAB_RESTORING_TOPIC, this);
     win.defaultView.gBrowser.removeTabsProgressListener(URICountListener);
   },
--- a/browser/modules/test/browser/browser_UsageTelemetry.js
+++ b/browser/modules/test/browser/browser_UsageTelemetry.js
@@ -339,15 +339,18 @@ add_task(async function test_tabsHistogr
     let oldLastRecordTabCount = BrowserUsageTelemetry._lastRecordTabCount;
     await BrowserTestUtils.loadURI(tab.linkedBrowser, "http://example.com/");
     await BrowserTestUtils.browserLoaded(tab.linkedBrowser);
     checkTabCountHistogram(tabCountHist.snapshot(), [0,0,1,2,1,1,0,2], "TAB_COUNT telemetry - page load, recount event included");
     ok(BrowserUsageTelemetry._lastRecordTabCount != oldLastRecordTabCount, "TAB_COUNT telemetry - _lastRecordTabCount updated");
     ok(BrowserUsageTelemetry._lastRecordTabCount > new Date().getTime() - BrowserUsageTelemetry._tabCountIntervalMillis, "TAB_COUNT telemetry - _lastRecordTabCount invariant");
   }
 
+  BrowserUsageTelemetry._lastRecordTabCount = 0;
+  await BrowserTestUtils.removeTab(openedTabs2.pop());
+  checkTabCountHistogram(tabCountHist.snapshot(), [0,0,1,2,1,1,1,2], "TAB_COUNT telemetry - closing a tab");
 
   // Remove all the extra windows and tabs.
   for (let tab of openedTabs) {
     await BrowserTestUtils.removeTab(tab);
   }
   await BrowserTestUtils.closeWindow(win);
 });