PageRenderTime 33ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/Source/WebInspectorUI/UserInterface/Models/SourceCode.js

https://gitlab.com/paretje/qtwebkit
JavaScript | 212 lines | 133 code | 43 blank | 36 comment | 14 complexity | 9cf046b3600ed2da10fbc90f47e73774 MD5 | raw file
  1. /*
  2. * Copyright (C) 2013 Apple 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
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
  14. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  15. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
  17. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  23. * THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. WebInspector.SourceCode = class SourceCode extends WebInspector.Object
  26. {
  27. constructor()
  28. {
  29. super();
  30. this._originalRevision = new WebInspector.SourceCodeRevision(this, null, false);
  31. this._currentRevision = this._originalRevision;
  32. this._sourceMaps = null;
  33. this._formatterSourceMap = null;
  34. this._requestContentPromise = null;
  35. }
  36. // Public
  37. get displayName()
  38. {
  39. // Implemented by subclasses.
  40. console.error("Needs to be implemented by a subclass.");
  41. return "";
  42. }
  43. get originalRevision()
  44. {
  45. return this._originalRevision;
  46. }
  47. get currentRevision()
  48. {
  49. return this._currentRevision;
  50. }
  51. set currentRevision(revision)
  52. {
  53. console.assert(revision instanceof WebInspector.SourceCodeRevision);
  54. if (!(revision instanceof WebInspector.SourceCodeRevision))
  55. return;
  56. console.assert(revision.sourceCode === this);
  57. if (revision.sourceCode !== this)
  58. return;
  59. this._currentRevision = revision;
  60. this.dispatchEventToListeners(WebInspector.SourceCode.Event.ContentDidChange);
  61. }
  62. get content()
  63. {
  64. return this._currentRevision.content;
  65. }
  66. get sourceMaps()
  67. {
  68. return this._sourceMaps || [];
  69. }
  70. addSourceMap(sourceMap)
  71. {
  72. console.assert(sourceMap instanceof WebInspector.SourceMap);
  73. if (!this._sourceMaps)
  74. this._sourceMaps = [];
  75. this._sourceMaps.push(sourceMap);
  76. this.dispatchEventToListeners(WebInspector.SourceCode.Event.SourceMapAdded);
  77. }
  78. get formatterSourceMap()
  79. {
  80. return this._formatterSourceMap;
  81. }
  82. set formatterSourceMap(formatterSourceMap)
  83. {
  84. console.assert(this._formatterSourceMap === null || formatterSourceMap === null);
  85. console.assert(formatterSourceMap === null || formatterSourceMap instanceof WebInspector.FormatterSourceMap);
  86. this._formatterSourceMap = formatterSourceMap;
  87. this.dispatchEventToListeners(WebInspector.SourceCode.Event.FormatterDidChange);
  88. }
  89. requestContent()
  90. {
  91. this._requestContentPromise = this._requestContentPromise || this.requestContentFromBackend().then(this._processContent.bind(this));
  92. return this._requestContentPromise;
  93. }
  94. createSourceCodeLocation(lineNumber, columnNumber)
  95. {
  96. return new WebInspector.SourceCodeLocation(this, lineNumber, columnNumber);
  97. }
  98. createLazySourceCodeLocation(lineNumber, columnNumber)
  99. {
  100. return new WebInspector.LazySourceCodeLocation(this, lineNumber, columnNumber);
  101. }
  102. createSourceCodeTextRange(textRange)
  103. {
  104. return new WebInspector.SourceCodeTextRange(this, textRange);
  105. }
  106. // Protected
  107. revisionContentDidChange(revision)
  108. {
  109. if (this._ignoreRevisionContentDidChangeEvent)
  110. return;
  111. if (revision !== this._currentRevision)
  112. return;
  113. this.handleCurrentRevisionContentChange();
  114. this.dispatchEventToListeners(WebInspector.SourceCode.Event.ContentDidChange);
  115. }
  116. handleCurrentRevisionContentChange()
  117. {
  118. // Implemented by subclasses if needed.
  119. }
  120. get revisionForRequestedContent()
  121. {
  122. // Implemented by subclasses if needed.
  123. return this._originalRevision;
  124. }
  125. markContentAsStale()
  126. {
  127. this._requestContentPromise = null;
  128. this._contentReceived = false;
  129. }
  130. requestContentFromBackend()
  131. {
  132. // Implemented by subclasses.
  133. console.error("Needs to be implemented by a subclass.");
  134. return Promise.reject(new Error("Needs to be implemented by a subclass."));
  135. }
  136. get mimeType()
  137. {
  138. // Implemented by subclasses.
  139. console.error("Needs to be implemented by a subclass.");
  140. }
  141. // Private
  142. _processContent(parameters)
  143. {
  144. // Different backend APIs return one of `content, `body`, `text`, or `scriptSource`.
  145. var content = parameters.content || parameters.body || parameters.text || parameters.scriptSource;
  146. var error = parameters.error;
  147. if (parameters.base64Encoded)
  148. content = decodeBase64ToBlob(content, this.mimeType);
  149. var revision = this.revisionForRequestedContent;
  150. this._ignoreRevisionContentDidChangeEvent = true;
  151. revision.content = content || null;
  152. this._ignoreRevisionContentDidChangeEvent = false;
  153. // FIXME: Returning the content in this promise is misleading. It may not be current content
  154. // now, and it may become out-dated later on. We should drop content from this promise
  155. // and require clients to ask for the current contents from the sourceCode in the result.
  156. return Promise.resolve({
  157. error,
  158. sourceCode: this,
  159. content,
  160. });
  161. }
  162. };
  163. WebInspector.SourceCode.Event = {
  164. ContentDidChange: "source-code-content-did-change",
  165. SourceMapAdded: "source-code-source-map-added",
  166. FormatterDidChange: "source-code-formatter-did-change",
  167. LoadingDidFinish: "source-code-loading-did-finish",
  168. LoadingDidFail: "source-code-loading-did-fail"
  169. };