toolkit/modules/WindowsRegistry.jsm

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     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 "use strict";
     6 const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
     8 this.EXPORTED_SYMBOLS = ["WindowsRegistry"];
    10 const WindowsRegistry = {
    11   /**
    12    * Safely reads a value from the registry.
    13    *
    14    * @param aRoot
    15    *        The root registry to use.
    16    * @param aPath
    17    *        The registry path to the key.
    18    * @param aKey
    19    *        The key name.
    20    * @return The key value or undefined if it doesn't exist.  If the key is
    21    *         a REG_MULTI_SZ, an array is returned.
    22    */
    23   readRegKey: function(aRoot, aPath, aKey) {
    24     const kRegMultiSz = 7;
    25     let registry = Cc["@mozilla.org/windows-registry-key;1"].
    26                    createInstance(Ci.nsIWindowsRegKey);
    27     try {
    28       registry.open(aRoot, aPath, Ci.nsIWindowsRegKey.ACCESS_READ);
    29       if (registry.hasValue(aKey)) {
    30         let type = registry.getValueType(aKey);
    31         switch (type) {
    32           case kRegMultiSz:
    33             // nsIWindowsRegKey doesn't support REG_MULTI_SZ type out of the box.
    34             let str = registry.readStringValue(aKey);
    35             return [v for each (v in str.split("\0")) if (v)];
    36           case Ci.nsIWindowsRegKey.TYPE_STRING:
    37             return registry.readStringValue(aKey);
    38           case Ci.nsIWindowsRegKey.TYPE_INT:
    39             return registry.readIntValue(aKey);
    40           default:
    41             throw new Error("Unsupported registry value.");
    42         }
    43       }
    44     } catch (ex) {
    45     } finally {
    46       registry.close();
    47     }
    48     return undefined;
    49   },
    50 };

mercurial