CompilerScriptMapping.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. * @param {WebInspector.SimpleWorkspaceProvider} networkWorkspaceProvider
  35. */
  36. WebInspector.CompilerScriptMapping = function(workspace, networkWorkspaceProvider)
  37. {
  38. this._workspace = workspace;
  39. this._workspace.addEventListener(WebInspector.UISourceCodeProvider.Events.UISourceCodeAdded, this._uiSourceCodeAddedToWorkspace, this);
  40. this._networkWorkspaceProvider = networkWorkspaceProvider;
  41. /** @type {Object.<string, WebInspector.SourceMap>} */
  42. this._sourceMapForSourceMapURL = {};
  43. /** @type {Object.<string, WebInspector.SourceMap>} */
  44. this._sourceMapForScriptId = {};
  45. this._scriptForSourceMap = new Map();
  46. /** @type {Object.<string, WebInspector.SourceMap>} */
  47. this._sourceMapForURL = {};
  48. WebInspector.debuggerModel.addEventListener(WebInspector.DebuggerModel.Events.GlobalObjectCleared, this._debuggerReset, this);
  49. }
  50. WebInspector.CompilerScriptMapping.prototype = {
  51. /**
  52. * @param {WebInspector.RawLocation} rawLocation
  53. * @return {WebInspector.UILocation}
  54. */
  55. rawLocationToUILocation: function(rawLocation)
  56. {
  57. var debuggerModelLocation = /** @type {WebInspector.DebuggerModel.Location} */ (rawLocation);
  58. var sourceMap = this._sourceMapForScriptId[debuggerModelLocation.scriptId];
  59. var lineNumber = debuggerModelLocation.lineNumber;
  60. var columnNumber = debuggerModelLocation.columnNumber || 0;
  61. var entry = sourceMap.findEntry(lineNumber, columnNumber);
  62. if (!entry || entry.length === 2)
  63. return null;
  64. var url = entry[2];
  65. var uiSourceCode = this._workspace.uiSourceCodeForURL(url);
  66. if (!uiSourceCode)
  67. return null;
  68. return new WebInspector.UILocation(uiSourceCode, entry[3], entry[4]);
  69. },
  70. /**
  71. * @param {WebInspector.UISourceCode} uiSourceCode
  72. * @param {number} lineNumber
  73. * @param {number} columnNumber
  74. * @return {WebInspector.DebuggerModel.Location}
  75. */
  76. uiLocationToRawLocation: function(uiSourceCode, lineNumber, columnNumber)
  77. {
  78. if (!uiSourceCode.url)
  79. return null;
  80. var sourceMap = this._sourceMapForURL[uiSourceCode.url];
  81. if (!sourceMap)
  82. return null;
  83. var entry = sourceMap.findEntryReversed(uiSourceCode.url, lineNumber);
  84. return WebInspector.debuggerModel.createRawLocation(this._scriptForSourceMap.get(sourceMap), entry[0], entry[1]);
  85. },
  86. /**
  87. * @return {boolean}
  88. */
  89. isIdentity: function()
  90. {
  91. return false;
  92. },
  93. /**
  94. * @param {WebInspector.Script} script
  95. */
  96. addScript: function(script)
  97. {
  98. var sourceMap = this.loadSourceMapForScript(script);
  99. if (!sourceMap)
  100. return;
  101. if (this._scriptForSourceMap.get(sourceMap)) {
  102. this._sourceMapForScriptId[script.scriptId] = sourceMap;
  103. script.pushSourceMapping(this);
  104. return;
  105. }
  106. this._sourceMapForScriptId[script.scriptId] = sourceMap;
  107. this._scriptForSourceMap.put(sourceMap, script);
  108. var sourceURLs = sourceMap.sources();
  109. for (var i = 0; i < sourceURLs.length; ++i) {
  110. var sourceURL = sourceURLs[i];
  111. if (this._sourceMapForURL[sourceURL])
  112. continue;
  113. this._sourceMapForURL[sourceURL] = sourceMap;
  114. if (!this._workspace.hasMappingForURL(sourceURL) && !this._workspace.uiSourceCodeForURL(sourceURL)) {
  115. var sourceContent = sourceMap.sourceContent(sourceURL);
  116. var contentProvider;
  117. if (sourceContent)
  118. contentProvider = new WebInspector.StaticContentProvider(WebInspector.resourceTypes.Script, sourceContent);
  119. else
  120. contentProvider = new WebInspector.CompilerSourceMappingContentProvider(sourceURL);
  121. this._networkWorkspaceProvider.addFileForURL(sourceURL, contentProvider, true);
  122. }
  123. var uiSourceCode = this._workspace.uiSourceCodeForURL(sourceURL);
  124. if (uiSourceCode) {
  125. this._bindUISourceCode(uiSourceCode);
  126. uiSourceCode.isContentScript = script.isContentScript;
  127. }
  128. }
  129. script.pushSourceMapping(this);
  130. },
  131. /**
  132. * @param {WebInspector.UISourceCode} uiSourceCode
  133. */
  134. _bindUISourceCode: function(uiSourceCode)
  135. {
  136. uiSourceCode.setSourceMapping(this);
  137. },
  138. /**
  139. * @param {WebInspector.Event} event
  140. */
  141. _uiSourceCodeAddedToWorkspace: function(event)
  142. {
  143. var uiSourceCode = /** @type {WebInspector.UISourceCode} */ (event.data);
  144. if (!uiSourceCode.url || !this._sourceMapForURL[uiSourceCode.url])
  145. return;
  146. this._bindUISourceCode(uiSourceCode);
  147. },
  148. /**
  149. * @param {WebInspector.Script} script
  150. * @return {?WebInspector.SourceMap}
  151. */
  152. loadSourceMapForScript: function(script)
  153. {
  154. // script.sourceURL can be a random string, but is generally an absolute path -> complete it to inspected page url for
  155. // relative links.
  156. if (!script.sourceMapURL)
  157. return null;
  158. var scriptURL = WebInspector.ParsedURL.completeURL(WebInspector.inspectedPageURL, script.sourceURL);
  159. if (!scriptURL)
  160. return null;
  161. var sourceMapURL = WebInspector.ParsedURL.completeURL(scriptURL, script.sourceMapURL);
  162. if (!sourceMapURL)
  163. return null;
  164. var sourceMap = this._sourceMapForSourceMapURL[sourceMapURL];
  165. if (sourceMap)
  166. return sourceMap;
  167. sourceMap = WebInspector.SourceMap.load(sourceMapURL, scriptURL);
  168. if (!sourceMap)
  169. return null;
  170. this._sourceMapForSourceMapURL[sourceMapURL] = sourceMap;
  171. return sourceMap;
  172. },
  173. _debuggerReset: function()
  174. {
  175. this._sourceMapForSourceMapURL = {};
  176. this._sourceMapForScriptId = {};
  177. this._scriptForSourceMap = new Map();
  178. this._sourceMapForURL = {};
  179. }
  180. }