lib/webdriver/process.js

1// Copyright 2011 Software Freedom Conservancy. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15/**
16 * @fileoverview Provides access to the current process' environment variables.
17 * When running in node, this is simply a wrapper for {@code process.env}.
18 * When running in a browser, environment variables are loaded by parsing the
19 * current URL's query string. Variables that have more than one variable will
20 * be initialized to the JSON representation of the array of all values,
21 * otherwise the variable will be initialized to a sole string value. If a
22 * variable does not have any values, but is nonetheless present in the query
23 * string, it will be initialized to an empty string.
24 * After the initial parsing, environment variables must be queried and set
25 * through the API defined in this file.
26 */
27
28goog.provide('webdriver.process');
29
30goog.require('goog.Uri');
31goog.require('goog.array');
32goog.require('goog.json');
33
34
35/**
36 * @return {boolean} Whether the current process is Node's native process
37 * object.
38 */
39webdriver.process.isNative = function() {
40 return webdriver.process.IS_NATIVE_PROCESS_;
41};
42
43
44/**
45 * Queries for a named environment variable.
46 * @param {string} name The name of the environment variable to look up.
47 * @param {string=} opt_default The default value if the named variable is not
48 * defined.
49 * @return {string} The queried environment variable.
50 */
51webdriver.process.getEnv = function(name, opt_default) {
52 var value = webdriver.process.PROCESS_.env[name];
53 return goog.isDefAndNotNull(value) ? value : opt_default;
54};
55
56
57/**
58 * Sets an environment value. If the new value is either null or undefined, the
59 * environment variable will be cleared.
60 * @param {string} name The value to set.
61 * @param {*} value The new value; will be coerced to a string.
62 */
63webdriver.process.setEnv = function(name, value) {
64 webdriver.process.PROCESS_.env[name] =
65 goog.isDefAndNotNull(value) ? value + '' : null;
66};
67
68
69/**
70 * Whether the current environment is using Node's native process object.
71 * @private {boolean}
72 * @const
73 */
74webdriver.process.IS_NATIVE_PROCESS_ = typeof process !== 'undefined';
75
76
77/**
78 * Initializes a process object for use in a browser window.
79 * @param {!Window=} opt_window The window object to initialize the process
80 * from; if not specified, will default to the current window. Should only
81 * be set for unit testing.
82 * @return {!Object} The new process object.
83 * @private
84 */
85webdriver.process.initBrowserProcess_ = function(opt_window) {
86 var process = {'env': {}};
87
88 var win = opt_window;
89 if (!win && typeof window != 'undefined') {
90 win = window;
91 }
92
93 // Initialize the global error handler.
94 if (win) {
95 // Initialize the environment variable map by parsing the current URL query
96 // string.
97 if (win.location) {
98 var data = new goog.Uri(win.location).getQueryData();
99 goog.array.forEach(data.getKeys(), function(key) {
100 var values = data.getValues(key);
101 process.env[key] = values.length == 0 ? '' :
102 values.length == 1 ? values[0] :
103 goog.json.serialize(values);
104 });
105 }
106 }
107
108 return process;
109};
110
111
112/**
113 * The global process object to use. Will either be Node's global
114 * {@code process} object, or an approximation of it for use in a browser
115 * environment.
116 * @private {!Object}
117 * @const
118 */
119webdriver.process.PROCESS_ = webdriver.process.IS_NATIVE_PROCESS_ ? process :
120 webdriver.process.initBrowserProcess_();