DefaultScriptMapping.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. * @implements {WebInspector.ScriptSourceMapping}
  33. * @param {WebInspector.Workspace} workspace
  34. */
  35. WebInspector.DefaultScriptMapping = function(workspace)
  36. {
  37. this._projectDelegate = new WebInspector.DebuggerProjectDelegate();
  38. this._workspace = workspace;
  39. this._workspace.addProject(this._projectDelegate);
  40. WebInspector.debuggerModel.addEventListener(WebInspector.DebuggerModel.Events.GlobalObjectCleared, this._debuggerReset, this);
  41. this._debuggerReset();
  42. }
  43. WebInspector.DefaultScriptMapping.prototype = {
  44. /**
  45. * @param {WebInspector.RawLocation} rawLocation
  46. * @return {WebInspector.UILocation}
  47. */
  48. rawLocationToUILocation: function(rawLocation)
  49. {
  50. var debuggerModelLocation = /** @type {WebInspector.DebuggerModel.Location} */ (rawLocation);
  51. var script = WebInspector.debuggerModel.scriptForId(debuggerModelLocation.scriptId);
  52. var uiSourceCode = this._uiSourceCodeForScriptId[script.scriptId];
  53. var lineNumber = debuggerModelLocation.lineNumber;
  54. var columnNumber = debuggerModelLocation.columnNumber || 0;
  55. return new WebInspector.UILocation(uiSourceCode, lineNumber, columnNumber);
  56. },
  57. /**
  58. * @param {WebInspector.UISourceCode} uiSourceCode
  59. * @param {number} lineNumber
  60. * @param {number} columnNumber
  61. * @return {WebInspector.DebuggerModel.Location}
  62. */
  63. uiLocationToRawLocation: function(uiSourceCode, lineNumber, columnNumber)
  64. {
  65. var scriptId = this._scriptIdForUISourceCode.get(uiSourceCode);
  66. var script = WebInspector.debuggerModel.scriptForId(scriptId);
  67. return WebInspector.debuggerModel.createRawLocation(script, lineNumber, columnNumber);
  68. },
  69. /**
  70. * @return {boolean}
  71. */
  72. isIdentity: function()
  73. {
  74. return true;
  75. },
  76. /**
  77. * @param {WebInspector.Script} script
  78. */
  79. addScript: function(script)
  80. {
  81. var path = this._projectDelegate.addScript(script);
  82. var uiSourceCode = this._workspace.uiSourceCode(this._projectDelegate.id(), path);
  83. this._uiSourceCodeForScriptId[script.scriptId] = uiSourceCode;
  84. this._scriptIdForUISourceCode.put(uiSourceCode, script.scriptId);
  85. uiSourceCode.setSourceMapping(this);
  86. script.pushSourceMapping(this);
  87. script.addEventListener(WebInspector.Script.Events.ScriptEdited, this._scriptEdited.bind(this, script.scriptId));
  88. return uiSourceCode;
  89. },
  90. /**
  91. * @param {string} scriptId
  92. * @param {WebInspector.Event} event
  93. */
  94. _scriptEdited: function(scriptId, event)
  95. {
  96. var content = /** @type {string} */(event.data);
  97. this._uiSourceCodeForScriptId[scriptId].addRevision(content);
  98. },
  99. _debuggerReset: function()
  100. {
  101. /** @type {Object.<string, WebInspector.UISourceCode>} */
  102. this._uiSourceCodeForScriptId = {};
  103. this._scriptIdForUISourceCode = new Map();
  104. this._projectDelegate.reset();
  105. }
  106. }
  107. /**
  108. * @constructor
  109. * @extends {WebInspector.ContentProviderBasedProjectDelegate}
  110. */
  111. WebInspector.DebuggerProjectDelegate = function()
  112. {
  113. WebInspector.ContentProviderBasedProjectDelegate.call(this, WebInspector.projectTypes.Debugger);
  114. }
  115. WebInspector.DebuggerProjectDelegate.prototype = {
  116. /**
  117. * @return {string}
  118. */
  119. id: function()
  120. {
  121. return "debugger:";
  122. },
  123. /**
  124. * @return {string}
  125. */
  126. displayName: function()
  127. {
  128. return "debugger";
  129. },
  130. /**
  131. * @param {WebInspector.Script} script
  132. * @return {string}
  133. */
  134. addScript: function(script)
  135. {
  136. var contentProvider = script.isInlineScript() ? new WebInspector.ConcatenatedScriptsContentProvider([script]) : script;
  137. var splitURL = WebInspector.ParsedURL.splitURL(script.sourceURL);
  138. var name = splitURL[splitURL.length - 1];
  139. name = "[VM] " + name + " (" + script.scriptId + ")";
  140. return this.addContentProvider("", name, script.sourceURL, contentProvider, false, script.isContentScript);
  141. },
  142. __proto__: WebInspector.ContentProviderBasedProjectDelegate.prototype
  143. }