deletecert.js (3390B)
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/. */ 4 5 "use strict"; 6 7 /** 8 * @file Implements the functionality of deletecert.xhtml: a dialog that allows a 9 * user to confirm whether to delete certain certificates. 10 * @param {string} window.arguments.0 11 * One of the tab IDs listed in certManager.xhtml. 12 * @param {object[]} window.arguments.1 13 * An array of objects representing the certs to delete. 14 * Each must have a 'cert' property or a 'hostPort' property. 15 * @param {DeleteCertReturnValues} window.arguments.2 16 * Object holding the return values of calling the dialog. 17 */ 18 19 /** 20 * @typedef DeleteCertReturnValues 21 * @type {object} 22 * @property {boolean} deleteConfirmed 23 * Set to true if the user confirmed deletion of the given certs, 24 * false otherwise. 25 */ 26 27 /** 28 * Returns the element to represent the given cert to delete. 29 * 30 * @param {object} certToDelete 31 * The item to represent. 32 * @returns {Element} 33 * A element of each cert tree item. 34 */ 35 function getLabelForCertToDelete(certToDelete) { 36 let element = document.createXULElement("label"); 37 let cert = certToDelete.cert; 38 if (!cert) { 39 element.setAttribute("value", certToDelete.hostPort); 40 return element; 41 } 42 43 const attributes = [ 44 cert.commonName, 45 cert.organizationalUnit, 46 cert.organization, 47 cert.subjectName, 48 ]; 49 for (let attribute of attributes) { 50 if (attribute) { 51 element.setAttribute("value", attribute); 52 return element; 53 } 54 } 55 56 document.l10n.setAttributes(element, "cert-with-serial", { 57 serialNumber: cert.serialNumber, 58 }); 59 return element; 60 } 61 62 /** 63 * onload() handler. 64 */ 65 function onLoad() { 66 let typeFlag = window.arguments[0]; 67 let confirm = document.getElementById("confirm"); 68 let impact = document.getElementById("impact"); 69 let prefixForType; 70 switch (typeFlag) { 71 case "mine_tab": 72 prefixForType = "delete-user-cert-"; 73 break; 74 case "websites_tab": 75 prefixForType = "delete-ssl-override-"; 76 break; 77 case "ca_tab": 78 prefixForType = "delete-ca-cert-"; 79 break; 80 case "others_tab": 81 prefixForType = "delete-email-cert-"; 82 break; 83 default: 84 return; 85 } 86 87 document.l10n.setAttributes( 88 document.documentElement, 89 prefixForType + "title" 90 ); 91 document.l10n.setAttributes(confirm, prefixForType + "confirm"); 92 document.l10n.setAttributes(impact, prefixForType + "impact"); 93 94 document.addEventListener("dialogaccept", onDialogAccept); 95 document.addEventListener("dialogcancel", onDialogCancel); 96 97 let box = document.getElementById("certlist"); 98 let certsToDelete = window.arguments[1]; 99 for (let certToDelete of certsToDelete) { 100 let listItem = document.createXULElement("richlistitem"); 101 let label = getLabelForCertToDelete(certToDelete); 102 listItem.appendChild(label); 103 box.appendChild(listItem); 104 } 105 } 106 107 /** 108 * ondialogaccept() handler. 109 */ 110 function onDialogAccept() { 111 let retVals = window.arguments[2]; 112 retVals.deleteConfirmed = true; 113 } 114 115 /** 116 * ondialogcancel() handler. 117 */ 118 function onDialogCancel() { 119 let retVals = window.arguments[2]; 120 retVals.deleteConfirmed = false; 121 } 122 123 window.addEventListener("load", () => onLoad());