[go: nahoru, domu]

blob: 42c945f2ce18576dce8674832e97bb2288ca87d8 [file] [log] [blame]
Eric Sum183a8de2023-12-15 19:46:091// Copyright 2023 The Chromium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef ASH_AMBIENT_AMBIENT_BACKUP_PHOTO_DOWNLOADER_H_
6#define ASH_AMBIENT_AMBIENT_BACKUP_PHOTO_DOWNLOADER_H_
7
8#include <string>
9#include <vector>
10
11#include "ash/ash_export.h"
12#include "base/files/file_path.h"
13#include "base/functional/callback.h"
14#include "base/memory/scoped_refptr.h"
15#include "base/memory/weak_ptr.h"
16#include "ui/gfx/geometry/size.h"
17
18namespace base {
19class SequencedTaskRunner;
20} // namespace base
21
22namespace gfx {
23class ImageSkia;
24} // namespace gfx
25
26namespace ash {
27
28class AmbientAccessTokenController;
29
30// Downloads a "backup" image from a fixed url, resizes it to roughly match a
31// target size provided by the caller, and saves it to disc. After it's
32// downloaded, the backup image may be used in future ambient sessions if there
33// are issues fetching the primary stream of photos from the backend.
34//
35// `AmbientBackupPhotoDownloader` only downloads/saves one photo in its lifetime
36// and runs the `completion_cb` when done. It may be destroyed at any point to
37// stop whatever task it's running internally, and the `completion_cb` will not
38// be run.
39class ASH_EXPORT AmbientBackupPhotoDownloader {
40 public:
41 AmbientBackupPhotoDownloader(
42 AmbientAccessTokenController& access_token_controller,
43 int cache_idx,
44 gfx::Size target_size,
45 const std::string& url,
46 base::OnceCallback<void(bool success)> completion_cb);
47 AmbientBackupPhotoDownloader(const AmbientBackupPhotoDownloader&) = delete;
48 AmbientBackupPhotoDownloader& operator=(const AmbientBackupPhotoDownloader&) =
49 delete;
50 ~AmbientBackupPhotoDownloader();
51
52 private:
53 void RunCompletionCallback(bool success);
54 void DecodeImage(base::FilePath temp_image_path);
55 void ScheduleResizeAndEncode(const gfx::ImageSkia& decoded_image);
56 void SaveImage(const std::vector<unsigned char>& encoded_image);
57
58 const int cache_idx_;
59 const gfx::Size target_size_;
60 const scoped_refptr<base::SequencedTaskRunner> file_task_runner_;
61 base::OnceCallback<void(bool success)> completion_cb_;
62 // Temporary location where the image is downloaded first before it is
63 // resized and then saved to the final destination.
64 base::FilePath temp_image_path_;
65 base::WeakPtrFactory<AmbientBackupPhotoDownloader> weak_factory_{this};
66};
67
68} // namespace ash
69
70#endif // ASH_AMBIENT_AMBIENT_BACKUP_PHOTO_DOWNLOADER_H_