summaryrefslogtreecommitdiffstats
path: root/toolkit/modules/WindowsRegistry.sys.mjs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 17:32:43 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 17:32:43 +0000
commit6bf0a5cb5034a7e684dcc3500e841785237ce2dd (patch)
treea68f146d7fa01f0134297619fbe7e33db084e0aa /toolkit/modules/WindowsRegistry.sys.mjs
parentInitial commit. (diff)
downloadthunderbird-6bf0a5cb5034a7e684dcc3500e841785237ce2dd.tar.xz
thunderbird-6bf0a5cb5034a7e684dcc3500e841785237ce2dd.zip
Adding upstream version 1:115.7.0.upstream/1%115.7.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'toolkit/modules/WindowsRegistry.sys.mjs')
-rw-r--r--toolkit/modules/WindowsRegistry.sys.mjs88
1 files changed, 88 insertions, 0 deletions
diff --git a/toolkit/modules/WindowsRegistry.sys.mjs b/toolkit/modules/WindowsRegistry.sys.mjs
new file mode 100644
index 0000000000..7bc6c77ae1
--- /dev/null
+++ b/toolkit/modules/WindowsRegistry.sys.mjs
@@ -0,0 +1,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;
+ },
+};