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

Side by Side 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 unified diff | Download patch
« no previous file with comments | « chromeos/system/statistics_provider.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chromeos/system/statistics_provider.h" 5 #include "chromeos/system/statistics_provider.h"
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/command_line.h" 10 #include "base/command_line.h"
11 #include "base/files/file_path.h" 11 #include "base/files/file_path.h"
12 #include "base/files/file_util.h" 12 #include "base/files/file_util.h"
13 #include "base/json/json_file_value_serializer.h" 13 #include "base/json/json_file_value_serializer.h"
14 #include "base/location.h" 14 #include "base/location.h"
15 #include "base/logging.h" 15 #include "base/logging.h"
16 #include "base/macros.h" 16 #include "base/macros.h"
17 #include "base/memory/singleton.h" 17 #include "base/memory/singleton.h"
18 #include "base/path_service.h" 18 #include "base/path_service.h"
19 #include "base/strings/string_number_conversions.h" 19 #include "base/strings/string_number_conversions.h"
20 #include "base/strings/string_util.h"
20 #include "base/synchronization/cancellation_flag.h" 21 #include "base/synchronization/cancellation_flag.h"
21 #include "base/synchronization/waitable_event.h" 22 #include "base/synchronization/waitable_event.h"
22 #include "base/sys_info.h" 23 #include "base/sys_info.h"
23 #include "base/task_runner.h" 24 #include "base/task_runner.h"
24 #include "base/threading/thread_restrictions.h" 25 #include "base/threading/thread_restrictions.h"
25 #include "base/time/time.h" 26 #include "base/time/time.h"
26 #include "base/values.h" 27 #include "base/values.h"
27 #include "chromeos/app_mode/kiosk_oem_manifest_parser.h" 28 #include "chromeos/app_mode/kiosk_oem_manifest_parser.h"
28 #include "chromeos/chromeos_constants.h" 29 #include "chromeos/chromeos_constants.h"
29 #include "chromeos/chromeos_paths.h" 30 #include "chromeos/chromeos_paths.h"
30 #include "chromeos/chromeos_switches.h" 31 #include "chromeos/chromeos_switches.h"
31 #include "chromeos/system/name_value_pairs_parser.h" 32 #include "chromeos/system/name_value_pairs_parser.h"
32 33
33 namespace chromeos { 34 namespace chromeos {
34 namespace system { 35 namespace system {
35 36
36 namespace { 37 namespace {
37 38
38 // Path to the tool used to get system info, and delimiters for the output 39 // Path to the tool used to get system info, and delimiters for the output
39 // format of the tool. 40 // format of the tool.
40 const char* kCrosSystemTool[] = { "/usr/bin/crossystem" }; 41 const char* kCrosSystemTool[] = { "/usr/bin/crossystem" };
41 const char kCrosSystemEq[] = "="; 42 const char kCrosSystemEq[] = "=";
42 const char kCrosSystemDelim[] = "\n"; 43 const char kCrosSystemDelim[] = "\n";
43 const char kCrosSystemCommentDelim[] = "#"; 44 const char kCrosSystemCommentDelim[] = "#";
44 const char kCrosSystemUnknownValue[] = "(error)"; 45 const char kCrosSystemValueError[] = "(error)";
45 46
46 const char kHardwareClassCrosSystemKey[] = "hwid"; 47 const char kHardwareClassCrosSystemKey[] = "hwid";
47 const char kUnknownHardwareClass[] = "unknown"; 48 const char kHardwareClassValueUnknown[] = "unknown";
48 49
49 // File to get system vendor information from. 50 const char kIsVmCrosSystemKey[] = "inside_vm";
50 const char kSystemVendorFile[] = "/sys/class/dmi/id/sys_vendor";
51 51
52 // Key/value delimiters of machine hardware info file. machine-info is generated 52 // Key/value delimiters of machine hardware info file. machine-info is generated
53 // only for OOBE and enterprise enrollment and may not be present. See 53 // only for OOBE and enterprise enrollment and may not be present. See
54 // login-manager/init/machine-info.conf. 54 // login-manager/init/machine-info.conf.
55 const char kMachineHardwareInfoEq[] = "="; 55 const char kMachineHardwareInfoEq[] = "=";
56 const char kMachineHardwareInfoDelim[] = " \n"; 56 const char kMachineHardwareInfoDelim[] = " \n";
57 57
58 // File to get ECHO coupon info from, and key/value delimiters of 58 // File to get ECHO coupon info from, and key/value delimiters of
59 // the file. 59 // the file.
60 const char kEchoCouponFile[] = "/var/cache/echo/vpd_echo.txt"; 60 const char kEchoCouponFile[] = "/var/cache/echo/vpd_echo.txt";
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 const char kCheckEnrollmentKey[] = "check_enrollment"; 151 const char kCheckEnrollmentKey[] = "check_enrollment";
152 const char kCustomizationIdKey[] = "customization_id"; 152 const char kCustomizationIdKey[] = "customization_id";
153 const char kDevSwitchBootKey[] = "devsw_boot"; 153 const char kDevSwitchBootKey[] = "devsw_boot";
154 const char kDevSwitchBootValueDev[] = "1"; 154 const char kDevSwitchBootValueDev[] = "1";
155 const char kDevSwitchBootValueVerified[] = "0"; 155 const char kDevSwitchBootValueVerified[] = "0";
156 const char kFirmwareTypeKey[] = "mainfw_type"; 156 const char kFirmwareTypeKey[] = "mainfw_type";
157 const char kFirmwareTypeValueDeveloper[] = "developer"; 157 const char kFirmwareTypeValueDeveloper[] = "developer";
158 const char kFirmwareTypeValueNonchrome[] = "nonchrome"; 158 const char kFirmwareTypeValueNonchrome[] = "nonchrome";
159 const char kFirmwareTypeValueNormal[] = "normal"; 159 const char kFirmwareTypeValueNormal[] = "normal";
160 const char kHardwareClassKey[] = "hardware_class"; 160 const char kHardwareClassKey[] = "hardware_class";
161 const char kSystemVendorKey[] = "system_vendor"; 161 const char kIsVmKey[] = "is_vm";
162 const char kIsVmValueTrue[] = "1";
163 const char kIsVmValueFalse[] = "0";
162 const char kOffersCouponCodeKey[] = "ubind_attribute"; 164 const char kOffersCouponCodeKey[] = "ubind_attribute";
163 const char kOffersGroupCodeKey[] = "gbind_attribute"; 165 const char kOffersGroupCodeKey[] = "gbind_attribute";
164 const char kRlzBrandCodeKey[] = "rlz_brand_code"; 166 const char kRlzBrandCodeKey[] = "rlz_brand_code";
165 const char kWriteProtectSwitchBootKey[] = "wpsw_boot"; 167 const char kWriteProtectSwitchBootKey[] = "wpsw_boot";
166 const char kWriteProtectSwitchBootValueOff[] = "0"; 168 const char kWriteProtectSwitchBootValueOff[] = "0";
167 const char kWriteProtectSwitchBootValueOn[] = "1"; 169 const char kWriteProtectSwitchBootValueOn[] = "1";
168 const char kRegionKey[] = "region"; 170 const char kRegionKey[] = "region";
169 const char kInitialLocaleKey[] = "initial_locale"; 171 const char kInitialLocaleKey[] = "initial_locale";
170 const char kInitialTimezoneKey[] = "initial_timezone"; 172 const char kInitialTimezoneKey[] = "initial_timezone";
171 const char kKeyboardLayoutKey[] = "keyboard_layout"; 173 const char kKeyboardLayoutKey[] = "keyboard_layout";
(...skipping 13 matching lines...) Expand all
185 public: 187 public:
186 // StatisticsProvider implementation: 188 // StatisticsProvider implementation:
187 void StartLoadingMachineStatistics( 189 void StartLoadingMachineStatistics(
188 const scoped_refptr<base::TaskRunner>& file_task_runner, 190 const scoped_refptr<base::TaskRunner>& file_task_runner,
189 bool load_oem_manifest) override; 191 bool load_oem_manifest) override;
190 bool GetMachineStatistic(const std::string& name, 192 bool GetMachineStatistic(const std::string& name,
191 std::string* result) override; 193 std::string* result) override;
192 bool GetMachineFlag(const std::string& name, bool* result) override; 194 bool GetMachineFlag(const std::string& name, bool* result) override;
193 void Shutdown() override; 195 void Shutdown() override;
194 196
197 // Returns true when Chrome OS is running in a VM. NOTE: if crossystem is not
198 // installed it will return false even if Chrome OS is running in a VM.
199 bool IsRunningOnVm() override;
200
195 static StatisticsProviderImpl* GetInstance(); 201 static StatisticsProviderImpl* GetInstance();
196 202
197 protected: 203 protected:
198 typedef std::map<std::string, bool> MachineFlags; 204 typedef std::map<std::string, bool> MachineFlags;
199 typedef bool (*RegionDataExtractor)(const base::DictionaryValue*, 205 typedef bool (*RegionDataExtractor)(const base::DictionaryValue*,
200 std::string*); 206 std::string*);
201 friend struct base::DefaultSingletonTraits<StatisticsProviderImpl>; 207 friend struct base::DefaultSingletonTraits<StatisticsProviderImpl>;
202 208
203 StatisticsProviderImpl(); 209 StatisticsProviderImpl();
204 ~StatisticsProviderImpl() override; 210 ~StatisticsProviderImpl() override;
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after
368 } 374 }
369 if (result != nullptr) 375 if (result != nullptr)
370 *result = iter->second; 376 *result = iter->second;
371 return true; 377 return true;
372 } 378 }
373 379
374 void StatisticsProviderImpl::Shutdown() { 380 void StatisticsProviderImpl::Shutdown() {
375 cancellation_flag_.Set(); // Cancel any pending loads 381 cancellation_flag_.Set(); // Cancel any pending loads
376 } 382 }
377 383
384 bool StatisticsProviderImpl::IsRunningOnVm() {
385 if (!base::SysInfo::IsRunningOnChromeOS())
386 return false;
387 std::string is_vm;
388 return GetMachineStatistic(kIsVmKey, &is_vm) && is_vm == kIsVmValueTrue;
389 }
390
378 StatisticsProviderImpl::StatisticsProviderImpl() 391 StatisticsProviderImpl::StatisticsProviderImpl()
379 : load_statistics_started_(false), 392 : load_statistics_started_(false),
380 on_statistics_loaded_(base::WaitableEvent::ResetPolicy::MANUAL, 393 on_statistics_loaded_(base::WaitableEvent::ResetPolicy::MANUAL,
381 base::WaitableEvent::InitialState::NOT_SIGNALED), 394 base::WaitableEvent::InitialState::NOT_SIGNALED),
382 oem_manifest_loaded_(false) { 395 oem_manifest_loaded_(false) {
383 regional_data_extractors_[kInitialLocaleKey] = 396 regional_data_extractors_[kInitialLocaleKey] =
384 &GetInitialLocaleFromRegionalData; 397 &GetInitialLocaleFromRegionalData;
385 regional_data_extractors_[kKeyboardLayoutKey] = 398 regional_data_extractors_[kKeyboardLayoutKey] =
386 &GetKeyboardLayoutFromRegionalData; 399 &GetKeyboardLayoutFromRegionalData;
387 regional_data_extractors_[kInitialTimezoneKey] = 400 regional_data_extractors_[kInitialTimezoneKey] =
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
440 int bytes_written = base::WriteFile(machine_info_path, 453 int bytes_written = base::WriteFile(machine_info_path,
441 stub_contents.c_str(), 454 stub_contents.c_str(),
442 stub_contents.size()); 455 stub_contents.size());
443 // static_cast<int> is fine because stub_contents is small. 456 // static_cast<int> is fine because stub_contents is small.
444 if (bytes_written < static_cast<int>(stub_contents.size())) { 457 if (bytes_written < static_cast<int>(stub_contents.size())) {
445 LOG(ERROR) << "Error writing machine info stub: " 458 LOG(ERROR) << "Error writing machine info stub: "
446 << machine_info_path.value(); 459 << machine_info_path.value();
447 } 460 }
448 } 461 }
449 462
450 if (base::SysInfo::IsRunningOnChromeOS()) {
451 std::string system_vendor;
452 base::ReadFileToString(base::FilePath(kSystemVendorFile), &system_vendor);
453 machine_info_[kSystemVendorKey] = system_vendor;
454 }
455
456 parser.GetNameValuePairsFromFile(machine_info_path, 463 parser.GetNameValuePairsFromFile(machine_info_path,
457 kMachineHardwareInfoEq, 464 kMachineHardwareInfoEq,
458 kMachineHardwareInfoDelim); 465 kMachineHardwareInfoDelim);
459 parser.GetNameValuePairsFromFile(base::FilePath(kEchoCouponFile), 466 parser.GetNameValuePairsFromFile(base::FilePath(kEchoCouponFile),
460 kEchoCouponEq, 467 kEchoCouponEq,
461 kEchoCouponDelim); 468 kEchoCouponDelim);
462 parser.GetNameValuePairsFromFile(base::FilePath(kVpdFile), 469 parser.GetNameValuePairsFromFile(base::FilePath(kVpdFile),
463 kVpdEq, 470 kVpdEq,
464 kVpdDelim); 471 kVpdDelim);
465 472
466 // Ensure that the hardware class key is present with the expected 473 // Ensure that the hardware class key is present with the expected
467 // key name, and if it couldn't be retrieved, that the value is "unknown". 474 // key name, and if it couldn't be retrieved, that the value is "unknown".
468 std::string hardware_class = machine_info_[kHardwareClassCrosSystemKey]; 475 std::string hardware_class = machine_info_[kHardwareClassCrosSystemKey];
469 if (hardware_class.empty() || hardware_class == kCrosSystemUnknownValue) 476 if (hardware_class.empty() || hardware_class == kCrosSystemValueError) {
470 machine_info_[kHardwareClassKey] = kUnknownHardwareClass; 477 machine_info_[kHardwareClassKey] = kHardwareClassValueUnknown;
471 else 478 } else {
472 machine_info_[kHardwareClassKey] = hardware_class; 479 machine_info_[kHardwareClassKey] = hardware_class;
480 }
481
482 if (base::SysInfo::IsRunningOnChromeOS()) {
483 // By default, assume that this is *not* a VM. If crossystem is not present,
484 // report that we are not in a VM.
485 machine_info_[kIsVmKey] = kIsVmValueFalse;
486 const auto is_vm_iter = machine_info_.find(kIsVmCrosSystemKey);
487 if (is_vm_iter != machine_info_.end() &&
488 is_vm_iter->second == kIsVmValueTrue) {
489 machine_info_[kIsVmKey] = kIsVmValueTrue;
490 }
491 }
473 492
474 if (load_oem_manifest) { 493 if (load_oem_manifest) {
475 // If kAppOemManifestFile switch is specified, load OEM Manifest file. 494 // If kAppOemManifestFile switch is specified, load OEM Manifest file.
476 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); 495 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
477 if (command_line->HasSwitch(switches::kAppOemManifestFile)) { 496 if (command_line->HasSwitch(switches::kAppOemManifestFile)) {
478 LoadOemManifestFromFile( 497 LoadOemManifestFromFile(
479 command_line->GetSwitchValuePath(switches::kAppOemManifestFile)); 498 command_line->GetSwitchValuePath(switches::kAppOemManifestFile));
480 } else if (base::SysInfo::IsRunningOnChromeOS()) { 499 } else if (base::SysInfo::IsRunningOnChromeOS()) {
481 LoadOemManifestFromFile(base::FilePath(kOemManifestFilePath)); 500 LoadOemManifestFromFile(base::FilePath(kOemManifestFilePath));
482 } 501 }
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
571 return StatisticsProviderImpl::GetInstance(); 590 return StatisticsProviderImpl::GetInstance();
572 } 591 }
573 592
574 // static 593 // static
575 void StatisticsProvider::SetTestProvider(StatisticsProvider* test_provider) { 594 void StatisticsProvider::SetTestProvider(StatisticsProvider* test_provider) {
576 g_test_statistics_provider = test_provider; 595 g_test_statistics_provider = test_provider;
577 } 596 }
578 597
579 } // namespace system 598 } // namespace system
580 } // namespace chromeos 599 } // namespace chromeos
OLDNEW
« 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