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
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141

ash / ambient / ambient_backup_photo_downloader.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/ambient/ambient_backup_photo_downloader.h"

#include <utility>
#include <vector>

#include "ash/ambient/ambient_controller.h"
#include "ash/ambient/ambient_photo_cache.h"
#include "ash/public/cpp/ambient/proto/photo_cache_entry.pb.h"
#include "ash/public/cpp/image_util.h"
#include "ash/shell.h"
#include "base/check.h"
#include "base/files/file_util.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/image/image_skia_operations.h"
#include "ui/gfx/image/image_util.h"

namespace ash {

namespace {

// TODO(b/304527743): Ask the server to resize the photo for us if the backend
// can make the appropriate config changes.
std::vector<unsigned char> ResizeAndEncode(const gfx::ImageSkia& image,
                                           gfx::Size target_size) {
  static constexpr int kJpegEncodingQuality = 95;
  // Only shrink images, and keep the original aspect ratio.
  gfx::Size original_size = image.size();
  if (target_size.width() < original_size.width() &&
      target_size.height() < original_size.height()) {
    float width_scale =
        static_cast<float>(target_size.width()) / original_size.width();
    float height_scale =
        static_cast<float>(target_size.height()) / original_size.height();
    target_size = gfx::ScaleToRoundedSize(original_size,
                                          std::max(width_scale, height_scale));
  } else {
    target_size = original_size;
  }

  gfx::ImageSkia resized_image = gfx::ImageSkiaOperations::CreateResizedImage(
      image, skia::ImageOperations::RESIZE_BETTER, target_size);
  CHECK(!resized_image.isNull());

  std::optional<std::vector<uint8_t>> encoded_image =
      gfx::JPEG1xEncodedDataFromImage(gfx::Image(resized_image),
                                      kJpegEncodingQuality);
  return encoded_image.value_or(std::vector<uint8_t>());
}

}  // namespace

AmbientBackupPhotoDownloader::AmbientBackupPhotoDownloader(
    AmbientAccessTokenController& access_token_controller,
    int cache_idx,
    gfx::Size target_size,
    const std::string& url,
    base::OnceCallback<void(bool success)> completion_cb)
    : cache_idx_(cache_idx),
      target_size_(target_size),
      file_task_runner_(
          base::ThreadPool::CreateSequencedTaskRunner({base::MayBlock()})),
      completion_cb_(std::move(completion_cb)) {
  // Since the photo is large, downloading to an in-memory string exceeds the
  // response size limits imposed by `SimpleUrLoader`. Downloading to a
  // temporary file is recommended instead.
  ambient_photo_cache::DownloadPhotoToTempFile(
      url, access_token_controller,
      base::BindOnce(&AmbientBackupPhotoDownloader::DecodeImage,
                     weak_factory_.GetWeakPtr()));
}

AmbientBackupPhotoDownloader::~AmbientBackupPhotoDownloader() {
  if (!temp_image_path_.empty()) {
    // The same `file_task_runner_` that reads/decodes the file is used to
    // delete the file. Prevents unpredictable behavior if the call to
    // `image_util::DecodeImageFile()` below is pending when this class is
    // destroyed.
    file_task_runner_->PostTask(FROM_HERE,
                                base::GetDeleteFileCallback(temp_image_path_));
  }
}

void AmbientBackupPhotoDownloader::RunCompletionCallback(bool success) {
  CHECK(completion_cb_);
  std::move(completion_cb_).Run(success);
}

void AmbientBackupPhotoDownloader::DecodeImage(base::FilePath temp_image_path) {
  if (temp_image_path.empty()) {
    RunCompletionCallback(false);
    return;
  }
  temp_image_path_ = std::move(temp_image_path);
  image_util::DecodeImageFile(
      base::BindOnce(&AmbientBackupPhotoDownloader::ScheduleResizeAndEncode,
                     weak_factory_.GetWeakPtr()),
      temp_image_path_, data_decoder::mojom::ImageCodec::kDefault,
      file_task_runner_);
}

void AmbientBackupPhotoDownloader::ScheduleResizeAndEncode(
    const gfx::ImageSkia& decoded_image) {
  if (decoded_image.isNull()) {
    RunCompletionCallback(false);
    return;
  }
  // `ResizeAndEncode()` can be a computationally expensive operation, so run it
  // on a blocking thread in the thread pool to prevent blocking the main
  // thread.
  base::ThreadPool::PostTaskAndReplyWithResult(
      FROM_HERE, {base::MayBlock()},
      base::BindOnce(&ResizeAndEncode, decoded_image, target_size_),
      base::BindOnce(&AmbientBackupPhotoDownloader::SaveImage,
                     weak_factory_.GetWeakPtr()));
}

void AmbientBackupPhotoDownloader::SaveImage(
    const std::vector<unsigned char>& encoded_image) {
  if (encoded_image.empty()) {
    RunCompletionCallback(false);
    return;
  }
  ::ambient::PhotoCacheEntry cache_entry;
  cache_entry.mutable_primary_photo()->mutable_image()->assign(
      reinterpret_cast<const char*>(encoded_image.data()),
      encoded_image.size());
  ambient_photo_cache::WritePhotoCache(
      ambient_photo_cache::Store::kBackup, cache_idx_, cache_entry,
      base::BindOnce(&AmbientBackupPhotoDownloader::RunCompletionCallback,
                     weak_factory_.GetWeakPtr(), true));
}

}  // namespace ash