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, SpecialSubscription} = | |
23 require("../adblockpluscore/lib/subscriptionClasses"); | |
24 | |
25 const BACKUP_NAME = "file:indexedDB-backup"; | |
26 | |
27 let minWriteInterval; | |
Sebastian Noack
2018/09/05 14:34:49
Nit: This is no longer a "min" but a fixed value.
geo
2018/09/05 15:59:08
Done.
I've also changed the name of `writeInterval
| |
28 let pendingBackup = false; | |
29 | |
30 function setBackupInterval(backupInterval = 60 * 1000) | |
31 { | |
32 minWriteInterval = backupInterval; | |
33 } | |
34 | |
35 setBackupInterval(); | |
36 | |
37 function scheduleBackup() | |
38 { | |
39 if (!pendingBackup) | |
40 { | |
41 pendingBackup = true; | |
42 | |
43 setTimeout( | |
44 () => | |
45 { | |
46 saveToStorage(); | |
47 pendingBackup = false; | |
48 }, | |
49 minWriteInterval | |
50 ); | |
51 } | |
52 } | |
53 | |
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() | |
65 { | |
66 let buffer = []; | |
67 | |
68 for (let subscription of FilterStorage.subscriptions) | |
69 { | |
70 if (subscription instanceof SpecialSubscription) | |
71 { | |
72 subscription.serialize(buffer); | |
73 buffer.push("[Subscription filters]"); | |
74 subscription.serializeFilters(buffer); | |
75 } | |
76 else if (subscription instanceof DownloadableSubscription) | |
77 { | |
78 let {homepage, title, url, disabled} = subscription; | |
79 | |
80 buffer.push( | |
81 "[Subscription]", | |
82 `homepage=${homepage}`, | |
83 `title=${title}`, | |
84 `url=${url}`, | |
85 `disabled=${disabled}` | |
86 ); | |
87 } | |
88 } | |
89 return buffer; | |
90 } | |
91 | |
92 function getBackupData() | |
93 { | |
94 return browser.storage.local.get(BACKUP_NAME).then(items => | |
95 { | |
96 let entry = items[BACKUP_NAME]; | |
97 if (entry) | |
98 return entry; | |
99 | |
100 throw {type: "NoSuchFile"}; | |
101 }); | |
102 } | |
103 | |
104 FilterNotifier.on("load", scheduleBackup); | |
105 FilterNotifier.on("subscription.updated", scheduleBackup); | |
106 FilterNotifier.on("subscription.added", scheduleBackup); | |
107 FilterNotifier.on("subscription.removed", scheduleBackup); | |
108 FilterNotifier.on("subscription.disabled", scheduleBackup); | |
109 FilterNotifier.on("filter.added", scheduleBackup); | |
110 FilterNotifier.on("filter.removed", scheduleBackup); | |
111 FilterNotifier.on("filter.moved", scheduleBackup); | |
112 FilterNotifier.on("filter.disabled", scheduleBackup); | |
113 | |
114 exports.IndexedDBBackup = | |
115 { | |
116 getBackupData, | |
117 // Non-public API, just for tests. | |
118 setBackupInterval | |
119 }; | |
OLD | NEW |