local_device_info_provider_impl.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Copyright 2014 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_provider_impl.h"
  5. #include "base/bind.h"
  6. #include "components/sync/base/sync_prefs.h"
  7. #include "components/sync/base/sync_util.h"
  8. #include "components/sync_device_info/device_info_sync_client.h"
  9. #include "components/sync_device_info/device_info_util.h"
  10. #include "components/sync_device_info/local_device_info_util.h"
  11. namespace syncer {
  12. LocalDeviceInfoProviderImpl::LocalDeviceInfoProviderImpl(
  13. version_info::Channel channel,
  14. const std::string& version,
  15. const DeviceInfoSyncClient* sync_client)
  16. : channel_(channel), version_(version), sync_client_(sync_client) {
  17. DCHECK(sync_client);
  18. }
  19. LocalDeviceInfoProviderImpl::~LocalDeviceInfoProviderImpl() {
  20. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  21. }
  22. version_info::Channel LocalDeviceInfoProviderImpl::GetChannel() const {
  23. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  24. return channel_;
  25. }
  26. const DeviceInfo* LocalDeviceInfoProviderImpl::GetLocalDeviceInfo() const {
  27. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  28. if (!local_device_info_) {
  29. return nullptr;
  30. }
  31. // Pull new values for settings that aren't automatically updated.
  32. local_device_info_->set_send_tab_to_self_receiving_enabled(
  33. sync_client_->GetSendTabToSelfReceivingEnabled());
  34. local_device_info_->set_sharing_info(sync_client_->GetLocalSharingInfo());
  35. // Do not update previous values if the service is not fully initialized.
  36. // absl::nullopt means that the value is unknown yet and the previous value
  37. // should be kept.
  38. const absl::optional<std::string> fcm_token =
  39. sync_client_->GetFCMRegistrationToken();
  40. if (fcm_token) {
  41. local_device_info_->set_fcm_registration_token(*fcm_token);
  42. }
  43. const absl::optional<ModelTypeSet> interested_data_types =
  44. sync_client_->GetInterestedDataTypes();
  45. if (interested_data_types) {
  46. local_device_info_->set_interested_data_types(*interested_data_types);
  47. }
  48. absl::optional<DeviceInfo::PhoneAsASecurityKeyInfo> paask_info =
  49. sync_client_->GetPhoneAsASecurityKeyInfo();
  50. if (paask_info) {
  51. local_device_info_->set_paask_info(std::move(*paask_info));
  52. }
  53. // This check is required to ensure user's who toggle UMA have their
  54. // hardware class data updated on next sync.
  55. if (!IsUmaEnabledOnCrOSDevice()) {
  56. local_device_info_->set_full_hardware_class("");
  57. } else {
  58. local_device_info_->set_full_hardware_class(full_hardware_class_);
  59. }
  60. return local_device_info_.get();
  61. }
  62. base::CallbackListSubscription
  63. LocalDeviceInfoProviderImpl::RegisterOnInitializedCallback(
  64. const base::RepeatingClosure& callback) {
  65. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  66. DCHECK(!local_device_info_);
  67. return closure_list_.Add(callback);
  68. }
  69. // Returns whether a ChromeOS device has UMA enabled.
  70. // Returns false when called on non-CrOS devices.
  71. bool LocalDeviceInfoProviderImpl::IsUmaEnabledOnCrOSDevice() const {
  72. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  73. return sync_client_->IsUmaEnabledOnCrOSDevice();
  74. }
  75. void LocalDeviceInfoProviderImpl::Initialize(
  76. const std::string& cache_guid,
  77. const std::string& client_name,
  78. const std::string& manufacturer_name,
  79. const std::string& model_name,
  80. const std::string& full_hardware_class,
  81. std::unique_ptr<DeviceInfo> device_info_restored_from_store) {
  82. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  83. DCHECK(!cache_guid.empty());
  84. // Some values of |DeviceInfo| may not immediately be ready. For these, their
  85. // previous value is extracted from the restored |DeviceInfo| and used to
  86. // initialise the object. |GetLocalDeviceInfo| will update them if they have
  87. // become ready by then.
  88. std::string last_fcm_registration_token;
  89. ModelTypeSet last_interested_data_types;
  90. absl::optional<DeviceInfo::PhoneAsASecurityKeyInfo> paask_info;
  91. if (device_info_restored_from_store) {
  92. last_fcm_registration_token =
  93. device_info_restored_from_store->fcm_registration_token();
  94. last_interested_data_types =
  95. device_info_restored_from_store->interested_data_types();
  96. paask_info = device_info_restored_from_store->paask_info();
  97. }
  98. // The local device doesn't have a last updated timestamps. It will be set in
  99. // the specifics when it will be synced up.
  100. local_device_info_ = std::make_unique<DeviceInfo>(
  101. cache_guid, client_name, version_, MakeUserAgentForSync(channel_),
  102. GetLocalDeviceType(), sync_client_->GetSigninScopedDeviceId(),
  103. manufacturer_name, model_name, full_hardware_class,
  104. /*last_updated_timestamp=*/base::Time(),
  105. DeviceInfoUtil::GetPulseInterval(),
  106. sync_client_->GetSendTabToSelfReceivingEnabled(),
  107. sync_client_->GetLocalSharingInfo(), paask_info,
  108. last_fcm_registration_token, last_interested_data_types);
  109. full_hardware_class_ = full_hardware_class;
  110. // Notify observers.
  111. closure_list_.Notify();
  112. }
  113. void LocalDeviceInfoProviderImpl::Clear() {
  114. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  115. local_device_info_.reset();
  116. }
  117. void LocalDeviceInfoProviderImpl::UpdateClientName(
  118. const std::string& client_name) {
  119. DCHECK(local_device_info_);
  120. local_device_info_->set_client_name(client_name);
  121. }
  122. } // namespace syncer