Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: lib/indexedDBBackup.js

Issue 29860578: Issue 6775 - Work around filter data stored in IndexedDB getting lost on Microsoft Edge (Closed)
Patch Set: Created Sept. 4, 2018, 4:07 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | lib/ioIndexedDB.js » ('j') | lib/ioIndexedDB.js » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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");
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.
25
26 const BACKUP_NAME = "file:indexedDB-backup";
27
28 let minWriteInterval;
29 let lastBackup;
30 let lastRun = -minWriteInterval;
31
32 function setBackupInterval(backupInterval = 60 * 1000)
33 {
34 minWriteInterval = backupInterval;
35 }
36
37 setBackupInterval();
38
39 function scheduleBackup()
40 {
41 if (!lastBackup)
42 {
43 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.
44 () =>
45 {
46 saveToStorage();
47 lastBackup = null;
48 lastRun = performance.now();
49 },
50 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
51 );
52 }
53 }
54
55 function saveToStorage()
56 {
57 browser.storage.local.set({
58 [BACKUP_NAME]: {
59 content: serialize(),
60 lastModified: Date.now()
61 }
62 });
63 }
64
65 function serialize()
66 {
67 let buffer = [];
68
69 for (let subscription of FilterStorage.subscriptions)
70 {
71 if (subscription instanceof SpecialSubscription)
72 {
73 subscription.serialize(buffer);
74 buffer.push("[Subscription filters]");
75 subscription.serializeFilters(buffer);
76 }
77 else if (subscription instanceof DownloadableSubscription)
78 {
79 let {homepage, title, url, disabled} = subscription;
80
81 buffer.push(
82 "[Subscription]",
83 `homepage=${homepage}`,
84 `title=${title}`,
85 `url=${url}`,
86 `disabled=${disabled}`
87 );
88 }
89 }
90 return buffer;
91 }
92
93 function getBackupData()
94 {
95 return browser.storage.local.get(BACKUP_NAME).then(items =>
96 {
97 let entry = items[BACKUP_NAME];
98 if (entry)
99 return entry;
100
101 throw {type: "NoSuchFile"};
102 });
103 }
104
105 FilterNotifier.on("load", scheduleBackup);
106 FilterNotifier.on("subscription.updated", scheduleBackup);
107 FilterNotifier.on("subscription.added", scheduleBackup);
108 FilterNotifier.on("subscription.removed", scheduleBackup);
109 FilterNotifier.on("subscription.disabled", scheduleBackup);
110 FilterNotifier.on("filter.added", scheduleBackup);
111 FilterNotifier.on("filter.removed", scheduleBackup);
112 FilterNotifier.on("filter.moved", scheduleBackup);
113 FilterNotifier.on("filter.disabled", scheduleBackup);
114
115 exports.IndexedDBBackup =
116 {
117 getBackupData,
118 // Non-public API, just for tests.
119 setBackupInterval
120 };
OLDNEW
« no previous file with comments | « no previous file | lib/ioIndexedDB.js » ('j') | lib/ioIndexedDB.js » ('J')

Powered by Google App Engine
This is Rietveld