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 / wallpaper / google_photos_wallpaper_manager.cc [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.

#include "ash/wallpaper/google_photos_wallpaper_manager.h"

#include "ash/shell.h"
#include "ash/wallpaper/wallpaper_constants.h"
#include "ash/wallpaper/wallpaper_image_downloader.h"
#include "ash/wallpaper/wallpaper_utils/wallpaper_file_utils.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "components/account_id/account_id.h"
#include "ui/gfx/image/image.h"

namespace ash {

GooglePhotosWallpaperManager::GooglePhotosWallpaperManager(
    WallpaperImageDownloader* wallpaper_image_downloader,
    WallpaperFileManager* wallpaper_file_manager)
    : wallpaper_image_downloader_(wallpaper_image_downloader),
      wallpaper_file_manager_(wallpaper_file_manager) {}

GooglePhotosWallpaperManager::~GooglePhotosWallpaperManager() = default;

void GooglePhotosWallpaperManager::SetClient(
    WallpaperControllerClient* client) {
  wallpaper_controller_client_ = client;
}

void GooglePhotosWallpaperManager::GetGooglePhotosWallpaper(
    const base::FilePath& wallpaper_dir,
    const GooglePhotosWallpaperParams& params,
    ash::personalization_app::mojom::GooglePhotosPhotoPtr photo,
    LoadGooglePhotosWallpaperCallback callback) {
  const std::string location = photo->id;
  auto on_load = base::BindOnce(&GooglePhotosWallpaperManager::
                                    OnLoadExistingGooglePhotosWallpaperComplete,
                                weak_factory_.GetWeakPtr(), wallpaper_dir,
                                params, std::move(photo), std::move(callback));
  wallpaper_file_manager_->LoadWallpaper(
      params.daily_refresh_enabled ? WallpaperType::kDailyGooglePhotos
                                   : WallpaperType::kOnceGooglePhotos,
      wallpaper_dir, location, std::move(on_load));
}

void GooglePhotosWallpaperManager::DownloadGooglePhotosWallpaper(
    ash::personalization_app::mojom::GooglePhotosPhotoPtr photo,
    const AccountId& account_id,
    LoadGooglePhotosWallpaperCallback callback) {
  wallpaper_controller_client_->FetchGooglePhotosAccessToken(
      account_id, base::BindOnce(&GooglePhotosWallpaperManager::
                                     OnGooglePhotosAuthenticationTokenFetched,
                                 weak_factory_.GetWeakPtr(), std::move(photo),
                                 account_id, std::move(callback)));
}

void GooglePhotosWallpaperManager::OnGooglePhotosAuthenticationTokenFetched(
    ash::personalization_app::mojom::GooglePhotosPhotoPtr photo,
    const AccountId& account_id,
    LoadGooglePhotosWallpaperCallback callback,
    const std::optional<std::string>& access_token) {
  wallpaper_image_downloader_->DownloadGooglePhotosImage(
      photo->url, account_id, access_token, std::move(callback));
}

void GooglePhotosWallpaperManager::OnGooglePhotosWallpaperDownloaded(
    const WallpaperType type,
    const base::FilePath& wallpaper_dir,
    const std::string& photo_id,
    const WallpaperLayout layout,
    LoadGooglePhotosWallpaperCallback callback,
    const gfx::ImageSkia& image) {
  wallpaper_file_manager_->SaveWallpaperToDisk(type, wallpaper_dir, photo_id,
                                               layout, image);
  std::move(callback).Run(image);
}

void GooglePhotosWallpaperManager::OnLoadExistingGooglePhotosWallpaperComplete(
    const base::FilePath& wallpaper_dir,
    const GooglePhotosWallpaperParams& params,
    ash::personalization_app::mojom::GooglePhotosPhotoPtr photo,
    LoadGooglePhotosWallpaperCallback callback,
    const gfx::ImageSkia& image) {
  DCHECK(callback);
  if (image.isNull()) {
    auto on_download = base::BindOnce(
        &GooglePhotosWallpaperManager::OnGooglePhotosWallpaperDownloaded,
        weak_factory_.GetWeakPtr(),
        params.daily_refresh_enabled ? WallpaperType::kDailyGooglePhotos
                                     : WallpaperType::kOnceGooglePhotos,
        wallpaper_dir, photo->id, params.layout, std::move(callback));
    DownloadGooglePhotosWallpaper(std::move(photo), params.account_id,
                                  std::move(on_download));
  } else {
    std::move(callback).Run(image);
  }
}

}  // namespace ash