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

branch
TOR_BUG_3246
changeset 7
129ffea94266
equal deleted inserted replaced
-1:000000000000 0:2a463be344be
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/. */
4
5 'use strict';
6
7 module.metadata = {
8 "stability": "stable"
9 };
10
11 const { Cc, Ci } = require('chrome');
12 const { get, set, exists } = Cc['@mozilla.org/process/environment;1'].
13 getService(Ci.nsIEnvironment);
14
15 exports.env = Proxy.create({
16 // XPCOM does not provides a way to enumerate environment variables, so we
17 // just don't support enumeration.
18 getPropertyNames: function() [],
19 getOwnPropertyNames: function() [],
20 enumerate: function() [],
21 keys: function() [],
22 // We do not support freezing, cause it would make it impossible to set new
23 // environment variables.
24 fix: function() undefined,
25 // We present all environment variables as own properties of this object,
26 // so we just delegate this call to `getOwnPropertyDescriptor`.
27 getPropertyDescriptor: function(name) this.getOwnPropertyDescriptor(name),
28 // If environment variable with this name is defined, we generate proprety
29 // descriptor for it, otherwise fall back to `undefined` so that for consumer
30 // this property does not exists.
31 getOwnPropertyDescriptor: function(name) {
32 return !exists(name) ? undefined : {
33 value: get(name),
34 enumerable: false, // Non-enumerable as we don't support enumeration.
35 configurable: true, // Configurable as it may be deleted.
36 writable: true // Writable as we do support set.
37 }
38 },
39
40 // New environment variables can be defined just by defining properties
41 // on this object.
42 defineProperty: function(name, { value }) set(name, value),
43 delete: function(name) {
44 set(name, null);
45 return true;
46 },
47
48 // We present all properties as own, there for we just delegate to `hasOwn`.
49 has: function(name) this.hasOwn(name),
50 // We do support checks for existence of an environment variable, via `in`
51 // operator on this object.
52 hasOwn: function(name) exists(name),
53
54 // On property get / set we do read / write appropriate environment variables,
55 // please note though, that variables with names of standard object properties
56 // intentionally (so that this behaves as normal object) can not be
57 // read / set.
58 get: function(proxy, name) Object.prototype[name] || get(name) || undefined,
59 set: function(proxy, name, value) Object.prototype[name] || set(name, value)
60 });

mercurial