wallpaper_pref_manager.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. #ifndef ASH_WALLPAPER_WALLPAPER_PREF_MANAGER_H_
  5. #define ASH_WALLPAPER_WALLPAPER_PREF_MANAGER_H_
  6. #include <string>
  7. #include "ash/ash_export.h"
  8. #include "ash/public/cpp/session/session_observer.h"
  9. #include "ash/public/cpp/style/color_mode_observer.h"
  10. #include "ash/public/cpp/wallpaper/wallpaper_controller.h"
  11. #include "ash/public/cpp/wallpaper/wallpaper_info.h"
  12. #include "base/containers/flat_map.h"
  13. #include "base/files/file_path.h"
  14. #include "base/memory/weak_ptr.h"
  15. #include "base/observer_list.h"
  16. #include "base/observer_list_types.h"
  17. #include "base/strings/string_piece.h"
  18. #include "base/time/time.h"
  19. #include "base/timer/wall_clock_timer.h"
  20. #include "components/account_id/account_id.h"
  21. class PrefService;
  22. class PrefRegistrySimple;
  23. namespace ash {
  24. class WallpaperControllerClient;
  25. // Interface that provides user profiles from an account id. Abstracts the
  26. // details of the PrefService from clients and makes testing easier.
  27. class WallpaperProfileHelper {
  28. public:
  29. virtual ~WallpaperProfileHelper() = default;
  30. virtual void SetClient(WallpaperControllerClient* client) = 0;
  31. // Returns the syncable pref service for the user with |id| if it's available.
  32. // Otherwise, returns null.
  33. virtual PrefService* GetUserPrefServiceSyncable(const AccountId& id) = 0;
  34. // Returns the AccountId for the currently active account.
  35. virtual AccountId GetActiveAccountId() const = 0;
  36. // Returns true iff wallpaper sync is enabled for |id|.
  37. virtual bool IsWallpaperSyncEnabled(const AccountId& id) const = 0;
  38. // Returns true if at least one user is logged in.
  39. virtual bool IsActiveUserSessionStarted() const = 0;
  40. // Returns true if the user should store data in memory only.
  41. virtual bool IsEphemeral(const AccountId& id) const = 0;
  42. };
  43. // Manages wallpaper preferences and tracks the currently configured wallpaper.
  44. class ASH_EXPORT WallpaperPrefManager
  45. : public base::SupportsWeakPtr<WallpaperPrefManager>,
  46. public ColorModeObserver,
  47. public SessionObserver {
  48. public:
  49. // Names of nodes with wallpaper info in |kUserWallpaperInfo| dictionary.
  50. static const char kNewWallpaperAssetIdNodeName[];
  51. static const char kNewWallpaperCollectionIdNodeName[];
  52. static const char kNewWallpaperDateNodeName[];
  53. static const char kNewWallpaperDedupKeyNodeName[];
  54. static const char kNewWallpaperLayoutNodeName[];
  55. static const char kNewWallpaperLocationNodeName[];
  56. static const char kNewWallpaperUserFilePathNodeName[];
  57. static const char kNewWallpaperTypeNodeName[];
  58. static const char kNewWallpaperUnitIdNodeName[];
  59. static const char kNewWallpaperVariantListNodeName[];
  60. // Names of nodes for the online wallpaper variant dictionary.
  61. static const char kOnlineWallpaperTypeNodeName[];
  62. static const char kOnlineWallpaperUrlNodeName[];
  63. static std::unique_ptr<WallpaperPrefManager> Create(PrefService* local_state);
  64. // Create a PrefManager where pref service retrieval can be modified through
  65. // |profile_helper|.
  66. static std::unique_ptr<WallpaperPrefManager> CreateForTesting(
  67. PrefService* local_state,
  68. std::unique_ptr<WallpaperProfileHelper> profile_helper);
  69. WallpaperPrefManager(const WallpaperPrefManager&) = delete;
  70. ~WallpaperPrefManager() override = default;
  71. static void RegisterLocalStatePrefs(PrefRegistrySimple* registry);
  72. static void RegisterProfilePrefs(PrefRegistrySimple* registry);
  73. virtual void SetClient(WallpaperControllerClient* client) = 0;
  74. // Retrieve the wallpaper preference value for |account_id| and use it to
  75. // populate |info|. Returns true if |info| was populated successfully.
  76. //
  77. // NOTE: WallpaperPrefManager does not enforce any checks for policy
  78. // enforcement. Callers must check that the user is allowed to commit the pref
  79. // change.
  80. virtual bool GetUserWallpaperInfo(const AccountId& account_id,
  81. WallpaperInfo* info) const = 0;
  82. virtual bool SetUserWallpaperInfo(const AccountId& account_id,
  83. const WallpaperInfo& info) = 0;
  84. // Overload for |GetUserWallpaperInfo| that allow callers to specify
  85. // whether |account_id| is ephemeral. Used for callers before signin has
  86. // occurred and |is_ephemeral| cannot be determined by session controller.
  87. virtual bool GetUserWallpaperInfo(const AccountId& account_id,
  88. bool is_ephemeral,
  89. WallpaperInfo* info) const = 0;
  90. // Overload for |SetUserWallpaperInfo| that allow callers to specify
  91. // whether |account_id| is ephemeral. Used for callers before signin has
  92. // occurred and |is_ephemeral| cannot be determined by session controller.
  93. virtual bool SetUserWallpaperInfo(const AccountId& account_id,
  94. bool is_ephemeral,
  95. const WallpaperInfo& info) = 0;
  96. // Remove the wallpaper entry for |account_id|.
  97. virtual void RemoveUserWallpaperInfo(const AccountId& account_id) = 0;
  98. virtual void CacheProminentColors(const AccountId& account_id,
  99. const std::vector<SkColor>& colors) = 0;
  100. virtual void RemoveProminentColors(const AccountId& account_id) = 0;
  101. // Returns the cached prominent colors for a wallpaper with `location` if
  102. // present.
  103. virtual absl::optional<std::vector<SkColor>> GetCachedProminentColors(
  104. const base::StringPiece location) const = 0;
  105. virtual void CacheKMeanColor(const AccountId& account_id,
  106. SkColor k_mean_color) = 0;
  107. // Returns the cached KMeans color value for the wallpaper at `location`.
  108. virtual absl::optional<SkColor> GetCachedKMeanColor(
  109. const base::StringPiece location) const = 0;
  110. virtual void RemoveKMeanColor(const AccountId& account_id) = 0;
  111. virtual bool SetDailyGooglePhotosWallpaperIdCache(
  112. const AccountId& account_id,
  113. const WallpaperController::DailyGooglePhotosIdCache& ids) = 0;
  114. virtual bool GetDailyGooglePhotosWallpaperIdCache(
  115. const AccountId& account_id,
  116. WallpaperController::DailyGooglePhotosIdCache& ids_out) const = 0;
  117. // Get/Set the wallpaper info in local storage.
  118. // TODO(crbug.com/1298586): Remove these functions from the interface when
  119. // this can be abstracted from callers.
  120. virtual bool GetLocalWallpaperInfo(const AccountId& account_id,
  121. WallpaperInfo* info) const = 0;
  122. virtual bool SetLocalWallpaperInfo(const AccountId& account_id,
  123. const WallpaperInfo& info) = 0;
  124. // Get/Set the wallpaper info from synced prefs.
  125. // TODO(crbug.com/1298586): Remove these functions from the interface when
  126. // this can be abstracted from callers.
  127. virtual bool GetSyncedWallpaperInfo(const AccountId& account_id,
  128. WallpaperInfo* info) const = 0;
  129. virtual bool SetSyncedWallpaperInfo(const AccountId& account_id,
  130. const WallpaperInfo& info) = 0;
  131. // Returns the delta for the next daily refresh update for `account_id`.
  132. virtual base::TimeDelta GetTimeToNextDailyRefreshUpdate(
  133. const AccountId& account_id) const = 0;
  134. protected:
  135. WallpaperPrefManager() = default;
  136. };
  137. } // namespace ash
  138. #endif // ASH_WALLPAPER_WALLPAPER_PREF_MANAGER_H_