LEFT | RIGHT |
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 Loading... |
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 // Make sure we don't leak chunks when saving smaller files in Edge | |
53 ext.storage.onChanged.addListener(function(changes, namespace) { | |
54 if (namespace != 'local') | |
55 return; | |
56 | |
57 for (key in changes) { | |
58 var oldValue = changes[key].oldValue; | |
59 var newValue = changes[key].newValue; | |
60 if (typeof oldValue == "object" && | |
61 typeof oldValue["chunks"] == "number") | |
62 { | |
63 for (let i = newValue.chunks; i < oldValue.chunks; ++i) | |
64 ext.storage.remove(key + ":" + i); | |
65 } | |
66 } | |
67 }); | |
68 | |
69 | 52 |
70 exports.IO = | 53 exports.IO = |
71 { | 54 { |
72 resolveFilePath: function(path) | 55 resolveFilePath: function(path) |
73 { | 56 { |
74 return new FakeFile(path); | 57 return new FakeFile(path); |
75 }, | 58 }, |
76 | 59 |
77 readFromFile: function(file, listener, callback) | 60 readFromFile: function(file, listener, callback) |
78 { | 61 { |
(...skipping 18 matching lines...) Expand all Loading... |
97 for (let key of keys) | 80 for (let key of keys) |
98 for (let line of items[key]) | 81 for (let line of items[key]) |
99 listener.process(line); | 82 listener.process(line); |
100 | 83 |
101 listener.process(null); | 84 listener.process(null); |
102 callback(null); | 85 callback(null); |
103 }); | 86 }); |
104 } | 87 } |
105 } | 88 } |
106 | 89 |
107 loadFile(file, onLoaded, callback); | 90 loadFile(file).then(onLoaded, callback); |
108 }, | 91 }, |
109 | 92 |
110 writeToFile: function(file, data, callback) | 93 writeToFile: function(file, data, callback) |
111 { | 94 { |
112 let key = fileToKey(file); | |
113 let items = {}; | 95 let items = {}; |
114 items[key] = {lastModified: Date.now()}; | 96 let entry = items[fileToKey(file)] = {lastModified: Date.now()}; |
115 | 97 |
116 // Edge currently has a limitation of how much data can be stored | 98 if (typeof browser == "object") |
117 // using chrome.storage.local.set. The current limit is 1Mb per operation. | |
118 // Edge uses 'browser' namespace instead of 'chrome'. | |
119 if (typeof browser != "object") | |
120 { | 99 { |
121 // In Edge strings are stored in UTF-16 format, so 2 bytes per char | 100 loadFile(file).catch(() => null) |
122 // We subtract 1000 bytes for keys and array overhead | 101 .then(oldEntry => |
123 let quota = 1024 * 1024 / 2 - 1000; | |
124 let chunks = []; | |
125 let chunk = []; | |
126 let chunkSize = 0; | |
127 | |
128 for (let line of data) | |
129 { | 102 { |
130 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) |
131 { | 109 { |
132 chunks.push(chunk); | 110 if (line.length + chunkSize > quota) |
133 chunk = []; | 111 { |
134 chunkSize = 0; | 112 chunks.push(chunk); |
| 113 chunk = []; |
| 114 chunkSize = 0; |
| 115 } |
| 116 |
| 117 chunk.push(line); |
| 118 chunkSize += line.length; |
135 } | 119 } |
| 120 chunks.push(chunk); |
136 | 121 |
137 chunk.push(line); | |
138 chunkSize += line.length; | |
139 } | |
140 chunks.push(chunk); | |
141 | |
142 if (chunks.length > 1) | |
143 { | |
144 for (let i = 0; i < chunks.length; i++) | 122 for (let i = 0; i < chunks.length; i++) |
145 items[fileToKey(file, i)] = chunks[i]; | 123 items[fileToKey(file, i)] = chunks[i]; |
146 items[key].chunks = chunks.length; | 124 entry.chunks = chunks.length; |
147 ext.storage.set(items, callback); | 125 ext.storage.set(items, callback); |
148 return; | 126 |
149 } | 127 if (oldEntry && "chunks" in oldEntry) |
| 128 for (let i = entry.chunks; i < oldEntry.chunks; i++) |
| 129 ext.storage.remove(fileToKey(file, i)); |
| 130 }); |
150 } | 131 } |
151 | 132 else |
152 items[key].content = data; | 133 { |
153 ext.storage.set(items, callback); | 134 entry.content = data; |
| 135 ext.storage.set(items, callback); |
| 136 } |
154 }, | 137 }, |
155 | 138 |
156 statFile: function(file, callback) | 139 statFile: function(file, callback) |
157 { | 140 { |
158 function onLoaded(entry) | 141 function onLoaded(entry) |
159 { | 142 { |
160 callback(null, { | 143 callback(null, { |
161 exists: true, | 144 exists: true, |
162 lastModified: entry.lastModified | 145 lastModified: entry.lastModified |
163 }); | 146 }); |
164 } | 147 } |
165 | 148 |
166 loadFile(file, onLoaded, callback); | 149 loadFile(file).then(onLoaded, callback); |
167 }, | 150 } |
168 }; | 151 }; |
LEFT | RIGHT |