summaryrefslogtreecommitdiffstats
path: root/comm/mail/components/prompts/PromptCollection.jsm
blob: ddb413de6f444884925417e14765a863bf834e8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/* 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 = ["PromptCollection"];

const { XPCOMUtils } = ChromeUtils.importESModule(
  "resource://gre/modules/XPCOMUtils.sys.mjs"
);

/**
 * Implements nsIPromptCollection
 *
 * @class PromptCollection
 */
class PromptCollection {
  asyncBeforeUnloadCheck(browsingContext) {
    let title;
    let message;
    let leaveLabel;
    let stayLabel;

    try {
      title = this.domBundle.GetStringFromName("OnBeforeUnloadTitle");
      message = this.domBundle.GetStringFromName("OnBeforeUnloadMessage2");
      leaveLabel = this.domBundle.GetStringFromName(
        "OnBeforeUnloadLeaveButton"
      );
      stayLabel = this.domBundle.GetStringFromName("OnBeforeUnloadStayButton");
    } catch (exception) {
      console.error("Failed to get strings from dom.properties");
      return false;
    }

    let contentViewer = browsingContext?.docShell?.contentViewer;

    // TODO: Do we really want to allow modal dialogs from inactive
    // content viewers at all, particularly for permit unload prompts?
    let modalAllowed = contentViewer
      ? contentViewer.isTabModalPromptAllowed
      : browsingContext.ancestorsAreCurrent;

    let modalType =
      Ci.nsIPromptService[
        modalAllowed ? "MODAL_TYPE_CONTENT" : "MODAL_TYPE_WINDOW"
      ];

    let buttonFlags =
      Ci.nsIPromptService.BUTTON_POS_0_DEFAULT |
      (Ci.nsIPromptService.BUTTON_TITLE_IS_STRING *
        Ci.nsIPromptService.BUTTON_POS_0) |
      (Ci.nsIPromptService.BUTTON_TITLE_IS_STRING *
        Ci.nsIPromptService.BUTTON_POS_1);

    return Services.prompt
      .asyncConfirmEx(
        browsingContext,
        modalType,
        title,
        message,
        buttonFlags,
        leaveLabel,
        stayLabel,
        null,
        null,
        false,
        // Tell the prompt service that this is a permit unload prompt
        // so that it can set the appropriate flag on the detail object
        // of the events it dispatches.
        { inPermitUnload: true }
      )
      .then(
        result =>
          result.QueryInterface(Ci.nsIPropertyBag2).get("buttonNumClicked") == 0
      );
  }
}

XPCOMUtils.defineLazyGetter(
  PromptCollection.prototype,
  "domBundle",
  function () {
    let bundle = Services.strings.createBundle(
      "chrome://global/locale/dom/dom.properties"
    );
    if (!bundle) {
      throw new Error("String bundle for dom not present!");
    }
    return bundle;
  }
);

PromptCollection.prototype.classID = Components.ID(
  "{7913837c-9623-11ea-bb37-0242ac130002}"
);
PromptCollection.prototype.QueryInterface = ChromeUtils.generateQI([
  "nsIPromptCollection",
]);