Commit d2955b8a authored by Andrei-Laurențiu Olteanu's avatar Andrei-Laurențiu Olteanu Committed by Commit Bot

[Telemetry SWX] Remove mojo deps from untrusted

Update integration tests of GetAvailableRoutines to make them
compliant.

Change annotation types for Diagnostics and Telemetry to remove mojo
dependency.

Remove mojo dependency from untrusted.js.

Change error handling for Diagnostics and Telemetry.

Bug: b:153371185
Change-Id: Ia1f25b5a141551bcd37f92cc7cc895ab70aa0f8b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2339984Reviewed-by: default avatarOleh Lamzin <lamzin@google.com>
Reviewed-by: default avatarGiovanni Ortuño Urquidi <ortuno@chromium.org>
Commit-Queue: Laurențiu Olteanu <lolteanu@google.com>
Cr-Commit-Position: refs/heads/master@{#796316}
parent 7f3d60c4
...@@ -35,6 +35,7 @@ js_library("trusted") { ...@@ -35,6 +35,7 @@ js_library("trusted") {
sources = [ "trusted.js" ] sources = [ "trusted.js" ]
deps = [ deps = [
":message_types", ":message_types",
"../mojom:mojom_js_library_for_compile",
"//chromeos/components/system_apps/public/js:message_pipe", "//chromeos/components/system_apps/public/js:message_pipe",
] ]
} }
...@@ -48,7 +49,6 @@ js_library("untrusted") { ...@@ -48,7 +49,6 @@ js_library("untrusted") {
} }
js_library("message_types") { js_library("message_types") {
deps = [ "../mojom:mojom_js_library_for_compile" ]
} }
js_library("untrusted_worker") { js_library("untrusted_worker") {
......
...@@ -15,7 +15,7 @@ const dpsl_internal = {}; ...@@ -15,7 +15,7 @@ const dpsl_internal = {};
/** /**
* Enum for message types. * Enum for message types.
* @enum {string} * @enum { string }
*/ */
dpsl_internal.Message = { dpsl_internal.Message = {
DIAGNOSTICS_AVAILABLE_ROUTINES: 'DiagnosticsService.GetAvailableRoutines', DIAGNOSTICS_AVAILABLE_ROUTINES: 'DiagnosticsService.GetAvailableRoutines',
...@@ -25,15 +25,14 @@ dpsl_internal.Message = { ...@@ -25,15 +25,14 @@ dpsl_internal.Message = {
/** /**
* Request message sent by the unprivileged context to request the privileged * Request message sent by the unprivileged context to request the privileged
* context to diagnostics to get available routines. * context to diagnostics to get available routines.
* @typedef {null} * @typedef { null }
*/ */
dpsl_internal.DiagnosticsGetAvailableRoutinesRequest; dpsl_internal.DiagnosticsGetAvailableRoutinesRequest;
/** /**
* Response message sent by the privileged context containing diagnostic * Response message sent by the privileged context containing diagnostic
* routine enums. * routine enums.
* @typedef {{availableRoutines: * @typedef { !Array<!string> }
* !Array<!chromeos.health.mojom.DiagnosticRoutineEnum>}}
*/ */
dpsl_internal.DiagnosticsGetAvailableRoutinesResponse; dpsl_internal.DiagnosticsGetAvailableRoutinesResponse;
...@@ -47,9 +46,6 @@ dpsl_internal.ProbeTelemetryInfoRequest; ...@@ -47,9 +46,6 @@ dpsl_internal.ProbeTelemetryInfoRequest;
/** /**
* Response message sent by the privileged context sending telemetry * Response message sent by the privileged context sending telemetry
* information. * information.
* @typedef {{ * @typedef { !Object|!Error }
* telemetryInfo: (!chromeos.health.mojom.TelemetryInfo|undefined),
* error: (!Error|undefined)
* }}
*/ */
dpsl_internal.ProbeTelemetryInfoResponse; dpsl_internal.ProbeTelemetryInfoResponse;
...@@ -29,6 +29,66 @@ function getOrCreateProbeService() { ...@@ -29,6 +29,66 @@ function getOrCreateProbeService() {
probeService); probeService);
} }
/**
* Proxying diagnostics requests between DiagnosticsRequester on
* chrome-untrusted:// side with WebIDL types and DiagnosticsService on
* chrome:// side with Mojo types.
*/
class DiagnosticsProxy {
constructor() {
const routineEnum = chromeos.health.mojom.DiagnosticRoutineEnum;
/**
* @type { !Map<!chromeos.health.mojom.DiagnosticRoutineEnum, !string> }
* @const
*/
this.routineToEnum_ = new Map([
[routineEnum.kBatteryCapacity, 'battery-capacity'],
[routineEnum.kBatteryHealth, 'battery-health'],
[routineEnum.kUrandom, 'urandom'],
[routineEnum.kSmartctlCheck, 'smartctl-check'],
[routineEnum.kAcPower, 'ac-power'],
[routineEnum.kCpuCache, 'cpu-cache'],
[routineEnum.kCpuStress, 'cpu-stress'],
[routineEnum.kFloatingPointAccuracy, 'floating-point-accuracy'],
[routineEnum.kNvmeWearLevel, 'nvme-wear-level'],
[routineEnum.kNvmeSelfTest, 'nvme-self-test'],
[routineEnum.kDiskRead, 'disk-read'],
[routineEnum.kPrimeSearch, 'prime-search'],
[routineEnum.kBatteryDischarge, 'battery-discharge'],
]);
if (this.routineToEnum_.size != routineEnum.MAX_VALUE + 1) {
throw RangeError('routineToEnum_ does not contain all items from enum!');
}
}
/**
* @param { !Array<!chromeos.health.mojom.DiagnosticRoutineEnum> } routines
* @return { !Array<!string> }
*/
convertRoutines(routines) {
return routines.map((routine) => {
if (!this.routineToEnum_.has(routine)) {
throw TypeError(`Diagnostic routine '${routine}' is unknown.`);
}
return this.routineToEnum_.get(routine);
});
}
/**
* Requests available routines.
* @return { !Promise<dpsl_internal.DiagnosticsGetAvailableRoutinesResponse> }
*/
async handleGetAvailableRoutines() {
const availableRoutines =
await getOrCreateDiagnosticsService().getAvailableRoutines();
return this.convertRoutines(availableRoutines.availableRoutines);
};
};
const diagnosticsProxy = new DiagnosticsProxy();
/** /**
* Proxying telemetry requests between TelemetryRequester on * Proxying telemetry requests between TelemetryRequester on
* chrome-untrusted:// side with WebIDL types and ProbeService on chrome:// * chrome-untrusted:// side with WebIDL types and ProbeService on chrome://
...@@ -52,6 +112,10 @@ class TelemetryProxy { ...@@ -52,6 +112,10 @@ class TelemetryProxy {
['stateful-partition', categoryEnum.kStatefulPartition], ['stateful-partition', categoryEnum.kStatefulPartition],
['bluetooth', categoryEnum.kBluetooth] ['bluetooth', categoryEnum.kBluetooth]
]); ]);
if (this.categoryToEnum_.size != categoryEnum.MAX_VALUE + 1) {
throw RangeError('categoryToEnum_ does not contain all items from enum!');
}
} }
/** /**
...@@ -114,7 +178,7 @@ class TelemetryProxy { ...@@ -114,7 +178,7 @@ class TelemetryProxy {
/** /**
* Requests telemetry info. * Requests telemetry info.
* @param { Object } message * @param { Object } message
* @return { !Object } * @return { !Promise<!dpsl_internal.ProbeTelemetryInfoResponse> }
*/ */
async handleProbeTelemetryInfo(message) { async handleProbeTelemetryInfo(message) {
const request = const request =
...@@ -125,13 +189,13 @@ class TelemetryProxy { ...@@ -125,13 +189,13 @@ class TelemetryProxy {
try { try {
categories = this.convertCategories(request); categories = this.convertCategories(request);
} catch (/** @type {!Error}*/ error) { } catch (/** @type {!Error}*/ error) {
return {error}; return error;
} }
const telemetryInfo = const telemetryInfo =
await getOrCreateProbeService().probeTelemetryInfo(categories); await getOrCreateProbeService().probeTelemetryInfo(categories);
return /** @type {Object} */ (this.convert(telemetryInfo)) || return /** @type {!Object} */ (
{telemetryInfo: {}}; this.convert(telemetryInfo.telemetryInfo) || {});
} }
}; };
...@@ -141,9 +205,8 @@ const untrustedMessagePipe = ...@@ -141,9 +205,8 @@ const untrustedMessagePipe =
new MessagePipe('chrome-untrusted://telemetry-extension'); new MessagePipe('chrome-untrusted://telemetry-extension');
untrustedMessagePipe.registerHandler( untrustedMessagePipe.registerHandler(
dpsl_internal.Message.DIAGNOSTICS_AVAILABLE_ROUTINES, async () => { dpsl_internal.Message.DIAGNOSTICS_AVAILABLE_ROUTINES,
return await getOrCreateDiagnosticsService().getAvailableRoutines(); () => diagnosticsProxy.handleGetAvailableRoutines());
});
untrustedMessagePipe.registerHandler( untrustedMessagePipe.registerHandler(
dpsl_internal.Message.PROBE_TELEMETRY_INFO, dpsl_internal.Message.PROBE_TELEMETRY_INFO,
......
...@@ -7,6 +7,4 @@ ...@@ -7,6 +7,4 @@
<title>Untrusted Telemetry Extension</title> <title>Untrusted Telemetry Extension</title>
<h1 id='untrusted-title'>Telemetry Extension</h1> <h1 id='untrusted-title'>Telemetry Extension</h1>
<script src="mojo_bindings_lite.js"></script>
<script src="diagnostics_service.mojom-lite.js"></script>
<script src="untrusted_scripts.js"></script> <script src="untrusted_scripts.js"></script>
...@@ -16,6 +16,8 @@ ...@@ -16,6 +16,8 @@
* | information. * | information.
*/ */
var chromeos = {};
chromeos.diagnostics = null; chromeos.diagnostics = null;
chromeos.telemetry = null; chromeos.telemetry = null;
...@@ -38,7 +40,7 @@ chromeos.test_support = {}; ...@@ -38,7 +40,7 @@ chromeos.test_support = {};
/** /**
* Requests a list of available routines. * Requests a list of available routines.
* @return {!Promise<!Array<!chromeos.health.mojom.DiagnosticRoutineEnum>>} * @return { !Promise<!Array<!string>> }
* @public * @public
*/ */
async getAvailableRoutines() { async getAvailableRoutines() {
...@@ -46,7 +48,7 @@ chromeos.test_support = {}; ...@@ -46,7 +48,7 @@ chromeos.test_support = {};
/** @type {dpsl_internal.DiagnosticsGetAvailableRoutinesResponse} */ ( /** @type {dpsl_internal.DiagnosticsGetAvailableRoutinesResponse} */ (
await messagePipe.sendMessage( await messagePipe.sendMessage(
dpsl_internal.Message.DIAGNOSTICS_AVAILABLE_ROUTINES)); dpsl_internal.Message.DIAGNOSTICS_AVAILABLE_ROUTINES));
return response.availableRoutines; return response;
} }
}; };
...@@ -59,7 +61,7 @@ chromeos.test_support = {}; ...@@ -59,7 +61,7 @@ chromeos.test_support = {};
/** /**
* Requests telemetry info. * Requests telemetry info.
* @param { !Array<!string> } categories * @param { !Array<!string> } categories
* @return { !Promise<!chromeos.health.mojom.TelemetryInfo> } * @return { !Object }
* @public * @public
*/ */
async probeTelemetryInfo(categories) { async probeTelemetryInfo(categories) {
...@@ -67,11 +69,10 @@ chromeos.test_support = {}; ...@@ -67,11 +69,10 @@ chromeos.test_support = {};
/** @type {dpsl_internal.ProbeTelemetryInfoResponse} */ ( /** @type {dpsl_internal.ProbeTelemetryInfoResponse} */ (
await messagePipe.sendMessage( await messagePipe.sendMessage(
dpsl_internal.Message.PROBE_TELEMETRY_INFO, categories)); dpsl_internal.Message.PROBE_TELEMETRY_INFO, categories));
if (response.error !== undefined) { if (response instanceof Error) {
throw response.error; throw response;
} }
return /** @type {!chromeos.health.mojom.TelemetryInfo} */ ( return response;
response.telemetryInfo);
} }
}; };
......
...@@ -72,11 +72,6 @@ CreateUntrustedTelemetryExtensionDataSource() { ...@@ -72,11 +72,6 @@ CreateUntrustedTelemetryExtensionDataSource() {
"untrusted_scripts.js", IDR_TELEMETRY_EXTENSION_UNTRUSTED_SCRIPTS_JS); "untrusted_scripts.js", IDR_TELEMETRY_EXTENSION_UNTRUSTED_SCRIPTS_JS);
untrusted_source->AddResourcePath( untrusted_source->AddResourcePath(
"untrusted_worker.js", IDR_TELEMETRY_EXTENSION_UNTRUSTED_WORKER_JS); "untrusted_worker.js", IDR_TELEMETRY_EXTENSION_UNTRUSTED_WORKER_JS);
untrusted_source->AddResourcePath("mojo_bindings_lite.js",
IDR_MOJO_MOJO_BINDINGS_LITE_JS);
untrusted_source->AddResourcePath(
"diagnostics_service.mojom-lite.js",
IDR_TELEMETRY_EXTENSION_DIAGNOSTICS_SERVICE_MOJO_LITE_JS);
untrusted_source->OverrideContentSecurityPolicy( untrusted_source->OverrideContentSecurityPolicy(
network::mojom::CSPDirectiveName::FrameAncestors, network::mojom::CSPDirectiveName::FrameAncestors,
......
...@@ -70,22 +70,21 @@ UNTRUSTED_TEST('UntrustedRequestTelemetryInfoUnknownCategory', async () => { ...@@ -70,22 +70,21 @@ UNTRUSTED_TEST('UntrustedRequestTelemetryInfoUnknownCategory', async () => {
// Tests that array of available routines can be successfully // Tests that array of available routines can be successfully
// requested from chrome-untrusted://. // requested from chrome-untrusted://.
UNTRUSTED_TEST('UntrustedRequestAvailableRoutines', async () => { UNTRUSTED_TEST('UntrustedRequestAvailableRoutines', async () => {
/** @type {!Array<!chromeos.health.mojom.DiagnosticRoutineEnum>} */
const response = await chromeos.diagnostics.getAvailableRoutines(); const response = await chromeos.diagnostics.getAvailableRoutines();
assertDeepEquals(response, [ assertDeepEquals(response, [
chromeos.health.mojom.DiagnosticRoutineEnum.kBatteryCapacity, 'battery-capacity',
chromeos.health.mojom.DiagnosticRoutineEnum.kBatteryHealth, 'battery-health',
chromeos.health.mojom.DiagnosticRoutineEnum.kUrandom, 'urandom',
chromeos.health.mojom.DiagnosticRoutineEnum.kSmartctlCheck, 'smartctl-check',
chromeos.health.mojom.DiagnosticRoutineEnum.kAcPower, 'ac-power',
chromeos.health.mojom.DiagnosticRoutineEnum.kCpuCache, 'cpu-cache',
chromeos.health.mojom.DiagnosticRoutineEnum.kCpuStress, 'cpu-stress',
chromeos.health.mojom.DiagnosticRoutineEnum.kFloatingPointAccuracy, 'floating-point-accuracy',
chromeos.health.mojom.DiagnosticRoutineEnum.kNvmeWearLevel, 'nvme-wear-level',
chromeos.health.mojom.DiagnosticRoutineEnum.kNvmeSelfTest, 'nvme-self-test',
chromeos.health.mojom.DiagnosticRoutineEnum.kDiskRead, 'disk-read',
chromeos.health.mojom.DiagnosticRoutineEnum.kPrimeSearch, 'prime-search',
chromeos.health.mojom.DiagnosticRoutineEnum.kBatteryDischarge, 'battery-discharge',
]); ]);
}); });
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment