2014-06-19 04:56:02 +04:00
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
|
|
|
|
|
|
|
|
this.EXPORTED_SYMBOLS = ["WindowsRegistry"];
|
|
|
|
|
2015-10-07 15:03:21 +03:00
|
|
|
var WindowsRegistry = {
|
2014-06-19 04:56:02 +04:00
|
|
|
/**
|
|
|
|
* 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.
|
2016-04-15 11:23:00 +03:00
|
|
|
* @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.
|
2014-06-19 04:56:02 +04:00
|
|
|
* @return The key value or undefined if it doesn't exist. If the key is
|
|
|
|
* a REG_MULTI_SZ, an array is returned.
|
|
|
|
*/
|
2016-04-15 11:23:00 +03:00
|
|
|
readRegKey: function(aRoot, aPath, aKey, aRegistryNode=0) {
|
2014-06-19 04:56:02 +04:00
|
|
|
const kRegMultiSz = 7;
|
2016-04-15 11:23:00 +03:00
|
|
|
const kMode = Ci.nsIWindowsRegKey.ACCESS_READ | aRegistryNode;
|
2014-06-19 04:56:02 +04:00
|
|
|
let registry = Cc["@mozilla.org/windows-registry-key;1"].
|
|
|
|
createInstance(Ci.nsIWindowsRegKey);
|
|
|
|
try {
|
2016-04-15 11:23:00 +03:00
|
|
|
registry.open(aRoot, aPath, kMode);
|
2014-06-19 04:56:02 +04:00
|
|
|
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);
|
2015-10-18 22:57:14 +03:00
|
|
|
return str.split("\0").filter(v => v);
|
2014-06-19 04:56:02 +04:00
|
|
|
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;
|
|
|
|
},
|
2016-02-22 20:34:30 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
2016-04-15 11:23:00 +03:00
|
|
|
* @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.
|
2016-04-27 12:37:00 +03:00
|
|
|
* @return True if the key was removed or never existed, false otherwise.
|
2016-02-22 20:34:30 +03:00
|
|
|
*/
|
2016-04-15 11:23:00 +03:00
|
|
|
removeRegKey: function(aRoot, aPath, aKey, aRegistryNode=0) {
|
2016-02-22 20:34:30 +03:00
|
|
|
let registry = Cc["@mozilla.org/windows-registry-key;1"].
|
|
|
|
createInstance(Ci.nsIWindowsRegKey);
|
2016-04-27 12:37:00 +03:00
|
|
|
let result = false;
|
2016-02-22 20:34:30 +03:00
|
|
|
try {
|
|
|
|
let mode = Ci.nsIWindowsRegKey.ACCESS_QUERY_VALUE |
|
2016-04-15 11:23:00 +03:00
|
|
|
Ci.nsIWindowsRegKey.ACCESS_SET_VALUE |
|
|
|
|
aRegistryNode;
|
2016-02-22 20:34:30 +03:00
|
|
|
registry.open(aRoot, aPath, mode);
|
|
|
|
if (registry.hasValue(aKey)) {
|
|
|
|
registry.removeValue(aKey);
|
2016-04-27 12:37:00 +03:00
|
|
|
result = !registry.hasValue(aKey);
|
|
|
|
} else {
|
|
|
|
result = true;
|
2016-02-22 20:34:30 +03:00
|
|
|
}
|
|
|
|
} catch (ex) {
|
|
|
|
} finally {
|
|
|
|
registry.close();
|
2016-04-27 12:37:00 +03:00
|
|
|
return result;
|
2016-02-22 20:34:30 +03:00
|
|
|
}
|
|
|
|
}
|
2014-06-19 04:56:02 +04:00
|
|
|
};
|