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 */
36WebInspector.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
45WebInspector.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     * @param {!WebInspector.Script} script
75     */
76    addScript: function(script)
77    {
78        var path = this._projectDelegate.addScript(script);
79        var uiSourceCode = this._workspace.uiSourceCode(this._projectDelegate.id(), path);
80        if (!uiSourceCode) {
81            console.assert(uiSourceCode);
82            return;
83        }
84        this._uiSourceCodeForScriptId[script.scriptId] = uiSourceCode;
85        this._scriptIdForUISourceCode.put(uiSourceCode, script.scriptId);
86        uiSourceCode.setSourceMapping(this);
87        script.pushSourceMapping(this);
88        script.addEventListener(WebInspector.Script.Events.ScriptEdited, this._scriptEdited.bind(this, script.scriptId));
89    },
90
91    /**
92     * @param {string} scriptId
93     * @param {!WebInspector.Event} event
94     */
95    _scriptEdited: function(scriptId, event)
96    {
97        var content = /** @type {string} */(event.data);
98        this._uiSourceCodeForScriptId[scriptId].addRevision(content);
99    },
100
101    _debuggerReset: function()
102    {
103        /** @type {!Object.<string, !WebInspector.UISourceCode>} */
104        this._uiSourceCodeForScriptId = {};
105        this._scriptIdForUISourceCode = new Map();
106        this._projectDelegate.reset();
107    }
108}
109
110/**
111 * @constructor
112 * @extends {WebInspector.ContentProviderBasedProjectDelegate}
113 */
114WebInspector.DebuggerProjectDelegate = function()
115{
116    WebInspector.ContentProviderBasedProjectDelegate.call(this, WebInspector.projectTypes.Debugger);
117}
118
119WebInspector.DebuggerProjectDelegate.prototype = {
120    /**
121     * @return {string}
122     */
123    id: function()
124    {
125        return "debugger:";
126    },
127
128    /**
129     * @return {string}
130     */
131    displayName: function()
132    {
133        return "debugger";
134    },
135
136    /**
137     * @param {!WebInspector.Script} script
138     * @return {string}
139     */
140    addScript: function(script)
141    {
142        var contentProvider = script.isInlineScript() ? new WebInspector.ConcatenatedScriptsContentProvider([script]) : script;
143        var splitURL = WebInspector.ParsedURL.splitURL(script.sourceURL);
144        var name = splitURL[splitURL.length - 1];
145        name = "VM" + script.scriptId + (name ? " " + name : "");
146        return this.addContentProvider("", name, script.sourceURL, contentProvider, false, script.isContentScript);
147    },
148
149    __proto__: WebInspector.ContentProviderBasedProjectDelegate.prototype
150}
151