ContentProviders.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * Copyright (C) 2011 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.ContentProvider}
  33. * @param {Array.<WebInspector.Script>} scripts
  34. */
  35. WebInspector.ConcatenatedScriptsContentProvider = function(scripts)
  36. {
  37. this._mimeType = "text/html";
  38. this._scripts = scripts;
  39. }
  40. WebInspector.ConcatenatedScriptsContentProvider.scriptOpenTag = "<script>";
  41. WebInspector.ConcatenatedScriptsContentProvider.scriptCloseTag = "</script>";
  42. WebInspector.ConcatenatedScriptsContentProvider.prototype = {
  43. /**
  44. * @return {Array.<WebInspector.Script>}
  45. */
  46. _sortedScripts: function()
  47. {
  48. if (this._sortedScriptsArray)
  49. return this._sortedScriptsArray;
  50. this._sortedScriptsArray = [];
  51. var scripts = this._scripts.slice();
  52. scripts.sort(function(x, y) { return x.lineOffset - y.lineOffset || x.columnOffset - y.columnOffset; });
  53. var scriptOpenTagLength = WebInspector.ConcatenatedScriptsContentProvider.scriptOpenTag.length;
  54. var scriptCloseTagLength = WebInspector.ConcatenatedScriptsContentProvider.scriptCloseTag.length;
  55. this._sortedScriptsArray.push(scripts[0]);
  56. for (var i = 1; i < scripts.length; ++i) {
  57. var previousScript = this._sortedScriptsArray[this._sortedScriptsArray.length - 1];
  58. var lineNumber = previousScript.endLine;
  59. var columnNumber = previousScript.endColumn + scriptCloseTagLength + scriptOpenTagLength;
  60. if (lineNumber < scripts[i].lineOffset || (lineNumber === scripts[i].lineOffset && columnNumber <= scripts[i].columnOffset))
  61. this._sortedScriptsArray.push(scripts[i]);
  62. }
  63. return this._sortedScriptsArray;
  64. },
  65. /**
  66. * @return {string}
  67. */
  68. contentURL: function()
  69. {
  70. return "";
  71. },
  72. /**
  73. * @return {WebInspector.ResourceType}
  74. */
  75. contentType: function()
  76. {
  77. return WebInspector.resourceTypes.Document;
  78. },
  79. /**
  80. * @param {function(?string,boolean,string)} callback
  81. */
  82. requestContent: function(callback)
  83. {
  84. var scripts = this._sortedScripts();
  85. var sources = [];
  86. function didRequestSource(content, contentEncoded, mimeType)
  87. {
  88. sources.push(content);
  89. if (sources.length == scripts.length)
  90. callback(this._concatenateScriptsContent(scripts, sources), false, this._mimeType);
  91. }
  92. for (var i = 0; i < scripts.length; ++i)
  93. scripts[i].requestContent(didRequestSource.bind(this));
  94. },
  95. /**
  96. * @param {string} query
  97. * @param {boolean} caseSensitive
  98. * @param {boolean} isRegex
  99. * @param {function(Array.<WebInspector.ContentProvider.SearchMatch>)} callback
  100. */
  101. searchInContent: function(query, caseSensitive, isRegex, callback)
  102. {
  103. var results = {};
  104. var scripts = this._sortedScripts();
  105. var scriptsLeft = scripts.length;
  106. function maybeCallback()
  107. {
  108. if (scriptsLeft)
  109. return;
  110. var result = [];
  111. for (var i = 0; i < scripts.length; ++i)
  112. result = result.concat(results[scripts[i].scriptId]);
  113. callback(result);
  114. }
  115. /**
  116. * @param {WebInspector.Script} script
  117. * @param {Array.<PageAgent.SearchMatch>} searchMatches
  118. */
  119. function searchCallback(script, searchMatches)
  120. {
  121. results[script.scriptId] = [];
  122. for (var i = 0; i < searchMatches.length; ++i) {
  123. var searchMatch = new WebInspector.ContentProvider.SearchMatch(searchMatches[i].lineNumber + script.lineOffset, searchMatches[i].lineContent);
  124. results[script.scriptId].push(searchMatch);
  125. }
  126. scriptsLeft--;
  127. maybeCallback.call(this);
  128. }
  129. maybeCallback();
  130. for (var i = 0; i < scripts.length; ++i)
  131. scripts[i].searchInContent(query, caseSensitive, isRegex, searchCallback.bind(this, scripts[i]));
  132. },
  133. /**
  134. * @return {string}
  135. */
  136. _concatenateScriptsContent: function(scripts, sources)
  137. {
  138. var content = "";
  139. var lineNumber = 0;
  140. var columnNumber = 0;
  141. var scriptOpenTag = WebInspector.ConcatenatedScriptsContentProvider.scriptOpenTag;
  142. var scriptCloseTag = WebInspector.ConcatenatedScriptsContentProvider.scriptCloseTag;
  143. for (var i = 0; i < scripts.length; ++i) {
  144. // Fill the gap with whitespace characters.
  145. for (var newLinesCount = scripts[i].lineOffset - lineNumber; newLinesCount > 0; --newLinesCount) {
  146. columnNumber = 0;
  147. content += "\n";
  148. }
  149. for (var spacesCount = scripts[i].columnOffset - columnNumber - scriptOpenTag.length; spacesCount > 0; --spacesCount)
  150. content += " ";
  151. // Add script tag.
  152. content += scriptOpenTag;
  153. content += sources[i];
  154. content += scriptCloseTag;
  155. lineNumber = scripts[i].endLine;
  156. columnNumber = scripts[i].endColumn + scriptCloseTag.length;
  157. }
  158. return content;
  159. },
  160. __proto__: WebInspector.ContentProvider.prototype
  161. }
  162. /**
  163. * @constructor
  164. * @param {string} sourceURL
  165. * @implements {WebInspector.ContentProvider}
  166. */
  167. WebInspector.CompilerSourceMappingContentProvider = function(sourceURL)
  168. {
  169. this._sourceURL = sourceURL;
  170. }
  171. WebInspector.CompilerSourceMappingContentProvider.prototype = {
  172. /**
  173. * @return {string}
  174. */
  175. contentURL: function()
  176. {
  177. return this._sourceURL;
  178. },
  179. /**
  180. * @return {WebInspector.ResourceType}
  181. */
  182. contentType: function()
  183. {
  184. return WebInspector.resourceTypes.Script;
  185. },
  186. /**
  187. * @param {function(?string,boolean,string)} callback
  188. */
  189. requestContent: function(callback)
  190. {
  191. var sourceCode = "";
  192. try {
  193. // FIXME: make sendRequest async.
  194. sourceCode = InspectorFrontendHost.loadResourceSynchronously(this._sourceURL);
  195. } catch(e) {
  196. console.error(e.message);
  197. }
  198. callback(sourceCode, false, "text/javascript");
  199. },
  200. /**
  201. * @param {string} query
  202. * @param {boolean} caseSensitive
  203. * @param {boolean} isRegex
  204. * @param {function(Array.<WebInspector.ContentProvider.SearchMatch>)} callback
  205. */
  206. searchInContent: function(query, caseSensitive, isRegex, callback)
  207. {
  208. callback([]);
  209. },
  210. __proto__: WebInspector.ContentProvider.prototype
  211. }
  212. /**
  213. * @constructor
  214. * @implements {WebInspector.ContentProvider}
  215. * @param {WebInspector.ResourceType} contentType
  216. * @param {string} content
  217. * @param {string=} mimeType
  218. */
  219. WebInspector.StaticContentProvider = function(contentType, content, mimeType)
  220. {
  221. this._content = content;
  222. this._contentType = contentType;
  223. this._mimeType = mimeType;
  224. }
  225. WebInspector.StaticContentProvider.prototype = {
  226. /**
  227. * @return {string}
  228. */
  229. contentURL: function()
  230. {
  231. return "";
  232. },
  233. /**
  234. * @return {WebInspector.ResourceType}
  235. */
  236. contentType: function()
  237. {
  238. return this._contentType;
  239. },
  240. /**
  241. * @param {function(?string,boolean,string)} callback
  242. */
  243. requestContent: function(callback)
  244. {
  245. callback(this._content, false, this._mimeType || this._contentType.canonicalMimeType());
  246. },
  247. /**
  248. * @param {string} query
  249. * @param {boolean} caseSensitive
  250. * @param {boolean} isRegex
  251. * @param {function(Array.<WebInspector.ContentProvider.SearchMatch>)} callback
  252. */
  253. searchInContent: function(query, caseSensitive, isRegex, callback)
  254. {
  255. function performSearch()
  256. {
  257. callback(WebInspector.ContentProvider.performSearchInContent(this._content, query, caseSensitive, isRegex));
  258. }
  259. // searchInContent should call back later.
  260. window.setTimeout(performSearch.bind(this), 0);
  261. },
  262. __proto__: WebInspector.ContentProvider.prototype
  263. }