Index: lib/indexedDBBackup.js |
diff --git a/lib/indexedDBBackup.js b/lib/indexedDBBackup.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..e0464b5e3714d0eac4c9cfc7c3e0e0395b88bc55 |
--- /dev/null |
+++ b/lib/indexedDBBackup.js |
@@ -0,0 +1,120 @@ |
+/* |
+ * 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"); |
Sebastian Noack
2018/09/04 21:00:01
Nit: No reason to wrap twice here (also more consi
geo
2018/09/05 14:04:33
Done.
|
+ |
+const BACKUP_NAME = "file:indexedDB-backup"; |
+ |
+let minWriteInterval; |
+let lastBackup; |
+let lastRun = -minWriteInterval; |
+ |
+function setBackupInterval(backupInterval = 60 * 1000) |
+{ |
+ minWriteInterval = backupInterval; |
+} |
+ |
+setBackupInterval(); |
+ |
+function scheduleBackup() |
+{ |
+ if (!lastBackup) |
+ { |
+ lastBackup = setTimeout( |
Sebastian Noack
2018/09/04 21:00:01
It seems now the only reason to have the lastBacku
geo
2018/09/05 14:04:33
Done.
|
+ () => |
+ { |
+ saveToStorage(); |
+ lastBackup = null; |
+ lastRun = performance.now(); |
+ }, |
+ lastRun + minWriteInterval - performance.now() |
Sebastian Noack
2018/09/04 21:00:01
Just an idea, I'm wondering if a simpler throttlin
geo
2018/09/05 14:04:33
Yup, it does make things simpler. And you are prob
|
+ ); |
+ } |
+} |
+ |
+function saveToStorage() |
+{ |
+ 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}` |
+ ); |
+ } |
+ } |
+ 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. |
+ setBackupInterval |
+}; |