Chromium Code Reviews| OLD | NEW |
|---|---|
| 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"; |
| 51 | |
| 52 // File to get system vendor information from. (x86-only) | |
| 50 const char kSystemVendorFile[] = "/sys/class/dmi/id/sys_vendor"; | 53 const char kSystemVendorFile[] = "/sys/class/dmi/id/sys_vendor"; |
| 54 const char kSystemVendorValueUnknown[] = "unknown"; | |
| 51 | 55 |
| 52 // Key/value delimiters of machine hardware info file. machine-info is generated | 56 // 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 | 57 // only for OOBE and enterprise enrollment and may not be present. See |
| 54 // login-manager/init/machine-info.conf. | 58 // login-manager/init/machine-info.conf. |
| 55 const char kMachineHardwareInfoEq[] = "="; | 59 const char kMachineHardwareInfoEq[] = "="; |
| 56 const char kMachineHardwareInfoDelim[] = " \n"; | 60 const char kMachineHardwareInfoDelim[] = " \n"; |
| 57 | 61 |
| 58 // File to get ECHO coupon info from, and key/value delimiters of | 62 // File to get ECHO coupon info from, and key/value delimiters of |
| 59 // the file. | 63 // the file. |
| 60 const char kEchoCouponFile[] = "/var/cache/echo/vpd_echo.txt"; | 64 const char kEchoCouponFile[] = "/var/cache/echo/vpd_echo.txt"; |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 151 const char kCheckEnrollmentKey[] = "check_enrollment"; | 155 const char kCheckEnrollmentKey[] = "check_enrollment"; |
| 152 const char kCustomizationIdKey[] = "customization_id"; | 156 const char kCustomizationIdKey[] = "customization_id"; |
| 153 const char kDevSwitchBootKey[] = "devsw_boot"; | 157 const char kDevSwitchBootKey[] = "devsw_boot"; |
| 154 const char kDevSwitchBootValueDev[] = "1"; | 158 const char kDevSwitchBootValueDev[] = "1"; |
| 155 const char kDevSwitchBootValueVerified[] = "0"; | 159 const char kDevSwitchBootValueVerified[] = "0"; |
| 156 const char kFirmwareTypeKey[] = "mainfw_type"; | 160 const char kFirmwareTypeKey[] = "mainfw_type"; |
| 157 const char kFirmwareTypeValueDeveloper[] = "developer"; | 161 const char kFirmwareTypeValueDeveloper[] = "developer"; |
| 158 const char kFirmwareTypeValueNonchrome[] = "nonchrome"; | 162 const char kFirmwareTypeValueNonchrome[] = "nonchrome"; |
| 159 const char kFirmwareTypeValueNormal[] = "normal"; | 163 const char kFirmwareTypeValueNormal[] = "normal"; |
| 160 const char kHardwareClassKey[] = "hardware_class"; | 164 const char kHardwareClassKey[] = "hardware_class"; |
| 165 const char kIsCrosVMKey[] = "inside_cros_vm"; | |
| 166 const char kIsCrosVMValueTrue[] = "1"; | |
| 167 const char kIsCrosVMValueFalse[] = "0"; | |
| 161 const char kSystemVendorKey[] = "system_vendor"; | 168 const char kSystemVendorKey[] = "system_vendor"; |
| 162 const char kOffersCouponCodeKey[] = "ubind_attribute"; | 169 const char kOffersCouponCodeKey[] = "ubind_attribute"; |
| 163 const char kOffersGroupCodeKey[] = "gbind_attribute"; | 170 const char kOffersGroupCodeKey[] = "gbind_attribute"; |
| 164 const char kRlzBrandCodeKey[] = "rlz_brand_code"; | 171 const char kRlzBrandCodeKey[] = "rlz_brand_code"; |
| 165 const char kWriteProtectSwitchBootKey[] = "wpsw_boot"; | 172 const char kWriteProtectSwitchBootKey[] = "wpsw_boot"; |
| 166 const char kWriteProtectSwitchBootValueOff[] = "0"; | 173 const char kWriteProtectSwitchBootValueOff[] = "0"; |
| 167 const char kWriteProtectSwitchBootValueOn[] = "1"; | 174 const char kWriteProtectSwitchBootValueOn[] = "1"; |
| 168 const char kRegionKey[] = "region"; | 175 const char kRegionKey[] = "region"; |
| 169 const char kInitialLocaleKey[] = "initial_locale"; | 176 const char kInitialLocaleKey[] = "initial_locale"; |
| 170 const char kInitialTimezoneKey[] = "initial_timezone"; | 177 const char kInitialTimezoneKey[] = "initial_timezone"; |
| 171 const char kKeyboardLayoutKey[] = "keyboard_layout"; | 178 const char kKeyboardLayoutKey[] = "keyboard_layout"; |
| 172 | 179 |
| 173 // OEM specific statistics. Must be prefixed with "oem_". | 180 // OEM specific statistics. Must be prefixed with "oem_". |
| 174 const char kOemCanExitEnterpriseEnrollmentKey[] = "oem_can_exit_enrollment"; | 181 const char kOemCanExitEnterpriseEnrollmentKey[] = "oem_can_exit_enrollment"; |
| 175 const char kOemDeviceRequisitionKey[] = "oem_device_requisition"; | 182 const char kOemDeviceRequisitionKey[] = "oem_device_requisition"; |
| 176 const char kOemIsEnterpriseManagedKey[] = "oem_enterprise_managed"; | 183 const char kOemIsEnterpriseManagedKey[] = "oem_enterprise_managed"; |
| 177 const char kOemKeyboardDrivenOobeKey[] = "oem_keyboard_driven_oobe"; | 184 const char kOemKeyboardDrivenOobeKey[] = "oem_keyboard_driven_oobe"; |
| 178 | 185 |
| 179 bool HasOemPrefix(const std::string& name) { | 186 bool HasOemPrefix(const std::string& name) { |
| 180 return name.substr(0, 4) == "oem_"; | 187 return name.substr(0, 4) == "oem_"; |
| 181 } | 188 } |
| 182 | 189 |
| 190 bool isRunningOnChromeOSVM() { | |
| 191 if (!base::SysInfo::IsRunningOnChromeOS()) | |
| 192 return false; | |
| 193 std::string is_vm; | |
| 194 if (StatisticsProvider::GetInstance()->GetMachineStatistic(kIsCrosVMKey, | |
|
Daniel Erat
2016/08/10 13:58:05
nit:
return GetMachineStatistic(kIsCrosVmKey, &
| |
| 195 &is_vm)) { | |
| 196 return is_vm == kIsCrosVMValueTrue; | |
| 197 } | |
| 198 return false; | |
| 199 } | |
| 200 | |
| 183 // The StatisticsProvider implementation used in production. | 201 // The StatisticsProvider implementation used in production. |
| 184 class StatisticsProviderImpl : public StatisticsProvider { | 202 class StatisticsProviderImpl : public StatisticsProvider { |
| 185 public: | 203 public: |
| 186 // StatisticsProvider implementation: | 204 // StatisticsProvider implementation: |
| 187 void StartLoadingMachineStatistics( | 205 void StartLoadingMachineStatistics( |
| 188 const scoped_refptr<base::TaskRunner>& file_task_runner, | 206 const scoped_refptr<base::TaskRunner>& file_task_runner, |
| 189 bool load_oem_manifest) override; | 207 bool load_oem_manifest) override; |
| 190 bool GetMachineStatistic(const std::string& name, | 208 bool GetMachineStatistic(const std::string& name, |
| 191 std::string* result) override; | 209 std::string* result) override; |
| 192 bool GetMachineFlag(const std::string& name, bool* result) override; | 210 bool GetMachineFlag(const std::string& name, bool* result) override; |
| (...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 440 int bytes_written = base::WriteFile(machine_info_path, | 458 int bytes_written = base::WriteFile(machine_info_path, |
| 441 stub_contents.c_str(), | 459 stub_contents.c_str(), |
| 442 stub_contents.size()); | 460 stub_contents.size()); |
| 443 // static_cast<int> is fine because stub_contents is small. | 461 // static_cast<int> is fine because stub_contents is small. |
| 444 if (bytes_written < static_cast<int>(stub_contents.size())) { | 462 if (bytes_written < static_cast<int>(stub_contents.size())) { |
| 445 LOG(ERROR) << "Error writing machine info stub: " | 463 LOG(ERROR) << "Error writing machine info stub: " |
| 446 << machine_info_path.value(); | 464 << machine_info_path.value(); |
| 447 } | 465 } |
| 448 } | 466 } |
| 449 | 467 |
| 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, | 468 parser.GetNameValuePairsFromFile(machine_info_path, |
| 457 kMachineHardwareInfoEq, | 469 kMachineHardwareInfoEq, |
| 458 kMachineHardwareInfoDelim); | 470 kMachineHardwareInfoDelim); |
| 459 parser.GetNameValuePairsFromFile(base::FilePath(kEchoCouponFile), | 471 parser.GetNameValuePairsFromFile(base::FilePath(kEchoCouponFile), |
| 460 kEchoCouponEq, | 472 kEchoCouponEq, |
| 461 kEchoCouponDelim); | 473 kEchoCouponDelim); |
| 462 parser.GetNameValuePairsFromFile(base::FilePath(kVpdFile), | 474 parser.GetNameValuePairsFromFile(base::FilePath(kVpdFile), |
| 463 kVpdEq, | 475 kVpdEq, |
| 464 kVpdDelim); | 476 kVpdDelim); |
| 465 | 477 |
| 466 // Ensure that the hardware class key is present with the expected | 478 // 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". | 479 // key name, and if it couldn't be retrieved, that the value is "unknown". |
| 468 std::string hardware_class = machine_info_[kHardwareClassCrosSystemKey]; | 480 std::string hardware_class = machine_info_[kHardwareClassCrosSystemKey]; |
| 469 if (hardware_class.empty() || hardware_class == kCrosSystemUnknownValue) | 481 if (hardware_class.empty() || hardware_class == kCrosSystemValueError) { |
| 470 machine_info_[kHardwareClassKey] = kUnknownHardwareClass; | 482 machine_info_[kHardwareClassKey] = kHardwareClassValueUnknown; |
| 471 else | 483 } else { |
| 472 machine_info_[kHardwareClassKey] = hardware_class; | 484 machine_info_[kHardwareClassKey] = hardware_class; |
| 485 } | |
| 486 | |
| 487 if (base::SysInfo::IsRunningOnChromeOS()) { | |
| 488 // By default, assume that this is *not* a VM. If crossystem is not present, | |
| 489 // report that we are not in a VM. | |
| 490 machine_info_[kIsCrosVMKey] = kIsCrosVMValueFalse; | |
| 491 const auto is_vm_iter = machine_info_.find(kIsVMCrosSystemKey); | |
| 492 if (is_vm_iter != machine_info_.end() && | |
| 493 is_vm_iter->second == kIsCrosVMValueTrue) { | |
| 494 machine_info_[kIsCrosVMKey] = kIsCrosVMValueTrue; | |
| 495 } | |
|
Daniel Erat
2016/08/10 13:58:05
nit: add a blank line after this to set the two se
| |
| 496 // Ensure that the system vendor key is present, in particular it is | |
| 497 // potentially useful for VM environments from different vendors. | |
| 498 // On non-x86 architectures the value will be "unknown". | |
| 499 machine_info_[kSystemVendorKey] = kSystemVendorValueUnknown; | |
| 500 std::string system_vendor; | |
| 501 if (base::ReadFileToString(base::FilePath(kSystemVendorFile), | |
| 502 &system_vendor)) { | |
| 503 base::TrimWhitespaceASCII(system_vendor, base::TRIM_ALL, &system_vendor); | |
| 504 if (!system_vendor.empty()) | |
| 505 machine_info_[kSystemVendorKey] = system_vendor; | |
| 506 } | |
| 507 } | |
| 473 | 508 |
| 474 if (load_oem_manifest) { | 509 if (load_oem_manifest) { |
| 475 // If kAppOemManifestFile switch is specified, load OEM Manifest file. | 510 // If kAppOemManifestFile switch is specified, load OEM Manifest file. |
| 476 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | 511 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 477 if (command_line->HasSwitch(switches::kAppOemManifestFile)) { | 512 if (command_line->HasSwitch(switches::kAppOemManifestFile)) { |
| 478 LoadOemManifestFromFile( | 513 LoadOemManifestFromFile( |
| 479 command_line->GetSwitchValuePath(switches::kAppOemManifestFile)); | 514 command_line->GetSwitchValuePath(switches::kAppOemManifestFile)); |
| 480 } else if (base::SysInfo::IsRunningOnChromeOS()) { | 515 } else if (base::SysInfo::IsRunningOnChromeOS()) { |
| 481 LoadOemManifestFromFile(base::FilePath(kOemManifestFilePath)); | 516 LoadOemManifestFromFile(base::FilePath(kOemManifestFilePath)); |
| 482 } | 517 } |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 571 return StatisticsProviderImpl::GetInstance(); | 606 return StatisticsProviderImpl::GetInstance(); |
| 572 } | 607 } |
| 573 | 608 |
| 574 // static | 609 // static |
| 575 void StatisticsProvider::SetTestProvider(StatisticsProvider* test_provider) { | 610 void StatisticsProvider::SetTestProvider(StatisticsProvider* test_provider) { |
| 576 g_test_statistics_provider = test_provider; | 611 g_test_statistics_provider = test_provider; |
| 577 } | 612 } |
| 578 | 613 |
| 579 } // namespace system | 614 } // namespace system |
| 580 } // namespace chromeos | 615 } // namespace chromeos |
| OLD | NEW |