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: Modify Safari storage. Refactor chunk name creation. Add comments. Created March 31, 2016, 1:53 p.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
(...skipping 16 matching lines...) Expand all
27 key += file.path; 27 key += file.path;
28 else 28 else
29 key += file.spec; 29 key += file.spec;
30 30
31 if (typeof chunk != "undefined") 31 if (typeof chunk != "undefined")
32 key += ":" + chunk; 32 key += ":" + chunk;
33 33
34 return key; 34 return key;
35 } 35 }
36 36
37 function loadFile(file, successCallback, errorCallback) 37 function loadFile(file)
38 { 38 {
39 let key = fileToKey(file); 39 return new Promise((resolve, reject) =>
40
41 ext.storage.get([key], function(items)
42 { 40 {
43 let entry = items[key]; 41 let key = fileToKey(file);
44 42 ext.storage.get([key], items =>
45 if (entry) 43 {
46 successCallback(entry); 44 let entry = items[key];
47 else 45 if (entry)
48 errorCallback(new Error("File doesn't exist")); 46 resolve(entry);
47 else
48 reject(new Error("File doesn't exist"));
49 });
49 }); 50 });
50 } 51 }
51 52
52 exports.IO = 53 exports.IO =
53 { 54 {
54 resolveFilePath: function(path) 55 resolveFilePath: function(path)
55 { 56 {
56 return new FakeFile(path); 57 return new FakeFile(path);
57 }, 58 },
58 59
(...skipping 20 matching lines...) Expand all
79 for (let key of keys) 80 for (let key of keys)
80 for (let line of items[key]) 81 for (let line of items[key])
81 listener.process(line); 82 listener.process(line);
82 83
83 listener.process(null); 84 listener.process(null);
84 callback(null); 85 callback(null);
85 }); 86 });
86 } 87 }
87 } 88 }
88 89
89 loadFile(file, onLoaded, callback); 90 loadFile(file).then(onLoaded, callback);
90 }, 91 },
91 92
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
93 writeToFile: function(file, data, callback) 93 writeToFile: function(file, data, callback)
94 { 94 {
95 let key = fileToKey(file);
96 let items = {}; 95 let items = {};
97 items[key] = {lastModified: Date.now()}; 96 let entry = items[fileToKey(file)] = {lastModified: Date.now()};
98 97
99 // Edge currently has a limitation of how much data can be stored
100 // using chrome.storage.local.set. The current limit is 1Mb per operation.
101 // Edge uses 'browser' namespace instead of 'chrome'.
102 if (typeof browser == "object") 98 if (typeof browser == "object")
103 { 99 {
104 // In Edge strings are stored in UTF-16 format, so 2 bytes per char 100 loadFile(file).catch(() => null)
Sebastian Noack 2016/03/31 14:08:53 How about mentioning that we reserve another 1000
105 let quota = 1024 * 1024 / 2 - 1000; 101 .then(oldEntry =>
106 let chunks = [];
107 let chunk = [];
108 let chunkSize = 0;
109
110 for (let line of data)
111 { 102 {
112 if (line.length + chunkSize > quota) 103 let quota = 1024 * 1024 / 2 - 1000;
104 let chunks = [];
105 let chunk = [];
106 let chunkSize = 0;
107
108 for (let line of data)
113 { 109 {
114 chunks.push(chunk); 110 if (line.length + chunkSize > quota)
115 chunk = []; 111 {
116 chunkSize = 0; 112 chunks.push(chunk);
113 chunk = [];
114 chunkSize = 0;
115 }
116
117 chunk.push(line);
118 chunkSize += line.length;
117 } 119 }
120 chunks.push(chunk);
118 121
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++) 122 for (let i = 0; i < chunks.length; i++)
127 items[fileToKey(file, i)] = chunks[i]; 123 items[fileToKey(file, i)] = chunks[i];
128 items[key].chunks = chunks.length; 124 entry.chunks = chunks.length;
129 ext.storage.set(items, callback); 125 ext.storage.set(items, callback);
130 return; 126
131 } 127 if (oldEntry && "chunks" in oldEntry)
128 for (let i = entry.chunks; i < oldEntry.chunks; i++)
129 ext.storage.remove(fileToKey(file, i));
130 });
132 } 131 }
133 132 else
134 items[key].content = data; 133 {
135 ext.storage.set(items, callback); 134 entry.content = data;
135 ext.storage.set(items, callback);
136 }
136 }, 137 },
137 138
138 statFile: function(file, callback) 139 statFile: function(file, callback)
139 { 140 {
140 function onLoaded(entry) 141 function onLoaded(entry)
141 { 142 {
142 callback(null, { 143 callback(null, {
143 exists: true, 144 exists: true,
144 lastModified: entry.lastModified 145 lastModified: entry.lastModified
145 }); 146 });
146 } 147 }
147 148
148 loadFile(file, onLoaded, callback); 149 loadFile(file).then(onLoaded, callback);
149 } 150 }
150 }; 151 };
LEFTRIGHT

Powered by Google App Engine
This is Rietveld