autozoom_nudge_controller.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright 2022 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/system/camera/autozoom_nudge_controller.h"
  5. #include "ash/constants/ash_features.h"
  6. #include "ash/constants/ash_pref_names.h"
  7. #include "ash/session/session_controller_impl.h"
  8. #include "ash/shell.h"
  9. #include "ash/system/camera/autozoom_controller_impl.h"
  10. #include "ash/system/camera/autozoom_nudge.h"
  11. #include "base/json/values_util.h"
  12. #include "components/prefs/pref_registry_simple.h"
  13. #include "components/prefs/pref_service.h"
  14. #include "components/prefs/scoped_user_pref_update.h"
  15. namespace ash {
  16. namespace {
  17. // Keys for autozoom nudge sub-preferences for shown count and last time shown.
  18. constexpr char kShownCount[] = "shown_count";
  19. constexpr char kLastTimeShown[] = "last_time_shown";
  20. constexpr char kHadEnabled[] = "had_enabled";
  21. constexpr int kNotificationLimit = 3;
  22. constexpr base::TimeDelta kMinInterval = base::Days(1);
  23. } // namespace
  24. AutozoomNudgeController::AutozoomNudgeController(
  25. AutozoomControllerImpl* autozoom_controller)
  26. : autozoom_controller_(autozoom_controller) {
  27. autozoom_controller_->AddObserver(this);
  28. if (base::FeatureList::IsEnabled(features::kAutozoomNudgeSessionReset))
  29. Shell::Get()->session_controller()->AddObserver(this);
  30. }
  31. AutozoomNudgeController::~AutozoomNudgeController() {
  32. autozoom_controller_->RemoveObserver(this);
  33. if (base::FeatureList::IsEnabled(features::kAutozoomNudgeSessionReset))
  34. Shell::Get()->session_controller()->RemoveObserver(this);
  35. }
  36. // static
  37. void AutozoomNudgeController::RegisterProfilePrefs(
  38. PrefRegistrySimple* registry) {
  39. registry->RegisterDictionaryPref(prefs::kAutozoomNudges);
  40. }
  41. void AutozoomNudgeController::OnActiveUserPrefServiceChanged(
  42. PrefService* prefs) {
  43. // Reset the nudge prefs so that the nudge can be shown again. This observer
  44. // callback is only registered and called when
  45. // features::kAutozoomNudgeSessionReset is enabled.
  46. DictionaryPrefUpdate update(prefs, prefs::kAutozoomNudges);
  47. update->SetIntPath(kShownCount, 0);
  48. update->SetPath(kLastTimeShown, base::TimeToValue(base::Time()));
  49. update->SetBoolPath(kHadEnabled, false);
  50. }
  51. base::Time AutozoomNudgeController::GetTime() {
  52. return base::Time::Now();
  53. }
  54. void AutozoomNudgeController::HandleNudgeShown() {
  55. PrefService* prefs =
  56. Shell::Get()->session_controller()->GetLastActiveUserPrefService();
  57. if (!prefs)
  58. return;
  59. const int shown_count = GetShownCount(prefs);
  60. DictionaryPrefUpdate update(prefs, prefs::kAutozoomNudges);
  61. update->SetIntPath(kShownCount, shown_count + 1);
  62. update->SetPath(kLastTimeShown, base::TimeToValue(GetTime()));
  63. }
  64. bool AutozoomNudgeController::GetHadEnabled(PrefService* prefs) {
  65. const base::Value* dictionary = prefs->GetDictionary(prefs::kAutozoomNudges);
  66. if (!dictionary)
  67. return false;
  68. return dictionary->FindBoolPath(kHadEnabled).value_or(false);
  69. }
  70. int AutozoomNudgeController::GetShownCount(PrefService* prefs) {
  71. const base::Value* dictionary = prefs->GetDictionary(prefs::kAutozoomNudges);
  72. if (!dictionary)
  73. return 0;
  74. return dictionary->FindIntPath(kShownCount).value_or(0);
  75. }
  76. base::Time AutozoomNudgeController::GetLastShownTime(PrefService* prefs) {
  77. const base::Value* dictionary = prefs->GetDictionary(prefs::kAutozoomNudges);
  78. if (!dictionary)
  79. return base::Time();
  80. absl::optional<base::Time> last_shown_time =
  81. base::ValueToTime(dictionary->FindPath(kLastTimeShown));
  82. return last_shown_time.value_or(base::Time());
  83. }
  84. bool AutozoomNudgeController::ShouldShowNudge(PrefService* prefs) {
  85. if (!prefs)
  86. return false;
  87. bool had_enabled = GetHadEnabled(prefs);
  88. // We should not show more nudge after user had enabled autozoom before.
  89. if (had_enabled)
  90. return false;
  91. int nudge_shown_count = GetShownCount(prefs);
  92. // We should not show more nudges after hitting the limit.
  93. if (nudge_shown_count >= kNotificationLimit)
  94. return false;
  95. base::Time last_shown_time = GetLastShownTime(prefs);
  96. // If the nudge has yet to be shown, we should return true.
  97. if (last_shown_time.is_null())
  98. return true;
  99. // We should show the nudge if enough time has passed since the nudge was last
  100. // shown.
  101. return (base::Time::Now() - last_shown_time) > kMinInterval;
  102. }
  103. void AutozoomNudgeController::OnAutozoomControlEnabledChanged(bool enabled) {
  104. if (!enabled)
  105. return;
  106. PrefService* prefs =
  107. Shell::Get()->session_controller()->GetLastActiveUserPrefService();
  108. if (!ShouldShowNudge(prefs))
  109. return;
  110. ShowNudge();
  111. HandleNudgeShown();
  112. }
  113. void AutozoomNudgeController::OnAutozoomStateChanged(
  114. cros::mojom::CameraAutoFramingState state) {
  115. if (state != cros::mojom::CameraAutoFramingState::OFF) {
  116. PrefService* prefs =
  117. Shell::Get()->session_controller()->GetLastActiveUserPrefService();
  118. if (!prefs)
  119. return;
  120. DictionaryPrefUpdate update(prefs, prefs::kAutozoomNudges);
  121. update->SetBoolPath(kHadEnabled, true);
  122. }
  123. }
  124. std::unique_ptr<SystemNudge> AutozoomNudgeController::CreateSystemNudge() {
  125. return std::make_unique<AutozoomNudge>();
  126. }
  127. } // namespace ash