export default class DebuggerModel extends SDK.SDKModel{constructor(target){super(target);target.registerDebuggerDispatcher(new DebuggerDispatcher(this));this._agent=target.debuggerAgent();this._runtimeModel=(target.model(SDK.RuntimeModel));this._sourceMapManager=new SDK.SourceMapManager(target);this._sourceMapIdToScript=new Map();this._debuggerPausedDetails=null;this._scripts=new Map();this._scriptsBySourceURL=new Map();this._discardableScripts=[];this._breakpointResolvedEventTarget=new Common.Object();this._autoStepOver=false;this._isPausing=false;Common.moduleSetting('pauseOnExceptionEnabled').addChangeListener(this._pauseOnExceptionStateChanged,this);Common.moduleSetting('pauseOnCaughtException').addChangeListener(this._pauseOnExceptionStateChanged,this);Common.moduleSetting('disableAsyncStackTraces').addChangeListener(this._asyncStackTracesStateChanged,this);Common.moduleSetting('breakpointsActive').addChangeListener(this._breakpointsActiveChanged,this);if(!target.suspended()){this._enableDebugger();} this._stringMap=new Map();this._sourceMapManager.setEnabled(Common.moduleSetting('jsSourceMapsEnabled').get());Common.moduleSetting('jsSourceMapsEnabled').addChangeListener(event=>this._sourceMapManager.setEnabled((event.data)));} static _sourceMapId(executionContextId,sourceURL,sourceMapURL){if(!sourceMapURL){return null;} return executionContextId+':'+sourceURL+':'+sourceMapURL;} sourceMapManager(){return this._sourceMapManager;} runtimeModel(){return this._runtimeModel;} debuggerEnabled(){return!!this._debuggerEnabled;} _enableDebugger(){if(this._debuggerEnabled){return Promise.resolve();} this._debuggerEnabled=true;const isRemoteFrontend=Root.Runtime.queryParam('remoteFrontend')||Root.Runtime.queryParam('ws');const maxScriptsCacheSize=isRemoteFrontend?10e6:100e6;const enablePromise=this._agent.enable(maxScriptsCacheSize);enablePromise.then(this._registerDebugger.bind(this));this._pauseOnExceptionStateChanged();this._asyncStackTracesStateChanged();if(!Common.moduleSetting('breakpointsActive').get()){this._breakpointsActiveChanged();} if(SDK.DebuggerModel._scheduledPauseOnAsyncCall){this._pauseOnAsyncCall(SDK.DebuggerModel._scheduledPauseOnAsyncCall);} this.dispatchEventToListeners(Events.DebuggerWasEnabled,this);return enablePromise;} _registerDebugger(debuggerId){if(!debuggerId){return;} SDK.DebuggerModel._debuggerIdToModel.set(debuggerId,this);this._debuggerId=debuggerId;this.dispatchEventToListeners(Events.DebuggerIsReadyToPause,this);} isReadyToPause(){return!!this._debuggerId;} static modelForDebuggerId(debuggerId){return SDK.DebuggerModel._debuggerIdToModel.get(debuggerId)||null;} _disableDebugger(){if(!this._debuggerEnabled){return Promise.resolve();} this._debuggerEnabled=false;const disablePromise=this._agent.disable();this._isPausing=false;this._asyncStackTracesStateChanged();this.globalObjectCleared();this.dispatchEventToListeners(Events.DebuggerWasDisabled);SDK.DebuggerModel._debuggerIdToModel.delete(this._debuggerId);return disablePromise;} _skipAllPauses(skip){if(this._skipAllPausesTimeout){clearTimeout(this._skipAllPausesTimeout);delete this._skipAllPausesTimeout;} this._agent.setSkipAllPauses(skip);} skipAllPausesUntilReloadOrTimeout(timeout){if(this._skipAllPausesTimeout){clearTimeout(this._skipAllPausesTimeout);} this._agent.setSkipAllPauses(true);this._skipAllPausesTimeout=setTimeout(this._skipAllPauses.bind(this,false),timeout);} _pauseOnExceptionStateChanged(){let state;if(!Common.moduleSetting('pauseOnExceptionEnabled').get()){state=PauseOnExceptionsState.DontPauseOnExceptions;}else if(Common.moduleSetting('pauseOnCaughtException').get()){state=PauseOnExceptionsState.PauseOnAllExceptions;}else{state=PauseOnExceptionsState.PauseOnUncaughtExceptions;} this._agent.setPauseOnExceptions(state);} _asyncStackTracesStateChanged(){const maxAsyncStackChainDepth=32;const enabled=!Common.moduleSetting('disableAsyncStackTraces').get()&&this._debuggerEnabled;this._agent.setAsyncCallStackDepth(enabled?maxAsyncStackChainDepth:0);} _breakpointsActiveChanged(){this._agent.setBreakpointsActive(Common.moduleSetting('breakpointsActive').get());} stepInto(){this._agent.stepInto();} stepOver(){this._autoStepOver=true;this._agent.stepOver();} stepOut(){this._agent.stepOut();} scheduleStepIntoAsync(){this._agent.invoke_stepInto({breakOnAsyncCall:true});} resume(){this._agent.resume();this._isPausing=false;} pause(){this._isPausing=true;this._skipAllPauses(false);this._agent.pause();} _pauseOnAsyncCall(parentStackTraceId){return this._agent.invoke_pauseOnAsyncCall({parentStackTraceId:parentStackTraceId});} async setBreakpointByURL(url,lineNumber,columnNumber,condition){let urlRegex;if(this.target().type()===SDK.Target.Type.Node){const platformPath=Common.ParsedURL.urlToPlatformPath(url,Host.isWin());urlRegex=`${platformPath.escapeForRegExp()}|${url.escapeForRegExp()}`;} let minColumnNumber=0;const scripts=this._scriptsBySourceURL.get(url)||[];for(let i=0,l=scripts.length;iLocation.fromPayload(this,payload));} return{locations:locations,breakpointId:response.breakpointId};} async setBreakpointInAnonymousScript(scriptId,scriptHash,lineNumber,columnNumber,condition){const response=await this._agent.invoke_setBreakpointByUrl({lineNumber:lineNumber,scriptHash:scriptHash,columnNumber:columnNumber,condition:condition});const error=response[Protocol.Error];if(error){if(error!=='Either url or urlRegex must be specified.'){return{locations:[],breakpointId:null};} return this._setBreakpointBySourceId(scriptId,lineNumber,columnNumber,condition);} let locations=[];if(response.locations){locations=response.locations.map(payload=>Location.fromPayload(this,payload));} return{locations:locations,breakpointId:response.breakpointId};} async _setBreakpointBySourceId(scriptId,lineNumber,columnNumber,condition){const response=await this._agent.invoke_setBreakpoint({location:{scriptId:scriptId,lineNumber:lineNumber,columnNumber:columnNumber},condition:condition});if(response[Protocol.Error]){return{breakpointId:null,locations:[]};} let actualLocation=[];if(response.actualLocation){actualLocation=[Location.fromPayload(this,response.actualLocation)];} return{locations:actualLocation,breakpointId:response.breakpointId};} async removeBreakpoint(breakpointId){const response=await this._agent.invoke_removeBreakpoint({breakpointId});if(response[Protocol.Error]){console.error('Failed to remove breakpoint: '+response[Protocol.Error]);}} async getPossibleBreakpoints(startLocation,endLocation,restrictToFunction){const response=await this._agent.invoke_getPossibleBreakpoints({start:startLocation.payload(),end:endLocation?endLocation.payload():undefined,restrictToFunction:restrictToFunction});if(response[Protocol.Error]||!response.locations){return[];} return response.locations.map(location=>BreakLocation.fromPayload(this,location));} async fetchAsyncStackTrace(stackId){const response=await this._agent.invoke_getStackTrace({stackTraceId:stackId});return response[Protocol.Error]?null:response.stackTrace;} _breakpointResolved(breakpointId,location){this._breakpointResolvedEventTarget.dispatchEventToListeners(breakpointId,Location.fromPayload(this,location));} globalObjectCleared(){this._setDebuggerPausedDetails(null);this._reset();this.dispatchEventToListeners(Events.GlobalObjectCleared,this);} _reset(){for(const scriptWithSourceMap of this._sourceMapIdToScript.values()){this._sourceMapManager.detachSourceMap(scriptWithSourceMap);} this._sourceMapIdToScript.clear();this._scripts.clear();this._scriptsBySourceURL.clear();this._stringMap.clear();this._discardableScripts=[];this._autoStepOver=false;} scripts(){return Array.from(this._scripts.values());} scriptForId(scriptId){return this._scripts.get(scriptId)||null;} scriptsForSourceURL(sourceURL){if(!sourceURL){return[];} return this._scriptsBySourceURL.get(sourceURL)||[];} scriptsForExecutionContext(executionContext){const result=[];for(const script of this._scripts.values()){if(script.executionContextId===executionContext.id){result.push(script);}} return result;} setScriptSource(scriptId,newSource,callback){this._scripts.get(scriptId).editSource(newSource,this._didEditScriptSource.bind(this,scriptId,newSource,callback));} _didEditScriptSource(scriptId,newSource,callback,error,exceptionDetails,callFrames,asyncStackTrace,asyncStackTraceId,needsStepIn){callback(error,exceptionDetails);if(needsStepIn){this.stepInto();return;} if(!error&&callFrames&&callFrames.length){this._pausedScript(callFrames,this._debuggerPausedDetails.reason,this._debuggerPausedDetails.auxData,this._debuggerPausedDetails.breakpointIds,asyncStackTrace,asyncStackTraceId);}} get callFrames(){return this._debuggerPausedDetails?this._debuggerPausedDetails.callFrames:null;} debuggerPausedDetails(){return this._debuggerPausedDetails;} _setDebuggerPausedDetails(debuggerPausedDetails){this._isPausing=false;this._debuggerPausedDetails=debuggerPausedDetails;if(this._debuggerPausedDetails){if(this._beforePausedCallback){if(!this._beforePausedCallback.call(null,this._debuggerPausedDetails)){return false;}} this._autoStepOver=false;this.dispatchEventToListeners(Events.DebuggerPaused,this);} if(debuggerPausedDetails){this.setSelectedCallFrame(debuggerPausedDetails.callFrames[0]);}else{this.setSelectedCallFrame(null);} return true;} setBeforePausedCallback(callback){this._beforePausedCallback=callback;} async _pausedScript(callFrames,reason,auxData,breakpointIds,asyncStackTrace,asyncStackTraceId,asyncCallStackTraceId){if(asyncCallStackTraceId){SDK.DebuggerModel._scheduledPauseOnAsyncCall=asyncCallStackTraceId;const promises=[];for(const model of SDK.DebuggerModel._debuggerIdToModel.values()){promises.push(model._pauseOnAsyncCall(asyncCallStackTraceId));} await Promise.all(promises);this.resume();return;} const pausedDetails=new DebuggerPausedDetails(this,callFrames,reason,auxData,breakpointIds,asyncStackTrace,asyncStackTraceId);if(pausedDetails&&this._continueToLocationCallback){const callback=this._continueToLocationCallback;delete this._continueToLocationCallback;if(callback(pausedDetails)){return;}} if(!this._setDebuggerPausedDetails(pausedDetails)){if(this._autoStepOver){this._agent.stepOver();}else{this._agent.stepInto();}} SDK.DebuggerModel._scheduledPauseOnAsyncCall=null;} _resumedScript(){this._setDebuggerPausedDetails(null);this.dispatchEventToListeners(Events.DebuggerResumed,this);} _parsedScriptSource(scriptId,sourceURL,startLine,startColumn,endLine,endColumn,executionContextId,hash,executionContextAuxData,isLiveEdit,sourceMapURL,hasSourceURLComment,hasSyntaxError,length,originStackTrace){if(this._scripts.has(scriptId)){return this._scripts.get(scriptId);} let isContentScript=false;if(executionContextAuxData&&('isDefault'in executionContextAuxData)){isContentScript=!executionContextAuxData['isDefault'];} sourceURL=this._internString(sourceURL);const script=new SDK.Script(this,scriptId,sourceURL,startLine,startColumn,endLine,endColumn,executionContextId,this._internString(hash),isContentScript,isLiveEdit,sourceMapURL,hasSourceURLComment,length,originStackTrace);this._registerScript(script);this.dispatchEventToListeners(Events.ParsedScriptSource,script);const sourceMapId=SDK.DebuggerModel._sourceMapId(script.executionContextId,script.sourceURL,script.sourceMapURL);if(sourceMapId&&!hasSyntaxError){const previousScript=this._sourceMapIdToScript.get(sourceMapId);if(previousScript){this._sourceMapManager.detachSourceMap(previousScript);} this._sourceMapIdToScript.set(sourceMapId,script);this._sourceMapManager.attachSourceMap(script,script.sourceURL,script.sourceMapURL);} const isDiscardable=hasSyntaxError&&script.isAnonymousScript();if(isDiscardable){this._discardableScripts.push(script);this._collectDiscardedScripts();} return script;} setSourceMapURL(script,newSourceMapURL){let sourceMapId=SDK.DebuggerModel._sourceMapId(script.executionContextId,script.sourceURL,script.sourceMapURL);if(sourceMapId&&this._sourceMapIdToScript.get(sourceMapId)===script){this._sourceMapIdToScript.delete(sourceMapId);} this._sourceMapManager.detachSourceMap(script);script.sourceMapURL=newSourceMapURL;sourceMapId=SDK.DebuggerModel._sourceMapId(script.executionContextId,script.sourceURL,script.sourceMapURL);if(!sourceMapId){return;} this._sourceMapIdToScript.set(sourceMapId,script);this._sourceMapManager.attachSourceMap(script,script.sourceURL,script.sourceMapURL);} executionContextDestroyed(executionContext){const sourceMapIds=Array.from(this._sourceMapIdToScript.keys());for(const sourceMapId of sourceMapIds){const script=this._sourceMapIdToScript.get(sourceMapId);if(script.executionContextId===executionContext.id){this._sourceMapIdToScript.delete(sourceMapId);this._sourceMapManager.detachSourceMap(script);}}} _registerScript(script){this._scripts.set(script.scriptId,script);if(script.isAnonymousScript()){return;} let scripts=this._scriptsBySourceURL.get(script.sourceURL);if(!scripts){scripts=[];this._scriptsBySourceURL.set(script.sourceURL,scripts);} scripts.push(script);} _unregisterScript(script){console.assert(script.isAnonymousScript());this._scripts.delete(script.scriptId);} _collectDiscardedScripts(){if(this._discardableScripts.length<1000){return;} const scriptsToDiscard=this._discardableScripts.splice(0,100);for(const script of scriptsToDiscard){this._unregisterScript(script);this.dispatchEventToListeners(Events.DiscardedAnonymousScriptSource,script);}} createRawLocation(script,lineNumber,columnNumber){return new Location(this,script.scriptId,lineNumber,columnNumber);} createRawLocationByURL(sourceURL,lineNumber,columnNumber){let closestScript=null;const scripts=this._scriptsBySourceURL.get(sourceURL)||[];for(let i=0,l=scripts.length;ilineNumber||(script.lineOffset===lineNumber&&script.columnOffset>columnNumber)){continue;} if(script.endLine