summaryrefslogtreecommitdiffstats
path: root/security/manager/pki/resources/content/load_device.js
blob: ac122851e0ab01acc7bf175cbcbf6aa4695ec2ec (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
/* 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";

document.addEventListener("dialogaccept", onDialogAccept);

/**
 * @file Implements the functionality of load_device.xhtml: a dialog that allows
 *       a PKCS #11 module to be loaded into Firefox.
 */

async function onBrowseBtnPress() {
  let fp = Cc["@mozilla.org/filepicker;1"].createInstance(Ci.nsIFilePicker);
  let [loadPK11ModuleFilePickerTitle] = await document.l10n.formatValues([
    { id: "load-pk11-module-file-picker-title" },
  ]);
  fp.init(
    window.browsingContext,
    loadPK11ModuleFilePickerTitle,
    Ci.nsIFilePicker.modeOpen
  );
  fp.appendFilters(Ci.nsIFilePicker.filterAll);
  fp.open(rv => {
    if (rv == Ci.nsIFilePicker.returnOK) {
      document.getElementById("device_path").value = fp.file.path;
    }

    // This notification gets sent solely for test purposes. It should not be
    // used by production code.
    Services.obs.notifyObservers(window, "LoadPKCS11Module:FilePickHandled");
  });
}

/**
 * ondialogaccept() handler.
 *
 * @param {object} event
 *        The event causing this handler function to be called.
 */
function onDialogAccept(event) {
  let nameBox = document.getElementById("device_name");
  let pathBox = document.getElementById("device_path");
  let pkcs11ModuleDB = Cc["@mozilla.org/security/pkcs11moduledb;1"].getService(
    Ci.nsIPKCS11ModuleDB
  );

  try {
    pkcs11ModuleDB.addModule(nameBox.value, pathBox.value, 0, 0);
  } catch (e) {
    addModuleFailure("add-module-failure");
    event.preventDefault();
  }
}

async function addModuleFailure(l10nID) {
  let [AddModuleFailure] = await document.l10n.formatValues([{ id: l10nID }]);
  alertPromptService(null, AddModuleFailure);
}

function validateModuleName() {
  let name = document.getElementById("device_name").value;
  let helpText = document.getElementById("helpText");
  helpText.value = "";
  let dialogNode = document.querySelector("dialog");
  dialogNode.removeAttribute("buttondisabledaccept");
  if (name == "") {
    document.l10n.setAttributes(helpText, "load-module-help-empty-module-name");
    dialogNode.setAttribute("buttondisabledaccept", true);
  }
  if (name == "Root Certs") {
    document.l10n.setAttributes(
      helpText,
      "load-module-help-root-certs-module-name"
    );
    dialogNode.setAttribute("buttondisabledaccept", true);
  }
}