summaryrefslogtreecommitdiffstats
path: root/security/manager/pki/resources/content/deletecert.js
blob: 7d926863edcfb21fb984adf0f496f95e8ff8f3e3 (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
/* 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/. */
/* import-globals-from pippki.js */
"use strict";

/**
 * @file Implements the functionality of deletecert.xhtml: a dialog that allows a
 *       user to confirm whether to delete certain certificates.
 * @param {string} window.arguments.0
 *           One of the tab IDs listed in certManager.xhtml.
 * @param {object[]} window.arguments.1
 *           An array of objects representing the certs to delete.
 *           Each must have a 'cert' property or a 'hostPort' property.
 * @param {DeleteCertReturnValues} window.arguments.2
 *           Object holding the return values of calling the dialog.
 */

/**
 * @typedef DeleteCertReturnValues
 * @type {object}
 * @property {boolean} deleteConfirmed
 *           Set to true if the user confirmed deletion of the given certs,
 *           false otherwise.
 */

/**
 * Returns the element to represent the given cert to delete.
 *
 * @param {object} certToDelete
 *        The item to represent.
 * @returns {Element}
 *          A element of each cert tree item.
 */
function getLabelForCertToDelete(certToDelete) {
  let element = document.createXULElement("label");
  let cert = certToDelete.cert;
  if (!cert) {
    element.setAttribute("value", certToDelete.hostPort);
    return element;
  }

  const attributes = [
    cert.commonName,
    cert.organizationalUnit,
    cert.organization,
    cert.subjectName,
  ];
  for (let attribute of attributes) {
    if (attribute) {
      element.setAttribute("value", attribute);
      return element;
    }
  }

  document.l10n.setAttributes(element, "cert-with-serial", {
    serialNumber: cert.serialNumber,
  });
  return element;
}

/**
 * onload() handler.
 */
function onLoad() {
  let typeFlag = window.arguments[0];
  let confirm = document.getElementById("confirm");
  let impact = document.getElementById("impact");
  let prefixForType;
  switch (typeFlag) {
    case "mine_tab":
      prefixForType = "delete-user-cert-";
      break;
    case "websites_tab":
      prefixForType = "delete-ssl-override-";
      break;
    case "ca_tab":
      prefixForType = "delete-ca-cert-";
      break;
    case "others_tab":
      prefixForType = "delete-email-cert-";
      break;
    default:
      return;
  }

  document.l10n.setAttributes(
    document.documentElement,
    prefixForType + "title"
  );
  document.l10n.setAttributes(confirm, prefixForType + "confirm");
  document.l10n.setAttributes(impact, prefixForType + "impact");

  document.addEventListener("dialogaccept", onDialogAccept);
  document.addEventListener("dialogcancel", onDialogCancel);

  let box = document.getElementById("certlist");
  let certsToDelete = window.arguments[1];
  for (let certToDelete of certsToDelete) {
    let listItem = document.createXULElement("richlistitem");
    let label = getLabelForCertToDelete(certToDelete);
    listItem.appendChild(label);
    box.appendChild(listItem);
  }
}

/**
 * ondialogaccept() handler.
 */
function onDialogAccept() {
  let retVals = window.arguments[2];
  retVals.deleteConfirmed = true;
}

/**
 * ondialogcancel() handler.
 */
function onDialogCancel() {
  let retVals = window.arguments[2];
  retVals.deleteConfirmed = false;
}