preferences.js 4.99 KB
/*
 * ADOBE CONFIDENTIAL
 * __________________
 *
 *  Copyright 2016 Adobe Systems Incorporated
 *  All Rights Reserved.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Adobe Systems Incorporated and its suppliers,
 * if any.  The intellectual and technical concepts contained
 * herein are proprietary to Adobe Systems Incorporated and its
 * suppliers and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Adobe Systems Incorporated.
 */

/* globals */
define('screens/player/command/preferences', [
    'jquery',
    'underscore',
    'screens/player/command/command',
    'screens/player/shared/serviceadmin',
    'screens/player/shared/csrf',
    'screens/player/store/store',
    'screens/player/firmware/preferences/preferences',
    'screens/player/firmware/core/firmware/firmware',
    'module'
], function($, _, PlayerCommand, ServiceAdmin, CSRFSupport, Store, Preferences, Firmware, mod) {
    'use strict';

    var ENDPOINT_SUFFIX = '.preferences.json';

    /**
     * Removes sensitive information from the provided preferences.
     *
     * @param  {Map} prefs The preferences to clean
     *
     * @return {Map} the preferences with sensitive information removed
     */
    function _removeSensitiveData(prefs) {
        var preferences = _.assign({}, prefs);
        delete preferences.device;
        delete preferences.user;
        delete preferences.password;
        return preferences;
    }

    /**
     * This command handler is able to process the following commands:
     * - "preferences-update" updates the preferences from the server
     * - "preferences-send" sends the device's preferences to the server without containing sensitive data
     *
     * @class PreferencesCommand
     * @implements {PlayerCommand}
     */
    var PreferencesCommand = _.extend({}, PlayerCommand, {

        serviceId: mod.id,

        /**
         * @function PreferencesCommand#canHandle
         * @inheritdoc
         */
        canHandle: function(cmd) {
            return [Preferences.COMMANDS.UPDATE, Preferences.COMMANDS.SEND].indexOf(cmd) > -1;
        },

        /**
         * @function PreferencesCommand#activate
         * @inheritdoc
         */
        activate: function() {
            return new Promise(function(resolve, reject) {
                ServiceAdmin.onServiceHighestRankedStart(Store.serviceName, function(store) {
                    resolve(store);
                });
            }).then(function() {
                return new Promise(function(resolve, reject) {
                    ServiceAdmin.onServiceHighestRankedStart(Preferences.serviceName, function(prefsSvc) {
                        resolve(prefsSvc);
                    });
                });
            }).then(function() {
                return new Promise(function(resolve, reject) {
                    ServiceAdmin.onServiceHighestRankedStart(Firmware.serviceName, function(firmwareSvc) {
                        resolve(firmwareSvc);
                    });
                });
            });
        },

        /**
         * @function PreferencesCommand#handleCommand
         * @inheritdoc
         */
        handleCommand: function(command, payload) {
            var storeSvc = ServiceAdmin.getService(Store.serviceName);
            var state = storeSvc.getState();

            var csrf = new CSRFSupport({
                serverURL: state.statusmodel.server
            });

            switch (command) {
                case Preferences.COMMANDS.UPDATE:
                    return csrf.$ajax($, {
                        url: state.statusmodel.devicePostUrl + ENDPOINT_SUFFIX,
                        type: 'GET'
                    })
                        .then(function(data) {
                            if (data.status === 'ok') {
                                var prefsSvc = ServiceAdmin.getService(Preferences.serviceName);
                                delete data.status;
                                var flags = {};
                                flags[Preferences.FLAGS.REBOOT_ON_SAVE] = true;
                                prefsSvc.save(data, flags);
                            }
                        });
                case Preferences.COMMANDS.SEND:
                    var preferences = _removeSensitiveData(state.preferences);
                    return csrf.$ajax($, {
                        url: state.statusmodel.devicePostUrl + ENDPOINT_SUFFIX,
                        type: 'POST',
                        data: {
                            data: JSON.stringify(preferences)
                        }
                    });
                default: return null;
            }
        }

    });

    return PreferencesCommand;
});

/* istanbul ignore next */
require([
    'screens/player/command/preferences',
    'screens/player/shared/serviceadmin'
], function(Service, ServiceAdmin) {
    'use strict';
    ServiceAdmin.register(Service);
});