host_device_timestamp_manager_impl.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright 2018 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 "ash/services/multidevice_setup/host_device_timestamp_manager_impl.h"
  5. #include "ash/components/multidevice/logging/logging.h"
  6. #include "base/memory/ptr_util.h"
  7. #include "base/time/clock.h"
  8. #include "components/prefs/pref_registry_simple.h"
  9. #include "components/prefs/pref_service.h"
  10. namespace ash {
  11. namespace multidevice_setup {
  12. namespace {
  13. const int64_t kTimestampNotSet = 0;
  14. } // namespace
  15. // static
  16. HostDeviceTimestampManagerImpl::Factory*
  17. HostDeviceTimestampManagerImpl::Factory::test_factory_ = nullptr;
  18. // static
  19. std::unique_ptr<HostDeviceTimestampManager>
  20. HostDeviceTimestampManagerImpl::Factory::Create(
  21. HostStatusProvider* host_status_provider,
  22. PrefService* pref_service,
  23. base::Clock* clock) {
  24. if (test_factory_) {
  25. return test_factory_->CreateInstance(host_status_provider, pref_service,
  26. clock);
  27. }
  28. return base::WrapUnique(new HostDeviceTimestampManagerImpl(
  29. host_status_provider, pref_service, clock));
  30. }
  31. // static
  32. void HostDeviceTimestampManagerImpl::Factory::SetFactoryForTesting(
  33. Factory* test_factory) {
  34. test_factory_ = test_factory;
  35. }
  36. HostDeviceTimestampManagerImpl::Factory::~Factory() = default;
  37. // static
  38. const char
  39. HostDeviceTimestampManagerImpl::kWasHostSetFromThisChromebookPrefName[] =
  40. "multidevice_setup.was_host_set_from_this_chromebook";
  41. // static
  42. const char HostDeviceTimestampManagerImpl::kSetupFlowCompletedPrefName[] =
  43. "multidevice_setup.setup_flow_completed";
  44. // static
  45. const char
  46. HostDeviceTimestampManagerImpl::kHostVerifiedUpdateReceivedPrefName[] =
  47. "multidevice_setup.host_verified_update_received";
  48. // static
  49. void HostDeviceTimestampManagerImpl::RegisterPrefs(
  50. PrefRegistrySimple* registry) {
  51. registry->RegisterBooleanPref(kWasHostSetFromThisChromebookPrefName, false);
  52. registry->RegisterInt64Pref(kSetupFlowCompletedPrefName, kTimestampNotSet);
  53. registry->RegisterInt64Pref(kHostVerifiedUpdateReceivedPrefName,
  54. kTimestampNotSet);
  55. }
  56. HostDeviceTimestampManagerImpl::~HostDeviceTimestampManagerImpl() {
  57. host_status_provider_->RemoveObserver(this);
  58. }
  59. HostDeviceTimestampManagerImpl::HostDeviceTimestampManagerImpl(
  60. HostStatusProvider* host_status_provider,
  61. PrefService* pref_service,
  62. base::Clock* clock)
  63. : host_status_provider_(host_status_provider),
  64. pref_service_(pref_service),
  65. clock_(clock) {
  66. host_status_provider_->AddObserver(this);
  67. }
  68. bool HostDeviceTimestampManagerImpl::WasHostSetFromThisChromebook() {
  69. return pref_service_->GetBoolean(kWasHostSetFromThisChromebookPrefName);
  70. }
  71. absl::optional<base::Time>
  72. HostDeviceTimestampManagerImpl::GetLatestSetupFlowCompletionTimestamp() {
  73. if (pref_service_->GetInt64(kSetupFlowCompletedPrefName) == kTimestampNotSet)
  74. return absl::nullopt;
  75. return base::Time::FromJavaTime(
  76. pref_service_->GetInt64(kSetupFlowCompletedPrefName));
  77. }
  78. absl::optional<base::Time>
  79. HostDeviceTimestampManagerImpl::GetLatestVerificationTimestamp() {
  80. if (pref_service_->GetInt64(kHostVerifiedUpdateReceivedPrefName) ==
  81. kTimestampNotSet)
  82. return absl::nullopt;
  83. return base::Time::FromJavaTime(
  84. pref_service_->GetInt64(kHostVerifiedUpdateReceivedPrefName));
  85. }
  86. void HostDeviceTimestampManagerImpl::OnHostStatusChange(
  87. const HostStatusProvider::HostStatusWithDevice& host_status_with_device) {
  88. // Check if setup flow was completed on this Chromebook. Note that it suffices
  89. // to use a host status update with the status
  90. // kHostSetLocallyButWaitingForBackendConfirmation as a proxy for completing
  91. // setup flow because the Chromebook sets a host locally (i.e. enters this
  92. // state) exactly when it successfully completes the flow. Note that this is
  93. // equivalent to a host being set on the Chromebook.
  94. if (host_status_with_device.host_status() ==
  95. mojom::HostStatus::kHostSetLocallyButWaitingForBackendConfirmation) {
  96. pref_service_->SetInt64(kSetupFlowCompletedPrefName,
  97. clock_->Now().ToJavaTime());
  98. pref_service_->SetBoolean(kWasHostSetFromThisChromebookPrefName, true);
  99. PA_LOG(VERBOSE) << "HostDeviceTimestampManagerImpl::OnHostStatusChange(): "
  100. << "Setup flow successfully completed. Recording timestamp "
  101. << pref_service_->GetInt64(kSetupFlowCompletedPrefName)
  102. << ".";
  103. }
  104. // Check if a host has been verified.
  105. if (host_status_with_device.host_status() ==
  106. mojom::HostStatus::kHostVerified) {
  107. pref_service_->SetInt64(kHostVerifiedUpdateReceivedPrefName,
  108. clock_->Now().ToJavaTime());
  109. PA_LOG(VERBOSE) << "HostDeviceTimestampManagerImpl::OnHostStatusChange(): "
  110. << "New host verified. Recording timestamp "
  111. << pref_service_->GetInt64(
  112. kHostVerifiedUpdateReceivedPrefName)
  113. << ".";
  114. }
  115. // If there is no host set, set the "was host set form this Chromebook" bit to
  116. // false.
  117. if (host_status_with_device.host_status() ==
  118. mojom::HostStatus::kNoEligibleHosts ||
  119. host_status_with_device.host_status() ==
  120. mojom::HostStatus::kEligibleHostExistsButNoHostSet) {
  121. pref_service_->SetBoolean(kWasHostSetFromThisChromebookPrefName, false);
  122. }
  123. }
  124. } // namespace multidevice_setup
  125. } // namespace ash