Commit 56f38832 authored by Oleh Lamzin's avatar Oleh Lamzin Committed by Commit Bot

[Telemetry SWX] Remove ProbeCategoryEnum from untrusted.js

Remove telemetry Mojo category enum dependency from chrome-untrusted://.
Instead we pass string to chrome:// and convert it to Mojo enum in
chrome:// side.

This is the first step to remove Mojo dependency from
chrome-untrusted://.

Bug: 153371185
Change-Id: I49aca7430ed54f11e72ab2a7c24ecdac5935e54b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2332666
Commit-Queue: Oleh Lamzin <lamzin@google.com>
Reviewed-by: default avatarGiovanni Ortuño Urquidi <ortuno@chromium.org>
Reviewed-by: default avatarMahmoud Gawad <mgawad@google.com>
Cr-Commit-Position: refs/heads/master@{#794484}
parent 2e1d4934
......@@ -40,13 +40,16 @@ dpsl_internal.DiagnosticsGetAvailableRoutinesResponse;
/**
* Request message sent by the unprivileged context to request the privileged
* context to probe telemetry information
* @typedef { !Array<!chromeos.health.mojom.ProbeCategoryEnum> }
* @typedef { !Array<!string> }
*/
dpsl_internal.ProbeTelemetryInfoRequest;
/**
* Response message sent by the privileged context sending telemetry
* information.
* @typedef {{ telemetryInfo: !chromeos.health.mojom.TelemetryInfo }}
* @typedef {{
* telemetryInfo: (!chromeos.health.mojom.TelemetryInfo|undefined),
* error: (!Error|undefined)
* }}
*/
dpsl_internal.ProbeTelemetryInfoResponse;
......@@ -35,6 +35,67 @@ document.addEventListener('DOMContentLoaded', async () => {
probeService);
}
/**
* Proxying telemetry requests between TelemetryRequester on
* chrome-untrusted:// side with WebIDL types and ProbeService on chrome://
* side with Mojo types.
*/
class TelemetryProxy {
constructor() {
const categoryEnum = chromeos.health.mojom.ProbeCategoryEnum;
/**
* @type { !Map<!string, !chromeos.health.mojom.ProbeCategoryEnum> }
* @const
*/
this.categoryToEnum_ = new Map([
['battery', categoryEnum.kBattery],
['non-removable-block-devices', categoryEnum.kNonRemovableBlockDevices],
['cached-vpd-data', categoryEnum.kCachedVpdData],
['cpu', categoryEnum.kCpu], ['timezone', categoryEnum.kTimezone],
['memory', categoryEnum.kMemory],
['backlight', categoryEnum.kBacklight], ['fan', categoryEnum.kFan],
['stateful-partition', categoryEnum.kStatefulPartition],
['bluetooth', categoryEnum.kBluetooth]
]);
}
/**
* @param { !Array<!string> } categories
* @return { !Array<!chromeos.health.mojom.ProbeCategoryEnum> }
*/
convertCategories(categories) {
return categories.map((category) => {
if (!this.categoryToEnum_.has(category)) {
throw TypeError(`Telemetry category '${category}' is unknown.`);
}
return this.categoryToEnum_.get(category);
});
}
/**
* Requests telemetry info.
* @param { Object } message
* @return { dpsl_internal.ProbeTelemetryInfoResponse }
*/
async handleProbeTelemetryInfo(message) {
const request =
/** @type {!dpsl_internal.ProbeTelemetryInfoRequest} */ (message);
/** @type {!Array<!chromeos.health.mojom.ProbeCategoryEnum>} */
let categories = [];
try {
categories = telemetryProxy.convertCategories(request);
} catch (/** @type {!Error}*/ error) {
return {error};
}
return await getOrCreateProbeService().probeTelemetryInfo(categories);
}
};
const telemetryProxy = new TelemetryProxy();
const untrustedMessagePipe =
new MessagePipe('chrome-untrusted://telemetry-extension');
......@@ -44,11 +105,8 @@ document.addEventListener('DOMContentLoaded', async () => {
});
untrustedMessagePipe.registerHandler(
dpsl_internal.Message.PROBE_TELEMETRY_INFO, async (message) => {
const request =
/** @type { !dpsl_internal.ProbeTelemetryInfoRequest } */ (message);
return await getOrCreateProbeService().probeTelemetryInfo(request);
});
dpsl_internal.Message.PROBE_TELEMETRY_INFO,
(message) => telemetryProxy.handleProbeTelemetryInfo(message));
globalThis.untrustedMessagePipe = untrustedMessagePipe;
});
......@@ -54,39 +54,7 @@ chromeos.test_support = {};
* DPSL Telemetry Requester.
*/
class TelemetryRequester {
constructor() {
const categoryEnum = chromeos.health.mojom.ProbeCategoryEnum;
/**
* @type { !Map<!string, !chromeos.health.mojom.ProbeCategoryEnum> }
* @const
* @private
*/
this.categoryToEnum_ = new Map([
['battery', categoryEnum.kBattery],
['non-removable-block-devices', categoryEnum.kNonRemovableBlockDevices],
['cached-vpd-data', categoryEnum.kCachedVpdData],
['cpu', categoryEnum.kCpu], ['timezone', categoryEnum.kTimezone],
['memory', categoryEnum.kMemory],
['backlight', categoryEnum.kBacklight], ['fan', categoryEnum.kFan],
['stateful-partition', categoryEnum.kStatefulPartition],
['bluetooth', categoryEnum.kBluetooth]
]);
}
/**
* @param { !Array<!string> } categories
* @return { !Array<!chromeos.health.mojom.ProbeCategoryEnum> }
* @private
*/
convertCategories(categories) {
return categories.map((category) => {
if (!this.categoryToEnum_.has(category)) {
throw TypeError(`Telemetry category '${category}' is unknown.`);
}
return this.categoryToEnum_.get(category);
});
}
constructor() {}
/**
* Requests telemetry info.
......@@ -98,9 +66,12 @@ chromeos.test_support = {};
const response =
/** @type {dpsl_internal.ProbeTelemetryInfoResponse} */ (
await messagePipe.sendMessage(
dpsl_internal.Message.PROBE_TELEMETRY_INFO,
this.convertCategories(categories)));
return response.telemetryInfo;
dpsl_internal.Message.PROBE_TELEMETRY_INFO, categories));
if (response.error !== undefined) {
throw response.error;
}
return /** @type {!chromeos.health.mojom.TelemetryInfo} */ (
response.telemetryInfo);
}
};
......
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