cache-service.js 5.8 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 may be covered by U.S. and Foreign Patents,
 * patents in process, 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 Promise */

define('screens/player/firmware/cache/impl/cache-service', [
    'underscore',
    'screens/player/shared/serviceadmin',
    'screens/player/firmware/core/firmware/firmware',
    'screens/player/firmware/cache/cache',
    'screens/player/firmware/cache/spi/cache',
    'screens/player/store/store',
    'module'
], function(_, ServiceAdmin, Firmware, Cache, CacheSPI, Store, mod) {
    'use strict';

    function _stopBridge() {
        // workaround to stop the bridge until we have a proper service
        var firmwareSvc = ServiceAdmin.getService(Firmware.serviceName);
        return firmwareSvc.getPlayer().bridge.stop();
    }

    function _reboot() {
        var firmwareSvc = ServiceAdmin.getService(Firmware.serviceName);
        firmwareSvc.reboot();
    }

    function reportError(err) {
        console.error(err);
    }

    function dispatchDone() {
        this._store.dispatch({ type: Cache.ACTIONS.CLEAR_DONE });
    }

    /**
     * Implements the cache service
     *
     * @class CacheService
     * @implements {Player.Cache}
     */
    var CacheService = function() {};

    CacheService.serviceName = Cache.serviceName;

    CacheService.prototype = _.assign({}, Cache, {

        serviceId: mod.id,

        /**
         * @function CacheService#activate
         * @inheritdoc
         */
        activate: function() {
            var self = this;
            // wait for the store and device cache service to start.
            return new Promise(function(resolve) {
                ServiceAdmin.onServiceHighestRankedStart(Store.serviceName, function(store) {
                    self._store = store;
                    resolve(store);
                });
            })
            .then(function(store) {
                return new Promise(function(resolve) {
                    ServiceAdmin.onServiceHighestRankedStart(CacheSPI.serviceName, function(spi) {
                        self._spi = spi;
                        resolve();
                    });
                });
            });
        },

        /**
         * @function CacheService#deactivate
         * @inheritdoc
         */
        deactivate: function() {
            delete this._store;
            delete this._spi;
        },

        /**
         * @function CacheService#resetFirmware
         * @inheritdoc
         */
        resetFirmware: function(flags) {
            flags = flags || {};

            this._store.dispatch({ type: Cache.ACTIONS.RESET_FIRMWARE });
            _stopBridge()
                .then(function() { return this._spi.resetFirmware(); }.bind(this))
                .catch(reportError)
                .then(dispatchDone.bind(this))
                .then(function() {
                    if (flags[Cache.FLAGS.REBOOT_ON_COMPLETE]) {
                        _reboot();
                    }
                });
        },

        /**
         * @function CacheService#clearChannelsData
         * @inheritdoc
         */
        clearChannelsData: function(flags) {
            flags = flags || {};

            this._store.dispatch({ type: Cache.ACTIONS.CLEAR_CHANNELS_DATA });
            _stopBridge()
                .then(function() { return this._spi.clearChannelsData(); }.bind(this))
                .catch(reportError)
                .then(dispatchDone.bind(this))
                .then(function() {
                    if (flags[Cache.FLAGS.REBOOT_ON_COMPLETE]) {
                        _reboot();
                    }
                });
        },

        /**
         * @function CacheService#clearAppCache
         * @inheritdoc
         */
        clearAppCache: function(flags) {
            flags = flags || {};

            this._store.dispatch({ type: Cache.ACTIONS.CLEAR_APP_CACHE });
            _stopBridge()
                .then(function() { return this._spi.clearAppCache(); }.bind(this))
                .catch(reportError)
                .then(dispatchDone.bind(this))
                .then(function() { this._store.reset(); }.bind(this))
                .then(function() {
                    if (flags[Cache.FLAGS.REBOOT_ON_COMPLETE]) {
                        _reboot();
                    }
                });
        },

        /**
         * @function CacheService#clearAll
         * @inheritdoc
         */
        clearAll: function(flags) {
            flags = flags || {};

            this._store.dispatch({ type: Cache.ACTIONS.CLEAR_ALL });
            _stopBridge()
                .then(function() { return this._spi.clearChannelsData(); }.bind(this))
                .then(function() { return this._spi.clearAppCache(); }.bind(this))
                .catch(reportError)
                .then(dispatchDone.bind(this))
                .then(function() { return this._store.reset(); }.bind(this))
                .then(function() {
                    if (flags[Cache.FLAGS.REBOOT_ON_COMPLETE]) {
                        _reboot();
                    }
                });
        }
    });

    return CacheService;
});

/* istanbul ignore next */
require([
    'screens/player/firmware/cache/impl/cache-service',
    'screens/player/shared/serviceadmin'
], function(Service, ServiceAdmin) {
    'use strict';
    ServiceAdmin.register(new Service());
});