load_device.js (2890B)
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 const { alertPromptService } = ChromeUtils.importESModule( 8 "resource://gre/modules/psm/pippki.sys.mjs" 9 ); 10 11 document.addEventListener("dialogaccept", onDialogAccept); 12 13 /** 14 * @file Implements the functionality of load_device.xhtml: a dialog that allows 15 * a PKCS #11 module to be loaded into Firefox. 16 */ 17 18 async function onBrowseBtnPress() { 19 let fp = Cc["@mozilla.org/filepicker;1"].createInstance(Ci.nsIFilePicker); 20 let [loadPK11ModuleFilePickerTitle] = await document.l10n.formatValues([ 21 { id: "load-pk11-module-file-picker-title" }, 22 ]); 23 fp.init( 24 window.browsingContext, 25 loadPK11ModuleFilePickerTitle, 26 Ci.nsIFilePicker.modeOpen 27 ); 28 fp.appendFilters(Ci.nsIFilePicker.filterAll); 29 fp.open(rv => { 30 if (rv == Ci.nsIFilePicker.returnOK) { 31 document.getElementById("device_path").value = fp.file.path; 32 } 33 34 // This notification gets sent solely for test purposes. It should not be 35 // used by production code. 36 Services.obs.notifyObservers(window, "LoadPKCS11Module:FilePickHandled"); 37 }); 38 } 39 40 /** 41 * ondialogaccept() handler. 42 * 43 * @param {object} event 44 * The event causing this handler function to be called. 45 */ 46 function onDialogAccept(event) { 47 let nameBox = document.getElementById("device_name"); 48 let pathBox = document.getElementById("device_path"); 49 let pkcs11ModuleDB = Cc["@mozilla.org/security/pkcs11moduledb;1"].getService( 50 Ci.nsIPKCS11ModuleDB 51 ); 52 53 try { 54 pkcs11ModuleDB.addModule(nameBox.value, pathBox.value, 0, 0); 55 } catch (e) { 56 addModuleFailure("add-module-failure"); 57 event.preventDefault(); 58 } 59 } 60 61 async function addModuleFailure(l10nID) { 62 let [AddModuleFailure] = await document.l10n.formatValues([{ id: l10nID }]); 63 alertPromptService(window, null, AddModuleFailure); 64 } 65 66 function validateModuleName() { 67 let name = document.getElementById("device_name").value; 68 let helpText = document.getElementById("helpText"); 69 helpText.value = ""; 70 let dialogNode = document.querySelector("dialog"); 71 dialogNode.removeAttribute("buttondisabledaccept"); 72 if (name == "") { 73 document.l10n.setAttributes(helpText, "load-module-help-empty-module-name"); 74 dialogNode.setAttribute("buttondisabledaccept", true); 75 } 76 if (name == "Root Certs") { 77 document.l10n.setAttributes( 78 helpText, 79 "load-module-help-root-certs-module-name" 80 ); 81 dialogNode.setAttribute("buttondisabledaccept", true); 82 } 83 } 84 85 window.addEventListener("load", () => { 86 document 87 .getElementById("device_name") 88 .addEventListener("change", () => validateModuleName()); 89 document 90 .getElementById("browse") 91 .addEventListener("command", () => onBrowseBtnPress()); 92 });