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

Delta Between Two Patch Sets: lib/io.js

Issue 29339112: Issue 3716 - Split up files stored in storage.local (Closed)
Left Patch Set: Remove setMultiple. Use suggested code. Created March 31, 2016, 9:51 a.m.
Right Patch Set: Remove the trailing comma. Created April 1, 2016, 1:14 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « chrome/ext/background.js ('k') | lib/prefs.js » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 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";
19
18 const keyPrefix = "file:"; 20 const keyPrefix = "file:";
19 21
20 function fileToKey(file) 22 function fileToKey(file, chunk)
21 { 23 {
22 return keyPrefix + (file instanceof FakeFile ? file.path : file.spec); 24 let key = keyPrefix;
25
26 if (file instanceof FakeFile)
27 key += file.path;
28 else
29 key += file.spec;
30
31 if (typeof chunk != "undefined")
32 key += ":" + chunk;
33
34 return key;
23 } 35 }
24 36
25 function loadFile(file, successCallback, errorCallback) 37 function loadFile(file)
26 { 38 {
27 let key = fileToKey(file); 39 return new Promise((resolve, reject) =>
28
29 ext.storage.get([key], function(items)
30 { 40 {
31 let entry = items[key]; 41 let key = fileToKey(file);
32 42 ext.storage.get([key], items =>
33 if (entry) 43 {
34 successCallback(entry); 44 let entry = items[key];
35 else 45 if (entry)
36 errorCallback(new Error("File doesn't exist")); 46 resolve(entry);
47 else
48 reject(new Error("File doesn't exist"));
49 });
37 }); 50 });
38 }
39
40 // Save the file, splitting it into chunks, if required.
41 function saveFile(file, data, callback)
42 {
43 let key = fileToKey(file);
44 let items = {};
45 items[key] = {lastModified: Date.now()};
46
47 if (typeof browser == "object")
Sebastian Noack 2016/03/31 12:36:54 A comment that this accounts for Edge and how the
Sebastian Noack 2016/03/31 14:08:53 I just noticed that we potentially leak chunks whe
48 {
49 let quota = 1024 * 1024 / 2 - 1000;
50 let chunks = [];
51 let chunk = [];
52 let chunkSize = 0;
53
54 for (let line of data)
55 {
56 if (line.length + chunkSize > quota)
57 {
58 chunks.push(chunk);
59 chunk = [];
60 chunkSize = 0;
61 }
62
63 chunk.push(line);
64 chunkSize += line.length;
65 }
66 chunks.push(chunk);
67
68 if (chunks.length > 1)
69 {
70 for (let i = 0; i < chunks.length; i++)
71 items[key + ":" + i] = chunks[i];
72 items[key].chunks = chunks.length;
73 ext.storage.set(items, callback);
74 return;
75 }
76 }
77
78 items[key].content = data;
79 ext.storage.set(items, callback);
80 } 51 }
81 52
82 exports.IO = 53 exports.IO =
83 { 54 {
84 resolveFilePath: function(path) 55 resolveFilePath: function(path)
85 { 56 {
86 return new FakeFile(path); 57 return new FakeFile(path);
87 }, 58 },
88 59
89 readFromFile: function(file, listener, callback) 60 readFromFile: function(file, listener, callback)
90 { 61 {
91 function onLoaded(entry) 62 function onLoaded(entry)
92 { 63 {
93 if ("content" in entry) 64 if ("content" in entry)
94 { 65 {
95 for (let line of entry.content) 66 for (let line of entry.content)
96 listener.process(line); 67 listener.process(line);
97 68
98 listener.process(null); 69 listener.process(null);
99 callback(null); 70 callback(null);
100 } 71 }
101 else 72 else
102 { 73 {
103 let keys = []; 74 let keys = [];
104 for (let i = 0; i < entry.chunks; i++) 75 for (let i = 0; i < entry.chunks; i++)
105 keys.push(fileToKey(file) + ":" + i); 76 keys.push(fileToKey(file, i));
Sebastian Noack 2016/03/31 12:36:54 How about moving the logic to create those keys in
106 77
107 ext.storage.get(keys, items => 78 ext.storage.get(keys, items =>
108 { 79 {
109 for (let key of keys) 80 for (let key of keys)
110 for (let line of items[key]) 81 for (let line of items[key])
111 listener.process(line); 82 listener.process(line);
112 83
113 listener.process(null); 84 listener.process(null);
114 callback(null); 85 callback(null);
115 }); 86 });
116 } 87 }
117 } 88 }
118 89
119 loadFile(file, onLoaded, callback); 90 loadFile(file).then(onLoaded, callback);
120 }, 91 },
121 92
122 writeToFile: function(file, data, callback) 93 writeToFile: function(file, data, callback)
123 { 94 {
124 saveFile(file, data, callback); 95 let items = {};
96 let entry = items[fileToKey(file)] = {lastModified: Date.now()};
97
98 if (typeof browser == "object")
99 {
100 loadFile(file).catch(() => null)
101 .then(oldEntry =>
102 {
103 let quota = 1024 * 1024 / 2 - 1000;
104 let chunks = [];
105 let chunk = [];
106 let chunkSize = 0;
107
108 for (let line of data)
109 {
110 if (line.length + chunkSize > quota)
111 {
112 chunks.push(chunk);
113 chunk = [];
114 chunkSize = 0;
115 }
116
117 chunk.push(line);
118 chunkSize += line.length;
119 }
120 chunks.push(chunk);
121
122 for (let i = 0; i < chunks.length; i++)
123 items[fileToKey(file, i)] = chunks[i];
124 entry.chunks = chunks.length;
125 ext.storage.set(items, callback);
126
127 if (oldEntry && "chunks" in oldEntry)
128 for (let i = entry.chunks; i < oldEntry.chunks; i++)
129 ext.storage.remove(fileToKey(file, i));
130 });
131 }
132 else
133 {
134 entry.content = data;
135 ext.storage.set(items, callback);
136 }
125 }, 137 },
126 138
127 statFile: function(file, callback) 139 statFile: function(file, callback)
128 { 140 {
129 function onLoaded(entry) 141 function onLoaded(entry)
130 { 142 {
131 callback(null, { 143 callback(null, {
132 exists: true, 144 exists: true,
133 lastModified: entry.lastModified 145 lastModified: entry.lastModified
134 }); 146 });
135 } 147 }
136 148
137 loadFile(file, onLoaded, callback); 149 loadFile(file).then(onLoaded, callback);
138 } 150 }
139 }; 151 };
LEFTRIGHT

Powered by Google App Engine
This is Rietveld