1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/addon-sdk/source/lib/sdk/system.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,160 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +'use strict'; 1.9 + 1.10 +module.metadata = { 1.11 + "stability": "unstable" 1.12 +}; 1.13 + 1.14 +const { Cc, Ci, CC } = require('chrome'); 1.15 +const options = require('@loader/options'); 1.16 +const file = require('./io/file'); 1.17 +const runtime = require("./system/runtime"); 1.18 + 1.19 +const appStartup = Cc['@mozilla.org/toolkit/app-startup;1']. 1.20 + getService(Ci.nsIAppStartup); 1.21 +const appInfo = Cc["@mozilla.org/xre/app-info;1"]. 1.22 + getService(Ci.nsIXULAppInfo); 1.23 +const directoryService = Cc['@mozilla.org/file/directory_service;1']. 1.24 + getService(Ci.nsIProperties); 1.25 + 1.26 +const PR_WRONLY = parseInt("0x02"); 1.27 +const PR_CREATE_FILE = parseInt("0x08"); 1.28 +const PR_APPEND = parseInt("0x10"); 1.29 +const PR_TRUNCATE = parseInt("0x20"); 1.30 + 1.31 +function openFile(path, mode) { 1.32 + let file = Cc["@mozilla.org/file/local;1"]. 1.33 + createInstance(Ci.nsILocalFile); 1.34 + file.initWithPath(path); 1.35 + let stream = Cc["@mozilla.org/network/file-output-stream;1"]. 1.36 + createInstance(Ci.nsIFileOutputStream); 1.37 + stream.init(file, mode, -1, 0); 1.38 + return stream 1.39 +} 1.40 + 1.41 +const { eAttemptQuit: E_ATTEMPT, eForceQuit: E_FORCE } = appStartup; 1.42 + 1.43 +/** 1.44 + * Parsed JSON object that was passed via `cfx --static-args "{ foo: 'bar' }"` 1.45 + */ 1.46 +exports.staticArgs = options.staticArgs; 1.47 + 1.48 +/** 1.49 + * Environment variables. Environment variables are non-enumerable properties 1.50 + * of this object (key is name and value is value). 1.51 + */ 1.52 +exports.env = require('./system/environment').env; 1.53 + 1.54 +/** 1.55 + * Ends the process with the specified `code`. If omitted, exit uses the 1.56 + * 'success' code 0. To exit with failure use `1`. 1.57 + * TODO: Improve platform to actually quit with an exit code. 1.58 + */ 1.59 +let forcedExit = false; 1.60 +exports.exit = function exit(code) { 1.61 + if (forcedExit) { 1.62 + // a forced exit was already tried 1.63 + // NOTE: exit(0) is called twice sometimes (ex when using cfx testaddons) 1.64 + return; 1.65 + } 1.66 + 1.67 + // This is used by 'cfx' to find out exit code. 1.68 + if ('resultFile' in options && options.resultFile) { 1.69 + let mode = PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE; 1.70 + let stream = openFile(options.resultFile, mode); 1.71 + let status = code ? 'FAIL' : 'OK'; 1.72 + stream.write(status, status.length); 1.73 + stream.flush(); 1.74 + stream.close(); 1.75 + } 1.76 + 1.77 + if (code == 0) { 1.78 + forcedExit = true; 1.79 + } 1.80 + appStartup.quit(code ? E_ATTEMPT : E_FORCE); 1.81 +}; 1.82 + 1.83 +// Adapter for nodejs's stdout & stderr: 1.84 +// http://nodejs.org/api/process.html#process_process_stdout 1.85 +let stdout = Object.freeze({ write: dump, end: dump }); 1.86 +exports.stdout = stdout; 1.87 +exports.stderr = stdout; 1.88 + 1.89 +/** 1.90 + * Returns a path of the system's or application's special directory / file 1.91 + * associated with a given `id`. For list of possible `id`s please see: 1.92 + * https://developer.mozilla.org/en-US/docs/Code_snippets/File_I_O#Getting_files_in_special_directories 1.93 + * http://mxr.mozilla.org/mozilla-central/source/xpcom/io/nsAppDirectoryServiceDefs.h 1.94 + * @example 1.95 + * 1.96 + * // get firefox profile path 1.97 + * let profilePath = require('system').pathFor('ProfD'); 1.98 + * // get OS temp files directory (/tmp) 1.99 + * let temps = require('system').pathFor('TmpD'); 1.100 + * // get OS desktop path for an active user (~/Desktop on linux 1.101 + * // or C:\Documents and Settings\username\Desktop on windows). 1.102 + * let desktopPath = require('system').pathFor('Desk'); 1.103 + */ 1.104 +exports.pathFor = function pathFor(id) { 1.105 + return directoryService.get(id, Ci.nsIFile).path; 1.106 +}; 1.107 + 1.108 +/** 1.109 + * What platform you're running on (all lower case string). 1.110 + * For possible values see: 1.111 + * https://developer.mozilla.org/en/OS_TARGET 1.112 + */ 1.113 +exports.platform = runtime.OS.toLowerCase(); 1.114 + 1.115 +const [, architecture, compiler] = runtime.XPCOMABI ? 1.116 + runtime.XPCOMABI.match(/^([^-]*)-(.*)$/) : 1.117 + [, null, null]; 1.118 + 1.119 +/** 1.120 + * What processor architecture you're running on: 1.121 + * `'arm', 'ia32', or 'x64'`. 1.122 + */ 1.123 +exports.architecture = architecture; 1.124 + 1.125 +/** 1.126 + * What compiler used for build: 1.127 + * `'msvc', 'n32', 'gcc2', 'gcc3', 'sunc', 'ibmc'...` 1.128 + */ 1.129 +exports.compiler = compiler; 1.130 + 1.131 +/** 1.132 + * The application's build ID/date, for example "2004051604". 1.133 + */ 1.134 +exports.build = appInfo.appBuildID; 1.135 + 1.136 +/** 1.137 + * The XUL application's UUID. 1.138 + * This has traditionally been in the form 1.139 + * `{AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE}` but for some applications it may 1.140 + * be: "appname@vendor.tld". 1.141 + */ 1.142 +exports.id = appInfo.ID; 1.143 + 1.144 +/** 1.145 + * The name of the application. 1.146 + */ 1.147 +exports.name = appInfo.name; 1.148 + 1.149 +/** 1.150 + * The XUL application's version, for example "0.8.0+" or "3.7a1pre". 1.151 + */ 1.152 +exports.version = appInfo.version; 1.153 + 1.154 +/** 1.155 + * XULRunner version. 1.156 + */ 1.157 +exports.platformVersion = appInfo.platformVersion; 1.158 + 1.159 + 1.160 +/** 1.161 + * The name of the application vendor, for example "Mozilla". 1.162 + */ 1.163 +exports.vendor = appInfo.vendor;