Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / devtools / front_end / workspace / FileSystemMapping.js
blobc8f362e751290825623b0d4c7e248decc4161023
1 /*
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
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 * @extends {WebInspector.Object}
35 WebInspector.FileSystemMapping = function()
37 WebInspector.Object.call(this);
38 this._fileSystemMappingSetting = WebInspector.settings.createLocalSetting("fileSystemMapping", {});
39 /** @type {!Object.<string, !Array.<!WebInspector.FileSystemMapping.Entry>>} */
40 this._fileSystemMappings = {};
41 this._loadFromSettings();
44 WebInspector.FileSystemMapping.Events = {
45 FileMappingAdded: "FileMappingAdded",
46 FileMappingRemoved: "FileMappingRemoved"
50 WebInspector.FileSystemMapping.prototype = {
51 _loadFromSettings: function()
53 var savedMapping = this._fileSystemMappingSetting.get();
54 this._fileSystemMappings = {};
55 for (var fileSystemPath in savedMapping) {
56 var savedFileSystemMappings = savedMapping[fileSystemPath];
58 this._fileSystemMappings[fileSystemPath] = [];
59 var fileSystemMappings = this._fileSystemMappings[fileSystemPath];
61 for (var i = 0; i < savedFileSystemMappings.length; ++i) {
62 var savedEntry = savedFileSystemMappings[i];
63 var entry = new WebInspector.FileSystemMapping.Entry(savedEntry.fileSystemPath, savedEntry.urlPrefix, savedEntry.pathPrefix);
64 fileSystemMappings.push(entry);
68 this._rebuildIndexes();
71 _saveToSettings: function()
73 var savedMapping = this._fileSystemMappings;
74 this._fileSystemMappingSetting.set(savedMapping);
76 this._rebuildIndexes();
79 _rebuildIndexes: function()
81 // We are building an index here to search for the longest url prefix match faster.
82 this._mappingForURLPrefix = {};
83 this._urlPrefixes = [];
84 for (var fileSystemPath in this._fileSystemMappings) {
85 var fileSystemMapping = this._fileSystemMappings[fileSystemPath];
86 for (var i = 0; i < fileSystemMapping.length; ++i) {
87 var entry = fileSystemMapping[i];
88 this._mappingForURLPrefix[entry.urlPrefix] = entry;
89 this._urlPrefixes.push(entry.urlPrefix);
92 this._urlPrefixes.sort();
95 /**
96 * @param {string} fileSystemPath
98 addFileSystem: function(fileSystemPath)
100 if (this._fileSystemMappings[fileSystemPath])
101 return;
103 this._fileSystemMappings[fileSystemPath] = [];
104 this._saveToSettings();
108 * @param {string} fileSystemPath
110 removeFileSystem: function(fileSystemPath)
112 if (!this._fileSystemMappings[fileSystemPath])
113 return;
114 delete this._fileSystemMappings[fileSystemPath];
115 this._saveToSettings();
119 * @param {string} fileSystemPath
120 * @param {string} urlPrefix
121 * @param {string} pathPrefix
123 addFileMapping: function(fileSystemPath, urlPrefix, pathPrefix)
125 var entry = new WebInspector.FileSystemMapping.Entry(fileSystemPath, urlPrefix, pathPrefix);
126 this._fileSystemMappings[fileSystemPath].push(entry);
127 this._saveToSettings();
128 this.dispatchEventToListeners(WebInspector.FileSystemMapping.Events.FileMappingAdded, entry);
132 * @param {string} fileSystemPath
133 * @param {string} urlPrefix
134 * @param {string} pathPrefix
136 removeFileMapping: function(fileSystemPath, urlPrefix, pathPrefix)
138 var entry = this._mappingEntryForPathPrefix(fileSystemPath, pathPrefix);
139 if (!entry)
140 return;
141 this._fileSystemMappings[fileSystemPath].remove(entry);
142 this._saveToSettings();
143 this.dispatchEventToListeners(WebInspector.FileSystemMapping.Events.FileMappingRemoved, entry);
147 * @return {!Array.<string>}
149 fileSystemPaths: function()
151 return Object.keys(this._fileSystemMappings);
155 * @param {string} url
156 * @return {?WebInspector.FileSystemMapping.Entry}
158 _mappingEntryForURL: function(url)
160 for (var i = this._urlPrefixes.length - 1; i >= 0; --i) {
161 var urlPrefix = this._urlPrefixes[i];
162 if (url.startsWith(urlPrefix))
163 return this._mappingForURLPrefix[urlPrefix];
165 return null;
169 * @param {string} fileSystemPath
170 * @param {string} filePath
171 * @return {?WebInspector.FileSystemMapping.Entry}
173 _mappingEntryForPath: function(fileSystemPath, filePath)
175 var entries = this._fileSystemMappings[fileSystemPath];
176 if (!entries)
177 return null;
179 var entry = null;
180 for (var i = 0; i < entries.length; ++i) {
181 var pathPrefix = entries[i].pathPrefix;
182 // We are looking for the longest pathPrefix match.
183 if (entry && entry.pathPrefix.length > pathPrefix.length)
184 continue;
185 if (filePath.startsWith(pathPrefix.substr(1)))
186 entry = entries[i];
188 return entry;
192 * @param {string} fileSystemPath
193 * @param {string} pathPrefix
194 * @return {?WebInspector.FileSystemMapping.Entry}
196 _mappingEntryForPathPrefix: function(fileSystemPath, pathPrefix)
198 var entries = this._fileSystemMappings[fileSystemPath];
199 for (var i = 0; i < entries.length; ++i) {
200 if (pathPrefix === entries[i].pathPrefix)
201 return entries[i];
203 return null;
207 * @param {string} fileSystemPath
208 * @return {!Array.<!WebInspector.FileSystemMapping.Entry>}
210 mappingEntries: function(fileSystemPath)
212 return this._fileSystemMappings[fileSystemPath].slice();
216 * @param {string} url
217 * @return {boolean}
219 hasMappingForURL: function(url)
221 return !!this._mappingEntryForURL(url);
225 * @param {string} url
226 * @return {?{fileSystemPath: string, filePath: string}}
228 fileForURL: function(url)
230 var entry = this._mappingEntryForURL(url);
231 if (!entry)
232 return null;
233 var file = {};
234 file.fileSystemPath = entry.fileSystemPath;
235 file.filePath = entry.pathPrefix.substr(1) + url.substr(entry.urlPrefix.length);
236 return file;
240 * @param {string} fileSystemPath
241 * @param {string} filePath
242 * @return {string}
244 urlForPath: function(fileSystemPath, filePath)
246 var entry = this._mappingEntryForPath(fileSystemPath, filePath);
247 if (!entry)
248 return "";
249 return entry.urlPrefix + filePath.substring(entry.pathPrefix.length - 1);
253 * @param {string} url
255 removeMappingForURL: function(url)
257 var entry = this._mappingEntryForURL(url);
258 if (!entry)
259 return;
260 this._fileSystemMappings[entry.fileSystemPath].remove(entry);
261 this._saveToSettings();
265 * @param {string} url
266 * @param {string} fileSystemPath
267 * @param {string} filePath
269 addMappingForResource: function(url, fileSystemPath, filePath)
271 var commonPathSuffixLength = 0;
272 var normalizedFilePath = "/" + filePath;
273 for (var i = 0; i < normalizedFilePath.length; ++i) {
274 var filePathCharacter = normalizedFilePath[normalizedFilePath.length - 1 - i];
275 var urlCharacter = url[url.length - 1 - i];
276 if (filePathCharacter !== urlCharacter)
277 break;
278 if (filePathCharacter === "/")
279 commonPathSuffixLength = i;
281 var pathPrefix = normalizedFilePath.substr(0, normalizedFilePath.length - commonPathSuffixLength);
282 var urlPrefix = url.substr(0, url.length - commonPathSuffixLength);
283 this.addFileMapping(fileSystemPath, urlPrefix, pathPrefix);
286 __proto__: WebInspector.Object.prototype
290 * @constructor
291 * @param {string} fileSystemPath
292 * @param {string} urlPrefix
293 * @param {string} pathPrefix
295 WebInspector.FileSystemMapping.Entry = function(fileSystemPath, urlPrefix, pathPrefix)
297 this.fileSystemPath = fileSystemPath;
298 this.urlPrefix = urlPrefix;
299 this.pathPrefix = pathPrefix;