Files
react-native-devtools-frontend/front_end/lighthouse/LighthouseProtocolService.js
T
Connor Clark 88d9ad2f3b [Lighthouse] Translate the Lighthouse report based on system locale.
Bug: 941561
Change-Id: I8d20f83077f3d0b124e64838fac6bf176c90ac33
Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/2382176
Commit-Queue: Connor Clark <cjamcl@chromium.org>
Reviewed-by: Vidal Diazleal <vidorteg@microsoft.com>
Reviewed-by: Peter Marshall <petermarshall@chromium.org>
Reviewed-by: Paul Irish <paulirish@chromium.org>
2020-09-02 21:52:01 +00:00

108 lines
3.1 KiB
JavaScript

// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// @ts-nocheck
// TODO(crbug.com/1011811): Enable TypeScript compiler checks
import * as Common from '../common/common.js';
import * as ProtocolClient from '../protocol_client/protocol_client.js'; // eslint-disable-line no-unused-vars
import * as SDK from '../sdk/sdk.js';
export class ProtocolService extends Common.ObjectWrapper.ObjectWrapper {
constructor() {
super();
/** @type {?ProtocolClient.InspectorBackend.Connection} */
this._rawConnection = null;
/** @type {?Services.ServiceManager.Service} */
this._backend = null;
/** @type {?Promise} */
this._backendPromise = null;
/** @type {?function(string)} */
this._status = null;
}
/**
* @return {!Promise<undefined>}
*/
async attach() {
await SDK.SDKModel.TargetManager.instance().suspendAllTargets();
const childTargetManager =
SDK.SDKModel.TargetManager.instance().mainTarget().model(SDK.ChildTargetManager.ChildTargetManager);
this._rawConnection = await childTargetManager.createParallelConnection(this._dispatchProtocolMessage.bind(this));
}
getLocales() {
return navigator.languages;
}
/**
* @param {string} auditURL
* @param {!Array<string>} categoryIDs
* @param {!Object} flags
* @return {!Promise<!ReportRenderer.RunnerResult>}
*/
startLighthouse(auditURL, categoryIDs, flags) {
const locales = this.getLocales();
return this._send('start', {url: auditURL, categoryIDs, flags, locales});
}
/**
* @return {!Promise<undefined>}
*/
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();
}
/**
* @param {function (string): undefined} callback
*/
registerStatusCallback(callback) {
this._status = callback;
}
/**
* @param {(!Object|string)} message
*/
_dispatchProtocolMessage(message) {
this._send('dispatchProtocolMessage', {message: JSON.stringify(message)});
}
_initWorker() {
this._backendPromise =
Services.serviceManager.createAppService('lighthouse_worker', 'LighthouseService').then(backend => {
if (this._backend) {
return;
}
this._backend = backend;
this._backend.on('statusUpdate', result => this._status(result.message));
this._backend.on('sendProtocolMessage', result => this._sendProtocolMessage(result.message));
});
}
/**
* @param {string} message
*/
_sendProtocolMessage(message) {
this._rawConnection.sendRawMessage(message);
}
/**
* @param {string} method
* @param {!Object=} params
* @return {!Promise<!ReportRenderer.RunnerResult>}
*/
_send(method, params) {
if (!this._backendPromise) {
this._initWorker();
}
return this._backendPromise.then(_ => this._backend.send(method, params));
}
}