Commit 666e5eec authored by asvitkine's avatar asvitkine Committed by Commit bot

Implement SysInfo::HardwareModelName() on iOS.

This allows the hardware_class field in UMA to be populated on
iOS (with values like "iPhone9,3"), which will allow us to stop
sending those strings in the cpu_architecture field which is
what's currently being done.

BUG=370104

Review-Url: https://codereview.chromium.org/2860663005
Cr-Commit-Position: refs/heads/master@{#469499}
parent 111449d5
......@@ -49,7 +49,7 @@ bool SysInfo::IsLowEndDevice() {
}
#endif
#if (!defined(OS_MACOSX) || defined(OS_IOS)) && !defined(OS_ANDROID)
#if !defined(OS_MACOSX) && !defined(OS_ANDROID)
std::string SysInfo::HardwareModelName() {
return std::string();
}
......
......@@ -69,8 +69,9 @@ class BASE_EXPORT SysInfo {
// Returns a descriptive string for the current machine model or an empty
// string if the machine model is unknown or an error occured.
// e.g. "MacPro1,1" on Mac, or "Nexus 5" on Android. Only implemented on OS X,
// Android, and Chrome OS. This returns an empty string on other platforms.
// e.g. "MacPro1,1" on Mac, "iPhone9,3" on iOS or "Nexus 5" on Android. Only
// implemented on OS X, iOS, Android, and Chrome OS. This returns an empty
// string on other platforms.
static std::string HardwareModelName();
// Returns the name of the host operating system.
......
......@@ -20,6 +20,23 @@
namespace base {
namespace {
// Queries sysctlbyname() for the given key and returns the value from the
// system or the empty string on failure.
std::string GetSysctlValue(const char* key_name) {
char value[256];
size_t len = arraysize(value);
if (sysctlbyname(key_name, &value, &len, nullptr, 0) == 0) {
DCHECK_GE(len, 1u);
DCHECK_EQ('\0', value[len - 1]);
return std::string(value, len - 1);
}
return std::string();
}
} // namespace
// static
std::string SysInfo::OperatingSystemName() {
static dispatch_once_t get_system_name_once;
......@@ -94,11 +111,20 @@ int64_t SysInfo::AmountOfAvailablePhysicalMemory() {
// static
std::string SysInfo::CPUModelName() {
char name[256];
size_t len = arraysize(name);
if (sysctlbyname("machdep.cpu.brand_string", &name, &len, NULL, 0) == 0)
return name;
return std::string();
return GetSysctlValue("machdep.cpu.brand_string");
}
// static
std::string SysInfo::HardwareModelName() {
#if TARGET_OS_SIMULATOR
// On the simulator, "hw.machine" returns "i386" or "x86_64" which doesn't
// match the expected format, so supply a fake string here.
return "Simulator1,1";
#else
// Note: This uses "hw.machine" instead of "hw.model" like the Mac code,
// because "hw.model" doesn't always return the right string on some devices.
return GetSysctlValue("hw.machine");
#endif
}
} // namespace base
......@@ -24,6 +24,23 @@
namespace base {
namespace {
// Queries sysctlbyname() for the given key and returns the value from the
// system or the empty string on failure.
std::string GetSysctlValue(const char* key_name) {
char value[256];
size_t len = arraysize(value);
if (sysctlbyname(key_name, &value, &len, nullptr, 0) == 0) {
DCHECK_GE(len, 1u);
DCHECK_EQ('\0', value[len - 1]);
return std::string(value, len - 1);
}
return std::string();
}
} // namespace
// static
std::string SysInfo::OperatingSystemName() {
return "Mac OS X";
......@@ -94,19 +111,12 @@ int64_t SysInfo::AmountOfAvailablePhysicalMemory() {
// static
std::string SysInfo::CPUModelName() {
char name[256];
size_t len = arraysize(name);
if (sysctlbyname("machdep.cpu.brand_string", &name, &len, NULL, 0) == 0)
return name;
return std::string();
return GetSysctlValue("machdep.cpu.brand_string");
}
// static
std::string SysInfo::HardwareModelName() {
char model[256];
size_t len = sizeof(model);
if (sysctlbyname("hw.model", model, &len, NULL, 0) == 0)
return std::string(model, 0, len);
return std::string();
return GetSysctlValue("hw.model");
}
} // namespace base
......@@ -7,6 +7,8 @@
#include "base/environment.h"
#include "base/files/file_util.h"
#include "base/process/process_metrics.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/sys_info.h"
#include "base/threading/platform_thread.h"
#include "base/time/time.h"
......@@ -95,10 +97,17 @@ TEST_F(SysInfoTest, Uptime) {
EXPECT_GT(up_time_2.InMicroseconds(), up_time_1.InMicroseconds());
}
#if defined(OS_MACOSX) && !defined(OS_IOS)
TEST_F(SysInfoTest, HardwareModelName) {
#if defined(OS_MACOSX)
TEST_F(SysInfoTest, HardwareModelNameFormatMacAndiOS) {
std::string hardware_model = SysInfo::HardwareModelName();
EXPECT_FALSE(hardware_model.empty());
ASSERT_FALSE(hardware_model.empty());
// Check that the model is of the expected format "Foo,Bar" where "Bar" is
// a number.
std::vector<StringPiece> pieces =
SplitStringPiece(hardware_model, ",", KEEP_WHITESPACE, SPLIT_WANT_ALL);
ASSERT_EQ(2u, pieces.size()) << hardware_model;
int value;
EXPECT_TRUE(StringToInt(pieces[1], &value)) << hardware_model;
}
#endif
......
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