Bug 1605894 reduce the proliferation of DefaultLoopbackTone to only AudioStreamFlowin...
[gecko.git] / security / manager / ssl / ClientAuthDialogService.sys.mjs
blob7019dcc5cd74489f57a558f73e38ba43bfacb79b
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
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 // ClientAuthDialogService implements nsIClientAuthDialogService, and aims to
6 // open a dialog asking the user to select a client authentication certificate.
7 // Ideally the dialog will be tab-modal to the tab corresponding to the load
8 // that resulted in the request for the client authentication certificate.
9 export function ClientAuthDialogService() {}
11 // Given a loadContext (CanonicalBrowsingContext), attempts to return a
12 // TabDialogBox for the browser corresponding to loadContext.
13 function getTabDialogBoxForLoadContext(loadContext) {
14   let tabBrowser = loadContext?.topFrameElement?.getTabBrowser();
15   if (!tabBrowser) {
16     return null;
17   }
18   for (let browser of tabBrowser.browsers) {
19     if (browser.browserId == loadContext.top?.browserId) {
20       return tabBrowser.getTabDialogBox(browser);
21     }
22   }
23   return null;
26 ClientAuthDialogService.prototype = {
27   classID: Components.ID("{d7d2490d-2640-411b-9f09-a538803c11ee}"),
28   QueryInterface: ChromeUtils.generateQI(["nsIClientAuthDialogService"]),
30   chooseCertificate: function ClientAuthDialogService_chooseCertificate(
31     hostname,
32     certArray,
33     loadContext,
34     callback
35   ) {
36     const clientAuthAskURI = "chrome://pippki/content/clientauthask.xhtml";
37     let retVals = { cert: null, rememberDecision: false };
38     // First attempt to find a TabDialogBox for the loadContext. This allows
39     // for a tab-modal dialog specific to the tab causing the load, which is a
40     // better user experience.
41     let tabDialogBox = getTabDialogBoxForLoadContext(loadContext);
42     if (tabDialogBox) {
43       tabDialogBox
44         .open(clientAuthAskURI, {}, { hostname, certArray, retVals })
45         .closedPromise.then(() => {
46           callback.certificateChosen(retVals.cert, retVals.rememberDecision);
47         });
48       return;
49     }
50     // Otherwise, attempt to open a window-modal dialog on the window that at
51     // least has the tab the load is occurring in.
52     let browserWindow = loadContext?.topFrameElement?.ownerGlobal;
53     // Failing that, open a window-modal dialog on the most recent window.
54     if (!browserWindow) {
55       browserWindow = Services.wm.getMostRecentBrowserWindow();
56     }
57     if (browserWindow) {
58       browserWindow.gDialogBox
59         .open(clientAuthAskURI, { hostname, certArray, retVals })
60         .then(() => {
61           callback.certificateChosen(retVals.cert, retVals.rememberDecision);
62         });
63       return;
64     }
65     // Otherwise, continue the connection with no certificate.
66     callback.certificateChosen(null, false);
67   },