Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(178)

Side by Side Diff: third_party/WebKit/Source/devtools/front_end/workspace/FileSystemMapping.js

Issue 2868543002: DevTools: move FileSystemMapping under persistence/ module (Closed)
Patch Set: fix copyright Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * Copyright (C) 2012 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
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.
17 *
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.
29 */
30
31 /**
32 * @unrestricted
33 */
34 Workspace.FileSystemMapping = class extends Common.Object {
35 /**
36 * @param {!Workspace.IsolatedFileSystemManager} fileSystemManager
37 */
38 constructor(fileSystemManager) {
39 super();
40 this._fileSystemMappingSetting = Common.settings.createLocalSetting('fileSys temMapping', {});
41 /** @type {!Object.<string, !Array.<!Workspace.FileSystemMapping.Entry>>} */
42 this._fileSystemMappings = {};
43 this._loadFromSettings();
44
45 this._eventListeners = [
46 fileSystemManager.addEventListener(
47 Workspace.IsolatedFileSystemManager.Events.FileSystemAdded, this._file SystemAdded, this),
48 fileSystemManager.addEventListener(
49 Workspace.IsolatedFileSystemManager.Events.FileSystemRemoved, this._fi leSystemRemoved, this),
50 ];
51 fileSystemManager.waitForFileSystems().then(this._fileSystemsLoaded.bind(thi s));
52 }
53
54 /**
55 * @param {!Array<!Workspace.IsolatedFileSystem>} fileSystems
56 */
57 _fileSystemsLoaded(fileSystems) {
58 for (var fileSystem of fileSystems)
59 this.addFileSystem(fileSystem.path());
60 }
61
62 /**
63 * @param {!Common.Event} event
64 */
65 _fileSystemAdded(event) {
66 var fileSystem = /** @type {!Workspace.IsolatedFileSystem} */ (event.data);
67 this.addFileSystem(fileSystem.path());
68 }
69
70 /**
71 * @param {!Common.Event} event
72 */
73 _fileSystemRemoved(event) {
74 var fileSystem = /** @type {!Workspace.IsolatedFileSystem} */ (event.data);
75 this.removeFileSystem(fileSystem.path());
76 }
77
78 _loadFromSettings() {
79 var savedMapping = this._fileSystemMappingSetting.get();
80 this._fileSystemMappings = {};
81 for (var fileSystemPath in savedMapping) {
82 var savedFileSystemMappings = savedMapping[fileSystemPath];
83 fileSystemPath = Common.ParsedURL.platformPathToURL(fileSystemPath);
84 this._fileSystemMappings[fileSystemPath] = [];
85 var fileSystemMappings = this._fileSystemMappings[fileSystemPath];
86
87 for (var i = 0; i < savedFileSystemMappings.length; ++i) {
88 var savedEntry = savedFileSystemMappings[i];
89 var entry = new Workspace.FileSystemMapping.Entry(fileSystemPath, savedE ntry.urlPrefix, savedEntry.pathPrefix);
90 fileSystemMappings.push(entry);
91 }
92 }
93
94 this._rebuildIndexes();
95 }
96
97 _saveToSettings() {
98 var setting = {};
99 for (var fileSystemPath in this._fileSystemMappings) {
100 setting[fileSystemPath] = [];
101 var entries = this._fileSystemMappings[fileSystemPath];
102 for (var entry of entries)
103 setting[fileSystemPath].push(entry);
104 }
105 this._fileSystemMappingSetting.set(setting);
106 }
107
108 _rebuildIndexes() {
109 // We are building an index here to search for the longest url prefix match faster.
110 this._mappingForURLPrefix = {};
111 this._urlPrefixes = [];
112 for (var fileSystemPath in this._fileSystemMappings) {
113 var fileSystemMapping = this._fileSystemMappings[fileSystemPath];
114 for (var i = 0; i < fileSystemMapping.length; ++i) {
115 var entry = fileSystemMapping[i];
116 this._mappingForURLPrefix[entry.urlPrefix] = entry;
117 if (this._urlPrefixes.indexOf(entry.urlPrefix) === -1)
118 this._urlPrefixes.push(entry.urlPrefix);
119 }
120 }
121 this._urlPrefixes.sort();
122 }
123
124 /**
125 * @param {string} fileSystemPath
126 */
127 addFileSystem(fileSystemPath) {
128 if (this._fileSystemMappings[fileSystemPath])
129 return;
130
131 this._fileSystemMappings[fileSystemPath] = [];
132 this._saveToSettings();
133 }
134
135 /**
136 * @param {string} fileSystemPath
137 */
138 removeFileSystem(fileSystemPath) {
139 if (!this._fileSystemMappings[fileSystemPath])
140 return;
141 delete this._fileSystemMappings[fileSystemPath];
142 this._rebuildIndexes();
143 this._saveToSettings();
144 }
145
146 /**
147 * @param {string} fileSystemPath
148 * @param {string} urlPrefix
149 * @param {string} pathPrefix
150 */
151 addFileMapping(fileSystemPath, urlPrefix, pathPrefix) {
152 if (!urlPrefix.endsWith('/'))
153 urlPrefix += '/';
154 if (!pathPrefix.endsWith('/'))
155 pathPrefix += '/';
156 if (!pathPrefix.startsWith('/'))
157 pathPrefix = '/' + pathPrefix;
158 this._innerAddFileMapping(fileSystemPath, urlPrefix, pathPrefix);
159 this._saveToSettings();
160 }
161
162 /**
163 * @param {string} fileSystemPath
164 * @param {string} urlPrefix
165 * @param {string} pathPrefix
166 */
167 _innerAddFileMapping(fileSystemPath, urlPrefix, pathPrefix) {
168 var entry = new Workspace.FileSystemMapping.Entry(fileSystemPath, urlPrefix, pathPrefix);
169 this._fileSystemMappings[fileSystemPath].push(entry);
170 this._rebuildIndexes();
171 this.dispatchEventToListeners(Workspace.FileSystemMapping.Events.FileMapping Added, entry);
172 }
173
174 /**
175 * @param {string} fileSystemPath
176 * @param {string} urlPrefix
177 * @param {string} pathPrefix
178 */
179 removeFileMapping(fileSystemPath, urlPrefix, pathPrefix) {
180 var entry = this._configurableMappingEntryForPathPrefix(fileSystemPath, path Prefix);
181 if (!entry)
182 return;
183 this._fileSystemMappings[fileSystemPath].remove(entry);
184 this._rebuildIndexes();
185 this._saveToSettings();
186 this.dispatchEventToListeners(Workspace.FileSystemMapping.Events.FileMapping Removed, entry);
187 }
188
189 /**
190 * @param {string} url
191 * @return {?Workspace.FileSystemMapping.Entry}
192 */
193 _mappingEntryForURL(url) {
194 for (var i = this._urlPrefixes.length - 1; i >= 0; --i) {
195 var urlPrefix = this._urlPrefixes[i];
196 if (url.startsWith(urlPrefix))
197 return this._mappingForURLPrefix[urlPrefix];
198 }
199 return null;
200 }
201
202 /**
203 * @param {string} fileSystemPath
204 * @param {string} filePath
205 * @return {?Workspace.FileSystemMapping.Entry}
206 */
207 _mappingEntryForPath(fileSystemPath, filePath) {
208 var entries = this._fileSystemMappings[fileSystemPath];
209 if (!entries)
210 return null;
211
212 var entry = null;
213 for (var i = 0; i < entries.length; ++i) {
214 var pathPrefix = entries[i].pathPrefix;
215 // We are looking for the longest pathPrefix match.
216 if (entry && entry.pathPrefix.length > pathPrefix.length)
217 continue;
218 if (filePath.startsWith(pathPrefix))
219 entry = entries[i];
220 }
221 return entry;
222 }
223
224 /**
225 * @param {string} fileSystemPath
226 * @param {string} pathPrefix
227 * @return {?Workspace.FileSystemMapping.Entry}
228 */
229 _configurableMappingEntryForPathPrefix(fileSystemPath, pathPrefix) {
230 var entries = this._fileSystemMappings[fileSystemPath];
231 for (var i = 0; i < entries.length; ++i) {
232 if (pathPrefix === entries[i].pathPrefix)
233 return entries[i];
234 }
235 return null;
236 }
237
238 /**
239 * @param {string} fileSystemPath
240 * @return {!Array.<!Workspace.FileSystemMapping.Entry>}
241 */
242 mappingEntries(fileSystemPath) {
243 return this._fileSystemMappings[fileSystemPath].slice();
244 }
245
246 /**
247 * @param {string} url
248 * @return {boolean}
249 */
250 hasMappingForNetworkURL(url) {
251 return !!this._mappingEntryForURL(url);
252 }
253
254 /**
255 * @param {string} url
256 * @return {?{fileSystemPath: string, fileURL: string}}
257 */
258 fileForURL(url) {
259 var entry = this._mappingEntryForURL(url);
260 if (!entry)
261 return null;
262 var file = {};
263 file.fileSystemPath = entry.fileSystemPath;
264 file.fileURL = entry.fileSystemPath + entry.pathPrefix + url.substr(entry.ur lPrefix.length);
265 return file;
266 }
267
268 /**
269 * @param {string} fileSystemPath
270 * @param {string} filePath
271 * @return {string}
272 */
273 networkURLForFileSystemURL(fileSystemPath, filePath) {
274 var relativePath = filePath.substring(fileSystemPath.length);
275 var entry = this._mappingEntryForPath(fileSystemPath, relativePath);
276 if (!entry)
277 return '';
278 return entry.urlPrefix + relativePath.substring(entry.pathPrefix.length);
279 }
280
281 /**
282 * @param {string} url
283 */
284 removeMappingForURL(url) {
285 var entry = this._mappingEntryForURL(url);
286 if (!entry)
287 return;
288 this._fileSystemMappings[entry.fileSystemPath].remove(entry);
289 this._saveToSettings();
290 }
291
292 /**
293 * @param {string} url
294 * @param {string} fileSystemPath
295 * @param {string} filePath
296 */
297 addMappingForResource(url, fileSystemPath, filePath) {
298 var commonPathSuffixLength = 0;
299 for (var i = 0; i < filePath.length; ++i) {
300 var filePathCharacter = filePath[filePath.length - 1 - i];
301 var urlCharacter = url[url.length - 1 - i];
302 if (filePathCharacter !== urlCharacter)
303 break;
304 if (filePathCharacter === '/')
305 commonPathSuffixLength = i;
306 }
307 var from = fileSystemPath.length;
308 var to = filePath.length - commonPathSuffixLength;
309 var pathPrefix = filePath.substring(from, to);
310 var urlPrefix = url.substr(0, url.length - commonPathSuffixLength);
311 if (to >= from)
312 this.addFileMapping(fileSystemPath, urlPrefix, pathPrefix);
313 else
314 this.addFileMapping(fileSystemPath, urlPrefix + pathPrefix, '/');
315 }
316
317 resetForTesting() {
318 this._fileSystemMappings = {};
319 }
320
321 dispose() {
322 Common.EventTarget.removeEventListeners(this._eventListeners);
323 }
324 };
325
326 /** @enum {symbol} */
327 Workspace.FileSystemMapping.Events = {
328 FileMappingAdded: Symbol('FileMappingAdded'),
329 FileMappingRemoved: Symbol('FileMappingRemoved')
330 };
331
332 /**
333 * @unrestricted
334 */
335 Workspace.FileSystemMapping.Entry = class {
336 /**
337 * @param {string} fileSystemPath
338 * @param {string} urlPrefix
339 * @param {string} pathPrefix
340 */
341 constructor(fileSystemPath, urlPrefix, pathPrefix) {
342 this.fileSystemPath = fileSystemPath;
343 this.urlPrefix = urlPrefix;
344 this.pathPrefix = pathPrefix;
345 }
346 };
347
348 /**
349 * @type {!Workspace.FileSystemMapping}
350 */
351 Workspace.fileSystemMapping;
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698