Bug 1456589 - Part 5 Port Bug 1435910 Part 0.1 [Move PlacesUIUtils.confirmOpenInTabs into new OpenInTabsUtils.jsm and add promiseConfirmOpenInTabs]. r=IanN
authorFrank-Rainer Grahl <frgrahl@gmx.net>
Mon, 23 Jul 2018 22:06:15 +0200
changeset 24335 5515f2dd2916d6faf1276c55f0958415bcabb501
parent 24334 8ca24e8c7a6b342d4063c68dc86f0801e1527c9a
child 24336 97c3890193980546a28a5f638c6955c39e257e1b
push id14652
push userfrgrahl@gmx.net
push dateMon, 23 Jul 2018 20:17:58 +0000
treeherdercomm-central@07e930aae6f2 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersIanN
bugs1456589, 1435910
Bug 1456589 - Part 5 Port Bug 1435910 Part 0.1 [Move PlacesUIUtils.confirmOpenInTabs into new OpenInTabsUtils.jsm and add promiseConfirmOpenInTabs]. r=IanN
suite/components/places/PlacesUIUtils.jsm
suite/locales/en-US/chrome/browser/tabbrowser.properties
suite/locales/en-US/chrome/common/places/places.properties
suite/modules/OpenInTabsUtils.jsm
suite/modules/moz.build
--- a/suite/components/places/PlacesUIUtils.jsm
+++ b/suite/components/places/PlacesUIUtils.jsm
@@ -7,16 +7,17 @@ var EXPORTED_SYMBOLS = ["PlacesUIUtils"]
 
 ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
 ChromeUtils.import("resource://gre/modules/Services.jsm");
 ChromeUtils.import("resource://gre/modules/Timer.jsm");
 
 Cu.importGlobalProperties(["Element"]);
 
 XPCOMUtils.defineLazyModuleGetters(this, {
+  OpenInTabsUtils: "resource:///modules/OpenInTabsUtils.jsm",
   PlacesUtils: "resource://gre/modules/PlacesUtils.jsm",
   PluralForm: "resource://gre/modules/PluralForm.jsm",
   PrivateBrowsingUtils: "resource://gre/modules/PrivateBrowsingUtils.jsm",
   RecentWindow: "resource:///modules/RecentWindow.jsm",
   PromiseUtils: "resource://gre/modules/PromiseUtils.jsm",
   PlacesTransactions: "resource://gre/modules/PlacesTransactions.jsm",
 });
 
@@ -586,57 +587,16 @@ var PlacesUIUtils = {
     // because the only way to access the left pane contents goes through
     // "resolving" the leftPaneFolderId getter.
     if (typeof Object.getOwnPropertyDescriptor(this, "leftPaneFolderId").get == "function") {
       return false;
     }
     return itemId == this.leftPaneFolderId;
   },
 
-  /**
-   * Gives the user a chance to cancel loading lots of tabs at once
-   */
-  confirmOpenInTabs(numTabsToOpen, aWindow) {
-    const WARN_ON_OPEN_PREF = "browser.tabs.warnOnOpen";
-    var reallyOpen = true;
-
-    if (Services.prefs.getBoolPref(WARN_ON_OPEN_PREF)) {
-      if (numTabsToOpen >= Services.prefs.getIntPref("browser.tabs.maxOpenBeforeWarn")) {
-        // default to true: if it were false, we wouldn't get this far
-        var warnOnOpen = { value: true };
-
-        var messageKey = "tabs.openWarningMultipleBranded";
-        var openKey = "tabs.openButtonMultiple";
-        const BRANDING_BUNDLE_URI = "chrome://branding/locale/brand.properties";
-        var brandShortName = Services.strings
-                                     .createBundle(BRANDING_BUNDLE_URI)
-                                     .GetStringFromName("brandShortName");
-
-        var buttonPressed = Services.prompt.confirmEx(
-          aWindow,
-          this.getString("tabs.openWarningTitle"),
-          this.getFormattedString(messageKey, [numTabsToOpen, brandShortName]),
-          (Services.prompt.BUTTON_TITLE_IS_STRING * Services.prompt.BUTTON_POS_0) +
-            (Services.prompt.BUTTON_TITLE_CANCEL * Services.prompt.BUTTON_POS_1),
-          this.getString(openKey), null, null,
-          this.getFormattedString("tabs.openWarningPromptMeBranded",
-                                  [brandShortName]),
-          warnOnOpen
-        );
-
-        reallyOpen = (buttonPressed == 0);
-        // don't set the pref unless they press OK and it's false
-        if (reallyOpen && !warnOnOpen.value)
-          Services.prefs.setBoolPref(WARN_ON_OPEN_PREF, false);
-      }
-    }
-
-    return reallyOpen;
-  },
-
   /** aItemsToOpen needs to be an array of objects of the form:
     * {uri: string, isBookmark: boolean}
     */
   _openTabset: function PUIU__openTabset(aItemsToOpen, aEvent, aWindow) {
     if (!aItemsToOpen.length)
       return;
 
     // Prefer the caller window if it's a browser window, otherwise use
@@ -695,28 +655,28 @@ var PlacesUIUtils = {
       .then(aLivemark => {
         let urlsToOpen = [];
 
         let nodes = aLivemark.getNodesForContainer(aNode);
         for (let node of nodes) {
           urlsToOpen.push({uri: node.uri, isBookmark: false});
         }
 
-        if (this.confirmOpenInTabs(urlsToOpen.length, window)) {
+        if (OpenInTabsUtils.confirmOpenInTabs(urlsToOpen.length, window)) {
           this._openTabset(urlsToOpen, aEvent, window);
         }
       }, Cu.reportError);
   },
 
   openContainerNodeInTabs:
   function PUIU_openContainerInTabs(aNode, aEvent, aView) {
     let window = aView.ownerWindow;
 
     let urlsToOpen = PlacesUtils.getURLsForContainerNode(aNode);
-    if (this.confirmOpenInTabs(urlsToOpen.length, window)) {
+    if (OpenInTabsUtils.confirmOpenInTabs(urlsToOpen.length, window)) {
       this._openTabset(urlsToOpen, aEvent, window);
     }
   },
 
   openURINodesInTabs: function PUIU_openURINodesInTabs(aNodes, aEvent, aView) {
     let window = aView.ownerWindow;
 
     let urlsToOpen = [];
--- a/suite/locales/en-US/chrome/browser/tabbrowser.properties
+++ b/suite/locales/en-US/chrome/browser/tabbrowser.properties
@@ -3,8 +3,16 @@
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 tabs.loading=Loading…
 tabs.untitled=Untitled
 tabs.closeWarningTitle=Confirm Closing Other Tabs
 tabs.closeWarning=You are about to close %S other tab(s). Are you sure you want to continue?
 tabs.closeButton=Close other tabs
 tabs.closeWarningPromptMe=Warn me when I attempt to close other tabs
+
+# LOCALIZATION NOTE (tabs.openWarningTitle, tabs.openWarningMultipleBranded, tabs.openButtonMultiple, tabs.openWarningPromptMeBranded):
+# These items were moved from /chrome/common/places/places.properties.
+# Now they are not specific to bookmarks.
+tabs.openWarningTitle=Confirm open
+tabs.openWarningMultipleBranded=You are about to open %S tabs.  This might slow down %S while the pages are loading.  Are you sure you want to continue?
+tabs.openButtonMultiple=Open tabs
+tabs.openWarningPromptMeBranded=Warn me when opening multiple tabs might slow down %S
--- a/suite/locales/en-US/chrome/common/places/places.properties
+++ b/suite/locales/en-US/chrome/common/places/places.properties
@@ -43,21 +43,16 @@ view.sortBy.1.dateAdded.accesskey=e
 view.sortBy.1.lastModified.label=Sort by Last Modified
 view.sortBy.1.lastModified.accesskey=M
 view.sortBy.1.tags.label=Sort by Tags
 view.sortBy.1.tags.accesskey=T
 
 searchBookmarks=Search Bookmarks
 searchHistory=Search History
 
-tabs.openWarningTitle=Confirm open
-tabs.openWarningMultipleBranded=You are about to open %S tabs.  This might slow down %S while the pages are loading.  Are you sure you want to continue?
-tabs.openButtonMultiple=Open tabs
-tabs.openWarningPromptMeBranded=Warn me when opening multiple tabs might slow down %S
-
 SelectImport=Import Bookmarks File
 EnterExport=Export Bookmarks File
 
 detailsPane.noItems=No items
 # LOCALIZATION NOTE (detailsPane.itemsCountLabel): Semicolon-separated list of plural forms.
 # See: http://developer.mozilla.org/en/docs/Localization_and_Plurals
 # #1 number of items
 # example: 111 items
new file mode 100644
--- /dev/null
+++ b/suite/modules/OpenInTabsUtils.jsm
@@ -0,0 +1,83 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this file,
+ * You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+var EXPORTED_SYMBOLS = ["OpenInTabsUtils"];
+
+ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
+ChromeUtils.import("resource://gre/modules/Services.jsm");
+
+XPCOMUtils.defineLazyGetter(this, "bundle", function() {
+  return Services.strings.createBundle("chrome://navigator/locale/tabbrowser.properties");
+});
+
+/**
+ * Utility functions that can be used when opening multiple tabs, that can be
+ * called without any tabbrowser instance.
+ */
+var OpenInTabsUtils = {
+  getString(key) {
+    return bundle.GetStringFromName(key);
+  },
+
+  getFormattedString(key, params) {
+    return bundle.formatStringFromName(key, params, params.length);
+  },
+
+  /**
+   * Gives the user a chance to cancel loading lots of tabs at once.
+   */
+  confirmOpenInTabs(numTabsToOpen, aWindow) {
+    const WARN_ON_OPEN_PREF = "browser.tabs.warnOnOpen";
+    const MAX_OPNE_PREF = "browser.tabs.maxOpenBeforeWarn";
+    if (!Services.prefs.getBoolPref(WARN_ON_OPEN_PREF)) {
+      return true;
+    }
+    if (numTabsToOpen < Services.prefs.getIntPref(MAX_OPNE_PREF)) {
+      return true;
+    }
+
+    // default to true: if it were false, we wouldn't get this far
+    let warnOnOpen = { value: true };
+
+    const messageKey = "tabs.openWarningMultipleBranded";
+    const openKey = "tabs.openButtonMultiple";
+    const BRANDING_BUNDLE_URI = "chrome://branding/locale/brand.properties";
+    let brandShortName = Services.strings
+                                 .createBundle(BRANDING_BUNDLE_URI)
+                                 .GetStringFromName("brandShortName");
+
+    let buttonPressed = Services.prompt.confirmEx(
+      aWindow,
+      this.getString("tabs.openWarningTitle"),
+      this.getFormattedString(messageKey, [numTabsToOpen, brandShortName]),
+      (Services.prompt.BUTTON_TITLE_IS_STRING * Services.prompt.BUTTON_POS_0) +
+        (Services.prompt.BUTTON_TITLE_CANCEL * Services.prompt.BUTTON_POS_1),
+      this.getString(openKey), null, null,
+      this.getFormattedString("tabs.openWarningPromptMeBranded",
+                              [brandShortName]),
+      warnOnOpen
+    );
+
+    let reallyOpen = (buttonPressed == 0);
+    // don't set the pref unless they press OK and it's false
+    if (reallyOpen && !warnOnOpen.value) {
+      Services.prefs.setBoolPref(WARN_ON_OPEN_PREF, false);
+    }
+
+    return reallyOpen;
+  },
+
+  /*
+   * Async version of confirmOpenInTabs.
+   */
+  promiseConfirmOpenInTabs(numTabsToOpen, aWindow) {
+    return new Promise(resolve => {
+      Services.tm.dispatchToMainThread(() => {
+        resolve(this.confirmOpenInTabs(numTabsToOpen, aWindow));
+      });
+    });
+  }
+};
--- a/suite/modules/moz.build
+++ b/suite/modules/moz.build
@@ -1,16 +1,17 @@
 # vim: set filetype=python:
 # This Source Code Form is subject to the terms of the Mozilla Public
 # License, v. 2.0. If a copy of the MPL was not distributed with this
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 XPCSHELL_TESTS_MANIFESTS += ['test/unit/xpcshell.ini']
 
 EXTRA_JS_MODULES += [
+    'OpenInTabsUtils.jsm',
     'RecentWindow.jsm',
     'Sanitizer.jsm',
     'WindowsPreviewPerTab.jsm',
 ]
 
 if CONFIG['OS_ARCH'] == 'WINNT':
     EXTRA_JS_MODULES += ['WindowsJumpLists.jsm']