diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
commit | 2aa4a82499d4becd2284cdb482213d541b8804dd (patch) | |
tree | b80bf8bf13c3766139fbacc530efd0dd9d54394c /toolkit/components/normandy/lib/Storage.jsm | |
parent | Initial commit. (diff) | |
download | firefox-upstream.tar.xz firefox-upstream.zip |
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'toolkit/components/normandy/lib/Storage.jsm')
-rw-r--r-- | toolkit/components/normandy/lib/Storage.jsm | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/toolkit/components/normandy/lib/Storage.jsm b/toolkit/components/normandy/lib/Storage.jsm new file mode 100644 index 0000000000..78ccd80b93 --- /dev/null +++ b/toolkit/components/normandy/lib/Storage.jsm @@ -0,0 +1,97 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +"use strict"; + +const { XPCOMUtils } = ChromeUtils.import( + "resource://gre/modules/XPCOMUtils.jsm" +); + +ChromeUtils.defineModuleGetter( + this, + "JSONFile", + "resource://gre/modules/JSONFile.jsm" +); +ChromeUtils.defineModuleGetter(this, "OS", "resource://gre/modules/osfile.jsm"); + +var EXPORTED_SYMBOLS = ["Storage"]; + +// Lazy-load JSON file that backs Storage instances. +XPCOMUtils.defineLazyGetter(this, "lazyStore", async function() { + const path = OS.Path.join( + OS.Constants.Path.profileDir, + "shield-recipe-client.json" + ); + const store = new JSONFile({ path }); + await store.load(); + return store; +}); + +var Storage = class { + constructor(prefix) { + this.prefix = prefix; + } + + /** + * Clear ALL storage data and save to the disk. + */ + static async clearAllStorage() { + const store = await lazyStore; + store.data = {}; + store.saveSoon(); + } + + /** + * Sets an item in the prefixed storage. + * @returns {Promise} + * @resolves With the stored value, or null. + * @rejects Javascript exception. + */ + async getItem(name) { + const store = await lazyStore; + const namespace = store.data[this.prefix] || {}; + return namespace[name] || null; + } + + /** + * Sets an item in the prefixed storage. + * @returns {Promise} + * @resolves When the operation is completed successfully + * @rejects Javascript exception. + */ + async setItem(name, value) { + const store = await lazyStore; + if (!(this.prefix in store.data)) { + store.data[this.prefix] = {}; + } + store.data[this.prefix][name] = value; + store.saveSoon(); + } + + /** + * Removes a single item from the prefixed storage. + * @returns {Promise} + * @resolves When the operation is completed successfully + * @rejects Javascript exception. + */ + async removeItem(name) { + const store = await lazyStore; + if (this.prefix in store.data) { + delete store.data[this.prefix][name]; + store.saveSoon(); + } + } + + /** + * Clears all storage for the prefix. + * @returns {Promise} + * @resolves When the operation is completed successfully + * @rejects Javascript exception. + */ + async clear() { + const store = await lazyStore; + store.data[this.prefix] = {}; + store.saveSoon(); + } +}; |