Chromium Code Reviews| Index: chromeos/system/statistics_provider.cc |
| diff --git a/chromeos/system/statistics_provider.cc b/chromeos/system/statistics_provider.cc |
| index 18c697681aa5e6ff66a2578d14a6db441b2bcc91..673c3cf0babc00803f45eb9503fc38f87fbe830b 100644 |
| --- a/chromeos/system/statistics_provider.cc |
| +++ b/chromeos/system/statistics_provider.cc |
| @@ -17,6 +17,7 @@ |
| #include "base/memory/singleton.h" |
| #include "base/path_service.h" |
| #include "base/strings/string_number_conversions.h" |
| +#include "base/strings/string_util.h" |
| #include "base/synchronization/cancellation_flag.h" |
| #include "base/synchronization/waitable_event.h" |
| #include "base/sys_info.h" |
| @@ -41,13 +42,16 @@ const char* kCrosSystemTool[] = { "/usr/bin/crossystem" }; |
| const char kCrosSystemEq[] = "="; |
| const char kCrosSystemDelim[] = "\n"; |
| const char kCrosSystemCommentDelim[] = "#"; |
| -const char kCrosSystemUnknownValue[] = "(error)"; |
| +const char kCrosSystemValueError[] = "(error)"; |
| const char kHardwareClassCrosSystemKey[] = "hwid"; |
| -const char kUnknownHardwareClass[] = "unknown"; |
| +const char kHardwareClassValueUnknown[] = "unknown"; |
| -// File to get system vendor information from. |
| +const char kIsVMCrosSystemKey[] = "inside_vm"; |
| + |
| +// File to get system vendor information from. (x86-only) |
| const char kSystemVendorFile[] = "/sys/class/dmi/id/sys_vendor"; |
| +const char kSystemVendorValueUnknown[] = "unknown"; |
| // Key/value delimiters of machine hardware info file. machine-info is generated |
| // only for OOBE and enterprise enrollment and may not be present. See |
| @@ -158,6 +162,9 @@ const char kFirmwareTypeValueDeveloper[] = "developer"; |
| const char kFirmwareTypeValueNonchrome[] = "nonchrome"; |
| const char kFirmwareTypeValueNormal[] = "normal"; |
| const char kHardwareClassKey[] = "hardware_class"; |
| +const char kIsCrosVMKey[] = "inside_cros_vm"; |
| +const char kIsCrosVMValueTrue[] = "1"; |
| +const char kIsCrosVMValueFalse[] = "0"; |
| const char kSystemVendorKey[] = "system_vendor"; |
| const char kOffersCouponCodeKey[] = "ubind_attribute"; |
| const char kOffersGroupCodeKey[] = "gbind_attribute"; |
| @@ -180,6 +187,17 @@ bool HasOemPrefix(const std::string& name) { |
| return name.substr(0, 4) == "oem_"; |
| } |
| +bool isRunningOnChromeOSVM() { |
| + if (!base::SysInfo::IsRunningOnChromeOS()) |
| + return false; |
| + std::string is_vm; |
| + if (StatisticsProvider::GetInstance()->GetMachineStatistic(kIsCrosVMKey, |
|
Daniel Erat
2016/08/10 13:58:05
nit:
return GetMachineStatistic(kIsCrosVmKey, &
|
| + &is_vm)) { |
| + return is_vm == kIsCrosVMValueTrue; |
| + } |
| + return false; |
| +} |
| + |
| // The StatisticsProvider implementation used in production. |
| class StatisticsProviderImpl : public StatisticsProvider { |
| public: |
| @@ -447,12 +465,6 @@ void StatisticsProviderImpl::LoadMachineStatistics(bool load_oem_manifest) { |
| } |
| } |
| - if (base::SysInfo::IsRunningOnChromeOS()) { |
| - std::string system_vendor; |
| - base::ReadFileToString(base::FilePath(kSystemVendorFile), &system_vendor); |
| - machine_info_[kSystemVendorKey] = system_vendor; |
| - } |
| - |
| parser.GetNameValuePairsFromFile(machine_info_path, |
| kMachineHardwareInfoEq, |
| kMachineHardwareInfoDelim); |
| @@ -466,10 +478,33 @@ void StatisticsProviderImpl::LoadMachineStatistics(bool load_oem_manifest) { |
| // Ensure that the hardware class key is present with the expected |
| // key name, and if it couldn't be retrieved, that the value is "unknown". |
| std::string hardware_class = machine_info_[kHardwareClassCrosSystemKey]; |
| - if (hardware_class.empty() || hardware_class == kCrosSystemUnknownValue) |
| - machine_info_[kHardwareClassKey] = kUnknownHardwareClass; |
| - else |
| + if (hardware_class.empty() || hardware_class == kCrosSystemValueError) { |
| + machine_info_[kHardwareClassKey] = kHardwareClassValueUnknown; |
| + } else { |
| machine_info_[kHardwareClassKey] = hardware_class; |
| + } |
| + |
| + if (base::SysInfo::IsRunningOnChromeOS()) { |
| + // By default, assume that this is *not* a VM. If crossystem is not present, |
| + // report that we are not in a VM. |
| + machine_info_[kIsCrosVMKey] = kIsCrosVMValueFalse; |
| + const auto is_vm_iter = machine_info_.find(kIsVMCrosSystemKey); |
| + if (is_vm_iter != machine_info_.end() && |
| + is_vm_iter->second == kIsCrosVMValueTrue) { |
| + machine_info_[kIsCrosVMKey] = kIsCrosVMValueTrue; |
| + } |
|
Daniel Erat
2016/08/10 13:58:05
nit: add a blank line after this to set the two se
|
| + // Ensure that the system vendor key is present, in particular it is |
| + // potentially useful for VM environments from different vendors. |
| + // On non-x86 architectures the value will be "unknown". |
| + machine_info_[kSystemVendorKey] = kSystemVendorValueUnknown; |
| + std::string system_vendor; |
| + if (base::ReadFileToString(base::FilePath(kSystemVendorFile), |
| + &system_vendor)) { |
| + base::TrimWhitespaceASCII(system_vendor, base::TRIM_ALL, &system_vendor); |
| + if (!system_vendor.empty()) |
| + machine_info_[kSystemVendorKey] = system_vendor; |
| + } |
| + } |
| if (load_oem_manifest) { |
| // If kAppOemManifestFile switch is specified, load OEM Manifest file. |