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..70d2232f9844581d369bb7f98ea9efc7e339ae22 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"; |
|
Daniel Erat
2016/08/10 16:32:28
this code is only built for chrome os, so includin
norvez
2016/08/11 17:10:34
Done.
|
| +const char kIsCrosVmValueTrue[] = "1"; |
| +const char kIsCrosVmValueFalse[] = "0"; |
| const char kSystemVendorKey[] = "system_vendor"; |
| const char kOffersCouponCodeKey[] = "ubind_attribute"; |
| const char kOffersGroupCodeKey[] = "gbind_attribute"; |
| @@ -192,6 +199,10 @@ class StatisticsProviderImpl : public StatisticsProvider { |
| bool GetMachineFlag(const std::string& name, bool* result) override; |
| void Shutdown() override; |
| + // Returns true when Chrome OS is running in a VM. NOTE: if crossystem is not |
| + // installed it will return false even if Chrome OS is running in a VM. |
| + bool IsRunningOnVm() override; |
| + |
| static StatisticsProviderImpl* GetInstance(); |
| protected: |
| @@ -375,6 +386,14 @@ void StatisticsProviderImpl::Shutdown() { |
| cancellation_flag_.Set(); // Cancel any pending loads |
| } |
| +bool StatisticsProviderImpl::IsRunningOnVm() { |
| + if (!base::SysInfo::IsRunningOnChromeOS()) |
| + return false; |
| + std::string is_vm; |
| + return GetMachineStatistic(kIsCrosVmKey, &is_vm) && |
| + is_vm == kIsCrosVmValueTrue; |
| +} |
| + |
| StatisticsProviderImpl::StatisticsProviderImpl() |
| : load_statistics_started_(false), |
| on_statistics_loaded_(base::WaitableEvent::ResetPolicy::MANUAL, |
| @@ -447,12 +466,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 +479,34 @@ 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; |
| + } |
| + |
| + // 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; |
|
achuithb
2016/08/10 18:24:58
Do we actually care about this?
norvez
2016/08/11 17:10:34
Removed system_vendor altogether
|
| + } |
| + } |
| if (load_oem_manifest) { |
| // If kAppOemManifestFile switch is specified, load OEM Manifest file. |