FileSystemProjectDelegate.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 {WebInspector.IsolatedFileSystem} isolatedFileSystem
  35. * @param {WebInspector.Workspace} workspace
  36. */
  37. WebInspector.FileSystemProjectDelegate = function(isolatedFileSystem, workspace)
  38. {
  39. this._fileSystem = isolatedFileSystem;
  40. this._workspace = workspace;
  41. }
  42. WebInspector.FileSystemProjectDelegate._scriptExtensions = ["js", "java", "cc", "cpp", "h", "cs", "py", "php"].keySet();
  43. WebInspector.FileSystemProjectDelegate.projectId = function(fileSystemPath)
  44. {
  45. return "filesystem:" + fileSystemPath;
  46. }
  47. WebInspector.FileSystemProjectDelegate.prototype = {
  48. /**
  49. * @return {string}
  50. */
  51. id: function()
  52. {
  53. return WebInspector.FileSystemProjectDelegate.projectId(this._fileSystem.path());
  54. },
  55. /**
  56. * @return {string}
  57. */
  58. type: function()
  59. {
  60. return WebInspector.projectTypes.FileSystem;
  61. },
  62. /**
  63. * @return {string}
  64. */
  65. fileSystemPath: function()
  66. {
  67. return this._fileSystem.path();
  68. },
  69. /**
  70. * @return {string}
  71. */
  72. displayName: function()
  73. {
  74. return this._fileSystem.path().substr(this._fileSystem.path().lastIndexOf("/") + 1);
  75. },
  76. /**
  77. * @param {Array.<string>} path
  78. * @return {string}
  79. */
  80. _filePathForPath: function(path)
  81. {
  82. return "/" + path.join("/");
  83. },
  84. /**
  85. * @param {Array.<string>} path
  86. * @param {function(?string,boolean,string)} callback
  87. */
  88. requestFileContent: function(path, callback)
  89. {
  90. var filePath = this._filePathForPath(path);
  91. this._fileSystem.requestFileContent(filePath, innerCallback.bind(this));
  92. /**
  93. * @param {?string} content
  94. */
  95. function innerCallback(content)
  96. {
  97. var contentType = this._contentTypeForPath(path);
  98. callback(content, false, contentType.canonicalMimeType());
  99. }
  100. },
  101. /**
  102. * @return {boolean}
  103. */
  104. canSetFileContent: function()
  105. {
  106. return true;
  107. },
  108. /**
  109. * @param {Array.<string>} path
  110. * @param {string} newContent
  111. * @param {function(?string)} callback
  112. */
  113. setFileContent: function(path, newContent, callback)
  114. {
  115. var filePath = this._filePathForPath(path);
  116. this._fileSystem.setFileContent(filePath, newContent, callback.bind(this, ""));
  117. },
  118. /**
  119. * @param {Array.<string>} path
  120. * @param {string} query
  121. * @param {boolean} caseSensitive
  122. * @param {boolean} isRegex
  123. * @param {function(Array.<WebInspector.ContentProvider.SearchMatch>)} callback
  124. */
  125. searchInFileContent: function(path, query, caseSensitive, isRegex, callback)
  126. {
  127. var filePath = this._filePathForPath(path);
  128. this._fileSystem.requestFileContent(filePath, contentCallback.bind(this));
  129. /**
  130. * @param {?string} content
  131. */
  132. function contentCallback(content)
  133. {
  134. var result = [];
  135. if (content !== null)
  136. result = WebInspector.ContentProvider.performSearchInContent(content, query, caseSensitive, isRegex);
  137. callback(result);
  138. }
  139. },
  140. /**
  141. * @param {Array.<string>} path
  142. * @return {WebInspector.ResourceType}
  143. */
  144. _contentTypeForPath: function(path)
  145. {
  146. var fileName = path[path.length - 1];
  147. var extensionIndex = fileName.lastIndexOf(".");
  148. var extension = "";
  149. if (extensionIndex !== -1)
  150. extension = fileName.substring(extensionIndex + 1);
  151. var contentType = WebInspector.resourceTypes.Other;
  152. if (WebInspector.FileSystemProjectDelegate._scriptExtensions[extension])
  153. return WebInspector.resourceTypes.Script;
  154. if (extension === "css")
  155. return WebInspector.resourceTypes.Stylesheet;
  156. if (extension === "html")
  157. return WebInspector.resourceTypes.Document;
  158. return WebInspector.resourceTypes.Other;
  159. },
  160. populate: function()
  161. {
  162. this._fileSystem.requestFilesRecursive("", fileLoaded.bind(this));
  163. function fileLoaded(filePath)
  164. {
  165. var path = filePath.split("/");
  166. path.shift();
  167. console.assert(path.length);
  168. var fullPath = this._fileSystem.path() + filePath;
  169. var url = this._workspace.urlForPath(fullPath);
  170. var contentType = this._contentTypeForPath(path);
  171. var fileDescriptor = new WebInspector.FileDescriptor(path, "file://" + fullPath, url, contentType, true);
  172. this._addFile(fileDescriptor);
  173. }
  174. },
  175. /**
  176. * @param {WebInspector.FileDescriptor} fileDescriptor
  177. */
  178. _addFile: function(fileDescriptor)
  179. {
  180. this.dispatchEventToListeners(WebInspector.ProjectDelegate.Events.FileAdded, fileDescriptor);
  181. },
  182. /**
  183. * @param {Array.<string>} path
  184. */
  185. _removeFile: function(path)
  186. {
  187. this.dispatchEventToListeners(WebInspector.ProjectDelegate.Events.FileRemoved, path);
  188. },
  189. reset: function()
  190. {
  191. this.dispatchEventToListeners(WebInspector.ProjectDelegate.Events.Reset, null);
  192. },
  193. __proto__: WebInspector.Object.prototype
  194. }
  195. /**
  196. * @type {?WebInspector.FileSystemProjectDelegate}
  197. */
  198. WebInspector.fileSystemProjectDelegate = null;
  199. /**
  200. * @constructor
  201. * @param {WebInspector.IsolatedFileSystemManager} isolatedFileSystemManager
  202. * @param {WebInspector.Workspace} workspace
  203. */
  204. WebInspector.FileSystemWorkspaceProvider = function(isolatedFileSystemManager, workspace)
  205. {
  206. this._isolatedFileSystemManager = isolatedFileSystemManager;
  207. this._workspace = workspace;
  208. this._isolatedFileSystemManager.addEventListener(WebInspector.IsolatedFileSystemManager.Events.FileSystemAdded, this._fileSystemAdded, this);
  209. this._isolatedFileSystemManager.addEventListener(WebInspector.IsolatedFileSystemManager.Events.FileSystemRemoved, this._fileSystemRemoved, this);
  210. this._simpleProjectDelegates = {};
  211. }
  212. WebInspector.FileSystemWorkspaceProvider.prototype = {
  213. /**
  214. * @param {WebInspector.Event} event
  215. */
  216. _fileSystemAdded: function(event)
  217. {
  218. var fileSystem = /** @type {WebInspector.IsolatedFileSystem} */ (event.data);
  219. var projectId = WebInspector.FileSystemProjectDelegate.projectId(fileSystem.path());
  220. var projectDelegate = new WebInspector.FileSystemProjectDelegate(fileSystem, this._workspace)
  221. this._simpleProjectDelegates[projectDelegate.id()] = projectDelegate;
  222. console.assert(!this._workspace.project(projectDelegate.id()));
  223. this._workspace.addProject(projectDelegate);
  224. projectDelegate.populate();
  225. },
  226. /**
  227. * @param {WebInspector.Event} event
  228. */
  229. _fileSystemRemoved: function(event)
  230. {
  231. var fileSystem = /** @type {WebInspector.IsolatedFileSystem} */ (event.data);
  232. var projectId = WebInspector.FileSystemProjectDelegate.projectId(fileSystem.path());
  233. this._workspace.removeProject(projectId);
  234. delete this._simpleProjectDelegates[projectId];
  235. },
  236. /**
  237. * @param {WebInspector.UISourceCode} uiSourceCode
  238. */
  239. fileSystemPath: function(uiSourceCode)
  240. {
  241. var projectDelegate = this._simpleProjectDelegates[uiSourceCode.project().id()];
  242. return projectDelegate.fileSystemPath();
  243. }
  244. }
  245. /**
  246. * @type {?WebInspector.FileSystemWorkspaceProvider}
  247. */
  248. WebInspector.fileSystemWorkspaceProvider = null;