text: import*as Common from"../common/common.js";import*as ProtocolClient from"../protocol_client/protocol_client.js";import*as SDK from"../sdk/sdk.js";export class ProtocolService extends Common.ObjectWrapper.ObjectWrapper{constructor(){super(),this._rawConnection=null,this._backend=null,this._backendPromise=null,this._status=null}async attach(){await SDK.SDKModel.TargetManager.instance().suspendAllTargets();const e=SDK.SDKModel.TargetManager.instance().mainTarget().model(SDK.ChildTargetManager.ChildTargetManager);this._rawConnection=await e.createParallelConnection(this._dispatchProtocolMessage.bind(this))}startLighthouse(e,s,t){return this._send("start",{url:e,categoryIDs:s,flags:t})}async detach(){await this._send("stop"),await this._backend.dispose(),delete this._backend,delete this._backendPromise,await this._rawConnection.disconnect(),await SDK.SDKModel.TargetManager.instance().resumeAllTargets()}registerStatusCallback(e){this._status=e}_dispatchProtocolMessage(e){this._send("dispatchProtocolMessage",{message:JSON.stringify(e)})}_initWorker(){this._backendPromise=Services.serviceManager.createAppService("lighthouse_worker","LighthouseService").then(e=>{this._backend||(this._backend=e,this._backend.on("statusUpdate",e=>this._status(e.message)),this._backend.on("sendProtocolMessage",e=>this._sendProtocolMessage(e.message)))})}_sendProtocolMessage(e){this._rawConnection.sendRawMessage(e)}_send(e,s){return this._backendPromise||this._initWorker(),this._backendPromise.then(t=>this._backend.send(e,s))}}