Bug 1675375 Part 7: Update expectations in helper_hittest_clippath.html. r=botond
[gecko.git] / browser / modules / OpenInTabsUtils.jsm
bloba299d9e0d848fb6bc38c3a30c00dd9490459d8be
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 "use strict";
7 this.EXPORTED_SYMBOLS = ["OpenInTabsUtils"];
9 const { XPCOMUtils } = ChromeUtils.import(
10   "resource://gre/modules/XPCOMUtils.jsm"
12 const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
14 XPCOMUtils.defineLazyGetter(this, "bundle", function() {
15   return Services.strings.createBundle(
16     "chrome://browser/locale/tabbrowser.properties"
17   );
18 });
20 /**
21  * Utility functions that can be used when opening multiple tabs, that can be
22  * called without any tabbrowser instance.
23  */
24 this.OpenInTabsUtils = {
25   getString(key) {
26     return bundle.GetStringFromName(key);
27   },
29   getFormattedString(key, params) {
30     return bundle.formatStringFromName(key, params);
31   },
33   /**
34    * Gives the user a chance to cancel loading lots of tabs at once.
35    */
36   confirmOpenInTabs(numTabsToOpen, aWindow) {
37     const WARN_ON_OPEN_PREF = "browser.tabs.warnOnOpen";
38     const MAX_OPNE_PREF = "browser.tabs.maxOpenBeforeWarn";
39     if (!Services.prefs.getBoolPref(WARN_ON_OPEN_PREF)) {
40       return true;
41     }
42     if (numTabsToOpen < Services.prefs.getIntPref(MAX_OPNE_PREF)) {
43       return true;
44     }
46     // default to true: if it were false, we wouldn't get this far
47     let warnOnOpen = { value: true };
49     const messageKey = "tabs.openWarningMultipleBranded";
50     const openKey = "tabs.openButtonMultiple";
51     const BRANDING_BUNDLE_URI = "chrome://branding/locale/brand.properties";
52     let brandShortName = Services.strings
53       .createBundle(BRANDING_BUNDLE_URI)
54       .GetStringFromName("brandShortName");
56     let buttonPressed = Services.prompt.confirmEx(
57       aWindow,
58       this.getString("tabs.openWarningTitle"),
59       this.getFormattedString(messageKey, [numTabsToOpen, brandShortName]),
60       Services.prompt.BUTTON_TITLE_IS_STRING * Services.prompt.BUTTON_POS_0 +
61         Services.prompt.BUTTON_TITLE_CANCEL * Services.prompt.BUTTON_POS_1,
62       this.getString(openKey),
63       null,
64       null,
65       this.getFormattedString("tabs.openWarningPromptMeBranded", [
66         brandShortName,
67       ]),
68       warnOnOpen
69     );
71     let reallyOpen = buttonPressed == 0;
72     // don't set the pref unless they press OK and it's false
73     if (reallyOpen && !warnOnOpen.value) {
74       Services.prefs.setBoolPref(WARN_ON_OPEN_PREF, false);
75     }
77     return reallyOpen;
78   },
80   /*
81    * Async version of confirmOpenInTabs.
82    */
83   promiseConfirmOpenInTabs(numTabsToOpen, aWindow) {
84     return new Promise(resolve => {
85       Services.tm.dispatchToMainThread(() => {
86         resolve(this.confirmOpenInTabs(numTabsToOpen, aWindow));
87       });
88     });
89   },