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 * @implements {WebInspector.ScriptSourceMapping}
34 * @param {!WebInspector.DebuggerModel} debuggerModel
35 * @param {!WebInspector.Workspace} workspace
37 WebInspector.ResourceScriptMapping = function(debuggerModel, workspace)
39 this._debuggerModel = debuggerModel;
40 this._workspace = workspace;
41 this._workspace.addEventListener(WebInspector.Workspace.Events.UISourceCodeAdded, this._uiSourceCodeAddedToWorkspace, this);
43 debuggerModel.addEventListener(WebInspector.DebuggerModel.Events.GlobalObjectCleared, this._debuggerReset, this);
47 WebInspector.ResourceScriptMapping.prototype = {
49 * @param {!WebInspector.RawLocation} rawLocation
50 * @return {?WebInspector.UILocation}
52 rawLocationToUILocation: function(rawLocation)
54 var debuggerModelLocation = /** @type {!WebInspector.DebuggerModel.Location} */ (rawLocation);
55 var script = this._debuggerModel.scriptForId(debuggerModelLocation.scriptId);
56 var uiSourceCode = this._workspaceUISourceCodeForScript(script);
59 var scriptFile = uiSourceCode.scriptFile();
60 if (scriptFile && ((scriptFile.hasDivergedFromVM() && !scriptFile.isMergingToVM()) || scriptFile.isDivergingFromVM()))
62 return new WebInspector.UILocation(uiSourceCode, debuggerModelLocation.lineNumber, debuggerModelLocation.columnNumber || 0);
66 * @param {!WebInspector.UISourceCode} uiSourceCode
67 * @param {number} lineNumber
68 * @param {number} columnNumber
69 * @return {?WebInspector.DebuggerModel.Location}
71 uiLocationToRawLocation: function(uiSourceCode, lineNumber, columnNumber)
73 var scripts = this._scriptsForUISourceCode(uiSourceCode);
74 console.assert(scripts.length);
75 return this._debuggerModel.createRawLocation(scripts[0], lineNumber, columnNumber);
79 * @param {!WebInspector.Script} script
81 addScript: function(script)
83 if (script.isAnonymousScript())
85 script.pushSourceMapping(this);
87 var scriptsForSourceURL = script.isInlineScript() ? this._inlineScriptsForSourceURL : this._nonInlineScriptsForSourceURL;
88 scriptsForSourceURL.put(script.sourceURL, scriptsForSourceURL.get(script.sourceURL) || []);
89 scriptsForSourceURL.get(script.sourceURL).push(script);
91 var uiSourceCode = this._workspaceUISourceCodeForScript(script);
95 this._bindUISourceCodeToScripts(uiSourceCode, [script]);
98 _uiSourceCodeAddedToWorkspace: function(event)
100 var uiSourceCode = /** @type {!WebInspector.UISourceCode} */ (event.data);
101 if (!uiSourceCode.url)
104 var scripts = this._scriptsForUISourceCode(uiSourceCode);
108 this._bindUISourceCodeToScripts(uiSourceCode, scripts);
112 * @param {!WebInspector.UISourceCode} uiSourceCode
114 _hasMergedToVM: function(uiSourceCode)
116 var scripts = this._scriptsForUISourceCode(uiSourceCode);
119 for (var i = 0; i < scripts.length; ++i)
120 scripts[i].updateLocations();
124 * @param {!WebInspector.UISourceCode} uiSourceCode
126 _hasDivergedFromVM: function(uiSourceCode)
128 var scripts = this._scriptsForUISourceCode(uiSourceCode);
131 for (var i = 0; i < scripts.length; ++i)
132 scripts[i].updateLocations();
136 * @param {!WebInspector.Script} script
137 * @return {?WebInspector.UISourceCode}
139 _workspaceUISourceCodeForScript: function(script)
141 if (script.isAnonymousScript())
143 return this._workspace.uiSourceCodeForURL(script.sourceURL);
147 * @param {!WebInspector.UISourceCode} uiSourceCode
148 * @return {!Array.<!WebInspector.Script>}
150 _scriptsForUISourceCode: function(uiSourceCode)
153 switch (uiSourceCode.contentType()) {
154 case WebInspector.resourceTypes.Document:
155 isInlineScript = true;
157 case WebInspector.resourceTypes.Script:
158 isInlineScript = false;
163 if (!uiSourceCode.url)
165 var scriptsForSourceURL = isInlineScript ? this._inlineScriptsForSourceURL : this._nonInlineScriptsForSourceURL;
166 return scriptsForSourceURL.get(uiSourceCode.url) || [];
170 * @param {!WebInspector.UISourceCode} uiSourceCode
171 * @param {!Array.<!WebInspector.Script>} scripts
173 _bindUISourceCodeToScripts: function(uiSourceCode, scripts)
175 console.assert(scripts.length);
176 var scriptFile = new WebInspector.ResourceScriptFile(this, uiSourceCode, scripts);
177 uiSourceCode.setScriptFile(scriptFile);
178 for (var i = 0; i < scripts.length; ++i)
179 scripts[i].updateLocations();
180 uiSourceCode.setSourceMapping(this);
184 * @param {!WebInspector.UISourceCode} uiSourceCode
185 * @param {!Array.<!WebInspector.Script>} scripts
187 _unbindUISourceCodeFromScripts: function(uiSourceCode, scripts)
189 console.assert(scripts.length);
190 var scriptFile = /** @type {!WebInspector.ResourceScriptFile} */ (uiSourceCode.scriptFile());
192 scriptFile.dispose();
193 uiSourceCode.setScriptFile(null);
195 uiSourceCode.setSourceMapping(null);
198 _initialize: function()
200 /** @type {!StringMap.<!Array.<!WebInspector.Script>>} */
201 this._inlineScriptsForSourceURL = new StringMap();
202 /** @type {!StringMap.<!Array.<!WebInspector.Script>>} */
203 this._nonInlineScriptsForSourceURL = new StringMap();
206 _debuggerReset: function()
209 * @param {!Array.<!WebInspector.Script>} scripts
210 * @this {WebInspector.ResourceScriptMapping}
212 function unbindUISourceCodesForScripts(scripts)
216 var uiSourceCode = this._workspaceUISourceCodeForScript(scripts[0]);
219 this._unbindUISourceCodeFromScripts(uiSourceCode, scripts);
222 this._inlineScriptsForSourceURL.values().forEach(unbindUISourceCodesForScripts.bind(this));
223 this._nonInlineScriptsForSourceURL.values().forEach(unbindUISourceCodesForScripts.bind(this));
230 * @extends {WebInspector.EventTarget}
232 WebInspector.ScriptFile = function()
236 WebInspector.ScriptFile.Events = {
237 DidMergeToVM: "DidMergeToVM",
238 DidDivergeFromVM: "DidDivergeFromVM",
241 WebInspector.ScriptFile.prototype = {
245 hasDivergedFromVM: function() { return false; },
250 isDivergingFromVM: function() { return false; },
255 isMergingToVM: function() { return false; },
257 checkMapping: function() { },
262 * @implements {WebInspector.ScriptFile}
263 * @extends {WebInspector.Object}
264 * @param {!WebInspector.ResourceScriptMapping} resourceScriptMapping
265 * @param {!WebInspector.UISourceCode} uiSourceCode
267 WebInspector.ResourceScriptFile = function(resourceScriptMapping, uiSourceCode, scripts)
269 console.assert(scripts.length);
271 WebInspector.ScriptFile.call(this);
272 this._resourceScriptMapping = resourceScriptMapping;
273 this._uiSourceCode = uiSourceCode;
275 if (this._uiSourceCode.contentType() === WebInspector.resourceTypes.Script)
276 this._script = scripts[0];
278 this._uiSourceCode.addEventListener(WebInspector.UISourceCode.Events.WorkingCopyCommitted, this._workingCopyCommitted, this);
279 this._uiSourceCode.addEventListener(WebInspector.UISourceCode.Events.WorkingCopyChanged, this._workingCopyChanged, this);
283 WebInspector.ResourceScriptFile.prototype = {
284 _workingCopyCommitted: function(event)
287 * @param {?string} error
288 * @param {!DebuggerAgent.SetScriptSourceError=} errorData
289 * @this {WebInspector.ResourceScriptFile}
291 function innerCallback(error, errorData)
295 WebInspector.LiveEditSupport.logDetailedError(error, errorData, this._script);
299 this._scriptSource = source;
301 WebInspector.LiveEditSupport.logSuccess();
305 var source = this._uiSourceCode.workingCopy();
306 this._resourceScriptMapping._debuggerModel.setScriptSource(this._script.scriptId, source, innerCallback.bind(this));
312 _isDiverged: function()
314 if (this._uiSourceCode.formatted())
316 if (this._uiSourceCode.isDirty())
320 if (typeof this._scriptSource === "undefined")
322 return this._uiSourceCode.workingCopy() !== this._scriptSource;
326 * @param {!WebInspector.Event} event
328 _workingCopyChanged: function(event)
335 if (this._isDiverged() && !this._hasDivergedFromVM)
336 this._divergeFromVM();
337 else if (!this._isDiverged() && this._hasDivergedFromVM)
341 _divergeFromVM: function()
343 this._isDivergingFromVM = true;
344 this._resourceScriptMapping._hasDivergedFromVM(this._uiSourceCode);
345 delete this._isDivergingFromVM;
346 this._hasDivergedFromVM = true;
347 this.dispatchEventToListeners(WebInspector.ScriptFile.Events.DidDivergeFromVM, this._uiSourceCode);
350 _mergeToVM: function()
352 delete this._hasDivergedFromVM;
353 this._isMergingToVM = true;
354 this._resourceScriptMapping._hasMergedToVM(this._uiSourceCode);
355 delete this._isMergingToVM;
356 this.dispatchEventToListeners(WebInspector.ScriptFile.Events.DidMergeToVM, this._uiSourceCode);
362 hasDivergedFromVM: function()
364 return this._hasDivergedFromVM;
370 isDivergingFromVM: function()
372 return this._isDivergingFromVM;
378 isMergingToVM: function()
380 return this._isMergingToVM;
383 checkMapping: function()
387 if (typeof this._scriptSource !== "undefined")
389 this._script.requestContent(callback.bind(this));
392 * @param {?string} source
393 * @this {WebInspector.ResourceScriptFile}
395 function callback(source)
397 this._scriptSource = source;
404 this._uiSourceCode.removeEventListener(WebInspector.UISourceCode.Events.WorkingCopyCommitted, this._workingCopyCommitted, this);
405 this._uiSourceCode.removeEventListener(WebInspector.UISourceCode.Events.WorkingCopyChanged, this._workingCopyChanged, this);
408 __proto__: WebInspector.Object.prototype