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/. */
5 export var WindowsRegistry = {
7 * Safely reads a value from the registry.
10 * The root registry to use.
12 * The registry path to the key.
15 * @param [aRegistryNode=0]
16 * Optionally set to nsIWindowsRegKey.WOW64_64 (or nsIWindowsRegKey.WOW64_32)
17 * to access a 64-bit (32-bit) key from either a 32-bit or 64-bit application.
18 * @return The key value or undefined if it doesn't exist. If the key is
19 * a REG_MULTI_SZ, an array is returned.
21 readRegKey(aRoot, aPath, aKey, aRegistryNode = 0) {
22 const kRegMultiSz = 7;
23 const kMode = Ci.nsIWindowsRegKey.ACCESS_READ | aRegistryNode;
24 let registry = Cc["@mozilla.org/windows-registry-key;1"].createInstance(
28 registry.open(aRoot, aPath, kMode);
29 if (registry.hasValue(aKey)) {
30 let type = registry.getValueType(aKey);
33 // nsIWindowsRegKey doesn't support REG_MULTI_SZ type out of the box.
34 let str = registry.readStringValue(aKey);
35 return str.split("\0").filter(v => v);
36 case Ci.nsIWindowsRegKey.TYPE_STRING:
37 return registry.readStringValue(aKey);
38 case Ci.nsIWindowsRegKey.TYPE_INT:
39 return registry.readIntValue(aKey);
41 throw new Error("Unsupported registry value.");
52 * Safely removes a key from the registry.
55 * The root registry to use.
57 * The registry path to the key.
60 * @param [aRegistryNode=0]
61 * Optionally set to nsIWindowsRegKey.WOW64_64 (or nsIWindowsRegKey.WOW64_32)
62 * to access a 64-bit (32-bit) key from either a 32-bit or 64-bit application.
63 * @return True if the key was removed or never existed, false otherwise.
65 removeRegKey(aRoot, aPath, aKey, aRegistryNode = 0) {
66 let registry = Cc["@mozilla.org/windows-registry-key;1"].createInstance(
72 Ci.nsIWindowsRegKey.ACCESS_QUERY_VALUE |
73 Ci.nsIWindowsRegKey.ACCESS_SET_VALUE |
75 registry.open(aRoot, aPath, mode);
76 if (registry.hasValue(aKey)) {
77 registry.removeValue(aKey);
78 result = !registry.hasValue(aKey);