Bug 1316740 - Add warning messages when containers are deleted, r=Gijs
authorAndrea Marchesini <amarchesini@mozilla.com>
Wed, 23 Nov 2016 16:33:57 +0100
changeset 324098 5032b5290d7cd1ac6fcbb0ef57311771ccdfad4b
parent 324097 9789423a63aba19ca6b5fecb97f4dacbb1fc0181
child 324099 1053366efde1bec1f6b826e620843cf1cf15c59a
push id24
push usermaklebus@msu.edu
push dateTue, 20 Dec 2016 03:11:33 +0000
reviewersGijs
bugs1316740
milestone53.0a1
Bug 1316740 - Add warning messages when containers are deleted, r=Gijs
browser/components/contextualidentity/test/browser/browser_count_and_remove.js
browser/components/preferences/in-content/containers.js
browser/components/preferences/in-content/privacy.js
browser/locales/en-US/chrome/browser/preferences/preferences.properties
toolkit/components/contextualidentity/ContextualIdentityService.jsm
--- a/browser/components/contextualidentity/test/browser/browser_count_and_remove.js
+++ b/browser/components/contextualidentity/test/browser/browser_count_and_remove.js
@@ -17,18 +17,31 @@ add_task(function* setup() {
         ]});
 });
 
 add_task(function* test() {
   is(ContextualIdentityService.countContainerTabs(), 0, "0 container tabs by default.");
 
   openTabInUserContext(1);
   is(ContextualIdentityService.countContainerTabs(), 1, "1 container tab created");
+  is(ContextualIdentityService.countContainerTabs(1), 1, "1 container tab created with id 1");
+  is(ContextualIdentityService.countContainerTabs(2), 0, "0 container tabs created with id 2");
 
   openTabInUserContext(1);
-  is(ContextualIdentityService.countContainerTabs(), 2, "2 container tab created");
+  is(ContextualIdentityService.countContainerTabs(), 2, "2 container tabs created");
+  is(ContextualIdentityService.countContainerTabs(1), 2, "2 container tabs created with id 1");
+  is(ContextualIdentityService.countContainerTabs(2), 0, "0 container tabs created with id 2");
 
   openTabInUserContext(2);
   is(ContextualIdentityService.countContainerTabs(), 3, "3 container tab created");
+  is(ContextualIdentityService.countContainerTabs(1), 2, "2 container tabs created with id 1");
+  is(ContextualIdentityService.countContainerTabs(2), 1, "1 container tab created with id 2");
 
-  ContextualIdentityService.closeAllContainerTabs();
-  is(ContextualIdentityService.countContainerTabs(), 0, "0 container tab at the end.");
+  ContextualIdentityService.closeContainerTabs(1);
+  is(ContextualIdentityService.countContainerTabs(), 1, "1 container tab created");
+  is(ContextualIdentityService.countContainerTabs(1), 0, "0 container tabs created with id 1");
+  is(ContextualIdentityService.countContainerTabs(2), 1, "1 container tab created with id 2");
+
+  ContextualIdentityService.closeContainerTabs();
+  is(ContextualIdentityService.countContainerTabs(), 0, "0 container tabs at the end.");
+  is(ContextualIdentityService.countContainerTabs(1), 0, "0 container tabs at the end with id 1.");
+  is(ContextualIdentityService.countContainerTabs(2), 0, "0 container tabs at the end with id 2.");
 });
--- a/browser/components/preferences/in-content/containers.js
+++ b/browser/components/preferences/in-content/containers.js
@@ -34,20 +34,44 @@ let gContainersPane = {
       item.setAttribute("containerColor", container.color);
       item.setAttribute("userContextId", container.userContextId);
 
       this._list.appendChild(item);
     }
   },
 
   onRemoveClick(button) {
-    let userContextId = button.getAttribute("value");
+    let userContextId = parseInt(button.getAttribute("value"), 10);
+
+    let count = ContextualIdentityService.countContainerTabs(userContextId);
+    if (count > 0) {
+      let bundlePreferences = document.getElementById("bundlePreferences");
+
+      let title = bundlePreferences.getString("removeContainerAlertTitle");
+      let message = PluralForm.get(count, bundlePreferences.getString("removeContainerMsg"))
+                              .replace("#S", count)
+      let okButton = bundlePreferences.getString("removeContainerOkButton");
+      let cancelButton = bundlePreferences.getString("removeContainerButton2");
+
+      let buttonFlags = (Ci.nsIPrompt.BUTTON_TITLE_IS_STRING * Ci.nsIPrompt.BUTTON_POS_0) +
+                        (Ci.nsIPrompt.BUTTON_TITLE_IS_STRING * Ci.nsIPrompt.BUTTON_POS_1);
+
+      let rv = Services.prompt.confirmEx(window, title, message, buttonFlags,
+                                         okButton, cancelButton, null, null, {});
+      if (rv != 0) {
+        return;
+      }
+
+      ContextualIdentityService.closeContainerTabs(userContextId);
+    }
+
     ContextualIdentityService.remove(userContextId);
     this._rebuildView();
   },
+
   onPeferenceClick(button) {
     this.openPreferenceDialog(button.getAttribute("value"));
   },
 
   onAddButtonClick(button) {
     this.openPreferenceDialog(null);
   },
 
--- a/browser/components/preferences/in-content/privacy.js
+++ b/browser/components/preferences/in-content/privacy.js
@@ -99,17 +99,17 @@ var gPrivacyPane = {
     let cancelButton = bundlePreferences.getString("disableContainersButton2");
 
     let buttonFlags = (Ci.nsIPrompt.BUTTON_TITLE_IS_STRING * Ci.nsIPrompt.BUTTON_POS_0) +
                       (Ci.nsIPrompt.BUTTON_TITLE_IS_STRING * Ci.nsIPrompt.BUTTON_POS_1);
 
     let rv = Services.prompt.confirmEx(window, title, message, buttonFlags,
                                        okButton, cancelButton, null, null, {});
     if (rv == 0) {
-      ContextualIdentityService.closeAllContainerTabs();
+      ContextualIdentityService.closeContainerTabs();
       Services.prefs.setBoolPref("privacy.userContext.enabled", false);
       return;
     }
 
     checkbox.checked = true;
   },
 
   /**
--- a/browser/locales/en-US/chrome/browser/preferences/preferences.properties
+++ b/browser/locales/en-US/chrome/browser/preferences/preferences.properties
@@ -193,8 +193,18 @@ disableContainersAlertTitle=Close All Co
 disableContainersMsg=If you disable Container Tabs now, #S container tab will be closed. Are you sure you want to disable Container Tabs?;If you disable Container Tabs now, #S container tabs will be closed. Are you sure you want to disable Container Tabs?
 
 # LOCALIZATION NOTE (disableContainersOkButton): Semi-colon list of plural forms.
 # See: http://developer.mozilla.org/en/docs/Localization_and_Plurals
 # #S is the number of container tabs
 disableContainersOkButton=Close #S Container Tab;Close #S Container Tabs
 
 disableContainersButton2=Keep enabled
+
+removeContainerAlertTitle=Remove This Container?
+
+# LOCALIZATION NOTE (removeContainerMsg): Semi-colon list of plural forms.
+# See: http://developer.mozilla.org/en/docs/Localization_and_Plurals
+# #S is the number of container tabs
+removeContainerMsg=If you remove this Container now, #S container tab will be closed. Are you sure you want to remove this Container?;If you remove this Container now, #S container tabs will be closed. Are you sure you want to remove this Container?
+
+removeContainerOkButton=Remove this Container
+removeContainerButton2=Don't remove this Container
--- a/toolkit/components/contextualidentity/ContextualIdentityService.jsm
+++ b/toolkit/components/contextualidentity/ContextualIdentityService.jsm
@@ -278,41 +278,45 @@ function _ContextualIdentityService(path
       return;
     }
 
     let userContextId = tab.getAttribute("usercontextid");
     let identity = this.getIdentityFromId(userContextId);
     tab.setAttribute("data-identity-color", identity ? identity.color : "");
   },
 
-  countContainerTabs() {
+  countContainerTabs(userContextId = 0) {
     let count = 0;
-    this._forEachContainerTab(function() { ++count; });
+    this._forEachContainerTab(function() {
+      ++count;
+    }, userContextId);
     return count;
   },
 
-  closeAllContainerTabs() {
+  closeContainerTabs(userContextId = 0) {
     this._forEachContainerTab(function(tab, tabbrowser) {
       tabbrowser.removeTab(tab);
-    });
+    }, userContextId);
   },
 
-  _forEachContainerTab(callback) {
+  _forEachContainerTab(callback, userContextId = 0) {
     let windowList = Services.wm.getEnumerator("navigator:browser");
     while (windowList.hasMoreElements()) {
       let win = windowList.getNext();
 
       if (win.closed || !win.gBrowser) {
 	continue;
       }
 
       let tabbrowser = win.gBrowser;
       for (let i = tabbrowser.tabContainer.childNodes.length - 1; i >= 0; --i) {
         let tab = tabbrowser.tabContainer.childNodes[i];
-	if (tab.hasAttribute("usercontextid")) {
+	if (tab.hasAttribute("usercontextid") &&
+            (!userContextId ||
+             parseInt(tab.getAttribute("usercontextid"), 10) == userContextId)) {
 	  callback(tab, tabbrowser);
 	}
       }
     }
   },
 
   telemetry(userContextId) {
     let identity = this.getIdentityFromId(userContextId);