summaryrefslogtreecommitdiffstats
path: root/toolkit/components/normandy/lib/Storage.sys.mjs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 17:32:43 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 17:32:43 +0000
commit6bf0a5cb5034a7e684dcc3500e841785237ce2dd (patch)
treea68f146d7fa01f0134297619fbe7e33db084e0aa /toolkit/components/normandy/lib/Storage.sys.mjs
parentInitial commit. (diff)
downloadthunderbird-upstream.tar.xz
thunderbird-upstream.zip
Adding upstream version 1:115.7.0.upstream/1%115.7.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'toolkit/components/normandy/lib/Storage.sys.mjs')
-rw-r--r--toolkit/components/normandy/lib/Storage.sys.mjs90
1 files changed, 90 insertions, 0 deletions
diff --git a/toolkit/components/normandy/lib/Storage.sys.mjs b/toolkit/components/normandy/lib/Storage.sys.mjs
new file mode 100644
index 0000000000..8d563db24c
--- /dev/null
+++ b/toolkit/components/normandy/lib/Storage.sys.mjs
@@ -0,0 +1,90 @@
+/* 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/. */
+
+import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";
+
+const lazy = {};
+
+ChromeUtils.defineESModuleGetters(lazy, {
+ JSONFile: "resource://gre/modules/JSONFile.sys.mjs",
+});
+
+// Lazy-load JSON file that backs Storage instances.
+XPCOMUtils.defineLazyGetter(lazy, "lazyStore", async function () {
+ const path = PathUtils.join(
+ PathUtils.profileDir,
+ "shield-recipe-client.json"
+ );
+ const store = new lazy.JSONFile({ path });
+ await store.load();
+ return store;
+});
+
+export var Storage = class {
+ constructor(prefix) {
+ this.prefix = prefix;
+ }
+
+ /**
+ * Clear ALL storage data and save to the disk.
+ */
+ static async clearAllStorage() {
+ const store = await lazy.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 lazy.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 lazy.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 lazy.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 lazy.lazyStore;
+ store.data[this.prefix] = {};
+ store.saveSoon();
+ }
+};