local_device_info_util.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/sync_device_info/local_device_info_util.h"
  5. #include "base/barrier_closure.h"
  6. #include "base/callback.h"
  7. #include "base/callback_helpers.h"
  8. #include "base/location.h"
  9. #include "base/strings/string_util.h"
  10. #include "base/system/sys_info.h"
  11. #include "base/task/thread_pool.h"
  12. #include "base/threading/scoped_blocking_call.h"
  13. #include "build/build_config.h"
  14. #include "build/chromeos_buildflags.h"
  15. #include "ui/base/device_form_factor.h"
  16. #if BUILDFLAG(IS_CHROMEOS_ASH)
  17. #include "chromeos/system/statistics_provider.h"
  18. #endif
  19. namespace syncer {
  20. // Declared here but defined in platform-specific files.
  21. std::string GetPersonalizableDeviceNameInternal();
  22. #if BUILDFLAG(IS_CHROMEOS)
  23. std::string GetChromeOSDeviceNameFromType();
  24. #endif
  25. LocalDeviceNameInfo::LocalDeviceNameInfo() = default;
  26. LocalDeviceNameInfo::LocalDeviceNameInfo(const LocalDeviceNameInfo& other) =
  27. default;
  28. LocalDeviceNameInfo::~LocalDeviceNameInfo() = default;
  29. namespace {
  30. void OnLocalDeviceNameInfoReady(
  31. base::OnceCallback<void(LocalDeviceNameInfo)> callback,
  32. std::unique_ptr<LocalDeviceNameInfo> name_info) {
  33. std::move(callback).Run(std::move(*name_info));
  34. }
  35. void OnHardwareInfoReady(LocalDeviceNameInfo* name_info_ptr,
  36. base::ScopedClosureRunner done_closure,
  37. base::SysInfo::HardwareInfo hardware_info) {
  38. name_info_ptr->manufacturer_name = std::move(hardware_info.manufacturer);
  39. #if BUILDFLAG(IS_CHROMEOS)
  40. // For ChromeOS the returned model values are product code names like Eve. We
  41. // want to use generic names like Chromebook.
  42. name_info_ptr->model_name = GetChromeOSDeviceNameFromType();
  43. #else
  44. name_info_ptr->model_name = std::move(hardware_info.model);
  45. #endif
  46. }
  47. void OnPersonalizableDeviceNameReady(LocalDeviceNameInfo* name_info_ptr,
  48. base::ScopedClosureRunner done_closure,
  49. std::string personalizable_name) {
  50. name_info_ptr->personalizable_name = std::move(personalizable_name);
  51. }
  52. void OnMachineStatisticsLoaded(LocalDeviceNameInfo* name_info_ptr,
  53. base::ScopedClosureRunner done_closure) {
  54. #if BUILDFLAG(IS_CHROMEOS_ASH)
  55. // |full_hardware_class| is set on Chrome OS devices if the user has UMA
  56. // enabled. Otherwise |full_hardware_class| is set to an empty string.
  57. chromeos::system::StatisticsProvider::GetInstance()->GetMachineStatistic(
  58. chromeos::system::kHardwareClassKey, &name_info_ptr->full_hardware_class);
  59. #else
  60. name_info_ptr->full_hardware_class = "";
  61. #endif
  62. }
  63. } // namespace
  64. sync_pb::SyncEnums::DeviceType GetLocalDeviceType() {
  65. #if BUILDFLAG(IS_CHROMEOS)
  66. return sync_pb::SyncEnums_DeviceType_TYPE_CROS;
  67. #elif BUILDFLAG(IS_LINUX)
  68. return sync_pb::SyncEnums_DeviceType_TYPE_LINUX;
  69. #elif BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS)
  70. return ui::GetDeviceFormFactor() == ui::DEVICE_FORM_FACTOR_TABLET
  71. ? sync_pb::SyncEnums_DeviceType_TYPE_TABLET
  72. : sync_pb::SyncEnums_DeviceType_TYPE_PHONE;
  73. #elif BUILDFLAG(IS_MAC)
  74. return sync_pb::SyncEnums_DeviceType_TYPE_MAC;
  75. #elif BUILDFLAG(IS_WIN)
  76. return sync_pb::SyncEnums_DeviceType_TYPE_WIN;
  77. #else
  78. return sync_pb::SyncEnums_DeviceType_TYPE_OTHER;
  79. #endif
  80. }
  81. std::string GetPersonalizableDeviceNameBlocking() {
  82. base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
  83. base::BlockingType::MAY_BLOCK);
  84. std::string device_name = GetPersonalizableDeviceNameInternal();
  85. if (device_name == "Unknown" || device_name.empty()) {
  86. device_name = base::SysInfo::OperatingSystemName();
  87. }
  88. DCHECK(base::IsStringUTF8(device_name));
  89. return device_name;
  90. }
  91. void GetLocalDeviceNameInfo(
  92. base::OnceCallback<void(LocalDeviceNameInfo)> callback) {
  93. auto name_info = std::make_unique<LocalDeviceNameInfo>();
  94. LocalDeviceNameInfo* name_info_ptr = name_info.get();
  95. auto done_closure = base::BarrierClosure(
  96. /*num_closures=*/3,
  97. base::BindOnce(&OnLocalDeviceNameInfoReady, std::move(callback),
  98. std::move(name_info)));
  99. base::SysInfo::GetHardwareInfo(
  100. base::BindOnce(&OnHardwareInfoReady, name_info_ptr,
  101. base::ScopedClosureRunner(done_closure)));
  102. #if BUILDFLAG(IS_CHROMEOS_ASH)
  103. // Bind hwclass once the statistics are available on ChromeOS devices.
  104. chromeos::system::StatisticsProvider::GetInstance()
  105. ->ScheduleOnMachineStatisticsLoaded(
  106. base::BindOnce(&OnMachineStatisticsLoaded, name_info_ptr,
  107. base::ScopedClosureRunner(done_closure)));
  108. #else
  109. OnMachineStatisticsLoaded(name_info_ptr,
  110. base::ScopedClosureRunner(done_closure));
  111. #endif
  112. base::ThreadPool::PostTaskAndReplyWithResult(
  113. FROM_HERE,
  114. {base::MayBlock(), base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN},
  115. base::BindOnce(&GetPersonalizableDeviceNameBlocking),
  116. base::BindOnce(&OnPersonalizableDeviceNameReady, name_info_ptr,
  117. base::ScopedClosureRunner(done_closure)));
  118. }
  119. } // namespace syncer