# 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 at6//7//      http://www.apache.org/licenses/LICENSE-2.08//9// Unless required by applicable law or agreed to in writing, software10// 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 and13// limitations under the License.1415/**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 the19 * current URL's query string. Variables that have more than one variable will20 * 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 a22 * variable does not have any values, but is nonetheless present in the query23 * string, it will be initialized to an empty string.24 * After the initial parsing, environment variables must be queried and set25 * through the API defined in this file.26 */2728goog.provide('webdriver.process');2930goog.require('goog.Uri');31goog.require('goog.array');32goog.require('goog.json');333435/**36 * @return {boolean} Whether the current process is Node's native process37 *     object.38 */39webdriver.process.isNative = function() {40  return webdriver.process.IS_NATIVE_PROCESS_;41};424344/**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 not48 *     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};555657/**58 * Sets an environment value. If the new value is either null or undefined, the59 *     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};676869/**70 * Whether the current environment is using Node's native process object.71 * @private {boolean}72 * @const73 */74webdriver.process.IS_NATIVE_PROCESS_ = typeof process !== 'undefined';757677/**78 * Initializes a process object for use in a browser window.79 * @param {!Window=} opt_window The window object to initialize the process80 *     from; if not specified, will default to the current window. Should only81 *     be set for unit testing.82 * @return {!Object} The new process object.83 * @private84 */85webdriver.process.initBrowserProcess_ = function(opt_window) {86  var process = {'env': {}};8788  var win = opt_window;89  if (!win && typeof window != 'undefined') {90    win = window;91  }9293  // Initialize the global error handler.94  if (win) {95    // Initialize the environment variable map by parsing the current URL query96    // 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  }107108  return process;109};110111112/**113 * The global process object to use. Will either be Node's global114 * {@code process} object, or an approximation of it for use in a browser115 * environment.116 * @private {!Object}117 * @const118 */119webdriver.process.PROCESS_ = webdriver.process.IS_NATIVE_PROCESS_ ? process :120    webdriver.process.initBrowserProcess_();