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 |
26 const BACKUP_NAME = "file:indexedDB-backup"; | 25 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 | 26 |
33 function init(backupInterval = 60 * 1000) | 27 let backupDelay; |
28 let pendingBackup = false; | |
29 | |
30 function setBackupInterval(backupInterval = 60 * 1000) | |
34 { | 31 { |
35 clearInterval(lastBackup); | 32 backupDelay = backupInterval; |
36 pendingUpdate = false; | |
37 isSaving = false; | |
38 lastRun = false; | |
39 MIN_WRITE_INTERVAL = backupInterval; | |
40 } | 33 } |
Sebastian Noack
2018/08/31 18:07:04
Could we have a setBackupInterval() function inste
geo
2018/09/04 16:12:07
Done.
| |
41 | 34 |
42 init(); | 35 setBackupInterval(); |
43 | 36 |
44 function scheduleBackup() | 37 function scheduleBackup() |
45 { | 38 { |
46 pendingUpdate = true; | 39 if (!pendingBackup) |
47 if (!lastRun) | |
48 { | 40 { |
49 lastRun = performance.now(); | 41 pendingBackup = true; |
50 pendingUpdate = false; | |
51 isSaving = true; | |
52 | 42 |
53 saveToStorage().then(() => | 43 setTimeout( |
54 { | 44 () => |
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 { | 45 { |
69 isSaving = true; | 46 saveToStorage(); |
70 pendingUpdate = false; | 47 pendingBackup = false; |
71 lastRun = performance.now(); | 48 }, |
72 saveToStorage().then(() => | 49 backupDelay |
73 { | 50 ); |
74 isSaving = false; | |
75 lastRun = performance.now(); | |
76 if (pendingUpdate) | |
77 scheduleBackup(); | |
78 }); | |
79 } | |
80 }, MIN_WRITE_INTERVAL - (performance.now() - lastRun)); | |
81 } | 51 } |
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 } | 52 } |
83 | 53 |
84 function saveToStorage() | 54 function saveToStorage() |
85 { | 55 { |
86 return browser.storage.local.set({ | 56 browser.storage.local.set({ |
87 [BACKUP_NAME]: { | 57 [BACKUP_NAME]: { |
88 content: serialize(), | 58 content: serialize(), |
89 lastModified: Date.now() | 59 lastModified: Date.now() |
90 } | 60 } |
91 }); | 61 }); |
92 } | 62 } |
93 | 63 |
94 function serialize() | 64 function serialize() |
95 { | 65 { |
96 let buffer = []; | 66 let buffer = []; |
97 | 67 |
98 for (let subscription of FilterStorage.subscriptions) | 68 for (let subscription of FilterStorage.subscriptions) |
99 { | 69 { |
100 if (subscription instanceof SpecialSubscription) | 70 if (subscription instanceof SpecialSubscription) |
101 { | 71 { |
102 subscription.serialize(buffer); | 72 subscription.serialize(buffer); |
103 buffer.push("[Subscription filters]"); | 73 buffer.push("[Subscription filters]"); |
104 subscription.serializeFilters(buffer); | 74 subscription.serializeFilters(buffer); |
105 } | 75 } |
106 else if (subscription instanceof DownloadableSubscription) | 76 else if (subscription instanceof DownloadableSubscription) |
107 { | 77 { |
108 let {homepage, title, url, disabled} = subscription; | 78 let {homepage, title, url, disabled} = subscription; |
109 | 79 |
110 buffer.push( | 80 buffer.push( |
111 "[Subscription]", | 81 "[Subscription]", |
112 `homepage=${homepage}`, | 82 `homepage=${homepage}`, |
113 `title=${title}`, | 83 `title=${title}`, |
114 `url=${url}`, | 84 `url=${url}`, |
115 `disabled=${disabled}`); | 85 `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.
| |
86 ); | |
116 } | 87 } |
117 } | 88 } |
118 return buffer; | 89 return buffer; |
119 } | 90 } |
120 | 91 |
121 function getBackupData() | 92 function getBackupData() |
122 { | 93 { |
123 return browser.storage.local.get(BACKUP_NAME).then(items => | 94 return browser.storage.local.get(BACKUP_NAME).then(items => |
124 { | 95 { |
125 let entry = items[BACKUP_NAME]; | 96 let entry = items[BACKUP_NAME]; |
(...skipping 11 matching lines...) Expand all Loading... | |
137 FilterNotifier.on("subscription.disabled", scheduleBackup); | 108 FilterNotifier.on("subscription.disabled", scheduleBackup); |
138 FilterNotifier.on("filter.added", scheduleBackup); | 109 FilterNotifier.on("filter.added", scheduleBackup); |
139 FilterNotifier.on("filter.removed", scheduleBackup); | 110 FilterNotifier.on("filter.removed", scheduleBackup); |
140 FilterNotifier.on("filter.moved", scheduleBackup); | 111 FilterNotifier.on("filter.moved", scheduleBackup); |
141 FilterNotifier.on("filter.disabled", scheduleBackup); | 112 FilterNotifier.on("filter.disabled", scheduleBackup); |
142 | 113 |
143 exports.IndexedDBBackup = | 114 exports.IndexedDBBackup = |
144 { | 115 { |
145 getBackupData, | 116 getBackupData, |
146 // Non-public API, just for tests. | 117 // Non-public API, just for tests. |
147 init | 118 setBackupInterval |
148 }; | 119 }; |
LEFT | RIGHT |