All files / skeleton preload.js

90.7% Statements 39/43
92.86% Branches 13/14
90% Functions 9/10
90.7% Lines 39/43
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                                                                        1x 1x   1x 1x 1x                             1x                             10x 10x 10x 5x   10x 10x 4x   6x   10x 6x 6x 3x 1x   3x 2x   3x                         5x                     5x                       1x 1x 1x 2x 2x 6x 2x                                                     1x 1x 1x 1x                   1x 1x       13x               1x  
// TODO: we have a Chrome 51 in Electron now - this must be rewritten to ES6!
 
// This is a modified version of
// https://github.com/electron-webapps/meteor-electron/blob/master/app/preload.js
 
/**
 The MIT License (MIT)
 
 Copyright (c) 2015 Michael Risse
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in all
 copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 SOFTWARE.
 */
 
/**
 * Since we've disabled Node integration in the Browser window, we must selectively expose
 * main-process/Node modules via this script.
 *
 * @WARNING This file must take care not to leak the imported modules to the Browser window!
 * See https://github.com/atom/electron/issues/1753#issuecomment-104719851.
 */
var _ = require('lodash');
var ipc = require('electron').ipcRenderer;
 
let devtron = null;
try {
    devtron = require('devtron');
    window.__devtron = {require: require, process: process};
} catch (e) {
 
}
 
/**
 * Callback passed to ipc on/once methods.
 *
 * @callback ipcListener
 * @param {string} event - event name
 * @param {...*=}  args  - event's arguments
 */
 
 
var Desktop = {
    devtron,
    onceEventListeners: {},
    eventListeners: {},
    registeredInIpc: {},
 
    /**
     * Adds a callback to internal listeners placeholders and registers real ipc hooks.
     *
     * @param {string}      module   - module name
     * @param {string}      event    - the name of an event
     * @param {ipcListener} callback - callback to fire when event arrives
     * @param {boolean}     once     - whether this should be fired only once
     */
    addToListeners: function addToListeners(module, event, callback, once) {
        var self = this;
        let listeners = 'eventListeners';
        if (once) {
            listeners = 'onceEventListeners';
        }
        var eventName = this.getEventName(module, event);
        if (eventName in this[listeners]) {
            this[listeners][eventName].push(callback);
        } else {
            this[listeners][eventName] = [callback];
        }
        if (!(eventName in this.registeredInIpc)) {
            this.registeredInIpc[eventName] = true;
            ipc.on(eventName, function ipcOn(/* event, ...args */) {
                if (eventName in self.eventListeners) {
                    _.invokeMap(self.eventListeners[eventName], 'apply', undefined, arguments);
                }
                if (eventName in self.onceEventListeners) {
                    _.invokeMap(self.onceEventListeners[eventName], 'apply', undefined, arguments);
                }
                self.onceEventListeners[eventName] = [];
            });
        }
    },
 
    /**
     * Invokes callback when the specified IPC event is fired.
     *
     * @param {string} module        - module name
     * @param {string} event         - the name of an event
     * @param {ipcListener} callback - function to invoke when `event` is triggered
     */
    on: function on(module, event, callback) {
        this.addToListeners(module, event, callback);
    },
 
    /**
     * Invokes a callback once when the specified IPC event is fired.
     *
     * @param {string} module        - module name
     * @param {string} event         - the name of an event
     * @param {ipcListener} callback - function to invoke when `event` is triggered
     */
    once: function once(module, event, callback) {
        this.addToListeners(module, event, callback, true);
    },
 
    /**
     * Unregisters a callback.
     *
     * @param {string} module     - module name
     * @param {string} event      - the name of an event
     * @param {function} callback - listener to unregister
     */
    removeListener: function removeListener(module, event, callback) {
        var i;
        var self = this;
        var eventName = this.getEventName(module, event);
        ['eventListeners', 'onceEventListeners'].forEach(function removeListenerFrom(listeners) {
            Eif (eventName in self[listeners]) {
                for (i = self[listeners][eventName].length - 1; i >= 0; i--) {
                    if (self[listeners][eventName][i] === callback) {
                        self[listeners][eventName].splice(i, 1);
                    }
                }
            }
        });
    },
 
    /**
     * Unregisters all callbacks.
     *
     * @param {string} module     - module name
     * @param {string} event      - the name of an event
     */
    removeAllListeners: function removeAllListeners(module, event) {
        var eventName = this.getEventName(module, event);
        this.onceEventListeners[eventName] = [];
        this.eventListeners[eventName] = [];
    },
 
    /**
     * Send an event to the main Electron process.
     *
     * @param {String} module - module name
     * @param {String} event  - the name of an event
     * @param {...*} arg      - additional arguments to pass to event handler
     */
    send: function send(/* module, event , ...args */) {
        var args = Array.prototype.slice.call(arguments);
        var module = args.shift();
        args[0] = this.getEventName(module, args[0]);
        ipc.send.apply(null, args);
    },
 
    /**
     * Send an global event to the main Electron process.
     *
     * @param {String} event - the name of an event
     * @param {...*} arg     - additional arguments to pass to event handler
     */
    sendGlobal: function send(/* event , ...args */) {
        var args = Array.prototype.slice.call(arguments);
        ipc.send.apply(ipc, args);
    },
 
    getEventName: function getEventName(module, event) {
        return module + '__' + event;
    }
};
 
 
/**
 * @global
 */
global.Desktop = Desktop;