summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/handling/content/permissionDialog.js
blob: 336852cb3dae59838a71ea7cce23bc2e1b5a1ffd (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/* 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/. */

const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
const { EnableDelayHelper } = ChromeUtils.import(
  "resource://gre/modules/SharedPromptUtils.jsm"
);

let dialog = {
  /**
   * This function initializes the content of the dialog.
   */
  initialize() {
    let args = window.arguments[0].wrappedJSObject || window.arguments[0];
    let {
      handler,
      principal,
      outArgs,
      canPersistPermission,
      preferredHandlerName,
      browsingContext,
    } = args;

    this._handlerInfo = handler.QueryInterface(Ci.nsIHandlerInfo);
    this._principal = principal?.QueryInterface(Ci.nsIPrincipal);
    this._browsingContext = browsingContext;
    this._outArgs = outArgs.QueryInterface(Ci.nsIWritablePropertyBag);
    this._preferredHandlerName = preferredHandlerName;

    this._dialog = document.querySelector("dialog");
    this._checkRemember = document.getElementById("remember");
    this._checkRememberContainer = document.getElementById("rememberContainer");

    if (!canPersistPermission) {
      this._checkRememberContainer.hidden = true;
    }

    let changeAppLink = document.getElementById("change-app");
    if (this._preferredHandlerName) {
      changeAppLink.hidden = false;

      changeAppLink.addEventListener("click", () => this.onChangeApp());
    }

    document.addEventListener("dialogaccept", () => this.onAccept());
    document.mozSubdialogReady = this.initL10n().then(() => {
      window.sizeToContent();
    });

    this._delayHelper = new EnableDelayHelper({
      disableDialog: () => {
        this._dialog.setAttribute("buttondisabledaccept", true);
      },
      enableDialog: () => {
        this._dialog.setAttribute("buttondisabledaccept", false);
      },
      focusTarget: window,
    });
  },

  /**
   * Checks whether the principal that triggered this dialog is top level
   * (not embedded in a frame).
   * @returns {boolean} - true if principal is top level, false otherwise.
   * If the triggering principal is null this method always returns false.
   */
  triggeringPrincipalIsTop() {
    let topContentPrincipal = this._browsingContext?.top.embedderElement
      ?.contentPrincipal;
    if (!topContentPrincipal) {
      return false;
    }
    return this._principal.equals(topContentPrincipal);
  },

  /**
   * Determines the l10n ID to use for the dialog description, depending on
   * the triggering principal and the preferred application handler.
   */
  get l10nDescriptionId() {
    if (this._principal?.schemeIs("file")) {
      if (this._preferredHandlerName) {
        return "permission-dialog-description-file-app";
      }
      return "permission-dialog-description-file";
    }

    // We only show the website address if the request didn't come from the top
    // level frame.
    if (this._principal?.exposablePrePath && !this.triggeringPrincipalIsTop()) {
      if (this._preferredHandlerName) {
        return "permission-dialog-description-host-app";
      }
      return "permission-dialog-description-host";
    }

    if (this._preferredHandlerName) {
      return "permission-dialog-description-app";
    }

    return "permission-dialog-description";
  },

  /**
   * Determines the l10n ID to use for the "remember permission" checkbox,
   * depending on the triggering principal and the preferred application
   * handler.
   */
  get l10nCheckboxId() {
    if (!this._principal) {
      return null;
    }

    if (this._principal.schemeIs("file")) {
      return "permission-dialog-remember-file";
    }
    return "permission-dialog-remember";
  },

  async initL10n() {
    // The UI labels depend on whether we will show the application chooser next
    // or directly open the assigned protocol handler.

    // Fluent id for dialog accept button
    let idAcceptButton;
    if (this._preferredHandlerName) {
      idAcceptButton = "permission-dialog-btn-open-link";
    } else {
      idAcceptButton = "permission-dialog-btn-choose-app";

      let descriptionExtra = document.getElementById("description-extra");
      descriptionExtra.hidden = false;
    }

    let description = document.getElementById("description");

    document.l10n.pauseObserving();
    let pendingElements = [description];

    let host = this._principal?.exposablePrePath;
    let scheme = this._handlerInfo.type;

    document.l10n.setAttributes(description, this.l10nDescriptionId, {
      host,
      scheme,
      appName: this._preferredHandlerName,
    });

    if (!this._checkRememberContainer.hidden) {
      let checkboxLabel = document.getElementById("remember-label");
      document.l10n.setAttributes(checkboxLabel, this.l10nCheckboxId, {
        host,
        scheme,
      });
      pendingElements.push(checkboxLabel);
    }

    // Set the dialog button labels.
    // Ideally we would do this via attributes, however the <dialog> element
    // does not support changing l10n ids on the fly.
    let acceptButton = this._dialog.getButton("accept");
    let [result] = await document.l10n.formatMessages([{ id: idAcceptButton }]);
    result.attributes.forEach(attr => {
      if (attr.name == "label") {
        acceptButton.label = attr.value;
      } else {
        acceptButton.accessKey = attr.value;
      }
    });

    document.l10n.resumeObserving();

    await document.l10n.translateElements(pendingElements);
    return document.l10n.ready;
  },

  onAccept() {
    this._outArgs.setProperty("remember", this._checkRemember.checked);
    this._outArgs.setProperty("granted", true);
  },

  onChangeApp() {
    this._outArgs.setProperty("resetHandlerChoice", true);

    // We can't call the dialogs accept handler here. If the accept button is
    // still disabled, it will prevent closing.
    this.onAccept();
    window.close();
  },
};

window.addEventListener("DOMContentLoaded", () => dialog.initialize(), {
  once: true,
});