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.
33 * @extends {WebInspector.Object}
35 WebInspector.FileSystemMapping = function()
37 WebInspector.Object.call(this);
38 this._fileSystemMappingSetting = WebInspector.settings.createSetting("fileSystemMapping", {});
39 this._excludedFoldersSetting = WebInspector.settings.createSetting("workspaceExcludedFolders", {});
40 var defaultCommonExcludedFolders = [
49 var defaultWinExcludedFolders = [
55 var defaultMacExcludedFolders = [
58 "/\\.Spotlight-V100$",
64 var defaultLinuxExcludedFolders = [
67 var defaultExcludedFolders = defaultCommonExcludedFolders;
68 if (WebInspector.isWin())
69 defaultExcludedFolders = defaultExcludedFolders.concat(defaultWinExcludedFolders);
70 else if (WebInspector.isMac())
71 defaultExcludedFolders = defaultExcludedFolders.concat(defaultMacExcludedFolders);
73 defaultExcludedFolders = defaultExcludedFolders.concat(defaultLinuxExcludedFolders);
74 var defaultExcludedFoldersPattern = defaultExcludedFolders.join("|");
75 WebInspector.settings.workspaceFolderExcludePattern = WebInspector.settings.createRegExpSetting("workspaceFolderExcludePattern", defaultExcludedFoldersPattern, WebInspector.isWin() ? "i" : "");
76 /** @type {!Object.<string, !Array.<!WebInspector.FileSystemMapping.Entry>>} */
77 this._fileSystemMappings = {};
78 /** @type {!Object.<string, !Array.<!WebInspector.FileSystemMapping.ExcludedFolderEntry>>} */
79 this._excludedFolders = {};
80 this._loadFromSettings();
83 WebInspector.FileSystemMapping.Events = {
84 FileMappingAdded: "FileMappingAdded",
85 FileMappingRemoved: "FileMappingRemoved",
86 ExcludedFolderAdded: "ExcludedFolderAdded",
87 ExcludedFolderRemoved: "ExcludedFolderRemoved"
91 WebInspector.FileSystemMapping.prototype = {
92 _loadFromSettings: function()
94 var savedMapping = this._fileSystemMappingSetting.get();
95 this._fileSystemMappings = {};
96 for (var fileSystemPath in savedMapping) {
97 var savedFileSystemMappings = savedMapping[fileSystemPath];
99 this._fileSystemMappings[fileSystemPath] = [];
100 var fileSystemMappings = this._fileSystemMappings[fileSystemPath];
102 for (var i = 0; i < savedFileSystemMappings.length; ++i) {
103 var savedEntry = savedFileSystemMappings[i];
104 var entry = new WebInspector.FileSystemMapping.Entry(savedEntry.fileSystemPath, savedEntry.urlPrefix, savedEntry.pathPrefix);
105 fileSystemMappings.push(entry);
109 var savedExcludedFolders = this._excludedFoldersSetting.get();
110 this._excludedFolders = {};
111 for (var fileSystemPath in savedExcludedFolders) {
112 var savedExcludedFoldersForPath = savedExcludedFolders[fileSystemPath];
114 this._excludedFolders[fileSystemPath] = [];
115 var excludedFolders = this._excludedFolders[fileSystemPath];
117 for (var i = 0; i < savedExcludedFoldersForPath.length; ++i) {
118 var savedEntry = savedExcludedFoldersForPath[i];
119 var entry = new WebInspector.FileSystemMapping.ExcludedFolderEntry(savedEntry.fileSystemPath, savedEntry.path);
120 excludedFolders.push(entry);
124 this._rebuildIndexes();
127 _saveToSettings: function()
129 var savedMapping = this._fileSystemMappings;
130 this._fileSystemMappingSetting.set(savedMapping);
132 var savedExcludedFolders = this._excludedFolders;
133 this._excludedFoldersSetting.set(savedExcludedFolders);
135 this._rebuildIndexes();
138 _rebuildIndexes: function()
140 // We are building an index here to search for the longest url prefix match faster.
141 this._mappingForURLPrefix = {};
142 this._urlPrefixes = [];
143 for (var fileSystemPath in this._fileSystemMappings) {
144 var fileSystemMapping = this._fileSystemMappings[fileSystemPath];
145 for (var i = 0; i < fileSystemMapping.length; ++i) {
146 var entry = fileSystemMapping[i];
147 this._mappingForURLPrefix[entry.urlPrefix] = entry;
148 this._urlPrefixes.push(entry.urlPrefix);
151 this._urlPrefixes.sort();
155 * @param {string} fileSystemPath
157 addFileSystem: function(fileSystemPath)
159 if (this._fileSystemMappings[fileSystemPath])
162 this._fileSystemMappings[fileSystemPath] = [];
163 this._saveToSettings();
167 * @param {string} fileSystemPath
169 removeFileSystem: function(fileSystemPath)
171 if (!this._fileSystemMappings[fileSystemPath])
173 delete this._fileSystemMappings[fileSystemPath];
174 delete this._excludedFolders[fileSystemPath];
175 this._saveToSettings();
179 * @param {string} fileSystemPath
180 * @param {string} urlPrefix
181 * @param {string} pathPrefix
183 addFileMapping: function(fileSystemPath, urlPrefix, pathPrefix)
185 var entry = new WebInspector.FileSystemMapping.Entry(fileSystemPath, urlPrefix, pathPrefix);
186 this._fileSystemMappings[fileSystemPath].push(entry);
187 this._saveToSettings();
188 this.dispatchEventToListeners(WebInspector.FileSystemMapping.Events.FileMappingAdded, entry);
192 * @param {string} fileSystemPath
193 * @param {string} urlPrefix
194 * @param {string} pathPrefix
196 removeFileMapping: function(fileSystemPath, urlPrefix, pathPrefix)
198 var entry = this._mappingEntryForPathPrefix(fileSystemPath, pathPrefix);
201 this._fileSystemMappings[fileSystemPath].remove(entry);
202 this._saveToSettings();
203 this.dispatchEventToListeners(WebInspector.FileSystemMapping.Events.FileMappingRemoved, entry);
207 * @param {string} fileSystemPath
208 * @param {string} excludedFolderPath
210 addExcludedFolder: function(fileSystemPath, excludedFolderPath)
212 if (!this._excludedFolders[fileSystemPath])
213 this._excludedFolders[fileSystemPath] = [];
214 var entry = new WebInspector.FileSystemMapping.ExcludedFolderEntry(fileSystemPath, excludedFolderPath);
215 this._excludedFolders[fileSystemPath].push(entry);
216 this._saveToSettings();
217 this.dispatchEventToListeners(WebInspector.FileSystemMapping.Events.ExcludedFolderAdded, entry);
221 * @param {string} fileSystemPath
222 * @param {string} path
224 removeExcludedFolder: function(fileSystemPath, path)
226 var entry = this._excludedFolderEntryForPath(fileSystemPath, path);
229 this._excludedFolders[fileSystemPath].remove(entry);
230 this._saveToSettings();
231 this.dispatchEventToListeners(WebInspector.FileSystemMapping.Events.ExcludedFolderRemoved, entry);
235 * @return {!Array.<string>}
237 fileSystemPaths: function()
239 return Object.keys(this._fileSystemMappings);
243 * @param {string} url
244 * @return {?WebInspector.FileSystemMapping.Entry}
246 _mappingEntryForURL: function(url)
248 for (var i = this._urlPrefixes.length - 1; i >= 0; --i) {
249 var urlPrefix = this._urlPrefixes[i];
250 if (url.startsWith(urlPrefix))
251 return this._mappingForURLPrefix[urlPrefix];
257 * @param {string} fileSystemPath
258 * @param {string} path
259 * @return {?WebInspector.FileSystemMapping.ExcludedFolderEntry}
261 _excludedFolderEntryForPath: function(fileSystemPath, path)
263 var entries = this._excludedFolders[fileSystemPath];
267 for (var i = 0; i < entries.length; ++i) {
268 if (entries[i].path === path)
275 * @param {string} fileSystemPath
276 * @param {string} filePath
277 * @return {?WebInspector.FileSystemMapping.Entry}
279 _mappingEntryForPath: function(fileSystemPath, filePath)
281 var entries = this._fileSystemMappings[fileSystemPath];
286 for (var i = 0; i < entries.length; ++i) {
287 var pathPrefix = entries[i].pathPrefix;
288 // We are looking for the longest pathPrefix match.
289 if (entry && entry.pathPrefix.length > pathPrefix.length)
291 if (filePath.startsWith(pathPrefix.substr(1)))
298 * @param {string} fileSystemPath
299 * @param {string} pathPrefix
300 * @return {?WebInspector.FileSystemMapping.Entry}
302 _mappingEntryForPathPrefix: function(fileSystemPath, pathPrefix)
304 var entries = this._fileSystemMappings[fileSystemPath];
305 for (var i = 0; i < entries.length; ++i) {
306 if (pathPrefix === entries[i].pathPrefix)
313 * @param {string} fileSystemPath
314 * @param {string} folderPath
317 isFileExcluded: function(fileSystemPath, folderPath)
319 var excludedFolders = this._excludedFolders[fileSystemPath] || [];
320 for (var i = 0; i < excludedFolders.length; ++i) {
321 var entry = excludedFolders[i];
322 if (entry.path === folderPath)
325 var regex = WebInspector.settings.workspaceFolderExcludePattern.asRegExp();
326 return regex && regex.test(folderPath);
330 * @param {string} fileSystemPath
331 * @return {!Array.<!WebInspector.FileSystemMapping.ExcludedFolderEntry>}
333 excludedFolders: function(fileSystemPath)
335 var excludedFolders = this._excludedFolders[fileSystemPath];
336 return excludedFolders ? excludedFolders.slice() : [];
340 * @param {string} fileSystemPath
341 * @return {!Array.<!WebInspector.FileSystemMapping.Entry>}
343 mappingEntries: function(fileSystemPath)
345 return this._fileSystemMappings[fileSystemPath].slice();
349 * @param {string} url
352 hasMappingForURL: function(url)
354 return !!this._mappingEntryForURL(url);
358 * @param {string} url
359 * @return {?{fileSystemPath: string, filePath: string}}
361 fileForURL: function(url)
363 var entry = this._mappingEntryForURL(url);
367 file.fileSystemPath = entry.fileSystemPath;
368 file.filePath = entry.pathPrefix.substr(1) + url.substr(entry.urlPrefix.length);
373 * @param {string} fileSystemPath
374 * @param {string} filePath
377 urlForPath: function(fileSystemPath, filePath)
379 var entry = this._mappingEntryForPath(fileSystemPath, filePath);
382 return entry.urlPrefix + filePath.substring(entry.pathPrefix.length - 1);
386 * @param {string} url
388 removeMappingForURL: function(url)
390 var entry = this._mappingEntryForURL(url);
393 this._fileSystemMappings[entry.fileSystemPath].remove(entry);
394 this._saveToSettings();
398 * @param {string} url
399 * @param {string} fileSystemPath
400 * @param {string} filePath
402 addMappingForResource: function(url, fileSystemPath, filePath)
404 var commonPathSuffixLength = 0;
405 var normalizedFilePath = "/" + filePath;
406 for (var i = 0; i < normalizedFilePath.length; ++i) {
407 var filePathCharacter = normalizedFilePath[normalizedFilePath.length - 1 - i];
408 var urlCharacter = url[url.length - 1 - i];
409 if (filePathCharacter !== urlCharacter)
411 if (filePathCharacter === "/")
412 commonPathSuffixLength = i;
414 var pathPrefix = normalizedFilePath.substr(0, normalizedFilePath.length - commonPathSuffixLength);
415 var urlPrefix = url.substr(0, url.length - commonPathSuffixLength);
416 this.addFileMapping(fileSystemPath, urlPrefix, pathPrefix);
419 __proto__: WebInspector.Object.prototype
424 * @param {string} fileSystemPath
425 * @param {string} urlPrefix
426 * @param {string} pathPrefix
428 WebInspector.FileSystemMapping.Entry = function(fileSystemPath, urlPrefix, pathPrefix)
430 this.fileSystemPath = fileSystemPath;
431 this.urlPrefix = urlPrefix;
432 this.pathPrefix = pathPrefix;
437 * @param {string} fileSystemPath
438 * @param {string} path
440 WebInspector.FileSystemMapping.ExcludedFolderEntry = function(fileSystemPath, path)
442 this.fileSystemPath = fileSystemPath;