addon-sdk/source/lib/sdk/system/environment.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/lib/sdk/system/environment.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,60 @@
     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 +
    1.10 +module.metadata = {
    1.11 +  "stability": "stable"
    1.12 +};
    1.13 +
    1.14 +const { Cc, Ci } = require('chrome');
    1.15 +const { get, set, exists } = Cc['@mozilla.org/process/environment;1'].
    1.16 +                             getService(Ci.nsIEnvironment);
    1.17 +
    1.18 +exports.env = Proxy.create({
    1.19 +  // XPCOM does not provides a way to enumerate environment variables, so we
    1.20 +  // just don't support enumeration.
    1.21 +  getPropertyNames: function() [],
    1.22 +  getOwnPropertyNames: function() [],
    1.23 +  enumerate: function() [],
    1.24 +  keys: function() [],
    1.25 +  // We do not support freezing, cause it would make it impossible to set new
    1.26 +  // environment variables.
    1.27 +  fix: function() undefined,
    1.28 +  // We present all environment variables as own properties of this object,
    1.29 +  // so we just delegate this call to `getOwnPropertyDescriptor`.
    1.30 +  getPropertyDescriptor: function(name) this.getOwnPropertyDescriptor(name),
    1.31 +  // If environment variable with this name is defined, we generate proprety
    1.32 +  // descriptor for it, otherwise fall back to `undefined` so that for consumer
    1.33 +  // this property does not exists.
    1.34 +  getOwnPropertyDescriptor: function(name) {
    1.35 +    return !exists(name) ? undefined : {
    1.36 +      value: get(name),
    1.37 +      enumerable: false,    // Non-enumerable as we don't support enumeration.
    1.38 +      configurable: true,   // Configurable as it may be deleted.
    1.39 +      writable: true        // Writable as we do support set.
    1.40 +    }
    1.41 +  },
    1.42 +
    1.43 +  // New environment variables can be defined just by defining properties
    1.44 +  // on this object.
    1.45 +  defineProperty: function(name, { value }) set(name, value),
    1.46 +  delete: function(name) {
    1.47 +    set(name, null);
    1.48 +    return true;
    1.49 +  },
    1.50 +
    1.51 +  // We present all properties as own, there for we just delegate to `hasOwn`.
    1.52 +  has: function(name) this.hasOwn(name),
    1.53 +  // We do support checks for existence of an environment variable, via `in`
    1.54 +  // operator on this object.
    1.55 +  hasOwn: function(name) exists(name),
    1.56 +
    1.57 +  // On property get / set we do read / write appropriate environment variables,
    1.58 +  // please note though, that variables with names of standard object properties
    1.59 +  // intentionally (so that this behaves as normal object) can not be
    1.60 +  // read / set.
    1.61 +  get: function(proxy, name) Object.prototype[name] || get(name) || undefined,
    1.62 +  set: function(proxy, name, value) Object.prototype[name] || set(name, value)
    1.63 +});

mercurial