Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / devtools / front_end / workspace / IsolatedFileSystem.js
bloba35f4b46881adf31dd5b4f99e5b5f3c695670a79
1 /*
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
6 * met:
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.
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 /**
32 * @constructor
33 * @param {!WebInspector.IsolatedFileSystemManager} manager
34 * @param {string} path
35 * @param {string} name
36 * @param {string} rootURL
38 WebInspector.IsolatedFileSystem = function(manager, path, name, rootURL)
40 this._manager = manager;
41 this._path = path;
42 this._name = name;
43 this._rootURL = rootURL;
46 /**
47 * @param {!FileError} error
48 * @return {string}
50 WebInspector.IsolatedFileSystem.errorMessage = function(error)
52 return WebInspector.UIString("File system error: %s", error.message);
55 /**
56 * @param {string} fileSystemPath
57 * @return {string}
59 WebInspector.IsolatedFileSystem.normalizePath = function(fileSystemPath)
61 if (WebInspector.isWin())
62 return fileSystemPath.replace(/\\/g, "/");
63 return fileSystemPath;
66 WebInspector.IsolatedFileSystem.prototype = {
67 /**
68 * @return {string}
70 path: function()
72 return this._path;
75 /**
76 * @return {string}
78 normalizedPath: function()
80 if (this._normalizedPath)
81 return this._normalizedPath;
82 this._normalizedPath = WebInspector.IsolatedFileSystem.normalizePath(this._path);
83 return this._normalizedPath;
86 /**
87 * @return {string}
89 name: function()
91 return this._name;
94 /**
95 * @return {string}
97 rootURL: function()
99 return this._rootURL;
103 * @param {function(?DOMFileSystem)} callback
105 _requestFileSystem: function(callback)
107 this._manager.requestDOMFileSystem(this._path, callback);
111 * @param {string} path
112 * @param {function(string)} fileCallback
113 * @param {function()=} finishedCallback
115 requestFilesRecursive: function(path, fileCallback, finishedCallback)
117 var domFileSystem;
118 var pendingRequests = 0;
119 this._requestFileSystem(fileSystemLoaded.bind(this));
121 * @param {?DOMFileSystem} fs
122 * @this {WebInspector.IsolatedFileSystem}
124 function fileSystemLoaded(fs)
126 domFileSystem = /** @type {!DOMFileSystem} */ (fs);
127 console.assert(domFileSystem);
128 ++pendingRequests;
129 this._requestEntries(domFileSystem, path, innerCallback.bind(this));
133 * @param {!Array.<!FileEntry>} entries
134 * @this {WebInspector.IsolatedFileSystem}
136 function innerCallback(entries)
138 for (var i = 0; i < entries.length; ++i) {
139 var entry = entries[i];
140 if (!entry.isDirectory) {
141 if (this._manager.excludedFolderManager().isFileExcluded(this._path, entry.fullPath))
142 continue;
143 fileCallback(entry.fullPath.substr(1));
145 else {
146 if (this._manager.excludedFolderManager().isFileExcluded(this._path, entry.fullPath + "/"))
147 continue;
148 ++pendingRequests;
149 this._requestEntries(domFileSystem, entry.fullPath, innerCallback.bind(this));
152 if (finishedCallback && (--pendingRequests === 0))
153 finishedCallback();
158 * @param {string} path
159 * @param {?string} name
160 * @param {function(?string)} callback
162 createFile: function(path, name, callback)
164 this._requestFileSystem(fileSystemLoaded.bind(this));
165 var newFileIndex = 1;
166 if (!name)
167 name = "NewFile";
168 var nameCandidate;
171 * @param {?DOMFileSystem} fs
172 * @this {WebInspector.IsolatedFileSystem}
174 function fileSystemLoaded(fs)
176 var domFileSystem = /** @type {!DOMFileSystem} */ (fs);
177 console.assert(domFileSystem);
178 domFileSystem.root.getDirectory(path, null, dirEntryLoaded.bind(this), errorHandler.bind(this));
182 * @param {!DirectoryEntry} dirEntry
183 * @this {WebInspector.IsolatedFileSystem}
185 function dirEntryLoaded(dirEntry)
187 var nameCandidate = name;
188 if (newFileIndex > 1)
189 nameCandidate += newFileIndex;
190 ++newFileIndex;
191 dirEntry.getFile(nameCandidate, { create: true, exclusive: true }, fileCreated, fileCreationError.bind(this));
193 function fileCreated(entry)
195 callback(entry.fullPath.substr(1));
199 * @this {WebInspector.IsolatedFileSystem}
201 function fileCreationError(error)
203 if (error.code === FileError.INVALID_MODIFICATION_ERR) {
204 dirEntryLoaded.call(this, dirEntry);
205 return;
208 var errorMessage = WebInspector.IsolatedFileSystem.errorMessage(error);
209 console.error(errorMessage + " when testing if file exists '" + (this._path + "/" + path + "/" + nameCandidate) + "'");
210 callback(null);
215 * @this {WebInspector.IsolatedFileSystem}
217 function errorHandler(error)
219 var errorMessage = WebInspector.IsolatedFileSystem.errorMessage(error);
220 var filePath = this._path + "/" + path;
221 if (nameCandidate)
222 filePath += "/" + nameCandidate;
223 console.error(errorMessage + " when getting content for file '" + (filePath) + "'");
224 callback(null);
229 * @param {string} path
231 deleteFile: function(path)
233 this._requestFileSystem(fileSystemLoaded.bind(this));
236 * @param {?DOMFileSystem} fs
237 * @this {WebInspector.IsolatedFileSystem}
239 function fileSystemLoaded(fs)
241 var domFileSystem = /** @type {!DOMFileSystem} */ (fs);
242 console.assert(domFileSystem);
243 domFileSystem.root.getFile(path, null, fileEntryLoaded.bind(this), errorHandler.bind(this));
247 * @param {!FileEntry} fileEntry
248 * @this {WebInspector.IsolatedFileSystem}
250 function fileEntryLoaded(fileEntry)
252 fileEntry.remove(fileEntryRemoved, errorHandler.bind(this));
255 function fileEntryRemoved()
260 * @param {!FileError} error
261 * @this {WebInspector.IsolatedFileSystem}
263 function errorHandler(error)
265 var errorMessage = WebInspector.IsolatedFileSystem.errorMessage(error);
266 console.error(errorMessage + " when deleting file '" + (this._path + "/" + path) + "'");
271 * @param {string} path
272 * @param {function(?Date, ?number)} callback
274 requestMetadata: function(path, callback)
276 this._requestFileSystem(fileSystemLoaded);
279 * @param {?DOMFileSystem} fs
281 function fileSystemLoaded(fs)
283 var domFileSystem = /** @type {!DOMFileSystem} */ (fs);
284 console.assert(domFileSystem);
285 domFileSystem.root.getFile(path, null, fileEntryLoaded, errorHandler);
289 * @param {!FileEntry} entry
291 function fileEntryLoaded(entry)
293 entry.getMetadata(successHandler, errorHandler);
297 * @param {!Metadata} metadata
299 function successHandler(metadata)
301 callback(metadata.modificationTime, metadata.size);
305 * @param {!FileError} error
307 function errorHandler(error)
309 callback(null, null);
314 * @param {string} path
315 * @param {function(?string)} callback
317 requestFileContent: function(path, callback)
319 this._requestFileSystem(fileSystemLoaded.bind(this));
322 * @param {?DOMFileSystem} fs
323 * @this {WebInspector.IsolatedFileSystem}
325 function fileSystemLoaded(fs)
327 var domFileSystem = /** @type {!DOMFileSystem} */ (fs);
328 console.assert(domFileSystem);
329 domFileSystem.root.getFile(path, null, fileEntryLoaded.bind(this), errorHandler.bind(this));
333 * @param {!FileEntry} entry
334 * @this {WebInspector.IsolatedFileSystem}
336 function fileEntryLoaded(entry)
338 entry.file(fileLoaded, errorHandler.bind(this));
342 * @param {!Blob} file
344 function fileLoaded(file)
346 var reader = new FileReader();
347 reader.onloadend = readerLoadEnd;
348 reader.readAsText(file);
352 * @this {!FileReader}
354 function readerLoadEnd()
356 /** @type {?string} */
357 var string = null;
358 try {
359 string = /** @type {string} */ (this.result);
360 } catch (e) {
361 console.error("Can't read file: " + path + ": " + e);
363 callback(string);
367 * @this {WebInspector.IsolatedFileSystem}
369 function errorHandler(error)
371 if (error.code === FileError.NOT_FOUND_ERR) {
372 callback(null);
373 return;
376 var errorMessage = WebInspector.IsolatedFileSystem.errorMessage(error);
377 console.error(errorMessage + " when getting content for file '" + (this._path + "/" + path) + "'");
378 callback(null);
383 * @param {string} path
384 * @param {string} content
385 * @param {function()} callback
387 setFileContent: function(path, content, callback)
389 this._requestFileSystem(fileSystemLoaded.bind(this));
390 WebInspector.userMetrics.FileSavedInWorkspace.record();
393 * @param {?DOMFileSystem} fs
394 * @this {WebInspector.IsolatedFileSystem}
396 function fileSystemLoaded(fs)
398 var domFileSystem = /** @type {!DOMFileSystem} */ (fs);
399 console.assert(domFileSystem);
400 domFileSystem.root.getFile(path, { create: true }, fileEntryLoaded.bind(this), errorHandler.bind(this));
404 * @param {!FileEntry} entry
405 * @this {WebInspector.IsolatedFileSystem}
407 function fileEntryLoaded(entry)
409 entry.createWriter(fileWriterCreated.bind(this), errorHandler.bind(this));
413 * @param {!FileWriter} fileWriter
414 * @this {WebInspector.IsolatedFileSystem}
416 function fileWriterCreated(fileWriter)
418 fileWriter.onerror = errorHandler.bind(this);
419 fileWriter.onwriteend = fileWritten;
420 var blob = new Blob([content], { type: "text/plain" });
421 fileWriter.write(blob);
423 function fileWritten()
425 fileWriter.onwriteend = writerEnd;
426 fileWriter.truncate(blob.size);
430 function writerEnd()
432 callback();
436 * @this {WebInspector.IsolatedFileSystem}
438 function errorHandler(error)
440 var errorMessage = WebInspector.IsolatedFileSystem.errorMessage(error);
441 console.error(errorMessage + " when setting content for file '" + (this._path + "/" + path) + "'");
442 callback();
447 * @param {string} path
448 * @param {string} newName
449 * @param {function(boolean, string=)} callback
451 renameFile: function(path, newName, callback)
453 newName = newName ? newName.trim() : newName;
454 if (!newName || newName.indexOf("/") !== -1) {
455 callback(false);
456 return;
458 var fileEntry;
459 var dirEntry;
460 this._requestFileSystem(fileSystemLoaded.bind(this));
463 * @param {?DOMFileSystem} fs
464 * @this {WebInspector.IsolatedFileSystem}
466 function fileSystemLoaded(fs)
468 var domFileSystem = /** @type {!DOMFileSystem} */ (fs);
469 console.assert(domFileSystem);
470 domFileSystem.root.getFile(path, null, fileEntryLoaded.bind(this), errorHandler.bind(this));
474 * @param {!FileEntry} entry
475 * @this {WebInspector.IsolatedFileSystem}
477 function fileEntryLoaded(entry)
479 if (entry.name === newName) {
480 callback(false);
481 return;
484 fileEntry = entry;
485 fileEntry.getParent(dirEntryLoaded.bind(this), errorHandler.bind(this));
489 * @param {!Entry} entry
490 * @this {WebInspector.IsolatedFileSystem}
492 function dirEntryLoaded(entry)
494 dirEntry = entry;
495 dirEntry.getFile(newName, null, newFileEntryLoaded, newFileEntryLoadErrorHandler.bind(this));
499 * @param {!FileEntry} entry
501 function newFileEntryLoaded(entry)
503 callback(false);
507 * @this {WebInspector.IsolatedFileSystem}
509 function newFileEntryLoadErrorHandler(error)
511 if (error.code !== FileError.NOT_FOUND_ERR) {
512 callback(false);
513 return;
515 fileEntry.moveTo(dirEntry, newName, fileRenamed, errorHandler.bind(this));
519 * @param {!FileEntry} entry
521 function fileRenamed(entry)
523 callback(true, entry.name);
527 * @this {WebInspector.IsolatedFileSystem}
529 function errorHandler(error)
531 var errorMessage = WebInspector.IsolatedFileSystem.errorMessage(error);
532 console.error(errorMessage + " when renaming file '" + (this._path + "/" + path) + "' to '" + newName + "'");
533 callback(false);
538 * @param {!DirectoryEntry} dirEntry
539 * @param {function(!Array.<!FileEntry>)} callback
541 _readDirectory: function(dirEntry, callback)
543 var dirReader = dirEntry.createReader();
544 var entries = [];
546 function innerCallback(results)
548 if (!results.length)
549 callback(entries.sort());
550 else {
551 entries = entries.concat(toArray(results));
552 dirReader.readEntries(innerCallback, errorHandler);
556 function toArray(list)
558 return Array.prototype.slice.call(list || [], 0);
561 dirReader.readEntries(innerCallback, errorHandler);
563 function errorHandler(error)
565 var errorMessage = WebInspector.IsolatedFileSystem.errorMessage(error);
566 console.error(errorMessage + " when reading directory '" + dirEntry.fullPath + "'");
567 callback([]);
572 * @param {!DOMFileSystem} domFileSystem
573 * @param {string} path
574 * @param {function(!Array.<!FileEntry>)} callback
576 _requestEntries: function(domFileSystem, path, callback)
578 domFileSystem.root.getDirectory(path, null, innerCallback.bind(this), errorHandler);
581 * @param {!DirectoryEntry} dirEntry
582 * @this {WebInspector.IsolatedFileSystem}
584 function innerCallback(dirEntry)
586 this._readDirectory(dirEntry, callback);
589 function errorHandler(error)
591 var errorMessage = WebInspector.IsolatedFileSystem.errorMessage(error);
592 console.error(errorMessage + " when requesting entry '" + path + "'");
593 callback([]);