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

ash / wallpaper / online_wallpaper_manager.h [blame]

// Copyright 2023 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_WALLPAPER_ONLINE_WALLPAPER_MANAGER_H_
#define ASH_WALLPAPER_ONLINE_WALLPAPER_MANAGER_H_

#include <vector>

#include "ash/ash_export.h"
#include "ash/public/cpp/wallpaper/online_wallpaper_variant.h"
#include "ash/public/cpp/wallpaper/wallpaper_info.h"
#include "ash/public/cpp/wallpaper/wallpaper_types.h"
#include "ash/wallpaper/wallpaper_file_manager.h"
#include "ash/wallpaper/wallpaper_utils/wallpaper_resolution.h"
#include "base/files/file_path.h"
#include "base/functional/callback_forward.h"
#include "base/memory/weak_ptr.h"
#include "components/account_id/account_id.h"
#include "ui/gfx/image/image_skia.h"
#include "url/gurl.h"

namespace ash {

class WallpaperImageDownloader;

// Handles loading and saving online wallpaper images for WallpaperController.
class ASH_EXPORT OnlineWallpaperManager {
 public:
  explicit OnlineWallpaperManager(
      WallpaperImageDownloader* wallpaper_image_downloader,
      WallpaperFileManager* wallpaper_file_manager);

  OnlineWallpaperManager(const OnlineWallpaperManager&) = delete;
  OnlineWallpaperManager& operator=(const OnlineWallpaperManager&) = delete;

  ~OnlineWallpaperManager();

  using LoadOnlineWallpaperCallback =
      base::OnceCallback<void(const gfx::ImageSkia&)>;
  // Attempts to load the wallpaper from the disk first. Instead of failing if
  // loading the wallpaper is unsuccessful, it tries to download the wallpaper
  // along with other wallpapers in `wallpaper_info.variants` over the network.
  void GetOnlineWallpaper(const base::FilePath& wallpaper_dir,
                          const AccountId& account_id,
                          const WallpaperInfo& wallpaper_info,
                          LoadOnlineWallpaperCallback callback);

 private:
  void OnLoadExistingOnlineWallpaperComplete(
      const base::FilePath& wallpaper_dir,
      const AccountId& account_id,
      const WallpaperInfo& wallpaper_info,
      LoadOnlineWallpaperCallback callback,
      const gfx::ImageSkia& image);

  void DownloadAndSaveAllVariants(const base::FilePath& wallpaper_dir,
                                  const AccountId& account_id,
                                  const WallpaperInfo& wallpaper_info,
                                  LoadOnlineWallpaperCallback callback);

  // Just bundles together the ultimate output needed in
  // OnAllVariantsDownloaded(). Gets filled in incrementally as each individual
  // variant is downloaded.
  struct VariantsDownloadResult {
    // Filled with the variant that was requested by the caller and is needed
    // immediately.
    gfx::ImageSkia target_variant;
    bool any_downloads_failed = false;
  };
  void OnAllVariantsDownloaded(
      std::unique_ptr<VariantsDownloadResult> downloads_result,
      LoadOnlineWallpaperCallback callback);

  void OnVariantDownloaded(WallpaperType type,
                           const base::FilePath& wallpaper_dir,
                           const GURL& variant_url,
                           WallpaperLayout layout,
                           bool is_target_variant,
                           VariantsDownloadResult* downloads_result,
                           base::RepeatingClosure on_done,
                           const gfx::ImageSkia& image);

  raw_ptr<WallpaperImageDownloader> wallpaper_image_downloader_;

  raw_ptr<WallpaperFileManager> wallpaper_file_manager_;

  base::WeakPtrFactory<OnlineWallpaperManager> weak_factory_{this};
};

}  // namespace ash

#endif  // ASH_WALLPAPER_ONLINE_WALLPAPER_MANAGER_H_