function setCordovaAndInitialize()

in src/app-host/app-host.js [65:200]


function setCordovaAndInitialize(originalCordova) {
    var channel,
        platform,
        platformBootstrap;

    if (cordova) {
        return;
    }

    cordova = originalCordova;

    cordova.define.remove('cordova/exec');
    cordova.define('cordova/exec', function (require, exports, module) {
        module.exports = exec;
    });

    platform = cordova.require('cordova/platform');
    platformBootstrap = platform.bootstrap;
    platform.bootstrap = function () {
    };

    // default Windows bootstrap function tries to load WinJS which is not
    // available and not required in simulation mode so we override bootstrap
    if (cordova.platformId === 'windows') {
        platformBootstrap = function () {
            cordova.require('cordova/modulemapper')
                .clobbers('cordova/exec/proxy', 'cordova.commandProxy');

        };
    }

    channel = cordova.require('cordova/channel');

    // define our own channel to delay the initialization until sim-host tells
    // us everything's ready (fired in 'start' event handler).
    channel.createSticky('onCordovaSimulateReady');
    channel.waitForInitialization('onCordovaSimulateReady');

    socket.on('start-live-reload', function () {
        livereload.start(socket);
    });

    socket.on('init-telemetry', function () {
        telemetry.init(socket);
    });

    socket.on('init-xhr-proxy', function () {
        require('xhr-proxy').init();
    });

    socket.on('init-touch-events', function () {
        require('./touch-events').init();
    });

    socket.on('refresh', function () {
        document.location.reload(true);
    });

    // firing of onNativeReady is delayed until SIM_HOST tells us it's ready
    socket.once('init', function () {
        // sim-host is ready, register exec handlers, fire onNativeReady and send
        // the list of plugins
        socket.on('exec-success', function (data) {
            var execCacheInfo = execCache[data.index];
            if (execCacheInfo && typeof execCacheInfo.success === 'function') {
                execCacheInfo.success(data.result);
            }
        });

        socket.on('exec-failure', function (data) {
            var execCacheInfo = execCache[data.index];
            if (execCacheInfo && typeof execCacheInfo.fail === 'function') {
                execCacheInfo.fail(data.error);
            }
        });

        if (cordova.platformId !== 'browser') {
            channel.onPluginsReady.subscribe(function () {
                var pluginList;
                try {
                    pluginList = cordova.require('cordova/plugin_list').metadata;
                } catch (ex) {
                    // when the app doesn't contain any plugin, the module "cordova/plugin_list"
                    // is not loaded and cordova.require throws an exception
                    pluginList = {};
                }
                socket.emit('app-plugin-list', pluginList);
            });
        } else {
            socket.emit('app-plugin-list', {});
        }

        applyPlugins(plugins);
        applyPlugins(pluginHandlersDefinitions, pluginHandlers, serviceToPluginMap);
        applyPlugins(pluginClobberDefinitions, window);

        telemetry.registerPluginServices(serviceToPluginMap);

        platformBootstrap();

        switch (cordova.platformId) {
            // these platform fire onNativeReady in their bootstrap
            case 'ios':
            case 'browser':
            case 'blackberry10':
            case 'firefoxos':
            case 'ubuntu':
            case 'webos':
                break;
            // windows has an overridden bootstrap which does not fire
            // onNativeReady
            case 'windows':
            // android specified here just to be explicit about it
            /* falls through */
            case 'android':
            default:
                channel.onNativeReady.fire();
                break;
        }

    });

    socket.once('start', function () {
        // all set, fire onCordovaSimulate ready (which up to this point was
        // delaying onDeviceReady).
        channel.onCordovaSimulateReady.fire();
        // an init after start means reload. it is only sent if sim-host was
        // reloaded
        socket.once('init', function () {
            window.location.reload(true);
        });
    });

    // register app-host
    socket.emit('register-app-host');
}