1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101

ash / style / dark_light_mode_controller_impl.h [blame]

// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef ASH_STYLE_DARK_LIGHT_MODE_CONTROLLER_IMPL_H_
#define ASH_STYLE_DARK_LIGHT_MODE_CONTROLLER_IMPL_H_

#include "ash/ash_export.h"
#include "ash/login/ui/login_data_dispatcher.h"
#include "ash/public/cpp/login_types.h"
#include "ash/public/cpp/style/dark_light_mode_controller.h"
#include "ash/public/cpp/wallpaper/wallpaper_controller_observer.h"
#include "ash/system/scheduled_feature/scheduled_feature.h"
#include "base/memory/raw_ptr.h"
#include "base/observer_list.h"

class AccountId;
class PrefChangeRegistrar;
class PrefRegistrySimple;
class PrefService;

namespace ash {

class ColorModeObserver;

// Controls the behavior of dark/light mode. Turns on the dark mode at sunset
// and off at sunrise if auto schedule is set (custom start and end for
// scheduling is not supported).
class ASH_EXPORT DarkLightModeControllerImpl
    : public DarkLightModeController,
      public LoginDataDispatcher::Observer,
      public ScheduledFeature {
 public:
  DarkLightModeControllerImpl();
  DarkLightModeControllerImpl(const DarkLightModeControllerImpl&) = delete;
  DarkLightModeControllerImpl& operator=(const DarkLightModeControllerImpl&) =
      delete;
  ~DarkLightModeControllerImpl() override;

  static DarkLightModeControllerImpl* Get();

  static void RegisterProfilePrefs(PrefRegistrySimple* registry);

  // Enables or disables auto scheduling on dark mode feature. When enabled,
  // the dark mode will automatically turn on during sunset to sunrise and off
  // outside that period.
  void SetAutoScheduleEnabled(bool enabled);

  // True if dark mode is automatically scheduled to turn on at sunset and off
  // at sunrise.
  bool GetAutoScheduleEnabled() const;

  // Toggles pref |kDarkModeEnabled|.
  void ToggleColorMode();

  // DarkLightModeController:
  void AddObserver(ColorModeObserver* observer) override;
  void RemoveObserver(ColorModeObserver* observer) override;
  bool IsDarkModeEnabled() const override;
  void SetDarkModeEnabledForTest(bool enabled) override;

  // LoginDataDispatcher::Observer:
  void OnOobeDialogStateChanged(OobeDialogState state) override;
  void OnFocusPod(const AccountId& account_id) override;

  // ScheduledFeature:
  void OnActiveUserPrefServiceChanged(PrefService* prefs) override;
  void OnSessionStateChanged(session_manager::SessionState state) override;

 private:
  // ScheduledFeature:
  const char* GetFeatureName() const override;

  // Notifies all the observers on color mode changes and refreshes the system's
  // colors on this change.
  void NotifyColorModeChanges();

  // Returns a closure which calls `NotifyIfDarkModeChanged` if the dark mode
  // changed between creation and getting out of scope.
  base::ScopedClosureRunner GetNotifyOnDarkModeChangeClosure();
  void NotifyIfDarkModeChanged(bool old_is_dark_mode_enabled);

  // Temporary field for testing purposes while OOBE WebUI is being migrated.
  std::optional<bool> is_dark_mode_enabled_in_oobe_for_testing_;

  OobeDialogState oobe_state_ = OobeDialogState::HIDDEN;

  // Keep track of the last value that was sent to avoid multiple notifications.
  std::optional<bool> last_value_;

  // std::nullopt in case no user pod is focused.
  std::optional<bool> is_dark_mode_enabled_for_focused_pod_;

  base::ObserverList<ColorModeObserver> observers_;
  std::unique_ptr<PrefChangeRegistrar> pref_change_registrar_;
  raw_ptr<PrefService> active_user_pref_service_ = nullptr;  // Not owned.
};

}  // namespace ash

#endif  // ASH_STYLE_DARK_LIGHT_MODE_CONTROLLER_IMPL_H_