From ef03b708a87a406d15fc629dbcbdac564a8454ab Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Wed, 31 May 2017 17:22:07 +0100 Subject: [PATCH] Add MatrixClient.clearStores - to clear both sets of storage on logout --- src/client.js | 22 +++++++++++++++++++++- src/crypto/store/indexeddb-crypto-store.js | 22 ++++++++++++++++++++++ src/crypto/store/memory-crypto-store.js | 11 +++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/src/client.js b/src/client.js index 9b0e9ff3f..064e19e79 100644 --- a/src/client.js +++ b/src/client.js @@ -156,8 +156,9 @@ function MatrixClient(opts) { this._notifTimelineSet = null; this._crypto = null; + this._cryptoStore = opts.cryptoStore; if (CRYPTO_ENABLED && Boolean(opts.sessionStore) && - Boolean(opts.cryptoStore) && + Boolean(this._cryptoStore) && userId !== null && this.deviceId !== null) { this._crypto = new Crypto( this, this, @@ -173,6 +174,25 @@ function MatrixClient(opts) { utils.inherits(MatrixClient, EventEmitter); utils.extend(MatrixClient.prototype, MatrixBaseApis.prototype); +/** + * Clear any data out of the persistent stores used by the client. + * + * @returns {Promise} Promise which resolves when the stores have been cleared. + */ +MatrixClient.prototype.clearStores = function() { + if (this._clientRunning) { + throw new Error("Cannot clear stores while client is running"); + } + + const promises = []; + + promises.push(this.store.deleteAllData()); + if (this._cryptoStore) { + promises.push(this._cryptoStore.deleteAllData()); + } + return q.all(promises); +}; + /** * Get the user-id of the logged-in user * diff --git a/src/crypto/store/indexeddb-crypto-store.js b/src/crypto/store/indexeddb-crypto-store.js index 239d99565..d2a08abca 100644 --- a/src/crypto/store/indexeddb-crypto-store.js +++ b/src/crypto/store/indexeddb-crypto-store.js @@ -88,6 +88,28 @@ export default class IndexedDBCryptoStore { }); return this._dbPromise; } + + /** + * Delete all data from this store. + * + * @returns {Promise} resolves when the store has been cleared. + */ + deleteAllData() { + return new q.Promise((resolve, reject) => { + console.log(`Removing indexeddb instance: ${this._dbName}`); + const req = this._indexedDB.deleteDatabase(this._dbName); + req.onerror = (ev) => { + reject(new Error( + "unable to delete indexeddb: " + ev.target.error, + )); + }; + + req.onsuccess = () => { + console.log(`Removed indexeddb instance: ${this._dbName}`); + resolve(); + }; + }); + } } function createDatabase(db) { diff --git a/src/crypto/store/memory-crypto-store.js b/src/crypto/store/memory-crypto-store.js index 5890f58e6..23bc2fbca 100644 --- a/src/crypto/store/memory-crypto-store.js +++ b/src/crypto/store/memory-crypto-store.js @@ -14,6 +14,8 @@ See the License for the specific language governing permissions and limitations under the License. */ +import q from 'q'; + /** * Internal module. in-memory storage for e2e. * @@ -26,4 +28,13 @@ limitations under the License. export default class MemoryCryptoStore { constructor() { } + + /** + * Delete all data from this store. + * + * @returns {Promise} Promise which resolves when the store has been cleared. + */ + deleteAllData() { + return q(); + } }