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_WEB_APPLICATIONS_COMPONENTS_APP_ICON_MANAGER_H_
6 #define CHROME_BROWSER_WEB_APPLICATIONS_COMPONENTS_APP_ICON_MANAGER_H_
7 
8 #include <cstdint>
9 #include <map>
10 #include <vector>
11 
12 #include "base/callback_forward.h"
13 #include "base/optional.h"
14 #include "chrome/browser/web_applications/components/web_app_id.h"
15 #include "chrome/browser/web_applications/components/web_application_info.h"
16 #include "third_party/skia/include/core/SkBitmap.h"
17 
18 namespace web_app {
19 
20 // Icon bitmaps for each IconPurpose.
21 struct IconBitmaps {
22   IconBitmaps();
23   ~IconBitmaps();
24   IconBitmaps(const IconBitmaps&);
25   IconBitmaps(IconBitmaps&&) noexcept;
26   void SetBitmapsForPurpose(IconPurpose purpose,
27                             std::map<SquareSizePx, SkBitmap> bitmaps);
28   bool empty();
29 
30   std::map<SquareSizePx, SkBitmap> any;
31   std::map<SquareSizePx, SkBitmap> maskable;
32   // TODO (crbug.com/1114638): Monochrome support.
33 };
34 
35 // Exclusively used from the UI thread.
36 class AppIconManager {
37  public:
38   AppIconManager() = default;
39   AppIconManager(const AppIconManager&) = delete;
40   AppIconManager& operator=(const AppIconManager&) = delete;
41   virtual ~AppIconManager() = default;
42 
43   virtual void Start() = 0;
44   virtual void Shutdown() = 0;
45 
46   // Returns false if any icon in |icon_sizes_in_px| is missing from downloaded
47   // icons for a given app and |purpose|.
48   virtual bool HasIcons(const AppId& app_id,
49                         IconPurpose purpose,
50                         const SortedSizesPx& icon_sizes_in_px) const = 0;
51   struct IconSizeAndPurpose {
52     SquareSizePx size_px = 0;
53     IconPurpose purpose = IconPurpose::ANY;
54   };
55   // For each of |purposes|, in the given order, looks for an icon with size at
56   // least |min_icon_size|. Returns information on the first icon found.
57   virtual base::Optional<IconSizeAndPurpose> FindIconMatchBigger(
58       const AppId& app_id,
59       const std::vector<IconPurpose>& purposes,
60       SquareSizePx min_size) const = 0;
61   // Returns whether there is a downloaded icon of at least |min_size| for any
62   // of the given |purposes|.
63   virtual bool HasSmallestIcon(const AppId& app_id,
64                                const std::vector<IconPurpose>& purposes,
65                                SquareSizePx min_size) const = 0;
66 
67   using ReadIconsCallback =
68       base::OnceCallback<void(std::map<SquareSizePx, SkBitmap> icon_bitmaps)>;
69   // Reads specified icon bitmaps for an app and |purpose|. Returns empty map in
70   // |callback| if IO error.
71   virtual void ReadIcons(const AppId& app_id,
72                          IconPurpose purpose,
73                          const SortedSizesPx& icon_sizes,
74                          ReadIconsCallback callback) const = 0;
75 
76   using ReadShortcutsMenuIconsCallback = base::OnceCallback<void(
77       ShortcutsMenuIconsBitmaps shortcuts_menu_icons_bitmaps)>;
78 
79   // Reads bitmaps for all shortcuts menu icons for an app. Returns a vector of
80   // map<SquareSizePx, SkBitmap>. The index of a map in the vector is the same
81   // as that of its corresponding shortcut in the manifest's shortcuts vector.
82   // Returns empty vector in |callback| if we hit any error.
83   virtual void ReadAllShortcutsMenuIcons(
84       const AppId& app_id,
85       ReadShortcutsMenuIconsCallback callback) const = 0;
86 
87   // TODO (crbug.com/1102701): Callback with const ref instead of value.
88   using ReadIconBitmapsCallback =
89       base::OnceCallback<void(IconBitmaps icon_bitmaps)>;
90   // Reads all icon bitmaps for an app. Returns empty |icon_bitmaps| in
91   // |callback| if IO error.
92   virtual void ReadAllIcons(const AppId& app_id,
93                             ReadIconBitmapsCallback callback) const = 0;
94 
95   using ReadIconWithPurposeCallback =
96       base::OnceCallback<void(IconPurpose, const SkBitmap&)>;
97   // For each of |purposes|, in the given order, looks for an icon with size at
98   // least |min_icon_size|. Returns the first icon found, as a bitmap. Returns
99   // an empty SkBitmap in |callback| if IO error.
100   virtual void ReadSmallestIcon(const AppId& app_id,
101                                 const std::vector<IconPurpose>& purposes,
102                                 SquareSizePx min_icon_size,
103                                 ReadIconWithPurposeCallback callback) const = 0;
104 
105   using ReadIconCallback = base::OnceCallback<void(const SkBitmap&)>;
106   // Convenience method for |ReadSmallestIcon| with IconPurpose::ANY only.
107   void ReadSmallestIconAny(const AppId& app_id,
108                            SquareSizePx min_icon_size,
109                            ReadIconCallback callback) const;
110 
111   using ReadCompressedIconWithPurposeCallback =
112       base::OnceCallback<void(IconPurpose, std::vector<uint8_t> data)>;
113   // For each of |purposes|, in the given order, looks for an icon with size at
114   // least |min_icon_size|. Returns the first icon found, compressed as PNG.
115   // Returns empty |data| in |callback| if IO error.
116   virtual void ReadSmallestCompressedIcon(
117       const AppId& app_id,
118       const std::vector<IconPurpose>& purposes,
119       SquareSizePx min_icon_size,
120       ReadCompressedIconWithPurposeCallback callback) const = 0;
121 
122   using ReadCompressedIconCallback =
123       base::OnceCallback<void(std::vector<uint8_t> data)>;
124   // Convenience method for |ReadSmallestCompressedIcon| with IconPurpose::ANY
125   // only.
126   void ReadSmallestCompressedIconAny(const AppId& app_id,
127                                      SquareSizePx min_icon_size,
128                                      ReadCompressedIconCallback callback) const;
129 
130   // Returns a square icon of gfx::kFaviconSize px, or an empty bitmap if not
131   // found.
132   virtual SkBitmap GetFavicon(const AppId& app_id) const = 0;
133 
134  protected:
135   static void WrapReadIconWithPurposeCallback(
136       ReadIconWithPurposeCallback callback,
137       IconPurpose purpose,
138       const SkBitmap& bitmap);
139 
140 };
141 
142 }  // namespace web_app
143 
144 #endif  // CHROME_BROWSER_WEB_APPLICATIONS_COMPONENTS_APP_ICON_MANAGER_H_
145