Chromium Code Reviews| 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 | |
|
apavlov
2014/11/11 10:11:04
Please never use '!' with non-object types, as the
| |
| 44 * @param {!string} name | |
|
apavlov
2014/11/11 10:11:04
ditto
| |
| 45 * @return {!Promise} | |
|
apavlov
2014/11/11 10:11:04
You need to specify the promise resolution type: {
| |
| 46 */ | |
| 47 WebInspector.TempFile.create = function(dirPath, name) | |
| 48 { | |
| 49 var file = new WebInspector.TempFile(); | |
| 50 | |
| 51 function requestTempFileSystem() | |
|
apavlov
2014/11/11 10:11:04
Please annotate
| |
| 52 { | |
| 53 return new Promise(window.requestFileSystem.bind(window, window.TEMPORAR Y, 10)); | |
|
apavlov
2014/11/11 10:11:05
How are you going to handle requestFileSystem fail
| |
| 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 })); |
|
yurys
2014/11/10 16:29:30
I'd rather make it more verbose and arguably more
loislo
2014/11/10 21:02:17
it would be too wordy.
| |
| 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) |
|
apavlov
2014/11/11 10:11:05
no "get" prefix
| |
| 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) |
|
apavlov
2014/11/11 10:11:04
Please annotate
| |
| 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) |
|
apavlov
2014/11/11 10:11:05
Please annotate
| |
| 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); |
|
yurys
2014/11/11 09:41:04
Let's move all error handling to the client side.
| |
| 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); |
|
yurys
2014/11/10 16:29:30
Where is catch? There should still be errorHandler
loislo
2014/11/10 21:02:17
as far as I understand .catch statement eats the r
| |
| 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)); | |
|
apavlov
2014/11/11 10:11:05
_failedToCreateTempFile
| |
| 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) | |
|
apavlov
2014/11/11 10:11:05
Please annotate
| |
| 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) |
|
apavlov
2014/11/11 10:11:05
Please annotate
| |
| 362 * @constructor | |
| 363 */ | |
| 364 WebInspector.TempStorageCleaner = function() | |
| 365 { | 371 { |
| 372 /** | |
| 373 * @param {!Event} event | |
| 374 */ | |
| 375 function handleError(event) | |
| 376 { | |
| 377 WebInspector.console.error(WebInspector.UIString("Failed to clear temp s torage: %s", event.data)); | |
| 378 reject(event.data); | |
| 379 } | |
| 380 | |
| 381 /** | |
| 382 * @param {!Event} event | |
| 383 */ | |
| 384 function handleMessage(event) | |
| 385 { | |
| 386 if (event.data.type === "tempStorageCleared") { | |
| 387 if (event.data.error) | |
| 388 WebInspector.console.error(event.data.error); | |
| 389 else | |
| 390 fulfill(); | |
| 391 return; | |
| 392 } | |
| 393 reject(event.data); | |
| 394 } | |
| 395 | |
| 366 try { | 396 try { |
| 367 this._worker = Runtime.startSharedWorker("temp_storage_shared_worker", " TempStorageCleaner"); | 397 var worker = Runtime.startSharedWorker("temp_storage_shared_worker", "Te mpStorageCleaner"); |
|
apavlov
2014/11/11 10:11:05
Please rebase, as our workers now start asynchrono
| |
| 368 this._worker.onerror = this._handleError.bind(this); | 398 worker.onerror = handleError; |
| 369 this._callbacks = []; | 399 worker.port.onmessage = handleMessage; |
| 370 this._worker.port.onmessage = this._handleMessage.bind(this); | 400 worker.port.onerror = handleError; |
| 371 this._worker.port.onerror = this._handleError.bind(this); | |
| 372 } catch (e) { | 401 } catch (e) { |
| 373 if (e.name === "URLMismatchError") | 402 if (e.name === "URLMismatchError") |
| 374 console.log("Shared worker wasn't started due to url difference. " + e); | 403 console.log("Shared worker wasn't started due to url difference. " + e); |
| 375 else | 404 else |
| 376 throw e; | 405 throw e; |
| 377 } | 406 } |
| 378 } | 407 } |
| 379 | 408 |
| 380 WebInspector.TempStorageCleaner.prototype = { | 409 /** |
| 381 /** | 410 * @return {!Promise} |
|
apavlov
2014/11/11 10:11:05
Please specify the Promise resolution type. If the
| |
| 382 * @param {!function()} callback | 411 */ |
| 383 */ | 412 WebInspector.TempFile.ensureTempStorageCleared = function() |
| 384 ensureStorageCleared: function(callback) | 413 { |
| 385 { | 414 if (!WebInspector.TempFile._storageCleanerPromise) |
| 386 if (this._callbacks) | 415 WebInspector.TempFile._storageCleanerPromise = new Promise(WebInspector. TempFile._clearTempStorage); |
| 387 this._callbacks.push(callback); | 416 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 } | 417 } |
| 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 |