FileUtils.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. */
  30. /**
  31. * @interface
  32. */
  33. WebInspector.OutputStreamDelegate = function()
  34. {
  35. }
  36. WebInspector.OutputStreamDelegate.prototype = {
  37. onTransferStarted: function() { },
  38. onTransferFinished: function() { },
  39. /**
  40. * @param {WebInspector.ChunkedReader} reader
  41. */
  42. onChunkTransferred: function(reader) { },
  43. /**
  44. * @param {WebInspector.ChunkedReader} reader
  45. */
  46. onError: function(reader, event) { },
  47. }
  48. /**
  49. * @interface
  50. */
  51. WebInspector.OutputStream = function()
  52. {
  53. }
  54. WebInspector.OutputStream.prototype = {
  55. /**
  56. * @param {string} data
  57. * @param {function(WebInspector.OutputStream)=} callback
  58. */
  59. write: function(data, callback) { },
  60. close: function() { }
  61. }
  62. /**
  63. * @interface
  64. */
  65. WebInspector.ChunkedReader = function()
  66. {
  67. }
  68. WebInspector.ChunkedReader.prototype = {
  69. /**
  70. * @return {number}
  71. */
  72. fileSize: function() { },
  73. /**
  74. * @return {number}
  75. */
  76. loadedSize: function() { },
  77. /**
  78. * @return {string}
  79. */
  80. fileName: function() { },
  81. cancel: function() { }
  82. }
  83. /**
  84. * @constructor
  85. * @implements {WebInspector.ChunkedReader}
  86. * @param {!File} file
  87. * @param {number} chunkSize
  88. * @param {!WebInspector.OutputStreamDelegate} delegate
  89. */
  90. WebInspector.ChunkedFileReader = function(file, chunkSize, delegate)
  91. {
  92. this._file = file;
  93. this._fileSize = file.size;
  94. this._loadedSize = 0;
  95. this._chunkSize = chunkSize;
  96. this._delegate = delegate;
  97. this._isCanceled = false;
  98. }
  99. WebInspector.ChunkedFileReader.prototype = {
  100. /**
  101. * @param {!WebInspector.OutputStream} output
  102. */
  103. start: function(output)
  104. {
  105. this._output = output;
  106. this._reader = new FileReader();
  107. this._reader.onload = this._onChunkLoaded.bind(this);
  108. this._reader.onerror = this._delegate.onError.bind(this._delegate, this);
  109. this._delegate.onTransferStarted();
  110. this._loadChunk();
  111. },
  112. cancel: function()
  113. {
  114. this._isCanceled = true;
  115. },
  116. /**
  117. * @return {number}
  118. */
  119. loadedSize: function()
  120. {
  121. return this._loadedSize;
  122. },
  123. /**
  124. * @return {number}
  125. */
  126. fileSize: function()
  127. {
  128. return this._fileSize;
  129. },
  130. /**
  131. * @return {string}
  132. */
  133. fileName: function()
  134. {
  135. return this._file.name;
  136. },
  137. /**
  138. * @param {Event} event
  139. */
  140. _onChunkLoaded: function(event)
  141. {
  142. if (this._isCanceled)
  143. return;
  144. if (event.target.readyState !== FileReader.DONE)
  145. return;
  146. var data = event.target.result;
  147. this._loadedSize += data.length;
  148. this._output.write(data);
  149. if (this._isCanceled)
  150. return;
  151. this._delegate.onChunkTransferred(this);
  152. if (this._loadedSize === this._fileSize) {
  153. this._file = null;
  154. this._reader = null;
  155. this._output.close();
  156. this._delegate.onTransferFinished();
  157. return;
  158. }
  159. this._loadChunk();
  160. },
  161. _loadChunk: function()
  162. {
  163. var chunkStart = this._loadedSize;
  164. var chunkEnd = Math.min(this._fileSize, chunkStart + this._chunkSize)
  165. var nextPart = this._file.slice(chunkStart, chunkEnd);
  166. this._reader.readAsText(nextPart);
  167. }
  168. }
  169. /**
  170. * @constructor
  171. * @implements {WebInspector.ChunkedReader}
  172. * @param {string} url
  173. * @param {!WebInspector.OutputStreamDelegate} delegate
  174. */
  175. WebInspector.ChunkedXHRReader = function(url, delegate)
  176. {
  177. this._url = url;
  178. this._delegate = delegate;
  179. this._fileSize = 0;
  180. this._loadedSize = 0;
  181. this._isCanceled = false;
  182. }
  183. WebInspector.ChunkedXHRReader.prototype = {
  184. /**
  185. * @param {!WebInspector.OutputStream} output
  186. */
  187. start: function(output)
  188. {
  189. this._output = output;
  190. this._xhr = new XMLHttpRequest();
  191. this._xhr.open("GET", this._url, true);
  192. this._xhr.onload = this._onLoad.bind(this);
  193. this._xhr.onprogress = this._onProgress.bind(this);
  194. this._xhr.onerror = this._delegate.onError.bind(this._delegate, this);
  195. this._xhr.send(null);
  196. this._delegate.onTransferStarted();
  197. },
  198. cancel: function()
  199. {
  200. this._isCanceled = true;
  201. this._xhr.abort();
  202. },
  203. /**
  204. * @return {number}
  205. */
  206. loadedSize: function()
  207. {
  208. return this._loadedSize;
  209. },
  210. /**
  211. * @return {number}
  212. */
  213. fileSize: function()
  214. {
  215. return this._fileSize;
  216. },
  217. /**
  218. * @return {string}
  219. */
  220. fileName: function()
  221. {
  222. return this._url;
  223. },
  224. /**
  225. * @param {Event} event
  226. */
  227. _onProgress: function(event)
  228. {
  229. if (this._isCanceled)
  230. return;
  231. if (event.lengthComputable)
  232. this._fileSize = event.total;
  233. var data = this._xhr.responseText.substring(this._loadedSize);
  234. if (!data.length)
  235. return;
  236. this._loadedSize += data.length;
  237. this._output.write(data);
  238. if (this._isCanceled)
  239. return;
  240. this._delegate.onChunkTransferred(this);
  241. },
  242. /**
  243. * @param {Event} event
  244. */
  245. _onLoad: function(event)
  246. {
  247. this._onProgress(event);
  248. if (this._isCanceled)
  249. return;
  250. this._output.close();
  251. this._delegate.onTransferFinished();
  252. }
  253. }
  254. /**
  255. * @param {function(!File)} callback
  256. * @return {Node}
  257. */
  258. WebInspector.createFileSelectorElement = function(callback) {
  259. var fileSelectorElement = document.createElement("input");
  260. fileSelectorElement.type = "file";
  261. fileSelectorElement.setAttribute("tabindex", -1);
  262. fileSelectorElement.style.zIndex = -1;
  263. fileSelectorElement.style.position = "absolute";
  264. fileSelectorElement.onchange = function(event) {
  265. callback(fileSelectorElement.files[0]);
  266. };
  267. return fileSelectorElement;
  268. }
  269. /**
  270. * @param {string} source
  271. * @param {number=} startIndex
  272. * @param {number=} lastIndex
  273. */
  274. WebInspector.findBalancedCurlyBrackets = function(source, startIndex, lastIndex) {
  275. lastIndex = lastIndex || source.length;
  276. startIndex = startIndex || 0;
  277. var counter = 0;
  278. var inString = false;
  279. for (var index = startIndex; index < lastIndex; ++index) {
  280. var character = source[index];
  281. if (inString) {
  282. if (character === "\\")
  283. ++index;
  284. else if (character === "\"")
  285. inString = false;
  286. } else {
  287. if (character === "\"")
  288. inString = true;
  289. else if (character === "{")
  290. ++counter;
  291. else if (character === "}") {
  292. if (--counter === 0)
  293. return index + 1;
  294. }
  295. }
  296. }
  297. return -1;
  298. }
  299. /**
  300. * @constructor
  301. * @implements {WebInspector.OutputStream}
  302. */
  303. WebInspector.FileOutputStream = function()
  304. {
  305. }
  306. WebInspector.FileOutputStream.prototype = {
  307. /**
  308. * @param {string} fileName
  309. * @param {function(WebInspector.FileOutputStream, string=)} callback
  310. */
  311. open: function(fileName, callback)
  312. {
  313. this._closed = false;
  314. this._writeCallbacks = [];
  315. this._fileName = fileName;
  316. function callbackWrapper()
  317. {
  318. WebInspector.fileManager.removeEventListener(WebInspector.FileManager.EventTypes.SavedURL, callbackWrapper, this);
  319. WebInspector.fileManager.addEventListener(WebInspector.FileManager.EventTypes.AppendedToURL, this._onAppendDone, this);
  320. callback(this);
  321. }
  322. WebInspector.fileManager.addEventListener(WebInspector.FileManager.EventTypes.SavedURL, callbackWrapper, this);
  323. WebInspector.fileManager.save(this._fileName, "", true);
  324. },
  325. /**
  326. * @param {string} data
  327. * @param {function(WebInspector.OutputStream)=} callback
  328. */
  329. write: function(data, callback)
  330. {
  331. this._writeCallbacks.push(callback);
  332. WebInspector.fileManager.append(this._fileName, data);
  333. },
  334. close: function()
  335. {
  336. this._closed = true;
  337. if (this._writeCallbacks.length)
  338. return;
  339. WebInspector.fileManager.removeEventListener(WebInspector.FileManager.EventTypes.AppendedToURL, this._onAppendDone, this);
  340. WebInspector.fileManager.close(this._fileName);
  341. },
  342. /**
  343. * @param {Event} event
  344. */
  345. _onAppendDone: function(event)
  346. {
  347. if (event.data !== this._fileName)
  348. return;
  349. if (!this._writeCallbacks.length) {
  350. if (this._closed) {
  351. WebInspector.fileManager.removeEventListener(WebInspector.FileManager.EventTypes.AppendedToURL, this._onAppendDone, this);
  352. WebInspector.fileManager.close(this._fileName);
  353. }
  354. return;
  355. }
  356. var callback = this._writeCallbacks.shift();
  357. if (callback)
  358. callback(this);
  359. }
  360. }