Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / devtools / front_end / bindings / FileUtils.js
blobc97192adc8c2c5702f9f4c3db15115756c97ca40
1 /*
2  * Copyright (C) 2012 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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
13  * distribution.
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.
17  *
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.
29  */
31 /**
32  * @interface
33  */
34 WebInspector.OutputStreamDelegate = function()
38 WebInspector.OutputStreamDelegate.prototype = {
39     onTransferStarted: function() { },
41     onTransferFinished: function() { },
43     /**
44      * @param {!WebInspector.ChunkedReader} reader
45      */
46     onChunkTransferred: function(reader) { },
48     /**
49      * @param {!WebInspector.ChunkedReader} reader
50      * @param {!Event} event
51      */
52     onError: function(reader, event) { },
55 /**
56  * @interface
57  */
58 WebInspector.ChunkedReader = function()
62 WebInspector.ChunkedReader.prototype = {
63     /**
64      * @return {number}
65      */
66     fileSize: function() { },
68     /**
69      * @return {number}
70      */
71     loadedSize: function() { },
73     /**
74      * @return {string}
75      */
76     fileName: function() { },
78     cancel: function() { }
81 /**
82  * @constructor
83  * @implements {WebInspector.ChunkedReader}
84  * @param {!File} file
85  * @param {number} chunkSize
86  * @param {!WebInspector.OutputStreamDelegate} delegate
87  */
88 WebInspector.ChunkedFileReader = function(file, chunkSize, delegate)
90     this._file = file;
91     this._fileSize = file.size;
92     this._loadedSize = 0;
93     this._chunkSize = chunkSize;
94     this._delegate = delegate;
95     this._decoder = new TextDecoder();
96     this._isCanceled = false;
99 WebInspector.ChunkedFileReader.prototype = {
100     /**
101      * @param {!WebInspector.OutputStream} output
102      */
103     start: function(output)
104     {
105         this._output = output;
107         this._reader = new FileReader();
108         this._reader.onload = this._onChunkLoaded.bind(this);
109         this._reader.onerror = this._delegate.onError.bind(this._delegate, this);
110         this._delegate.onTransferStarted();
111         this._loadChunk();
112     },
114     /**
115      * @override
116      */
117     cancel: function()
118     {
119         this._isCanceled = true;
120     },
122     /**
123      * @override
124      * @return {number}
125      */
126     loadedSize: function()
127     {
128         return this._loadedSize;
129     },
131     /**
132      * @override
133      * @return {number}
134      */
135     fileSize: function()
136     {
137         return this._fileSize;
138     },
140     /**
141      * @override
142      * @return {string}
143      */
144     fileName: function()
145     {
146         return this._file.name;
147     },
149     /**
150      * @param {!Event} event
151      */
152     _onChunkLoaded: function(event)
153     {
154         if (this._isCanceled)
155             return;
157         if (event.target.readyState !== FileReader.DONE)
158             return;
160         var buffer = event.target.result;
161         this._loadedSize += buffer.byteLength;
162         var endOfFile = this._loadedSize === this._fileSize;
163         var decodedString = this._decoder.decode(buffer, {stream: !endOfFile});
164         this._output.write(decodedString);
165         if (this._isCanceled)
166             return;
167         this._delegate.onChunkTransferred(this);
169         if (endOfFile) {
170             this._file = null;
171             this._reader = null;
172             this._output.close();
173             this._delegate.onTransferFinished();
174             return;
175         }
177         this._loadChunk();
178     },
180     _loadChunk: function()
181     {
182         var chunkStart = this._loadedSize;
183         var chunkEnd = Math.min(this._fileSize, chunkStart + this._chunkSize);
184         var nextPart = this._file.slice(chunkStart, chunkEnd);
185         this._reader.readAsArrayBuffer(nextPart);
186     }
190  * @param {function(!File)} callback
191  * @return {!Node}
192  */
193 WebInspector.createFileSelectorElement = function(callback)
195     var fileSelectorElement = createElement("input");
196     fileSelectorElement.type = "file";
197     fileSelectorElement.style.display = "none";
198     fileSelectorElement.setAttribute("tabindex", -1);
199     fileSelectorElement.onchange = onChange;
200     function onChange(event)
201     {
202         callback(fileSelectorElement.files[0]);
203     };
204     return fileSelectorElement;
208  * @constructor
209  * @implements {WebInspector.OutputStream}
210  */
211 WebInspector.FileOutputStream = function()
215 WebInspector.FileOutputStream.prototype = {
216     /**
217      * @param {string} fileName
218      * @param {function(boolean)} callback
219      */
220     open: function(fileName, callback)
221     {
222         this._closed = false;
223         this._writeCallbacks = [];
224         this._fileName = fileName;
226         /**
227          * @param {boolean} accepted
228          * @this {WebInspector.FileOutputStream}
229          */
230         function callbackWrapper(accepted)
231         {
232             if (accepted)
233                 WebInspector.fileManager.addEventListener(WebInspector.FileManager.EventTypes.AppendedToURL, this._onAppendDone, this);
234             callback(accepted);
235         }
236         WebInspector.fileManager.save(this._fileName, "", true, callbackWrapper.bind(this));
237     },
239     /**
240      * @override
241      * @param {string} data
242      * @param {function(!WebInspector.OutputStream)=} callback
243      */
244     write: function(data, callback)
245     {
246         this._writeCallbacks.push(callback);
247         WebInspector.fileManager.append(this._fileName, data);
248     },
250     /**
251      * @override
252      */
253     close: function()
254     {
255         this._closed = true;
256         if (this._writeCallbacks.length)
257             return;
258         WebInspector.fileManager.removeEventListener(WebInspector.FileManager.EventTypes.AppendedToURL, this._onAppendDone, this);
259         WebInspector.fileManager.close(this._fileName);
260     },
262     /**
263      * @param {!WebInspector.Event} event
264      */
265     _onAppendDone: function(event)
266     {
267         if (event.data !== this._fileName)
268             return;
269         var callback = this._writeCallbacks.shift();
270         if (callback)
271             callback(this);
272         if (!this._writeCallbacks.length) {
273             if (this._closed) {
274                 WebInspector.fileManager.removeEventListener(WebInspector.FileManager.EventTypes.AppendedToURL, this._onAppendDone, this);
275                 WebInspector.fileManager.close(this._fileName);
276             }
277         }
278     }