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

Side by Side Diff: lib/io.js

Issue 29339112: Issue 3716 - Split up files stored in storage.local (Closed)
Patch Set: Modify Safari storage. Refactor chunk name creation. Add comments. Created March 31, 2016, 1:53 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
OLDNEW
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, successCallback, errorCallback)
26 { 38 {
27 let key = fileToKey(file); 39 let key = fileToKey(file);
28 40
29 ext.storage.get([key], function(items) 41 ext.storage.get([key], function(items)
30 { 42 {
31 let entry = items[key]; 43 let entry = items[key];
32 44
33 if (entry) 45 if (entry)
34 successCallback(entry); 46 successCallback(entry);
35 else 47 else
36 errorCallback(new Error("File doesn't exist")); 48 errorCallback(new Error("File doesn't exist"));
37 }); 49 });
38 } 50 }
39 51
40 function saveFile(file, data, callback)
41 {
42 ext.storage.set(
43 fileToKey(file),
44 {
45 content: data,
46 lastModified: Date.now()
47 },
48 callback
49 );
50 }
51
52 exports.IO = 52 exports.IO =
53 { 53 {
54 resolveFilePath: function(path) 54 resolveFilePath: function(path)
55 { 55 {
56 return new FakeFile(path); 56 return new FakeFile(path);
57 }, 57 },
58 58
59 readFromFile: function(file, listener, callback) 59 readFromFile: function(file, listener, callback)
60 { 60 {
61 function onLoaded(entry) 61 function onLoaded(entry)
62 { 62 {
63 for (let line of entry.content) 63 if ("content" in entry)
64 listener.process(line); 64 {
65 for (let line of entry.content)
66 listener.process(line);
65 67
66 listener.process(null); 68 listener.process(null);
67 callback(null); 69 callback(null);
70 }
71 else
72 {
73 let keys = [];
74 for (let i = 0; i < entry.chunks; i++)
75 keys.push(fileToKey(file, i));
76
77 ext.storage.get(keys, items =>
78 {
79 for (let key of keys)
80 for (let line of items[key])
81 listener.process(line);
82
83 listener.process(null);
84 callback(null);
85 });
86 }
68 } 87 }
69 88
70 loadFile(file, onLoaded, callback); 89 loadFile(file, onLoaded, callback);
71 }, 90 },
72 91
92 // Write to file, splitting into chunks, if required.
Sebastian Noack 2016/03/31 14:08:53 Nit: This comment is kinda redundant with the comm
73 writeToFile: function(file, data, callback) 93 writeToFile: function(file, data, callback)
74 { 94 {
75 saveFile(file, data, callback); 95 let key = fileToKey(file);
76 }, 96 let items = {};
97 items[key] = {lastModified: Date.now()};
77 98
78 copyFile: function(fromFile, toFile, callback) 99 // Edge currently has a limitation of how much data can be stored
79 { 100 // using chrome.storage.local.set. The current limit is 1Mb per operation.
80 function onLoaded(entry) 101 // Edge uses 'browser' namespace instead of 'chrome'.
102 if (typeof browser == "object")
81 { 103 {
82 saveFile(toFile, entry.content, callback); 104 // In Edge strings are stored in UTF-16 format, so 2 bytes per char
Sebastian Noack 2016/03/31 14:08:53 How about mentioning that we reserve another 1000
105 let quota = 1024 * 1024 / 2 - 1000;
106 let chunks = [];
107 let chunk = [];
108 let chunkSize = 0;
109
110 for (let line of data)
111 {
112 if (line.length + chunkSize > quota)
113 {
114 chunks.push(chunk);
115 chunk = [];
116 chunkSize = 0;
117 }
118
119 chunk.push(line);
120 chunkSize += line.length;
121 }
122 chunks.push(chunk);
123
124 if (chunks.length > 1)
125 {
126 for (let i = 0; i < chunks.length; i++)
127 items[fileToKey(file, i)] = chunks[i];
128 items[key].chunks = chunks.length;
129 ext.storage.set(items, callback);
130 return;
131 }
83 } 132 }
84 133
85 loadFile(fromFile, onLoaded, callback); 134 items[key].content = data;
86 }, 135 ext.storage.set(items, callback);
87
88 renameFile: function(fromFile, newName, callback)
89 {
90 function onLoaded()
91 {
92 ext.storage.remove(fileToKey(fromFile), function()
93 {
94 ext.storage.set(keyPrefix + newName, entry, callback);
95 });
96 }
97
98 loadFile(fromFile, onLoaded, callback);
99 },
100
101 removeFile: function(file, callback)
102 {
103 ext.storage.remove(fileToKey(file), callback);
104 }, 136 },
105 137
106 statFile: function(file, callback) 138 statFile: function(file, callback)
107 { 139 {
108 function onLoaded(entry) 140 function onLoaded(entry)
109 { 141 {
110 callback(null, { 142 callback(null, {
111 exists: true, 143 exists: true,
112 lastModified: entry.lastModified 144 lastModified: entry.lastModified
113 }); 145 });
114 } 146 }
115 147
116 loadFile(file, onLoaded, callback); 148 loadFile(file, onLoaded, callback);
117 } 149 }
118 }; 150 };
OLDNEW
« no previous file with comments | « chrome/ext/background.js ('k') | lib/prefs.js » ('j') | safari/ext/background.js » ('J')

Powered by Google App Engine
This is Rietveld