Bug 1797587 - Make nsSystemInfo return the CPU model name, r=mhowell,chutten.

Differential Revision: https://phabricator.services.mozilla.com/D160394
This commit is contained in:
Florian Quèze 2022-11-01 20:22:23 +00:00
parent 356fdbcefc
commit 16962cd066
4 changed files with 38 additions and 0 deletions

View File

@ -125,6 +125,8 @@ Structure:
count: <number>, // desktop only, e.g. 8, or null on failure - logical cpus
cores: <number>, // desktop only, e.g., 4, or null on failure - physical cores
vendor: <string>, // desktop only, e.g. "GenuineIntel", or null on failure
name: <string>, // desktop only, e.g. "Intel(R) Core(TM) i9-8950HK CPU @ 2.90GHz",
// or null on failure
family: <number>, // desktop only, null on failure
model: <number, // desktop only, null on failure
stepping: <number>, // desktop only, null on failure

View File

@ -485,6 +485,7 @@ var TelemetryEnvironmentTesting = {
"l3cacheKB",
"speedMHz",
"vendor",
"name",
];
for (let f of EXTRA_CPU_FIELDS) {

View File

@ -596,6 +596,7 @@ static const struct PropItems {
nsresult CollectProcessInfo(ProcessInfo& info) {
nsAutoCString cpuVendor;
nsAutoCString cpuName;
int cpuSpeed = -1;
int cpuFamily = -1;
int cpuModel = -1;
@ -692,6 +693,20 @@ nsresult CollectProcessInfo(ProcessInfo& info) {
CopyUTF16toUTF8(nsDependentString(cpuVendorStr), cpuVendor);
}
// Limit to 64 double byte characters, should be plenty, but create
// a buffer one larger as the result may not be null terminated. If
// it is more than 64, we will not get the value.
// The expected string size is 48 characters or less.
wchar_t cpuNameStr[64 + 1];
len = sizeof(cpuNameStr) - 2;
if (RegQueryValueExW(key, L"ProcessorNameString", 0, &vtype,
reinterpret_cast<LPBYTE>(cpuNameStr),
&len) == ERROR_SUCCESS &&
vtype == REG_SZ && len % 2 == 0 && len > 1) {
cpuNameStr[len / 2] = 0; // In case it isn't null terminated
CopyUTF16toUTF8(nsDependentString(cpuNameStr), cpuName);
}
RegCloseKey(key);
}
@ -749,6 +764,14 @@ nsresult CollectProcessInfo(ProcessInfo& info) {
delete[] cpuVendorStr;
}
if (!sysctlbyname("machdep.cpu.brand_string", NULL, &len, NULL, 0)) {
char* cpuNameStr = new char[len];
if (!sysctlbyname("machdep.cpu.brand_string", cpuNameStr, &len, NULL, 0)) {
cpuName = cpuNameStr;
}
delete[] cpuNameStr;
}
len = sizeof(sysctlValue32);
if (!sysctlbyname("machdep.cpu.family", &sysctlValue32, &len, NULL, 0)) {
cpuFamily = static_cast<int>(sysctlValue32);
@ -777,6 +800,9 @@ nsresult CollectProcessInfo(ProcessInfo& info) {
// cpuVendor from "vendor_id"
info.cpuVendor.Assign(keyValuePairs["vendor_id"_ns]);
// cpuName from "model name"
info.cpuName.Assign(keyValuePairs["model name"_ns]);
{
// cpuFamily from "cpu family"
Tokenizer::Token t;
@ -874,6 +900,9 @@ nsresult CollectProcessInfo(ProcessInfo& info) {
if (!cpuVendor.IsEmpty()) {
info.cpuVendor = cpuVendor;
}
if (!cpuName.IsEmpty()) {
info.cpuName = cpuName;
}
if (cpuFamily >= 0) {
info.cpuFamily = cpuFamily;
}
@ -1329,6 +1358,11 @@ JSObject* GetJSObjForProcessInfo(JSContext* aCx, const ProcessInfo& info) {
JS::Rooted<JS::Value> valVendor(aCx, JS::StringValue(strVendor));
JS_SetProperty(aCx, jsInfo, "vendor", valVendor);
JSString* strName =
JS_NewStringCopyN(aCx, info.cpuName.get(), info.cpuName.Length());
JS::Rooted<JS::Value> valName(aCx, JS::StringValue(strName));
JS_SetProperty(aCx, jsInfo, "name", valName);
JS::Rooted<JS::Value> valFamilyInfo(aCx, JS::Int32Value(info.cpuFamily));
JS_SetProperty(aCx, jsInfo, "family", valFamilyInfo);

View File

@ -57,6 +57,7 @@ struct ProcessInfo {
int32_t cpuCount = 0;
int32_t cpuCores = 0;
nsCString cpuVendor;
nsCString cpuName;
int32_t cpuFamily = 0;
int32_t cpuModel = 0;
int32_t cpuStepping = 0;