2 * Copyright (C) 2012 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.
34 WebInspector.OutputStreamDelegate = function()
38 WebInspector.OutputStreamDelegate.prototype = {
39 onTransferStarted: function() { },
41 onTransferFinished: function() { },
44 * @param {!WebInspector.ChunkedReader} reader
46 onChunkTransferred: function(reader) { },
49 * @param {!WebInspector.ChunkedReader} reader
50 * @param {!Event} event
52 onError: function(reader, event) { },
58 WebInspector.OutputStream = function()
62 WebInspector.OutputStream.prototype = {
64 * @param {string} data
65 * @param {function(!WebInspector.OutputStream)=} callback
67 write: function(data, callback) { },
75 WebInspector.ChunkedReader = function()
79 WebInspector.ChunkedReader.prototype = {
83 fileSize: function() { },
88 loadedSize: function() { },
93 fileName: function() { },
95 cancel: function() { }
100 * @implements {WebInspector.ChunkedReader}
101 * @param {!File} file
102 * @param {number} chunkSize
103 * @param {!WebInspector.OutputStreamDelegate} delegate
105 WebInspector.ChunkedFileReader = function(file, chunkSize, delegate)
108 this._fileSize = file.size;
109 this._loadedSize = 0;
110 this._chunkSize = chunkSize;
111 this._delegate = delegate;
112 this._isCanceled = false;
115 WebInspector.ChunkedFileReader.prototype = {
117 * @param {!WebInspector.OutputStream} output
119 start: function(output)
121 this._output = output;
123 this._reader = new FileReader();
124 this._reader.onload = this._onChunkLoaded.bind(this);
125 this._reader.onerror = this._delegate.onError.bind(this._delegate, this);
126 this._delegate.onTransferStarted();
132 this._isCanceled = true;
138 loadedSize: function()
140 return this._loadedSize;
148 return this._fileSize;
156 return this._file.name;
160 * @param {!Event} event
162 _onChunkLoaded: function(event)
164 if (this._isCanceled)
167 if (event.target.readyState !== FileReader.DONE)
170 var data = event.target.result;
171 this._loadedSize += data.length;
173 this._output.write(data);
174 if (this._isCanceled)
176 this._delegate.onChunkTransferred(this);
178 if (this._loadedSize === this._fileSize) {
181 this._output.close();
182 this._delegate.onTransferFinished();
189 _loadChunk: function()
191 var chunkStart = this._loadedSize;
192 var chunkEnd = Math.min(this._fileSize, chunkStart + this._chunkSize)
193 var nextPart = this._file.slice(chunkStart, chunkEnd);
194 this._reader.readAsText(nextPart);
199 * @param {function(!File)} callback
202 WebInspector.createFileSelectorElement = function(callback) {
203 var fileSelectorElement = createElement("input");
204 fileSelectorElement.type = "file";
205 fileSelectorElement.style.display = "none";
206 fileSelectorElement.setAttribute("tabindex", -1);
207 fileSelectorElement.onchange = onChange;
208 function onChange(event)
210 callback(fileSelectorElement.files[0]);
212 return fileSelectorElement;
217 * @implements {WebInspector.OutputStream}
219 WebInspector.FileOutputStream = function()
223 WebInspector.FileOutputStream.prototype = {
225 * @param {string} fileName
226 * @param {function(boolean)} callback
228 open: function(fileName, callback)
230 this._closed = false;
231 this._writeCallbacks = [];
232 this._fileName = fileName;
235 * @param {boolean} accepted
236 * @this {WebInspector.FileOutputStream}
238 function callbackWrapper(accepted)
241 WebInspector.fileManager.addEventListener(WebInspector.FileManager.EventTypes.AppendedToURL, this._onAppendDone, this);
244 WebInspector.fileManager.save(this._fileName, "", true, callbackWrapper.bind(this));
248 * @param {string} data
249 * @param {function(!WebInspector.OutputStream)=} callback
251 write: function(data, callback)
253 this._writeCallbacks.push(callback);
254 WebInspector.fileManager.append(this._fileName, data);
260 if (this._writeCallbacks.length)
262 WebInspector.fileManager.removeEventListener(WebInspector.FileManager.EventTypes.AppendedToURL, this._onAppendDone, this);
263 WebInspector.fileManager.close(this._fileName);
267 * @param {!WebInspector.Event} event
269 _onAppendDone: function(event)
271 if (event.data !== this._fileName)
273 var callback = this._writeCallbacks.shift();
276 if (!this._writeCallbacks.length) {
278 WebInspector.fileManager.removeEventListener(WebInspector.FileManager.EventTypes.AppendedToURL, this._onAppendDone, this);
279 WebInspector.fileManager.close(this._fileName);