summaryrefslogtreecommitdiffstats
path: root/security/manager/pki/resources/content/deletecert.js
blob: 784001ea2968ab8ca63fdb34fce9f5a1577cab63 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
/* 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.xul: a dialog that allows a
 *       user to confirm whether to delete certain certificates.
 * @argument {String} window.arguments[0]
 *           One of the tab IDs listed in certManager.xul.
 * @argument {nsICertTreeItem[]} window.arguments[1]
 *           An array of cert tree items representing the certs to delete.
 * @argument {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.
 */

const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components;

/**
 * Returns the most appropriate string to represent the given nsICertTreeItem.
 * @param {nsICertTreeItem} certTreeItem
 *        The item to represent.
 * @returns {String}
 *          A representative string.
 */
function certTreeItemToString(certTreeItem) {
  let cert = certTreeItem.cert;
  if (!cert) {
    return certTreeItem.hostPort;
  }

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

  let bundle = document.getElementById("pippki_bundle");
  return bundle.getFormattedString("certWithSerial", [cert.serialNumber]);
}

/**
 * onload() handler.
 */
function onLoad() {
  let typeFlag = window.arguments[0];
  let bundle = document.getElementById("pippki_bundle");
  let title;
  let confirm;
  let impact;

  switch (typeFlag) {
    case "mine_tab":
      title = bundle.getString("deleteUserCertTitle");
      confirm = bundle.getString("deleteUserCertConfirm");
      impact = bundle.getString("deleteUserCertImpact");
      break;
    case "websites_tab":
      title = bundle.getString("deleteSslCertTitle3");
      confirm = bundle.getString("deleteSslCertConfirm3");
      impact = bundle.getString("deleteSslCertImpact3");
      break;
    case "ca_tab":
      title = bundle.getString("deleteCaCertTitle2");
      confirm = bundle.getString("deleteCaCertConfirm2");
      impact = bundle.getString("deleteCaCertImpactX2");
      break;
    case "others_tab":
      title = bundle.getString("deleteEmailCertTitle");
      confirm = bundle.getString("deleteEmailCertConfirm");
      impact = bundle.getString("deleteEmailCertImpactDesc");
      break;
    case "orphan_tab":
      title = bundle.getString("deleteOrphanCertTitle");
      confirm = bundle.getString("deleteOrphanCertConfirm");
      impact = "";
      break;
    default:
      return;
  }

  document.title = title;

  setText("confirm", confirm);

  let box = document.getElementById("certlist");
  let certTreeItems = window.arguments[1];
  for (let certTreeItem of certTreeItems) {
    let listItem = document.createElement("richlistitem");
    let label = document.createElement("label");
    label.setAttribute("value", certTreeItemToString(certTreeItem));
    listItem.appendChild(label);
    box.appendChild(listItem);
  }

  setText("impact", impact);
}

/**
 * ondialogaccept() handler.
 *
 * @returns {Boolean} true to make the dialog close, false otherwise.
 */
function onDialogAccept() {
  let retVals = window.arguments[2];
  retVals.deleteConfirmed = true;
  return true;
}

/**
 * ondialogcancel() handler.
 *
 * @returns {Boolean} true to make the dialog close, false otherwise.
 */
function onDialogCancel() {
  let retVals = window.arguments[2];
  retVals.deleteConfirmed = false;
  return true;
}