FileSystemProjectDelegate.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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._normalizedFileSystemPath = this._fileSystem.path();
  41. if (WebInspector.isWin())
  42. this._normalizedFileSystemPath = this._normalizedFileSystemPath.replace(/\\/g, "/");
  43. this._fileSystemURL = "file://" + this._normalizedFileSystemPath + "/";
  44. this._workspace = workspace;
  45. /** @type {Object.<number, function(Array.<string>)>} */
  46. this._searchCallbacks = {};
  47. /** @type {Object.<number, function()>} */
  48. this._indexingCallbacks = {};
  49. /** @type {Object.<number, WebInspector.Progress>} */
  50. this._indexingProgresses = {};
  51. }
  52. WebInspector.FileSystemProjectDelegate._scriptExtensions = ["js", "java", "coffee", "ts", "dart"].keySet();
  53. WebInspector.FileSystemProjectDelegate._styleSheetExtensions = ["css", "scss", "sass", "less"].keySet();
  54. WebInspector.FileSystemProjectDelegate._documentExtensions = ["htm", "html", "asp", "aspx", "phtml", "jsp"].keySet();
  55. WebInspector.FileSystemProjectDelegate.projectId = function(fileSystemPath)
  56. {
  57. return "filesystem:" + fileSystemPath;
  58. }
  59. WebInspector.FileSystemProjectDelegate._lastRequestId = 0;
  60. WebInspector.FileSystemProjectDelegate.prototype = {
  61. /**
  62. * @return {string}
  63. */
  64. id: function()
  65. {
  66. return WebInspector.FileSystemProjectDelegate.projectId(this._fileSystem.path());
  67. },
  68. /**
  69. * @return {string}
  70. */
  71. type: function()
  72. {
  73. return WebInspector.projectTypes.FileSystem;
  74. },
  75. /**
  76. * @return {string}
  77. */
  78. fileSystemPath: function()
  79. {
  80. return this._fileSystem.path();
  81. },
  82. /**
  83. * @return {string}
  84. */
  85. displayName: function()
  86. {
  87. return this._normalizedFileSystemPath.substr(this._normalizedFileSystemPath.lastIndexOf("/") + 1);
  88. },
  89. /**
  90. * @param {string} path
  91. * @return {string}
  92. */
  93. _filePathForPath: function(path)
  94. {
  95. return "/" + path;
  96. },
  97. /**
  98. * @param {string} path
  99. * @param {function(?string,boolean,string)} callback
  100. */
  101. requestFileContent: function(path, callback)
  102. {
  103. var filePath = this._filePathForPath(path);
  104. this._fileSystem.requestFileContent(filePath, innerCallback.bind(this));
  105. /**
  106. * @param {?string} content
  107. */
  108. function innerCallback(content)
  109. {
  110. var extension = this._extensionForPath(path);
  111. var mimeType = WebInspector.ResourceType.mimeTypesForExtensions[extension];
  112. callback(content, false, mimeType);
  113. }
  114. },
  115. /**
  116. * @param {string} path
  117. * @param {function(?Date, ?number)} callback
  118. */
  119. requestMetadata: function(path, callback)
  120. {
  121. var filePath = this._filePathForPath(path);
  122. this._fileSystem.requestMetadata(filePath, callback);
  123. },
  124. /**
  125. * @return {boolean}
  126. */
  127. canSetFileContent: function()
  128. {
  129. return true;
  130. },
  131. /**
  132. * @param {string} path
  133. * @param {string} newContent
  134. * @param {function(?string)} callback
  135. */
  136. setFileContent: function(path, newContent, callback)
  137. {
  138. var filePath = this._filePathForPath(path);
  139. this._fileSystem.setFileContent(filePath, newContent, callback.bind(this, ""));
  140. },
  141. /**
  142. * @return {boolean}
  143. */
  144. canRename: function()
  145. {
  146. return true;
  147. },
  148. /**
  149. * @param {string} path
  150. * @param {string} newName
  151. * @param {function(boolean, string=)} callback
  152. */
  153. rename: function(path, newName, callback)
  154. {
  155. var filePath = this._filePathForPath(path);
  156. this._fileSystem.renameFile(filePath, newName, callback);
  157. },
  158. /**
  159. * @param {string} path
  160. * @param {string} query
  161. * @param {boolean} caseSensitive
  162. * @param {boolean} isRegex
  163. * @param {function(Array.<WebInspector.ContentProvider.SearchMatch>)} callback
  164. */
  165. searchInFileContent: function(path, query, caseSensitive, isRegex, callback)
  166. {
  167. var filePath = this._filePathForPath(path);
  168. this._fileSystem.requestFileContent(filePath, contentCallback.bind(this));
  169. /**
  170. * @param {?string} content
  171. */
  172. function contentCallback(content)
  173. {
  174. var result = [];
  175. if (content !== null)
  176. result = WebInspector.ContentProvider.performSearchInContent(content, query, caseSensitive, isRegex);
  177. callback(result);
  178. }
  179. },
  180. /**
  181. * @param {string} query
  182. * @param {boolean} caseSensitive
  183. * @param {boolean} isRegex
  184. * @param {WebInspector.Progress} progress
  185. * @param {function(StringMap)} callback
  186. */
  187. searchInContent: function(query, caseSensitive, isRegex, progress, callback)
  188. {
  189. var requestId = ++WebInspector.FileSystemProjectDelegate._lastRequestId;
  190. this._searchCallbacks[requestId] = innerCallback.bind(this);
  191. InspectorFrontendHost.searchInPath(requestId, this._fileSystem.path(), isRegex ? "" : query);
  192. function innerCallback(files)
  193. {
  194. function trimAndNormalizeFileSystemPath(fullPath)
  195. {
  196. var trimmedPath = fullPath.substr(this._fileSystem.path().length + 1);
  197. if (WebInspector.isWin())
  198. trimmedPath = trimmedPath.replace(/\\/g, "/");
  199. return trimmedPath;
  200. }
  201. files = files.map(trimAndNormalizeFileSystemPath.bind(this));
  202. var result = new StringMap();
  203. progress.setTotalWork(files.length);
  204. if (files.length === 0) {
  205. progress.done();
  206. callback(result);
  207. return;
  208. }
  209. var fileIndex = 0;
  210. var maxFileContentRequests = 20;
  211. var callbacksLeft = 0;
  212. function searchInNextFiles()
  213. {
  214. for (; callbacksLeft < maxFileContentRequests; ++callbacksLeft) {
  215. if (fileIndex >= files.length)
  216. break;
  217. var path = files[fileIndex++];
  218. var filePath = this._filePathForPath(path);
  219. this._fileSystem.requestFileContent(filePath, contentCallback.bind(this, path));
  220. }
  221. }
  222. searchInNextFiles.call(this);
  223. /**
  224. * @param {string} path
  225. * @param {?string} content
  226. */
  227. function contentCallback(path, content)
  228. {
  229. var matches = [];
  230. if (content !== null)
  231. matches = WebInspector.ContentProvider.performSearchInContent(content, query, caseSensitive, isRegex);
  232. result.put(path, matches);
  233. progress.worked(1);
  234. --callbacksLeft;
  235. if (fileIndex < files.length) {
  236. searchInNextFiles.call(this);
  237. } else {
  238. if (callbacksLeft)
  239. return;
  240. progress.done();
  241. callback(result);
  242. }
  243. }
  244. }
  245. },
  246. /**
  247. * @param {number} requestId
  248. * @param {Array.<string>} files
  249. */
  250. searchCompleted: function(requestId, files)
  251. {
  252. if (!this._searchCallbacks[requestId])
  253. return;
  254. var callback = this._searchCallbacks[requestId];
  255. delete this._searchCallbacks[requestId];
  256. callback(files);
  257. },
  258. /**
  259. * @param {WebInspector.Progress} progress
  260. * @param {function()} callback
  261. */
  262. indexContent: function(progress, callback)
  263. {
  264. var requestId = ++WebInspector.FileSystemProjectDelegate._lastRequestId;
  265. this._indexingCallbacks[requestId] = callback;
  266. this._indexingProgresses[requestId] = progress;
  267. progress.setTotalWork(1);
  268. progress.addEventListener(WebInspector.Progress.Events.Canceled, this._indexingCanceled.bind(this, requestId));
  269. InspectorFrontendHost.indexPath(requestId, this._fileSystem.path());
  270. },
  271. /**
  272. * @param {number} requestId
  273. */
  274. _indexingCanceled: function(requestId)
  275. {
  276. if (!this._indexingProgresses[requestId])
  277. return;
  278. InspectorFrontendHost.stopIndexing(requestId);
  279. delete this._indexingProgresses[requestId];
  280. delete this._indexingCallbacks[requestId];
  281. },
  282. /**
  283. * @param {number} requestId
  284. * @param {number} totalWork
  285. */
  286. indexingTotalWorkCalculated: function(requestId, totalWork)
  287. {
  288. if (!this._indexingProgresses[requestId])
  289. return;
  290. var progress = this._indexingProgresses[requestId];
  291. progress.setTotalWork(totalWork);
  292. },
  293. /**
  294. * @param {number} requestId
  295. * @param {number} worked
  296. */
  297. indexingWorked: function(requestId, worked)
  298. {
  299. if (!this._indexingProgresses[requestId])
  300. return;
  301. var progress = this._indexingProgresses[requestId];
  302. progress.worked(worked);
  303. },
  304. /**
  305. * @param {number} requestId
  306. */
  307. indexingDone: function(requestId)
  308. {
  309. if (!this._indexingProgresses[requestId])
  310. return;
  311. var progress = this._indexingProgresses[requestId];
  312. var callback = this._indexingCallbacks[requestId];
  313. delete this._indexingProgresses[requestId];
  314. delete this._indexingCallbacks[requestId];
  315. progress.done();
  316. callback.call();
  317. },
  318. /**
  319. * @param {string} path
  320. * @return {string}
  321. */
  322. _extensionForPath: function(path)
  323. {
  324. var extensionIndex = path.lastIndexOf(".");
  325. if (extensionIndex === -1)
  326. return "";
  327. return path.substring(extensionIndex + 1).toLowerCase();
  328. },
  329. /**
  330. * @param {string} extension
  331. * @return {WebInspector.ResourceType}
  332. */
  333. _contentTypeForExtension: function(extension)
  334. {
  335. if (WebInspector.FileSystemProjectDelegate._scriptExtensions[extension])
  336. return WebInspector.resourceTypes.Script;
  337. if (WebInspector.FileSystemProjectDelegate._styleSheetExtensions[extension])
  338. return WebInspector.resourceTypes.Stylesheet;
  339. if (WebInspector.FileSystemProjectDelegate._documentExtensions[extension])
  340. return WebInspector.resourceTypes.Document;
  341. return WebInspector.resourceTypes.Other;
  342. },
  343. populate: function()
  344. {
  345. this._fileSystem.requestFilesRecursive("", this._addFile.bind(this));
  346. },
  347. /**
  348. * @param {string} path
  349. */
  350. refresh: function(path)
  351. {
  352. this._fileSystem.requestFilesRecursive(path, this._addFile.bind(this));
  353. },
  354. /**
  355. * @param {string} path
  356. */
  357. excludeFolder: function(path)
  358. {
  359. WebInspector.isolatedFileSystemManager.mapping().addExcludedFolder(this._fileSystem.path(), path);
  360. },
  361. /**
  362. * @param {string} path
  363. * @param {?string} name
  364. * @param {function(?string)} callback
  365. */
  366. createFile: function(path, name, callback)
  367. {
  368. this._fileSystem.createFile(path, name, innerCallback.bind(this));
  369. function innerCallback(filePath)
  370. {
  371. this._addFile(filePath);
  372. callback(filePath);
  373. }
  374. },
  375. /**
  376. * @param {string} path
  377. */
  378. deleteFile: function(path)
  379. {
  380. this._fileSystem.deleteFile(path);
  381. this._removeFile(path);
  382. },
  383. remove: function()
  384. {
  385. WebInspector.isolatedFileSystemManager.removeFileSystem(this._fileSystem.path());
  386. },
  387. /**
  388. * @param {string} filePath
  389. */
  390. _addFile: function(filePath)
  391. {
  392. if (!filePath)
  393. console.assert(false);
  394. var slash = filePath.lastIndexOf("/");
  395. var parentPath = filePath.substring(0, slash);
  396. var name = filePath.substring(slash + 1);
  397. var url = this._workspace.urlForPath(this._fileSystem.path(), filePath);
  398. var extension = this._extensionForPath(name);
  399. var contentType = this._contentTypeForExtension(extension);
  400. var fileDescriptor = new WebInspector.FileDescriptor(parentPath, name, this._fileSystemURL + filePath, url, contentType, true);
  401. this.dispatchEventToListeners(WebInspector.ProjectDelegate.Events.FileAdded, fileDescriptor);
  402. },
  403. /**
  404. * @param {string} path
  405. */
  406. _removeFile: function(path)
  407. {
  408. this.dispatchEventToListeners(WebInspector.ProjectDelegate.Events.FileRemoved, path);
  409. },
  410. reset: function()
  411. {
  412. this.dispatchEventToListeners(WebInspector.ProjectDelegate.Events.Reset, null);
  413. },
  414. __proto__: WebInspector.Object.prototype
  415. }
  416. /**
  417. * @type {?WebInspector.FileSystemProjectDelegate}
  418. */
  419. WebInspector.fileSystemProjectDelegate = null;
  420. /**
  421. * @constructor
  422. * @param {WebInspector.IsolatedFileSystemManager} isolatedFileSystemManager
  423. * @param {WebInspector.Workspace} workspace
  424. */
  425. WebInspector.FileSystemWorkspaceProvider = function(isolatedFileSystemManager, workspace)
  426. {
  427. this._isolatedFileSystemManager = isolatedFileSystemManager;
  428. this._workspace = workspace;
  429. this._isolatedFileSystemManager.addEventListener(WebInspector.IsolatedFileSystemManager.Events.FileSystemAdded, this._fileSystemAdded, this);
  430. this._isolatedFileSystemManager.addEventListener(WebInspector.IsolatedFileSystemManager.Events.FileSystemRemoved, this._fileSystemRemoved, this);
  431. this._projectDelegates = {};
  432. }
  433. WebInspector.FileSystemWorkspaceProvider.prototype = {
  434. /**
  435. * @param {WebInspector.Event} event
  436. */
  437. _fileSystemAdded: function(event)
  438. {
  439. var fileSystem = /** @type {WebInspector.IsolatedFileSystem} */ (event.data);
  440. var projectId = WebInspector.FileSystemProjectDelegate.projectId(fileSystem.path());
  441. var projectDelegate = new WebInspector.FileSystemProjectDelegate(fileSystem, this._workspace)
  442. this._projectDelegates[projectDelegate.id()] = projectDelegate;
  443. console.assert(!this._workspace.project(projectDelegate.id()));
  444. this._workspace.addProject(projectDelegate);
  445. projectDelegate.populate();
  446. },
  447. /**
  448. * @param {WebInspector.Event} event
  449. */
  450. _fileSystemRemoved: function(event)
  451. {
  452. var fileSystem = /** @type {WebInspector.IsolatedFileSystem} */ (event.data);
  453. var projectId = WebInspector.FileSystemProjectDelegate.projectId(fileSystem.path());
  454. this._workspace.removeProject(projectId);
  455. delete this._projectDelegates[projectId];
  456. },
  457. /**
  458. * @param {WebInspector.UISourceCode} uiSourceCode
  459. */
  460. fileSystemPath: function(uiSourceCode)
  461. {
  462. var projectDelegate = this._projectDelegates[uiSourceCode.project().id()];
  463. return projectDelegate.fileSystemPath();
  464. },
  465. /**
  466. * @param {WebInspector.FileSystemProjectDelegate} fileSystemPath
  467. */
  468. delegate: function(fileSystemPath)
  469. {
  470. var projectId = WebInspector.FileSystemProjectDelegate.projectId(fileSystemPath);
  471. return this._projectDelegates[projectId];
  472. }
  473. }
  474. /**
  475. * @type {?WebInspector.FileSystemWorkspaceProvider}
  476. */
  477. WebInspector.fileSystemWorkspaceProvider = null;