ContentProviders.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. * @param {WebInspector.ResourceType} contentType
  166. * @param {string} mimeType
  167. * @implements {WebInspector.ContentProvider}
  168. */
  169. WebInspector.CompilerSourceMappingContentProvider = function(sourceURL, contentType, mimeType)
  170. {
  171. this._sourceURL = sourceURL;
  172. this._contentType = contentType;
  173. this._mimeType = mimeType;
  174. }
  175. WebInspector.CompilerSourceMappingContentProvider.prototype = {
  176. /**
  177. * @return {string}
  178. */
  179. contentURL: function()
  180. {
  181. return this._sourceURL;
  182. },
  183. /**
  184. * @return {WebInspector.ResourceType}
  185. */
  186. contentType: function()
  187. {
  188. return this._contentType;
  189. },
  190. /**
  191. * @param {function(?string,boolean,string)} callback
  192. */
  193. requestContent: function(callback)
  194. {
  195. NetworkAgent.loadResourceForFrontend(WebInspector.resourceTreeModel.mainFrame.id, this._sourceURL, undefined, contentLoaded.bind(this));
  196. /**
  197. * @param {?Protocol.Error} error
  198. * @param {number} statusCode
  199. * @param {NetworkAgent.Headers} headers
  200. * @param {string} content
  201. */
  202. function contentLoaded(error, statusCode, headers, content)
  203. {
  204. if (error || statusCode >= 400) {
  205. console.error("Could not load content for " + this._sourceURL + " : " + (error || ("HTTP status code: " + statusCode)));
  206. callback(null, false, this._mimeType);
  207. return;
  208. }
  209. callback(content, false, this._mimeType);
  210. }
  211. },
  212. /**
  213. * @param {string} query
  214. * @param {boolean} caseSensitive
  215. * @param {boolean} isRegex
  216. * @param {function(Array.<WebInspector.ContentProvider.SearchMatch>)} callback
  217. */
  218. searchInContent: function(query, caseSensitive, isRegex, callback)
  219. {
  220. this.requestContent(contentLoaded);
  221. /**
  222. * @param {?string} content
  223. * @param {boolean} base64Encoded
  224. * @param {string} mimeType
  225. */
  226. function contentLoaded(content, base64Encoded, mimeType)
  227. {
  228. if (typeof content !== "string") {
  229. callback([]);
  230. return;
  231. }
  232. callback(WebInspector.ContentProvider.performSearchInContent(content, query, caseSensitive, isRegex));
  233. }
  234. },
  235. __proto__: WebInspector.ContentProvider.prototype
  236. }
  237. /**
  238. * @constructor
  239. * @implements {WebInspector.ContentProvider}
  240. * @param {WebInspector.ResourceType} contentType
  241. * @param {string} content
  242. * @param {string=} mimeType
  243. */
  244. WebInspector.StaticContentProvider = function(contentType, content, mimeType)
  245. {
  246. this._content = content;
  247. this._contentType = contentType;
  248. this._mimeType = mimeType;
  249. }
  250. WebInspector.StaticContentProvider.prototype = {
  251. /**
  252. * @return {string}
  253. */
  254. contentURL: function()
  255. {
  256. return "";
  257. },
  258. /**
  259. * @return {WebInspector.ResourceType}
  260. */
  261. contentType: function()
  262. {
  263. return this._contentType;
  264. },
  265. /**
  266. * @param {function(?string,boolean,string)} callback
  267. */
  268. requestContent: function(callback)
  269. {
  270. callback(this._content, false, this._mimeType || this._contentType.canonicalMimeType());
  271. },
  272. /**
  273. * @param {string} query
  274. * @param {boolean} caseSensitive
  275. * @param {boolean} isRegex
  276. * @param {function(Array.<WebInspector.ContentProvider.SearchMatch>)} callback
  277. */
  278. searchInContent: function(query, caseSensitive, isRegex, callback)
  279. {
  280. function performSearch()
  281. {
  282. callback(WebInspector.ContentProvider.performSearchInContent(this._content, query, caseSensitive, isRegex));
  283. }
  284. // searchInContent should call back later.
  285. window.setTimeout(performSearch.bind(this), 0);
  286. },
  287. __proto__: WebInspector.ContentProvider.prototype
  288. }
  289. /**
  290. * @constructor
  291. * @implements {WebInspector.ContentProvider}
  292. * @param {WebInspector.ContentProvider} contentProvider
  293. * @param {string} mimeType
  294. */
  295. WebInspector.ContentProviderOverridingMimeType = function(contentProvider, mimeType)
  296. {
  297. this._contentProvider = contentProvider;
  298. this._mimeType = mimeType;
  299. }
  300. WebInspector.ContentProviderOverridingMimeType.prototype = {
  301. /**
  302. * @return {string}
  303. */
  304. contentURL: function()
  305. {
  306. return this._contentProvider.contentURL();
  307. },
  308. /**
  309. * @return {WebInspector.ResourceType}
  310. */
  311. contentType: function()
  312. {
  313. return this._contentProvider.contentType();
  314. },
  315. /**
  316. * @param {function(?string,boolean,string)} callback
  317. */
  318. requestContent: function(callback)
  319. {
  320. this._contentProvider.requestContent(innerCallback.bind(this));
  321. function innerCallback(content, contentEncoded, mimeType)
  322. {
  323. callback(content, contentEncoded, this._mimeType);
  324. }
  325. },
  326. /**
  327. * @param {string} query
  328. * @param {boolean} caseSensitive
  329. * @param {boolean} isRegex
  330. * @param {function(Array.<WebInspector.ContentProvider.SearchMatch>)} callback
  331. */
  332. searchInContent: function(query, caseSensitive, isRegex, callback)
  333. {
  334. this._contentProvider.searchInContent(query, caseSensitive, isRegex, callback);
  335. },
  336. __proto__: WebInspector.ContentProvider.prototype
  337. }