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