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 * @constructor 33 * @implements {WebInspector.ScriptSourceMapping} 34 * @param {WebInspector.Workspace} workspace 35 */ 36 WebInspector.DefaultScriptMapping = function(workspace) 37 { 38 this._projectDelegate = new WebInspector.DebuggerProjectDelegate(); 39 this._workspace = workspace; 40 this._workspace.addProject(this._projectDelegate); 41 WebInspector.debuggerModel.addEventListener(WebInspector.DebuggerModel.Events.GlobalObjectCleared, this._debuggerReset, this); 42 this._debuggerReset(); 43 } 44 45 WebInspector.DefaultScriptMapping.prototype = { 46 /** 47 * @param {WebInspector.RawLocation} rawLocation 48 * @return {WebInspector.UILocation} 49 */ 50 rawLocationToUILocation: function(rawLocation) 51 { 52 var debuggerModelLocation = /** @type {WebInspector.DebuggerModel.Location} */ (rawLocation); 53 var script = WebInspector.debuggerModel.scriptForId(debuggerModelLocation.scriptId); 54 var uiSourceCode = this._uiSourceCodeForScriptId[script.scriptId]; 55 var lineNumber = debuggerModelLocation.lineNumber; 56 var columnNumber = debuggerModelLocation.columnNumber || 0; 57 return new WebInspector.UILocation(uiSourceCode, lineNumber, columnNumber); 58 }, 59 60 /** 61 * @param {WebInspector.UISourceCode} uiSourceCode 62 * @param {number} lineNumber 63 * @param {number} columnNumber 64 * @return {WebInspector.DebuggerModel.Location} 65 */ 66 uiLocationToRawLocation: function(uiSourceCode, lineNumber, columnNumber) 67 { 68 var scriptId = this._scriptIdForUISourceCode.get(uiSourceCode); 69 var script = WebInspector.debuggerModel.scriptForId(scriptId); 70 return WebInspector.debuggerModel.createRawLocation(script, lineNumber, columnNumber); 71 }, 72 73 /** 74 * @return {boolean} 75 */ 76 isIdentity: function() 77 { 78 return true; 79 }, 80 81 /** 82 * @param {WebInspector.Script} script 83 */ 84 addScript: function(script) 85 { 86 var path = this._projectDelegate.addScript(script); 87 var uiSourceCode = this._workspace.uiSourceCode(this._projectDelegate.id(), path); 88 this._uiSourceCodeForScriptId[script.scriptId] = uiSourceCode; 89 this._scriptIdForUISourceCode.put(uiSourceCode, script.scriptId); 90 uiSourceCode.setSourceMapping(this); 91 script.pushSourceMapping(this); 92 script.addEventListener(WebInspector.Script.Events.ScriptEdited, this._scriptEdited.bind(this, script.scriptId)); 93 return uiSourceCode; 94 }, 95 96 /** 97 * @param {string} scriptId 98 * @param {WebInspector.Event} event 99 */ 100 _scriptEdited: function(scriptId, event) 101 { 102 var content = /** @type {string} */(event.data); 103 this._uiSourceCodeForScriptId[scriptId].addRevision(content); 104 }, 105 106 _debuggerReset: function() 107 { 108 /** @type {Object.<string, WebInspector.UISourceCode>} */ 109 this._uiSourceCodeForScriptId = {}; 110 this._scriptIdForUISourceCode = new Map(); 111 this._projectDelegate.reset(); 112 } 113 } 114 115 /** 116 * @constructor 117 * @extends {WebInspector.ContentProviderBasedProjectDelegate} 118 */ 119 WebInspector.DebuggerProjectDelegate = function() 120 { 121 WebInspector.ContentProviderBasedProjectDelegate.call(this, WebInspector.projectTypes.Debugger); 122 } 123 124 WebInspector.DebuggerProjectDelegate.prototype = { 125 /** 126 * @return {string} 127 */ 128 id: function() 129 { 130 return "debugger:"; 131 }, 132 133 /** 134 * @return {string} 135 */ 136 displayName: function() 137 { 138 return "debugger"; 139 }, 140 141 /** 142 * @param {WebInspector.Script} script 143 * @return {string} 144 */ 145 addScript: function(script) 146 { 147 var contentProvider = script.isInlineScript() ? new WebInspector.ConcatenatedScriptsContentProvider([script]) : script; 148 var splitURL = WebInspector.ParsedURL.splitURL(script.sourceURL); 149 var name = splitURL[splitURL.length - 1]; 150 name = "[VM] " + name + " (" + script.scriptId + ")"; 151 return this.addContentProvider("", name, script.sourceURL, contentProvider, false, script.isContentScript); 152 }, 153 154 __proto__: WebInspector.ContentProviderBasedProjectDelegate.prototype 155 } 156