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 * @extends {WebInspector.Object}
34 * @param {!WebInspector.Workspace} workspace
36 WebInspector.ScriptSnippetModel = function(workspace)
38 this._workspace = workspace;
39 /** @type {!Object.<string, !WebInspector.UISourceCode>} */
40 this._uiSourceCodeForScriptId = {};
41 /** @type {!Map.<!WebInspector.UISourceCode, !WebInspector.Script>} */
42 this._scriptForUISourceCode = new Map();
43 /** @type {!Object.<string, !WebInspector.UISourceCode>} */
44 this._uiSourceCodeForSnippetId = {};
45 /** @type {!Map.<!WebInspector.UISourceCode, string>} */
46 this._snippetIdForUISourceCode = new Map();
48 this._snippetStorage = new WebInspector.SnippetStorage("script", "Script snippet #");
49 this._lastSnippetEvaluationIndexSetting = WebInspector.settings.createSetting("lastSnippetEvaluationIndex", 0);
50 this._snippetScriptMapping = new WebInspector.SnippetScriptMapping(this);
51 this._projectDelegate = new WebInspector.SnippetsProjectDelegate(this);
52 this._project = this._workspace.addProject(this._projectDelegate);
54 WebInspector.debuggerModel.addEventListener(WebInspector.DebuggerModel.Events.GlobalObjectCleared, this._debuggerReset, this);
57 WebInspector.ScriptSnippetModel.prototype = {
59 * @return {!WebInspector.SnippetScriptMapping}
63 return this._snippetScriptMapping;
67 * @return {!WebInspector.Project}
74 _loadSnippets: function()
76 var snippets = this._snippetStorage.snippets();
77 for (var i = 0; i < snippets.length; ++i)
78 this._addScriptSnippet(snippets[i]);
82 * @param {string} content
85 createScriptSnippet: function(content)
87 var snippet = this._snippetStorage.createSnippet();
88 snippet.content = content;
89 return this._addScriptSnippet(snippet);
93 * @param {!WebInspector.Snippet} snippet
96 _addScriptSnippet: function(snippet)
98 var path = this._projectDelegate.addSnippet(snippet.name, new WebInspector.SnippetContentProvider(snippet));
99 var uiSourceCode = this._workspace.uiSourceCode(this._projectDelegate.id(), path);
101 console.assert(uiSourceCode);
104 var scriptFile = new WebInspector.SnippetScriptFile(this, uiSourceCode);
105 uiSourceCode.setScriptFile(scriptFile);
106 this._snippetIdForUISourceCode.put(uiSourceCode, snippet.id);
107 uiSourceCode.setSourceMapping(this._snippetScriptMapping);
108 this._uiSourceCodeForSnippetId[snippet.id] = uiSourceCode;
113 * @param {string} path
115 deleteScriptSnippet: function(path)
117 var uiSourceCode = this._workspace.uiSourceCode(this._projectDelegate.id(), path);
120 var snippetId = this._snippetIdForUISourceCode.get(uiSourceCode) || "";
121 var snippet = this._snippetStorage.snippetForId(snippetId);
122 this._snippetStorage.deleteSnippet(snippet);
123 this._removeBreakpoints(uiSourceCode);
124 this._releaseSnippetScript(uiSourceCode);
125 delete this._uiSourceCodeForSnippetId[snippet.id];
126 this._snippetIdForUISourceCode.remove(uiSourceCode);
127 this._projectDelegate.removeFile(snippet.name);
131 * @param {string} name
132 * @param {string} newName
133 * @param {function(boolean, string=)} callback
135 renameScriptSnippet: function(name, newName, callback)
137 newName = newName.trim();
138 if (!newName || newName.indexOf("/") !== -1 || name === newName || this._snippetStorage.snippetForName(newName)) {
142 var snippet = this._snippetStorage.snippetForName(name);
143 console.assert(snippet, "Snippet '" + name + "' was not found.");
144 var uiSourceCode = this._uiSourceCodeForSnippetId[snippet.id];
145 console.assert(uiSourceCode, "No uiSourceCode was found for snippet '" + name + "'.");
147 var breakpointLocations = this._removeBreakpoints(uiSourceCode);
148 snippet.name = newName;
149 this._restoreBreakpoints(uiSourceCode, breakpointLocations);
150 callback(true, newName);
154 * @param {string} name
155 * @param {string} newContent
157 _setScriptSnippetContent: function(name, newContent)
159 var snippet = this._snippetStorage.snippetForName(name);
160 snippet.content = newContent;
164 * @param {!WebInspector.UISourceCode} uiSourceCode
166 _scriptSnippetEdited: function(uiSourceCode)
168 var script = this._scriptForUISourceCode.get(uiSourceCode);
172 var breakpointLocations = this._removeBreakpoints(uiSourceCode);
173 this._releaseSnippetScript(uiSourceCode);
174 this._restoreBreakpoints(uiSourceCode, breakpointLocations);
175 var scriptUISourceCode = script.rawLocationToUILocation(0, 0).uiSourceCode;
176 if (scriptUISourceCode)
177 this._restoreBreakpoints(scriptUISourceCode, breakpointLocations);
181 * @param {string} snippetId
184 _nextEvaluationIndex: function(snippetId)
186 var evaluationIndex = this._lastSnippetEvaluationIndexSetting.get() + 1;
187 this._lastSnippetEvaluationIndexSetting.set(evaluationIndex);
188 return evaluationIndex;
192 * @param {!WebInspector.UISourceCode} uiSourceCode
194 evaluateScriptSnippet: function(uiSourceCode)
196 var breakpointLocations = this._removeBreakpoints(uiSourceCode);
197 this._releaseSnippetScript(uiSourceCode);
198 this._restoreBreakpoints(uiSourceCode, breakpointLocations);
199 var snippetId = this._snippetIdForUISourceCode.get(uiSourceCode) || "";
200 var evaluationIndex = this._nextEvaluationIndex(snippetId);
201 uiSourceCode._evaluationIndex = evaluationIndex;
202 var evaluationUrl = this._evaluationSourceURL(uiSourceCode);
204 var expression = uiSourceCode.workingCopy();
206 // In order to stop on the breakpoints during the snippet evaluation we need to compile and run it separately.
207 // If separate compilation and execution is not supported by the port we fall back to evaluation in console.
208 // In case we don't need that since debugger is already paused.
209 // We do the same when we are stopped on the call frame since debugger is already paused and can not stop on breakpoint anymore.
210 if (WebInspector.debuggerModel.selectedCallFrame()) {
211 expression = uiSourceCode.workingCopy() + "\n//# sourceURL=" + evaluationUrl + "\n";
212 WebInspector.evaluateInConsole(expression, true);
216 WebInspector.showConsole();
217 DebuggerAgent.compileScript(expression, evaluationUrl, compileCallback.bind(this));
220 * @param {?string} error
221 * @param {string=} scriptId
222 * @param {string=} syntaxErrorMessage
223 * @this {WebInspector.ScriptSnippetModel}
225 function compileCallback(error, scriptId, syntaxErrorMessage)
227 if (!uiSourceCode || uiSourceCode._evaluationIndex !== evaluationIndex)
231 console.error(error);
236 var consoleMessage = WebInspector.ConsoleMessage.create(
237 WebInspector.ConsoleMessage.MessageSource.JS,
238 WebInspector.ConsoleMessage.MessageLevel.Error,
239 syntaxErrorMessage || "");
240 WebInspector.console.addMessage(consoleMessage);
244 var breakpointLocations = this._removeBreakpoints(uiSourceCode);
245 this._restoreBreakpoints(uiSourceCode, breakpointLocations);
247 this._runScript(scriptId);
252 * @param {!DebuggerAgent.ScriptId} scriptId
254 _runScript: function(scriptId)
256 var currentExecutionContext = WebInspector.runtimeModel.currentExecutionContext();
257 DebuggerAgent.runScript(scriptId, currentExecutionContext ? currentExecutionContext.id : undefined, "console", false, runCallback.bind(this));
260 * @param {?string} error
261 * @param {?RuntimeAgent.RemoteObject} result
262 * @param {boolean=} wasThrown
263 * @this {WebInspector.ScriptSnippetModel}
265 function runCallback(error, result, wasThrown)
268 console.error(error);
272 this._printRunScriptResult(result, wasThrown);
277 * @param {?RuntimeAgent.RemoteObject} result
278 * @param {boolean=} wasThrown
280 _printRunScriptResult: function(result, wasThrown)
282 var level = (wasThrown ? WebInspector.ConsoleMessage.MessageLevel.Error : WebInspector.ConsoleMessage.MessageLevel.Log);
283 var message = WebInspector.ConsoleMessage.create(WebInspector.ConsoleMessage.MessageSource.JS, level, "", undefined, undefined, undefined, undefined, undefined, [result]);
284 WebInspector.console.addMessage(message)
288 * @param {!WebInspector.DebuggerModel.Location} rawLocation
289 * @return {?WebInspector.UILocation}
291 _rawLocationToUILocation: function(rawLocation)
293 var uiSourceCode = this._uiSourceCodeForScriptId[rawLocation.scriptId];
296 return new WebInspector.UILocation(uiSourceCode, rawLocation.lineNumber, rawLocation.columnNumber || 0);
300 * @param {!WebInspector.UISourceCode} uiSourceCode
301 * @param {number} lineNumber
302 * @param {number} columnNumber
303 * @return {?WebInspector.DebuggerModel.Location}
305 _uiLocationToRawLocation: function(uiSourceCode, lineNumber, columnNumber)
307 var script = this._scriptForUISourceCode.get(uiSourceCode);
311 return WebInspector.debuggerModel.createRawLocation(script, lineNumber, columnNumber);
315 * @param {!WebInspector.Script} script
317 _addScript: function(script)
319 var snippetId = this._snippetIdForSourceURL(script.sourceURL);
322 var uiSourceCode = this._uiSourceCodeForSnippetId[snippetId];
324 if (!uiSourceCode || this._evaluationSourceURL(uiSourceCode) !== script.sourceURL)
327 console.assert(!this._scriptForUISourceCode.get(uiSourceCode));
328 this._uiSourceCodeForScriptId[script.scriptId] = uiSourceCode;
329 this._scriptForUISourceCode.put(uiSourceCode, script);
330 uiSourceCode.scriptFile().setHasDivergedFromVM(false);
331 script.pushSourceMapping(this._snippetScriptMapping);
335 * @param {!WebInspector.UISourceCode} uiSourceCode
336 * @return {!Array.<!Object>}
338 _removeBreakpoints: function(uiSourceCode)
340 var breakpointLocations = WebInspector.breakpointManager.breakpointLocationsForUISourceCode(uiSourceCode);
341 for (var i = 0; i < breakpointLocations.length; ++i)
342 breakpointLocations[i].breakpoint.remove();
343 return breakpointLocations;
347 * @param {!WebInspector.UISourceCode} uiSourceCode
348 * @param {!Array.<!Object>} breakpointLocations
350 _restoreBreakpoints: function(uiSourceCode, breakpointLocations)
352 for (var i = 0; i < breakpointLocations.length; ++i) {
353 var uiLocation = breakpointLocations[i].uiLocation;
354 var breakpoint = breakpointLocations[i].breakpoint;
355 WebInspector.breakpointManager.setBreakpoint(uiSourceCode, uiLocation.lineNumber, uiLocation.columnNumber, breakpoint.condition(), breakpoint.enabled());
360 * @param {!WebInspector.UISourceCode} uiSourceCode
362 _releaseSnippetScript: function(uiSourceCode)
364 var script = this._scriptForUISourceCode.get(uiSourceCode);
368 uiSourceCode.scriptFile().setIsDivergingFromVM(true);
369 uiSourceCode.scriptFile().setHasDivergedFromVM(true);
370 delete this._uiSourceCodeForScriptId[script.scriptId];
371 this._scriptForUISourceCode.remove(uiSourceCode);
372 delete uiSourceCode._evaluationIndex;
373 uiSourceCode.scriptFile().setIsDivergingFromVM(false);
376 _debuggerReset: function()
378 for (var snippetId in this._uiSourceCodeForSnippetId) {
379 var uiSourceCode = this._uiSourceCodeForSnippetId[snippetId];
380 this._releaseSnippetScript(uiSourceCode);
385 * @param {!WebInspector.UISourceCode} uiSourceCode
388 _evaluationSourceURL: function(uiSourceCode)
390 var evaluationSuffix = "_" + uiSourceCode._evaluationIndex;
391 var snippetId = this._snippetIdForUISourceCode.get(uiSourceCode);
392 return WebInspector.Script.snippetSourceURLPrefix + snippetId + evaluationSuffix;
396 * @param {string} sourceURL
399 _snippetIdForSourceURL: function(sourceURL)
401 var snippetPrefix = WebInspector.Script.snippetSourceURLPrefix;
402 if (!sourceURL.startsWith(snippetPrefix))
404 var splitURL = sourceURL.substring(snippetPrefix.length).split("_");
405 var snippetId = splitURL[0];
411 /** @type {!Object.<string, !WebInspector.UISourceCode>} */
412 this._uiSourceCodeForScriptId = {};
413 this._scriptForUISourceCode = new Map();
414 /** @type {!Object.<string, !WebInspector.UISourceCode>} */
415 this._uiSourceCodeForSnippetId = {};
416 this._snippetIdForUISourceCode = new Map();
417 this._projectDelegate.reset();
418 this._loadSnippets();
421 __proto__: WebInspector.Object.prototype
426 * @implements {WebInspector.ScriptFile}
427 * @extends {WebInspector.Object}
428 * @param {!WebInspector.ScriptSnippetModel} scriptSnippetModel
429 * @param {!WebInspector.UISourceCode} uiSourceCode
431 WebInspector.SnippetScriptFile = function(scriptSnippetModel, uiSourceCode)
433 WebInspector.ScriptFile.call(this);
434 this._scriptSnippetModel = scriptSnippetModel;
435 this._uiSourceCode = uiSourceCode;
436 this._hasDivergedFromVM = true;
437 this._uiSourceCode.addEventListener(WebInspector.UISourceCode.Events.WorkingCopyChanged, this._workingCopyChanged, this);
440 WebInspector.SnippetScriptFile.prototype = {
444 hasDivergedFromVM: function()
446 return this._hasDivergedFromVM;
450 * @param {boolean} hasDivergedFromVM
452 setHasDivergedFromVM: function(hasDivergedFromVM)
454 this._hasDivergedFromVM = hasDivergedFromVM;
460 isDivergingFromVM: function()
462 return this._isDivergingFromVM;
465 checkMapping: function()
472 isMergingToVM: function()
478 * @param {boolean} isDivergingFromVM
480 setIsDivergingFromVM: function(isDivergingFromVM)
482 this._isDivergingFromVM = isDivergingFromVM;
485 _workingCopyChanged: function()
487 this._scriptSnippetModel._scriptSnippetEdited(this._uiSourceCode);
490 __proto__: WebInspector.Object.prototype
495 * @implements {WebInspector.ScriptSourceMapping}
496 * @param {!WebInspector.ScriptSnippetModel} scriptSnippetModel
498 WebInspector.SnippetScriptMapping = function(scriptSnippetModel)
500 this._scriptSnippetModel = scriptSnippetModel;
503 WebInspector.SnippetScriptMapping.prototype = {
505 * @param {!WebInspector.RawLocation} rawLocation
506 * @return {?WebInspector.UILocation}
508 rawLocationToUILocation: function(rawLocation)
510 var debuggerModelLocation = /** @type {!WebInspector.DebuggerModel.Location} */(rawLocation);
511 return this._scriptSnippetModel._rawLocationToUILocation(debuggerModelLocation);
515 * @param {!WebInspector.UISourceCode} uiSourceCode
516 * @param {number} lineNumber
517 * @param {number} columnNumber
518 * @return {?WebInspector.DebuggerModel.Location}
520 uiLocationToRawLocation: function(uiSourceCode, lineNumber, columnNumber)
522 return this._scriptSnippetModel._uiLocationToRawLocation(uiSourceCode, lineNumber, columnNumber);
526 * @param {string} sourceURL
529 snippetIdForSourceURL: function(sourceURL)
531 return this._scriptSnippetModel._snippetIdForSourceURL(sourceURL);
535 * @param {!WebInspector.Script} script
537 addScript: function(script)
539 this._scriptSnippetModel._addScript(script);
545 * @implements {WebInspector.ContentProvider}
546 * @param {!WebInspector.Snippet} snippet
548 WebInspector.SnippetContentProvider = function(snippet)
550 this._snippet = snippet;
553 WebInspector.SnippetContentProvider.prototype = {
557 contentURL: function()
563 * @return {!WebInspector.ResourceType}
565 contentType: function()
567 return WebInspector.resourceTypes.Script;
571 * @param {function(?string)} callback
573 requestContent: function(callback)
575 callback(this._snippet.content);
579 * @param {string} query
580 * @param {boolean} caseSensitive
581 * @param {boolean} isRegex
582 * @param {function(!Array.<!WebInspector.ContentProvider.SearchMatch>)} callback
584 searchInContent: function(query, caseSensitive, isRegex, callback)
587 * @this {WebInspector.SnippetContentProvider}
589 function performSearch()
591 callback(WebInspector.ContentProvider.performSearchInContent(this._snippet.content, query, caseSensitive, isRegex));
594 // searchInContent should call back later.
595 window.setTimeout(performSearch.bind(this), 0);
601 * @extends {WebInspector.ContentProviderBasedProjectDelegate}
602 * @param {!WebInspector.ScriptSnippetModel} model
604 WebInspector.SnippetsProjectDelegate = function(model)
606 WebInspector.ContentProviderBasedProjectDelegate.call(this, WebInspector.projectTypes.Snippets);
610 WebInspector.SnippetsProjectDelegate.prototype = {
617 return WebInspector.projectTypes.Snippets + ":";
621 * @param {string} name
622 * @param {!WebInspector.ContentProvider} contentProvider
625 addSnippet: function(name, contentProvider)
627 return this.addContentProvider("", name, name, contentProvider, true, false);
633 canSetFileContent: function()
639 * @param {string} path
640 * @param {string} newContent
641 * @param {function(?string)} callback
643 setFileContent: function(path, newContent, callback)
645 this._model._setScriptSnippetContent(path, newContent);
652 canRename: function()
658 * @param {string} path
659 * @param {string} newName
660 * @param {function(boolean, string=)} callback
662 performRename: function(path, newName, callback)
664 this._model.renameScriptSnippet(path, newName, callback);
668 * @param {string} path
669 * @param {?string} name
670 * @param {string} content
671 * @param {function(?string)} callback
673 createFile: function(path, name, content, callback)
675 var filePath = this._model.createScriptSnippet(content);
680 * @param {string} path
682 deleteFile: function(path)
684 this._model.deleteScriptSnippet(path);
687 __proto__: WebInspector.ContentProviderBasedProjectDelegate.prototype
691 * @type {!WebInspector.ScriptSnippetModel}
693 WebInspector.scriptSnippetModel;