/** * Secure Local Storage is a key value store modeled around html5 localstorage. * * The key differences from localstorage are: * * - Uses an Asynchronous api based on Ext.space.Promise * - Each application can more than one named collection of keys or easier data storage * - All data is encrypted before being persisted to disk * - The storage limits for SecureLocalStorage are much higher than the 2-3mb allocated for localstorage. * * var secrets = Ext.space.SecureLocalStorage.get('secrets'); * * secrets.set('myKey',object).then(function(){ * //do something when done. * }); * * secrets.get('myKey').then(function(object){ * var a = object.field; * }); * * secrets.remove().then(function(isDeleted){ * // done. * }); * * secrets.has(key).then(function(hasKey){ * * }); * * secrets.forEach(function(key, value){}).then(function(){ * // done. * }); * * secrets.count().then(function(numberOfItems){ * * }); * * secrets.clear().then(function(){ * // done. * }); */Ext.define("Ext.space.SecureLocalStorage", { singleton: true, /** * @private */ _collections: null, /** * @private */ constructor: function() { this._collections = {}; this.loaded = new Ext.space.Promise(); }, /** * Get a collection of name. Collections are automatically created if they do not exist. * * @param {String} collectionName The name of the collection to get. * @return {Ext.space.localstorage.Collection} the secure collection. * */ get: function(collectionName) { this.load(); var collection = this._collections[collectionName]; if (!collection) { collection = new Ext.space.localstorage.Collection(collectionName, this.loaded); this._collections[collectionName] = collection; } return collection; }, /** * @private */ load: function() { var me = this, loaded = me.loaded; if (me.db) { return; } Ext.onSpaceReady().then(function() { var lsdb = new Ext.space.securesql.Database({ name: "sencha_secure_local_store", displayName: "Secure Local Storage", skipVersionTracking: true }); lsdb.loaded.then(function(db) { me.db = db; var transaction = db.createTransaction(); transaction.sql("CREATE TABLE IF NOT EXISTS item (collection TEXT, name TEXT, value TEXT, PRIMARY KEY (collection, name))"); transaction.sql("CREATE INDEX IF NOT EXISTS name_idx on item (name)"); transaction.sql("CREATE INDEX IF NOT EXISTS collection_idx on item (collection)"); return transaction.execute().then(function() { loaded.fulfill(me.db); }); }); }).error(function(e) { loaded.reject(e); }); return loaded; }});