michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: 'use strict'; michael@0: michael@0: module.metadata = { michael@0: "stability": "unstable" michael@0: }; michael@0: michael@0: const { Cc, Ci, CC } = require('chrome'); michael@0: const options = require('@loader/options'); michael@0: const file = require('./io/file'); michael@0: const runtime = require("./system/runtime"); michael@0: michael@0: const appStartup = Cc['@mozilla.org/toolkit/app-startup;1']. michael@0: getService(Ci.nsIAppStartup); michael@0: const appInfo = Cc["@mozilla.org/xre/app-info;1"]. michael@0: getService(Ci.nsIXULAppInfo); michael@0: const directoryService = Cc['@mozilla.org/file/directory_service;1']. michael@0: getService(Ci.nsIProperties); michael@0: michael@0: const PR_WRONLY = parseInt("0x02"); michael@0: const PR_CREATE_FILE = parseInt("0x08"); michael@0: const PR_APPEND = parseInt("0x10"); michael@0: const PR_TRUNCATE = parseInt("0x20"); michael@0: michael@0: function openFile(path, mode) { michael@0: let file = Cc["@mozilla.org/file/local;1"]. michael@0: createInstance(Ci.nsILocalFile); michael@0: file.initWithPath(path); michael@0: let stream = Cc["@mozilla.org/network/file-output-stream;1"]. michael@0: createInstance(Ci.nsIFileOutputStream); michael@0: stream.init(file, mode, -1, 0); michael@0: return stream michael@0: } michael@0: michael@0: const { eAttemptQuit: E_ATTEMPT, eForceQuit: E_FORCE } = appStartup; michael@0: michael@0: /** michael@0: * Parsed JSON object that was passed via `cfx --static-args "{ foo: 'bar' }"` michael@0: */ michael@0: exports.staticArgs = options.staticArgs; michael@0: michael@0: /** michael@0: * Environment variables. Environment variables are non-enumerable properties michael@0: * of this object (key is name and value is value). michael@0: */ michael@0: exports.env = require('./system/environment').env; michael@0: michael@0: /** michael@0: * Ends the process with the specified `code`. If omitted, exit uses the michael@0: * 'success' code 0. To exit with failure use `1`. michael@0: * TODO: Improve platform to actually quit with an exit code. michael@0: */ michael@0: let forcedExit = false; michael@0: exports.exit = function exit(code) { michael@0: if (forcedExit) { michael@0: // a forced exit was already tried michael@0: // NOTE: exit(0) is called twice sometimes (ex when using cfx testaddons) michael@0: return; michael@0: } michael@0: michael@0: // This is used by 'cfx' to find out exit code. michael@0: if ('resultFile' in options && options.resultFile) { michael@0: let mode = PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE; michael@0: let stream = openFile(options.resultFile, mode); michael@0: let status = code ? 'FAIL' : 'OK'; michael@0: stream.write(status, status.length); michael@0: stream.flush(); michael@0: stream.close(); michael@0: } michael@0: michael@0: if (code == 0) { michael@0: forcedExit = true; michael@0: } michael@0: appStartup.quit(code ? E_ATTEMPT : E_FORCE); michael@0: }; michael@0: michael@0: // Adapter for nodejs's stdout & stderr: michael@0: // http://nodejs.org/api/process.html#process_process_stdout michael@0: let stdout = Object.freeze({ write: dump, end: dump }); michael@0: exports.stdout = stdout; michael@0: exports.stderr = stdout; michael@0: michael@0: /** michael@0: * Returns a path of the system's or application's special directory / file michael@0: * associated with a given `id`. For list of possible `id`s please see: michael@0: * https://developer.mozilla.org/en-US/docs/Code_snippets/File_I_O#Getting_files_in_special_directories michael@0: * http://mxr.mozilla.org/mozilla-central/source/xpcom/io/nsAppDirectoryServiceDefs.h michael@0: * @example michael@0: * michael@0: * // get firefox profile path michael@0: * let profilePath = require('system').pathFor('ProfD'); michael@0: * // get OS temp files directory (/tmp) michael@0: * let temps = require('system').pathFor('TmpD'); michael@0: * // get OS desktop path for an active user (~/Desktop on linux michael@0: * // or C:\Documents and Settings\username\Desktop on windows). michael@0: * let desktopPath = require('system').pathFor('Desk'); michael@0: */ michael@0: exports.pathFor = function pathFor(id) { michael@0: return directoryService.get(id, Ci.nsIFile).path; michael@0: }; michael@0: michael@0: /** michael@0: * What platform you're running on (all lower case string). michael@0: * For possible values see: michael@0: * https://developer.mozilla.org/en/OS_TARGET michael@0: */ michael@0: exports.platform = runtime.OS.toLowerCase(); michael@0: michael@0: const [, architecture, compiler] = runtime.XPCOMABI ? michael@0: runtime.XPCOMABI.match(/^([^-]*)-(.*)$/) : michael@0: [, null, null]; michael@0: michael@0: /** michael@0: * What processor architecture you're running on: michael@0: * `'arm', 'ia32', or 'x64'`. michael@0: */ michael@0: exports.architecture = architecture; michael@0: michael@0: /** michael@0: * What compiler used for build: michael@0: * `'msvc', 'n32', 'gcc2', 'gcc3', 'sunc', 'ibmc'...` michael@0: */ michael@0: exports.compiler = compiler; michael@0: michael@0: /** michael@0: * The application's build ID/date, for example "2004051604". michael@0: */ michael@0: exports.build = appInfo.appBuildID; michael@0: michael@0: /** michael@0: * The XUL application's UUID. michael@0: * This has traditionally been in the form michael@0: * `{AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE}` but for some applications it may michael@0: * be: "appname@vendor.tld". michael@0: */ michael@0: exports.id = appInfo.ID; michael@0: michael@0: /** michael@0: * The name of the application. michael@0: */ michael@0: exports.name = appInfo.name; michael@0: michael@0: /** michael@0: * The XUL application's version, for example "0.8.0+" or "3.7a1pre". michael@0: */ michael@0: exports.version = appInfo.version; michael@0: michael@0: /** michael@0: * XULRunner version. michael@0: */ michael@0: exports.platformVersion = appInfo.platformVersion; michael@0: michael@0: michael@0: /** michael@0: * The name of the application vendor, for example "Mozilla". michael@0: */ michael@0: exports.vendor = appInfo.vendor;