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);
42 this._boundURLs = new StringSet();
44 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 = debuggerModelLocation.script();
56 var uiSourceCode = this._workspaceUISourceCodeForScript(script);
59 var scriptFile = uiSourceCode.scriptFile();
60 if (scriptFile && ((scriptFile.hasDivergedFromVM() && !scriptFile.isMergingToVM()) || scriptFile.isDivergingFromVM()))
62 return uiSourceCode.uiLocation(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 uiSourceCode = this._workspaceUISourceCodeForScript(script);
91 this._bindUISourceCodeToScripts(uiSourceCode, [script]);
97 isIdentity: function()
102 _uiSourceCodeAddedToWorkspace: function(event)
104 var uiSourceCode = /** @type {!WebInspector.UISourceCode} */ (event.data);
105 if (uiSourceCode.project().isServiceProject())
107 if (!uiSourceCode.url)
110 var scripts = this._scriptsForUISourceCode(uiSourceCode);
114 this._bindUISourceCodeToScripts(uiSourceCode, scripts);
118 * @param {!WebInspector.UISourceCode} uiSourceCode
120 _hasMergedToVM: function(uiSourceCode)
122 var scripts = this._scriptsForUISourceCode(uiSourceCode);
125 for (var i = 0; i < scripts.length; ++i)
126 scripts[i].updateLocations();
130 * @param {!WebInspector.UISourceCode} uiSourceCode
132 _hasDivergedFromVM: function(uiSourceCode)
134 var scripts = this._scriptsForUISourceCode(uiSourceCode);
137 for (var i = 0; i < scripts.length; ++i)
138 scripts[i].updateLocations();
142 * @param {!WebInspector.Script} script
143 * @return {?WebInspector.UISourceCode}
145 _workspaceUISourceCodeForScript: function(script)
147 if (script.isAnonymousScript())
149 return this._workspace.uiSourceCodeForURL(script.sourceURL);
153 * @param {!WebInspector.UISourceCode} uiSourceCode
154 * @return {!Array.<!WebInspector.Script>}
156 _scriptsForUISourceCode: function(uiSourceCode)
158 if (!uiSourceCode.url)
160 return this._debuggerModel.scriptsForSourceURL(uiSourceCode.url);
164 * @param {!WebInspector.UISourceCode} uiSourceCode
165 * @param {!Array.<!WebInspector.Script>} scripts
167 _bindUISourceCodeToScripts: function(uiSourceCode, scripts)
169 console.assert(scripts.length);
170 var scriptFile = new WebInspector.ResourceScriptFile(this, uiSourceCode, scripts);
171 uiSourceCode.setScriptFile(scriptFile);
172 for (var i = 0; i < scripts.length; ++i)
173 scripts[i].updateLocations();
174 uiSourceCode.setSourceMapping(this);
175 this._boundURLs.add(uiSourceCode.url);
179 * @param {!WebInspector.UISourceCode} uiSourceCode
181 _unbindUISourceCode: function(uiSourceCode)
183 var scriptFile = /** @type {!WebInspector.ResourceScriptFile} */ (uiSourceCode.scriptFile());
185 scriptFile.dispose();
186 uiSourceCode.setScriptFile(null);
188 uiSourceCode.setSourceMapping(null);
191 _debuggerReset: function()
193 var boundURLs = this._boundURLs.values();
194 for (var i = 0; i < boundURLs.length; ++i)
196 var uiSourceCode = this._workspace.uiSourceCodeForURL(boundURLs[i]);
199 this._unbindUISourceCode(uiSourceCode);
201 this._boundURLs.clear();
207 * @extends {WebInspector.EventTarget}
209 WebInspector.ScriptFile = function()
213 WebInspector.ScriptFile.Events = {
214 DidMergeToVM: "DidMergeToVM",
215 DidDivergeFromVM: "DidDivergeFromVM",
218 WebInspector.ScriptFile.prototype = {
222 hasDivergedFromVM: function() { return false; },
227 isDivergingFromVM: function() { return false; },
232 isMergingToVM: function() { return false; },
234 checkMapping: function() { },
237 * @return {?WebInspector.Target}
239 target: function() { return null; },
244 * @implements {WebInspector.ScriptFile}
245 * @extends {WebInspector.Object}
246 * @param {!WebInspector.ResourceScriptMapping} resourceScriptMapping
247 * @param {!WebInspector.UISourceCode} uiSourceCode
249 WebInspector.ResourceScriptFile = function(resourceScriptMapping, uiSourceCode, scripts)
251 console.assert(scripts.length);
253 WebInspector.ScriptFile.call(this);
254 this._resourceScriptMapping = resourceScriptMapping;
255 this._uiSourceCode = uiSourceCode;
257 if (this._uiSourceCode.contentType() === WebInspector.resourceTypes.Script)
258 this._script = scripts[0];
260 this._uiSourceCode.addEventListener(WebInspector.UISourceCode.Events.WorkingCopyChanged, this._workingCopyChanged, this);
264 WebInspector.ResourceScriptFile.prototype = {
266 * @param {function(boolean)=} callback
268 commitLiveEdit: function(callback)
271 * @param {?string} error
272 * @param {!DebuggerAgent.SetScriptSourceError=} errorData
273 * @this {WebInspector.ResourceScriptFile}
275 function innerCallback(error, errorData)
279 WebInspector.LiveEditSupport.logDetailedError(error, errorData, this._script);
285 this._scriptSource = source;
287 WebInspector.LiveEditSupport.logSuccess();
293 var source = this._uiSourceCode.workingCopy();
294 this._resourceScriptMapping._debuggerModel.setScriptSource(this._script.scriptId, source, innerCallback.bind(this));
300 _isDiverged: function()
302 if (this._uiSourceCode.isDirty())
306 if (typeof this._scriptSource === "undefined")
308 if (!this._uiSourceCode.workingCopy().startsWith(this._scriptSource))
310 var suffix = this._uiSourceCode.workingCopy().substr(this._scriptSource.length);
311 return !!suffix.length && !suffix.match(WebInspector.Script.sourceURLRegex);
315 * @param {!WebInspector.Event} event
317 _workingCopyChanged: function(event)
324 if (this._isDiverged() && !this._hasDivergedFromVM)
325 this._divergeFromVM();
326 else if (!this._isDiverged() && this._hasDivergedFromVM)
330 _divergeFromVM: function()
332 this._isDivergingFromVM = true;
333 this._resourceScriptMapping._hasDivergedFromVM(this._uiSourceCode);
334 delete this._isDivergingFromVM;
335 this._hasDivergedFromVM = true;
336 this.dispatchEventToListeners(WebInspector.ScriptFile.Events.DidDivergeFromVM, this._uiSourceCode);
339 _mergeToVM: function()
341 delete this._hasDivergedFromVM;
342 this._isMergingToVM = true;
343 this._resourceScriptMapping._hasMergedToVM(this._uiSourceCode);
344 delete this._isMergingToVM;
345 this.dispatchEventToListeners(WebInspector.ScriptFile.Events.DidMergeToVM, this._uiSourceCode);
351 hasDivergedFromVM: function()
353 return this._hasDivergedFromVM;
359 isDivergingFromVM: function()
361 return this._isDivergingFromVM;
367 isMergingToVM: function()
369 return this._isMergingToVM;
372 checkMapping: function()
376 if (typeof this._scriptSource !== "undefined")
378 this._script.requestContent(callback.bind(this));
381 * @param {?string} source
382 * @this {WebInspector.ResourceScriptFile}
384 function callback(source)
386 this._scriptSource = source;
392 * @return {?WebInspector.Target}
398 return this._script.target();
403 this._uiSourceCode.removeEventListener(WebInspector.UISourceCode.Events.WorkingCopyChanged, this._workingCopyChanged, this);
406 __proto__: WebInspector.Object.prototype