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.<!WebInspector.TempFile>} | |
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 { |
86 if (!writer.length) { | |
87 file._writer = writer; | |
88 return Promise.resolve(file); | |
89 } | |
90 | |
78 /** | 91 /** |
79 * @this {WebInspector.TempFile} | 92 * @param {function(?)} fulfill |
93 * @param {function(*)} reject | |
80 */ | 94 */ |
81 function didTruncate(e) | 95 function truncate(fulfill, reject) |
82 { | 96 { |
83 this._writer = writer; | 97 writer.onwriteend = fulfill; |
98 writer.onerror = reject; | |
99 writer.truncate(0); | |
100 } | |
101 | |
102 function didTruncate() | |
103 { | |
104 file._writer = writer; | |
84 writer.onwriteend = null; | 105 writer.onwriteend = null; |
85 writer.onerror = null; | 106 writer.onerror = null; |
86 callback(this); | 107 return Promise.resolve(file); |
87 } | 108 } |
88 | 109 |
89 function onTruncateError(e) | 110 function onTruncateError(e) |
90 { | 111 { |
91 WebInspector.console.error("Failed to truncate temp file " + e.code + " : " + e.message); | 112 writer.onwriteend = null; |
92 callback(null); | 113 writer.onerror = null; |
114 throw e; | |
93 } | 115 } |
94 | 116 |
95 if (writer.length) { | 117 return new Promise(truncate) |
96 writer.onwriteend = didTruncate.bind(this); | 118 .then(didTruncate) |
yurys
2014/11/12 10:29:06
.then(didTruncate, onTruncateError);
| |
97 writer.onerror = onTruncateError; | 119 .catch(onTruncateError); |
98 writer.truncate(0); | |
99 } else { | |
100 this._writer = writer; | |
101 callback(this); | |
102 } | |
103 } | 120 } |
104 | 121 |
105 function errorHandler(e) | 122 return WebInspector.TempFile.ensureTempStorageCleared() |
106 { | 123 .then(requestTempFileSystem) |
107 WebInspector.console.error("Failed to create temp file " + e.code + " : " + e.message); | 124 .then(getDirectoryEntry) |
108 callback(null); | 125 .then(getFileEntry) |
109 } | 126 .then(createFileWriter) |
110 | 127 .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 } | 128 } |
120 | 129 |
121 WebInspector.TempFile.prototype = { | 130 WebInspector.TempFile.prototype = { |
122 /** | 131 /** |
123 * @param {!Array.<string>} strings | 132 * @param {!Array.<string>} strings |
124 * @param {!function(boolean)} callback | 133 * @param {!function(boolean)} callback |
125 */ | 134 */ |
126 write: function(strings, callback) | 135 write: function(strings, callback) |
127 { | 136 { |
128 var blob = new Blob(strings, {type: 'text/plain'}); | 137 var blob = new Blob(strings, {type: 'text/plain'}); |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
219 } | 228 } |
220 } | 229 } |
221 | 230 |
222 /** | 231 /** |
223 * @constructor | 232 * @constructor |
224 * @param {!string} dirPath | 233 * @param {!string} dirPath |
225 * @param {!string} name | 234 * @param {!string} name |
226 */ | 235 */ |
227 WebInspector.DeferredTempFile = function(dirPath, name) | 236 WebInspector.DeferredTempFile = function(dirPath, name) |
228 { | 237 { |
238 /** @type {?Array.<string>} */ | |
229 this._chunks = []; | 239 this._chunks = []; |
230 this._tempFile = null; | 240 this._tempFile = null; |
231 this._isWriting = false; | 241 this._isWriting = false; |
232 this._finishCallback = null; | 242 this._finishCallback = null; |
233 this._finishedWriting = false; | 243 this._finishedWriting = false; |
234 this._callsPendingOpen = []; | 244 this._callsPendingOpen = []; |
235 this._pendingReads = []; | 245 this._pendingReads = []; |
236 new WebInspector.TempFile(dirPath, name, this._didCreateTempFile.bind(this)) ; | 246 WebInspector.TempFile.create(dirPath, name) |
247 .then(this._didCreateTempFile.bind(this), this._failedToCreateTempFile.b ind(this)); | |
237 } | 248 } |
238 | 249 |
239 WebInspector.DeferredTempFile.prototype = { | 250 WebInspector.DeferredTempFile.prototype = { |
240 /** | 251 /** |
241 * @param {!Array.<string>} strings | 252 * @param {!Array.<string>} strings |
242 */ | 253 */ |
243 write: function(strings) | 254 write: function(strings) |
244 { | 255 { |
245 if (!this._chunks) | 256 if (!this._chunks) |
246 return; | 257 return; |
247 if (this._finishCallback) | 258 if (this._finishCallback) |
248 throw new Error("No writes are allowed after close."); | 259 throw new Error("No writes are allowed after close."); |
249 this._chunks.push.apply(this._chunks, strings); | 260 this._chunks.push.apply(this._chunks, strings); |
250 if (this._tempFile && !this._isWriting) | 261 if (this._tempFile && !this._isWriting) |
251 this._writeNextChunk(); | 262 this._writeNextChunk(); |
252 }, | 263 }, |
253 | 264 |
254 /** | 265 /** |
255 * @param {!function(?WebInspector.TempFile)} callback | 266 * @param {!function(?WebInspector.TempFile)} callback |
256 */ | 267 */ |
257 finishWriting: function(callback) | 268 finishWriting: function(callback) |
258 { | 269 { |
259 this._finishCallback = callback; | 270 this._finishCallback = callback; |
260 if (this._finishedWriting) | 271 if (this._finishedWriting) |
261 callback(this._tempFile); | 272 callback(this._tempFile); |
262 else if (!this._isWriting && !this._chunks.length) | 273 else if (!this._isWriting && !this._chunks.length) |
263 this._notifyFinished(); | 274 this._notifyFinished(); |
264 }, | 275 }, |
265 | 276 |
277 /** | |
278 * @param {*} e | |
279 */ | |
280 _failedToCreateTempFile: function(e) | |
281 { | |
282 WebInspector.console.error("Failed to create temp file " + e.code + " : " + e.message); | |
283 this._chunks = null; | |
284 this._notifyFinished(); | |
285 }, | |
286 | |
287 /** | |
288 * @param {!WebInspector.TempFile} tempFile | |
289 */ | |
266 _didCreateTempFile: function(tempFile) | 290 _didCreateTempFile: function(tempFile) |
267 { | 291 { |
268 this._tempFile = tempFile; | 292 this._tempFile = tempFile; |
269 var callsPendingOpen = this._callsPendingOpen; | 293 var callsPendingOpen = this._callsPendingOpen; |
270 this._callsPendingOpen = null; | 294 this._callsPendingOpen = null; |
271 for (var i = 0; i < callsPendingOpen.length; ++i) | 295 for (var i = 0; i < callsPendingOpen.length; ++i) |
272 callsPendingOpen[i](); | 296 callsPendingOpen[i](); |
273 if (!tempFile) { | |
274 this._chunks = null; | |
275 this._notifyFinished(); | |
276 return; | |
277 } | |
278 if (this._chunks.length) | 297 if (this._chunks.length) |
279 this._writeNextChunk(); | 298 this._writeNextChunk(); |
280 }, | 299 }, |
281 | 300 |
282 _writeNextChunk: function() | 301 _writeNextChunk: function() |
283 { | 302 { |
284 var chunks = this._chunks; | 303 var chunks = this._chunks; |
285 this._chunks = []; | 304 this._chunks = []; |
286 this._isWriting = true; | 305 this._isWriting = true; |
287 this._tempFile.write(chunks, this._didWriteChunk.bind(this)); | 306 this._tempFile.write(/** @type {!Array.<string>} */(chunks), this._didWr iteChunk.bind(this)); |
288 }, | 307 }, |
289 | 308 |
290 _didWriteChunk: function(success) | 309 _didWriteChunk: function(success) |
291 { | 310 { |
292 this._isWriting = false; | 311 this._isWriting = false; |
293 if (!success) { | 312 if (!success) { |
294 this._tempFile = null; | 313 this._tempFile = null; |
295 this._chunks = null; | 314 this._chunks = null; |
296 this._notifyFinished(); | 315 this._notifyFinished(); |
297 return; | 316 return; |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
352 if (this._callsPendingOpen) { | 371 if (this._callsPendingOpen) { |
353 this._callsPendingOpen.push(this.remove.bind(this)); | 372 this._callsPendingOpen.push(this.remove.bind(this)); |
354 return; | 373 return; |
355 } | 374 } |
356 if (this._tempFile) | 375 if (this._tempFile) |
357 this._tempFile.remove(); | 376 this._tempFile.remove(); |
358 } | 377 } |
359 } | 378 } |
360 | 379 |
361 /** | 380 /** |
362 * @constructor | 381 * @param {function(?)} fulfill |
382 * @param {function(*)} reject | |
363 */ | 383 */ |
364 WebInspector.TempStorageCleaner = function() | 384 WebInspector.TempFile._clearTempStorage = function(fulfill, reject) |
365 { | 385 { |
386 /** | |
387 * @param {!Event} event | |
388 */ | |
389 function handleError(event) | |
390 { | |
391 WebInspector.console.error(WebInspector.UIString("Failed to clear temp s torage: %s", event.data)); | |
392 reject(event.data); | |
393 } | |
394 | |
395 /** | |
396 * @param {!Event} event | |
397 */ | |
398 function handleMessage(event) | |
399 { | |
400 if (event.data.type === "tempStorageCleared") { | |
401 if (event.data.error) | |
402 WebInspector.console.error(event.data.error); | |
403 else | |
404 fulfill(undefined); | |
405 return; | |
406 } | |
407 reject(event.data); | |
408 } | |
409 | |
366 try { | 410 try { |
367 this._worker = new WorkerRuntime.Worker("temp_storage_shared_worker", "T empStorageCleaner"); | 411 var worker = new WorkerRuntime.Worker("temp_storage_shared_worker", "Tem pStorageCleaner"); |
368 this._worker.onerror = this._handleError.bind(this); | 412 worker.onerror = handleError; |
369 this._callbacks = []; | 413 worker.port.onmessage = handleMessage; |
370 this._worker.port.onmessage = this._handleMessage.bind(this); | 414 worker.port.onerror = handleError; |
371 this._worker.port.onerror = this._handleError.bind(this); | |
372 } catch (e) { | 415 } catch (e) { |
373 if (e.name === "URLMismatchError") | 416 if (e.name === "URLMismatchError") |
374 console.log("Shared worker wasn't started due to url difference. " + e); | 417 console.log("Shared worker wasn't started due to url difference. " + e); |
375 else | 418 else |
376 throw e; | 419 throw e; |
377 } | 420 } |
378 } | 421 } |
379 | 422 |
380 WebInspector.TempStorageCleaner.prototype = { | 423 /** |
381 /** | 424 * @return {!Promise.<undefined>} |
382 * @param {!function()} callback | 425 */ |
383 */ | 426 WebInspector.TempFile.ensureTempStorageCleared = function() |
384 ensureStorageCleared: function(callback) | 427 { |
385 { | 428 if (!WebInspector.TempFile._storageCleanerPromise) |
386 if (this._callbacks) | 429 WebInspector.TempFile._storageCleanerPromise = new Promise(WebInspector. TempFile._clearTempStorage); |
387 this._callbacks.push(callback); | 430 return WebInspector.TempFile._storageCleanerPromise; |
388 else | |
389 callback(); | |
390 }, | |
391 | |
392 _handleMessage: function(event) | |
393 { | |
394 if (event.data.type === "tempStorageCleared") { | |
395 if (event.data.error) | |
396 WebInspector.console.error(event.data.error); | |
397 this._notifyCallbacks(); | |
398 } | |
399 }, | |
400 | |
401 _handleError: function(event) | |
402 { | |
403 WebInspector.console.error(WebInspector.UIString("Failed to clear temp s torage: %s", event.data)); | |
404 this._notifyCallbacks(); | |
405 }, | |
406 | |
407 _notifyCallbacks: function() | |
408 { | |
409 var callbacks = this._callbacks; | |
410 this._callbacks = null; | |
411 for (var i = 0; i < callbacks.length; i++) | |
412 callbacks[i](); | |
413 } | |
414 } | 431 } |
415 | |
416 /** | |
417 * @param {!function()} callback | |
418 */ | |
419 WebInspector.TempFile._ensureTempStorageCleared = function(callback) | |
420 { | |
421 if (!WebInspector.TempFile._storageCleaner) | |
422 WebInspector.TempFile._storageCleaner = new WebInspector.TempStorageClea ner(); | |
423 WebInspector.TempFile._storageCleaner.ensureStorageCleared(callback); | |
424 } | |
OLD | NEW |