blob: 4290ede73a21b497ab310471fb64b1fcd85dfcad [file] [log] [blame]
Blink Reformat4c46d092018-04-07 15:32:37 +00001// Copyright 2018 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Paul Lewiscf2ef222019-11-22 14:55:35 +00005export class ProtocolService extends Common.Object {
Blink Reformat4c46d092018-04-07 15:32:37 +00006 constructor() {
7 super();
Dmitry Gozman99d7a6c2018-11-12 17:55:11 +00008 /** @type {?Protocol.Connection} */
Blink Reformat4c46d092018-04-07 15:32:37 +00009 this._rawConnection = null;
10 /** @type {?Services.ServiceManager.Service} */
11 this._backend = null;
12 /** @type {?Promise} */
13 this._backendPromise = null;
14 /** @type {?function(string)} */
15 this._status = null;
16 }
17
18 /**
19 * @return {!Promise<undefined>}
20 */
Connor Clarkca8905e2019-08-23 18:35:10 +000021 async attach() {
Paul Lewis4ae5f4f2020-01-23 10:19:33 +000022 await self.SDK.targetManager.suspendAllTargets();
23 const childTargetManager = self.SDK.targetManager.mainTarget().model(SDK.ChildTargetManager);
Connor Clarkca8905e2019-08-23 18:35:10 +000024 this._rawConnection = await childTargetManager.createParallelConnection(this._dispatchProtocolMessage.bind(this));
Blink Reformat4c46d092018-04-07 15:32:37 +000025 }
26
27 /**
28 * @param {string} auditURL
29 * @param {!Array<string>} categoryIDs
30 * @param {!Object} flags
Paul Irish8f1e33d2018-05-31 02:29:50 +000031 * @return {!Promise<!ReportRenderer.RunnerResult>}
Blink Reformat4c46d092018-04-07 15:32:37 +000032 */
33 startLighthouse(auditURL, categoryIDs, flags) {
34 return this._send('start', {url: auditURL, categoryIDs, flags});
35 }
36
37 /**
Connor Clarkca8905e2019-08-23 18:35:10 +000038 * @return {!Promise<undefined>}
Blink Reformat4c46d092018-04-07 15:32:37 +000039 */
Connor Clarkca8905e2019-08-23 18:35:10 +000040 async detach() {
41 await this._send('stop');
42 await this._backend.dispose();
43 delete this._backend;
44 delete this._backendPromise;
45 await this._rawConnection.disconnect();
Paul Lewis4ae5f4f2020-01-23 10:19:33 +000046 await self.SDK.targetManager.resumeAllTargets();
Blink Reformat4c46d092018-04-07 15:32:37 +000047 }
48
49 /**
50 * @param {function (string): undefined} callback
51 */
52 registerStatusCallback(callback) {
53 this._status = callback;
54 }
55
56 /**
Tim van der Lippeffa78622019-09-16 12:07:12 +000057 * @param {(!Object|string)} message
Blink Reformat4c46d092018-04-07 15:32:37 +000058 */
59 _dispatchProtocolMessage(message) {
Connor Clarkca8905e2019-08-23 18:35:10 +000060 this._send('dispatchProtocolMessage', {message: JSON.stringify(message)});
Blink Reformat4c46d092018-04-07 15:32:37 +000061 }
62
63 _initWorker() {
Connor Clark2bc3be22020-02-14 14:34:19 -080064 this._backendPromise =
65 Services.serviceManager.createAppService('lighthouse_worker', 'LighthouseService').then(backend => {
66 if (this._backend) {
67 return;
68 }
69 this._backend = backend;
70 this._backend.on('statusUpdate', result => this._status(result.message));
71 this._backend.on('sendProtocolMessage', result => this._sendProtocolMessage(result.message));
72 });
Blink Reformat4c46d092018-04-07 15:32:37 +000073 }
74
75 /**
76 * @param {string} message
77 */
78 _sendProtocolMessage(message) {
Alexey Kozyatinskiybd7d3a62018-07-28 02:33:51 +000079 this._rawConnection.sendRawMessage(message);
Blink Reformat4c46d092018-04-07 15:32:37 +000080 }
81
82 /**
83 * @param {string} method
84 * @param {!Object=} params
Paul Irish8f1e33d2018-05-31 02:29:50 +000085 * @return {!Promise<!ReportRenderer.RunnerResult>}
Blink Reformat4c46d092018-04-07 15:32:37 +000086 */
87 _send(method, params) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000088 if (!this._backendPromise) {
Blink Reformat4c46d092018-04-07 15:32:37 +000089 this._initWorker();
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000090 }
Blink Reformat4c46d092018-04-07 15:32:37 +000091
92 return this._backendPromise.then(_ => this._backend.send(method, params));
93 }
Paul Lewiscf2ef222019-11-22 14:55:35 +000094}