toolkit/modules/WindowsRegistry.jsm

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/modules/WindowsRegistry.jsm	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,50 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +"use strict";
     1.9 +const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
    1.10 +
    1.11 +this.EXPORTED_SYMBOLS = ["WindowsRegistry"];
    1.12 +
    1.13 +const WindowsRegistry = {
    1.14 +  /**
    1.15 +   * Safely reads a value from the registry.
    1.16 +   *
    1.17 +   * @param aRoot
    1.18 +   *        The root registry to use.
    1.19 +   * @param aPath
    1.20 +   *        The registry path to the key.
    1.21 +   * @param aKey
    1.22 +   *        The key name.
    1.23 +   * @return The key value or undefined if it doesn't exist.  If the key is
    1.24 +   *         a REG_MULTI_SZ, an array is returned.
    1.25 +   */
    1.26 +  readRegKey: function(aRoot, aPath, aKey) {
    1.27 +    const kRegMultiSz = 7;
    1.28 +    let registry = Cc["@mozilla.org/windows-registry-key;1"].
    1.29 +                   createInstance(Ci.nsIWindowsRegKey);
    1.30 +    try {
    1.31 +      registry.open(aRoot, aPath, Ci.nsIWindowsRegKey.ACCESS_READ);
    1.32 +      if (registry.hasValue(aKey)) {
    1.33 +        let type = registry.getValueType(aKey);
    1.34 +        switch (type) {
    1.35 +          case kRegMultiSz:
    1.36 +            // nsIWindowsRegKey doesn't support REG_MULTI_SZ type out of the box.
    1.37 +            let str = registry.readStringValue(aKey);
    1.38 +            return [v for each (v in str.split("\0")) if (v)];
    1.39 +          case Ci.nsIWindowsRegKey.TYPE_STRING:
    1.40 +            return registry.readStringValue(aKey);
    1.41 +          case Ci.nsIWindowsRegKey.TYPE_INT:
    1.42 +            return registry.readIntValue(aKey);
    1.43 +          default:
    1.44 +            throw new Error("Unsupported registry value.");
    1.45 +        }
    1.46 +      }
    1.47 +    } catch (ex) {
    1.48 +    } finally {
    1.49 +      registry.close();
    1.50 +    }
    1.51 +    return undefined;
    1.52 +  },
    1.53 +};

mercurial