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

Side by Side Diff: third_party/WebKit/Source/devtools/front_end/bindings/CompilerScriptMapping.js

Issue 2466123002: DevTools: reformat front-end code to match chromium style. (Closed)
Patch Set: all done Created 4 years, 1 month 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
1 /* 1 /*
2 * Copyright (C) 2012 Google Inc. All rights reserved. 2 * Copyright (C) 2012 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer 11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the 12 * in the documentation and/or other materials provided with the
13 * distribution. 13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its 14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from 15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission. 16 * this software without specific prior written permission.
17 * 17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 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. 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */ 29 */
30
31 /** 30 /**
32 * @constructor
33 * @implements {WebInspector.DebuggerSourceMapping} 31 * @implements {WebInspector.DebuggerSourceMapping}
34 * @param {!WebInspector.DebuggerModel} debuggerModel 32 * @unrestricted
35 * @param {!WebInspector.Workspace} workspace
36 * @param {!WebInspector.NetworkMapping} networkMapping
37 * @param {!WebInspector.NetworkProject} networkProject
38 * @param {!WebInspector.DebuggerWorkspaceBinding} debuggerWorkspaceBinding
39 */ 33 */
40 WebInspector.CompilerScriptMapping = function(debuggerModel, workspace, networkM apping, networkProject, debuggerWorkspaceBinding) 34 WebInspector.CompilerScriptMapping = class {
41 { 35 /**
36 * @param {!WebInspector.DebuggerModel} debuggerModel
37 * @param {!WebInspector.Workspace} workspace
38 * @param {!WebInspector.NetworkMapping} networkMapping
39 * @param {!WebInspector.NetworkProject} networkProject
40 * @param {!WebInspector.DebuggerWorkspaceBinding} debuggerWorkspaceBinding
41 */
42 constructor(debuggerModel, workspace, networkMapping, networkProject, debugger WorkspaceBinding) {
42 this._target = debuggerModel.target(); 43 this._target = debuggerModel.target();
43 this._debuggerModel = debuggerModel; 44 this._debuggerModel = debuggerModel;
44 this._networkMapping = networkMapping; 45 this._networkMapping = networkMapping;
45 this._networkProject = networkProject; 46 this._networkProject = networkProject;
46 this._debuggerWorkspaceBinding = debuggerWorkspaceBinding; 47 this._debuggerWorkspaceBinding = debuggerWorkspaceBinding;
47 48
48 /** @type {!Map<string, !Promise<?WebInspector.TextSourceMap>>} */ 49 /** @type {!Map<string, !Promise<?WebInspector.TextSourceMap>>} */
49 this._sourceMapLoadingPromises = new Map(); 50 this._sourceMapLoadingPromises = new Map();
50 /** @type {!Map<string, !WebInspector.TextSourceMap>} */ 51 /** @type {!Map<string, !WebInspector.TextSourceMap>} */
51 this._sourceMapForScriptId = new Map(); 52 this._sourceMapForScriptId = new Map();
52 /** @type {!Map.<!WebInspector.TextSourceMap, !WebInspector.Script>} */ 53 /** @type {!Map.<!WebInspector.TextSourceMap, !WebInspector.Script>} */
53 this._scriptForSourceMap = new Map(); 54 this._scriptForSourceMap = new Map();
54 /** @type {!Map.<string, !WebInspector.TextSourceMap>} */ 55 /** @type {!Map.<string, !WebInspector.TextSourceMap>} */
55 this._sourceMapForURL = new Map(); 56 this._sourceMapForURL = new Map();
56 /** @type {!Map.<string, !WebInspector.UISourceCode>} */ 57 /** @type {!Map.<string, !WebInspector.UISourceCode>} */
57 this._stubUISourceCodes = new Map(); 58 this._stubUISourceCodes = new Map();
58 59
59 var projectId = WebInspector.CompilerScriptMapping.projectIdForTarget(this._ target); 60 var projectId = WebInspector.CompilerScriptMapping.projectIdForTarget(this._ target);
60 this._stubProject = new WebInspector.ContentProviderBasedProject(workspace, projectId, WebInspector.projectTypes.Service, ""); 61 this._stubProject =
62 new WebInspector.ContentProviderBasedProject(workspace, projectId, WebIn spector.projectTypes.Service, '');
61 this._eventListeners = [ 63 this._eventListeners = [
62 workspace.addEventListener(WebInspector.Workspace.Events.UISourceCodeAdd ed, this._uiSourceCodeAddedToWorkspace, this), 64 workspace.addEventListener(
63 debuggerModel.addEventListener(WebInspector.DebuggerModel.Events.GlobalO bjectCleared, this._debuggerReset, this) 65 WebInspector.Workspace.Events.UISourceCodeAdded, this._uiSourceCodeAdd edToWorkspace, this),
66 debuggerModel.addEventListener(WebInspector.DebuggerModel.Events.GlobalObj ectCleared, this._debuggerReset, this)
64 ]; 67 ];
68 }
69
70 /**
71 * @param {!WebInspector.UISourceCode} uiSourceCode
72 * @return {?string}
73 */
74 static uiSourceCodeOrigin(uiSourceCode) {
75 return uiSourceCode[WebInspector.CompilerScriptMapping._originSymbol] || nul l;
76 }
77
78 /**
79 * @param {!WebInspector.Target} target
80 * @return {string}
81 */
82 static projectIdForTarget(target) {
83 return 'compiler-script-project:' + target.id();
84 }
85
86 /**
87 * @param {!WebInspector.DebuggerModel.Location} rawLocation
88 * @return {boolean}
89 */
90 mapsToSourceCode(rawLocation) {
91 var sourceMap = this._sourceMapForScriptId.get(rawLocation.scriptId);
92 if (!sourceMap) {
93 return true;
94 }
95 return !!sourceMap.findEntry(rawLocation.lineNumber, rawLocation.columnNumbe r);
96 }
97
98 /**
99 * @override
100 * @param {!WebInspector.DebuggerModel.Location} rawLocation
101 * @return {?WebInspector.UILocation}
102 */
103 rawLocationToUILocation(rawLocation) {
104 var debuggerModelLocation = /** @type {!WebInspector.DebuggerModel.Location} */ (rawLocation);
105
106 var stubUISourceCode = this._stubUISourceCodes.get(debuggerModelLocation.scr iptId);
107 if (stubUISourceCode)
108 return new WebInspector.UILocation(stubUISourceCode, rawLocation.lineNumbe r, rawLocation.columnNumber);
109
110 var sourceMap = this._sourceMapForScriptId.get(debuggerModelLocation.scriptI d);
111 if (!sourceMap)
112 return null;
113 var lineNumber = debuggerModelLocation.lineNumber;
114 var columnNumber = debuggerModelLocation.columnNumber || 0;
115 var entry = sourceMap.findEntry(lineNumber, columnNumber);
116 if (!entry || !entry.sourceURL)
117 return null;
118 var script = rawLocation.script();
119 if (!script)
120 return null;
121 var uiSourceCode = this._networkMapping.uiSourceCodeForScriptURL(/** @type { string} */ (entry.sourceURL), script);
122 if (!uiSourceCode)
123 return null;
124 return uiSourceCode.uiLocation(
125 /** @type {number} */ (entry.sourceLineNumber), /** @type {number} */ (e ntry.sourceColumnNumber));
126 }
127
128 /**
129 * @override
130 * @param {!WebInspector.UISourceCode} uiSourceCode
131 * @param {number} lineNumber
132 * @param {number} columnNumber
133 * @return {?WebInspector.DebuggerModel.Location}
134 */
135 uiLocationToRawLocation(uiSourceCode, lineNumber, columnNumber) {
136 if (uiSourceCode.project().type() === WebInspector.projectTypes.Service)
137 return null;
138 var sourceMap = this._sourceMapForURL.get(uiSourceCode.url());
139 if (!sourceMap)
140 return null;
141 var script = /** @type {!WebInspector.Script} */ (this._scriptForSourceMap.g et(sourceMap));
142 console.assert(script);
143 var entry = sourceMap.firstSourceLineMapping(uiSourceCode.url(), lineNumber) ;
144 if (!entry)
145 return null;
146 return this._debuggerModel.createRawLocation(script, entry.lineNumber, entry .columnNumber);
147 }
148
149 /**
150 * @param {!WebInspector.Script} script
151 */
152 addScript(script) {
153 if (!script.sourceMapURL) {
154 script.addEventListener(WebInspector.Script.Events.SourceMapURLAdded, this ._sourceMapURLAdded.bind(this));
155 return;
156 }
157
158 this._processScript(script);
159 }
160
161 /**
162 * @param {!WebInspector.Script} script
163 * @return {?WebInspector.TextSourceMap}
164 */
165 sourceMapForScript(script) {
166 return this._sourceMapForScriptId.get(script.scriptId) || null;
167 }
168
169 /**
170 * @param {!WebInspector.Script} script
171 */
172 maybeLoadSourceMap(script) {
173 if (!script.sourceMapURL)
174 return;
175 if (this._sourceMapLoadingPromises.has(script.sourceMapURL))
176 return;
177 if (this._sourceMapForScriptId.has(script.scriptId))
178 return;
179 this._processScript(script);
180 }
181
182 /**
183 * @param {!WebInspector.Event} event
184 */
185 _sourceMapURLAdded(event) {
186 var script = /** @type {!WebInspector.Script} */ (event.target);
187 if (!script.sourceMapURL)
188 return;
189 this._processScript(script);
190 }
191
192 /**
193 * @param {!WebInspector.Script} script
194 */
195 _processScript(script) {
196 if (WebInspector.blackboxManager.isBlackboxedURL(script.sourceURL, script.is ContentScript()))
197 return;
198 // Create stub UISourceCode for the time source mapping is being loaded.
199 var stubUISourceCode = this._stubProject.addContentProvider(
200 script.sourceURL,
201 WebInspector.StaticContentProvider.fromString(
202 script.sourceURL, WebInspector.resourceTypes.Script,
203 '\n\n\n\n\n// Please wait a bit.\n// Compiled script is not shown wh ile source map is being loaded!'));
204 this._stubUISourceCodes.set(script.scriptId, stubUISourceCode);
205
206 this._debuggerWorkspaceBinding.pushSourceMapping(script, this);
207 this._loadSourceMapForScript(script).then(this._sourceMapLoaded.bind(this, s cript, stubUISourceCode.url()));
208 }
209
210 /**
211 * @param {!WebInspector.Script} script
212 * @param {string} uiSourceCodePath
213 * @param {?WebInspector.TextSourceMap} sourceMap
214 */
215 _sourceMapLoaded(script, uiSourceCodePath, sourceMap) {
216 WebInspector.blackboxManager.sourceMapLoaded(script, sourceMap);
217
218 this._stubUISourceCodes.delete(script.scriptId);
219 this._stubProject.removeFile(uiSourceCodePath);
220
221 if (!sourceMap) {
222 this._debuggerWorkspaceBinding.updateLocations(script);
223 return;
224 }
225
226 if (this._scriptForSourceMap.get(sourceMap)) {
227 this._sourceMapForScriptId.set(script.scriptId, sourceMap);
228 this._debuggerWorkspaceBinding.updateLocations(script);
229 return;
230 }
231
232 this._sourceMapForScriptId.set(script.scriptId, sourceMap);
233 this._scriptForSourceMap.set(sourceMap, script);
234
235 // Report sources.
236 var missingSources = [];
237 for (var sourceURL of sourceMap.sourceURLs()) {
238 if (this._sourceMapForURL.get(sourceURL))
239 continue;
240 this._sourceMapForURL.set(sourceURL, sourceMap);
241 var uiSourceCode = this._networkMapping.uiSourceCodeForScriptURL(sourceURL , script);
242 if (!uiSourceCode) {
243 var contentProvider = sourceMap.sourceContentProvider(sourceURL, WebInsp ector.resourceTypes.SourceMapScript);
244 var embeddedContent = sourceMap.embeddedContentByURL(sourceURL);
245 var embeddedContentLength = typeof embeddedContent === 'string' ? embedd edContent.length : null;
246 uiSourceCode = this._networkProject.addFile(
247 contentProvider, WebInspector.ResourceTreeFrame.fromScript(script), script.isContentScript(),
248 embeddedContentLength);
249 uiSourceCode[WebInspector.CompilerScriptMapping._originSymbol] = script. sourceURL;
250 }
251 if (uiSourceCode) {
252 this._bindUISourceCode(uiSourceCode);
253 } else {
254 if (missingSources.length < 3)
255 missingSources.push(sourceURL);
256 else if (missingSources.peekLast() !== '\u2026')
257 missingSources.push('\u2026');
258 }
259 }
260 if (missingSources.length) {
261 WebInspector.console.warn(WebInspector.UIString(
262 'Source map %s points to the files missing from the workspace: [%s]', sourceMap.url(),
263 missingSources.join(', ')));
264 }
265
266 this._debuggerWorkspaceBinding.updateLocations(script);
267 }
268
269 /**
270 * @override
271 * @return {boolean}
272 */
273 isIdentity() {
274 return false;
275 }
276
277 /**
278 * @override
279 * @param {!WebInspector.UISourceCode} uiSourceCode
280 * @param {number} lineNumber
281 * @return {boolean}
282 */
283 uiLineHasMapping(uiSourceCode, lineNumber) {
284 var sourceMap = this._sourceMapForURL.get(uiSourceCode.url());
285 if (!sourceMap)
286 return true;
287 return !!sourceMap.firstSourceLineMapping(uiSourceCode.url(), lineNumber);
288 }
289
290 /**
291 * @param {!WebInspector.UISourceCode} uiSourceCode
292 */
293 _bindUISourceCode(uiSourceCode) {
294 this._debuggerWorkspaceBinding.setSourceMapping(this._target, uiSourceCode, this);
295 }
296
297 /**
298 * @param {!WebInspector.UISourceCode} uiSourceCode
299 */
300 _unbindUISourceCode(uiSourceCode) {
301 this._debuggerWorkspaceBinding.setSourceMapping(this._target, uiSourceCode, null);
302 }
303
304 /**
305 * @param {!WebInspector.Event} event
306 */
307 _uiSourceCodeAddedToWorkspace(event) {
308 var uiSourceCode = /** @type {!WebInspector.UISourceCode} */ (event.data);
309 if (!this._sourceMapForURL.get(uiSourceCode.url()))
310 return;
311 this._bindUISourceCode(uiSourceCode);
312 }
313
314 /**
315 * @param {!WebInspector.Script} script
316 * @return {!Promise<?WebInspector.TextSourceMap>}
317 */
318 _loadSourceMapForScript(script) {
319 // script.sourceURL can be a random string, but is generally an absolute pat h -> complete it to inspected page url for
320 // relative links.
321 var scriptURL = WebInspector.ParsedURL.completeURL(this._target.inspectedURL (), script.sourceURL);
322 if (!scriptURL)
323 return Promise.resolve(/** @type {?WebInspector.TextSourceMap} */ (null));
324
325 console.assert(script.sourceMapURL);
326 var scriptSourceMapURL = /** @type {string} */ (script.sourceMapURL);
327
328 var sourceMapURL = WebInspector.ParsedURL.completeURL(scriptURL, scriptSourc eMapURL);
329 if (!sourceMapURL)
330 return Promise.resolve(/** @type {?WebInspector.TextSourceMap} */ (null));
331
332 var loadingPromise = this._sourceMapLoadingPromises.get(sourceMapURL);
333 if (!loadingPromise) {
334 loadingPromise =
335 WebInspector.TextSourceMap.load(sourceMapURL, scriptURL).then(sourceMa pLoaded.bind(this, sourceMapURL));
336 this._sourceMapLoadingPromises.set(sourceMapURL, loadingPromise);
337 }
338 return loadingPromise;
339
340 /**
341 * @param {string} url
342 * @param {?WebInspector.TextSourceMap} sourceMap
343 * @this {WebInspector.CompilerScriptMapping}
344 */
345 function sourceMapLoaded(url, sourceMap) {
346 if (!sourceMap) {
347 this._sourceMapLoadingPromises.delete(url);
348 return null;
349 }
350
351 return sourceMap;
352 }
353 }
354
355 _debuggerReset() {
356 /**
357 * @param {!WebInspector.TextSourceMap} sourceMap
358 * @this {WebInspector.CompilerScriptMapping}
359 */
360 function unbindSourceMapSources(sourceMap) {
361 var script = this._scriptForSourceMap.get(sourceMap);
362 if (!script)
363 return;
364 for (var sourceURL of sourceMap.sourceURLs()) {
365 var uiSourceCode = this._networkMapping.uiSourceCodeForScriptURL(sourceU RL, script);
366 if (uiSourceCode)
367 this._unbindUISourceCode(uiSourceCode);
368 }
369 }
370
371 this._sourceMapForURL.valuesArray().forEach(unbindSourceMapSources.bind(this ));
372
373 this._sourceMapLoadingPromises.clear();
374 this._sourceMapForScriptId.clear();
375 this._scriptForSourceMap.clear();
376 this._sourceMapForURL.clear();
377 }
378
379 dispose() {
380 WebInspector.EventTarget.removeEventListeners(this._eventListeners);
381 this._debuggerReset();
382 this._stubProject.dispose();
383 }
65 }; 384 };
66 385
67 WebInspector.CompilerScriptMapping._originSymbol = Symbol("origin"); 386 WebInspector.CompilerScriptMapping._originSymbol = Symbol('origin');
68 387
69 /** 388
70 * @param {!WebInspector.UISourceCode} uiSourceCode
71 * @return {?string}
72 */
73 WebInspector.CompilerScriptMapping.uiSourceCodeOrigin = function(uiSourceCode)
74 {
75 return uiSourceCode[WebInspector.CompilerScriptMapping._originSymbol] || nul l;
76 };
77
78 WebInspector.CompilerScriptMapping.prototype = {
79 /**
80 * @param {!WebInspector.DebuggerModel.Location} rawLocation
81 * @return {boolean}
82 */
83 mapsToSourceCode: function(rawLocation) {
84 var sourceMap = this._sourceMapForScriptId.get(rawLocation.scriptId);
85 if (!sourceMap) {
86 return true;
87 }
88 return !!sourceMap.findEntry(rawLocation.lineNumber, rawLocation.columnN umber);
89 },
90
91 /**
92 * @override
93 * @param {!WebInspector.DebuggerModel.Location} rawLocation
94 * @return {?WebInspector.UILocation}
95 */
96 rawLocationToUILocation: function(rawLocation)
97 {
98 var debuggerModelLocation = /** @type {!WebInspector.DebuggerModel.Locat ion} */ (rawLocation);
99
100 var stubUISourceCode = this._stubUISourceCodes.get(debuggerModelLocation .scriptId);
101 if (stubUISourceCode)
102 return new WebInspector.UILocation(stubUISourceCode, rawLocation.lin eNumber, rawLocation.columnNumber);
103
104 var sourceMap = this._sourceMapForScriptId.get(debuggerModelLocation.scr iptId);
105 if (!sourceMap)
106 return null;
107 var lineNumber = debuggerModelLocation.lineNumber;
108 var columnNumber = debuggerModelLocation.columnNumber || 0;
109 var entry = sourceMap.findEntry(lineNumber, columnNumber);
110 if (!entry || !entry.sourceURL)
111 return null;
112 var script = rawLocation.script();
113 if (!script)
114 return null;
115 var uiSourceCode = this._networkMapping.uiSourceCodeForScriptURL(/** @ty pe {string} */ (entry.sourceURL), script);
116 if (!uiSourceCode)
117 return null;
118 return uiSourceCode.uiLocation(/** @type {number} */ (entry.sourceLineNu mber), /** @type {number} */ (entry.sourceColumnNumber));
119 },
120
121 /**
122 * @override
123 * @param {!WebInspector.UISourceCode} uiSourceCode
124 * @param {number} lineNumber
125 * @param {number} columnNumber
126 * @return {?WebInspector.DebuggerModel.Location}
127 */
128 uiLocationToRawLocation: function(uiSourceCode, lineNumber, columnNumber)
129 {
130 if (uiSourceCode.project().type() === WebInspector.projectTypes.Service)
131 return null;
132 var sourceMap = this._sourceMapForURL.get(uiSourceCode.url());
133 if (!sourceMap)
134 return null;
135 var script = /** @type {!WebInspector.Script} */ (this._scriptForSourceM ap.get(sourceMap));
136 console.assert(script);
137 var entry = sourceMap.firstSourceLineMapping(uiSourceCode.url(), lineNum ber);
138 if (!entry)
139 return null;
140 return this._debuggerModel.createRawLocation(script, entry.lineNumber, e ntry.columnNumber);
141 },
142
143 /**
144 * @param {!WebInspector.Script} script
145 */
146 addScript: function(script)
147 {
148 if (!script.sourceMapURL) {
149 script.addEventListener(WebInspector.Script.Events.SourceMapURLAdded , this._sourceMapURLAdded.bind(this));
150 return;
151 }
152
153 this._processScript(script);
154 },
155
156 /**
157 * @param {!WebInspector.Script} script
158 * @return {?WebInspector.TextSourceMap}
159 */
160 sourceMapForScript: function(script)
161 {
162 return this._sourceMapForScriptId.get(script.scriptId) || null;
163 },
164
165 /**
166 * @param {!WebInspector.Script} script
167 */
168 maybeLoadSourceMap: function(script)
169 {
170 if (!script.sourceMapURL)
171 return;
172 if (this._sourceMapLoadingPromises.has(script.sourceMapURL))
173 return;
174 if (this._sourceMapForScriptId.has(script.scriptId))
175 return;
176 this._processScript(script);
177 },
178
179 /**
180 * @param {!WebInspector.Event} event
181 */
182 _sourceMapURLAdded: function(event)
183 {
184 var script = /** @type {!WebInspector.Script} */ (event.target);
185 if (!script.sourceMapURL)
186 return;
187 this._processScript(script);
188 },
189
190 /**
191 * @param {!WebInspector.Script} script
192 */
193 _processScript: function(script)
194 {
195 if (WebInspector.blackboxManager.isBlackboxedURL(script.sourceURL, scrip t.isContentScript()))
196 return;
197 // Create stub UISourceCode for the time source mapping is being loaded.
198 var stubUISourceCode = this._stubProject.addContentProvider(script.sourc eURL, WebInspector.StaticContentProvider.fromString(script.sourceURL, WebInspect or.resourceTypes.Script, "\n\n\n\n\n// Please wait a bit.\n// Compiled script is not shown while source map is being loaded!"));
199 this._stubUISourceCodes.set(script.scriptId, stubUISourceCode);
200
201 this._debuggerWorkspaceBinding.pushSourceMapping(script, this);
202 this._loadSourceMapForScript(script).then(this._sourceMapLoaded.bind(thi s, script, stubUISourceCode.url()));
203 },
204
205 /**
206 * @param {!WebInspector.Script} script
207 * @param {string} uiSourceCodePath
208 * @param {?WebInspector.TextSourceMap} sourceMap
209 */
210 _sourceMapLoaded: function(script, uiSourceCodePath, sourceMap)
211 {
212 WebInspector.blackboxManager.sourceMapLoaded(script, sourceMap);
213
214 this._stubUISourceCodes.delete(script.scriptId);
215 this._stubProject.removeFile(uiSourceCodePath);
216
217 if (!sourceMap) {
218 this._debuggerWorkspaceBinding.updateLocations(script);
219 return;
220 }
221
222 if (this._scriptForSourceMap.get(sourceMap)) {
223 this._sourceMapForScriptId.set(script.scriptId, sourceMap);
224 this._debuggerWorkspaceBinding.updateLocations(script);
225 return;
226 }
227
228 this._sourceMapForScriptId.set(script.scriptId, sourceMap);
229 this._scriptForSourceMap.set(sourceMap, script);
230
231 // Report sources.
232 var missingSources = [];
233 for (var sourceURL of sourceMap.sourceURLs()) {
234 if (this._sourceMapForURL.get(sourceURL))
235 continue;
236 this._sourceMapForURL.set(sourceURL, sourceMap);
237 var uiSourceCode = this._networkMapping.uiSourceCodeForScriptURL(sou rceURL, script);
238 if (!uiSourceCode) {
239 var contentProvider = sourceMap.sourceContentProvider(sourceURL, WebInspector.resourceTypes.SourceMapScript);
240 var embeddedContent = sourceMap.embeddedContentByURL(sourceURL);
241 var embeddedContentLength = typeof embeddedContent === "string" ? embeddedContent.length : null;
242 uiSourceCode = this._networkProject.addFile(contentProvider, Web Inspector.ResourceTreeFrame.fromScript(script), script.isContentScript(), embedd edContentLength);
243 uiSourceCode[WebInspector.CompilerScriptMapping._originSymbol] = script.sourceURL;
244 }
245 if (uiSourceCode) {
246 this._bindUISourceCode(uiSourceCode);
247 } else {
248 if (missingSources.length < 3)
249 missingSources.push(sourceURL);
250 else if (missingSources.peekLast() !== "\u2026")
251 missingSources.push("\u2026");
252 }
253 }
254 if (missingSources.length) {
255 WebInspector.console.warn(
256 WebInspector.UIString("Source map %s points to the files missing from the workspace: [%s]",
257 sourceMap.url(), missingSources.join(", ") ));
258 }
259
260 this._debuggerWorkspaceBinding.updateLocations(script);
261 },
262
263 /**
264 * @override
265 * @return {boolean}
266 */
267 isIdentity: function()
268 {
269 return false;
270 },
271
272 /**
273 * @override
274 * @param {!WebInspector.UISourceCode} uiSourceCode
275 * @param {number} lineNumber
276 * @return {boolean}
277 */
278 uiLineHasMapping: function(uiSourceCode, lineNumber)
279 {
280 var sourceMap = this._sourceMapForURL.get(uiSourceCode.url());
281 if (!sourceMap)
282 return true;
283 return !!sourceMap.firstSourceLineMapping(uiSourceCode.url(), lineNumber );
284 },
285
286 /**
287 * @param {!WebInspector.UISourceCode} uiSourceCode
288 */
289 _bindUISourceCode: function(uiSourceCode)
290 {
291 this._debuggerWorkspaceBinding.setSourceMapping(this._target, uiSourceCo de, this);
292 },
293
294 /**
295 * @param {!WebInspector.UISourceCode} uiSourceCode
296 */
297 _unbindUISourceCode: function(uiSourceCode)
298 {
299 this._debuggerWorkspaceBinding.setSourceMapping(this._target, uiSourceCo de, null);
300 },
301
302 /**
303 * @param {!WebInspector.Event} event
304 */
305 _uiSourceCodeAddedToWorkspace: function(event)
306 {
307 var uiSourceCode = /** @type {!WebInspector.UISourceCode} */ (event.data );
308 if (!this._sourceMapForURL.get(uiSourceCode.url()))
309 return;
310 this._bindUISourceCode(uiSourceCode);
311 },
312
313 /**
314 * @param {!WebInspector.Script} script
315 * @return {!Promise<?WebInspector.TextSourceMap>}
316 */
317 _loadSourceMapForScript: function(script)
318 {
319 // script.sourceURL can be a random string, but is generally an absolute path -> complete it to inspected page url for
320 // relative links.
321 var scriptURL = WebInspector.ParsedURL.completeURL(this._target.inspecte dURL(), script.sourceURL);
322 if (!scriptURL)
323 return Promise.resolve(/** @type {?WebInspector.TextSourceMap} */(nu ll));
324
325 console.assert(script.sourceMapURL);
326 var scriptSourceMapURL = /** @type {string} */ (script.sourceMapURL);
327
328 var sourceMapURL = WebInspector.ParsedURL.completeURL(scriptURL, scriptS ourceMapURL);
329 if (!sourceMapURL)
330 return Promise.resolve(/** @type {?WebInspector.TextSourceMap} */(nu ll));
331
332 var loadingPromise = this._sourceMapLoadingPromises.get(sourceMapURL);
333 if (!loadingPromise) {
334 loadingPromise = WebInspector.TextSourceMap.load(sourceMapURL, scrip tURL).then(sourceMapLoaded.bind(this, sourceMapURL));
335 this._sourceMapLoadingPromises.set(sourceMapURL, loadingPromise);
336 }
337 return loadingPromise;
338
339 /**
340 * @param {string} url
341 * @param {?WebInspector.TextSourceMap} sourceMap
342 * @this {WebInspector.CompilerScriptMapping}
343 */
344 function sourceMapLoaded(url, sourceMap)
345 {
346 if (!sourceMap) {
347 this._sourceMapLoadingPromises.delete(url);
348 return null;
349 }
350
351 return sourceMap;
352 }
353 },
354
355 _debuggerReset: function()
356 {
357 /**
358 * @param {!WebInspector.TextSourceMap} sourceMap
359 * @this {WebInspector.CompilerScriptMapping}
360 */
361 function unbindSourceMapSources(sourceMap)
362 {
363 var script = this._scriptForSourceMap.get(sourceMap);
364 if (!script)
365 return;
366 for (var sourceURL of sourceMap.sourceURLs()) {
367 var uiSourceCode = this._networkMapping.uiSourceCodeForScriptURL (sourceURL, script);
368 if (uiSourceCode)
369 this._unbindUISourceCode(uiSourceCode);
370 }
371 }
372
373 this._sourceMapForURL.valuesArray().forEach(unbindSourceMapSources.bind( this));
374
375 this._sourceMapLoadingPromises.clear();
376 this._sourceMapForScriptId.clear();
377 this._scriptForSourceMap.clear();
378 this._sourceMapForURL.clear();
379 },
380
381 dispose: function()
382 {
383 WebInspector.EventTarget.removeEventListeners(this._eventListeners);
384 this._debuggerReset();
385 this._stubProject.dispose();
386 }
387 };
388
389 /**
390 * @param {!WebInspector.Target} target
391 * @return {string}
392 */
393 WebInspector.CompilerScriptMapping.projectIdForTarget = function(target)
394 {
395 return "compiler-script-project:" + target.id();
396 };
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698