CompilerScriptMapping.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. export default class CompilerScriptMapping{constructor(debuggerModel,workspace,debuggerWorkspaceBinding){this._debuggerModel=debuggerModel;this._sourceMapManager=this._debuggerModel.sourceMapManager();this._workspace=workspace;this._debuggerWorkspaceBinding=debuggerWorkspaceBinding;const target=debuggerModel.target();this._regularProject=new Bindings.ContentProviderBasedProject(workspace,'jsSourceMaps::'+target.id(),Workspace.projectTypes.Network,'',false);this._contentScriptsProject=new Bindings.ContentProviderBasedProject(workspace,'jsSourceMaps:extensions:'+target.id(),Workspace.projectTypes.ContentScripts,'',false);Bindings.NetworkProject.setTargetForProject(this._regularProject,target);Bindings.NetworkProject.setTargetForProject(this._contentScriptsProject,target);this._regularBindings=new Map();this._contentScriptsBindings=new Map();this._stubUISourceCodes=new Map();this._stubProject=new Bindings.ContentProviderBasedProject(workspace,'jsSourceMaps:stub:'+target.id(),Workspace.projectTypes.Service,'',true);this._eventListeners=[this._sourceMapManager.addEventListener(SDK.SourceMapManager.Events.SourceMapWillAttach,this._sourceMapWillAttach,this),this._sourceMapManager.addEventListener(SDK.SourceMapManager.Events.SourceMapFailedToAttach,this._sourceMapFailedToAttach,this),this._sourceMapManager.addEventListener(SDK.SourceMapManager.Events.SourceMapAttached,this._sourceMapAttached,this),this._sourceMapManager.addEventListener(SDK.SourceMapManager.Events.SourceMapDetached,this._sourceMapDetached,this),];}
  2. _addStubUISourceCode(script){const stubUISourceCode=this._stubProject.addContentProvider(script.sourceURL+':sourcemap',Common.StaticContentProvider.fromString(script.sourceURL,Common.resourceTypes.Script,'\n\n\n\n\n// Please wait a bit.\n// Compiled script is not shown while source map is being loaded!'),'text/javascript');this._stubUISourceCodes.set(script,stubUISourceCode);}
  3. _removeStubUISourceCode(script){const uiSourceCode=this._stubUISourceCodes.get(script);this._stubUISourceCodes.delete(script);this._stubProject.removeFile(uiSourceCode.url());this._debuggerWorkspaceBinding.updateLocations(script);}
  4. static uiSourceCodeOrigin(uiSourceCode){const sourceMap=uiSourceCode[_sourceMapSymbol];if(!sourceMap){return null;}
  5. return sourceMap.compiledURL();}
  6. mapsToSourceCode(rawLocation){const script=rawLocation.script();const sourceMap=script?this._sourceMapManager.sourceMapForClient(script):null;if(!sourceMap){return true;}
  7. const entry=sourceMap.findEntry(rawLocation.lineNumber,rawLocation.columnNumber);return!!entry&&entry.lineNumber===rawLocation.lineNumber&&entry.columnNumber===rawLocation.columnNumber;}
  8. uiSourceCodeForURL(url,isContentScript){return isContentScript?this._contentScriptsProject.uiSourceCodeForURL(url):this._regularProject.uiSourceCodeForURL(url);}
  9. rawLocationToUILocation(rawLocation){const script=rawLocation.script();if(!script){return null;}
  10. const lineNumber=rawLocation.lineNumber-script.lineOffset;let columnNumber=rawLocation.columnNumber;if(!lineNumber){columnNumber-=script.columnOffset;}
  11. const stubUISourceCode=this._stubUISourceCodes.get(script);if(stubUISourceCode){return new Workspace.UILocation(stubUISourceCode,lineNumber,columnNumber);}
  12. const sourceMap=this._sourceMapManager.sourceMapForClient(script);if(!sourceMap){return null;}
  13. const entry=sourceMap.findEntry(lineNumber,columnNumber);if(!entry||!entry.sourceURL){return null;}
  14. const uiSourceCode=script.isContentScript()?this._contentScriptsProject.uiSourceCodeForURL(entry.sourceURL):this._regularProject.uiSourceCodeForURL(entry.sourceURL);if(!uiSourceCode){return null;}
  15. return uiSourceCode.uiLocation((entry.sourceLineNumber),(entry.sourceColumnNumber));}
  16. uiLocationToRawLocations(uiSourceCode,lineNumber,columnNumber){const sourceMap=uiSourceCode[_sourceMapSymbol];if(!sourceMap){return[];}
  17. const scripts=this._sourceMapManager.clientsForSourceMap(sourceMap);if(!scripts.length){return[];}
  18. const entry=sourceMap.sourceLineMapping(uiSourceCode.url(),lineNumber,columnNumber);if(!entry){return[];}
  19. return scripts.map(script=>this._debuggerModel.createRawLocation(script,entry.lineNumber+script.lineOffset,!entry.lineNumber?entry.columnNumber+script.columnOffset:entry.columnNumber));}
  20. _sourceMapWillAttach(event){const script=(event.data);this._addStubUISourceCode(script);this._debuggerWorkspaceBinding.updateLocations(script);}
  21. _sourceMapFailedToAttach(event){const script=(event.data);this._removeStubUISourceCode(script);}
  22. _sourceMapAttached(event){const script=(event.data.client);const sourceMap=(event.data.sourceMap);this._removeStubUISourceCode(script);if(Bindings.blackboxManager.isBlackboxedURL(script.sourceURL,script.isContentScript())){this._sourceMapAttachedForTest(sourceMap);return;}
  23. this._populateSourceMapSources(script,sourceMap);this._sourceMapAttachedForTest(sourceMap);}
  24. _sourceMapDetached(event){const script=(event.data.client);const frameId=script[_frameIdSymbol];const sourceMap=(event.data.sourceMap);const bindings=script.isContentScript()?this._contentScriptsBindings:this._regularBindings;for(const sourceURL of sourceMap.sourceURLs()){const binding=bindings.get(sourceURL);if(binding){binding.removeSourceMap(sourceMap,frameId);if(!binding._uiSourceCode){bindings.delete(sourceURL);}}}
  25. this._debuggerWorkspaceBinding.updateLocations(script);}
  26. sourceMapForScript(script){return this._sourceMapManager.sourceMapForClient(script);}
  27. _sourceMapAttachedForTest(sourceMap){}
  28. _populateSourceMapSources(script,sourceMap){const frameId=Bindings.frameIdForScript(script);script[_frameIdSymbol]=frameId;const project=script.isContentScript()?this._contentScriptsProject:this._regularProject;const bindings=script.isContentScript()?this._contentScriptsBindings:this._regularBindings;for(const sourceURL of sourceMap.sourceURLs()){let binding=bindings.get(sourceURL);if(!binding){binding=new Binding(project,sourceURL);bindings.set(sourceURL,binding);}
  29. binding.addSourceMap(sourceMap,frameId);}
  30. this._debuggerWorkspaceBinding.updateLocations(script);}
  31. static uiLineHasMapping(uiSourceCode,lineNumber){const sourceMap=uiSourceCode[_sourceMapSymbol];if(!sourceMap){return true;}
  32. return!!sourceMap.sourceLineMapping(uiSourceCode.url(),lineNumber,0);}
  33. dispose(){Common.EventTarget.removeEventListeners(this._eventListeners);this._regularProject.dispose();this._contentScriptsProject.dispose();this._stubProject.dispose();}}
  34. const _frameIdSymbol=Symbol('_frameIdSymbol');const _sourceMapSymbol=Symbol('_sourceMapSymbol');class Binding{constructor(project,url){this._project=project;this._url=url;this._referringSourceMaps=[];this._activeSourceMap=null;this._uiSourceCode=null;}
  35. _recreateUISourceCodeIfNeeded(frameId){const sourceMap=this._referringSourceMaps.peekLast();if(this._activeSourceMap===sourceMap){return;}
  36. this._activeSourceMap=sourceMap;const newUISourceCode=this._project.createUISourceCode(this._url,Common.resourceTypes.SourceMapScript);newUISourceCode[_sourceMapSymbol]=sourceMap;const contentProvider=sourceMap.sourceContentProvider(this._url,Common.resourceTypes.SourceMapScript);const mimeType=Common.ResourceType.mimeFromURL(this._url)||'text/javascript';const embeddedContent=sourceMap.embeddedContentByURL(this._url);const metadata=typeof embeddedContent==='string'?new Workspace.UISourceCodeMetadata(null,embeddedContent.length):null;if(this._uiSourceCode){Bindings.NetworkProject.cloneInitialFrameAttribution(this._uiSourceCode,newUISourceCode);this._project.removeFile(this._uiSourceCode.url());}else{Bindings.NetworkProject.setInitialFrameAttribution(newUISourceCode,frameId);}
  37. this._uiSourceCode=newUISourceCode;this._project.addUISourceCodeWithProvider(this._uiSourceCode,contentProvider,metadata,mimeType);}
  38. addSourceMap(sourceMap,frameId){if(this._uiSourceCode){Bindings.NetworkProject.addFrameAttribution(this._uiSourceCode,frameId);}
  39. this._referringSourceMaps.push(sourceMap);this._recreateUISourceCodeIfNeeded(frameId);}
  40. removeSourceMap(sourceMap,frameId){Bindings.NetworkProject.removeFrameAttribution((this._uiSourceCode),frameId);const lastIndex=this._referringSourceMaps.lastIndexOf(sourceMap);if(lastIndex!==-1){this._referringSourceMaps.splice(lastIndex,1);}
  41. if(!this._referringSourceMaps.length){this._project.removeFile(this._uiSourceCode.url());this._uiSourceCode=null;}else{this._recreateUISourceCodeIfNeeded(frameId);}}}
  42. self.Bindings=self.Bindings||{};Bindings=Bindings||{};Bindings.CompilerScriptMapping=CompilerScriptMapping;