2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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.
31 window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
35 * @param {!string} dirPath
36 * @param {!string} name
37 * @param {!function(?WebInspector.TempFile)} callback
39 WebInspector.TempFile = function(dirPath, name, callback)
41 this._fileEntry = null;
45 * @param {!FileSystem} fs
46 * @this {WebInspector.TempFile}
48 function didInitFs(fs)
50 fs.root.getDirectory(dirPath, { create: true }, didGetDir.bind(this), errorHandler);
54 * @param {!DirectoryEntry} dir
55 * @this {WebInspector.TempFile}
57 function didGetDir(dir)
59 dir.getFile(name, { create: true }, didCreateFile.bind(this), errorHandler);
63 * @param {!FileEntry} fileEntry
64 * @this {WebInspector.TempFile}
66 function didCreateFile(fileEntry)
68 this._fileEntry = fileEntry;
69 fileEntry.createWriter(didCreateWriter.bind(this), errorHandler);
73 * @param {!FileWriter} writer
74 * @this {WebInspector.TempFile}
76 function didCreateWriter(writer)
79 * @this {WebInspector.TempFile}
81 function didTruncate(e)
83 this._writer = writer;
84 writer.onwrite = null;
85 writer.onerror = null;
89 function onTruncateError(e)
91 WebInspector.console.log("Failed to truncate temp file " + e.code + " : " + e.message,
92 WebInspector.ConsoleMessage.MessageLevel.Error);
97 writer.onwrite = didTruncate.bind(this);
98 writer.onerror = onTruncateError;
101 this._writer = writer;
106 function errorHandler(e)
108 WebInspector.console.log("Failed to create temp file " + e.code + " : " + e.message,
109 WebInspector.ConsoleMessage.MessageLevel.Error);
114 * @this {WebInspector.TempFile}
116 function didClearTempStorage()
118 window.requestFileSystem(window.TEMPORARY, 10, didInitFs.bind(this), errorHandler);
120 WebInspector.TempFile._ensureTempStorageCleared(didClearTempStorage.bind(this));
123 WebInspector.TempFile.prototype = {
125 * @param {!string} data
126 * @param {!function(boolean)} callback
128 write: function(data, callback)
130 var blob = new Blob([data], {type: 'text/plain'});
131 this._writer.onerror = function(e)
133 WebInspector.console.log("Failed to write into a temp file: " + e.message,
134 WebInspector.ConsoleMessage.MessageLevel.Error);
137 this._writer.onwrite = function(e)
141 this._writer.write(blob);
144 finishWriting: function()
150 * @param {function(?string)} callback
152 read: function(callback)
155 * @param {!File} file
157 function didGetFile(file)
159 var reader = new FileReader();
164 reader.onloadend = function(e)
166 callback(/** @type {?string} */ (this.result));
168 reader.onerror = function(error)
170 WebInspector.console.log("Failed to read from temp file: " + error.message,
171 WebInspector.ConsoleMessage.MessageLevel.Error);
173 reader.readAsText(file);
175 function didFailToGetFile(error)
177 WebInspector.console.log("Failed to load temp file: " + error.message,
178 WebInspector.ConsoleMessage.MessageLevel.Error);
181 this._fileEntry.file(didGetFile, didFailToGetFile);
185 * @param {!WebInspector.OutputStream} outputStream
186 * @param {!WebInspector.OutputStreamDelegate} delegate
188 writeToOutputSteam: function(outputStream, delegate)
191 * @param {!File} file
193 function didGetFile(file)
195 var reader = new WebInspector.ChunkedFileReader(file, 10*1000*1000, delegate);
196 reader.start(outputStream);
199 function didFailToGetFile(error)
201 WebInspector.console.log("Failed to load temp file: " + error.message,
202 WebInspector.ConsoleMessage.MessageLevel.Error);
203 outputStream.close();
206 this._fileEntry.file(didGetFile, didFailToGetFile);
212 this._fileEntry.remove(function() {});
218 * @param {!string} dirPath
219 * @param {!string} name
221 WebInspector.BufferedTempFileWriter = function(dirPath, name)
224 this._tempFile = null;
225 this._isWriting = false;
226 this._finishCallback = null;
227 this._isFinished = false;
228 new WebInspector.TempFile(dirPath, name, this._didCreateTempFile.bind(this));
231 WebInspector.BufferedTempFileWriter.prototype = {
233 * @param {!string} data
235 write: function(data)
239 if (this._finishCallback)
240 throw new Error("Now writes are allowed after close.");
241 this._chunks.push(data);
242 if (this._tempFile && !this._isWriting)
243 this._writeNextChunk();
247 * @param {!function(?WebInspector.TempFile)} callback
249 close: function(callback)
251 this._finishCallback = callback;
252 if (this._isFinished)
253 callback(this._tempFile);
254 else if (!this._isWriting && !this._chunks.length)
255 this._notifyFinished();
258 _didCreateTempFile: function(tempFile)
260 this._tempFile = tempFile;
263 this._notifyFinished();
266 if (this._chunks.length)
267 this._writeNextChunk();
270 _writeNextChunk: function()
274 for (; endIndex < this._chunks.length; endIndex++) {
275 chunkSize += this._chunks[endIndex].length;
276 if (chunkSize > 10 * 1000 * 1000)
279 var chunk = this._chunks.slice(0, endIndex + 1).join("");
280 this._chunks.splice(0, endIndex + 1);
281 this._isWriting = true;
282 this._tempFile.write(chunk, this._didWriteChunk.bind(this));
285 _didWriteChunk: function(success)
287 this._isWriting = false;
289 this._tempFile = null;
291 this._notifyFinished();
294 if (this._chunks.length)
295 this._writeNextChunk();
296 else if (this._finishCallback)
297 this._notifyFinished();
300 _notifyFinished: function()
302 this._isFinished = true;
304 this._tempFile.finishWriting();
305 if (this._finishCallback)
306 this._finishCallback(this._tempFile);
313 WebInspector.TempStorageCleaner = function()
315 this._worker = new SharedWorker("TempStorageSharedWorker.js", "TempStorage");
316 this._callbacks = [];
317 this._worker.port.onmessage = this._handleMessage.bind(this);
318 this._worker.port.onerror = this._handleError.bind(this);
321 WebInspector.TempStorageCleaner.prototype = {
323 * @param {!function()} callback
325 ensureStorageCleared: function(callback)
328 this._callbacks.push(callback);
333 _handleMessage: function(event)
335 if (event.data.type === "tempStorageCleared") {
336 if (event.data.error)
337 WebInspector.console.log(event.data.error, WebInspector.ConsoleMessage.MessageLevel.Error);
338 this._notifyCallbacks();
342 _handleError: function(event)
344 WebInspector.console.log(WebInspector.UIString("Failed to clear temp storage: %s", event.data),
345 WebInspector.ConsoleMessage.MessageLevel.Error);
346 this._notifyCallbacks();
349 _notifyCallbacks: function()
351 var callbacks = this._callbacks;
352 this._callbacks = null;
353 for (var i = 0; i < callbacks.length; i++)
359 * @param {!function()} callback
361 WebInspector.TempFile._ensureTempStorageCleared = function(callback)
363 if (!WebInspector.TempFile._storageCleaner)
364 WebInspector.TempFile._storageCleaner = new WebInspector.TempStorageCleaner();
365 WebInspector.TempFile._storageCleaner.ensureStorageCleared(callback);