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