Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(58)

Side by Side Diff: Source/devtools/front_end/bindings/TempFile.js

Issue 705063004: DevTools: Promisify WebInspector.TempFile (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: comments addressed Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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).then(didTruncate, onTruncateError);
96 writer.onwriteend = didTruncate.bind(this);
97 writer.onerror = onTruncateError;
98 writer.truncate(0);
99 } else {
100 this._writer = writer;
101 callback(this);
102 }
103 } 118 }
104 119
105 function errorHandler(e) 120 return WebInspector.TempFile.ensureTempStorageCleared()
106 { 121 .then(requestTempFileSystem)
107 WebInspector.console.error("Failed to create temp file " + e.code + " : " + e.message); 122 .then(getDirectoryEntry)
108 callback(null); 123 .then(getFileEntry)
109 } 124 .then(createFileWriter)
110 125 .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 } 126 }
120 127
121 WebInspector.TempFile.prototype = { 128 WebInspector.TempFile.prototype = {
122 /** 129 /**
123 * @param {!Array.<string>} strings 130 * @param {!Array.<string>} strings
124 * @param {!function(boolean)} callback 131 * @param {!function(boolean)} callback
125 */ 132 */
126 write: function(strings, callback) 133 write: function(strings, callback)
127 { 134 {
128 var blob = new Blob(strings, {type: 'text/plain'}); 135 var blob = new Blob(strings, {type: 'text/plain'});
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
219 } 226 }
220 } 227 }
221 228
222 /** 229 /**
223 * @constructor 230 * @constructor
224 * @param {!string} dirPath 231 * @param {!string} dirPath
225 * @param {!string} name 232 * @param {!string} name
226 */ 233 */
227 WebInspector.DeferredTempFile = function(dirPath, name) 234 WebInspector.DeferredTempFile = function(dirPath, name)
228 { 235 {
236 /** @type {?Array.<string>} */
229 this._chunks = []; 237 this._chunks = [];
230 this._tempFile = null; 238 this._tempFile = null;
231 this._isWriting = false; 239 this._isWriting = false;
232 this._finishCallback = null; 240 this._finishCallback = null;
233 this._finishedWriting = false; 241 this._finishedWriting = false;
234 this._callsPendingOpen = []; 242 this._callsPendingOpen = [];
235 this._pendingReads = []; 243 this._pendingReads = [];
236 new WebInspector.TempFile(dirPath, name, this._didCreateTempFile.bind(this)) ; 244 WebInspector.TempFile.create(dirPath, name)
245 .then(this._didCreateTempFile.bind(this), this._failedToCreateTempFile.b ind(this));
237 } 246 }
238 247
239 WebInspector.DeferredTempFile.prototype = { 248 WebInspector.DeferredTempFile.prototype = {
240 /** 249 /**
241 * @param {!Array.<string>} strings 250 * @param {!Array.<string>} strings
242 */ 251 */
243 write: function(strings) 252 write: function(strings)
244 { 253 {
245 if (!this._chunks) 254 if (!this._chunks)
246 return; 255 return;
247 if (this._finishCallback) 256 if (this._finishCallback)
248 throw new Error("No writes are allowed after close."); 257 throw new Error("No writes are allowed after close.");
249 this._chunks.push.apply(this._chunks, strings); 258 this._chunks.push.apply(this._chunks, strings);
250 if (this._tempFile && !this._isWriting) 259 if (this._tempFile && !this._isWriting)
251 this._writeNextChunk(); 260 this._writeNextChunk();
252 }, 261 },
253 262
254 /** 263 /**
255 * @param {!function(?WebInspector.TempFile)} callback 264 * @param {!function(?WebInspector.TempFile)} callback
256 */ 265 */
257 finishWriting: function(callback) 266 finishWriting: function(callback)
258 { 267 {
259 this._finishCallback = callback; 268 this._finishCallback = callback;
260 if (this._finishedWriting) 269 if (this._finishedWriting)
261 callback(this._tempFile); 270 callback(this._tempFile);
262 else if (!this._isWriting && !this._chunks.length) 271 else if (!this._isWriting && !this._chunks.length)
263 this._notifyFinished(); 272 this._notifyFinished();
264 }, 273 },
265 274
275 /**
276 * @param {*} e
277 */
278 _failedToCreateTempFile: function(e)
279 {
280 WebInspector.console.error("Failed to create temp file " + e.code + " : " + e.message);
281 this._chunks = null;
282 this._notifyFinished();
283 },
284
285 /**
286 * @param {!WebInspector.TempFile} tempFile
287 */
266 _didCreateTempFile: function(tempFile) 288 _didCreateTempFile: function(tempFile)
267 { 289 {
268 this._tempFile = tempFile; 290 this._tempFile = tempFile;
269 var callsPendingOpen = this._callsPendingOpen; 291 var callsPendingOpen = this._callsPendingOpen;
270 this._callsPendingOpen = null; 292 this._callsPendingOpen = null;
271 for (var i = 0; i < callsPendingOpen.length; ++i) 293 for (var i = 0; i < callsPendingOpen.length; ++i)
272 callsPendingOpen[i](); 294 callsPendingOpen[i]();
273 if (!tempFile) {
274 this._chunks = null;
275 this._notifyFinished();
276 return;
277 }
278 if (this._chunks.length) 295 if (this._chunks.length)
279 this._writeNextChunk(); 296 this._writeNextChunk();
280 }, 297 },
281 298
282 _writeNextChunk: function() 299 _writeNextChunk: function()
283 { 300 {
284 var chunks = this._chunks; 301 var chunks = this._chunks;
285 this._chunks = []; 302 this._chunks = [];
286 this._isWriting = true; 303 this._isWriting = true;
287 this._tempFile.write(chunks, this._didWriteChunk.bind(this)); 304 this._tempFile.write(/** @type {!Array.<string>} */(chunks), this._didWr iteChunk.bind(this));
288 }, 305 },
289 306
290 _didWriteChunk: function(success) 307 _didWriteChunk: function(success)
291 { 308 {
292 this._isWriting = false; 309 this._isWriting = false;
293 if (!success) { 310 if (!success) {
294 this._tempFile = null; 311 this._tempFile = null;
295 this._chunks = null; 312 this._chunks = null;
296 this._notifyFinished(); 313 this._notifyFinished();
297 return; 314 return;
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
352 if (this._callsPendingOpen) { 369 if (this._callsPendingOpen) {
353 this._callsPendingOpen.push(this.remove.bind(this)); 370 this._callsPendingOpen.push(this.remove.bind(this));
354 return; 371 return;
355 } 372 }
356 if (this._tempFile) 373 if (this._tempFile)
357 this._tempFile.remove(); 374 this._tempFile.remove();
358 } 375 }
359 } 376 }
360 377
361 /** 378 /**
362 * @constructor 379 * @param {function(?)} fulfill
380 * @param {function(*)} reject
363 */ 381 */
364 WebInspector.TempStorageCleaner = function() 382 WebInspector.TempFile._clearTempStorage = function(fulfill, reject)
365 { 383 {
384 /**
385 * @param {!Event} event
386 */
387 function handleError(event)
388 {
389 WebInspector.console.error(WebInspector.UIString("Failed to clear temp s torage: %s", event.data));
390 reject(event.data);
391 }
392
393 /**
394 * @param {!Event} event
395 */
396 function handleMessage(event)
397 {
398 if (event.data.type === "tempStorageCleared") {
399 if (event.data.error)
400 WebInspector.console.error(event.data.error);
401 else
402 fulfill(undefined);
403 return;
404 }
405 reject(event.data);
406 }
407
366 try { 408 try {
367 this._worker = new WorkerRuntime.Worker("temp_storage_shared_worker", "T empStorageCleaner"); 409 var worker = new WorkerRuntime.Worker("temp_storage_shared_worker", "Tem pStorageCleaner");
368 this._worker.onerror = this._handleError.bind(this); 410 worker.onerror = handleError;
369 this._callbacks = []; 411 worker.port.onmessage = handleMessage;
370 this._worker.port.onmessage = this._handleMessage.bind(this); 412 worker.port.onerror = handleError;
371 this._worker.port.onerror = this._handleError.bind(this);
372 } catch (e) { 413 } catch (e) {
373 if (e.name === "URLMismatchError") 414 if (e.name === "URLMismatchError")
374 console.log("Shared worker wasn't started due to url difference. " + e); 415 console.log("Shared worker wasn't started due to url difference. " + e);
375 else 416 else
376 throw e; 417 throw e;
377 } 418 }
378 } 419 }
379 420
380 WebInspector.TempStorageCleaner.prototype = { 421 /**
381 /** 422 * @return {!Promise.<undefined>}
382 * @param {!function()} callback 423 */
383 */ 424 WebInspector.TempFile.ensureTempStorageCleared = function()
384 ensureStorageCleared: function(callback) 425 {
385 { 426 if (!WebInspector.TempFile._storageCleanerPromise)
386 if (this._callbacks) 427 WebInspector.TempFile._storageCleanerPromise = new Promise(WebInspector. TempFile._clearTempStorage);
387 this._callbacks.push(callback); 428 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 } 429 }
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 }
OLDNEW
« no previous file with comments | « LayoutTests/inspector/tracing-test.js ('k') | Source/devtools/front_end/profiler/CPUProfileView.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698