Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: lib/indexedDBBackup.js

Issue 29860578: Issue 6775 - Work around filter data stored in IndexedDB getting lost on Microsoft Edge (Closed)
Patch Set: Created Aug. 31, 2018, 3:36 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | lib/ioIndexedDB.js » ('j') | lib/ioIndexedDB.js » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/indexedDBBackup.js
diff --git a/lib/indexedDBBackup.js b/lib/indexedDBBackup.js
new file mode 100644
index 0000000000000000000000000000000000000000..28287e576e91c14fc1fa73526e8804677f76e812
--- /dev/null
+++ b/lib/indexedDBBackup.js
@@ -0,0 +1,148 @@
+/*
+ * This file is part of Adblock Plus <https://adblockplus.org/>,
+ * Copyright (C) 2006-present eyeo GmbH
+ *
+ * Adblock Plus is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 3 as
+ * published by the Free Software Foundation.
+ *
+ * Adblock Plus is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+"use strict";
+
+const {FilterNotifier} = require("../adblockpluscore/lib/filterNotifier");
+const {FilterStorage} = require("../adblockpluscore/lib/filterStorage");
+const {DownloadableSubscription,
+ SpecialSubscription} =
+ require("../adblockpluscore/lib/subscriptionClasses");
+
+const BACKUP_NAME = "file:indexedDB-backup";
Sebastian Noack 2018/08/31 18:07:05 Nit: IMO it reads better with a blank line separat
geo 2018/09/04 16:12:07 Done.
+let MIN_WRITE_INTERVAL;
Sebastian Noack 2018/08/31 18:07:05 Nit: Since this isn't really a constant I would sp
geo 2018/09/04 16:12:07 Done.
+let pendingUpdate;
+let isSaving;
+let lastBackup;
+let lastRun;
+
+function init(backupInterval = 60 * 1000)
+{
+ clearInterval(lastBackup);
+ pendingUpdate = false;
+ isSaving = false;
+ lastRun = false;
+ MIN_WRITE_INTERVAL = backupInterval;
+}
Sebastian Noack 2018/08/31 18:07:04 Could we have a setBackupInterval() function inste
geo 2018/09/04 16:12:07 Done.
+
+init();
+
+function scheduleBackup()
+{
+ pendingUpdate = true;
+ if (!lastRun)
+ {
+ lastRun = performance.now();
+ pendingUpdate = false;
+ isSaving = true;
+
+ saveToStorage().then(() =>
+ {
+ isSaving = false;
+ lastRun = performance.now();
+ if (pendingUpdate)
+ scheduleBackup();
+ });
+ }
+ else
+ {
+ clearInterval(lastBackup);
+
+ lastBackup = setTimeout(() =>
+ {
+ if (!isSaving && (performance.now() - lastRun) >= MIN_WRITE_INTERVAL)
+ {
+ isSaving = true;
+ pendingUpdate = false;
+ lastRun = performance.now();
+ saveToStorage().then(() =>
+ {
+ isSaving = false;
+ lastRun = performance.now();
+ if (pendingUpdate)
+ scheduleBackup();
+ });
+ }
+ }, MIN_WRITE_INTERVAL - (performance.now() - lastRun));
+ }
Sebastian Noack 2018/08/31 18:07:04 Correct me if I'm wrong, but to me it seems the lo
geo 2018/09/04 16:12:07 Done.
+}
+
+function saveToStorage()
+{
+ return browser.storage.local.set({
+ [BACKUP_NAME]: {
+ content: serialize(),
+ lastModified: Date.now()
+ }
+ });
+}
+
+function serialize()
+{
+ let buffer = [];
+
+ for (let subscription of FilterStorage.subscriptions)
+ {
+ if (subscription instanceof SpecialSubscription)
+ {
+ subscription.serialize(buffer);
+ buffer.push("[Subscription filters]");
+ subscription.serializeFilters(buffer);
+ }
+ else if (subscription instanceof DownloadableSubscription)
+ {
+ let {homepage, title, url, disabled} = subscription;
+
+ buffer.push(
+ "[Subscription]",
+ `homepage=${homepage}`,
+ `title=${title}`,
+ `url=${url}`,
+ `disabled=${disabled}`);
Sebastian Noack 2018/08/31 18:07:05 Nit: We wrap argument lists always with either of
geo 2018/09/04 16:12:07 Done.
+ }
+ }
+ return buffer;
+}
+
+function getBackupData()
+{
+ return browser.storage.local.get(BACKUP_NAME).then(items =>
+ {
+ let entry = items[BACKUP_NAME];
+ if (entry)
+ return entry;
+
+ throw {type: "NoSuchFile"};
+ });
+}
+
+FilterNotifier.on("load", scheduleBackup);
+FilterNotifier.on("subscription.updated", scheduleBackup);
+FilterNotifier.on("subscription.added", scheduleBackup);
+FilterNotifier.on("subscription.removed", scheduleBackup);
+FilterNotifier.on("subscription.disabled", scheduleBackup);
+FilterNotifier.on("filter.added", scheduleBackup);
+FilterNotifier.on("filter.removed", scheduleBackup);
+FilterNotifier.on("filter.moved", scheduleBackup);
+FilterNotifier.on("filter.disabled", scheduleBackup);
+
+exports.IndexedDBBackup =
+{
+ getBackupData,
+ // Non-public API, just for tests.
+ init
+};
« no previous file with comments | « no previous file | lib/ioIndexedDB.js » ('j') | lib/ioIndexedDB.js » ('J')

Powered by Google App Engine
This is Rietveld