All files / universal/gpii/node_modules/processReporter/src processesBridge.js

93.62% Statements 44/47
77.78% Branches 14/18
92.31% Functions 12/13
93.62% Lines 44/47

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268                                3x 3x   3x                                                                                                                                                       3x 2x 6x 6x 6x                           3x 2x 4x 4x 4x   4x                             3x 30x 28x   30x 50x 22x                               3x 10x 10x   10x 40x 14x   40x                             3x 2x 2x 2x                 3x                               3x 4x                     3x 4x 4x         4x   4x 2x     4x                     3x 6x                                       3x      
/*!
GPII Process Reporter processes bridge, a component that encapsulates common
functions for retrieving information about OS processes with no requirement for
native (e.g., linux or windows) OS access.
 
Copyright 2014 Inclusive Design Research Centre, OCAD University
 
Licensed under the New BSD license. You may not use this file except in
compliance with this License.
 
You may obtain a copy of the License at
https://github.com/gpii/universal/LICENSE.txt
*/
 
"use strict";
 
var fluid = fluid || require("infusion");
var gpii = fluid.registerNamespace("gpii");
 
fluid.defaults("gpii.processes", {
    gradeNames: ["fluid.component", "fluid.contextAware"],
    contextAwareness: {
        platform: {
            checks: {
                linux: {
                    contextValue: "{gpii.context.linux}",
                    gradeNames: "gpii.processes.linux"
                },
                windows: {
                    contextValue: "{gpii.context.windows}",
                    gradeNames: "gpii.processes.windows"
                }
            }
        }
    },
    invokers: {
        findSolutionsByCommands: {
            funcName: "gpii.processes.findSolutionsByCommands",
            args: ["{that}", "{arguments}.0"]
                             // array of command names
        },
        findSolutionsByPids: {
            funcName: "gpii.processes.findSolutionsByPids",
            args: ["{that}", "{arguments}.0"]
                             // array of pids (process ids)
        },
        findProcessByPid: {
            funcName: "gpii.processes.findProcessByPid",
            args: ["{that}", "{arguments}.0", "{arguments}.1"]
                             // pid, procArray (optional)
        },
        findProcessesByCommand: {
            funcName: "gpii.processes.findProcessesByCommand",
            args: ["{that}", "{arguments}.0", "{arguments}.1"]
                             // command, procArray (optional)
        },
        findFirstProcessByCommand: {
            funcName: "gpii.processes.findFirstProcessByCommand",
            args: ["{that}", "{arguments}.0", "{arguments}.1"]
                             // command, procArray (optional)
        },
        isRunning: {
            funcName: "gpii.processes.isRunning",
            args: ["{that}", "{arguments}.0"]
                             // state (string)
        },
        updateProcInfo: {
            funcName: "gpii.processes.updateProcInfo",
            args: ["{that}", "{arguments}.0"]
                             // process info structure
        },
        initProcInfoNotRunning: {
            funcName: "gpii.processes.initProcInfoNotRunning",
            args: ["{that}", "{arguments}.0"]
                             // command name (string)
        },
        // Context aware invokers.
        getProcessList : {
            funcName: "gpii.processes.getProcessList",
            args: ["{arguments}.0"]
                   // optional string or numeric identifier of the process
        }
    }
});
 
/**
 * Return a list of process information objects corresponding to the names
 * of each of the passed in commands.  If nothing is found, returns an empty
 * array.
 *
 * @param that {Component} - an instance of a processes component.
 * @param commandNames {Array of String} - the names of the processes to inspect.
 * @return {Array} - Array of procInfo objects for each command name.  Empty
 *                   if no processes are found.
 */
gpii.processes.findSolutionsByCommands = function (that, commandNames) {
    return fluid.accumulate(commandNames, function (aCommand, matches) {
        var procInfos = that.findProcessesByCommand(aCommand);
        matches = matches.concat(procInfos);
        return matches;
    }, []);
};
 
/**
 * Return an list of process information objects corresponding to the
 * pids (process id numbers) passed in.  If nothing is found, returns an empty
 * array.
 *
 * @param that {Component} - an instance of a processes component.
 * @param pids {Array of Number} - the process ids of the processes to inspect.
 * @return {Array of ProcInfo} - Array of procInfo objects for each pid. Empty
 *                   if no corresponding processes are found.
 */
gpii.processes.findSolutionsByPids = function (that, pids) {
    return fluid.accumulate(pids, function (aPid, matches) {
        var found = that.findProcessByPid(aPid);
        Eif (found !== null) {
            matches.push(found);
        }
        return matches;
    }, []);
};
 
/**
 * Return a process information object corresponding to THE given process
 * id (process id number).  Returns null if there is no such process.
 *
 * @param that {Component} - an instance of a processes component.
 * @param pid {Number} - the process id of the processe to inspect.
 * @param procArray {Array of ProcInfo} [optional] - an array of process information
 *                                     objects to search.
 * @return {ProcInfo} - A process information object for the process with the
 *                    given id.  Returns null if there is no such process.
 */
gpii.processes.findProcessByPid = function (that, pid, procArray) {
    if (!procArray) {
        procArray = that.getProcessList(pid);
    }
    return fluid.find(procArray, function (procInfo) {
        if (procInfo.pid === pid) {
            return procInfo;
        }
    }, null);
};
 
/**
 * Return a list of process information objects that match the given
 * command name. Return san empty array if not matching name is found.
 *
 * @param that {Component} - an instance of a processes component.
 * @param commandName {String} - the name of the processe to inspect.
 * @param procArray {Array of ProcInfo} [optional] - an array of process information
 *                                     objects to search.
 * @return {Array of ProcInfo} - Array of procInfo objects matching the command name. Empty
 *                   if no corresponding processes are found.
 */
gpii.processes.findProcessesByCommand = function (that, commandName, procArray) {
    Eif (!procArray) {
        procArray = that.getProcessList(commandName);
    }
    return fluid.accumulate(procArray, function (aProcInfo, matchingProcs) {
        if (aProcInfo.command === commandName) {
            matchingProcs.push(aProcInfo);
        }
        return matchingProcs;
    }, []);
};
 
/**
 * Return the first process of an array of processes all with the same
 * command name. If there are no matching names, return null.
 *
 * @param that {Component} - an instance of a processes component.
 * @param commandName {String} - the name of the processe to inspect.
 * @param procArray {Array of ProcInfo} [optional] - an array of process information
 *                                     objects to search.
 * @return {Object} - A process information object for the process with the
 *                    given id, null if there is no such process.
 */
gpii.processes.findFirstProcessByCommand = function (that, commandName, procArray) {
    var commands = that.findProcessesByCommand(commandName, procArray);
    Eif (commands.length > 0) {
        return commands[0];
    }
    else {
        return null;
    }
};
 
// Map to reduce process state values into "Running" (= true) vs. "Not Running"
// (= false).
gpii.processes.stateToRunning = fluid.freezeRecursive({
    "Running": true,
    "Uninterruptible": true,
    "Sleeping": true,
    "Stopped": true,
    "Zombie": false,
    "NoSuchProcess": false
});
 
/**
 * Determine if a process is running based on its native state.
 *
 * @param that {Component} - an instance of a processes component.
 * @param state {String} - Native state of the process
 * @return {Boolean} - Returns true if the process is running, false otherwise.
 */
gpii.processes.isRunning = function (that, state) {
    return gpii.processes.stateToRunning[state];
};
 
/**
 * Renew the information about a process, or create a new "no such process"
 * information object.
 *
 * @param that {Component} - an instance of a processes component.
 * @param procInfo {ProcInfo} - Latest information about a process.
 * @return {ProcInfo} - Returns a new process information object about the process.
 */
gpii.processes.updateProcInfo = function (that, procInfo) {
    var newProcInfo = null;
    Iif (procInfo.state === "NoSuchProcess") {
        newProcInfo =
            that.findFirstProcessByCommand(procInfo.command);
    }
    else {
        newProcInfo = that.findProcessByPid(procInfo.pid);
    }
    if (newProcInfo === null) {
        newProcInfo =
            that.initProcInfoNotRunning(procInfo.command);
    }
    return newProcInfo;
};
 
/**
 * Create process information object for a not-running process.
 *
 * @param that {Component} - an instance of a processes component.
 * @param command {String} - Name of the process.
 * @return {ProcInfo} - Returns a new process information object initialized as
 *                    if the process is not running.
 */
gpii.processes.initProcInfoNotRunning = function (that, command) {
    return fluid.freezeRecursive({
        "command": command,
        "pid": -1,
        "ppid": -1,
        "uid": -1,
        "gid": -1,
        "fullPath": "",
        "argv": "",
        "state": "NoSuchProcess"
    });
};
 
/**
 * Default function for the base 'gpii.processes' grade to return an empty list
 * of process information objects.
 *
 * @param identifier {Number} or {String} - Optional process id number or
 *                                          command name of the process.
 * @return {Array} - Returns an empty list.
 */
gpii.processes.getProcessList = function (/* identifier */) {
    return [];
};