1 // Copyright 2018 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 
5 #ifndef CHROME_BROWSER_CHROMEOS_BACKDROP_WALLPAPER_HANDLERS_BACKDROP_WALLPAPER_HANDLERS_H_
6 #define CHROME_BROWSER_CHROMEOS_BACKDROP_WALLPAPER_HANDLERS_BACKDROP_WALLPAPER_HANDLERS_H_
7 
8 #include <memory>
9 
10 #include "base/callback.h"
11 #include "base/macros.h"
12 #include "services/network/public/cpp/simple_url_loader.h"
13 
14 namespace backdrop {
15 class Collection;
16 class Image;
17 }  // namespace backdrop
18 
19 namespace backdrop_wallpaper_handlers {
20 
21 class BackdropFetcher;
22 
23 // Downloads the wallpaper collections info from the Backdrop service.
24 class CollectionInfoFetcher {
25  public:
26   using OnCollectionsInfoFetched = base::OnceCallback<
27       void(bool success, const std::vector<backdrop::Collection>& collections)>;
28 
29   CollectionInfoFetcher();
30   ~CollectionInfoFetcher();
31 
32   // Starts the fetcher.
33   void Start(OnCollectionsInfoFetched callback);
34 
35  private:
36   // Called when the collections info download completes.
37   void OnResponseFetched(const std::string& response);
38 
39   // Used to download the proto from the Backdrop service.
40   std::unique_ptr<BackdropFetcher> backdrop_fetcher_;
41 
42   // The callback upon completion of downloading and deserializing the
43   // collections info.
44   OnCollectionsInfoFetched callback_;
45 
46   DISALLOW_COPY_AND_ASSIGN(CollectionInfoFetcher);
47 };
48 
49 // Downloads the wallpaper images info from the Backdrop service.
50 class ImageInfoFetcher {
51  public:
52   using OnImagesInfoFetched =
53       base::OnceCallback<void(bool success,
54                               const std::vector<backdrop::Image>& images)>;
55 
56   explicit ImageInfoFetcher(const std::string& collection_id);
57   ~ImageInfoFetcher();
58 
59   // Starts the fetcher.
60   void Start(OnImagesInfoFetched callback);
61 
62  private:
63   // Called when the images info download completes.
64   void OnResponseFetched(const std::string& response);
65 
66   // Used to download the proto from the Backdrop service.
67   std::unique_ptr<BackdropFetcher> backdrop_fetcher_;
68 
69   // The id of the collection, used as the token to fetch the images info.
70   const std::string collection_id_;
71 
72   // The callback upon completion of downloading and deserializing the images
73   // info.
74   OnImagesInfoFetched callback_;
75 
76   DISALLOW_COPY_AND_ASSIGN(ImageInfoFetcher);
77 };
78 
79 // Downloads the surprise me image info from the Backdrop service.
80 class SurpriseMeImageFetcher {
81  public:
82   using OnSurpriseMeImageFetched =
83       base::OnceCallback<void(bool success,
84                               const backdrop::Image& image,
85                               const std::string& new_resume_token)>;
86 
87   SurpriseMeImageFetcher(const std::string& collection_id,
88                          const std::string& resume_token);
89   ~SurpriseMeImageFetcher();
90 
91   // Starts the fetcher.
92   void Start(OnSurpriseMeImageFetched callback);
93 
94  private:
95   // Called when the surprise me image info download completes.
96   void OnResponseFetched(const std::string& response);
97 
98   // Used to download the proto from the Backdrop service.
99   std::unique_ptr<BackdropFetcher> backdrop_fetcher_;
100 
101   // The id of the collection, used as the token to fetch the image info.
102   const std::string collection_id_;
103 
104   // An opaque token returned by a previous image info fetch request. It is used
105   // to prevent duplicate images from being returned.
106   const std::string resume_token_;
107 
108   // The callback upon completion of downloading and deserializing the surprise
109   // me image info.
110   OnSurpriseMeImageFetched callback_;
111 
112   DISALLOW_COPY_AND_ASSIGN(SurpriseMeImageFetcher);
113 };
114 
115 }  // namespace backdrop_wallpaper_handlers
116 
117 #endif  // CHROME_BROWSER_CHROMEOS_BACKDROP_WALLPAPER_HANDLERS_BACKDROP_WALLPAPER_HANDLERS_H_
118