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: Created March 30, 2016, 4:50 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)
34 { 43 {
35 if (typeof entry.content.multipartKeys != "undefined") 44 let entry = items[key];
Sebastian Noack 2016/03/30 12:06:38 Please leave loadFile() untouched. There is no rea
36 { 45 if (entry)
37 var multipartData = []; 46 resolve(entry);
38 ext.storage.get(entry.content.multipartKeys, function(parts)
39 {
40 for (var part in parts)
41 {
42 multipartData = multipartData.concat(parts[part]);
43 }
44 entry.content = multipartData;
45 successCallback(entry);
46 }, errorCallback);
47 }
48 else 47 else
49 { 48 reject(new Error("File doesn't exist"));
50 successCallback(entry); 49 });
51 }
52 }
53 else
54 {
55 errorCallback(new Error("File doesn't exist"));
56 }
57 }); 50 });
58 }
59 // Save the file, splitting it into chunks, if required.
60 function saveFile(file, data, callback)
61 {
62 var storageList = {};
63 // Note: In future this should probably be something like
64 // browser.storage.local.QUOTA_BYTES_PER_ITEM
65 var chunkSize = 104856;
Sebastian Noack 2016/03/30 12:06:37 They told me that each character is two bytes. Mor
Oleksandr 2016/03/31 09:59:54 Yes, strings are UTF-16 in Edge, unlike in Chrome
66 if ((data.length > chunkSize) && (typeof browser == "object"))
67 {
68 for (var dataSize = 0; dataSize < data.length; dataSize += chunkSize)
69 {
70 var key = fileToKey(file) + "_" + dataSize;
71 storageList[key] = data.slice(dataSize, dataSize + chunkSize);
Sebastian Noack 2016/03/30 12:06:38 Note that data is an array of strings, where each
Oleksandr 2016/03/31 09:59:54 Yep. For some reason I did not notice the data com
72 }
73 storageList[fileToKey(file)] = {
74 content: { multipartKeys: Object.keys(storageList) },
Sebastian Noack 2016/03/30 12:06:38 We can easily reconstruct the keys for the chunks.
75 lastModified: Date.now()
76 };
77 }
78 else
79 {
80 storageList[fileToKey(file)] = {
81 content: data,
82 lastModified: Date.now()
83 };
84 }
85 ext.storage.setMultiple(storageList, callback);
86 } 51 }
87 52
88 exports.IO = 53 exports.IO =
89 { 54 {
90 resolveFilePath: function(path) 55 resolveFilePath: function(path)
91 { 56 {
92 return new FakeFile(path); 57 return new FakeFile(path);
93 }, 58 },
94 59
95 readFromFile: function(file, listener, callback) 60 readFromFile: function(file, listener, callback)
96 { 61 {
97 function onLoaded(entry) 62 function onLoaded(entry)
98 { 63 {
99 for (let line of entry.content) 64 if ("content" in entry)
100 listener.process(line); 65 {
66 for (let line of entry.content)
67 listener.process(line);
101 68
102 listener.process(null); 69 listener.process(null);
103 callback(null); 70 callback(null);
71 }
72 else
73 {
74 let keys = [];
75 for (let i = 0; i < entry.chunks; i++)
76 keys.push(fileToKey(file, i));
77
78 ext.storage.get(keys, items =>
79 {
80 for (let key of keys)
81 for (let line of items[key])
82 listener.process(line);
83
84 listener.process(null);
85 callback(null);
86 });
87 }
104 } 88 }
105 89
106 loadFile(file, onLoaded, callback); 90 loadFile(file).then(onLoaded, callback);
107 }, 91 },
108 92
109 writeToFile: function(file, data, callback) 93 writeToFile: function(file, data, callback)
110 { 94 {
111 saveFile(file, data, callback); 95 let items = {};
112 }, 96 let entry = items[fileToKey(file)] = {lastModified: Date.now()};
113 97
114 copyFile: function(fromFile, toFile, callback) 98 if (typeof browser == "object")
115 {
116 function onLoaded(entry)
117 { 99 {
118 saveFile(toFile, entry.content, callback); 100 loadFile(file).catch(() => null)
119 } 101 .then(oldEntry =>
102 {
103 let quota = 1024 * 1024 / 2 - 1000;
104 let chunks = [];
105 let chunk = [];
106 let chunkSize = 0;
120 107
121 loadFile(fromFile, onLoaded, callback); 108 for (let line of data)
122 }, 109 {
110 if (line.length + chunkSize > quota)
111 {
112 chunks.push(chunk);
113 chunk = [];
114 chunkSize = 0;
115 }
123 116
124 renameFile: function(fromFile, newName, callback) 117 chunk.push(line);
125 { 118 chunkSize += line.length;
126 function onLoaded() 119 }
127 { 120 chunks.push(chunk);
128 ext.storage.remove(fileToKey(fromFile), function() 121
129 { 122 for (let i = 0; i < chunks.length; i++)
130 ext.storage.set(keyPrefix + newName, entry, callback); 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));
131 }); 130 });
132 } 131 }
133 132 else
134 loadFile(fromFile, onLoaded, callback); 133 {
135 }, 134 entry.content = data;
136 135 ext.storage.set(items, callback);
137 removeFile: function(file, callback) 136 }
138 {
139 ext.storage.remove(fileToKey(file), callback);
140 }, 137 },
141 138
142 statFile: function(file, callback) 139 statFile: function(file, callback)
143 { 140 {
144 function onLoaded(entry) 141 function onLoaded(entry)
145 { 142 {
146 callback(null, { 143 callback(null, {
147 exists: true, 144 exists: true,
148 lastModified: entry.lastModified 145 lastModified: entry.lastModified
149 }); 146 });
150 } 147 }
151 148
152 loadFile(file, onLoaded, callback); 149 loadFile(file).then(onLoaded, callback);
153 } 150 }
154 }; 151 };
LEFTRIGHT

Powered by Google App Engine
This is Rietveld