| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, | |
| 3 * Copyright (C) 2006-present eyeo GmbH | |
| 4 * | |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify | |
| 6 * it under the terms of the GNU General Public License version 3 as | |
| 7 * published by the Free Software Foundation. | |
| 8 * | |
| 9 * Adblock Plus is distributed in the hope that it will be useful, | |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 12 * GNU General Public License for more details. | |
| 13 * | |
| 14 * You should have received a copy of the GNU General Public License | |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | |
| 16 */ | |
| 17 | |
| 18 "use strict"; | |
| 19 | |
| 20 const {FilterNotifier} = require("../adblockpluscore/lib/filterNotifier"); | |
| 21 const {FilterStorage} = require("../adblockpluscore/lib/filterStorage"); | |
| 22 const {DownloadableSubscription, | |
| 23 SpecialSubscription} = | |
| 24 require("../adblockpluscore/lib/subscriptionClasses"); | |
| 25 | |
| 26 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.
| |
| 27 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.
| |
| 28 let pendingUpdate; | |
| 29 let isSaving; | |
| 30 let lastBackup; | |
| 31 let lastRun; | |
| 32 | |
| 33 function init(backupInterval = 60 * 1000) | |
| 34 { | |
| 35 clearInterval(lastBackup); | |
| 36 pendingUpdate = false; | |
| 37 isSaving = false; | |
| 38 lastRun = false; | |
| 39 MIN_WRITE_INTERVAL = backupInterval; | |
| 40 } | |
|
Sebastian Noack
2018/08/31 18:07:04
Could we have a setBackupInterval() function inste
geo
2018/09/04 16:12:07
Done.
| |
| 41 | |
| 42 init(); | |
| 43 | |
| 44 function scheduleBackup() | |
| 45 { | |
| 46 pendingUpdate = true; | |
| 47 if (!lastRun) | |
| 48 { | |
| 49 lastRun = performance.now(); | |
| 50 pendingUpdate = false; | |
| 51 isSaving = true; | |
| 52 | |
| 53 saveToStorage().then(() => | |
| 54 { | |
| 55 isSaving = false; | |
| 56 lastRun = performance.now(); | |
| 57 if (pendingUpdate) | |
| 58 scheduleBackup(); | |
| 59 }); | |
| 60 } | |
| 61 else | |
| 62 { | |
| 63 clearInterval(lastBackup); | |
| 64 | |
| 65 lastBackup = setTimeout(() => | |
| 66 { | |
| 67 if (!isSaving && (performance.now() - lastRun) >= MIN_WRITE_INTERVAL) | |
| 68 { | |
| 69 isSaving = true; | |
| 70 pendingUpdate = false; | |
| 71 lastRun = performance.now(); | |
| 72 saveToStorage().then(() => | |
| 73 { | |
| 74 isSaving = false; | |
| 75 lastRun = performance.now(); | |
| 76 if (pendingUpdate) | |
| 77 scheduleBackup(); | |
| 78 }); | |
| 79 } | |
| 80 }, MIN_WRITE_INTERVAL - (performance.now() - lastRun)); | |
| 81 } | |
|
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.
| |
| 82 } | |
| 83 | |
| 84 function saveToStorage() | |
| 85 { | |
| 86 return browser.storage.local.set({ | |
| 87 [BACKUP_NAME]: { | |
| 88 content: serialize(), | |
| 89 lastModified: Date.now() | |
| 90 } | |
| 91 }); | |
| 92 } | |
| 93 | |
| 94 function serialize() | |
| 95 { | |
| 96 let buffer = []; | |
| 97 | |
| 98 for (let subscription of FilterStorage.subscriptions) | |
| 99 { | |
| 100 if (subscription instanceof SpecialSubscription) | |
| 101 { | |
| 102 subscription.serialize(buffer); | |
| 103 buffer.push("[Subscription filters]"); | |
| 104 subscription.serializeFilters(buffer); | |
| 105 } | |
| 106 else if (subscription instanceof DownloadableSubscription) | |
| 107 { | |
| 108 let {homepage, title, url, disabled} = subscription; | |
| 109 | |
| 110 buffer.push( | |
| 111 "[Subscription]", | |
| 112 `homepage=${homepage}`, | |
| 113 `title=${title}`, | |
| 114 `url=${url}`, | |
| 115 `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.
| |
| 116 } | |
| 117 } | |
| 118 return buffer; | |
| 119 } | |
| 120 | |
| 121 function getBackupData() | |
| 122 { | |
| 123 return browser.storage.local.get(BACKUP_NAME).then(items => | |
| 124 { | |
| 125 let entry = items[BACKUP_NAME]; | |
| 126 if (entry) | |
| 127 return entry; | |
| 128 | |
| 129 throw {type: "NoSuchFile"}; | |
| 130 }); | |
| 131 } | |
| 132 | |
| 133 FilterNotifier.on("load", scheduleBackup); | |
| 134 FilterNotifier.on("subscription.updated", scheduleBackup); | |
| 135 FilterNotifier.on("subscription.added", scheduleBackup); | |
| 136 FilterNotifier.on("subscription.removed", scheduleBackup); | |
| 137 FilterNotifier.on("subscription.disabled", scheduleBackup); | |
| 138 FilterNotifier.on("filter.added", scheduleBackup); | |
| 139 FilterNotifier.on("filter.removed", scheduleBackup); | |
| 140 FilterNotifier.on("filter.moved", scheduleBackup); | |
| 141 FilterNotifier.on("filter.disabled", scheduleBackup); | |
| 142 | |
| 143 exports.IndexedDBBackup = | |
| 144 { | |
| 145 getBackupData, | |
| 146 // Non-public API, just for tests. | |
| 147 init | |
| 148 }; | |
| OLD | NEW |