blob: ace514e8b3a781cc67c5c49ad54b9ce91d208190 [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
cjamcl@google.comaa1532c2019-05-31 03:01:24 +00005Audits.ProtocolService = class 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 */
21 attach() {
Dmitry Gozman99d7a6c2018-11-12 17:55:11 +000022 return SDK.interceptMainConnection(this._dispatchProtocolMessage.bind(this)).then(rawConnection => {
Blink Reformat4c46d092018-04-07 15:32:37 +000023 this._rawConnection = rawConnection;
24 });
25 }
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 /**
38 * @return {!Promise<!Object|undefined>}
39 */
40 detach() {
41 return Promise.resolve().then(() => this._send('stop')).then(() => this._backend.dispose()).then(() => {
42 delete this._backend;
43 delete this._backendPromise;
44 return this._rawConnection.disconnect();
45 });
46 }
47
48 /**
49 * @param {function (string): undefined} callback
50 */
51 registerStatusCallback(callback) {
52 this._status = callback;
53 }
54
55 /**
Dmitry Gozman99d7a6c2018-11-12 17:55:11 +000056 * @param {!Object|string} message
Blink Reformat4c46d092018-04-07 15:32:37 +000057 */
58 _dispatchProtocolMessage(message) {
59 this._send('dispatchProtocolMessage', {message: message});
60 }
61
62 _initWorker() {
cjamcl@google.comaa1532c2019-05-31 03:01:24 +000063 this._backendPromise = Services.serviceManager.createAppService('audits_worker', 'AuditsService').then(backend => {
64 if (this._backend)
65 return;
66 this._backend = backend;
67 this._backend.on('statusUpdate', result => this._status(result.message));
68 this._backend.on('sendProtocolMessage', result => this._sendProtocolMessage(result.message));
69 });
Blink Reformat4c46d092018-04-07 15:32:37 +000070 }
71
72 /**
73 * @param {string} message
74 */
75 _sendProtocolMessage(message) {
Alexey Kozyatinskiybd7d3a62018-07-28 02:33:51 +000076 this._rawConnection.sendRawMessage(message);
Blink Reformat4c46d092018-04-07 15:32:37 +000077 }
78
79 /**
80 * @param {string} method
81 * @param {!Object=} params
Paul Irish8f1e33d2018-05-31 02:29:50 +000082 * @return {!Promise<!ReportRenderer.RunnerResult>}
Blink Reformat4c46d092018-04-07 15:32:37 +000083 */
84 _send(method, params) {
85 if (!this._backendPromise)
86 this._initWorker();
87
88 return this._backendPromise.then(_ => this._backend.send(method, params));
89 }
90};