OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
11 * copyright notice, this list of conditions and the following disclaimer | 11 * copyright notice, this list of conditions and the following disclaimer |
12 * in the documentation and/or other materials provided with the | 12 * in the documentation and/or other materials provided with the |
13 * distribution. | 13 * distribution. |
14 * * Neither the name of Google Inc. nor the names of its | 14 * * Neither the name of Google Inc. nor the names of its |
15 * contributors may be used to endorse or promote products derived from | 15 * contributors may be used to endorse or promote products derived from |
16 * this software without specific prior written permission. | 16 * this software without specific prior written permission. |
17 * | 17 * |
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 */ | 29 */ |
30 | |
31 window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileS
ystem; | 30 window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileS
ystem; |
32 | 31 |
33 /** | 32 /** |
34 * @constructor | 33 * @unrestricted |
35 */ | 34 */ |
36 WebInspector.TempFile = function() | 35 WebInspector.TempFile = class { |
37 { | 36 constructor() { |
38 this._fileEntry = null; | 37 this._fileEntry = null; |
39 this._writer = null; | 38 this._writer = null; |
40 }; | 39 } |
41 | 40 |
42 /** | 41 /** |
43 * @param {string} dirPath | 42 * @param {string} dirPath |
44 * @param {string} name | 43 * @param {string} name |
45 * @return {!Promise.<!WebInspector.TempFile>} | 44 * @return {!Promise.<!WebInspector.TempFile>} |
46 */ | 45 */ |
47 WebInspector.TempFile.create = function(dirPath, name) | 46 static create(dirPath, name) { |
48 { | |
49 var file = new WebInspector.TempFile(); | 47 var file = new WebInspector.TempFile(); |
50 | 48 |
51 function requestTempFileSystem() | 49 function requestTempFileSystem() { |
52 { | 50 return new Promise(window.requestFileSystem.bind(window, window.TEMPORARY,
10)); |
53 return new Promise(window.requestFileSystem.bind(window, window.TEMPORAR
Y, 10)); | |
54 } | 51 } |
55 | 52 |
56 /** | 53 /** |
57 * @param {!FileSystem} fs | 54 * @param {!FileSystem} fs |
58 */ | 55 */ |
59 function getDirectoryEntry(fs) | 56 function getDirectoryEntry(fs) { |
60 { | 57 return new Promise(fs.root.getDirectory.bind(fs.root, dirPath, {create: tr
ue})); |
61 return new Promise(fs.root.getDirectory.bind(fs.root, dirPath, { create:
true })); | |
62 } | 58 } |
63 | 59 |
64 /** | 60 /** |
65 * @param {!DirectoryEntry} dir | 61 * @param {!DirectoryEntry} dir |
66 */ | 62 */ |
67 function getFileEntry(dir) | 63 function getFileEntry(dir) { |
68 { | 64 return new Promise(dir.getFile.bind(dir, name, {create: true})); |
69 return new Promise(dir.getFile.bind(dir, name, { create: true })); | |
70 } | 65 } |
71 | 66 |
72 /** | 67 /** |
73 * @param {!FileEntry} fileEntry | 68 * @param {!FileEntry} fileEntry |
74 */ | 69 */ |
75 function createFileWriter(fileEntry) | 70 function createFileWriter(fileEntry) { |
76 { | 71 file._fileEntry = fileEntry; |
77 file._fileEntry = fileEntry; | 72 return new Promise(fileEntry.createWriter.bind(fileEntry)); |
78 return new Promise(fileEntry.createWriter.bind(fileEntry)); | |
79 } | 73 } |
80 | 74 |
81 /** | 75 /** |
82 * @param {!FileWriter} writer | 76 * @param {!FileWriter} writer |
83 */ | 77 */ |
84 function truncateFile(writer) | 78 function truncateFile(writer) { |
85 { | 79 if (!writer.length) { |
86 if (!writer.length) { | 80 file._writer = writer; |
87 file._writer = writer; | 81 return Promise.resolve(file); |
88 return Promise.resolve(file); | 82 } |
89 } | |
90 | 83 |
91 /** | 84 /** |
92 * @param {function(?)} fulfill | 85 * @param {function(?)} fulfill |
93 * @param {function(*)} reject | 86 * @param {function(*)} reject |
94 */ | 87 */ |
95 function truncate(fulfill, reject) | 88 function truncate(fulfill, reject) { |
96 { | 89 writer.onwriteend = fulfill; |
97 writer.onwriteend = fulfill; | 90 writer.onerror = reject; |
98 writer.onerror = reject; | 91 writer.truncate(0); |
99 writer.truncate(0); | 92 } |
100 } | |
101 | 93 |
102 function didTruncate() | 94 function didTruncate() { |
103 { | 95 file._writer = writer; |
104 file._writer = writer; | 96 writer.onwriteend = null; |
105 writer.onwriteend = null; | 97 writer.onerror = null; |
106 writer.onerror = null; | 98 return Promise.resolve(file); |
107 return Promise.resolve(file); | 99 } |
108 } | |
109 | 100 |
110 function onTruncateError(e) | 101 function onTruncateError(e) { |
111 { | 102 writer.onwriteend = null; |
112 writer.onwriteend = null; | 103 writer.onerror = null; |
113 writer.onerror = null; | 104 throw e; |
114 throw e; | 105 } |
115 } | |
116 | 106 |
117 return new Promise(truncate).then(didTruncate, onTruncateError); | 107 return new Promise(truncate).then(didTruncate, onTruncateError); |
118 } | 108 } |
119 | 109 |
120 return WebInspector.TempFile.ensureTempStorageCleared() | 110 return WebInspector.TempFile.ensureTempStorageCleared() |
121 .then(requestTempFileSystem) | 111 .then(requestTempFileSystem) |
122 .then(getDirectoryEntry) | 112 .then(getDirectoryEntry) |
123 .then(getFileEntry) | 113 .then(getFileEntry) |
124 .then(createFileWriter) | 114 .then(createFileWriter) |
125 .then(truncateFile); | 115 .then(truncateFile); |
| 116 } |
| 117 |
| 118 /** |
| 119 * @return {!Promise.<undefined>} |
| 120 */ |
| 121 static ensureTempStorageCleared() { |
| 122 if (!WebInspector.TempFile._storageCleanerPromise) { |
| 123 WebInspector.TempFile._storageCleanerPromise = |
| 124 WebInspector.serviceManager.createAppService('utility_shared_worker',
'TempStorage', true).then(service => { |
| 125 if (service) |
| 126 return service.send('clear'); |
| 127 }); |
| 128 } |
| 129 return WebInspector.TempFile._storageCleanerPromise; |
| 130 } |
| 131 |
| 132 /** |
| 133 * @param {!Array.<string>} strings |
| 134 * @param {function(number)} callback |
| 135 */ |
| 136 write(strings, callback) { |
| 137 var blob = new Blob(strings, {type: 'text/plain'}); |
| 138 this._writer.onerror = function(e) { |
| 139 WebInspector.console.error('Failed to write into a temp file: ' + e.target
.error.message); |
| 140 callback(-1); |
| 141 }; |
| 142 this._writer.onwriteend = function(e) { |
| 143 callback(e.target.length); |
| 144 }; |
| 145 this._writer.write(blob); |
| 146 } |
| 147 |
| 148 finishWriting() { |
| 149 this._writer = null; |
| 150 } |
| 151 |
| 152 /** |
| 153 * @param {function(?string)} callback |
| 154 */ |
| 155 read(callback) { |
| 156 this.readRange(undefined, undefined, callback); |
| 157 } |
| 158 |
| 159 /** |
| 160 * @param {number|undefined} startOffset |
| 161 * @param {number|undefined} endOffset |
| 162 * @param {function(?string)} callback |
| 163 */ |
| 164 readRange(startOffset, endOffset, callback) { |
| 165 /** |
| 166 * @param {!Blob} file |
| 167 */ |
| 168 function didGetFile(file) { |
| 169 var reader = new FileReader(); |
| 170 |
| 171 if (typeof startOffset === 'number' || typeof endOffset === 'number') |
| 172 file = file.slice(/** @type {number} */ (startOffset), /** @type {number
} */ (endOffset)); |
| 173 /** |
| 174 * @this {FileReader} |
| 175 */ |
| 176 reader.onloadend = function(e) { |
| 177 callback(/** @type {?string} */ (this.result)); |
| 178 }; |
| 179 reader.onerror = function(error) { |
| 180 WebInspector.console.error('Failed to read from temp file: ' + error.mes
sage); |
| 181 }; |
| 182 reader.readAsText(file); |
| 183 } |
| 184 function didFailToGetFile(error) { |
| 185 WebInspector.console.error('Failed to load temp file: ' + error.message); |
| 186 callback(null); |
| 187 } |
| 188 this._fileEntry.file(didGetFile, didFailToGetFile); |
| 189 } |
| 190 |
| 191 /** |
| 192 * @param {!WebInspector.OutputStream} outputStream |
| 193 * @param {!WebInspector.OutputStreamDelegate} delegate |
| 194 */ |
| 195 copyToOutputStream(outputStream, delegate) { |
| 196 /** |
| 197 * @param {!File} file |
| 198 */ |
| 199 function didGetFile(file) { |
| 200 var reader = new WebInspector.ChunkedFileReader(file, 10 * 1000 * 1000, de
legate); |
| 201 reader.start(outputStream); |
| 202 } |
| 203 |
| 204 function didFailToGetFile(error) { |
| 205 WebInspector.console.error('Failed to load temp file: ' + error.message); |
| 206 outputStream.close(); |
| 207 } |
| 208 |
| 209 this._fileEntry.file(didGetFile, didFailToGetFile); |
| 210 } |
| 211 |
| 212 remove() { |
| 213 if (this._fileEntry) |
| 214 this._fileEntry.remove(function() {}); |
| 215 } |
126 }; | 216 }; |
127 | 217 |
128 WebInspector.TempFile.prototype = { | |
129 /** | |
130 * @param {!Array.<string>} strings | |
131 * @param {function(number)} callback | |
132 */ | |
133 write: function(strings, callback) | |
134 { | |
135 var blob = new Blob(strings, {type: "text/plain"}); | |
136 this._writer.onerror = function(e) | |
137 { | |
138 WebInspector.console.error("Failed to write into a temp file: " + e.
target.error.message); | |
139 callback(-1); | |
140 }; | |
141 this._writer.onwriteend = function(e) | |
142 { | |
143 callback(e.target.length); | |
144 }; | |
145 this._writer.write(blob); | |
146 }, | |
147 | |
148 finishWriting: function() | |
149 { | |
150 this._writer = null; | |
151 }, | |
152 | |
153 /** | |
154 * @param {function(?string)} callback | |
155 */ | |
156 read: function(callback) | |
157 { | |
158 this.readRange(undefined, undefined, callback); | |
159 }, | |
160 | |
161 /** | |
162 * @param {number|undefined} startOffset | |
163 * @param {number|undefined} endOffset | |
164 * @param {function(?string)} callback | |
165 */ | |
166 readRange: function(startOffset, endOffset, callback) | |
167 { | |
168 /** | |
169 * @param {!Blob} file | |
170 */ | |
171 function didGetFile(file) | |
172 { | |
173 var reader = new FileReader(); | |
174 | |
175 if (typeof startOffset === "number" || typeof endOffset === "number"
) | |
176 file = file.slice(/** @type {number} */ (startOffset), /** @type
{number} */ (endOffset)); | |
177 /** | |
178 * @this {FileReader} | |
179 */ | |
180 reader.onloadend = function(e) | |
181 { | |
182 callback(/** @type {?string} */ (this.result)); | |
183 }; | |
184 reader.onerror = function(error) | |
185 { | |
186 WebInspector.console.error("Failed to read from temp file: " + e
rror.message); | |
187 }; | |
188 reader.readAsText(file); | |
189 } | |
190 function didFailToGetFile(error) | |
191 { | |
192 WebInspector.console.error("Failed to load temp file: " + error.mess
age); | |
193 callback(null); | |
194 } | |
195 this._fileEntry.file(didGetFile, didFailToGetFile); | |
196 }, | |
197 | |
198 /** | |
199 * @param {!WebInspector.OutputStream} outputStream | |
200 * @param {!WebInspector.OutputStreamDelegate} delegate | |
201 */ | |
202 copyToOutputStream: function(outputStream, delegate) | |
203 { | |
204 /** | |
205 * @param {!File} file | |
206 */ | |
207 function didGetFile(file) | |
208 { | |
209 var reader = new WebInspector.ChunkedFileReader(file, 10 * 1000 * 10
00, delegate); | |
210 reader.start(outputStream); | |
211 } | |
212 | |
213 function didFailToGetFile(error) | |
214 { | |
215 WebInspector.console.error("Failed to load temp file: " + error.mess
age); | |
216 outputStream.close(); | |
217 } | |
218 | |
219 this._fileEntry.file(didGetFile, didFailToGetFile); | |
220 }, | |
221 | |
222 remove: function() | |
223 { | |
224 if (this._fileEntry) | |
225 this._fileEntry.remove(function() {}); | |
226 } | |
227 }; | |
228 | 218 |
229 /** | 219 /** |
230 * @constructor | 220 * @unrestricted |
231 * @param {string} dirPath | |
232 * @param {string} name | |
233 */ | 221 */ |
234 WebInspector.DeferredTempFile = function(dirPath, name) | 222 WebInspector.DeferredTempFile = class { |
235 { | 223 /** |
| 224 * @param {string} dirPath |
| 225 * @param {string} name |
| 226 */ |
| 227 constructor(dirPath, name) { |
236 /** @type {!Array.<!{strings: !Array.<string>, callback: ?function(number)}>
} */ | 228 /** @type {!Array.<!{strings: !Array.<string>, callback: ?function(number)}>
} */ |
237 this._chunks = []; | 229 this._chunks = []; |
238 this._tempFile = null; | 230 this._tempFile = null; |
239 this._isWriting = false; | 231 this._isWriting = false; |
240 this._finishCallback = null; | 232 this._finishCallback = null; |
241 this._finishedWriting = false; | 233 this._finishedWriting = false; |
242 this._callsPendingOpen = []; | 234 this._callsPendingOpen = []; |
243 this._pendingReads = []; | 235 this._pendingReads = []; |
244 WebInspector.TempFile.create(dirPath, name) | 236 WebInspector.TempFile.create(dirPath, name) |
245 .then(this._didCreateTempFile.bind(this), this._failedToCreateTempFile.b
ind(this)); | 237 .then(this._didCreateTempFile.bind(this), this._failedToCreateTempFile.b
ind(this)); |
| 238 } |
| 239 |
| 240 /** |
| 241 * @param {!Array.<string>} strings |
| 242 * @param {function(number)=} callback |
| 243 */ |
| 244 write(strings, callback) { |
| 245 if (this._finishCallback) |
| 246 throw new Error('No writes are allowed after close.'); |
| 247 this._chunks.push({strings: strings, callback: callback || null}); |
| 248 if (this._tempFile && !this._isWriting) |
| 249 this._writeNextChunk(); |
| 250 } |
| 251 |
| 252 /** |
| 253 * @param {function(?WebInspector.TempFile)} callback |
| 254 */ |
| 255 finishWriting(callback) { |
| 256 this._finishCallback = callback; |
| 257 if (this._finishedWriting) |
| 258 callback(this._tempFile); |
| 259 else if (!this._isWriting && !this._chunks.length) |
| 260 this._notifyFinished(); |
| 261 } |
| 262 |
| 263 /** |
| 264 * @param {*} e |
| 265 */ |
| 266 _failedToCreateTempFile(e) { |
| 267 WebInspector.console.error('Failed to create temp file ' + e.code + ' : ' +
e.message); |
| 268 this._notifyFinished(); |
| 269 } |
| 270 |
| 271 /** |
| 272 * @param {!WebInspector.TempFile} tempFile |
| 273 */ |
| 274 _didCreateTempFile(tempFile) { |
| 275 this._tempFile = tempFile; |
| 276 var callsPendingOpen = this._callsPendingOpen; |
| 277 this._callsPendingOpen = null; |
| 278 for (var i = 0; i < callsPendingOpen.length; ++i) |
| 279 callsPendingOpen[i](); |
| 280 if (this._chunks.length) |
| 281 this._writeNextChunk(); |
| 282 } |
| 283 |
| 284 _writeNextChunk() { |
| 285 // File was deleted while create or write was in-flight. |
| 286 if (!this._tempFile) |
| 287 return; |
| 288 var chunk = this._chunks.shift(); |
| 289 this._isWriting = true; |
| 290 this._tempFile.write( |
| 291 /** @type {!Array.<string>} */ (chunk.strings), this._didWriteChunk.bind
(this, chunk.callback)); |
| 292 } |
| 293 |
| 294 /** |
| 295 * @param {?function(number)} callback |
| 296 * @param {number} size |
| 297 */ |
| 298 _didWriteChunk(callback, size) { |
| 299 this._isWriting = false; |
| 300 if (size === -1) { |
| 301 this._tempFile = null; |
| 302 this._notifyFinished(); |
| 303 return; |
| 304 } |
| 305 if (callback) |
| 306 callback(size); |
| 307 if (this._chunks.length) |
| 308 this._writeNextChunk(); |
| 309 else if (this._finishCallback) |
| 310 this._notifyFinished(); |
| 311 } |
| 312 |
| 313 _notifyFinished() { |
| 314 this._finishedWriting = true; |
| 315 if (this._tempFile) |
| 316 this._tempFile.finishWriting(); |
| 317 var chunks = this._chunks; |
| 318 this._chunks = []; |
| 319 for (var i = 0; i < chunks.length; ++i) { |
| 320 if (chunks[i].callback) |
| 321 chunks[i].callback(-1); |
| 322 } |
| 323 if (this._finishCallback) |
| 324 this._finishCallback(this._tempFile); |
| 325 var pendingReads = this._pendingReads; |
| 326 this._pendingReads = []; |
| 327 for (var i = 0; i < pendingReads.length; ++i) |
| 328 pendingReads[i](); |
| 329 } |
| 330 |
| 331 /** |
| 332 * @param {number|undefined} startOffset |
| 333 * @param {number|undefined} endOffset |
| 334 * @param {function(string?)} callback |
| 335 */ |
| 336 readRange(startOffset, endOffset, callback) { |
| 337 if (!this._finishedWriting) { |
| 338 this._pendingReads.push(this.readRange.bind(this, startOffset, endOffset,
callback)); |
| 339 return; |
| 340 } |
| 341 if (!this._tempFile) { |
| 342 callback(null); |
| 343 return; |
| 344 } |
| 345 this._tempFile.readRange(startOffset, endOffset, callback); |
| 346 } |
| 347 |
| 348 /** |
| 349 * @param {!WebInspector.OutputStream} outputStream |
| 350 * @param {!WebInspector.OutputStreamDelegate} delegate |
| 351 */ |
| 352 copyToOutputStream(outputStream, delegate) { |
| 353 if (!this._finishedWriting) { |
| 354 this._pendingReads.push(this.copyToOutputStream.bind(this, outputStream, d
elegate)); |
| 355 return; |
| 356 } |
| 357 if (this._tempFile) |
| 358 this._tempFile.copyToOutputStream(outputStream, delegate); |
| 359 } |
| 360 |
| 361 remove() { |
| 362 if (this._callsPendingOpen) { |
| 363 this._callsPendingOpen.push(this.remove.bind(this)); |
| 364 return; |
| 365 } |
| 366 if (this._tempFile) |
| 367 this._tempFile.remove(); |
| 368 this._tempFile = null; |
| 369 } |
246 }; | 370 }; |
247 | 371 |
248 WebInspector.DeferredTempFile.prototype = { | |
249 /** | |
250 * @param {!Array.<string>} strings | |
251 * @param {function(number)=} callback | |
252 */ | |
253 write: function(strings, callback) | |
254 { | |
255 if (this._finishCallback) | |
256 throw new Error("No writes are allowed after close."); | |
257 this._chunks.push({strings: strings, callback: callback || null}); | |
258 if (this._tempFile && !this._isWriting) | |
259 this._writeNextChunk(); | |
260 }, | |
261 | |
262 /** | |
263 * @param {function(?WebInspector.TempFile)} callback | |
264 */ | |
265 finishWriting: function(callback) | |
266 { | |
267 this._finishCallback = callback; | |
268 if (this._finishedWriting) | |
269 callback(this._tempFile); | |
270 else if (!this._isWriting && !this._chunks.length) | |
271 this._notifyFinished(); | |
272 }, | |
273 | |
274 /** | |
275 * @param {*} e | |
276 */ | |
277 _failedToCreateTempFile: function(e) | |
278 { | |
279 WebInspector.console.error("Failed to create temp file " + e.code + " :
" + e.message); | |
280 this._notifyFinished(); | |
281 }, | |
282 | |
283 /** | |
284 * @param {!WebInspector.TempFile} tempFile | |
285 */ | |
286 _didCreateTempFile: function(tempFile) | |
287 { | |
288 this._tempFile = tempFile; | |
289 var callsPendingOpen = this._callsPendingOpen; | |
290 this._callsPendingOpen = null; | |
291 for (var i = 0; i < callsPendingOpen.length; ++i) | |
292 callsPendingOpen[i](); | |
293 if (this._chunks.length) | |
294 this._writeNextChunk(); | |
295 }, | |
296 | |
297 _writeNextChunk: function() | |
298 { | |
299 // File was deleted while create or write was in-flight. | |
300 if (!this._tempFile) | |
301 return; | |
302 var chunk = this._chunks.shift(); | |
303 this._isWriting = true; | |
304 this._tempFile.write(/** @type {!Array.<string>} */(chunk.strings), this
._didWriteChunk.bind(this, chunk.callback)); | |
305 }, | |
306 | |
307 /** | |
308 * @param {?function(number)} callback | |
309 * @param {number} size | |
310 */ | |
311 _didWriteChunk: function(callback, size) | |
312 { | |
313 this._isWriting = false; | |
314 if (size === -1) { | |
315 this._tempFile = null; | |
316 this._notifyFinished(); | |
317 return; | |
318 } | |
319 if (callback) | |
320 callback(size); | |
321 if (this._chunks.length) | |
322 this._writeNextChunk(); | |
323 else if (this._finishCallback) | |
324 this._notifyFinished(); | |
325 }, | |
326 | |
327 _notifyFinished: function() | |
328 { | |
329 this._finishedWriting = true; | |
330 if (this._tempFile) | |
331 this._tempFile.finishWriting(); | |
332 var chunks = this._chunks; | |
333 this._chunks = []; | |
334 for (var i = 0; i < chunks.length; ++i) { | |
335 if (chunks[i].callback) | |
336 chunks[i].callback(-1); | |
337 } | |
338 if (this._finishCallback) | |
339 this._finishCallback(this._tempFile); | |
340 var pendingReads = this._pendingReads; | |
341 this._pendingReads = []; | |
342 for (var i = 0; i < pendingReads.length; ++i) | |
343 pendingReads[i](); | |
344 }, | |
345 | |
346 /** | |
347 * @param {number|undefined} startOffset | |
348 * @param {number|undefined} endOffset | |
349 * @param {function(string?)} callback | |
350 */ | |
351 readRange: function(startOffset, endOffset, callback) | |
352 { | |
353 if (!this._finishedWriting) { | |
354 this._pendingReads.push(this.readRange.bind(this, startOffset, endOf
fset, callback)); | |
355 return; | |
356 } | |
357 if (!this._tempFile) { | |
358 callback(null); | |
359 return; | |
360 } | |
361 this._tempFile.readRange(startOffset, endOffset, callback); | |
362 }, | |
363 | |
364 /** | |
365 * @param {!WebInspector.OutputStream} outputStream | |
366 * @param {!WebInspector.OutputStreamDelegate} delegate | |
367 */ | |
368 copyToOutputStream: function(outputStream, delegate) | |
369 { | |
370 if (!this._finishedWriting) { | |
371 this._pendingReads.push(this.copyToOutputStream.bind(this, outputStr
eam, delegate)); | |
372 return; | |
373 } | |
374 if (this._tempFile) | |
375 this._tempFile.copyToOutputStream(outputStream, delegate); | |
376 }, | |
377 | |
378 remove: function() | |
379 { | |
380 if (this._callsPendingOpen) { | |
381 this._callsPendingOpen.push(this.remove.bind(this)); | |
382 return; | |
383 } | |
384 if (this._tempFile) | |
385 this._tempFile.remove(); | |
386 this._tempFile = null; | |
387 } | |
388 }; | |
389 | 372 |
390 /** | 373 /** |
391 * @return {!Promise.<undefined>} | 374 * @implements {WebInspector.BackingStorage} |
| 375 * @unrestricted |
392 */ | 376 */ |
393 WebInspector.TempFile.ensureTempStorageCleared = function() | 377 WebInspector.TempFileBackingStorage = class { |
394 { | 378 /** |
395 if (!WebInspector.TempFile._storageCleanerPromise) { | 379 * @param {string} dirName |
396 WebInspector.TempFile._storageCleanerPromise = WebInspector.serviceManag
er.createAppService("utility_shared_worker", "TempStorage", true).then(service =
> { | 380 */ |
397 if (service) | 381 constructor(dirName) { |
398 return service.send("clear"); | |
399 }); | |
400 } | |
401 return WebInspector.TempFile._storageCleanerPromise; | |
402 }; | |
403 | |
404 /** | |
405 * @constructor | |
406 * @implements {WebInspector.BackingStorage} | |
407 * @param {string} dirName | |
408 */ | |
409 WebInspector.TempFileBackingStorage = function(dirName) | |
410 { | |
411 this._dirName = dirName; | 382 this._dirName = dirName; |
412 this.reset(); | 383 this.reset(); |
| 384 } |
| 385 |
| 386 /** |
| 387 * @override |
| 388 * @param {string} string |
| 389 */ |
| 390 appendString(string) { |
| 391 this._strings.push(string); |
| 392 this._stringsLength += string.length; |
| 393 var flushStringLength = 10 * 1024 * 1024; |
| 394 if (this._stringsLength > flushStringLength) |
| 395 this._flush(false); |
| 396 } |
| 397 |
| 398 /** |
| 399 * @override |
| 400 * @param {string} string |
| 401 * @return {function():!Promise.<?string>} |
| 402 */ |
| 403 appendAccessibleString(string) { |
| 404 this._flush(false); |
| 405 this._strings.push(string); |
| 406 var chunk = /** @type {!WebInspector.TempFileBackingStorage.Chunk} */ (this.
_flush(true)); |
| 407 |
| 408 /** |
| 409 * @param {!WebInspector.TempFileBackingStorage.Chunk} chunk |
| 410 * @param {!WebInspector.DeferredTempFile} file |
| 411 * @return {!Promise.<?string>} |
| 412 */ |
| 413 function readString(chunk, file) { |
| 414 if (chunk.string) |
| 415 return /** @type {!Promise.<?string>} */ (Promise.resolve(chunk.string))
; |
| 416 |
| 417 console.assert(chunk.endOffset); |
| 418 if (!chunk.endOffset) |
| 419 return Promise.reject('Nor string nor offset to the string in the file w
ere found.'); |
| 420 |
| 421 /** |
| 422 * @param {function(?string)} fulfill |
| 423 * @param {function(*)} reject |
| 424 */ |
| 425 function readRange(fulfill, reject) { |
| 426 // FIXME: call reject for null strings. |
| 427 file.readRange(chunk.startOffset, chunk.endOffset, fulfill); |
| 428 } |
| 429 |
| 430 return new Promise(readRange); |
| 431 } |
| 432 |
| 433 return readString.bind(null, chunk, this._file); |
| 434 } |
| 435 |
| 436 /** |
| 437 * @param {boolean} createChunk |
| 438 * @return {?WebInspector.TempFileBackingStorage.Chunk} |
| 439 */ |
| 440 _flush(createChunk) { |
| 441 if (!this._strings.length) |
| 442 return null; |
| 443 |
| 444 var chunk = null; |
| 445 if (createChunk) { |
| 446 console.assert(this._strings.length === 1); |
| 447 chunk = {string: this._strings[0], startOffset: 0, endOffset: 0}; |
| 448 } |
| 449 |
| 450 /** |
| 451 * @this {WebInspector.TempFileBackingStorage} |
| 452 * @param {?WebInspector.TempFileBackingStorage.Chunk} chunk |
| 453 * @param {number} fileSize |
| 454 */ |
| 455 function didWrite(chunk, fileSize) { |
| 456 if (fileSize === -1) |
| 457 return; |
| 458 if (chunk) { |
| 459 chunk.startOffset = this._fileSize; |
| 460 chunk.endOffset = fileSize; |
| 461 chunk.string = null; |
| 462 } |
| 463 this._fileSize = fileSize; |
| 464 } |
| 465 |
| 466 this._file.write(this._strings, didWrite.bind(this, chunk)); |
| 467 this._strings = []; |
| 468 this._stringsLength = 0; |
| 469 return chunk; |
| 470 } |
| 471 |
| 472 /** |
| 473 * @override |
| 474 */ |
| 475 finishWriting() { |
| 476 this._flush(false); |
| 477 this._file.finishWriting(function() {}); |
| 478 } |
| 479 |
| 480 /** |
| 481 * @override |
| 482 */ |
| 483 reset() { |
| 484 if (this._file) |
| 485 this._file.remove(); |
| 486 this._file = new WebInspector.DeferredTempFile(this._dirName, String(Date.no
w())); |
| 487 /** |
| 488 * @type {!Array.<string>} |
| 489 */ |
| 490 this._strings = []; |
| 491 this._stringsLength = 0; |
| 492 this._fileSize = 0; |
| 493 } |
| 494 |
| 495 /** |
| 496 * @param {!WebInspector.OutputStream} outputStream |
| 497 * @param {!WebInspector.OutputStreamDelegate} delegate |
| 498 */ |
| 499 writeToStream(outputStream, delegate) { |
| 500 this._file.copyToOutputStream(outputStream, delegate); |
| 501 } |
413 }; | 502 }; |
414 | 503 |
415 /** | 504 /** |
416 * @typedef {{ | 505 * @typedef {{ |
417 * string: ?string, | 506 * string: ?string, |
418 * startOffset: number, | 507 * startOffset: number, |
419 * endOffset: number | 508 * endOffset: number |
420 * }} | 509 * }} |
421 */ | 510 */ |
422 WebInspector.TempFileBackingStorage.Chunk; | 511 WebInspector.TempFileBackingStorage.Chunk; |
423 | |
424 WebInspector.TempFileBackingStorage.prototype = { | |
425 /** | |
426 * @override | |
427 * @param {string} string | |
428 */ | |
429 appendString: function(string) | |
430 { | |
431 this._strings.push(string); | |
432 this._stringsLength += string.length; | |
433 var flushStringLength = 10 * 1024 * 1024; | |
434 if (this._stringsLength > flushStringLength) | |
435 this._flush(false); | |
436 }, | |
437 | |
438 /** | |
439 * @override | |
440 * @param {string} string | |
441 * @return {function():!Promise.<?string>} | |
442 */ | |
443 appendAccessibleString: function(string) | |
444 { | |
445 this._flush(false); | |
446 this._strings.push(string); | |
447 var chunk = /** @type {!WebInspector.TempFileBackingStorage.Chunk} */ (t
his._flush(true)); | |
448 | |
449 /** | |
450 * @param {!WebInspector.TempFileBackingStorage.Chunk} chunk | |
451 * @param {!WebInspector.DeferredTempFile} file | |
452 * @return {!Promise.<?string>} | |
453 */ | |
454 function readString(chunk, file) | |
455 { | |
456 if (chunk.string) | |
457 return /** @type {!Promise.<?string>} */ (Promise.resolve(chunk.
string)); | |
458 | |
459 console.assert(chunk.endOffset); | |
460 if (!chunk.endOffset) | |
461 return Promise.reject("Nor string nor offset to the string in th
e file were found."); | |
462 | |
463 /** | |
464 * @param {function(?string)} fulfill | |
465 * @param {function(*)} reject | |
466 */ | |
467 function readRange(fulfill, reject) | |
468 { | |
469 // FIXME: call reject for null strings. | |
470 file.readRange(chunk.startOffset, chunk.endOffset, fulfill); | |
471 } | |
472 | |
473 return new Promise(readRange); | |
474 } | |
475 | |
476 return readString.bind(null, chunk, this._file); | |
477 }, | |
478 | |
479 /** | |
480 * @param {boolean} createChunk | |
481 * @return {?WebInspector.TempFileBackingStorage.Chunk} | |
482 */ | |
483 _flush: function(createChunk) | |
484 { | |
485 if (!this._strings.length) | |
486 return null; | |
487 | |
488 var chunk = null; | |
489 if (createChunk) { | |
490 console.assert(this._strings.length === 1); | |
491 chunk = { | |
492 string: this._strings[0], | |
493 startOffset: 0, | |
494 endOffset: 0 | |
495 }; | |
496 } | |
497 | |
498 /** | |
499 * @this {WebInspector.TempFileBackingStorage} | |
500 * @param {?WebInspector.TempFileBackingStorage.Chunk} chunk | |
501 * @param {number} fileSize | |
502 */ | |
503 function didWrite(chunk, fileSize) | |
504 { | |
505 if (fileSize === -1) | |
506 return; | |
507 if (chunk) { | |
508 chunk.startOffset = this._fileSize; | |
509 chunk.endOffset = fileSize; | |
510 chunk.string = null; | |
511 } | |
512 this._fileSize = fileSize; | |
513 } | |
514 | |
515 this._file.write(this._strings, didWrite.bind(this, chunk)); | |
516 this._strings = []; | |
517 this._stringsLength = 0; | |
518 return chunk; | |
519 }, | |
520 | |
521 /** | |
522 * @override | |
523 */ | |
524 finishWriting: function() | |
525 { | |
526 this._flush(false); | |
527 this._file.finishWriting(function() {}); | |
528 }, | |
529 | |
530 /** | |
531 * @override | |
532 */ | |
533 reset: function() | |
534 { | |
535 if (this._file) | |
536 this._file.remove(); | |
537 this._file = new WebInspector.DeferredTempFile(this._dirName, String(Dat
e.now())); | |
538 /** | |
539 * @type {!Array.<string>} | |
540 */ | |
541 this._strings = []; | |
542 this._stringsLength = 0; | |
543 this._fileSize = 0; | |
544 }, | |
545 | |
546 /** | |
547 * @param {!WebInspector.OutputStream} outputStream | |
548 * @param {!WebInspector.OutputStreamDelegate} delegate | |
549 */ | |
550 writeToStream: function(outputStream, delegate) | |
551 { | |
552 this._file.copyToOutputStream(outputStream, delegate); | |
553 } | |
554 }; | |
OLD | NEW |