ScriptSnippetModel.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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. * @constructor
  32. * @extends {WebInspector.Object}
  33. * @param {WebInspector.Workspace} workspace
  34. */
  35. WebInspector.ScriptSnippetModel = function(workspace)
  36. {
  37. this._workspace = workspace;
  38. /** {Object.<string, WebInspector.UISourceCode>} */
  39. this._uiSourceCodeForScriptId = {};
  40. this._scriptForUISourceCode = new Map();
  41. /** {Object.<string, WebInspector.UISourceCode>} */
  42. this._uiSourceCodeForSnippetId = {};
  43. this._snippetIdForUISourceCode = new Map();
  44. this._snippetStorage = new WebInspector.SnippetStorage("script", "Script snippet #");
  45. this._lastSnippetEvaluationIndexSetting = WebInspector.settings.createSetting("lastSnippetEvaluationIndex", 0);
  46. this._snippetScriptMapping = new WebInspector.SnippetScriptMapping(this);
  47. this._workspaceProvider = new WebInspector.SimpleWorkspaceProvider(this._workspace, WebInspector.projectTypes.Snippets);
  48. this.reset();
  49. WebInspector.debuggerModel.addEventListener(WebInspector.DebuggerModel.Events.GlobalObjectCleared, this._debuggerReset, this);
  50. }
  51. WebInspector.ScriptSnippetModel.prototype = {
  52. /**
  53. * @return {WebInspector.SnippetScriptMapping}
  54. */
  55. get scriptMapping()
  56. {
  57. return this._snippetScriptMapping;
  58. },
  59. _loadSnippets: function()
  60. {
  61. var snippets = this._snippetStorage.snippets();
  62. for (var i = 0; i < snippets.length; ++i)
  63. this._addScriptSnippet(snippets[i]);
  64. },
  65. /**
  66. * @return {WebInspector.UISourceCode}
  67. */
  68. createScriptSnippet: function()
  69. {
  70. var snippet = this._snippetStorage.createSnippet();
  71. return this._addScriptSnippet(snippet);
  72. },
  73. /**
  74. * @param {WebInspector.Snippet} snippet
  75. * @return {WebInspector.UISourceCode}
  76. */
  77. _addScriptSnippet: function(snippet)
  78. {
  79. var uiSourceCode = this._workspaceProvider.addFileByName("", snippet.name, new WebInspector.SnippetContentProvider(snippet), true);
  80. var scriptFile = new WebInspector.SnippetScriptFile(this, uiSourceCode);
  81. uiSourceCode.setScriptFile(scriptFile);
  82. this._snippetIdForUISourceCode.put(uiSourceCode, snippet.id);
  83. uiSourceCode.setSourceMapping(this._snippetScriptMapping);
  84. this._uiSourceCodeForSnippetId[snippet.id] = uiSourceCode;
  85. return uiSourceCode;
  86. },
  87. /**
  88. * @param {WebInspector.UISourceCode} uiSourceCode
  89. */
  90. deleteScriptSnippet: function(uiSourceCode)
  91. {
  92. var snippetId = this._snippetIdForUISourceCode.get(uiSourceCode);
  93. var snippet = this._snippetStorage.snippetForId(snippetId);
  94. this._snippetStorage.deleteSnippet(snippet);
  95. this._removeBreakpoints(uiSourceCode);
  96. this._releaseSnippetScript(uiSourceCode);
  97. delete this._uiSourceCodeForSnippetId[snippet.id];
  98. this._snippetIdForUISourceCode.remove(uiSourceCode);
  99. this._workspaceProvider.removeFileByName("", snippet.name);
  100. },
  101. /**
  102. * @param {WebInspector.UISourceCode} uiSourceCode
  103. * @param {string} newName
  104. */
  105. renameScriptSnippet: function(uiSourceCode, newName)
  106. {
  107. var breakpointLocations = this._removeBreakpoints(uiSourceCode);
  108. var snippetId = this._snippetIdForUISourceCode.get(uiSourceCode);
  109. var snippet = this._snippetStorage.snippetForId(snippetId);
  110. if (!snippet || !newName || snippet.name === newName)
  111. return;
  112. snippet.name = newName;
  113. this._restoreBreakpoints(uiSourceCode, breakpointLocations);
  114. },
  115. /**
  116. * @param {WebInspector.UISourceCode} uiSourceCode
  117. * @param {string} newContent
  118. */
  119. _setScriptSnippetContent: function(uiSourceCode, newContent)
  120. {
  121. var snippetId = this._snippetIdForUISourceCode.get(uiSourceCode);
  122. var snippet = this._snippetStorage.snippetForId(snippetId);
  123. snippet.content = newContent;
  124. },
  125. /**
  126. * @param {WebInspector.UISourceCode} uiSourceCode
  127. */
  128. _scriptSnippetEdited: function(uiSourceCode)
  129. {
  130. var script = this._scriptForUISourceCode.get(uiSourceCode);
  131. if (!script)
  132. return;
  133. var breakpointLocations = this._removeBreakpoints(uiSourceCode);
  134. var scriptUISourceCode = this._releaseSnippetScript(uiSourceCode);
  135. this._restoreBreakpoints(uiSourceCode, breakpointLocations);
  136. if (scriptUISourceCode)
  137. this._restoreBreakpoints(scriptUISourceCode, breakpointLocations);
  138. },
  139. /**
  140. * @param {string} snippetId
  141. * @return {number}
  142. */
  143. _nextEvaluationIndex: function(snippetId)
  144. {
  145. var evaluationIndex = this._lastSnippetEvaluationIndexSetting.get() + 1;
  146. this._lastSnippetEvaluationIndexSetting.set(evaluationIndex);
  147. return evaluationIndex;
  148. },
  149. /**
  150. * @param {WebInspector.UISourceCode} uiSourceCode
  151. */
  152. evaluateScriptSnippet: function(uiSourceCode)
  153. {
  154. var breakpointLocations = this._removeBreakpoints(uiSourceCode);
  155. this._releaseSnippetScript(uiSourceCode);
  156. this._restoreBreakpoints(uiSourceCode, breakpointLocations);
  157. var snippetId = this._snippetIdForUISourceCode.get(uiSourceCode);
  158. var evaluationIndex = this._nextEvaluationIndex(snippetId);
  159. uiSourceCode._evaluationIndex = evaluationIndex;
  160. var evaluationUrl = this._evaluationSourceURL(uiSourceCode);
  161. var expression = uiSourceCode.workingCopy();
  162. // In order to stop on the breakpoints during the snippet evaluation we need to compile and run it separately.
  163. // If separate compilation and execution is not supported by the port we fall back to evaluation in console.
  164. // In case we don't need that since debugger is already paused.
  165. // We do the same when we are stopped on the call frame since debugger is already paused and can not stop on breakpoint anymore.
  166. if (WebInspector.debuggerModel.selectedCallFrame() || !Capabilities.separateScriptCompilationAndExecutionEnabled) {
  167. expression = uiSourceCode.workingCopy() + "\n//@ sourceURL=" + evaluationUrl + "\n";
  168. WebInspector.evaluateInConsole(expression, true);
  169. return;
  170. }
  171. WebInspector.showConsole();
  172. DebuggerAgent.compileScript(expression, evaluationUrl, compileCallback.bind(this));
  173. /**
  174. * @param {?string} error
  175. * @param {string=} scriptId
  176. * @param {string=} syntaxErrorMessage
  177. */
  178. function compileCallback(error, scriptId, syntaxErrorMessage)
  179. {
  180. if (!uiSourceCode || uiSourceCode._evaluationIndex !== evaluationIndex)
  181. return;
  182. if (error) {
  183. console.error(error);
  184. return;
  185. }
  186. if (!scriptId) {
  187. var consoleMessage = WebInspector.ConsoleMessage.create(
  188. WebInspector.ConsoleMessage.MessageSource.JS,
  189. WebInspector.ConsoleMessage.MessageLevel.Error,
  190. syntaxErrorMessage || "");
  191. WebInspector.console.addMessage(consoleMessage);
  192. return;
  193. }
  194. var breakpointLocations = this._removeBreakpoints(uiSourceCode);
  195. this._restoreBreakpoints(uiSourceCode, breakpointLocations);
  196. this._runScript(scriptId);
  197. }
  198. },
  199. /**
  200. * @param {DebuggerAgent.ScriptId} scriptId
  201. */
  202. _runScript: function(scriptId)
  203. {
  204. var currentExecutionContext = WebInspector.runtimeModel.currentExecutionContext();
  205. DebuggerAgent.runScript(scriptId, currentExecutionContext ? currentExecutionContext.id : undefined, "console", false, runCallback.bind(this));
  206. /**
  207. * @param {?string} error
  208. * @param {?RuntimeAgent.RemoteObject} result
  209. * @param {boolean=} wasThrown
  210. */
  211. function runCallback(error, result, wasThrown)
  212. {
  213. if (error) {
  214. console.error(error);
  215. return;
  216. }
  217. this._printRunScriptResult(result, wasThrown);
  218. }
  219. },
  220. /**
  221. * @param {?RuntimeAgent.RemoteObject} result
  222. * @param {boolean=} wasThrown
  223. */
  224. _printRunScriptResult: function(result, wasThrown)
  225. {
  226. var level = (wasThrown ? WebInspector.ConsoleMessage.MessageLevel.Error : WebInspector.ConsoleMessage.MessageLevel.Log);
  227. var message = WebInspector.ConsoleMessage.create(WebInspector.ConsoleMessage.MessageSource.JS, level, "", undefined, undefined, undefined, undefined, [result]);
  228. WebInspector.console.addMessage(message)
  229. },
  230. /**
  231. * @param {WebInspector.DebuggerModel.Location} rawLocation
  232. * @return {WebInspector.UILocation}
  233. */
  234. _rawLocationToUILocation: function(rawLocation)
  235. {
  236. var uiSourceCode = this._uiSourceCodeForScriptId[rawLocation.scriptId];
  237. return new WebInspector.UILocation(uiSourceCode, rawLocation.lineNumber, rawLocation.columnNumber || 0);
  238. },
  239. /**
  240. * @param {WebInspector.UISourceCode} uiSourceCode
  241. * @param {number} lineNumber
  242. * @param {number} columnNumber
  243. * @return {WebInspector.DebuggerModel.Location}
  244. */
  245. _uiLocationToRawLocation: function(uiSourceCode, lineNumber, columnNumber)
  246. {
  247. var script = this._scriptForUISourceCode.get(uiSourceCode);
  248. if (!script)
  249. return null;
  250. return WebInspector.debuggerModel.createRawLocation(script, lineNumber, columnNumber);
  251. },
  252. /**
  253. * @param {WebInspector.Script} script
  254. */
  255. _addScript: function(script)
  256. {
  257. var snippetId = this._snippetIdForSourceURL(script.sourceURL);
  258. if (!snippetId)
  259. return;
  260. var uiSourceCode = this._uiSourceCodeForSnippetId[snippetId];
  261. if (!uiSourceCode || this._evaluationSourceURL(uiSourceCode) !== script.sourceURL)
  262. return;
  263. console.assert(!this._scriptForUISourceCode.get(uiSourceCode));
  264. this._uiSourceCodeForScriptId[script.scriptId] = uiSourceCode;
  265. this._scriptForUISourceCode.put(uiSourceCode, script);
  266. uiSourceCode.scriptFile().setHasDivergedFromVM(false);
  267. script.pushSourceMapping(this._snippetScriptMapping);
  268. },
  269. /**
  270. * @param {WebInspector.UISourceCode} uiSourceCode
  271. * @return {Array.<Object>}
  272. */
  273. _removeBreakpoints: function(uiSourceCode)
  274. {
  275. var breakpointLocations = WebInspector.breakpointManager.breakpointLocationsForUISourceCode(uiSourceCode);
  276. for (var i = 0; i < breakpointLocations.length; ++i)
  277. breakpointLocations[i].breakpoint.remove();
  278. return breakpointLocations;
  279. },
  280. /**
  281. * @param {WebInspector.UISourceCode} uiSourceCode
  282. * @param {Array.<Object>} breakpointLocations
  283. */
  284. _restoreBreakpoints: function(uiSourceCode, breakpointLocations)
  285. {
  286. for (var i = 0; i < breakpointLocations.length; ++i) {
  287. var uiLocation = breakpointLocations[i].uiLocation;
  288. var breakpoint = breakpointLocations[i].breakpoint;
  289. WebInspector.breakpointManager.setBreakpoint(uiSourceCode, uiLocation.lineNumber, breakpoint.condition(), breakpoint.enabled());
  290. }
  291. },
  292. /**
  293. * @param {WebInspector.UISourceCode} uiSourceCode
  294. * @return {WebInspector.UISourceCode}
  295. */
  296. _releaseSnippetScript: function(uiSourceCode)
  297. {
  298. var script = this._scriptForUISourceCode.get(uiSourceCode);
  299. if (!script)
  300. return null;
  301. uiSourceCode.scriptFile().setIsDivergingFromVM(true);
  302. uiSourceCode.scriptFile().setHasDivergedFromVM(true);
  303. delete this._uiSourceCodeForScriptId[script.scriptId];
  304. this._scriptForUISourceCode.remove(uiSourceCode);
  305. delete uiSourceCode._evaluationIndex;
  306. script.popSourceMapping(this._snippetScriptMapping);
  307. uiSourceCode.scriptFile().setIsDivergingFromVM(false);
  308. return script.rawLocationToUILocation(0, 0).uiSourceCode;
  309. },
  310. _debuggerReset: function()
  311. {
  312. for (var snippetId in this._uiSourceCodeForSnippetId) {
  313. var uiSourceCode = this._uiSourceCodeForSnippetId[snippetId];
  314. this._releaseSnippetScript(uiSourceCode);
  315. }
  316. },
  317. /**
  318. * @param {WebInspector.UISourceCode} uiSourceCode
  319. * @return {string}
  320. */
  321. _evaluationSourceURL: function(uiSourceCode)
  322. {
  323. var evaluationSuffix = "_" + uiSourceCode._evaluationIndex;
  324. var snippetId = this._snippetIdForUISourceCode.get(uiSourceCode);
  325. return WebInspector.Script.snippetSourceURLPrefix + snippetId + evaluationSuffix;
  326. },
  327. /**
  328. * @param {string} sourceURL
  329. * @return {string|null}
  330. */
  331. _snippetIdForSourceURL: function(sourceURL)
  332. {
  333. var snippetPrefix = WebInspector.Script.snippetSourceURLPrefix;
  334. if (!sourceURL.startsWith(snippetPrefix))
  335. return null;
  336. var splittedURL = sourceURL.substring(snippetPrefix.length).split("_");
  337. var snippetId = splittedURL[0];
  338. return snippetId;
  339. },
  340. reset: function()
  341. {
  342. /** @type {!Object.<string, WebInspector.UISourceCode>} */
  343. this._uiSourceCodeForScriptId = {};
  344. this._scriptForUISourceCode = new Map();
  345. /** @type {!Object.<string, WebInspector.UISourceCode>} */
  346. this._uiSourceCodeForSnippetId = {};
  347. this._snippetIdForUISourceCode = new Map();
  348. this._workspaceProvider.reset();
  349. this._loadSnippets();
  350. },
  351. __proto__: WebInspector.Object.prototype
  352. }
  353. /**
  354. * @constructor
  355. * @implements {WebInspector.ScriptFile}
  356. * @extends {WebInspector.Object}
  357. * @param {WebInspector.ScriptSnippetModel} scriptSnippetModel
  358. * @param {WebInspector.UISourceCode} uiSourceCode
  359. */
  360. WebInspector.SnippetScriptFile = function(scriptSnippetModel, uiSourceCode)
  361. {
  362. WebInspector.ScriptFile.call(this);
  363. this._scriptSnippetModel = scriptSnippetModel;
  364. this._uiSourceCode = uiSourceCode;
  365. this._hasDivergedFromVM = true;
  366. this._uiSourceCode.addEventListener(WebInspector.UISourceCode.Events.WorkingCopyCommitted, this._workingCopyCommitted, this);
  367. this._uiSourceCode.addEventListener(WebInspector.UISourceCode.Events.WorkingCopyChanged, this._workingCopyChanged, this);
  368. }
  369. WebInspector.SnippetScriptFile.prototype = {
  370. /**
  371. * @return {boolean}
  372. */
  373. hasDivergedFromVM: function()
  374. {
  375. return this._hasDivergedFromVM;
  376. },
  377. /**
  378. * @param {boolean} hasDivergedFromVM
  379. */
  380. setHasDivergedFromVM: function(hasDivergedFromVM)
  381. {
  382. this._hasDivergedFromVM = hasDivergedFromVM;
  383. },
  384. /**
  385. * @return {boolean}
  386. */
  387. isDivergingFromVM: function()
  388. {
  389. return this._isDivergingFromVM;
  390. },
  391. checkMapping: function()
  392. {
  393. },
  394. /**
  395. * @return {boolean}
  396. */
  397. isMergingToVM: function()
  398. {
  399. return false;
  400. },
  401. /**
  402. * @param {boolean} isDivergingFromVM
  403. */
  404. setIsDivergingFromVM: function(isDivergingFromVM)
  405. {
  406. this._isDivergingFromVM = isDivergingFromVM;
  407. },
  408. _workingCopyCommitted: function()
  409. {
  410. this._scriptSnippetModel._setScriptSnippetContent(this._uiSourceCode, this._uiSourceCode.workingCopy());
  411. },
  412. _workingCopyChanged: function()
  413. {
  414. this._scriptSnippetModel._scriptSnippetEdited(this._uiSourceCode);
  415. },
  416. __proto__: WebInspector.Object.prototype
  417. }
  418. /**
  419. * @constructor
  420. * @implements {WebInspector.ScriptSourceMapping}
  421. * @param {WebInspector.ScriptSnippetModel} scriptSnippetModel
  422. */
  423. WebInspector.SnippetScriptMapping = function(scriptSnippetModel)
  424. {
  425. this._scriptSnippetModel = scriptSnippetModel;
  426. }
  427. WebInspector.SnippetScriptMapping.prototype = {
  428. /**
  429. * @param {WebInspector.RawLocation} rawLocation
  430. * @return {WebInspector.UILocation}
  431. */
  432. rawLocationToUILocation: function(rawLocation)
  433. {
  434. var debuggerModelLocation = /** @type {WebInspector.DebuggerModel.Location} */(rawLocation);
  435. return this._scriptSnippetModel._rawLocationToUILocation(debuggerModelLocation);
  436. },
  437. /**
  438. * @param {WebInspector.UISourceCode} uiSourceCode
  439. * @param {number} lineNumber
  440. * @param {number} columnNumber
  441. * @return {WebInspector.DebuggerModel.Location}
  442. */
  443. uiLocationToRawLocation: function(uiSourceCode, lineNumber, columnNumber)
  444. {
  445. return this._scriptSnippetModel._uiLocationToRawLocation(uiSourceCode, lineNumber, columnNumber);
  446. },
  447. /**
  448. * @return {boolean}
  449. */
  450. isIdentity: function()
  451. {
  452. return true;
  453. },
  454. /**
  455. * @param {string} sourceURL
  456. * @return {string|null}
  457. */
  458. snippetIdForSourceURL: function(sourceURL)
  459. {
  460. return this._scriptSnippetModel._snippetIdForSourceURL(sourceURL);
  461. },
  462. /**
  463. * @param {WebInspector.Script} script
  464. */
  465. addScript: function(script)
  466. {
  467. this._scriptSnippetModel._addScript(script);
  468. }
  469. }
  470. /**
  471. * @constructor
  472. * @extends {WebInspector.StaticContentProvider}
  473. * @param {WebInspector.Snippet} snippet
  474. */
  475. WebInspector.SnippetContentProvider = function(snippet)
  476. {
  477. WebInspector.StaticContentProvider.call(this, WebInspector.resourceTypes.Script, snippet.content);
  478. }
  479. WebInspector.SnippetContentProvider.prototype = {
  480. __proto__: WebInspector.StaticContentProvider.prototype
  481. }
  482. /**
  483. * @type {?WebInspector.ScriptSnippetModel}
  484. */
  485. WebInspector.scriptSnippetModel = null;