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.
33 * @param {!WebInspector.IsolatedFileSystemManager} isolatedFileSystemManager
34 * @param {!WebInspector.Workspace} workspace
36 WebInspector.FileSystemWorkspaceBinding = function(isolatedFileSystemManager, workspace)
38 this._isolatedFileSystemManager = isolatedFileSystemManager;
39 this._workspace = workspace;
40 this._isolatedFileSystemManager.addEventListener(WebInspector.IsolatedFileSystemManager.Events.FileSystemAdded, this._fileSystemAdded, this);
41 this._isolatedFileSystemManager.addEventListener(WebInspector.IsolatedFileSystemManager.Events.FileSystemRemoved, this._fileSystemRemoved, this);
42 /** @type {!Map.<string, !WebInspector.FileSystemWorkspaceBinding.FileSystem>} */
43 this._boundFileSystems = new Map();
45 /** @type {!Object.<number, function(!Array.<string>)>} */
47 /** @type {!Object.<number, !WebInspector.Progress>} */
48 this._progresses = {};
50 InspectorFrontendHost.events.addEventListener(InspectorFrontendHostAPI.Events.IndexingTotalWorkCalculated, this._onIndexingTotalWorkCalculated, this);
51 InspectorFrontendHost.events.addEventListener(InspectorFrontendHostAPI.Events.IndexingWorked, this._onIndexingWorked, this);
52 InspectorFrontendHost.events.addEventListener(InspectorFrontendHostAPI.Events.IndexingDone, this._onIndexingDone, this);
53 InspectorFrontendHost.events.addEventListener(InspectorFrontendHostAPI.Events.SearchCompleted, this._onSearchCompleted, this);
56 WebInspector.FileSystemWorkspaceBinding._scriptExtensions = ["js", "java", "coffee", "ts", "dart"].keySet();
57 WebInspector.FileSystemWorkspaceBinding._styleSheetExtensions = ["css", "scss", "sass", "less"].keySet();
58 WebInspector.FileSystemWorkspaceBinding._documentExtensions = ["htm", "html", "asp", "aspx", "phtml", "jsp"].keySet();
60 WebInspector.FileSystemWorkspaceBinding._lastRequestId = 0;
63 * @param {string} fileSystemPath
66 WebInspector.FileSystemWorkspaceBinding.projectId = function(fileSystemPath)
68 return "filesystem:" + fileSystemPath;
71 WebInspector.FileSystemWorkspaceBinding.prototype = {
73 * @param {!WebInspector.Event} event
75 _fileSystemAdded: function(event)
77 var fileSystem = /** @type {!WebInspector.IsolatedFileSystem} */ (event.data);
78 var boundFileSystem = new WebInspector.FileSystemWorkspaceBinding.FileSystem(this, fileSystem, this._workspace);
79 this._boundFileSystems.set(fileSystem.normalizedPath(), boundFileSystem);
83 * @param {!WebInspector.Event} event
85 _fileSystemRemoved: function(event)
87 var fileSystem = /** @type {!WebInspector.IsolatedFileSystem} */ (event.data);
88 var boundFileSystem = this._boundFileSystems.get(fileSystem.normalizedPath());
89 boundFileSystem.dispose();
90 this._boundFileSystems.remove(fileSystem.normalizedPath());
94 * @param {string} projectId
97 fileSystemPath: function(projectId)
99 var fileSystemPath = projectId.substr("filesystem:".length);
100 var normalizedPath = WebInspector.IsolatedFileSystem.normalizePath(fileSystemPath);
101 var boundFileSystem = this._boundFileSystems.get(normalizedPath);
102 return projectId.substr("filesystem:".length);
110 return ++WebInspector.FileSystemWorkspaceBinding._lastRequestId;
114 * @param {function(!Array.<string>)} callback
117 registerCallback: function(callback)
119 var requestId = this._nextId();
120 this._callbacks[requestId] = callback;
125 * @param {!WebInspector.Progress} progress
128 registerProgress: function(progress)
130 var requestId = this._nextId();
131 this._progresses[requestId] = progress;
136 * @param {!WebInspector.Event} event
138 _onIndexingTotalWorkCalculated: function(event)
140 var requestId = /** @type {number} */ (event.data["requestId"]);
141 var fileSystemPath = /** @type {string} */ (event.data["fileSystemPath"]);
142 var totalWork = /** @type {number} */ (event.data["totalWork"]);
144 var progress = this._progresses[requestId];
147 progress.setTotalWork(totalWork);
151 * @param {!WebInspector.Event} event
153 _onIndexingWorked: function(event)
155 var requestId = /** @type {number} */ (event.data["requestId"]);
156 var fileSystemPath = /** @type {string} */ (event.data["fileSystemPath"]);
157 var worked = /** @type {number} */ (event.data["worked"]);
159 var progress = this._progresses[requestId];
162 progress.worked(worked);
166 * @param {!WebInspector.Event} event
168 _onIndexingDone: function(event)
170 var requestId = /** @type {number} */ (event.data["requestId"]);
171 var fileSystemPath = /** @type {string} */ (event.data["fileSystemPath"]);
173 var progress = this._progresses[requestId];
177 delete this._progresses[requestId];
181 * @param {!WebInspector.Event} event
183 _onSearchCompleted: function(event)
185 var requestId = /** @type {number} */ (event.data["requestId"]);
186 var fileSystemPath = /** @type {string} */ (event.data["fileSystemPath"]);
187 var files = /** @type {!Array.<string>} */ (event.data["files"]);
189 var callback = this._callbacks[requestId];
192 callback.call(null, files);
193 delete this._callbacks[requestId];
199 * @implements {WebInspector.ProjectDelegate}
200 * @param {!WebInspector.IsolatedFileSystem} isolatedFileSystem
201 * @param {!WebInspector.Workspace} workspace
202 * @param {!WebInspector.FileSystemWorkspaceBinding} fileSystemWorkspaceBinding
204 WebInspector.FileSystemWorkspaceBinding.FileSystem = function(fileSystemWorkspaceBinding, isolatedFileSystem, workspace)
206 this._fileSystemWorkspaceBinding = fileSystemWorkspaceBinding;
207 this._fileSystem = isolatedFileSystem;
208 this._fileSystemURL = "file://" + this._fileSystem.normalizedPath() + "/";
209 this._workspace = workspace;
211 this._projectId = WebInspector.FileSystemWorkspaceBinding.projectId(this._fileSystem.path());
212 console.assert(!this._workspace.project(this._projectId));
213 this._projectStore = this._workspace.addProject(this._projectId, this);
217 WebInspector.FileSystemWorkspaceBinding.FileSystem.prototype = {
223 return WebInspector.projectTypes.FileSystem;
229 fileSystemPath: function()
231 return this._fileSystem.path();
237 displayName: function()
239 var normalizedPath = this._fileSystem.normalizedPath();
240 return normalizedPath.substr(normalizedPath.lastIndexOf("/") + 1);
248 // Overriddden by subclasses
253 * @param {string} path
256 _filePathForPath: function(path)
262 * @param {string} path
263 * @param {function(?string)} callback
265 requestFileContent: function(path, callback)
267 var filePath = this._filePathForPath(path);
268 this._fileSystem.requestFileContent(filePath, callback);
272 * @param {string} path
273 * @param {function(?Date, ?number)} callback
275 requestMetadata: function(path, callback)
277 var filePath = this._filePathForPath(path);
278 this._fileSystem.requestMetadata(filePath, callback);
284 canSetFileContent: function()
290 * @param {string} path
291 * @param {string} newContent
292 * @param {function(?string)} callback
294 setFileContent: function(path, newContent, callback)
296 var filePath = this._filePathForPath(path);
297 this._fileSystem.setFileContent(filePath, newContent, callback.bind(this, ""));
303 canRename: function()
309 * @param {string} path
310 * @param {string} newName
311 * @param {function(boolean, string=, string=, string=, !WebInspector.ResourceType=)} callback
313 rename: function(path, newName, callback)
315 var filePath = this._filePathForPath(path);
316 this._fileSystem.renameFile(filePath, newName, innerCallback.bind(this));
319 * @param {boolean} success
320 * @param {string=} newName
321 * @this {WebInspector.FileSystemWorkspaceBinding.FileSystem}
323 function innerCallback(success, newName)
326 callback(false, newName);
329 var validNewName = /** @type {string} */ (newName);
330 console.assert(validNewName);
331 var slash = filePath.lastIndexOf("/");
332 var parentPath = filePath.substring(0, slash);
333 filePath = parentPath + "/" + validNewName;
334 filePath = filePath.substr(1);
335 var newURL = this._workspace.urlForPath(this._fileSystem.path(), filePath);
336 var extension = this._extensionForPath(validNewName);
337 var newOriginURL = this._fileSystemURL + filePath
338 var newContentType = this._contentTypeForExtension(extension);
339 callback(true, validNewName, newURL, newOriginURL, newContentType);
344 * @param {string} path
345 * @param {string} query
346 * @param {boolean} caseSensitive
347 * @param {boolean} isRegex
348 * @param {function(!Array.<!WebInspector.ContentProvider.SearchMatch>)} callback
350 searchInFileContent: function(path, query, caseSensitive, isRegex, callback)
352 var filePath = this._filePathForPath(path);
353 this._fileSystem.requestFileContent(filePath, contentCallback);
356 * @param {?string} content
358 function contentCallback(content)
361 if (content !== null)
362 result = WebInspector.ContentProvider.performSearchInContent(content, query, caseSensitive, isRegex);
368 * @param {!WebInspector.ProjectSearchConfig} searchConfig
369 * @param {!Array.<string>} filesMathingFileQuery
370 * @param {!WebInspector.Progress} progress
371 * @param {function(!Array.<string>)} callback
373 findFilesMatchingSearchRequest: function(searchConfig, filesMathingFileQuery, progress, callback)
375 var result = filesMathingFileQuery;
376 var queriesToRun = searchConfig.queries().slice();
377 if (!queriesToRun.length)
378 queriesToRun.push("");
379 progress.setTotalWork(queriesToRun.length);
380 searchNextQuery.call(this);
383 * @this {WebInspector.FileSystemWorkspaceBinding.FileSystem}
385 function searchNextQuery()
387 if (!queriesToRun.length) {
392 var query = queriesToRun.shift();
393 this._searchInPath(searchConfig.isRegex() ? "" : query, progress, innerCallback.bind(this));
397 * @param {!Array.<string>} files
398 * @this {WebInspector.FileSystemWorkspaceBinding.FileSystem}
400 function innerCallback(files)
402 files = files.sort();
404 result = result.intersectOrdered(files, String.naturalOrderComparator);
405 searchNextQuery.call(this);
410 * @param {string} query
411 * @param {!WebInspector.Progress} progress
412 * @param {function(!Array.<string>)} callback
414 _searchInPath: function(query, progress, callback)
416 var requestId = this._fileSystemWorkspaceBinding.registerCallback(innerCallback.bind(this));
417 InspectorFrontendHost.searchInPath(requestId, this._fileSystem.path(), query);
420 * @param {!Array.<string>} files
421 * @this {WebInspector.FileSystemWorkspaceBinding.FileSystem}
423 function innerCallback(files)
426 * @param {string} fullPath
427 * @this {WebInspector.FileSystemWorkspaceBinding.FileSystem}
429 function trimAndNormalizeFileSystemPath(fullPath)
431 var trimmedPath = fullPath.substr(this._fileSystem.path().length + 1);
432 if (WebInspector.isWin())
433 trimmedPath = trimmedPath.replace(/\\/g, "/");
437 files = files.map(trimAndNormalizeFileSystemPath.bind(this));
444 * @param {!WebInspector.Progress} progress
446 indexContent: function(progress)
448 progress.setTotalWork(1);
449 var requestId = this._fileSystemWorkspaceBinding.registerProgress(progress);
450 progress.addEventListener(WebInspector.Progress.Events.Canceled, this._indexingCanceled.bind(this, requestId));
451 InspectorFrontendHost.indexPath(requestId, this._fileSystem.path());
455 * @param {number} requestId
457 _indexingCanceled: function(requestId)
459 InspectorFrontendHost.stopIndexing(requestId);
463 * @param {string} path
466 _extensionForPath: function(path)
468 var extensionIndex = path.lastIndexOf(".");
469 if (extensionIndex === -1)
471 return path.substring(extensionIndex + 1).toLowerCase();
475 * @param {string} extension
476 * @return {!WebInspector.ResourceType}
478 _contentTypeForExtension: function(extension)
480 if (WebInspector.FileSystemWorkspaceBinding._scriptExtensions[extension])
481 return WebInspector.resourceTypes.Script;
482 if (WebInspector.FileSystemWorkspaceBinding._styleSheetExtensions[extension])
483 return WebInspector.resourceTypes.Stylesheet;
484 if (WebInspector.FileSystemWorkspaceBinding._documentExtensions[extension])
485 return WebInspector.resourceTypes.Document;
486 return WebInspector.resourceTypes.Other;
491 this._fileSystem.requestFilesRecursive("", this._addFile.bind(this));
495 * @param {string} path
496 * @param {function()=} callback
498 refresh: function(path, callback)
500 this._fileSystem.requestFilesRecursive(path, this._addFile.bind(this), callback);
504 * @param {string} path
506 excludeFolder: function(path)
508 this._fileSystemWorkspaceBinding._isolatedFileSystemManager.mapping().addExcludedFolder(this._fileSystem.path(), path);
512 * @param {string} path
513 * @param {?string} name
514 * @param {string} content
515 * @param {function(?string)} callback
517 createFile: function(path, name, content, callback)
519 this._fileSystem.createFile(path, name, innerCallback.bind(this));
523 * @param {?string} filePath
524 * @this {WebInspector.FileSystemWorkspaceBinding.FileSystem}
526 function innerCallback(filePath)
532 createFilePath = filePath;
534 contentSet.call(this);
537 this._fileSystem.setFileContent(filePath, content, contentSet.bind(this));
541 * @this {WebInspector.FileSystemWorkspaceBinding.FileSystem}
543 function contentSet()
545 this._addFile(createFilePath);
546 callback(createFilePath);
551 * @param {string} path
553 deleteFile: function(path)
555 this._fileSystem.deleteFile(path);
556 this._removeFile(path);
561 this._fileSystemWorkspaceBinding._isolatedFileSystemManager.removeFileSystem(this._fileSystem.path());
565 * @param {string} filePath
567 _addFile: function(filePath)
570 console.assert(false);
572 var slash = filePath.lastIndexOf("/");
573 var parentPath = filePath.substring(0, slash);
574 var name = filePath.substring(slash + 1);
576 var url = this._workspace.urlForPath(this._fileSystem.path(), filePath);
577 var extension = this._extensionForPath(name);
578 var contentType = this._contentTypeForExtension(extension);
580 var fileDescriptor = new WebInspector.FileDescriptor(parentPath, name, this._fileSystemURL + filePath, url, contentType);
581 this._projectStore.addFile(fileDescriptor);
585 * @param {string} path
587 _removeFile: function(path)
589 this._projectStore.removeFile(path);
594 this._workspace.removeProject(this._projectId);
599 * @type {!WebInspector.FileSystemWorkspaceBinding}
601 WebInspector.fileSystemWorkspaceBinding;