Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | 'use strict'; |
michael@0 | 6 | |
michael@0 | 7 | module.metadata = { |
michael@0 | 8 | "stability": "stable" |
michael@0 | 9 | }; |
michael@0 | 10 | |
michael@0 | 11 | const { Cc, Ci } = require('chrome'); |
michael@0 | 12 | const { get, set, exists } = Cc['@mozilla.org/process/environment;1']. |
michael@0 | 13 | getService(Ci.nsIEnvironment); |
michael@0 | 14 | |
michael@0 | 15 | exports.env = Proxy.create({ |
michael@0 | 16 | // XPCOM does not provides a way to enumerate environment variables, so we |
michael@0 | 17 | // just don't support enumeration. |
michael@0 | 18 | getPropertyNames: function() [], |
michael@0 | 19 | getOwnPropertyNames: function() [], |
michael@0 | 20 | enumerate: function() [], |
michael@0 | 21 | keys: function() [], |
michael@0 | 22 | // We do not support freezing, cause it would make it impossible to set new |
michael@0 | 23 | // environment variables. |
michael@0 | 24 | fix: function() undefined, |
michael@0 | 25 | // We present all environment variables as own properties of this object, |
michael@0 | 26 | // so we just delegate this call to `getOwnPropertyDescriptor`. |
michael@0 | 27 | getPropertyDescriptor: function(name) this.getOwnPropertyDescriptor(name), |
michael@0 | 28 | // If environment variable with this name is defined, we generate proprety |
michael@0 | 29 | // descriptor for it, otherwise fall back to `undefined` so that for consumer |
michael@0 | 30 | // this property does not exists. |
michael@0 | 31 | getOwnPropertyDescriptor: function(name) { |
michael@0 | 32 | return !exists(name) ? undefined : { |
michael@0 | 33 | value: get(name), |
michael@0 | 34 | enumerable: false, // Non-enumerable as we don't support enumeration. |
michael@0 | 35 | configurable: true, // Configurable as it may be deleted. |
michael@0 | 36 | writable: true // Writable as we do support set. |
michael@0 | 37 | } |
michael@0 | 38 | }, |
michael@0 | 39 | |
michael@0 | 40 | // New environment variables can be defined just by defining properties |
michael@0 | 41 | // on this object. |
michael@0 | 42 | defineProperty: function(name, { value }) set(name, value), |
michael@0 | 43 | delete: function(name) { |
michael@0 | 44 | set(name, null); |
michael@0 | 45 | return true; |
michael@0 | 46 | }, |
michael@0 | 47 | |
michael@0 | 48 | // We present all properties as own, there for we just delegate to `hasOwn`. |
michael@0 | 49 | has: function(name) this.hasOwn(name), |
michael@0 | 50 | // We do support checks for existence of an environment variable, via `in` |
michael@0 | 51 | // operator on this object. |
michael@0 | 52 | hasOwn: function(name) exists(name), |
michael@0 | 53 | |
michael@0 | 54 | // On property get / set we do read / write appropriate environment variables, |
michael@0 | 55 | // please note though, that variables with names of standard object properties |
michael@0 | 56 | // intentionally (so that this behaves as normal object) can not be |
michael@0 | 57 | // read / set. |
michael@0 | 58 | get: function(proxy, name) Object.prototype[name] || get(name) || undefined, |
michael@0 | 59 | set: function(proxy, name, value) Object.prototype[name] || set(name, value) |
michael@0 | 60 | }); |