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 |
(...skipping 14 matching lines...) Expand all Loading... | |
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 | 30 |
31 window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileS ystem; | 31 window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileS ystem; |
32 | 32 |
33 /** | 33 /** |
34 * @constructor | 34 * @constructor |
35 * @param {!string} dirPath | |
36 * @param {!string} name | |
37 * @param {!function(?WebInspector.TempFile)} callback | |
38 */ | 35 */ |
39 WebInspector.TempFile = function(dirPath, name, callback) | 36 WebInspector.TempFile = function() |
40 { | 37 { |
41 this._fileEntry = null; | 38 this._fileEntry = null; |
42 this._writer = null; | 39 this._writer = null; |
40 } | |
41 | |
42 /** | |
43 * @param {!string} dirPath | |
44 * @param {!string} name | |
45 * @return {!Promise} | |
46 */ | |
47 WebInspector.TempFile.create = function(dirPath, name) | |
48 { | |
49 var file = new WebInspector.TempFile(); | |
50 | |
51 function requestTempFileSystem() | |
52 { | |
53 return new Promise(window.requestFileSystem.bind(window, window.TEMPORAR Y, 10)); | |
54 } | |
43 | 55 |
44 /** | 56 /** |
45 * @param {!FileSystem} fs | 57 * @param {!FileSystem} fs |
46 * @this {WebInspector.TempFile} | |
47 */ | 58 */ |
48 function didInitFs(fs) | 59 function getDirectoryEntry(fs) |
49 { | 60 { |
50 fs.root.getDirectory(dirPath, { create: true }, didGetDir.bind(this), er rorHandler); | 61 return new Promise(fs.root.getDirectory.bind(fs.root, dirPath, { create: true })); |
51 } | 62 } |
52 | 63 |
53 /** | 64 /** |
54 * @param {!DirectoryEntry} dir | 65 * @param {!DirectoryEntry} dir |
55 * @this {WebInspector.TempFile} | |
56 */ | 66 */ |
57 function didGetDir(dir) | 67 function getFileEntry(dir) |
58 { | 68 { |
59 dir.getFile(name, { create: true }, didCreateFile.bind(this), errorHandl er); | 69 return new Promise(dir.getFile.bind(dir, name, { create: true })); |
60 } | 70 } |
61 | 71 |
62 /** | 72 /** |
63 * @param {!FileEntry} fileEntry | 73 * @param {!FileEntry} fileEntry |
64 * @this {WebInspector.TempFile} | |
65 */ | 74 */ |
66 function didCreateFile(fileEntry) | 75 function createFileWriter(fileEntry) |
67 { | 76 { |
68 this._fileEntry = fileEntry; | 77 file._fileEntry = fileEntry; |
69 fileEntry.createWriter(didCreateWriter.bind(this), errorHandler); | 78 return new Promise(fileEntry.createWriter.bind(fileEntry)); |
70 } | 79 } |
71 | 80 |
72 /** | 81 /** |
73 * @param {!FileWriter} writer | 82 * @param {!FileWriter} writer |
74 * @this {WebInspector.TempFile} | |
75 */ | 83 */ |
76 function didCreateWriter(writer) | 84 function truncateFile(writer) |
77 { | 85 { |
78 /** | 86 if (!writer.length) { |
79 * @this {WebInspector.TempFile} | 87 file._writer = writer; |
80 */ | 88 return Promise.resolve(file); |
89 } | |
90 | |
91 function truncate(fulfill, reject) | |
92 { | |
93 writer.onwriteend = fulfill; | |
94 writer.onerror = reject; | |
95 writer.truncate(0); | |
96 } | |
97 | |
81 function didTruncate(e) | 98 function didTruncate(e) |
82 { | 99 { |
83 this._writer = writer; | 100 file._writer = writer; |
84 writer.onwriteend = null; | 101 writer.onwriteend = null; |
85 writer.onerror = null; | 102 writer.onerror = null; |
86 callback(this); | 103 return Promise.resolve(file); |
87 } | 104 } |
88 | 105 |
89 function onTruncateError(e) | 106 function onTruncateError(e) |
90 { | 107 { |
108 writer.onwriteend = null; | |
109 writer.onerror = null; | |
91 WebInspector.console.error("Failed to truncate temp file " + e.code + " : " + e.message); | 110 WebInspector.console.error("Failed to truncate temp file " + e.code + " : " + e.message); |
92 callback(null); | 111 return Promise.reject(e); |
93 } | 112 } |
94 | 113 |
95 if (writer.length) { | 114 return new Promise(truncate) |
96 writer.onwriteend = didTruncate.bind(this); | 115 .then(didTruncate) |
97 writer.onerror = onTruncateError; | 116 .catch(onTruncateError); |
98 writer.truncate(0); | |
99 } else { | |
100 this._writer = writer; | |
101 callback(this); | |
102 } | |
103 } | 117 } |
104 | 118 |
105 function errorHandler(e) | 119 return WebInspector.TempFile.ensureTempStorageCleared() |
106 { | 120 .then(requestTempFileSystem) |
107 WebInspector.console.error("Failed to create temp file " + e.code + " : " + e.message); | 121 .then(getDirectoryEntry) |
108 callback(null); | 122 .then(getFileEntry) |
109 } | 123 .then(createFileWriter) |
110 | 124 .then(truncateFile); |
111 /** | |
112 * @this {WebInspector.TempFile} | |
113 */ | |
114 function didClearTempStorage() | |
115 { | |
116 window.requestFileSystem(window.TEMPORARY, 10, didInitFs.bind(this), err orHandler); | |
117 } | |
118 WebInspector.TempFile._ensureTempStorageCleared(didClearTempStorage.bind(thi s)); | |
119 } | 125 } |
120 | 126 |
121 WebInspector.TempFile.prototype = { | 127 WebInspector.TempFile.prototype = { |
122 /** | 128 /** |
123 * @param {!Array.<string>} strings | 129 * @param {!Array.<string>} strings |
124 * @param {!function(boolean)} callback | 130 * @param {!function(boolean)} callback |
125 */ | 131 */ |
126 write: function(strings, callback) | 132 write: function(strings, callback) |
127 { | 133 { |
128 var blob = new Blob(strings, {type: 'text/plain'}); | 134 var blob = new Blob(strings, {type: 'text/plain'}); |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
226 */ | 232 */ |
227 WebInspector.DeferredTempFile = function(dirPath, name) | 233 WebInspector.DeferredTempFile = function(dirPath, name) |
228 { | 234 { |
229 this._chunks = []; | 235 this._chunks = []; |
230 this._tempFile = null; | 236 this._tempFile = null; |
231 this._isWriting = false; | 237 this._isWriting = false; |
232 this._finishCallback = null; | 238 this._finishCallback = null; |
233 this._finishedWriting = false; | 239 this._finishedWriting = false; |
234 this._callsPendingOpen = []; | 240 this._callsPendingOpen = []; |
235 this._pendingReads = []; | 241 this._pendingReads = []; |
236 new WebInspector.TempFile(dirPath, name, this._didCreateTempFile.bind(this)) ; | 242 WebInspector.TempFile.create(dirPath, name) |
243 .then(this._didCreateTempFile.bind(this), this._failToCreateTempFile.bin d(this)); | |
237 } | 244 } |
238 | 245 |
239 WebInspector.DeferredTempFile.prototype = { | 246 WebInspector.DeferredTempFile.prototype = { |
240 /** | 247 /** |
241 * @param {!Array.<string>} strings | 248 * @param {!Array.<string>} strings |
242 */ | 249 */ |
243 write: function(strings) | 250 write: function(strings) |
244 { | 251 { |
245 if (!this._chunks) | 252 if (!this._chunks) |
246 return; | 253 return; |
247 if (this._finishCallback) | 254 if (this._finishCallback) |
248 throw new Error("No writes are allowed after close."); | 255 throw new Error("No writes are allowed after close."); |
249 this._chunks.push.apply(this._chunks, strings); | 256 this._chunks.push.apply(this._chunks, strings); |
250 if (this._tempFile && !this._isWriting) | 257 if (this._tempFile && !this._isWriting) |
251 this._writeNextChunk(); | 258 this._writeNextChunk(); |
252 }, | 259 }, |
253 | 260 |
254 /** | 261 /** |
255 * @param {!function(?WebInspector.TempFile)} callback | 262 * @param {!function(?WebInspector.TempFile)} callback |
256 */ | 263 */ |
257 finishWriting: function(callback) | 264 finishWriting: function(callback) |
258 { | 265 { |
259 this._finishCallback = callback; | 266 this._finishCallback = callback; |
260 if (this._finishedWriting) | 267 if (this._finishedWriting) |
261 callback(this._tempFile); | 268 callback(this._tempFile); |
262 else if (!this._isWriting && !this._chunks.length) | 269 else if (!this._isWriting && !this._chunks.length) |
263 this._notifyFinished(); | 270 this._notifyFinished(); |
264 }, | 271 }, |
265 | 272 |
273 _failToCreateTempFile: function(e) | |
274 { | |
275 WebInspector.console.error("Failed to create temp file " + e.code + " : " + e.message); | |
276 this._chunks = null; | |
277 this._notifyFinished(); | |
278 }, | |
279 | |
266 _didCreateTempFile: function(tempFile) | 280 _didCreateTempFile: function(tempFile) |
267 { | 281 { |
268 this._tempFile = tempFile; | 282 this._tempFile = tempFile; |
269 var callsPendingOpen = this._callsPendingOpen; | 283 var callsPendingOpen = this._callsPendingOpen; |
270 this._callsPendingOpen = null; | 284 this._callsPendingOpen = null; |
271 for (var i = 0; i < callsPendingOpen.length; ++i) | 285 for (var i = 0; i < callsPendingOpen.length; ++i) |
272 callsPendingOpen[i](); | 286 callsPendingOpen[i](); |
273 if (!tempFile) { | |
274 this._chunks = null; | |
275 this._notifyFinished(); | |
276 return; | |
277 } | |
278 if (this._chunks.length) | 287 if (this._chunks.length) |
279 this._writeNextChunk(); | 288 this._writeNextChunk(); |
280 }, | 289 }, |
281 | 290 |
282 _writeNextChunk: function() | 291 _writeNextChunk: function() |
283 { | 292 { |
284 var chunks = this._chunks; | 293 var chunks = this._chunks; |
285 this._chunks = []; | 294 this._chunks = []; |
286 this._isWriting = true; | 295 this._isWriting = true; |
287 this._tempFile.write(chunks, this._didWriteChunk.bind(this)); | 296 this._tempFile.write(chunks, this._didWriteChunk.bind(this)); |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
351 { | 360 { |
352 if (this._callsPendingOpen) { | 361 if (this._callsPendingOpen) { |
353 this._callsPendingOpen.push(this.remove.bind(this)); | 362 this._callsPendingOpen.push(this.remove.bind(this)); |
354 return; | 363 return; |
355 } | 364 } |
356 if (this._tempFile) | 365 if (this._tempFile) |
357 this._tempFile.remove(); | 366 this._tempFile.remove(); |
358 } | 367 } |
359 } | 368 } |
360 | 369 |
361 /** | 370 WebInspector.TempFile._clearTempStorage = function(fulfill, reject) |
362 * @constructor | |
363 */ | |
364 WebInspector.TempStorageCleaner = function() | |
365 { | 371 { |
366 this._worker = Runtime.startSharedWorker("temp_storage_shared_worker", "Temp Storage"); | 372 var worker = Runtime.startSharedWorker("temp_storage_shared_worker", "TempSt orage"); |
apavlov
2014/11/07 11:06:03
I'm about to promisify the worker startup (see htt
| |
367 this._worker.onerror = this._handleError.bind(this); | |
368 this._callbacks = []; | |
369 this._worker.port.onmessage = this._handleMessage.bind(this); | |
370 this._worker.port.onerror = this._handleError.bind(this); | |
371 } | |
372 | 373 |
373 WebInspector.TempStorageCleaner.prototype = { | 374 function handleError(event) |
apavlov
2014/11/07 11:06:03
Please annotate things while you are here
loislo
2014/11/10 08:56:30
Done.
| |
374 /** | |
375 * @param {!function()} callback | |
376 */ | |
377 ensureStorageCleared: function(callback) | |
378 { | 375 { |
379 if (this._callbacks) | 376 WebInspector.console.error(WebInspector.UIString("Failed to clear temp s torage: %s", event.data)); |
380 this._callbacks.push(callback); | 377 reject(event.data); |
381 else | 378 } |
382 callback(); | |
383 }, | |
384 | 379 |
385 _handleMessage: function(event) | 380 function handleMessage(event) |
386 { | 381 { |
387 if (event.data.type === "tempStorageCleared") { | 382 if (event.data.type === "tempStorageCleared") { |
388 if (event.data.error) | 383 if (event.data.error) |
389 WebInspector.console.error(event.data.error); | 384 WebInspector.console.error(event.data.error); |
390 this._notifyCallbacks(); | 385 else |
386 fulfill(); | |
387 return; | |
391 } | 388 } |
392 }, | 389 reject(event.data); |
390 } | |
393 | 391 |
394 _handleError: function(event) | 392 worker.onerror = handleError; |
395 { | 393 worker.port.onmessage = handleMessage; |
396 WebInspector.console.error(WebInspector.UIString("Failed to clear temp s torage: %s", event.data)); | 394 worker.port.onerror = handleError; |
397 this._notifyCallbacks(); | |
398 }, | |
399 | |
400 _notifyCallbacks: function() | |
401 { | |
402 var callbacks = this._callbacks; | |
403 this._callbacks = null; | |
404 for (var i = 0; i < callbacks.length; i++) | |
405 callbacks[i](); | |
406 } | |
407 } | 395 } |
408 | 396 |
409 /** | 397 /** |
410 * @param {!function()} callback | 398 * @return {!Promise} |
411 */ | 399 */ |
412 WebInspector.TempFile._ensureTempStorageCleared = function(callback) | 400 WebInspector.TempFile.ensureTempStorageCleared = function() |
413 { | 401 { |
414 if (!WebInspector.TempFile._storageCleaner) | 402 if (!WebInspector.TempFile._storageCleanerPromise) |
415 WebInspector.TempFile._storageCleaner = new WebInspector.TempStorageClea ner(); | 403 WebInspector.TempFile._storageCleanerPromise = new Promise(WebInspector. TempFile._clearTempStorage); |
416 WebInspector.TempFile._storageCleaner.ensureStorageCleared(callback); | 404 return WebInspector.TempFile._storageCleanerPromise; |
417 } | 405 } |
OLD | NEW |