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

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

Issue 2493373002: DevTools: rename WebInspector into modules. (Closed)
Patch Set: for bots Created 4 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
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 window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileS ystem; 30 window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileS ystem;
31 31
32 /** 32 /**
33 * @unrestricted 33 * @unrestricted
34 */ 34 */
35 WebInspector.TempFile = class { 35 Bindings.TempFile = class {
36 constructor() { 36 constructor() {
37 this._fileEntry = null; 37 this._fileEntry = null;
38 this._writer = null; 38 this._writer = null;
39 } 39 }
40 40
41 /** 41 /**
42 * @param {string} dirPath 42 * @param {string} dirPath
43 * @param {string} name 43 * @param {string} name
44 * @return {!Promise.<!WebInspector.TempFile>} 44 * @return {!Promise.<!Bindings.TempFile>}
45 */ 45 */
46 static create(dirPath, name) { 46 static create(dirPath, name) {
47 var file = new WebInspector.TempFile(); 47 var file = new Bindings.TempFile();
48 48
49 function requestTempFileSystem() { 49 function requestTempFileSystem() {
50 return new Promise(window.requestFileSystem.bind(window, window.TEMPORARY, 10)); 50 return new Promise(window.requestFileSystem.bind(window, window.TEMPORARY, 10));
51 } 51 }
52 52
53 /** 53 /**
54 * @param {!FileSystem} fs 54 * @param {!FileSystem} fs
55 */ 55 */
56 function getDirectoryEntry(fs) { 56 function getDirectoryEntry(fs) {
57 return new Promise(fs.root.getDirectory.bind(fs.root, dirPath, {create: tr ue})); 57 return new Promise(fs.root.getDirectory.bind(fs.root, dirPath, {create: tr ue}));
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
100 100
101 function onTruncateError(e) { 101 function onTruncateError(e) {
102 writer.onwriteend = null; 102 writer.onwriteend = null;
103 writer.onerror = null; 103 writer.onerror = null;
104 throw e; 104 throw e;
105 } 105 }
106 106
107 return new Promise(truncate).then(didTruncate, onTruncateError); 107 return new Promise(truncate).then(didTruncate, onTruncateError);
108 } 108 }
109 109
110 return WebInspector.TempFile.ensureTempStorageCleared() 110 return Bindings.TempFile.ensureTempStorageCleared()
111 .then(requestTempFileSystem) 111 .then(requestTempFileSystem)
112 .then(getDirectoryEntry) 112 .then(getDirectoryEntry)
113 .then(getFileEntry) 113 .then(getFileEntry)
114 .then(createFileWriter) 114 .then(createFileWriter)
115 .then(truncateFile); 115 .then(truncateFile);
116 } 116 }
117 117
118 /** 118 /**
119 * @return {!Promise.<undefined>} 119 * @return {!Promise.<undefined>}
120 */ 120 */
121 static ensureTempStorageCleared() { 121 static ensureTempStorageCleared() {
122 if (!WebInspector.TempFile._storageCleanerPromise) { 122 if (!Bindings.TempFile._storageCleanerPromise) {
123 WebInspector.TempFile._storageCleanerPromise = 123 Bindings.TempFile._storageCleanerPromise =
124 WebInspector.serviceManager.createAppService('utility_shared_worker', 'TempStorage', true).then(service => { 124 Services.serviceManager.createAppService('utility_shared_worker', 'Tem pStorage', true).then(service => {
125 if (service) 125 if (service)
126 return service.send('clear'); 126 return service.send('clear');
127 }); 127 });
128 } 128 }
129 return WebInspector.TempFile._storageCleanerPromise; 129 return Bindings.TempFile._storageCleanerPromise;
130 } 130 }
131 131
132 /** 132 /**
133 * @param {!Array.<string>} strings 133 * @param {!Array.<string>} strings
134 * @param {function(number)} callback 134 * @param {function(number)} callback
135 */ 135 */
136 write(strings, callback) { 136 write(strings, callback) {
137 var blob = new Blob(strings, {type: 'text/plain'}); 137 var blob = new Blob(strings, {type: 'text/plain'});
138 this._writer.onerror = function(e) { 138 this._writer.onerror = function(e) {
139 WebInspector.console.error('Failed to write into a temp file: ' + e.target .error.message); 139 Common.console.error('Failed to write into a temp file: ' + e.target.error .message);
140 callback(-1); 140 callback(-1);
141 }; 141 };
142 this._writer.onwriteend = function(e) { 142 this._writer.onwriteend = function(e) {
143 callback(e.target.length); 143 callback(e.target.length);
144 }; 144 };
145 this._writer.write(blob); 145 this._writer.write(blob);
146 } 146 }
147 147
148 finishWriting() { 148 finishWriting() {
149 this._writer = null; 149 this._writer = null;
(...skipping 20 matching lines...) Expand all
170 170
171 if (typeof startOffset === 'number' || typeof endOffset === 'number') 171 if (typeof startOffset === 'number' || typeof endOffset === 'number')
172 file = file.slice(/** @type {number} */ (startOffset), /** @type {number } */ (endOffset)); 172 file = file.slice(/** @type {number} */ (startOffset), /** @type {number } */ (endOffset));
173 /** 173 /**
174 * @this {FileReader} 174 * @this {FileReader}
175 */ 175 */
176 reader.onloadend = function(e) { 176 reader.onloadend = function(e) {
177 callback(/** @type {?string} */ (this.result)); 177 callback(/** @type {?string} */ (this.result));
178 }; 178 };
179 reader.onerror = function(error) { 179 reader.onerror = function(error) {
180 WebInspector.console.error('Failed to read from temp file: ' + error.mes sage); 180 Common.console.error('Failed to read from temp file: ' + error.message);
181 }; 181 };
182 reader.readAsText(file); 182 reader.readAsText(file);
183 } 183 }
184 function didFailToGetFile(error) { 184 function didFailToGetFile(error) {
185 WebInspector.console.error('Failed to load temp file: ' + error.message); 185 Common.console.error('Failed to load temp file: ' + error.message);
186 callback(null); 186 callback(null);
187 } 187 }
188 this._fileEntry.file(didGetFile, didFailToGetFile); 188 this._fileEntry.file(didGetFile, didFailToGetFile);
189 } 189 }
190 190
191 /** 191 /**
192 * @param {!WebInspector.OutputStream} outputStream 192 * @param {!Common.OutputStream} outputStream
193 * @param {!WebInspector.OutputStreamDelegate} delegate 193 * @param {!Bindings.OutputStreamDelegate} delegate
194 */ 194 */
195 copyToOutputStream(outputStream, delegate) { 195 copyToOutputStream(outputStream, delegate) {
196 /** 196 /**
197 * @param {!File} file 197 * @param {!File} file
198 */ 198 */
199 function didGetFile(file) { 199 function didGetFile(file) {
200 var reader = new WebInspector.ChunkedFileReader(file, 10 * 1000 * 1000, de legate); 200 var reader = new Bindings.ChunkedFileReader(file, 10 * 1000 * 1000, delega te);
201 reader.start(outputStream); 201 reader.start(outputStream);
202 } 202 }
203 203
204 function didFailToGetFile(error) { 204 function didFailToGetFile(error) {
205 WebInspector.console.error('Failed to load temp file: ' + error.message); 205 Common.console.error('Failed to load temp file: ' + error.message);
206 outputStream.close(); 206 outputStream.close();
207 } 207 }
208 208
209 this._fileEntry.file(didGetFile, didFailToGetFile); 209 this._fileEntry.file(didGetFile, didFailToGetFile);
210 } 210 }
211 211
212 remove() { 212 remove() {
213 if (this._fileEntry) 213 if (this._fileEntry)
214 this._fileEntry.remove(function() {}); 214 this._fileEntry.remove(function() {});
215 } 215 }
216 }; 216 };
217 217
218 218
219 /** 219 /**
220 * @unrestricted 220 * @unrestricted
221 */ 221 */
222 WebInspector.DeferredTempFile = class { 222 Bindings.DeferredTempFile = class {
223 /** 223 /**
224 * @param {string} dirPath 224 * @param {string} dirPath
225 * @param {string} name 225 * @param {string} name
226 */ 226 */
227 constructor(dirPath, name) { 227 constructor(dirPath, name) {
228 /** @type {!Array.<!{strings: !Array.<string>, callback: ?function(number)}> } */ 228 /** @type {!Array.<!{strings: !Array.<string>, callback: ?function(number)}> } */
229 this._chunks = []; 229 this._chunks = [];
230 this._tempFile = null; 230 this._tempFile = null;
231 this._isWriting = false; 231 this._isWriting = false;
232 this._finishCallback = null; 232 this._finishCallback = null;
233 this._finishedWriting = false; 233 this._finishedWriting = false;
234 this._callsPendingOpen = []; 234 this._callsPendingOpen = [];
235 this._pendingReads = []; 235 this._pendingReads = [];
236 WebInspector.TempFile.create(dirPath, name) 236 Bindings.TempFile.create(dirPath, name)
237 .then(this._didCreateTempFile.bind(this), this._failedToCreateTempFile.b ind(this)); 237 .then(this._didCreateTempFile.bind(this), this._failedToCreateTempFile.b ind(this));
238 } 238 }
239 239
240 /** 240 /**
241 * @param {!Array.<string>} strings 241 * @param {!Array.<string>} strings
242 * @param {function(number)=} callback 242 * @param {function(number)=} callback
243 */ 243 */
244 write(strings, callback) { 244 write(strings, callback) {
245 if (this._finishCallback) 245 if (this._finishCallback)
246 throw new Error('No writes are allowed after close.'); 246 throw new Error('No writes are allowed after close.');
247 this._chunks.push({strings: strings, callback: callback || null}); 247 this._chunks.push({strings: strings, callback: callback || null});
248 if (this._tempFile && !this._isWriting) 248 if (this._tempFile && !this._isWriting)
249 this._writeNextChunk(); 249 this._writeNextChunk();
250 } 250 }
251 251
252 /** 252 /**
253 * @param {function(?WebInspector.TempFile)} callback 253 * @param {function(?Bindings.TempFile)} callback
254 */ 254 */
255 finishWriting(callback) { 255 finishWriting(callback) {
256 this._finishCallback = callback; 256 this._finishCallback = callback;
257 if (this._finishedWriting) 257 if (this._finishedWriting)
258 callback(this._tempFile); 258 callback(this._tempFile);
259 else if (!this._isWriting && !this._chunks.length) 259 else if (!this._isWriting && !this._chunks.length)
260 this._notifyFinished(); 260 this._notifyFinished();
261 } 261 }
262 262
263 /** 263 /**
264 * @param {*} e 264 * @param {*} e
265 */ 265 */
266 _failedToCreateTempFile(e) { 266 _failedToCreateTempFile(e) {
267 WebInspector.console.error('Failed to create temp file ' + e.code + ' : ' + e.message); 267 Common.console.error('Failed to create temp file ' + e.code + ' : ' + e.mess age);
268 this._notifyFinished(); 268 this._notifyFinished();
269 } 269 }
270 270
271 /** 271 /**
272 * @param {!WebInspector.TempFile} tempFile 272 * @param {!Bindings.TempFile} tempFile
273 */ 273 */
274 _didCreateTempFile(tempFile) { 274 _didCreateTempFile(tempFile) {
275 this._tempFile = tempFile; 275 this._tempFile = tempFile;
276 var callsPendingOpen = this._callsPendingOpen; 276 var callsPendingOpen = this._callsPendingOpen;
277 this._callsPendingOpen = null; 277 this._callsPendingOpen = null;
278 for (var i = 0; i < callsPendingOpen.length; ++i) 278 for (var i = 0; i < callsPendingOpen.length; ++i)
279 callsPendingOpen[i](); 279 callsPendingOpen[i]();
280 if (this._chunks.length) 280 if (this._chunks.length)
281 this._writeNextChunk(); 281 this._writeNextChunk();
282 } 282 }
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
339 return; 339 return;
340 } 340 }
341 if (!this._tempFile) { 341 if (!this._tempFile) {
342 callback(null); 342 callback(null);
343 return; 343 return;
344 } 344 }
345 this._tempFile.readRange(startOffset, endOffset, callback); 345 this._tempFile.readRange(startOffset, endOffset, callback);
346 } 346 }
347 347
348 /** 348 /**
349 * @param {!WebInspector.OutputStream} outputStream 349 * @param {!Common.OutputStream} outputStream
350 * @param {!WebInspector.OutputStreamDelegate} delegate 350 * @param {!Bindings.OutputStreamDelegate} delegate
351 */ 351 */
352 copyToOutputStream(outputStream, delegate) { 352 copyToOutputStream(outputStream, delegate) {
353 if (!this._finishedWriting) { 353 if (!this._finishedWriting) {
354 this._pendingReads.push(this.copyToOutputStream.bind(this, outputStream, d elegate)); 354 this._pendingReads.push(this.copyToOutputStream.bind(this, outputStream, d elegate));
355 return; 355 return;
356 } 356 }
357 if (this._tempFile) 357 if (this._tempFile)
358 this._tempFile.copyToOutputStream(outputStream, delegate); 358 this._tempFile.copyToOutputStream(outputStream, delegate);
359 } 359 }
360 360
361 remove() { 361 remove() {
362 if (this._callsPendingOpen) { 362 if (this._callsPendingOpen) {
363 this._callsPendingOpen.push(this.remove.bind(this)); 363 this._callsPendingOpen.push(this.remove.bind(this));
364 return; 364 return;
365 } 365 }
366 if (this._tempFile) 366 if (this._tempFile)
367 this._tempFile.remove(); 367 this._tempFile.remove();
368 this._tempFile = null; 368 this._tempFile = null;
369 } 369 }
370 }; 370 };
371 371
372 372
373 /** 373 /**
374 * @implements {WebInspector.BackingStorage} 374 * @implements {SDK.BackingStorage}
375 * @unrestricted 375 * @unrestricted
376 */ 376 */
377 WebInspector.TempFileBackingStorage = class { 377 Bindings.TempFileBackingStorage = class {
378 /** 378 /**
379 * @param {string} dirName 379 * @param {string} dirName
380 */ 380 */
381 constructor(dirName) { 381 constructor(dirName) {
382 this._dirName = dirName; 382 this._dirName = dirName;
383 this.reset(); 383 this.reset();
384 } 384 }
385 385
386 /** 386 /**
387 * @override 387 * @override
388 * @param {string} string 388 * @param {string} string
389 */ 389 */
390 appendString(string) { 390 appendString(string) {
391 this._strings.push(string); 391 this._strings.push(string);
392 this._stringsLength += string.length; 392 this._stringsLength += string.length;
393 var flushStringLength = 10 * 1024 * 1024; 393 var flushStringLength = 10 * 1024 * 1024;
394 if (this._stringsLength > flushStringLength) 394 if (this._stringsLength > flushStringLength)
395 this._flush(false); 395 this._flush(false);
396 } 396 }
397 397
398 /** 398 /**
399 * @override 399 * @override
400 * @param {string} string 400 * @param {string} string
401 * @return {function():!Promise.<?string>} 401 * @return {function():!Promise.<?string>}
402 */ 402 */
403 appendAccessibleString(string) { 403 appendAccessibleString(string) {
404 this._flush(false); 404 this._flush(false);
405 this._strings.push(string); 405 this._strings.push(string);
406 var chunk = /** @type {!WebInspector.TempFileBackingStorage.Chunk} */ (this. _flush(true)); 406 var chunk = /** @type {!Bindings.TempFileBackingStorage.Chunk} */ (this._flu sh(true));
407 407
408 /** 408 /**
409 * @param {!WebInspector.TempFileBackingStorage.Chunk} chunk 409 * @param {!Bindings.TempFileBackingStorage.Chunk} chunk
410 * @param {!WebInspector.DeferredTempFile} file 410 * @param {!Bindings.DeferredTempFile} file
411 * @return {!Promise.<?string>} 411 * @return {!Promise.<?string>}
412 */ 412 */
413 function readString(chunk, file) { 413 function readString(chunk, file) {
414 if (chunk.string) 414 if (chunk.string)
415 return /** @type {!Promise.<?string>} */ (Promise.resolve(chunk.string)) ; 415 return /** @type {!Promise.<?string>} */ (Promise.resolve(chunk.string)) ;
416 416
417 console.assert(chunk.endOffset); 417 console.assert(chunk.endOffset);
418 if (!chunk.endOffset) 418 if (!chunk.endOffset)
419 return Promise.reject('Nor string nor offset to the string in the file w ere found.'); 419 return Promise.reject('Nor string nor offset to the string in the file w ere found.');
420 420
421 /** 421 /**
422 * @param {function(?string)} fulfill 422 * @param {function(?string)} fulfill
423 * @param {function(*)} reject 423 * @param {function(*)} reject
424 */ 424 */
425 function readRange(fulfill, reject) { 425 function readRange(fulfill, reject) {
426 // FIXME: call reject for null strings. 426 // FIXME: call reject for null strings.
427 file.readRange(chunk.startOffset, chunk.endOffset, fulfill); 427 file.readRange(chunk.startOffset, chunk.endOffset, fulfill);
428 } 428 }
429 429
430 return new Promise(readRange); 430 return new Promise(readRange);
431 } 431 }
432 432
433 return readString.bind(null, chunk, this._file); 433 return readString.bind(null, chunk, this._file);
434 } 434 }
435 435
436 /** 436 /**
437 * @param {boolean} createChunk 437 * @param {boolean} createChunk
438 * @return {?WebInspector.TempFileBackingStorage.Chunk} 438 * @return {?Bindings.TempFileBackingStorage.Chunk}
439 */ 439 */
440 _flush(createChunk) { 440 _flush(createChunk) {
441 if (!this._strings.length) 441 if (!this._strings.length)
442 return null; 442 return null;
443 443
444 var chunk = null; 444 var chunk = null;
445 if (createChunk) { 445 if (createChunk) {
446 console.assert(this._strings.length === 1); 446 console.assert(this._strings.length === 1);
447 chunk = {string: this._strings[0], startOffset: 0, endOffset: 0}; 447 chunk = {string: this._strings[0], startOffset: 0, endOffset: 0};
448 } 448 }
449 449
450 /** 450 /**
451 * @this {WebInspector.TempFileBackingStorage} 451 * @this {Bindings.TempFileBackingStorage}
452 * @param {?WebInspector.TempFileBackingStorage.Chunk} chunk 452 * @param {?Bindings.TempFileBackingStorage.Chunk} chunk
453 * @param {number} fileSize 453 * @param {number} fileSize
454 */ 454 */
455 function didWrite(chunk, fileSize) { 455 function didWrite(chunk, fileSize) {
456 if (fileSize === -1) 456 if (fileSize === -1)
457 return; 457 return;
458 if (chunk) { 458 if (chunk) {
459 chunk.startOffset = this._fileSize; 459 chunk.startOffset = this._fileSize;
460 chunk.endOffset = fileSize; 460 chunk.endOffset = fileSize;
461 chunk.string = null; 461 chunk.string = null;
462 } 462 }
(...skipping 13 matching lines...) Expand all
476 this._flush(false); 476 this._flush(false);
477 this._file.finishWriting(function() {}); 477 this._file.finishWriting(function() {});
478 } 478 }
479 479
480 /** 480 /**
481 * @override 481 * @override
482 */ 482 */
483 reset() { 483 reset() {
484 if (this._file) 484 if (this._file)
485 this._file.remove(); 485 this._file.remove();
486 this._file = new WebInspector.DeferredTempFile(this._dirName, String(Date.no w())); 486 this._file = new Bindings.DeferredTempFile(this._dirName, String(Date.now()) );
487 /** 487 /**
488 * @type {!Array.<string>} 488 * @type {!Array.<string>}
489 */ 489 */
490 this._strings = []; 490 this._strings = [];
491 this._stringsLength = 0; 491 this._stringsLength = 0;
492 this._fileSize = 0; 492 this._fileSize = 0;
493 } 493 }
494 494
495 /** 495 /**
496 * @param {!WebInspector.OutputStream} outputStream 496 * @param {!Common.OutputStream} outputStream
497 * @param {!WebInspector.OutputStreamDelegate} delegate 497 * @param {!Bindings.OutputStreamDelegate} delegate
498 */ 498 */
499 writeToStream(outputStream, delegate) { 499 writeToStream(outputStream, delegate) {
500 this._file.copyToOutputStream(outputStream, delegate); 500 this._file.copyToOutputStream(outputStream, delegate);
501 } 501 }
502 }; 502 };
503 503
504 /** 504 /**
505 * @typedef {{ 505 * @typedef {{
506 * string: ?string, 506 * string: ?string,
507 * startOffset: number, 507 * startOffset: number,
508 * endOffset: number 508 * endOffset: number
509 * }} 509 * }}
510 */ 510 */
511 WebInspector.TempFileBackingStorage.Chunk; 511 Bindings.TempFileBackingStorage.Chunk;
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698