IsolatedFileSystemManager.js 7.6 KB

1234567891011121314151617181920212223242526272829303132333435
  1. export default class IsolatedFileSystemManager extends Common.Object{constructor(){super();this._fileSystems=new Map();this._callbacks=new Map();this._progresses=new Map();Host.InspectorFrontendHost.events.addEventListener(Host.InspectorFrontendHostAPI.Events.FileSystemRemoved,this._onFileSystemRemoved,this);Host.InspectorFrontendHost.events.addEventListener(Host.InspectorFrontendHostAPI.Events.FileSystemAdded,this._onFileSystemAdded,this);Host.InspectorFrontendHost.events.addEventListener(Host.InspectorFrontendHostAPI.Events.FileSystemFilesChangedAddedRemoved,this._onFileSystemFilesChanged,this);Host.InspectorFrontendHost.events.addEventListener(Host.InspectorFrontendHostAPI.Events.IndexingTotalWorkCalculated,this._onIndexingTotalWorkCalculated,this);Host.InspectorFrontendHost.events.addEventListener(Host.InspectorFrontendHostAPI.Events.IndexingWorked,this._onIndexingWorked,this);Host.InspectorFrontendHost.events.addEventListener(Host.InspectorFrontendHostAPI.Events.IndexingDone,this._onIndexingDone,this);Host.InspectorFrontendHost.events.addEventListener(Host.InspectorFrontendHostAPI.Events.SearchCompleted,this._onSearchCompleted,this);this._initExcludePatterSetting();this._fileSystemRequestResolve=null;this._fileSystemsLoadedPromise=this._requestFileSystems();}
  2. _requestFileSystems(){let fulfill;const promise=new Promise(f=>fulfill=f);Host.InspectorFrontendHost.events.addEventListener(Host.InspectorFrontendHostAPI.Events.FileSystemsLoaded,onFileSystemsLoaded,this);Host.InspectorFrontendHost.requestFileSystems();return promise;function onFileSystemsLoaded(event){const fileSystems=(event.data);const promises=[];for(let i=0;i<fileSystems.length;++i){promises.push(this._innerAddFileSystem(fileSystems[i],false));}
  3. Promise.all(promises).then(onFileSystemsAdded);}
  4. function onFileSystemsAdded(fileSystems){fulfill(fileSystems.filter(fs=>!!fs));}}
  5. addFileSystem(type){return new Promise(resolve=>{this._fileSystemRequestResolve=resolve;Host.InspectorFrontendHost.addFileSystem(type||'');});}
  6. removeFileSystem(fileSystem){Host.InspectorFrontendHost.removeFileSystem(fileSystem.embedderPath());}
  7. waitForFileSystems(){return this._fileSystemsLoadedPromise;}
  8. _innerAddFileSystem(fileSystem,dispatchEvent){const embedderPath=fileSystem.fileSystemPath;const fileSystemURL=Common.ParsedURL.platformPathToURL(fileSystem.fileSystemPath);const promise=Persistence.IsolatedFileSystem.create(this,fileSystemURL,embedderPath,fileSystem.type,fileSystem.fileSystemName,fileSystem.rootURL);return promise.then(storeFileSystem.bind(this));function storeFileSystem(fileSystem){if(!fileSystem){return null;}
  9. this._fileSystems.set(fileSystemURL,fileSystem);if(dispatchEvent){this.dispatchEventToListeners(Events.FileSystemAdded,fileSystem);}
  10. return fileSystem;}}
  11. addPlatformFileSystem(fileSystemURL,fileSystem){this._fileSystems.set(fileSystemURL,fileSystem);this.dispatchEventToListeners(Events.FileSystemAdded,fileSystem);}
  12. async _onFileSystemAdded(event){const errorMessage=(event.data['errorMessage']);let fileSystem=(event.data['fileSystem']);if(errorMessage){Common.console.error(Common.UIString('Unable to add filesystem: %s',errorMessage));if(!this._fileSystemRequestResolve){return;}
  13. this._fileSystemRequestResolve.call(null,null);this._fileSystemRequestResolve=null;}else if(fileSystem){fileSystem=await this._innerAddFileSystem(fileSystem,true);if(this._fileSystemRequestResolve){this._fileSystemRequestResolve.call(null,fileSystem);this._fileSystemRequestResolve=null;}}}
  14. _onFileSystemRemoved(event){const embedderPath=(event.data);const fileSystemPath=Common.ParsedURL.platformPathToURL(embedderPath);const isolatedFileSystem=this._fileSystems.get(fileSystemPath);if(!isolatedFileSystem){return;}
  15. this._fileSystems.delete(fileSystemPath);isolatedFileSystem.fileSystemRemoved();this.dispatchEventToListeners(Events.FileSystemRemoved,isolatedFileSystem);}
  16. _onFileSystemFilesChanged(event){const urlPaths={changed:groupFilePathsIntoFileSystemPaths.call(this,event.data.changed),added:groupFilePathsIntoFileSystemPaths.call(this,event.data.added),removed:groupFilePathsIntoFileSystemPaths.call(this,event.data.removed)};this.dispatchEventToListeners(Events.FileSystemFilesChanged,urlPaths);function groupFilePathsIntoFileSystemPaths(embedderPaths){const paths=new Platform.Multimap();for(const embedderPath of embedderPaths){const filePath=Common.ParsedURL.platformPathToURL(embedderPath);for(const fileSystemPath of this._fileSystems.keys()){if(this._fileSystems.get(fileSystemPath).isFileExcluded(embedderPath)){continue;}
  17. const pathPrefix=fileSystemPath.endsWith('/')?fileSystemPath:fileSystemPath+'/';if(!filePath.startsWith(pathPrefix)){continue;}
  18. paths.set(fileSystemPath,filePath);}}
  19. return paths;}}
  20. fileSystems(){return this._fileSystems.valuesArray();}
  21. fileSystem(fileSystemPath){return this._fileSystems.get(fileSystemPath)||null;}
  22. _initExcludePatterSetting(){const defaultCommonExcludedFolders=['/node_modules/','/bower_components/','/\\.devtools','/\\.git/','/\\.sass-cache/','/\\.hg/','/\\.idea/','/\\.svn/','/\\.cache/','/\\.project/'];const defaultWinExcludedFolders=['/Thumbs.db$','/ehthumbs.db$','/Desktop.ini$','/\\$RECYCLE.BIN/'];const defaultMacExcludedFolders=['/\\.DS_Store$','/\\.Trashes$','/\\.Spotlight-V100$','/\\.AppleDouble$','/\\.LSOverride$','/Icon$','/\\._.*$'];const defaultLinuxExcludedFolders=['/.*~$'];let defaultExcludedFolders=defaultCommonExcludedFolders;if(Host.isWin()){defaultExcludedFolders=defaultExcludedFolders.concat(defaultWinExcludedFolders);}else if(Host.isMac()){defaultExcludedFolders=defaultExcludedFolders.concat(defaultMacExcludedFolders);}else{defaultExcludedFolders=defaultExcludedFolders.concat(defaultLinuxExcludedFolders);}
  23. const defaultExcludedFoldersPattern=defaultExcludedFolders.join('|');this._workspaceFolderExcludePatternSetting=Common.settings.createRegExpSetting('workspaceFolderExcludePattern',defaultExcludedFoldersPattern,Host.isWin()?'i':'');}
  24. workspaceFolderExcludePatternSetting(){return this._workspaceFolderExcludePatternSetting;}
  25. registerCallback(callback){const requestId=++_lastRequestId;this._callbacks.set(requestId,callback);return requestId;}
  26. registerProgress(progress){const requestId=++_lastRequestId;this._progresses.set(requestId,progress);return requestId;}
  27. _onIndexingTotalWorkCalculated(event){const requestId=(event.data['requestId']);const totalWork=(event.data['totalWork']);const progress=this._progresses.get(requestId);if(!progress){return;}
  28. progress.setTotalWork(totalWork);}
  29. _onIndexingWorked(event){const requestId=(event.data['requestId']);const worked=(event.data['worked']);const progress=this._progresses.get(requestId);if(!progress){return;}
  30. progress.worked(worked);if(progress.isCanceled()){Host.InspectorFrontendHost.stopIndexing(requestId);this._onIndexingDone(event);}}
  31. _onIndexingDone(event){const requestId=(event.data['requestId']);const progress=this._progresses.get(requestId);if(!progress){return;}
  32. progress.done();this._progresses.delete(requestId);}
  33. _onSearchCompleted(event){const requestId=(event.data['requestId']);const files=(event.data['files']);const callback=this._callbacks.get(requestId);if(!callback){return;}
  34. callback.call(null,files);this._callbacks.delete(requestId);}}
  35. export const Events={FileSystemAdded:Symbol('FileSystemAdded'),FileSystemRemoved:Symbol('FileSystemRemoved'),FileSystemFilesChanged:Symbol('FileSystemFilesChanged'),ExcludedFolderAdded:Symbol('ExcludedFolderAdded'),ExcludedFolderRemoved:Symbol('ExcludedFolderRemoved')};let _lastRequestId=0;self.Persistence=self.Persistence||{};Persistence=Persistence||{};Persistence.IsolatedFileSystemManager=IsolatedFileSystemManager;Persistence.IsolatedFileSystemManager.Events=Events;Persistence.IsolatedFileSystemManager.FileSystem;Persistence.IsolatedFileSystemManager.FilesChangedData;Persistence.isolatedFileSystemManager;