Index: lib/indexedDBBackup.js |
diff --git a/lib/indexedDBBackup.js b/lib/indexedDBBackup.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..0a472d08c1ee1eabd5303957d719f097b2ec02bb |
--- /dev/null |
+++ b/lib/indexedDBBackup.js |
@@ -0,0 +1,154 @@ |
+/* |
+ * 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"; |
+let MIN_WRITE_INTERVAL; |
+let pendingUpdate; |
+let isSaving; |
+let lastBackup; |
+let lastRun; |
+let saveToStorage; |
+ |
+function init(backupInterval, saveFunction) |
+{ |
+ clearInterval(lastBackup); |
+ pendingUpdate = false; |
+ isSaving = false; |
+ lastRun = false; |
+ MIN_WRITE_INTERVAL = backupInterval || 60 * 1000; |
Sebastian Noack
2018/08/21 20:26:24
Just out of curiosity? Could you experience any pe
geo
2018/08/31 15:49:24
I've tried to measure CPU usage without too much l
|
+ saveToStorage = saveFunction || |
Sebastian Noack
2018/08/21 20:26:24
I'd rather mock browser.storage.local.set() in the
geo
2018/08/31 15:49:24
Done.
|
+ function() |
+ { |
+ return browser.storage.local.set({ |
+ [BACKUP_NAME]: { |
+ content: serialize(), |
+ lastModified: Date.now() |
+ } |
+ }); |
+ }; |
+} |
+ |
+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)); |
+ } |
+} |
+ |
+function serialize(subscriptions = FilterStorage.subscriptions) |
+{ |
+ let buffer = []; |
+ for (let subscription of subscriptions) |
+ { |
+ if (subscription instanceof SpecialSubscription) |
+ { |
+ buffer.push(""); |
Sebastian Noack
2018/08/21 20:26:24
Do we have to add an empty line here?
geo
2018/08/31 15:49:24
Done.
|
+ subscription.serialize(buffer); |
+ buffer.push("", "[Subscription filters]"); |
+ subscription.serializeFilters(buffer); |
+ } |
+ else if (subscription instanceof DownloadableSubscription) |
+ { |
+ let {homepage, title, url, disabled} = subscription; |
+ |
+ buffer.push( |
Sebastian Noack
2018/08/21 20:26:24
Can't we just use Subscription.serialize() (but wi
geo
2018/08/31 15:49:24
As discussed on IRC, I'm leaving the current imple
|
+ "", |
+ "[Subscription]", |
+ `homepage=${homepage}`, |
+ `title=${title}`, |
+ `url=${url}`, |
+ `disabled=${disabled}`, |
+ ); |
+ } |
+ } |
+ 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, |
+ scheduleBackup, |
Sebastian Noack
2018/08/21 20:26:24
Can't we just emit one of the above events in the
geo
2018/08/31 15:49:24
Done.
|
+ serialize |
+}; |