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();
18 for (let browser of tabBrowser.browsers) {
19 if (browser.browserId == loadContext.top?.browserId) {
20 return tabBrowser.getTabDialogBox(browser);
26 ClientAuthDialogService.prototype = {
27 classID: Components.ID("{d7d2490d-2640-411b-9f09-a538803c11ee}"),
28 QueryInterface: ChromeUtils.generateQI(["nsIClientAuthDialogService"]),
30 chooseCertificate: function ClientAuthDialogService_chooseCertificate(
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);
44 .open(clientAuthAskURI, {}, { hostname, certArray, retVals })
45 .closedPromise.then(() => {
46 callback.certificateChosen(retVals.cert, retVals.rememberDecision);
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.
55 browserWindow = Services.wm.getMostRecentBrowserWindow();
58 browserWindow.gDialogBox
59 .open(clientAuthAskURI, { hostname, certArray, retVals })
61 callback.certificateChosen(retVals.cert, retVals.rememberDecision);
65 // Otherwise, continue the connection with no certificate.
66 callback.certificateChosen(null, false);