Skip to content

Commit

Permalink
[Telemetry] Introduce getUsbBusInfo
Browse files Browse the repository at this point in the history
This change adds a `os.telemetry.getUsbBusInfo` method to the
Telemetry Extension API.

Bug: b/264402336
Test: unit_tests --gtest_filter="*Convert*"
Test: browser_tests --gtest_filter="*Telemetry*"
Test: Follow http://go/lacros-test-linux to run tests locally
Test: ./testing/xvfb.py build/lacros/test_runner.py \
        test out/lacros/unit_tests --gtest_filter="*TelemetryExtens*" \
        --ash-chrome-path ./out/lacros/ash_clang_x64/test_ash_chrome
Test: ./testing/xvfb.py build/lacros/test_runner.py \
        test out/lacros/lacros_chrome_browsertests \
        --gtest_filter="*TelemetryExtension*"
        --ash-chrome-path ./out/lacros/ash_clang_x64/test_ash_chrome

Change-Id: Ib2a789bdbf57a997fd94c504690e563f9f03baba
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4163281
Reviewed-by: Giovanni Ortuno Urquidi <ortuno@chromium.org>
Commit-Queue: Bastian Kersting <bkersting@google.com>
Reviewed-by: Oleh Lamzin <lamzin@google.com>
Cr-Commit-Position: refs/heads/main@{#1098511}
  • Loading branch information
1c3t3a authored and Chromium LUCI CQ committed Jan 30, 2023
1 parent 7aab4fa commit 2d9bc58
Show file tree
Hide file tree
Showing 11 changed files with 695 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,15 @@ std::string GetServiceWorkerForError(const std::string& error) {
);
chrome.test.succeed();
},
async function getUsbBusInfo() {
await chrome.test.assertPromiseRejects(
chrome.os.telemetry.getUsbBusInfo(),
'Error: Unauthorized access to ' +
'chrome.os.telemetry.getUsbBusInfo. ' +
'%s'
);
chrome.test.succeed();
},
async function getVpdInfo() {
await chrome.test.assertPromiseRejects(
chrome.os.telemetry.getVpdInfo(),
Expand Down
28 changes: 28 additions & 0 deletions chrome/browser/chromeos/extensions/telemetry/api/telemetry_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,34 @@ void OsTelemetryGetTpmInfoFunction::OnResult(
Respond(ArgumentList(telemetry::GetTpmInfo::Results::Create(result)));
}

// OsTelemetryGetUsbBusInfoFunction --------------------------------------------

void OsTelemetryGetUsbBusInfoFunction::RunIfAllowed() {
auto cb = base::BindOnce(&OsTelemetryGetUsbBusInfoFunction::OnResult, this);

GetRemoteService()->ProbeTelemetryInfo(
{crosapi::mojom::ProbeCategoryEnum::kBus}, std::move(cb));
}

void OsTelemetryGetUsbBusInfoFunction::OnResult(
crosapi::mojom::ProbeTelemetryInfoPtr ptr) {
if (!ptr || !ptr->bus_result || !ptr->bus_result->is_bus_devices_info()) {
Respond(Error("API internal error"));
return;
}

telemetry::UsbBusDevices result;
auto bus_infos = std::move(ptr->bus_result->get_bus_devices_info());
for (auto& info : bus_infos) {
if (info->is_usb_bus_info()) {
result.devices.push_back(converters::ConvertPtr<telemetry::UsbBusInfo>(
std::move(info->get_usb_bus_info())));
}
}

Respond(ArgumentList(telemetry::GetUsbBusInfo::Results::Create(result)));
}

// OsTelemetryGetVpdInfoFunction -----------------------------------------------

void OsTelemetryGetVpdInfoFunction::RunIfAllowed() {
Expand Down
35 changes: 24 additions & 11 deletions chrome/browser/chromeos/extensions/telemetry/api/telemetry_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,37 +152,50 @@ class OsTelemetryGetOsVersionInfoFunction : public TelemetryApiFunctionBase {
void OnResult(crosapi::mojom::ProbeTelemetryInfoPtr ptr);
};

class OsTelemetryGetVpdInfoFunction : public TelemetryApiFunctionBase {
DECLARE_EXTENSION_FUNCTION("os.telemetry.getVpdInfo", OS_TELEMETRY_GETVPDINFO)
class OsTelemetryGetStatefulPartitionInfoFunction
: public TelemetryApiFunctionBase {
DECLARE_EXTENSION_FUNCTION("os.telemetry.getStatefulPartitionInfo",
OS_TELEMETRY_GETSTATEFULPARTITIONINFO)

private:
~OsTelemetryGetVpdInfoFunction() override = default;
~OsTelemetryGetStatefulPartitionInfoFunction() override = default;

// BaseTelemetryExtensionApiGuardFunction:
void RunIfAllowed() override;

void OnResult(crosapi::mojom::ProbeTelemetryInfoPtr ptr);
};

class OsTelemetryGetStatefulPartitionInfoFunction
: public TelemetryApiFunctionBase {
DECLARE_EXTENSION_FUNCTION("os.telemetry.getStatefulPartitionInfo",
OS_TELEMETRY_GETSTATEFULPARTITIONINFO)
class OsTelemetryGetTpmInfoFunction : public TelemetryApiFunctionBase {
DECLARE_EXTENSION_FUNCTION("os.telemetry.getTpmInfo", OS_TELEMETRY_GETTPMINFO)

private:
~OsTelemetryGetStatefulPartitionInfoFunction() override = default;
~OsTelemetryGetTpmInfoFunction() override = default;

// BaseTelemetryExtensionApiGuardFunction:
void RunIfAllowed() override;

void OnResult(crosapi::mojom::ProbeTelemetryInfoPtr ptr);
};

class OsTelemetryGetTpmInfoFunction : public TelemetryApiFunctionBase {
DECLARE_EXTENSION_FUNCTION("os.telemetry.getTpmInfo", OS_TELEMETRY_GETTPMINFO)
class OsTelemetryGetUsbBusInfoFunction : public TelemetryApiFunctionBase {
DECLARE_EXTENSION_FUNCTION("os.telemetry.getUsbBusInfo",
OS_TELEMETRY_GETUSBBUSINFO)

private:
~OsTelemetryGetTpmInfoFunction() override = default;
~OsTelemetryGetUsbBusInfoFunction() override = default;

// BaseTelemetryExtensionApiGuardFunction:
void RunIfAllowed() override;

void OnResult(crosapi::mojom::ProbeTelemetryInfoPtr ptr);
};

class OsTelemetryGetVpdInfoFunction : public TelemetryApiFunctionBase {
DECLARE_EXTENSION_FUNCTION("os.telemetry.getVpdInfo", OS_TELEMETRY_GETVPDINFO)

private:
~OsTelemetryGetVpdInfoFunction() override = default;

// BaseTelemetryExtensionApiGuardFunction:
void RunIfAllowed() override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1411,6 +1411,30 @@ IN_PROC_BROWSER_TEST_F(TelemetryExtensionTelemetryApiBrowserTest,
)");
}

IN_PROC_BROWSER_TEST_F(TelemetryExtensionTelemetryApiBrowserTest,
GetUsbBusInfo_NoFeatureFlagEnabledError) {
#if BUILDFLAG(IS_CHROMEOS_LACROS)
// If Probe interface is not available on this version of ash-chrome, this
// test suite will no-op.
if (!IsServiceAvailable()) {
return;
}
#endif // BUILDFLAG(IS_CHROMEOS_LACROS)

// If the permission is not enabled, the method isn't defined
// on `chrome.os.telemetry`.
CreateExtensionAndRunServiceWorker(R"(
chrome.test.runTests([
function getUsbBusInfo() {
chrome.test.assertThrows(() => { chrome.os.telemetry.getUsbBusInfo(); },
[], "chrome.os.telemetry.getUsbBusInfo is not a function");
chrome.test.succeed();
}
]);
)");
}

class TelemetryExtensionTelemetryApiWithoutAdditionalPermissionsBrowserTest
: public TelemetryExtensionTelemetryApiBrowserTest {
public:
Expand Down Expand Up @@ -1673,4 +1697,140 @@ IN_PROC_BROWSER_TEST_F(
)");
}

class PendingApprovalTelemetryExtensionTelemetryApiBrowserTest
: public TelemetryExtensionTelemetryApiBrowserTest {
public:
PendingApprovalTelemetryExtensionTelemetryApiBrowserTest() {
feature_list_.InitAndEnableFeature(
extensions_features::kTelemetryExtensionPendingApprovalApi);
}

private:
base::test::ScopedFeatureList feature_list_;
};

IN_PROC_BROWSER_TEST_F(PendingApprovalTelemetryExtensionTelemetryApiBrowserTest,
GetUsbBusInfo_Error) {
#if BUILDFLAG(IS_CHROMEOS_LACROS)
// If Probe interface is not available on this version of ash-chrome, this
// test suite will no-op.
if (!IsServiceAvailable()) {
return;
}
#endif // BUILDFLAG(IS_CHROMEOS_LACROS)

// Configure FakeProbeService.
{
auto fake_service_impl = std::make_unique<FakeProbeService>();
fake_service_impl->SetExpectedLastRequestedCategories(
{crosapi::mojom::ProbeCategoryEnum::kBus});

SetServiceForTesting(std::move(fake_service_impl));
}

CreateExtensionAndRunServiceWorker(R"(
chrome.test.runTests([
async function getUsbBusInfo() {
await chrome.test.assertPromiseRejects(
chrome.os.telemetry.getUsbBusInfo(),
'Error: API internal error'
);
chrome.test.succeed();
}
]);
)");
}

IN_PROC_BROWSER_TEST_F(PendingApprovalTelemetryExtensionTelemetryApiBrowserTest,
GetUsbBusInfo_Success) {
#if BUILDFLAG(IS_CHROMEOS_LACROS)
// If Probe interface is not available on this version of ash-chrome, this
// test suite will no-op.
if (!IsServiceAvailable()) {
return;
}
#endif // BUILDFLAG(IS_CHROMEOS_LACROS)

// Configure FakeProbeService.
{
auto telemetry_info = crosapi::mojom::ProbeTelemetryInfo::New();
{
std::vector<crosapi::mojom::ProbeUsbBusInterfaceInfoPtr> interfaces;
interfaces.push_back(crosapi::mojom::ProbeUsbBusInterfaceInfo::New(
crosapi::mojom::UInt8Value::New(42),
crosapi::mojom::UInt8Value::New(41),
crosapi::mojom::UInt8Value::New(43),
crosapi::mojom::UInt8Value::New(44), "MyDriver"));

auto fwupd_version = crosapi::mojom::ProbeFwupdFirmwareVersionInfo::New(
"MyVersion", crosapi::mojom::ProbeFwupdVersionFormat::kPair);

auto usb_bus_info = crosapi::mojom::ProbeUsbBusInfo::New();
usb_bus_info->class_id = crosapi::mojom::UInt8Value::New(45);
usb_bus_info->subclass_id = crosapi::mojom::UInt8Value::New(46);
usb_bus_info->protocol_id = crosapi::mojom::UInt8Value::New(47);
usb_bus_info->vendor_id = crosapi::mojom::UInt16Value::New(48);
usb_bus_info->product_id = crosapi::mojom::UInt16Value::New(49);
usb_bus_info->interfaces = std::move(interfaces);
usb_bus_info->fwupd_firmware_version_info = std::move(fwupd_version);
usb_bus_info->version = crosapi::mojom::ProbeUsbVersion::kUsb3;
usb_bus_info->spec_speed = crosapi::mojom::ProbeUsbSpecSpeed::k20Gbps;

auto bus_info =
crosapi::mojom::ProbeBusInfo::NewUsbBusInfo(std::move(usb_bus_info));

std::vector<crosapi::mojom::ProbeBusInfoPtr> input;
input.push_back(std::move(bus_info));

telemetry_info->bus_result =
crosapi::mojom::ProbeBusResult::NewBusDevicesInfo(std::move(input));
}

auto fake_service_impl = std::make_unique<FakeProbeService>();
fake_service_impl->SetProbeTelemetryInfoResponse(std::move(telemetry_info));
fake_service_impl->SetExpectedLastRequestedCategories(
{crosapi::mojom::ProbeCategoryEnum::kBus});

SetServiceForTesting(std::move(fake_service_impl));
}

CreateExtensionAndRunServiceWorker(R"(
chrome.test.runTests([
async function getUsbBusInfo() {
const result = await chrome.os.telemetry.getUsbBusInfo();
chrome.test.assertEq(
// The dictionary members are ordered lexicographically by the Unicode
// codepoints that comprise their identifiers.
{
"devices": [
{
"classId": 45,
"fwupdFirmwareVersionInfo": {
"version": "MyVersion",
"version_format":"pair"
},
"interfaces": [
{
"classId": 41,
"driver": "MyDriver",
"interfaceNumber": 42,
"protocolId": 44,
"subclassId": 43
}
],
"productId": 49,
"protocolId": 47,
"spec_speed": "n20Gbps",
"subclassId": 46,
"vendorId": 48,
"version": "usb3"
}
]
}, result);
chrome.test.succeed();
}
]);
)");
}

} // namespace chromeos

0 comments on commit 2d9bc58

Please sign in to comment.