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
.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();
96 * @param {string} fileSystemPath
98 addFileSystem: function(fileSystemPath
)
100 if (this._fileSystemMappings
[fileSystemPath
])
103 this._fileSystemMappings
[fileSystemPath
] = [];
104 this._saveToSettings();
108 * @param {string} fileSystemPath
110 removeFileSystem: function(fileSystemPath
)
112 if (!this._fileSystemMappings
[fileSystemPath
])
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
);
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
];
169 * @param {string} fileSystemPath
170 * @param {string} filePath
171 * @return {?WebInspector.FileSystemMapping.Entry}
173 _mappingEntryForPath: function(fileSystemPath
, filePath
)
175 var entries
= this._fileSystemMappings
[fileSystemPath
];
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
)
185 if (filePath
.startsWith(pathPrefix
.substr(1)))
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
)
207 * @param {string} fileSystemPath
208 * @return {!Array.<!WebInspector.FileSystemMapping.Entry>}
210 mappingEntries: function(fileSystemPath
)
212 return this._fileSystemMappings
[fileSystemPath
].slice();
216 * @param {string} url
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
);
234 file
.fileSystemPath
= entry
.fileSystemPath
;
235 file
.filePath
= entry
.pathPrefix
.substr(1) + url
.substr(entry
.urlPrefix
.length
);
240 * @param {string} fileSystemPath
241 * @param {string} filePath
244 urlForPath: function(fileSystemPath
, filePath
)
246 var entry
= this._mappingEntryForPath(fileSystemPath
, filePath
);
249 return entry
.urlPrefix
+ filePath
.substring(entry
.pathPrefix
.length
- 1);
253 * @param {string} url
255 removeMappingForURL: function(url
)
257 var entry
= this._mappingEntryForURL(url
);
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
)
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
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
;