Bug 1475427 - Refactor warnAboutClosingTabs function in tabbrowser.js so that it directly takes the number of tabs to close as param. r?jaws draft refactor_warnAboutClosingTabs
authorAbdoulaye O. Ly <ablayelyfondou@gmail.com>
Wed, 18 Jul 2018 19:30:36 +0000
branchrefactor_warnAboutClosingTabs
changeset 820501 97b955ddd846eb4ca360a176bf6fa086803ccb35
parent 819486 afa310dc89beeb4b7a9564d2c89ff32906f427ad
push id116850
push userbmo:ablayelyfondou@gmail.com
push dateThu, 19 Jul 2018 18:28:14 +0000
reviewersjaws
bugs1475427
milestone63.0a1
Bug 1475427 - Refactor warnAboutClosingTabs function in tabbrowser.js so that it directly takes the number of tabs to close as param. r?jaws MozReview-Commit-ID: Hhdanmwx7rN
browser/base/content/browser.js
browser/base/content/tabbrowser.js
browser/components/nsBrowserGlue.js
--- a/browser/base/content/browser.js
+++ b/browser/base/content/browser.js
@@ -6977,18 +6977,21 @@ function WindowIsClosing() {
  * Checks if this is the last full *browser* window around. If it is, this will
  * be communicated like quitting. Otherwise, we warn about closing multiple tabs.
  * @returns true if closing can proceed, false if it got cancelled.
  */
 function warnAboutClosingWindow() {
   // Popups aren't considered full browser windows; we also ignore private windows.
   let isPBWindow = PrivateBrowsingUtils.isWindowPrivate(window) &&
         !PrivateBrowsingUtils.permanentPrivateBrowsing;
+
+  let closingTabs = gBrowser.tabs.length - gBrowser._removingTabs.length;
+
   if (!isPBWindow && !toolbar.visible)
-    return gBrowser.warnAboutClosingTabs(gBrowser.closingTabsEnum.ALL);
+    return gBrowser.warnAboutClosingTabs(closingTabs, gBrowser.closingTabsEnum.ALL);
 
   // Figure out if there's at least one other browser window around.
   let otherPBWindowExists = false;
   let nonPopupPresent = false;
   for (let win of browserWindows()) {
     if (!win.closed && win != window) {
       if (isPBWindow && PrivateBrowsingUtils.isWindowPrivate(win))
         otherPBWindowExists = true;
@@ -7010,17 +7013,17 @@ function warnAboutClosingWindow() {
     exitingCanceled.data = false;
     Services.obs.notifyObservers(exitingCanceled,
                                  "last-pb-context-exiting");
     if (exitingCanceled.data)
       return false;
   }
 
   if (nonPopupPresent) {
-    return isPBWindow || gBrowser.warnAboutClosingTabs(gBrowser.closingTabsEnum.ALL);
+    return isPBWindow || gBrowser.warnAboutClosingTabs(closingTabs, gBrowser.closingTabsEnum.ALL);
   }
 
   let os = Services.obs;
 
   let closingCanceled = Cc["@mozilla.org/supports-PRBool;1"].
                         createInstance(Ci.nsISupportsPRBool);
   os.notifyObservers(closingCanceled,
                      "browser-lastwindow-close-requested");
@@ -7028,17 +7031,17 @@ function warnAboutClosingWindow() {
     return false;
 
   os.notifyObservers(null, "browser-lastwindow-close-granted");
 
   // OS X doesn't quit the application when the last window is closed, but keeps
   // the session alive. Hence don't prompt users to save tabs, but warn about
   // closing multiple tabs.
   return AppConstants.platform != "macosx"
-         || (isPBWindow || gBrowser.warnAboutClosingTabs(gBrowser.closingTabsEnum.ALL));
+         || (isPBWindow || gBrowser.warnAboutClosingTabs(closingTabs, gBrowser.closingTabsEnum.ALL));
 }
 
 var MailIntegration = {
   sendLinkForBrowser(aBrowser) {
     this.sendMessage(gURLBar.makeURIReadable(aBrowser.currentURI).displaySpec, aBrowser.contentTitle);
   },
 
   sendMessage(aBody, aSubject) {
--- a/browser/base/content/tabbrowser.js
+++ b/browser/base/content/tabbrowser.js
@@ -2455,49 +2455,17 @@ window._gBrowser = {
     // Additionally send pinned tab events
     if (pinned) {
       this._notifyPinnedStatus(t);
     }
 
     return t;
   },
 
-  warnAboutClosingTabs(aCloseTabs, aTab, aOptionalMessage) {
-    var tabsToClose;
-    switch (aCloseTabs) {
-      case this.closingTabsEnum.ALL:
-        tabsToClose = this.tabs.length - this._removingTabs.length -
-          gBrowser._numPinnedTabs;
-        break;
-      case this.closingTabsEnum.OTHER:
-        if (!aTab) {
-          throw new Error("Required argument missing: aTab");
-        }
-        if (aTab.multiselected) {
-          tabsToClose = this.visibleTabs.filter(tab => !tab.multiselected && !tab.pinned).length;
-        } else {
-          // If aTab is pinned, it will already be considered
-          // with gBrowser._numPinnedTabs.
-          tabsToClose = this.visibleTabs.length - gBrowser._numPinnedTabs -
-            (aTab.pinned ? 0 : 1);
-        }
-        break;
-      case this.closingTabsEnum.TO_END:
-        if (!aTab) {
-          throw new Error("Required argument missing: aTab");
-        }
-        tabsToClose = this.getTabsToTheEndFrom(aTab).length;
-        break;
-      case this.closingTabsEnum.MULTI_SELECTED:
-        tabsToClose = this.multiSelectedTabsCount;
-        break;
-      default:
-        throw new Error("Invalid argument: " + aCloseTabs);
-    }
-
+  warnAboutClosingTabs(tabsToClose, aCloseTabs, aOptionalMessage) {
     if (tabsToClose <= 1)
       return true;
 
     const pref = aCloseTabs == this.closingTabsEnum.ALL ?
       "browser.tabs.warnOnClose" : "browser.tabs.warnOnCloseOtherTabs";
     var shouldPrompt = Services.prefs.getBoolPref(pref);
     if (!shouldPrompt)
       return true;
@@ -2549,48 +2517,51 @@ window._gBrowser = {
         break;
       }
       tabsToEnd.push(tabs[i]);
     }
     return tabsToEnd;
   },
 
   removeTabsToTheEndFrom(aTab) {
-    if (!this.warnAboutClosingTabs(this.closingTabsEnum.TO_END, aTab))
+    let tabs = this.getTabsToTheEndFrom(aTab);
+    if (!this.warnAboutClosingTabs(tabs.length, this.closingTabsEnum.TO_END)) {
       return;
-
-    let tabs = this.getTabsToTheEndFrom(aTab);
+    }
+
     this.removeTabs(tabs);
   },
 
   /**
    * In a multi-select context, all unpinned and unselected tabs are removed.
    * Otherwise all unpinned tabs except aTab are removed.
    */
   removeAllTabsBut(aTab) {
-    if (!this.warnAboutClosingTabs(this.closingTabsEnum.OTHER, aTab)) {
-      return;
-    }
-
     let tabsToRemove = [];
     if (aTab && aTab.multiselected) {
       tabsToRemove = this.visibleTabs.filter(tab => !tab.multiselected && !tab.pinned);
     } else {
       tabsToRemove = this.visibleTabs.filter(tab => tab != aTab && !tab.pinned);
       this.selectedTab = aTab;
     }
+
+    if (!this.warnAboutClosingTabs(tabsToRemove.length, this.closingTabsEnum.OTHER)) {
+      return;
+    }
+
     this.removeTabs(tabsToRemove);
   },
 
   removeMultiSelectedTabs() {
-    if (!this.warnAboutClosingTabs(this.closingTabsEnum.MULTI_SELECTED)) {
+    let selectedTabs = this.selectedTabs;
+    if (!this.warnAboutClosingTabs(selectedTabs.length, this.closingTabsEnum.MULTI_SELECTED)) {
       return;
     }
 
-    this.removeTabs(this.selectedTabs);
+    this.removeTabs(selectedTabs);
   },
 
   removeTabs(tabs) {
     let tabsWithBeforeUnload = [];
     let lastToClose;
     let aParams = {animation: true};
     for (let tab of tabs) {
       if (tab.selected)
--- a/browser/components/nsBrowserGlue.js
+++ b/browser/components/nsBrowserGlue.js
@@ -1399,28 +1399,29 @@ BrowserGlue.prototype = {
     if (sessionWillBeRestored || !Services.prefs.getBoolPref("browser.warnOnQuit") ||
         !Services.prefs.getBoolPref("browser.tabs.warnOnClose"))
       return;
 
     let win = BrowserWindowTracker.getTopWindow();
 
     // warnAboutClosingTabs checks browser.tabs.warnOnClose and returns if it's
     // ok to close the window. It doesn't actually close the window.
+    let closingTabs = win.gBrowser.tabs.length - win.gBrowser._removingTabs.length;
     if (windowcount == 1) {
       aCancelQuit.data =
-        !win.gBrowser.warnAboutClosingTabs(win.gBrowser.closingTabsEnum.ALL);
+        !win.gBrowser.warnAboutClosingTabs(closingTabs, win.gBrowser.closingTabsEnum.ALL);
     } else {
       // More than 1 window. Compose our own message.
       let tabSubstring = gTabbrowserBundle.GetStringFromName("tabs.closeWarningMultipleWindowsTabSnippet");
       tabSubstring = PluralForm.get(pagecount, tabSubstring).replace(/#1/, pagecount);
       let windowString = gTabbrowserBundle.GetStringFromName("tabs.closeWarningMultipleWindows");
       windowString = PluralForm.get(windowcount, windowString).replace(/#1/, windowcount);
       windowString = windowString.replace(/%(?:1$)?S/i, tabSubstring);
       aCancelQuit.data =
-        !win.gBrowser.warnAboutClosingTabs(win.gBrowser.closingTabsEnum.ALL, null, windowString);
+        !win.gBrowser.warnAboutClosingTabs(closingTabs, win.gBrowser.closingTabsEnum.ALL, null, windowString);
     }
   },
 
   _showUpdateNotification: function BG__showUpdateNotification() {
     Services.prefs.clearUserPref("app.update.postupdate");
 
     var um = Cc["@mozilla.org/updates/update-manager;1"].
              getService(Ci.nsIUpdateManager);