dark_light_mode_nudge_controller.cc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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/style/dark_light_mode_nudge_controller.h"
  5. #include "ash/constants/ash_pref_names.h"
  6. #include "ash/constants/ash_switches.h"
  7. #include "ash/session/session_controller_impl.h"
  8. #include "ash/shell.h"
  9. #include "ash/style/dark_light_mode_controller_impl.h"
  10. #include "ash/style/dark_light_mode_nudge.h"
  11. #include "base/command_line.h"
  12. #include "chromeos/constants/chromeos_features.h"
  13. #include "components/prefs/pref_service.h"
  14. #include "components/prefs/scoped_user_pref_update.h"
  15. namespace ash {
  16. namespace {
  17. PrefService* GetActiveUserPrefService() {
  18. return DarkLightModeControllerImpl::Get()->active_user_pref_service();
  19. }
  20. void SetRemainingShownCount(int count) {
  21. PrefService* prefs = GetActiveUserPrefService();
  22. if (prefs)
  23. prefs->SetInteger(prefs::kDarkLightModeNudge, count);
  24. }
  25. } // namespace
  26. DarkLightModeNudgeController::DarkLightModeNudgeController() = default;
  27. DarkLightModeNudgeController::~DarkLightModeNudgeController() = default;
  28. // static
  29. int DarkLightModeNudgeController::GetRemainingShownCount() {
  30. const PrefService* prefs = GetActiveUserPrefService();
  31. return prefs ? prefs->GetInteger(prefs::kDarkLightModeNudge) : 0;
  32. }
  33. void DarkLightModeNudgeController::MaybeShowNudge() {
  34. if (!ShouldShowNudge())
  35. return;
  36. const int shown_count = GetRemainingShownCount();
  37. ShowNudge();
  38. SetRemainingShownCount(shown_count - 1);
  39. }
  40. void DarkLightModeNudgeController::ToggledByUser() {
  41. SetRemainingShownCount(0);
  42. }
  43. std::unique_ptr<SystemNudge> DarkLightModeNudgeController::CreateSystemNudge() {
  44. return std::make_unique<DarkLightModeNudge>();
  45. }
  46. bool DarkLightModeNudgeController::ShouldShowNudge() const {
  47. if (!chromeos::features::IsDarkLightModeEnabled())
  48. return false;
  49. // Do not show the nudge if it is set to be hidden in the tests.
  50. if (hide_nudge_for_testing_)
  51. return false;
  52. // Do not show if the command line flag to hide nudges is set.
  53. if (base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshNoNudges))
  54. return false;
  55. auto* session_controller = Shell::Get()->session_controller();
  56. if (!session_controller->IsActiveUserSessionStarted())
  57. return false;
  58. absl::optional<user_manager::UserType> user_type =
  59. session_controller->GetUserType();
  60. // Must have a `user_type` because of the active user session check above.
  61. DCHECK(user_type);
  62. switch (*user_type) {
  63. case user_manager::USER_TYPE_REGULAR:
  64. case user_manager::USER_TYPE_CHILD:
  65. // We only allow regular and child accounts to see the nudge.
  66. break;
  67. case user_manager::USER_TYPE_GUEST:
  68. case user_manager::USER_TYPE_PUBLIC_ACCOUNT:
  69. case user_manager::USER_TYPE_KIOSK_APP:
  70. case user_manager::USER_TYPE_ARC_KIOSK_APP:
  71. case user_manager::USER_TYPE_WEB_KIOSK_APP:
  72. case user_manager::USER_TYPE_ACTIVE_DIRECTORY:
  73. case user_manager::NUM_USER_TYPES:
  74. return false;
  75. }
  76. return GetRemainingShownCount() > 0;
  77. }
  78. } // namespace ash