Upstream version 7.35.144.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / devtools / front_end / Script.js
1 /*
2  * Copyright (C) 2008 Apple 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
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 /**
27  * @constructor
28  * @extends {WebInspector.Object}
29  * @implements {WebInspector.ContentProvider}
30  * @param {string} scriptId
31  * @param {string} sourceURL
32  * @param {number} startLine
33  * @param {number} startColumn
34  * @param {number} endLine
35  * @param {number} endColumn
36  * @param {boolean} isContentScript
37  * @param {string=} sourceMapURL
38  * @param {boolean=} hasSourceURL
39  */
40 WebInspector.Script = function(scriptId, sourceURL, startLine, startColumn, endLine, endColumn, isContentScript, sourceMapURL, hasSourceURL)
41 {
42     this.scriptId = scriptId;
43     this.sourceURL = sourceURL;
44     this.lineOffset = startLine;
45     this.columnOffset = startColumn;
46     this.endLine = endLine;
47     this.endColumn = endColumn;
48     this.isContentScript = isContentScript;
49     this.sourceMapURL = sourceMapURL;
50     this.hasSourceURL = hasSourceURL;
51     /** @type {!Set.<!WebInspector.Script.Location>} */
52     this._locations = new Set();
53     /** @type {!Array.<!WebInspector.SourceMapping>} */
54     this._sourceMappings = [];
55 }
56
57 WebInspector.Script.Events = {
58     ScriptEdited: "ScriptEdited",
59 }
60
61 WebInspector.Script.snippetSourceURLPrefix = "snippets:///";
62
63 /**
64  * @param {string} source
65  * @return {string}
66  */
67 WebInspector.Script._trimSourceURLComment = function(source)
68 {
69     var sourceURLRegex = /\n[\040\t]*\/\/[@#]\ssourceURL=\s*(\S*?)\s*$/mg;
70     return source.replace(sourceURLRegex, "");
71 },
72
73
74 WebInspector.Script.prototype = {
75     /**
76      * @return {string}
77      */
78     contentURL: function()
79     {
80         return this.sourceURL;
81     },
82
83     /**
84      * @return {!WebInspector.ResourceType}
85      */
86     contentType: function()
87     {
88         return WebInspector.resourceTypes.Script;
89     },
90
91     /**
92      * @param {function(?string)} callback
93      */
94     requestContent: function(callback)
95     {
96         if (this._source) {
97             callback(this._source);
98             return;
99         }
100
101         /**
102          * @this {WebInspector.Script}
103          * @param {?Protocol.Error} error
104          * @param {string} source
105          */
106         function didGetScriptSource(error, source)
107         {
108             this._source = WebInspector.Script._trimSourceURLComment(error ? "" : source);
109             callback(this._source);
110         }
111         if (this.scriptId) {
112             // Script failed to parse.
113             DebuggerAgent.getScriptSource(this.scriptId, didGetScriptSource.bind(this));
114         } else
115             callback("");
116     },
117
118     /**
119      * @param {string} query
120      * @param {boolean} caseSensitive
121      * @param {boolean} isRegex
122      * @param {function(!Array.<!PageAgent.SearchMatch>)} callback
123      */
124     searchInContent: function(query, caseSensitive, isRegex, callback)
125     {
126         /**
127          * @param {?Protocol.Error} error
128          * @param {!Array.<!PageAgent.SearchMatch>} searchMatches
129          */
130         function innerCallback(error, searchMatches)
131         {
132             if (error)
133                 console.error(error);
134             var result = [];
135             for (var i = 0; i < searchMatches.length; ++i) {
136                 var searchMatch = new WebInspector.ContentProvider.SearchMatch(searchMatches[i].lineNumber, searchMatches[i].lineContent);
137                 result.push(searchMatch);
138             }
139             callback(result || []);
140         }
141
142         if (this.scriptId) {
143             // Script failed to parse.
144             DebuggerAgent.searchInContent(this.scriptId, query, caseSensitive, isRegex, innerCallback);
145         } else
146             callback([]);
147     },
148
149     /**
150      * @param {string} source
151      * @return {string}
152      */
153     _appendSourceURLCommentIfNeeded: function(source)
154     {
155         if (!this.hasSourceURL)
156             return source;
157         return source + "\n //# sourceURL=" + this.sourceURL;
158     },
159
160     /**
161      * @param {string} newSource
162      * @param {function(?Protocol.Error, !DebuggerAgent.SetScriptSourceError=, !Array.<!DebuggerAgent.CallFrame>=, !DebuggerAgent.StackTrace=, boolean=)} callback
163      */
164     editSource: function(newSource, callback)
165     {
166         /**
167          * @this {WebInspector.Script}
168          * @param {?Protocol.Error} error
169          * @param {!DebuggerAgent.SetScriptSourceError=} errorData
170          * @param {!Array.<!DebuggerAgent.CallFrame>=} callFrames
171          * @param {!Object=} debugData
172          * @param {!DebuggerAgent.StackTrace=} asyncStackTrace
173          */
174         function didEditScriptSource(error, errorData, callFrames, debugData, asyncStackTrace)
175         {
176             // FIXME: support debugData.stack_update_needs_step_in flag by calling WebInspector.debugger_model.callStackModified
177             if (!error)
178                 this._source = newSource;
179             var needsStepIn = !!debugData && debugData["stack_update_needs_step_in"] === true;
180             callback(error, errorData, callFrames, asyncStackTrace, needsStepIn);
181             if (!error)
182                 this.dispatchEventToListeners(WebInspector.Script.Events.ScriptEdited, newSource);
183         }
184
185         newSource = WebInspector.Script._trimSourceURLComment(newSource);
186         // We append correct sourceURL to script for consistency only. It's not actually needed for things to work correctly.
187         newSource = this._appendSourceURLCommentIfNeeded(newSource);
188
189         if (this.scriptId)
190             DebuggerAgent.setScriptSource(this.scriptId, newSource, undefined, didEditScriptSource.bind(this));
191         else
192             callback("Script failed to parse");
193     },
194
195     /**
196      * @return {boolean}
197      */
198     isInlineScript: function()
199     {
200         var startsAtZero = !this.lineOffset && !this.columnOffset;
201         return !!this.sourceURL && !startsAtZero;
202     },
203
204     /**
205      * @return {boolean}
206      */
207     isAnonymousScript: function()
208     {
209         return !this.sourceURL;
210     },
211
212     /**
213      * @return {boolean}
214      */
215     isSnippet: function()
216     {
217         return !!this.sourceURL && this.sourceURL.startsWith(WebInspector.Script.snippetSourceURLPrefix);
218     },
219
220     /**
221      * @param {number} lineNumber
222      * @param {number=} columnNumber
223      * @return {!WebInspector.UILocation}
224      */
225     rawLocationToUILocation: function(lineNumber, columnNumber)
226     {
227         var uiLocation;
228         var rawLocation = new WebInspector.DebuggerModel.Location(this.scriptId, lineNumber, columnNumber || 0);
229         for (var i = this._sourceMappings.length - 1; !uiLocation && i >= 0; --i)
230             uiLocation = this._sourceMappings[i].rawLocationToUILocation(rawLocation);
231         console.assert(uiLocation, "Script raw location can not be mapped to any ui location.");
232         return /** @type {!WebInspector.UILocation} */ (uiLocation);
233     },
234
235     /**
236      * @param {!WebInspector.SourceMapping} sourceMapping
237      */
238     pushSourceMapping: function(sourceMapping)
239     {
240         this._sourceMappings.push(sourceMapping);
241         this.updateLocations();
242     },
243
244     /**
245      * @return {!WebInspector.SourceMapping}
246      */
247     popSourceMapping: function()
248     {
249         var sourceMapping = this._sourceMappings.pop();
250         this.updateLocations();
251         return sourceMapping;
252     },
253
254     updateLocations: function()
255     {
256         var items = this._locations.items();
257         for (var i = 0; i < items.length; ++i)
258             items[i].update();
259     },
260
261     /**
262      * @param {!WebInspector.DebuggerModel.Location} rawLocation
263      * @param {function(!WebInspector.UILocation):(boolean|undefined)} updateDelegate
264      * @return {!WebInspector.Script.Location}
265      */
266     createLiveLocation: function(rawLocation, updateDelegate)
267     {
268         console.assert(rawLocation.scriptId === this.scriptId);
269         var location = new WebInspector.Script.Location(this, rawLocation, updateDelegate);
270         this._locations.add(location);
271         location.update();
272         return location;
273     },
274
275     __proto__: WebInspector.Object.prototype
276 }
277
278 /**
279  * @constructor
280  * @extends {WebInspector.LiveLocation}
281  * @param {!WebInspector.Script} script
282  * @param {!WebInspector.DebuggerModel.Location} rawLocation
283  * @param {function(!WebInspector.UILocation):(boolean|undefined)} updateDelegate
284  */
285 WebInspector.Script.Location = function(script, rawLocation, updateDelegate)
286 {
287     WebInspector.LiveLocation.call(this, rawLocation, updateDelegate);
288     this._script = script;
289 }
290
291 WebInspector.Script.Location.prototype = {
292     /**
293      * @return {!WebInspector.UILocation}
294      */
295     uiLocation: function()
296     {
297         var debuggerModelLocation = /** @type {!WebInspector.DebuggerModel.Location} */ (this.rawLocation());
298         return this._script.rawLocationToUILocation(debuggerModelLocation.lineNumber, debuggerModelLocation.columnNumber);
299     },
300
301     dispose: function()
302     {
303         WebInspector.LiveLocation.prototype.dispose.call(this);
304         this._script._locations.remove(this);
305     },
306
307     __proto__: WebInspector.LiveLocation.prototype
308 }