ContentProviderBasedProjectDelegate.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * Copyright (C) 2013 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.ProjectDelegate}
  33. * @extends {WebInspector.Object}
  34. * @param {string} type
  35. */
  36. WebInspector.ContentProviderBasedProjectDelegate = function(type)
  37. {
  38. this._type = type;
  39. /** @type {!Object.<string, !WebInspector.ContentProvider>} */
  40. this._contentProviders = {};
  41. /** @type {Object.<string, boolean>} */
  42. this._isContentScriptMap = {};
  43. }
  44. WebInspector.ContentProviderBasedProjectDelegate.prototype = {
  45. /**
  46. * @return {string}
  47. */
  48. id: function()
  49. {
  50. // Overriddden by subclasses
  51. return "";
  52. },
  53. /**
  54. * @return {string}
  55. */
  56. type: function()
  57. {
  58. return this._type;
  59. },
  60. /**
  61. * @return {string}
  62. */
  63. displayName: function()
  64. {
  65. // Overriddden by subclasses
  66. return "";
  67. },
  68. /**
  69. * @param {string} path
  70. * @param {function(?Date, ?number)} callback
  71. */
  72. requestMetadata: function(path, callback)
  73. {
  74. callback(null, null);
  75. },
  76. /**
  77. * @param {string} path
  78. * @param {function(?string,boolean,string)} callback
  79. */
  80. requestFileContent: function(path, callback)
  81. {
  82. var contentProvider = this._contentProviders[path];
  83. contentProvider.requestContent(callback);
  84. },
  85. /**
  86. * @return {boolean}
  87. */
  88. canSetFileContent: function()
  89. {
  90. return false;
  91. },
  92. /**
  93. * @param {string} path
  94. * @param {string} newContent
  95. * @param {function(?string)} callback
  96. */
  97. setFileContent: function(path, newContent, callback)
  98. {
  99. callback(null);
  100. },
  101. /**
  102. * @return {boolean}
  103. */
  104. canRename: function()
  105. {
  106. return false;
  107. },
  108. /**
  109. * @param {string} path
  110. * @param {string} newName
  111. * @param {function(boolean, string=)} callback
  112. */
  113. rename: function(path, newName, callback)
  114. {
  115. this.performRename(path, newName, innerCallback.bind(this));
  116. /**
  117. * @param {boolean} success
  118. * @param {string=} newName
  119. */
  120. function innerCallback(success, newName)
  121. {
  122. if (success)
  123. this._updateName(path, newName);
  124. callback(success, newName);
  125. }
  126. },
  127. /**
  128. * @param {string} path
  129. */
  130. refresh: function(path)
  131. {
  132. },
  133. /**
  134. * @param {string} path
  135. */
  136. excludeFolder: function(path)
  137. {
  138. },
  139. /**
  140. * @param {string} path
  141. * @param {?string} name
  142. * @param {function(?string)} callback
  143. */
  144. createFile: function(path, name, callback)
  145. {
  146. },
  147. /**
  148. * @param {string} path
  149. */
  150. deleteFile: function(path)
  151. {
  152. },
  153. remove: function()
  154. {
  155. },
  156. /**
  157. * @param {string} path
  158. * @param {string} newName
  159. * @param {function(boolean, string=)} callback
  160. */
  161. performRename: function(path, newName, callback)
  162. {
  163. callback(false);
  164. },
  165. /**
  166. * @param {string} path
  167. * @param {string} newName
  168. */
  169. _updateName: function(path, newName)
  170. {
  171. var oldPath = path;
  172. var copyOfPath = path.split("/");
  173. copyOfPath[copyOfPath.length - 1] = newName;
  174. var newPath = copyOfPath.join("/");
  175. this._contentProviders[newPath] = this._contentProviders[oldPath];
  176. delete this._contentProviders[oldPath];
  177. },
  178. /**
  179. * @param {string} path
  180. * @param {string} query
  181. * @param {boolean} caseSensitive
  182. * @param {boolean} isRegex
  183. * @param {function(Array.<WebInspector.ContentProvider.SearchMatch>)} callback
  184. */
  185. searchInFileContent: function(path, query, caseSensitive, isRegex, callback)
  186. {
  187. var contentProvider = this._contentProviders[path];
  188. contentProvider.searchInContent(query, caseSensitive, isRegex, callback);
  189. },
  190. /**
  191. * @param {string} query
  192. * @param {boolean} caseSensitive
  193. * @param {boolean} isRegex
  194. * @param {WebInspector.Progress} progress
  195. * @param {function(StringMap)} callback
  196. */
  197. searchInContent: function(query, caseSensitive, isRegex, progress, callback)
  198. {
  199. var result = new StringMap();
  200. var paths = Object.keys(this._contentProviders);
  201. var totalCount = paths.length;
  202. if (totalCount === 0) {
  203. // searchInContent should call back later.
  204. setTimeout(doneCallback, 0);
  205. return;
  206. }
  207. function filterOutContentScripts(path)
  208. {
  209. return !this._isContentScriptMap[path];
  210. }
  211. if (!WebInspector.settings.searchInContentScripts.get())
  212. paths = paths.filter(filterOutContentScripts.bind(this));
  213. var barrier = new CallbackBarrier();
  214. progress.setTotalWork(paths.length);
  215. for (var i = 0; i < paths.length; ++i)
  216. this._contentProviders[paths[i]].searchInContent(query, caseSensitive, isRegex, barrier.createCallback(contentCallback.bind(this, i)));
  217. barrier.callWhenDone(doneCallback);
  218. function contentCallback(i, searchMatches)
  219. {
  220. result.put(paths[i], searchMatches);
  221. progress.worked(1);
  222. }
  223. function doneCallback()
  224. {
  225. callback(result);
  226. progress.done();
  227. }
  228. },
  229. /**
  230. * @param {WebInspector.Progress} progress
  231. * @param {function()} callback
  232. */
  233. indexContent: function(progress, callback)
  234. {
  235. setTimeout(innerCallback, 0);
  236. function innerCallback()
  237. {
  238. progress.done();
  239. callback();
  240. }
  241. },
  242. /**
  243. * @param {string} parentPath
  244. * @param {string} name
  245. * @param {string} url
  246. * @param {WebInspector.ContentProvider} contentProvider
  247. * @param {boolean} isEditable
  248. * @param {boolean=} isContentScript
  249. * @return {string}
  250. */
  251. addContentProvider: function(parentPath, name, url, contentProvider, isEditable, isContentScript)
  252. {
  253. var path = parentPath ? parentPath + "/" + name : name;
  254. var fileDescriptor = new WebInspector.FileDescriptor(parentPath, name, url, url, contentProvider.contentType(), isEditable, isContentScript);
  255. this._contentProviders[path] = contentProvider;
  256. this._isContentScriptMap[path] = isContentScript || false;
  257. this.dispatchEventToListeners(WebInspector.ProjectDelegate.Events.FileAdded, fileDescriptor);
  258. return path;
  259. },
  260. /**
  261. * @param {string} path
  262. */
  263. removeFile: function(path)
  264. {
  265. delete this._contentProviders[path];
  266. delete this._isContentScriptMap[path];
  267. this.dispatchEventToListeners(WebInspector.ProjectDelegate.Events.FileRemoved, path);
  268. },
  269. /**
  270. * @return {Object.<string, WebInspector.ContentProvider>}
  271. */
  272. contentProviders: function()
  273. {
  274. return this._contentProviders;
  275. },
  276. reset: function()
  277. {
  278. this._contentProviders = {};
  279. this._isContentScriptMap = {};
  280. this.dispatchEventToListeners(WebInspector.ProjectDelegate.Events.Reset, null);
  281. },
  282. __proto__: WebInspector.Object.prototype
  283. }