Backed out changeset 496886cb30a5 (bug 1867152) for bc failures on browser_user_input...
[gecko.git] / browser / modules / OpenInTabsUtils.sys.mjs
blobbea547d91520c616af9bd9c14782b83dce2a9c9b
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3  * You can obtain one at http://mozilla.org/MPL/2.0/. */
5 const lazy = {};
7 ChromeUtils.defineLazyGetter(lazy, "l10n", () => {
8   return new Localization(
9     ["browser/tabbrowser.ftl", "branding/brand.ftl"],
10     true
11   );
12 });
14 /**
15  * Utility functions that can be used when opening multiple tabs, that can be
16  * called without any tabbrowser instance.
17  */
18 export const OpenInTabsUtils = {
19   /**
20    * Gives the user a chance to cancel loading lots of tabs at once.
21    */
22   confirmOpenInTabs(numTabsToOpen, aWindow) {
23     const WARN_ON_OPEN_PREF = "browser.tabs.warnOnOpen";
24     const MAX_OPNE_PREF = "browser.tabs.maxOpenBeforeWarn";
25     if (!Services.prefs.getBoolPref(WARN_ON_OPEN_PREF)) {
26       return true;
27     }
28     if (numTabsToOpen < Services.prefs.getIntPref(MAX_OPNE_PREF)) {
29       return true;
30     }
32     // default to true: if it were false, we wouldn't get this far
33     let warnOnOpen = { value: true };
35     const [title, message, button, checkbox] = lazy.l10n.formatMessagesSync([
36       { id: "tabbrowser-confirm-open-multiple-tabs-title" },
37       {
38         id: "tabbrowser-confirm-open-multiple-tabs-message",
39         args: { tabCount: numTabsToOpen },
40       },
41       { id: "tabbrowser-confirm-open-multiple-tabs-button" },
42       { id: "tabbrowser-confirm-open-multiple-tabs-checkbox" },
43     ]);
45     let buttonPressed = Services.prompt.confirmEx(
46       aWindow,
47       title.value,
48       message.value,
49       Services.prompt.BUTTON_TITLE_IS_STRING * Services.prompt.BUTTON_POS_0 +
50         Services.prompt.BUTTON_TITLE_CANCEL * Services.prompt.BUTTON_POS_1,
51       button.value,
52       null,
53       null,
54       checkbox.value,
55       warnOnOpen
56     );
58     let reallyOpen = buttonPressed == 0;
59     // don't set the pref unless they press OK and it's false
60     if (reallyOpen && !warnOnOpen.value) {
61       Services.prefs.setBoolPref(WARN_ON_OPEN_PREF, false);
62     }
64     return reallyOpen;
65   },
67   /*
68    * Async version of confirmOpenInTabs.
69    */
70   promiseConfirmOpenInTabs(numTabsToOpen, aWindow) {
71     return new Promise(resolve => {
72       Services.tm.dispatchToMainThread(() => {
73         resolve(this.confirmOpenInTabs(numTabsToOpen, aWindow));
74       });
75     });
76   },