Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 1 | // 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 Lewis | cf2ef22 | 2019-11-22 14:55:35 +0000 | [diff] [blame] | 5 | export class ProtocolService extends Common.Object { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 6 | constructor() { |
| 7 | super(); |
Dmitry Gozman | 99d7a6c | 2018-11-12 17:55:11 +0000 | [diff] [blame] | 8 | /** @type {?Protocol.Connection} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 9 | 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 Clark | ca8905e | 2019-08-23 18:35:10 +0000 | [diff] [blame] | 21 | async attach() { |
Paul Lewis | 4ae5f4f | 2020-01-23 10:19:33 +0000 | [diff] [blame^] | 22 | await self.SDK.targetManager.suspendAllTargets(); |
| 23 | const childTargetManager = self.SDK.targetManager.mainTarget().model(SDK.ChildTargetManager); |
Connor Clark | ca8905e | 2019-08-23 18:35:10 +0000 | [diff] [blame] | 24 | this._rawConnection = await childTargetManager.createParallelConnection(this._dispatchProtocolMessage.bind(this)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | /** |
| 28 | * @param {string} auditURL |
| 29 | * @param {!Array<string>} categoryIDs |
| 30 | * @param {!Object} flags |
Paul Irish | 8f1e33d | 2018-05-31 02:29:50 +0000 | [diff] [blame] | 31 | * @return {!Promise<!ReportRenderer.RunnerResult>} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 32 | */ |
| 33 | startLighthouse(auditURL, categoryIDs, flags) { |
| 34 | return this._send('start', {url: auditURL, categoryIDs, flags}); |
| 35 | } |
| 36 | |
| 37 | /** |
Connor Clark | ca8905e | 2019-08-23 18:35:10 +0000 | [diff] [blame] | 38 | * @return {!Promise<undefined>} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 39 | */ |
Connor Clark | ca8905e | 2019-08-23 18:35:10 +0000 | [diff] [blame] | 40 | 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 Lewis | 4ae5f4f | 2020-01-23 10:19:33 +0000 | [diff] [blame^] | 46 | await self.SDK.targetManager.resumeAllTargets(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | /** |
| 50 | * @param {function (string): undefined} callback |
| 51 | */ |
| 52 | registerStatusCallback(callback) { |
| 53 | this._status = callback; |
| 54 | } |
| 55 | |
| 56 | /** |
Tim van der Lippe | ffa7862 | 2019-09-16 12:07:12 +0000 | [diff] [blame] | 57 | * @param {(!Object|string)} message |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 58 | */ |
| 59 | _dispatchProtocolMessage(message) { |
Connor Clark | ca8905e | 2019-08-23 18:35:10 +0000 | [diff] [blame] | 60 | this._send('dispatchProtocolMessage', {message: JSON.stringify(message)}); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | _initWorker() { |
cjamcl@google.com | aa1532c | 2019-05-31 03:01:24 +0000 | [diff] [blame] | 64 | this._backendPromise = Services.serviceManager.createAppService('audits_worker', 'AuditsService').then(backend => { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 65 | if (this._backend) { |
cjamcl@google.com | aa1532c | 2019-05-31 03:01:24 +0000 | [diff] [blame] | 66 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 67 | } |
cjamcl@google.com | aa1532c | 2019-05-31 03:01:24 +0000 | [diff] [blame] | 68 | this._backend = backend; |
| 69 | this._backend.on('statusUpdate', result => this._status(result.message)); |
| 70 | this._backend.on('sendProtocolMessage', result => this._sendProtocolMessage(result.message)); |
| 71 | }); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @param {string} message |
| 76 | */ |
| 77 | _sendProtocolMessage(message) { |
Alexey Kozyatinskiy | bd7d3a6 | 2018-07-28 02:33:51 +0000 | [diff] [blame] | 78 | this._rawConnection.sendRawMessage(message); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /** |
| 82 | * @param {string} method |
| 83 | * @param {!Object=} params |
Paul Irish | 8f1e33d | 2018-05-31 02:29:50 +0000 | [diff] [blame] | 84 | * @return {!Promise<!ReportRenderer.RunnerResult>} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 85 | */ |
| 86 | _send(method, params) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 87 | if (!this._backendPromise) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 88 | this._initWorker(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 89 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 90 | |
| 91 | return this._backendPromise.then(_ => this._backend.send(method, params)); |
| 92 | } |
Paul Lewis | cf2ef22 | 2019-11-22 14:55:35 +0000 | [diff] [blame] | 93 | } |