adaptive_charging_nudge_controller.cc 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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/power/adaptive_charging_nudge_controller.h"
  5. #include <memory>
  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/power/adaptive_charging_nudge.h"
  10. #include "base/time/time.h"
  11. #include "base/timer/timer.h"
  12. #include "components/prefs/pref_service.h"
  13. namespace ash {
  14. namespace {
  15. // Delay time before the nudge should appear.
  16. constexpr base::TimeDelta kNudgeDelayTime = base::Seconds(3);
  17. } // namespace
  18. AdaptiveChargingNudgeController::AdaptiveChargingNudgeController() {}
  19. AdaptiveChargingNudgeController::~AdaptiveChargingNudgeController() {}
  20. void AdaptiveChargingNudgeController::ShowNudge() {
  21. PrefService* pref_service =
  22. Shell::Get()->session_controller()->GetActivePrefService();
  23. // Nudge should be shown only once for an user forever.
  24. if (!pref_service ||
  25. !pref_service->GetBoolean(ash::prefs::kPowerAdaptiveChargingEnabled) ||
  26. pref_service->GetBoolean(ash::prefs::kPowerAdaptiveChargingNudgeShown)) {
  27. return;
  28. }
  29. // Show nudge if the delay timer is complete.
  30. if (nudge_delay_timer_ && !nudge_delay_timer_->IsRunning()) {
  31. pref_service->SetBoolean(ash::prefs::kPowerAdaptiveChargingNudgeShown,
  32. true);
  33. SystemNudgeController::ShowNudge();
  34. return;
  35. }
  36. nudge_delay_timer_ = std::make_unique<base::OneShotTimer>();
  37. nudge_delay_timer_->Start(
  38. FROM_HERE, kNudgeDelayTime,
  39. base::BindOnce(&AdaptiveChargingNudgeController::ShowNudge,
  40. weak_ptr_factory_.GetWeakPtr()));
  41. }
  42. #if DCHECK_IS_ON()
  43. void AdaptiveChargingNudgeController::ShowNudgeForTesting() {
  44. SystemNudgeController::ShowNudge();
  45. }
  46. #endif // DCHECK_IS_ON()
  47. std::unique_ptr<SystemNudge>
  48. AdaptiveChargingNudgeController::CreateSystemNudge() {
  49. return std::make_unique<AdaptiveChargingNudge>();
  50. }
  51. } // namespace ash