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 Aug. 21, 2018, 4:40 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");
25
26
27 const BACKUP_NAME = "file:indexedDB-backup";
28 let MIN_WRITE_INTERVAL;
29 let pendingUpdate;
30 let isSaving;
31 let lastBackup;
32 let lastRun;
33 let saveToStorage;
34
35 function init(backupInterval, saveFunction)
36 {
37 clearInterval(lastBackup);
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 }
53
54 init();
55
56 function scheduleBackup()
57 {
58 pendingUpdate = true;
59 if (!lastRun)
60 {
61 lastRun = performance.now();
62 pendingUpdate = false;
63 isSaving = true;
64
65 saveToStorage().then(() =>
66 {
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 {
81 isSaving = true;
82 pendingUpdate = false;
83 lastRun = performance.now();
84 saveToStorage().then(() =>
85 {
86 isSaving = false;
87 lastRun = performance.now();
88 if (pendingUpdate)
89 scheduleBackup();
90 });
91 }
92 }, MIN_WRITE_INTERVAL - (performance.now() - lastRun));
93 }
94 }
95
96 function serialize(subscriptions = FilterStorage.subscriptions)
97 {
98 let buffer = [];
99 for (let subscription of subscriptions)
100 {
101 if (subscription instanceof SpecialSubscription)
102 {
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);
105 buffer.push("", "[Subscription filters]");
106 subscription.serializeFilters(buffer);
107 }
108 else if (subscription instanceof DownloadableSubscription)
109 {
110 let {homepage, title, url, disabled} = subscription;
111
112 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]",
115 `homepage=${homepage}`,
116 `title=${title}`,
117 `url=${url}`,
118 `disabled=${disabled}`,
119 );
120 }
121 }
122 return buffer;
123 }
124
125 function getBackupData()
126 {
127 return browser.storage.local.get(BACKUP_NAME).then(items =>
128 {
129 let entry = items[BACKUP_NAME];
130 if (entry)
131 return entry;
132
133 throw {type: "NoSuchFile"};
134 });
135 }
136
137 FilterNotifier.on("load", scheduleBackup);
138 FilterNotifier.on("subscription.updated", scheduleBackup);
139 FilterNotifier.on("subscription.added", scheduleBackup);
140 FilterNotifier.on("subscription.removed", scheduleBackup);
141 FilterNotifier.on("subscription.disabled", scheduleBackup);
142 FilterNotifier.on("filter.added", scheduleBackup);
143 FilterNotifier.on("filter.removed", scheduleBackup);
144 FilterNotifier.on("filter.moved", scheduleBackup);
145 FilterNotifier.on("filter.disabled", scheduleBackup);
146
147 exports.IndexedDBBackup =
148 {
149 getBackupData,
150 // Non-public API, just for tests.
151 init,
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 };
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