summaryrefslogtreecommitdiffstats
path: root/toolkit/modules/WindowsRegistry.sys.mjs
blob: 7bc6c77ae195dc5ceb4a57ca957a39b5143f445a (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
/* 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/. */

export var WindowsRegistry = {
  /**
   * Safely reads a value from the registry.
   *
   * @param aRoot
   *        The root registry to use.
   * @param aPath
   *        The registry path to the key.
   * @param aKey
   *        The key name.
   * @param [aRegistryNode=0]
   *        Optionally set to nsIWindowsRegKey.WOW64_64 (or nsIWindowsRegKey.WOW64_32)
   *        to access a 64-bit (32-bit) key from either a 32-bit or 64-bit application.
   * @return The key value or undefined if it doesn't exist.  If the key is
   *         a REG_MULTI_SZ, an array is returned.
   */
  readRegKey(aRoot, aPath, aKey, aRegistryNode = 0) {
    const kRegMultiSz = 7;
    const kMode = Ci.nsIWindowsRegKey.ACCESS_READ | aRegistryNode;
    let registry = Cc["@mozilla.org/windows-registry-key;1"].createInstance(
      Ci.nsIWindowsRegKey
    );
    try {
      registry.open(aRoot, aPath, kMode);
      if (registry.hasValue(aKey)) {
        let type = registry.getValueType(aKey);
        switch (type) {
          case kRegMultiSz:
            // nsIWindowsRegKey doesn't support REG_MULTI_SZ type out of the box.
            let str = registry.readStringValue(aKey);
            return str.split("\0").filter(v => v);
          case Ci.nsIWindowsRegKey.TYPE_STRING:
            return registry.readStringValue(aKey);
          case Ci.nsIWindowsRegKey.TYPE_INT:
            return registry.readIntValue(aKey);
          default:
            throw new Error("Unsupported registry value.");
        }
      }
    } catch (ex) {
    } finally {
      registry.close();
    }
    return undefined;
  },

  /**
   * Safely removes a key from the registry.
   *
   * @param aRoot
   *        The root registry to use.
   * @param aPath
   *        The registry path to the key.
   * @param aKey
   *        The key name.
   * @param [aRegistryNode=0]
   *        Optionally set to nsIWindowsRegKey.WOW64_64 (or nsIWindowsRegKey.WOW64_32)
   *        to access a 64-bit (32-bit) key from either a 32-bit or 64-bit application.
   * @return True if the key was removed or never existed, false otherwise.
   */
  removeRegKey(aRoot, aPath, aKey, aRegistryNode = 0) {
    let registry = Cc["@mozilla.org/windows-registry-key;1"].createInstance(
      Ci.nsIWindowsRegKey
    );
    let result = false;
    try {
      let mode =
        Ci.nsIWindowsRegKey.ACCESS_QUERY_VALUE |
        Ci.nsIWindowsRegKey.ACCESS_SET_VALUE |
        aRegistryNode;
      registry.open(aRoot, aPath, mode);
      if (registry.hasValue(aKey)) {
        registry.removeValue(aKey);
        result = !registry.hasValue(aKey);
      } else {
        result = true;
      }
    } catch (ex) {
    } finally {
      registry.close();
    }
    return result;
  },
};