File: permissionDialog.js

package info (click to toggle)
firefox-esr 128.13.0esr-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,230,012 kB
  • sloc: cpp: 7,103,971; javascript: 6,088,450; ansic: 3,653,980; python: 1,212,330; xml: 594,604; asm: 420,652; java: 182,969; sh: 71,124; makefile: 20,747; perl: 13,449; objc: 12,399; yacc: 4,583; cs: 3,846; pascal: 2,973; lex: 1,720; ruby: 1,194; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10
file content (236 lines) | stat: -rw-r--r-- 7,557 bytes parent folder | download | duplicates (10)
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/* 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 { EnableDelayHelper } = ChromeUtils.importESModule(
  "resource://gre/modules/PromptUtils.sys.mjs"
);

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._addonPolicy =
      this._principal?.addonPolicy ?? this._principal?.contentScriptAddonPolicy;
    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");

    // allow the user to choose another application if they wish,
    // but don't offer this if the protocol was opened via
    // system principal (URLbar) and there's a preferred handler
    if (this._preferredHandlerName && !this._principal?.isSystemPrincipal) {
      changeAppLink.hidden = false;

      changeAppLink.addEventListener("click", () => this.onChangeApp());
    }
    document.addEventListener("dialogaccept", () => this.onAccept());
    this.initL10n();

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

  /**
   * We only show the website address if the origin is not user-readable
   * in the address bar.
   * @returns {boolean} - true if principal is top level and user-readable,
   *                      false otherwise.
   * If the triggering principal is null this method always returns false.
   */
  shouldShowPrincipal() {
    if (!this._principal) {
      return false;
    }

    let topContentPrincipal =
      this._browsingContext?.top.embedderElement?.contentPrincipal;

    let shownScheme = this._browsingContext.currentURI.scheme;
    return (
      !topContentPrincipal ||
      !topContentPrincipal.equals(this._principal) ||
      !["http", "https", "file"].includes(shownScheme)
    );
  },

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

    if (this._principal?.schemeIs("file") && !this.shouldShowPrincipal()) {
      if (this._preferredHandlerName) {
        return "permission-dialog-description-file-app";
      }
      return "permission-dialog-description-file";
    }

    if (this._principal?.isSystemPrincipal && this._preferredHandlerName) {
      return "permission-dialog-description-system-app";
    }

    if (this._principal?.isSystemPrincipal && !this._preferredHandlerName) {
      return "permission-dialog-description-system-noapp";
    }

    if (this.shouldShowPrincipal() && this.userReadablePrincipal) {
      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._addonPolicy) {
      return "permission-dialog-remember-extension";
    }
    if (this._principal.schemeIs("file")) {
      return "permission-dialog-remember-file";
    }
    return "permission-dialog-remember";
  },

  /**
   * Computes text to show in the prompt that is a user-understandable
   * version of what is asking to open the external protocol.
   * It's usually the prePath of the site that wants to navigate to
   * the external protocol, though we use the OS syntax for paths if
   * the request comes from `file://`.
   * @returns {string|null} - text to show, or null if we can't derive an
   * readable string from the triggering principal.
   */
  get userReadablePrincipal() {
    if (!this._principal) {
      return null;
    }

    // NullPrincipals don't expose a meaningful prePath. Instead use the
    // precursorPrincipal, which the NullPrincipal was derived from.
    let p = this._principal.isNullPrincipal
      ? this._principal.precursorPrincipal
      : this._principal;
    // Files have `file://` and nothing else as the prepath. Showing the
    // full path seems more useful for users:
    if (p?.schemeIs("file")) {
      try {
        let { file } = p.URI.QueryInterface(Ci.nsIFileURL);
        return file.path;
      } catch (_ex) {
        return p.spec;
      }
    }
    return p?.exposablePrePath;
  },

  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;
    let acceptButton = this._dialog.getButton("accept");

    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;
      acceptButton.addEventListener("click", () => this.onChangeApp());
    }
    document.l10n.setAttributes(acceptButton, idAcceptButton);

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

    let host = this.userReadablePrincipal;
    let scheme = this._handlerInfo.type;

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

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

  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,
});