Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(142)

Unified Diff: chromeos/system/statistics_provider.cc

Issue 2218703006: Clean up handling of invalid HWID when running ChromeOS in VMs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix after review comments (no functional changes) Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chromeos/system/statistics_provider.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chromeos/system/statistics_provider.cc
diff --git a/chromeos/system/statistics_provider.cc b/chromeos/system/statistics_provider.cc
index 18c697681aa5e6ff66a2578d14a6db441b2bcc91..15db2c36874de0a1acd8689f6e20312174aead28 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,12 @@ 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 kSystemVendorFile[] = "/sys/class/dmi/id/sys_vendor";
+const char kIsVmCrosSystemKey[] = "inside_vm";
// 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,7 +158,9 @@ const char kFirmwareTypeValueDeveloper[] = "developer";
const char kFirmwareTypeValueNonchrome[] = "nonchrome";
const char kFirmwareTypeValueNormal[] = "normal";
const char kHardwareClassKey[] = "hardware_class";
-const char kSystemVendorKey[] = "system_vendor";
+const char kIsVmKey[] = "is_vm";
+const char kIsVmValueTrue[] = "1";
+const char kIsVmValueFalse[] = "0";
const char kOffersCouponCodeKey[] = "ubind_attribute";
const char kOffersGroupCodeKey[] = "gbind_attribute";
const char kRlzBrandCodeKey[] = "rlz_brand_code";
@@ -192,6 +194,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 +381,13 @@ 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(kIsVmKey, &is_vm) && is_vm == kIsVmValueTrue;
+}
+
StatisticsProviderImpl::StatisticsProviderImpl()
: load_statistics_started_(false),
on_statistics_loaded_(base::WaitableEvent::ResetPolicy::MANUAL,
@@ -447,12 +460,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 +473,22 @@ 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_[kIsVmKey] = kIsVmValueFalse;
+ const auto is_vm_iter = machine_info_.find(kIsVmCrosSystemKey);
+ if (is_vm_iter != machine_info_.end() &&
+ is_vm_iter->second == kIsVmValueTrue) {
+ machine_info_[kIsVmKey] = kIsVmValueTrue;
+ }
+ }
if (load_oem_manifest) {
// If kAppOemManifestFile switch is specified, load OEM Manifest file.
« no previous file with comments | « chromeos/system/statistics_provider.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698