AuditsProtocolService.js 1.5 KB

123456789101112
  1. export class ProtocolService extends Common.Object{constructor(){super();this._rawConnection=null;this._backend=null;this._backendPromise=null;this._status=null;}
  2. async attach(){await SDK.targetManager.suspendAllTargets();const childTargetManager=SDK.targetManager.mainTarget().model(SDK.ChildTargetManager);this._rawConnection=await childTargetManager.createParallelConnection(this._dispatchProtocolMessage.bind(this));}
  3. startLighthouse(auditURL,categoryIDs,flags){return this._send('start',{url:auditURL,categoryIDs,flags});}
  4. async detach(){await this._send('stop');await this._backend.dispose();delete this._backend;delete this._backendPromise;await this._rawConnection.disconnect();await SDK.targetManager.resumeAllTargets();}
  5. registerStatusCallback(callback){this._status=callback;}
  6. _dispatchProtocolMessage(message){this._send('dispatchProtocolMessage',{message:JSON.stringify(message)});}
  7. _initWorker(){this._backendPromise=Services.serviceManager.createAppService('audits_worker','AuditsService').then(backend=>{if(this._backend){return;}
  8. this._backend=backend;this._backend.on('statusUpdate',result=>this._status(result.message));this._backend.on('sendProtocolMessage',result=>this._sendProtocolMessage(result.message));});}
  9. _sendProtocolMessage(message){this._rawConnection.sendRawMessage(message);}
  10. _send(method,params){if(!this._backendPromise){this._initWorker();}
  11. return this._backendPromise.then(_=>this._backend.send(method,params));}}
  12. self.Audits=self.Audits||{};Audits=Audits||{};Audits.ProtocolService=ProtocolService;