| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2012 Google Inc. All rights reserved. | 2 * Copyright (C) 2012 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| 11 * copyright notice, this list of conditions and the following disclaimer | 11 * copyright notice, this list of conditions and the following disclaimer |
| 12 * in the documentation and/or other materials provided with the | 12 * in the documentation and/or other materials provided with the |
| 13 * distribution. | 13 * distribution. |
| 14 * * Neither the name of Google Inc. nor the names of its | 14 * * Neither the name of Google Inc. nor the names of its |
| 15 * contributors may be used to endorse or promote products derived from | 15 * contributors may be used to endorse or promote products derived from |
| 16 * this software without specific prior written permission. | 16 * this software without specific prior written permission. |
| 17 * | 17 * |
| 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ | 29 */ |
| 30 | |
| 31 /** | 30 /** |
| 32 * @constructor | |
| 33 * @extends {WebInspector.Object} | |
| 34 * @implements {WebInspector.TargetManager.Observer} | 31 * @implements {WebInspector.TargetManager.Observer} |
| 35 * @param {!WebInspector.Workspace} workspace | 32 * @unrestricted |
| 36 */ | 33 */ |
| 37 WebInspector.ScriptSnippetModel = function(workspace) | 34 WebInspector.ScriptSnippetModel = class extends WebInspector.Object { |
| 38 { | 35 /** |
| 36 * @param {!WebInspector.Workspace} workspace |
| 37 */ |
| 38 constructor(workspace) { |
| 39 super(); |
| 39 this._workspace = workspace; | 40 this._workspace = workspace; |
| 40 /** @type {!Object.<string, !WebInspector.UISourceCode>} */ | 41 /** @type {!Object.<string, !WebInspector.UISourceCode>} */ |
| 41 this._uiSourceCodeForSnippetId = {}; | 42 this._uiSourceCodeForSnippetId = {}; |
| 42 /** @type {!Map.<!WebInspector.UISourceCode, string>} */ | 43 /** @type {!Map.<!WebInspector.UISourceCode, string>} */ |
| 43 this._snippetIdForUISourceCode = new Map(); | 44 this._snippetIdForUISourceCode = new Map(); |
| 44 | 45 |
| 45 /** @type {!Map.<!WebInspector.Target, !WebInspector.SnippetScriptMapping>}
*/ | 46 /** @type {!Map.<!WebInspector.Target, !WebInspector.SnippetScriptMapping>}
*/ |
| 46 this._mappingForTarget = new Map(); | 47 this._mappingForTarget = new Map(); |
| 47 this._snippetStorage = new WebInspector.SnippetStorage("script", "Script sni
ppet #"); | 48 this._snippetStorage = new WebInspector.SnippetStorage('script', 'Script sni
ppet #'); |
| 48 this._lastSnippetEvaluationIndexSetting = WebInspector.settings.createSettin
g("lastSnippetEvaluationIndex", 0); | 49 this._lastSnippetEvaluationIndexSetting = WebInspector.settings.createSettin
g('lastSnippetEvaluationIndex', 0); |
| 49 this._project = new WebInspector.SnippetsProject(workspace, this); | 50 this._project = new WebInspector.SnippetsProject(workspace, this); |
| 50 this._loadSnippets(); | 51 this._loadSnippets(); |
| 51 WebInspector.targetManager.observeTargets(this); | 52 WebInspector.targetManager.observeTargets(this); |
| 52 }; | 53 } |
| 53 | 54 |
| 54 WebInspector.ScriptSnippetModel.snippetSourceURLPrefix = "snippets:///"; | 55 /** |
| 55 | 56 * @override |
| 56 | 57 * @param {!WebInspector.Target} target |
| 57 WebInspector.ScriptSnippetModel.prototype = { | 58 */ |
| 59 targetAdded(target) { |
| 60 var debuggerModel = WebInspector.DebuggerModel.fromTarget(target); |
| 61 if (debuggerModel) |
| 62 this._mappingForTarget.set(target, new WebInspector.SnippetScriptMapping(d
ebuggerModel, this)); |
| 63 } |
| 64 |
| 65 /** |
| 66 * @override |
| 67 * @param {!WebInspector.Target} target |
| 68 */ |
| 69 targetRemoved(target) { |
| 70 if (WebInspector.DebuggerModel.fromTarget(target)) |
| 71 this._mappingForTarget.remove(target); |
| 72 } |
| 73 |
| 74 /** |
| 75 * @param {!WebInspector.Target} target |
| 76 * @return {!WebInspector.SnippetScriptMapping|undefined} |
| 77 */ |
| 78 snippetScriptMapping(target) { |
| 79 return this._mappingForTarget.get(target); |
| 80 } |
| 81 |
| 82 /** |
| 83 * @return {!WebInspector.Project} |
| 84 */ |
| 85 project() { |
| 86 return this._project; |
| 87 } |
| 88 |
| 89 _loadSnippets() { |
| 90 for (var snippet of this._snippetStorage.snippets()) |
| 91 this._addScriptSnippet(snippet); |
| 92 } |
| 93 |
| 94 /** |
| 95 * @param {string} content |
| 96 * @return {!WebInspector.UISourceCode} |
| 97 */ |
| 98 createScriptSnippet(content) { |
| 99 var snippet = this._snippetStorage.createSnippet(); |
| 100 snippet.content = content; |
| 101 return this._addScriptSnippet(snippet); |
| 102 } |
| 103 |
| 104 /** |
| 105 * @param {!WebInspector.Snippet} snippet |
| 106 * @return {!WebInspector.UISourceCode} |
| 107 */ |
| 108 _addScriptSnippet(snippet) { |
| 109 var uiSourceCode = this._project.addSnippet(snippet.name, new WebInspector.S
nippetContentProvider(snippet)); |
| 110 uiSourceCode.addEventListener(WebInspector.UISourceCode.Events.WorkingCopyCh
anged, this._workingCopyChanged, this); |
| 111 this._snippetIdForUISourceCode.set(uiSourceCode, snippet.id); |
| 112 var breakpointLocations = this._removeBreakpoints(uiSourceCode); |
| 113 this._restoreBreakpoints(uiSourceCode, breakpointLocations); |
| 114 this._uiSourceCodeForSnippetId[snippet.id] = uiSourceCode; |
| 115 return uiSourceCode; |
| 116 } |
| 117 |
| 118 /** |
| 119 * @param {!WebInspector.Event} event |
| 120 */ |
| 121 _workingCopyChanged(event) { |
| 122 var uiSourceCode = /** @type {!WebInspector.UISourceCode} */ (event.target); |
| 123 this._scriptSnippetEdited(uiSourceCode); |
| 124 } |
| 125 |
| 126 /** |
| 127 * @param {string} url |
| 128 */ |
| 129 deleteScriptSnippet(url) { |
| 130 var uiSourceCode = this._project.uiSourceCodeForURL(url); |
| 131 if (!uiSourceCode) |
| 132 return; |
| 133 var snippetId = this._snippetIdForUISourceCode.get(uiSourceCode) || ''; |
| 134 var snippet = this._snippetStorage.snippetForId(snippetId); |
| 135 if (!snippet) |
| 136 return; |
| 137 this._snippetStorage.deleteSnippet(snippet); |
| 138 this._removeBreakpoints(uiSourceCode); |
| 139 this._releaseSnippetScript(uiSourceCode); |
| 140 delete this._uiSourceCodeForSnippetId[snippet.id]; |
| 141 this._snippetIdForUISourceCode.remove(uiSourceCode); |
| 142 this._project.removeFile(snippet.name); |
| 143 } |
| 144 |
| 145 /** |
| 146 * @param {string} name |
| 147 * @param {string} newName |
| 148 * @param {function(boolean, string=)} callback |
| 149 */ |
| 150 renameScriptSnippet(name, newName, callback) { |
| 151 newName = newName.trim(); |
| 152 if (!newName || newName.indexOf('/') !== -1 || name === newName || this._sni
ppetStorage.snippetForName(newName)) { |
| 153 callback(false); |
| 154 return; |
| 155 } |
| 156 var snippet = this._snippetStorage.snippetForName(name); |
| 157 console.assert(snippet, 'Snippet \'' + name + '\' was not found.'); |
| 158 var uiSourceCode = this._uiSourceCodeForSnippetId[snippet.id]; |
| 159 console.assert(uiSourceCode, 'No uiSourceCode was found for snippet \'' + na
me + '\'.'); |
| 160 |
| 161 var breakpointLocations = this._removeBreakpoints(uiSourceCode); |
| 162 snippet.name = newName; |
| 163 this._restoreBreakpoints(uiSourceCode, breakpointLocations); |
| 164 callback(true, newName); |
| 165 } |
| 166 |
| 167 /** |
| 168 * @param {string} name |
| 169 * @param {string} newContent |
| 170 */ |
| 171 _setScriptSnippetContent(name, newContent) { |
| 172 var snippet = this._snippetStorage.snippetForName(name); |
| 173 snippet.content = newContent; |
| 174 } |
| 175 |
| 176 /** |
| 177 * @param {!WebInspector.UISourceCode} uiSourceCode |
| 178 */ |
| 179 _scriptSnippetEdited(uiSourceCode) { |
| 180 var breakpointLocations = this._removeBreakpoints(uiSourceCode); |
| 181 this._releaseSnippetScript(uiSourceCode); |
| 182 this._restoreBreakpoints(uiSourceCode, breakpointLocations); |
| 183 this._mappingForTarget.valuesArray().forEach(function(mapping) { |
| 184 mapping._restoreBreakpoints(uiSourceCode, breakpointLocations); |
| 185 }); |
| 186 } |
| 187 |
| 188 /** |
| 189 * @return {number} |
| 190 */ |
| 191 _nextEvaluationIndex() { |
| 192 var evaluationIndex = this._lastSnippetEvaluationIndexSetting.get() + 1; |
| 193 this._lastSnippetEvaluationIndexSetting.set(evaluationIndex); |
| 194 return evaluationIndex; |
| 195 } |
| 196 |
| 197 /** |
| 198 * @param {!WebInspector.ExecutionContext} executionContext |
| 199 * @param {!WebInspector.UISourceCode} uiSourceCode |
| 200 */ |
| 201 evaluateScriptSnippet(executionContext, uiSourceCode) { |
| 202 var breakpointLocations = this._removeBreakpoints(uiSourceCode); |
| 203 this._releaseSnippetScript(uiSourceCode); |
| 204 this._restoreBreakpoints(uiSourceCode, breakpointLocations); |
| 205 |
| 206 var target = executionContext.target(); |
| 207 var runtimeModel = target.runtimeModel; |
| 208 var evaluationIndex = this._nextEvaluationIndex(); |
| 209 var mapping = this._mappingForTarget.get(target); |
| 210 mapping._setEvaluationIndex(evaluationIndex, uiSourceCode); |
| 211 var evaluationUrl = mapping._evaluationSourceURL(uiSourceCode); |
| 212 uiSourceCode.requestContent().then(compileSnippet.bind(this)); |
| 213 |
| 58 /** | 214 /** |
| 59 * @override | 215 * @this {WebInspector.ScriptSnippetModel} |
| 60 * @param {!WebInspector.Target} target | |
| 61 */ | 216 */ |
| 62 targetAdded: function(target) | 217 function compileSnippet() { |
| 63 { | 218 var expression = uiSourceCode.workingCopy(); |
| 64 var debuggerModel = WebInspector.DebuggerModel.fromTarget(target); | 219 WebInspector.console.show(); |
| 65 if (debuggerModel) | 220 runtimeModel.compileScript(expression, '', true, executionContext.id, comp
ileCallback.bind(this)); |
| 66 this._mappingForTarget.set(target, new WebInspector.SnippetScriptMap
ping(debuggerModel, this)); | 221 } |
| 67 }, | |
| 68 | 222 |
| 69 /** | 223 /** |
| 70 * @override | 224 * @param {!RuntimeAgent.ScriptId=} scriptId |
| 71 * @param {!WebInspector.Target} target | 225 * @param {?RuntimeAgent.ExceptionDetails=} exceptionDetails |
| 226 * @this {WebInspector.ScriptSnippetModel} |
| 72 */ | 227 */ |
| 73 targetRemoved: function(target) | 228 function compileCallback(scriptId, exceptionDetails) { |
| 74 { | 229 var mapping = this._mappingForTarget.get(target); |
| 75 if (WebInspector.DebuggerModel.fromTarget(target)) | 230 if (mapping.evaluationIndex(uiSourceCode) !== evaluationIndex) |
| 76 this._mappingForTarget.remove(target); | 231 return; |
| 77 }, | 232 |
| 78 | 233 var script = /** @type {!WebInspector.Script} */ ( |
| 79 /** | 234 executionContext.debuggerModel.scriptForId(/** @type {string} */ (scri
ptId || exceptionDetails.scriptId))); |
| 80 * @param {!WebInspector.Target} target | 235 mapping._addScript(script, uiSourceCode); |
| 81 * @return {!WebInspector.SnippetScriptMapping|undefined} | 236 if (!scriptId) { |
| 82 */ | 237 this._printRunOrCompileScriptResultFailure( |
| 83 snippetScriptMapping: function(target) | 238 target, /** @type {!RuntimeAgent.ExceptionDetails} */ (exceptionDeta
ils), evaluationUrl); |
| 84 { | 239 return; |
| 85 return this._mappingForTarget.get(target); | 240 } |
| 86 }, | 241 |
| 87 | 242 var breakpointLocations = this._removeBreakpoints(uiSourceCode); |
| 88 /** | 243 this._restoreBreakpoints(uiSourceCode, breakpointLocations); |
| 89 * @return {!WebInspector.Project} | 244 |
| 90 */ | 245 this._runScript(scriptId, executionContext, evaluationUrl); |
| 91 project: function() | 246 } |
| 92 { | 247 } |
| 93 return this._project; | 248 |
| 94 }, | 249 /** |
| 95 | 250 * @param {!RuntimeAgent.ScriptId} scriptId |
| 96 _loadSnippets: function() | 251 * @param {!WebInspector.ExecutionContext} executionContext |
| 97 { | 252 * @param {?string=} sourceURL |
| 98 for (var snippet of this._snippetStorage.snippets()) | 253 */ |
| 99 this._addScriptSnippet(snippet); | 254 _runScript(scriptId, executionContext, sourceURL) { |
| 100 }, | 255 var target = executionContext.target(); |
| 101 | 256 target.runtimeModel.runScript( |
| 102 /** | 257 scriptId, executionContext.id, 'console', /* silent */ false, /* include
CommandLineAPI */ true, |
| 103 * @param {string} content | 258 /* returnByValue */ false, /* generatePreview */ true, /* awaitPromise *
/ undefined, |
| 104 * @return {!WebInspector.UISourceCode} | 259 runCallback.bind(this, target)); |
| 105 */ | |
| 106 createScriptSnippet: function(content) | |
| 107 { | |
| 108 var snippet = this._snippetStorage.createSnippet(); | |
| 109 snippet.content = content; | |
| 110 return this._addScriptSnippet(snippet); | |
| 111 }, | |
| 112 | |
| 113 /** | |
| 114 * @param {!WebInspector.Snippet} snippet | |
| 115 * @return {!WebInspector.UISourceCode} | |
| 116 */ | |
| 117 _addScriptSnippet: function(snippet) | |
| 118 { | |
| 119 var uiSourceCode = this._project.addSnippet(snippet.name, new WebInspect
or.SnippetContentProvider(snippet)); | |
| 120 uiSourceCode.addEventListener(WebInspector.UISourceCode.Events.WorkingCo
pyChanged, this._workingCopyChanged, this); | |
| 121 this._snippetIdForUISourceCode.set(uiSourceCode, snippet.id); | |
| 122 var breakpointLocations = this._removeBreakpoints(uiSourceCode); | |
| 123 this._restoreBreakpoints(uiSourceCode, breakpointLocations); | |
| 124 this._uiSourceCodeForSnippetId[snippet.id] = uiSourceCode; | |
| 125 return uiSourceCode; | |
| 126 }, | |
| 127 | |
| 128 /** | |
| 129 * @param {!WebInspector.Event} event | |
| 130 */ | |
| 131 _workingCopyChanged: function(event) | |
| 132 { | |
| 133 var uiSourceCode = /** @type {!WebInspector.UISourceCode} */ (event.targ
et); | |
| 134 this._scriptSnippetEdited(uiSourceCode); | |
| 135 }, | |
| 136 | |
| 137 /** | |
| 138 * @param {string} url | |
| 139 */ | |
| 140 deleteScriptSnippet: function(url) | |
| 141 { | |
| 142 var uiSourceCode = this._project.uiSourceCodeForURL(url); | |
| 143 if (!uiSourceCode) | |
| 144 return; | |
| 145 var snippetId = this._snippetIdForUISourceCode.get(uiSourceCode) || ""; | |
| 146 var snippet = this._snippetStorage.snippetForId(snippetId); | |
| 147 if (!snippet) | |
| 148 return; | |
| 149 this._snippetStorage.deleteSnippet(snippet); | |
| 150 this._removeBreakpoints(uiSourceCode); | |
| 151 this._releaseSnippetScript(uiSourceCode); | |
| 152 delete this._uiSourceCodeForSnippetId[snippet.id]; | |
| 153 this._snippetIdForUISourceCode.remove(uiSourceCode); | |
| 154 this._project.removeFile(snippet.name); | |
| 155 }, | |
| 156 | |
| 157 /** | |
| 158 * @param {string} name | |
| 159 * @param {string} newName | |
| 160 * @param {function(boolean, string=)} callback | |
| 161 */ | |
| 162 renameScriptSnippet: function(name, newName, callback) | |
| 163 { | |
| 164 newName = newName.trim(); | |
| 165 if (!newName || newName.indexOf("/") !== -1 || name === newName || this.
_snippetStorage.snippetForName(newName)) { | |
| 166 callback(false); | |
| 167 return; | |
| 168 } | |
| 169 var snippet = this._snippetStorage.snippetForName(name); | |
| 170 console.assert(snippet, "Snippet '" + name + "' was not found."); | |
| 171 var uiSourceCode = this._uiSourceCodeForSnippetId[snippet.id]; | |
| 172 console.assert(uiSourceCode, "No uiSourceCode was found for snippet '" +
name + "'."); | |
| 173 | |
| 174 var breakpointLocations = this._removeBreakpoints(uiSourceCode); | |
| 175 snippet.name = newName; | |
| 176 this._restoreBreakpoints(uiSourceCode, breakpointLocations); | |
| 177 callback(true, newName); | |
| 178 }, | |
| 179 | |
| 180 /** | |
| 181 * @param {string} name | |
| 182 * @param {string} newContent | |
| 183 */ | |
| 184 _setScriptSnippetContent: function(name, newContent) | |
| 185 { | |
| 186 var snippet = this._snippetStorage.snippetForName(name); | |
| 187 snippet.content = newContent; | |
| 188 }, | |
| 189 | |
| 190 /** | |
| 191 * @param {!WebInspector.UISourceCode} uiSourceCode | |
| 192 */ | |
| 193 _scriptSnippetEdited: function(uiSourceCode) | |
| 194 { | |
| 195 var breakpointLocations = this._removeBreakpoints(uiSourceCode); | |
| 196 this._releaseSnippetScript(uiSourceCode); | |
| 197 this._restoreBreakpoints(uiSourceCode, breakpointLocations); | |
| 198 this._mappingForTarget.valuesArray().forEach(function(mapping) {mapping.
_restoreBreakpoints(uiSourceCode, breakpointLocations);}); | |
| 199 }, | |
| 200 | |
| 201 /** | |
| 202 * @return {number} | |
| 203 */ | |
| 204 _nextEvaluationIndex: function() | |
| 205 { | |
| 206 var evaluationIndex = this._lastSnippetEvaluationIndexSetting.get() + 1; | |
| 207 this._lastSnippetEvaluationIndexSetting.set(evaluationIndex); | |
| 208 return evaluationIndex; | |
| 209 }, | |
| 210 | |
| 211 /** | |
| 212 * @param {!WebInspector.ExecutionContext} executionContext | |
| 213 * @param {!WebInspector.UISourceCode} uiSourceCode | |
| 214 */ | |
| 215 evaluateScriptSnippet: function(executionContext, uiSourceCode) | |
| 216 { | |
| 217 var breakpointLocations = this._removeBreakpoints(uiSourceCode); | |
| 218 this._releaseSnippetScript(uiSourceCode); | |
| 219 this._restoreBreakpoints(uiSourceCode, breakpointLocations); | |
| 220 | |
| 221 var target = executionContext.target(); | |
| 222 var runtimeModel = target.runtimeModel; | |
| 223 var evaluationIndex = this._nextEvaluationIndex(); | |
| 224 var mapping = this._mappingForTarget.get(target); | |
| 225 mapping._setEvaluationIndex(evaluationIndex, uiSourceCode); | |
| 226 var evaluationUrl = mapping._evaluationSourceURL(uiSourceCode); | |
| 227 uiSourceCode.requestContent().then(compileSnippet.bind(this)); | |
| 228 | |
| 229 /** | |
| 230 * @this {WebInspector.ScriptSnippetModel} | |
| 231 */ | |
| 232 function compileSnippet() | |
| 233 { | |
| 234 var expression = uiSourceCode.workingCopy(); | |
| 235 WebInspector.console.show(); | |
| 236 runtimeModel.compileScript(expression, "", true, executionContext.id
, compileCallback.bind(this)); | |
| 237 } | |
| 238 | |
| 239 /** | |
| 240 * @param {!RuntimeAgent.ScriptId=} scriptId | |
| 241 * @param {?RuntimeAgent.ExceptionDetails=} exceptionDetails | |
| 242 * @this {WebInspector.ScriptSnippetModel} | |
| 243 */ | |
| 244 function compileCallback(scriptId, exceptionDetails) | |
| 245 { | |
| 246 var mapping = this._mappingForTarget.get(target); | |
| 247 if (mapping.evaluationIndex(uiSourceCode) !== evaluationIndex) | |
| 248 return; | |
| 249 | |
| 250 var script = /** @type {!WebInspector.Script} */(executionContext.de
buggerModel.scriptForId(/** @type {string} */ (scriptId || exceptionDetails.scri
ptId))); | |
| 251 mapping._addScript(script, uiSourceCode); | |
| 252 if (!scriptId) { | |
| 253 this._printRunOrCompileScriptResultFailure(target, /** @type {!R
untimeAgent.ExceptionDetails} */ (exceptionDetails), evaluationUrl); | |
| 254 return; | |
| 255 } | |
| 256 | |
| 257 var breakpointLocations = this._removeBreakpoints(uiSourceCode); | |
| 258 this._restoreBreakpoints(uiSourceCode, breakpointLocations); | |
| 259 | |
| 260 this._runScript(scriptId, executionContext, evaluationUrl); | |
| 261 } | |
| 262 }, | |
| 263 | |
| 264 /** | |
| 265 * @param {!RuntimeAgent.ScriptId} scriptId | |
| 266 * @param {!WebInspector.ExecutionContext} executionContext | |
| 267 * @param {?string=} sourceURL | |
| 268 */ | |
| 269 _runScript: function(scriptId, executionContext, sourceURL) | |
| 270 { | |
| 271 var target = executionContext.target(); | |
| 272 target.runtimeModel.runScript(scriptId, executionContext.id, "console",
/* silent */ false, /* includeCommandLineAPI */ true, /* returnByValue */ false,
/* generatePreview */ true, /* awaitPromise */ undefined, runCallback.bind(this
, target)); | |
| 273 | |
| 274 /** | |
| 275 * @param {!WebInspector.Target} target | |
| 276 * @param {?RuntimeAgent.RemoteObject} result | |
| 277 * @param {?RuntimeAgent.ExceptionDetails=} exceptionDetails | |
| 278 * @this {WebInspector.ScriptSnippetModel} | |
| 279 */ | |
| 280 function runCallback(target, result, exceptionDetails) | |
| 281 { | |
| 282 if (!exceptionDetails) | |
| 283 this._printRunScriptResult(target, result, scriptId, sourceURL); | |
| 284 else | |
| 285 this._printRunOrCompileScriptResultFailure(target, exceptionDeta
ils, sourceURL); | |
| 286 } | |
| 287 }, | |
| 288 | 260 |
| 289 /** | 261 /** |
| 290 * @param {!WebInspector.Target} target | 262 * @param {!WebInspector.Target} target |
| 291 * @param {?RuntimeAgent.RemoteObject} result | 263 * @param {?RuntimeAgent.RemoteObject} result |
| 292 * @param {!RuntimeAgent.ScriptId} scriptId | 264 * @param {?RuntimeAgent.ExceptionDetails=} exceptionDetails |
| 293 * @param {?string=} sourceURL | 265 * @this {WebInspector.ScriptSnippetModel} |
| 294 */ | 266 */ |
| 295 _printRunScriptResult: function(target, result, scriptId, sourceURL) | 267 function runCallback(target, result, exceptionDetails) { |
| 296 { | 268 if (!exceptionDetails) |
| 297 var consoleMessage = new WebInspector.ConsoleMessage( | 269 this._printRunScriptResult(target, result, scriptId, sourceURL); |
| 298 target, | 270 else |
| 299 WebInspector.ConsoleMessage.MessageSource.JS, | 271 this._printRunOrCompileScriptResultFailure(target, exceptionDetails, sou
rceURL); |
| 300 WebInspector.ConsoleMessage.MessageLevel.Log, | 272 } |
| 301 "", | 273 } |
| 302 undefined, | 274 |
| 303 sourceURL, | 275 /** |
| 304 undefined, | 276 * @param {!WebInspector.Target} target |
| 305 undefined, | 277 * @param {?RuntimeAgent.RemoteObject} result |
| 306 undefined, | 278 * @param {!RuntimeAgent.ScriptId} scriptId |
| 307 [result], | 279 * @param {?string=} sourceURL |
| 308 undefined, | 280 */ |
| 309 undefined, | 281 _printRunScriptResult(target, result, scriptId, sourceURL) { |
| 310 undefined, | 282 var consoleMessage = new WebInspector.ConsoleMessage( |
| 311 scriptId); | 283 target, WebInspector.ConsoleMessage.MessageSource.JS, WebInspector.Conso
leMessage.MessageLevel.Log, '', |
| 312 target.consoleModel.addMessage(consoleMessage); | 284 undefined, sourceURL, undefined, undefined, undefined, [result], undefin
ed, undefined, undefined, scriptId); |
| 313 }, | 285 target.consoleModel.addMessage(consoleMessage); |
| 314 | 286 } |
| 315 /** | 287 |
| 316 * @param {!WebInspector.Target} target | 288 /** |
| 317 * @param {!RuntimeAgent.ExceptionDetails} exceptionDetails | 289 * @param {!WebInspector.Target} target |
| 318 * @param {?string=} sourceURL | 290 * @param {!RuntimeAgent.ExceptionDetails} exceptionDetails |
| 319 */ | 291 * @param {?string=} sourceURL |
| 320 _printRunOrCompileScriptResultFailure: function(target, exceptionDetails, so
urceURL) | 292 */ |
| 321 { | 293 _printRunOrCompileScriptResultFailure(target, exceptionDetails, sourceURL) { |
| 322 target.consoleModel.addMessage(WebInspector.ConsoleMessage.fromException
(target, exceptionDetails, undefined, undefined, sourceURL || undefined)); | 294 target.consoleModel.addMessage(WebInspector.ConsoleMessage.fromException( |
| 323 }, | 295 target, exceptionDetails, undefined, undefined, sourceURL || undefined))
; |
| 324 | 296 } |
| 325 /** | 297 |
| 326 * @param {!WebInspector.UISourceCode} uiSourceCode | 298 /** |
| 327 * @return {!Array.<!{breakpoint: !WebInspector.BreakpointManager.Breakpoint
, uiLocation: !WebInspector.UILocation}>} | 299 * @param {!WebInspector.UISourceCode} uiSourceCode |
| 328 */ | 300 * @return {!Array.<!{breakpoint: !WebInspector.BreakpointManager.Breakpoint,
uiLocation: !WebInspector.UILocation}>} |
| 329 _removeBreakpoints: function(uiSourceCode) | 301 */ |
| 330 { | 302 _removeBreakpoints(uiSourceCode) { |
| 331 var breakpointLocations = WebInspector.breakpointManager.breakpointLocat
ionsForUISourceCode(uiSourceCode); | 303 var breakpointLocations = WebInspector.breakpointManager.breakpointLocations
ForUISourceCode(uiSourceCode); |
| 332 for (var i = 0; i < breakpointLocations.length; ++i) | 304 for (var i = 0; i < breakpointLocations.length; ++i) |
| 333 breakpointLocations[i].breakpoint.remove(); | 305 breakpointLocations[i].breakpoint.remove(); |
| 334 return breakpointLocations; | 306 return breakpointLocations; |
| 335 }, | 307 } |
| 336 | 308 |
| 337 /** | 309 /** |
| 338 * @param {!WebInspector.UISourceCode} uiSourceCode | 310 * @param {!WebInspector.UISourceCode} uiSourceCode |
| 339 * @param {!Array.<!{breakpoint: !WebInspector.BreakpointManager.Breakpoint,
uiLocation: !WebInspector.UILocation}>} breakpointLocations | 311 * @param {!Array.<!{breakpoint: !WebInspector.BreakpointManager.Breakpoint, u
iLocation: !WebInspector.UILocation}>} breakpointLocations |
| 340 */ | 312 */ |
| 341 _restoreBreakpoints: function(uiSourceCode, breakpointLocations) | 313 _restoreBreakpoints(uiSourceCode, breakpointLocations) { |
| 342 { | 314 for (var i = 0; i < breakpointLocations.length; ++i) { |
| 343 for (var i = 0; i < breakpointLocations.length; ++i) { | 315 var uiLocation = breakpointLocations[i].uiLocation; |
| 344 var uiLocation = breakpointLocations[i].uiLocation; | 316 var breakpoint = breakpointLocations[i].breakpoint; |
| 345 var breakpoint = breakpointLocations[i].breakpoint; | 317 WebInspector.breakpointManager.setBreakpoint( |
| 346 WebInspector.breakpointManager.setBreakpoint(uiSourceCode, uiLocatio
n.lineNumber, uiLocation.columnNumber, breakpoint.condition(), breakpoint.enable
d()); | 318 uiSourceCode, uiLocation.lineNumber, uiLocation.columnNumber, breakpoi
nt.condition(), breakpoint.enabled()); |
| 347 } | 319 } |
| 348 }, | 320 } |
| 349 | 321 |
| 350 /** | 322 /** |
| 351 * @param {!WebInspector.UISourceCode} uiSourceCode | 323 * @param {!WebInspector.UISourceCode} uiSourceCode |
| 352 */ | 324 */ |
| 353 _releaseSnippetScript: function(uiSourceCode) | 325 _releaseSnippetScript(uiSourceCode) { |
| 354 { | 326 this._mappingForTarget.valuesArray().forEach(function(mapping) { |
| 355 this._mappingForTarget.valuesArray().forEach(function(mapping) {mapping.
_releaseSnippetScript(uiSourceCode);}); | 327 mapping._releaseSnippetScript(uiSourceCode); |
| 356 }, | 328 }); |
| 357 | 329 } |
| 358 /** | 330 |
| 359 * @param {string} sourceURL | 331 /** |
| 360 * @return {?string} | 332 * @param {string} sourceURL |
| 361 */ | 333 * @return {?string} |
| 362 _snippetIdForSourceURL: function(sourceURL) | 334 */ |
| 363 { | 335 _snippetIdForSourceURL(sourceURL) { |
| 364 var snippetPrefix = WebInspector.ScriptSnippetModel.snippetSourceURLPref
ix; | 336 var snippetPrefix = WebInspector.ScriptSnippetModel.snippetSourceURLPrefix; |
| 365 if (!sourceURL.startsWith(snippetPrefix)) | 337 if (!sourceURL.startsWith(snippetPrefix)) |
| 366 return null; | 338 return null; |
| 367 var splitURL = sourceURL.substring(snippetPrefix.length).split("_"); | 339 var splitURL = sourceURL.substring(snippetPrefix.length).split('_'); |
| 368 var snippetId = splitURL[0]; | 340 var snippetId = splitURL[0]; |
| 369 return snippetId; | 341 return snippetId; |
| 370 }, | 342 } |
| 371 | |
| 372 __proto__: WebInspector.Object.prototype | |
| 373 }; | 343 }; |
| 374 | 344 |
| 345 WebInspector.ScriptSnippetModel.snippetSourceURLPrefix = 'snippets:///'; |
| 346 |
| 375 /** | 347 /** |
| 376 * @constructor | |
| 377 * @implements {WebInspector.DebuggerSourceMapping} | 348 * @implements {WebInspector.DebuggerSourceMapping} |
| 378 * @param {!WebInspector.DebuggerModel} debuggerModel | 349 * @unrestricted |
| 379 * @param {!WebInspector.ScriptSnippetModel} scriptSnippetModel | |
| 380 */ | 350 */ |
| 381 WebInspector.SnippetScriptMapping = function(debuggerModel, scriptSnippetModel) | 351 WebInspector.SnippetScriptMapping = class { |
| 382 { | 352 /** |
| 353 * @param {!WebInspector.DebuggerModel} debuggerModel |
| 354 * @param {!WebInspector.ScriptSnippetModel} scriptSnippetModel |
| 355 */ |
| 356 constructor(debuggerModel, scriptSnippetModel) { |
| 383 this._target = debuggerModel.target(); | 357 this._target = debuggerModel.target(); |
| 384 this._debuggerModel = debuggerModel; | 358 this._debuggerModel = debuggerModel; |
| 385 this._scriptSnippetModel = scriptSnippetModel; | 359 this._scriptSnippetModel = scriptSnippetModel; |
| 386 /** @type {!Object.<string, !WebInspector.UISourceCode>} */ | 360 /** @type {!Object.<string, !WebInspector.UISourceCode>} */ |
| 387 this._uiSourceCodeForScriptId = {}; | 361 this._uiSourceCodeForScriptId = {}; |
| 388 /** @type {!Map.<!WebInspector.UISourceCode, !WebInspector.Script>} */ | 362 /** @type {!Map.<!WebInspector.UISourceCode, !WebInspector.Script>} */ |
| 389 this._scriptForUISourceCode = new Map(); | 363 this._scriptForUISourceCode = new Map(); |
| 390 /** @type {!Map.<!WebInspector.UISourceCode, number>} */ | 364 /** @type {!Map.<!WebInspector.UISourceCode, number>} */ |
| 391 this._evaluationIndexForUISourceCode = new Map(); | 365 this._evaluationIndexForUISourceCode = new Map(); |
| 392 debuggerModel.addEventListener(WebInspector.DebuggerModel.Events.GlobalObjec
tCleared, this._reset, this); | 366 debuggerModel.addEventListener(WebInspector.DebuggerModel.Events.GlobalObjec
tCleared, this._reset, this); |
| 367 } |
| 368 |
| 369 /** |
| 370 * @param {!WebInspector.UISourceCode} uiSourceCode |
| 371 */ |
| 372 _releaseSnippetScript(uiSourceCode) { |
| 373 var script = this._scriptForUISourceCode.get(uiSourceCode); |
| 374 if (!script) |
| 375 return; |
| 376 |
| 377 delete this._uiSourceCodeForScriptId[script.scriptId]; |
| 378 this._scriptForUISourceCode.remove(uiSourceCode); |
| 379 this._evaluationIndexForUISourceCode.remove(uiSourceCode); |
| 380 } |
| 381 |
| 382 /** |
| 383 * @param {number} evaluationIndex |
| 384 * @param {!WebInspector.UISourceCode} uiSourceCode |
| 385 */ |
| 386 _setEvaluationIndex(evaluationIndex, uiSourceCode) { |
| 387 this._evaluationIndexForUISourceCode.set(uiSourceCode, evaluationIndex); |
| 388 } |
| 389 |
| 390 /** |
| 391 * @param {!WebInspector.UISourceCode} uiSourceCode |
| 392 * @return {number|undefined} |
| 393 */ |
| 394 evaluationIndex(uiSourceCode) { |
| 395 return this._evaluationIndexForUISourceCode.get(uiSourceCode); |
| 396 } |
| 397 |
| 398 /** |
| 399 * @param {!WebInspector.UISourceCode} uiSourceCode |
| 400 * @return {string} |
| 401 */ |
| 402 _evaluationSourceURL(uiSourceCode) { |
| 403 var evaluationSuffix = '_' + this._evaluationIndexForUISourceCode.get(uiSour
ceCode); |
| 404 var snippetId = this._scriptSnippetModel._snippetIdForUISourceCode.get(uiSou
rceCode); |
| 405 return WebInspector.ScriptSnippetModel.snippetSourceURLPrefix + snippetId +
evaluationSuffix; |
| 406 } |
| 407 |
| 408 _reset() { |
| 409 this._uiSourceCodeForScriptId = {}; |
| 410 this._scriptForUISourceCode.clear(); |
| 411 this._evaluationIndexForUISourceCode.clear(); |
| 412 } |
| 413 |
| 414 /** |
| 415 * @override |
| 416 * @param {!WebInspector.DebuggerModel.Location} rawLocation |
| 417 * @return {?WebInspector.UILocation} |
| 418 */ |
| 419 rawLocationToUILocation(rawLocation) { |
| 420 var debuggerModelLocation = /** @type {!WebInspector.DebuggerModel.Location}
*/ (rawLocation); |
| 421 var uiSourceCode = this._uiSourceCodeForScriptId[debuggerModelLocation.scrip
tId]; |
| 422 if (!uiSourceCode) |
| 423 return null; |
| 424 |
| 425 return uiSourceCode.uiLocation(debuggerModelLocation.lineNumber, debuggerMod
elLocation.columnNumber || 0); |
| 426 } |
| 427 |
| 428 /** |
| 429 * @override |
| 430 * @param {!WebInspector.UISourceCode} uiSourceCode |
| 431 * @param {number} lineNumber |
| 432 * @param {number} columnNumber |
| 433 * @return {?WebInspector.DebuggerModel.Location} |
| 434 */ |
| 435 uiLocationToRawLocation(uiSourceCode, lineNumber, columnNumber) { |
| 436 var script = this._scriptForUISourceCode.get(uiSourceCode); |
| 437 if (!script) |
| 438 return null; |
| 439 |
| 440 return this._debuggerModel.createRawLocation(script, lineNumber, columnNumbe
r); |
| 441 } |
| 442 |
| 443 /** |
| 444 * @param {!WebInspector.Script} script |
| 445 * @param {!WebInspector.UISourceCode} uiSourceCode |
| 446 */ |
| 447 _addScript(script, uiSourceCode) { |
| 448 console.assert(!this._scriptForUISourceCode.get(uiSourceCode)); |
| 449 WebInspector.debuggerWorkspaceBinding.setSourceMapping(this._target, uiSourc
eCode, this); |
| 450 this._uiSourceCodeForScriptId[script.scriptId] = uiSourceCode; |
| 451 this._scriptForUISourceCode.set(uiSourceCode, script); |
| 452 WebInspector.debuggerWorkspaceBinding.pushSourceMapping(script, this); |
| 453 } |
| 454 |
| 455 /** |
| 456 * @param {!WebInspector.UISourceCode} uiSourceCode |
| 457 * @param {!Array.<!{breakpoint: !WebInspector.BreakpointManager.Breakpoint, u
iLocation: !WebInspector.UILocation}>} breakpointLocations |
| 458 */ |
| 459 _restoreBreakpoints(uiSourceCode, breakpointLocations) { |
| 460 var script = this._scriptForUISourceCode.get(uiSourceCode); |
| 461 if (!script) |
| 462 return; |
| 463 var rawLocation = |
| 464 /** @type {!WebInspector.DebuggerModel.Location} */ (this._debuggerModel
.createRawLocation(script, 0, 0)); |
| 465 var scriptUISourceCode = WebInspector.debuggerWorkspaceBinding.rawLocationTo
UILocation(rawLocation).uiSourceCode; |
| 466 if (scriptUISourceCode) |
| 467 this._scriptSnippetModel._restoreBreakpoints(scriptUISourceCode, breakpoin
tLocations); |
| 468 } |
| 469 |
| 470 /** |
| 471 * @override |
| 472 * @return {boolean} |
| 473 */ |
| 474 isIdentity() { |
| 475 return false; |
| 476 } |
| 477 |
| 478 /** |
| 479 * @override |
| 480 * @param {!WebInspector.UISourceCode} uiSourceCode |
| 481 * @param {number} lineNumber |
| 482 * @return {boolean} |
| 483 */ |
| 484 uiLineHasMapping(uiSourceCode, lineNumber) { |
| 485 return true; |
| 486 } |
| 393 }; | 487 }; |
| 394 | 488 |
| 395 WebInspector.SnippetScriptMapping.prototype = { | 489 /** |
| 490 * @implements {WebInspector.ContentProvider} |
| 491 * @unrestricted |
| 492 */ |
| 493 WebInspector.SnippetContentProvider = class { |
| 494 /** |
| 495 * @param {!WebInspector.Snippet} snippet |
| 496 */ |
| 497 constructor(snippet) { |
| 498 this._snippet = snippet; |
| 499 } |
| 500 |
| 501 /** |
| 502 * @override |
| 503 * @return {string} |
| 504 */ |
| 505 contentURL() { |
| 506 return ''; |
| 507 } |
| 508 |
| 509 /** |
| 510 * @override |
| 511 * @return {!WebInspector.ResourceType} |
| 512 */ |
| 513 contentType() { |
| 514 return WebInspector.resourceTypes.Snippet; |
| 515 } |
| 516 |
| 517 /** |
| 518 * @override |
| 519 * @return {!Promise<?string>} |
| 520 */ |
| 521 requestContent() { |
| 522 return Promise.resolve(/** @type {?string} */ (this._snippet.content)); |
| 523 } |
| 524 |
| 525 /** |
| 526 * @override |
| 527 * @param {string} query |
| 528 * @param {boolean} caseSensitive |
| 529 * @param {boolean} isRegex |
| 530 * @param {function(!Array.<!WebInspector.ContentProvider.SearchMatch>)} callb
ack |
| 531 */ |
| 532 searchInContent(query, caseSensitive, isRegex, callback) { |
| 396 /** | 533 /** |
| 397 * @param {!WebInspector.UISourceCode} uiSourceCode | 534 * @this {WebInspector.SnippetContentProvider} |
| 398 */ | 535 */ |
| 399 _releaseSnippetScript: function(uiSourceCode) | 536 function performSearch() { |
| 400 { | 537 callback( |
| 401 var script = this._scriptForUISourceCode.get(uiSourceCode); | 538 WebInspector.ContentProvider.performSearchInContent(this._snippet.cont
ent, query, caseSensitive, isRegex)); |
| 402 if (!script) | |
| 403 return; | |
| 404 | |
| 405 delete this._uiSourceCodeForScriptId[script.scriptId]; | |
| 406 this._scriptForUISourceCode.remove(uiSourceCode); | |
| 407 this._evaluationIndexForUISourceCode.remove(uiSourceCode); | |
| 408 }, | |
| 409 | |
| 410 /** | |
| 411 +* @param {number} evaluationIndex | |
| 412 * @param {!WebInspector.UISourceCode} uiSourceCode | |
| 413 */ | |
| 414 _setEvaluationIndex: function(evaluationIndex, uiSourceCode) | |
| 415 { | |
| 416 this._evaluationIndexForUISourceCode.set(uiSourceCode, evaluationIndex); | |
| 417 }, | |
| 418 | |
| 419 /** | |
| 420 * @param {!WebInspector.UISourceCode} uiSourceCode | |
| 421 * @return {number|undefined} | |
| 422 */ | |
| 423 evaluationIndex: function(uiSourceCode) | |
| 424 { | |
| 425 return this._evaluationIndexForUISourceCode.get(uiSourceCode); | |
| 426 }, | |
| 427 | |
| 428 /** | |
| 429 * @param {!WebInspector.UISourceCode} uiSourceCode | |
| 430 * @return {string} | |
| 431 */ | |
| 432 _evaluationSourceURL: function(uiSourceCode) | |
| 433 { | |
| 434 var evaluationSuffix = "_" + this._evaluationIndexForUISourceCode.get(ui
SourceCode); | |
| 435 var snippetId = this._scriptSnippetModel._snippetIdForUISourceCode.get(u
iSourceCode); | |
| 436 return WebInspector.ScriptSnippetModel.snippetSourceURLPrefix + snippetI
d + evaluationSuffix; | |
| 437 }, | |
| 438 | |
| 439 _reset: function() | |
| 440 { | |
| 441 this._uiSourceCodeForScriptId = {}; | |
| 442 this._scriptForUISourceCode.clear(); | |
| 443 this._evaluationIndexForUISourceCode.clear(); | |
| 444 }, | |
| 445 | |
| 446 /** | |
| 447 * @override | |
| 448 * @param {!WebInspector.DebuggerModel.Location} rawLocation | |
| 449 * @return {?WebInspector.UILocation} | |
| 450 */ | |
| 451 rawLocationToUILocation: function(rawLocation) | |
| 452 { | |
| 453 var debuggerModelLocation = /** @type {!WebInspector.DebuggerModel.Locat
ion} */(rawLocation); | |
| 454 var uiSourceCode = this._uiSourceCodeForScriptId[debuggerModelLocation.s
criptId]; | |
| 455 if (!uiSourceCode) | |
| 456 return null; | |
| 457 | |
| 458 return uiSourceCode.uiLocation(debuggerModelLocation.lineNumber, debugge
rModelLocation.columnNumber || 0); | |
| 459 }, | |
| 460 | |
| 461 /** | |
| 462 * @override | |
| 463 * @param {!WebInspector.UISourceCode} uiSourceCode | |
| 464 * @param {number} lineNumber | |
| 465 * @param {number} columnNumber | |
| 466 * @return {?WebInspector.DebuggerModel.Location} | |
| 467 */ | |
| 468 uiLocationToRawLocation: function(uiSourceCode, lineNumber, columnNumber) | |
| 469 { | |
| 470 var script = this._scriptForUISourceCode.get(uiSourceCode); | |
| 471 if (!script) | |
| 472 return null; | |
| 473 | |
| 474 return this._debuggerModel.createRawLocation(script, lineNumber, columnN
umber); | |
| 475 }, | |
| 476 | |
| 477 /** | |
| 478 * @param {!WebInspector.Script} script | |
| 479 * @param {!WebInspector.UISourceCode} uiSourceCode | |
| 480 */ | |
| 481 _addScript: function(script, uiSourceCode) | |
| 482 { | |
| 483 console.assert(!this._scriptForUISourceCode.get(uiSourceCode)); | |
| 484 WebInspector.debuggerWorkspaceBinding.setSourceMapping(this._target, uiS
ourceCode, this); | |
| 485 this._uiSourceCodeForScriptId[script.scriptId] = uiSourceCode; | |
| 486 this._scriptForUISourceCode.set(uiSourceCode, script); | |
| 487 WebInspector.debuggerWorkspaceBinding.pushSourceMapping(script, this); | |
| 488 }, | |
| 489 | |
| 490 /** | |
| 491 * @param {!WebInspector.UISourceCode} uiSourceCode | |
| 492 * @param {!Array.<!{breakpoint: !WebInspector.BreakpointManager.Breakpoint,
uiLocation: !WebInspector.UILocation}>} breakpointLocations | |
| 493 */ | |
| 494 _restoreBreakpoints: function(uiSourceCode, breakpointLocations) | |
| 495 { | |
| 496 var script = this._scriptForUISourceCode.get(uiSourceCode); | |
| 497 if (!script) | |
| 498 return; | |
| 499 var rawLocation = /** @type {!WebInspector.DebuggerModel.Location} */ (t
his._debuggerModel.createRawLocation(script, 0, 0)); | |
| 500 var scriptUISourceCode = WebInspector.debuggerWorkspaceBinding.rawLocati
onToUILocation(rawLocation).uiSourceCode; | |
| 501 if (scriptUISourceCode) | |
| 502 this._scriptSnippetModel._restoreBreakpoints(scriptUISourceCode, bre
akpointLocations); | |
| 503 }, | |
| 504 | |
| 505 /** | |
| 506 * @override | |
| 507 * @return {boolean} | |
| 508 */ | |
| 509 isIdentity: function() | |
| 510 { | |
| 511 return false; | |
| 512 }, | |
| 513 | |
| 514 /** | |
| 515 * @override | |
| 516 * @param {!WebInspector.UISourceCode} uiSourceCode | |
| 517 * @param {number} lineNumber | |
| 518 * @return {boolean} | |
| 519 */ | |
| 520 uiLineHasMapping: function(uiSourceCode, lineNumber) | |
| 521 { | |
| 522 return true; | |
| 523 } | 539 } |
| 540 |
| 541 // searchInContent should call back later. |
| 542 window.setTimeout(performSearch.bind(this), 0); |
| 543 } |
| 524 }; | 544 }; |
| 525 | 545 |
| 526 /** | 546 /** |
| 527 * @constructor | 547 * @unrestricted |
| 528 * @implements {WebInspector.ContentProvider} | |
| 529 * @param {!WebInspector.Snippet} snippet | |
| 530 */ | 548 */ |
| 531 WebInspector.SnippetContentProvider = function(snippet) | 549 WebInspector.SnippetsProject = class extends WebInspector.ContentProviderBasedPr
oject { |
| 532 { | 550 /** |
| 533 this._snippet = snippet; | 551 * @param {!WebInspector.Workspace} workspace |
| 552 * @param {!WebInspector.ScriptSnippetModel} model |
| 553 */ |
| 554 constructor(workspace, model) { |
| 555 super(workspace, 'snippets:', WebInspector.projectTypes.Snippets, ''); |
| 556 this._model = model; |
| 557 } |
| 558 |
| 559 /** |
| 560 * @param {string} name |
| 561 * @param {!WebInspector.ContentProvider} contentProvider |
| 562 * @return {!WebInspector.UISourceCode} |
| 563 */ |
| 564 addSnippet(name, contentProvider) { |
| 565 return this.addContentProvider(name, contentProvider); |
| 566 } |
| 567 |
| 568 /** |
| 569 * @override |
| 570 * @return {boolean} |
| 571 */ |
| 572 canSetFileContent() { |
| 573 return true; |
| 574 } |
| 575 |
| 576 /** |
| 577 * @override |
| 578 * @param {!WebInspector.UISourceCode} uiSourceCode |
| 579 * @param {string} newContent |
| 580 * @param {function(?string)} callback |
| 581 */ |
| 582 setFileContent(uiSourceCode, newContent, callback) { |
| 583 this._model._setScriptSnippetContent(uiSourceCode.url(), newContent); |
| 584 callback(''); |
| 585 } |
| 586 |
| 587 /** |
| 588 * @override |
| 589 * @return {boolean} |
| 590 */ |
| 591 canRename() { |
| 592 return true; |
| 593 } |
| 594 |
| 595 /** |
| 596 * @override |
| 597 * @param {string} url |
| 598 * @param {string} newName |
| 599 * @param {function(boolean, string=)} callback |
| 600 */ |
| 601 performRename(url, newName, callback) { |
| 602 this._model.renameScriptSnippet(url, newName, callback); |
| 603 } |
| 604 |
| 605 /** |
| 606 * @override |
| 607 * @param {string} url |
| 608 * @param {?string} name |
| 609 * @param {string} content |
| 610 * @param {function(?WebInspector.UISourceCode)} callback |
| 611 */ |
| 612 createFile(url, name, content, callback) { |
| 613 callback(this._model.createScriptSnippet(content)); |
| 614 } |
| 615 |
| 616 /** |
| 617 * @override |
| 618 * @param {string} url |
| 619 */ |
| 620 deleteFile(url) { |
| 621 this._model.deleteScriptSnippet(url); |
| 622 } |
| 534 }; | 623 }; |
| 535 | 624 |
| 536 WebInspector.SnippetContentProvider.prototype = { | |
| 537 /** | |
| 538 * @override | |
| 539 * @return {string} | |
| 540 */ | |
| 541 contentURL: function() | |
| 542 { | |
| 543 return ""; | |
| 544 }, | |
| 545 | |
| 546 /** | |
| 547 * @override | |
| 548 * @return {!WebInspector.ResourceType} | |
| 549 */ | |
| 550 contentType: function() | |
| 551 { | |
| 552 return WebInspector.resourceTypes.Snippet; | |
| 553 }, | |
| 554 | |
| 555 /** | |
| 556 * @override | |
| 557 * @return {!Promise<?string>} | |
| 558 */ | |
| 559 requestContent: function() | |
| 560 { | |
| 561 return Promise.resolve(/** @type {?string} */(this._snippet.content)); | |
| 562 }, | |
| 563 | |
| 564 /** | |
| 565 * @override | |
| 566 * @param {string} query | |
| 567 * @param {boolean} caseSensitive | |
| 568 * @param {boolean} isRegex | |
| 569 * @param {function(!Array.<!WebInspector.ContentProvider.SearchMatch>)} cal
lback | |
| 570 */ | |
| 571 searchInContent: function(query, caseSensitive, isRegex, callback) | |
| 572 { | |
| 573 /** | |
| 574 * @this {WebInspector.SnippetContentProvider} | |
| 575 */ | |
| 576 function performSearch() | |
| 577 { | |
| 578 callback(WebInspector.ContentProvider.performSearchInContent(this._s
nippet.content, query, caseSensitive, isRegex)); | |
| 579 } | |
| 580 | |
| 581 // searchInContent should call back later. | |
| 582 window.setTimeout(performSearch.bind(this), 0); | |
| 583 } | |
| 584 }; | |
| 585 | |
| 586 /** | 625 /** |
| 587 * @constructor | |
| 588 * @extends {WebInspector.ContentProviderBasedProject} | |
| 589 * @param {!WebInspector.Workspace} workspace | |
| 590 * @param {!WebInspector.ScriptSnippetModel} model | |
| 591 */ | |
| 592 WebInspector.SnippetsProject = function(workspace, model) | |
| 593 { | |
| 594 WebInspector.ContentProviderBasedProject.call(this, workspace, "snippets:",
WebInspector.projectTypes.Snippets, ""); | |
| 595 this._model = model; | |
| 596 }; | |
| 597 | |
| 598 WebInspector.SnippetsProject.prototype = { | |
| 599 /** | |
| 600 * @param {string} name | |
| 601 * @param {!WebInspector.ContentProvider} contentProvider | |
| 602 * @return {!WebInspector.UISourceCode} | |
| 603 */ | |
| 604 addSnippet: function(name, contentProvider) | |
| 605 { | |
| 606 return this.addContentProvider(name, contentProvider); | |
| 607 }, | |
| 608 | |
| 609 /** | |
| 610 * @override | |
| 611 * @return {boolean} | |
| 612 */ | |
| 613 canSetFileContent: function() | |
| 614 { | |
| 615 return true; | |
| 616 }, | |
| 617 | |
| 618 /** | |
| 619 * @override | |
| 620 * @param {!WebInspector.UISourceCode} uiSourceCode | |
| 621 * @param {string} newContent | |
| 622 * @param {function(?string)} callback | |
| 623 */ | |
| 624 setFileContent: function(uiSourceCode, newContent, callback) | |
| 625 { | |
| 626 this._model._setScriptSnippetContent(uiSourceCode.url(), newContent); | |
| 627 callback(""); | |
| 628 }, | |
| 629 | |
| 630 /** | |
| 631 * @override | |
| 632 * @return {boolean} | |
| 633 */ | |
| 634 canRename: function() | |
| 635 { | |
| 636 return true; | |
| 637 }, | |
| 638 | |
| 639 /** | |
| 640 * @override | |
| 641 * @param {string} url | |
| 642 * @param {string} newName | |
| 643 * @param {function(boolean, string=)} callback | |
| 644 */ | |
| 645 performRename: function(url, newName, callback) | |
| 646 { | |
| 647 this._model.renameScriptSnippet(url, newName, callback); | |
| 648 }, | |
| 649 | |
| 650 /** | |
| 651 * @override | |
| 652 * @param {string} url | |
| 653 * @param {?string} name | |
| 654 * @param {string} content | |
| 655 * @param {function(?WebInspector.UISourceCode)} callback | |
| 656 */ | |
| 657 createFile: function(url, name, content, callback) | |
| 658 { | |
| 659 callback(this._model.createScriptSnippet(content)); | |
| 660 }, | |
| 661 | |
| 662 /** | |
| 663 * @override | |
| 664 * @param {string} url | |
| 665 */ | |
| 666 deleteFile: function(url) | |
| 667 { | |
| 668 this._model.deleteScriptSnippet(url); | |
| 669 }, | |
| 670 | |
| 671 __proto__: WebInspector.ContentProviderBasedProject.prototype | |
| 672 }; | |
| 673 | |
| 674 /** | |
| 675 * @type {!WebInspector.ScriptSnippetModel} | 626 * @type {!WebInspector.ScriptSnippetModel} |
| 676 */ | 627 */ |
| 677 WebInspector.scriptSnippetModel = new WebInspector.ScriptSnippetModel(WebInspect
or.workspace); | 628 WebInspector.scriptSnippetModel = new WebInspector.ScriptSnippetModel(WebInspect
or.workspace); |
| OLD | NEW |