File: web_app_icon_manager.h

package info (click to toggle)
chromium 140.0.7339.80-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,193,064 kB
  • sloc: cpp: 35,092,386; ansic: 7,161,671; javascript: 4,199,703; python: 1,441,797; asm: 949,904; xml: 747,409; pascal: 187,748; perl: 88,691; sh: 88,248; objc: 79,953; sql: 52,714; cs: 44,599; fortran: 24,137; makefile: 22,114; tcl: 15,277; php: 13,980; yacc: 9,000; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (295 lines) | stat: -rw-r--r-- 12,400 bytes parent folder | download | duplicates (4)
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_WEB_APPLICATIONS_WEB_APP_ICON_MANAGER_H_
#define CHROME_BROWSER_WEB_APPLICATIONS_WEB_APP_ICON_MANAGER_H_

#include <stddef.h>
#include <stdint.h>

#include <map>
#include <memory>
#include <optional>
#include <string>
#include <vector>

#include "base/containers/flat_map.h"
#include "base/files/file_path.h"
#include "base/functional/callback.h"
#include "base/gtest_prod_util.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/scoped_observation.h"
#include "chrome/browser/web_applications/web_app_install_info.h"
#include "chrome/browser/web_applications/web_app_install_manager_observer.h"
#include "components/webapps/common/web_app_id.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/image/image_skia.h"

class Profile;

namespace base {
class SequencedTaskRunner;
class Time;
}  // namespace base

namespace web_app {

class WebAppInstallManager;
class WebAppProvider;

using HomeTabIconBitmaps = std::vector<SkBitmap>;
using SquareSizeDip = int;

// Exclusively used from the UI thread.
class WebAppIconManager : public WebAppInstallManagerObserver {
 public:
  using FaviconReadCallback =
      base::RepeatingCallback<void(const webapps::AppId& app_id)>;
  using ReadImageSkiaCallback =
      base::OnceCallback<void(gfx::ImageSkia image_skia)>;

  explicit WebAppIconManager(Profile* profile);
  WebAppIconManager(const WebAppIconManager&) = delete;
  WebAppIconManager& operator=(const WebAppIconManager&) = delete;
  ~WebAppIconManager() override;

  using WriteDataCallback = base::OnceCallback<void(bool success)>;

  // Writes all data (icons) for an app.
  void WriteData(webapps::AppId app_id,
                 IconBitmaps icon_bitmaps,
                 IconBitmaps trusted_icon_bitmaps,
                 ShortcutsMenuIconBitmaps shortcuts_menu_icons,
                 IconsMap other_icons_map,
                 WriteDataCallback callback);

  // Writes pending icon bitmaps for an app.
  void WritePendingIconData(webapps::AppId app_id,
                            IconBitmaps pending_trusted_icon_bitmaps,
                            IconBitmaps pending_manifest_icon_bitmaps,
                            WriteDataCallback callback);
  void DeleteData(webapps::AppId app_id, WriteDataCallback callback);

  void SetProvider(base::PassKey<WebAppProvider>, WebAppProvider& provider);
  void Start();
  void Shutdown();

  // Returns false if any icon in |icon_sizes_in_px| is missing from downloaded
  // icons for a given app and |purpose|.
  bool HasIcons(const webapps::AppId& app_id,
                IconPurpose purpose,
                const SortedSizesPx& icon_sizes) const;

  // Returns false if any icon in |icon_sizes_in_px| is missing from stored
  // trusted icons for a given app and |purpose|.
  bool HasTrustedIcons(const webapps::AppId& app_id,
                       IconPurpose purpose,
                       const SortedSizesPx& icon_sizes) const;

  struct IconSizeAndPurpose {
    SquareSizePx size_px = 0;
    IconPurpose purpose = IconPurpose::ANY;
  };

  // For each of |purposes|, in the given order, looks for an icon with size at
  // least |min_icon_size|. Returns information on the first icon found.
  std::optional<IconSizeAndPurpose> FindIconMatchBigger(
      const webapps::AppId& app_id,
      const std::vector<IconPurpose>& purposes,
      SquareSizePx min_size) const;
  // Returns whether there is a downloaded icon of at least |min_size| for any
  // of the given |purposes|.
  bool HasSmallestIcon(const webapps::AppId& app_id,
                       const std::vector<IconPurpose>& purposes,
                       SquareSizePx min_size) const;

  using ReadIconsCallback =
      base::OnceCallback<void(std::map<SquareSizePx, SkBitmap> icon_bitmaps)>;

  // Reads specified icon bitmaps for an app and |purpose|. These icons are
  // downloaded directly from the manifest and are not always surfaced to the
  // end user, which is why they are untrusted. Returns empty map in |callback|
  // if IO error.
  void ReadUntrustedIcons(const webapps::AppId& app_id,
                          IconPurpose purpose,
                          const SortedSizesPx& icon_sizes,
                          ReadIconsCallback callback);

  // Reads the bitmaps for the trusted icon for an app of sizes specified in
  // `icon_sizes`. Returns empty map in `callback` if an IO error happens.
  // The `purpose_for_fallback` information is used as a fallback to read the
  // icon bitmaps obtained from the manifest, mimicking the behavior of
  // ReadUntrustedIcons().
  void ReadTrustedIconsWithFallbackToManifestIcons(
      const webapps::AppId& app_id,
      const SortedSizesPx& icon_sizes,
      IconPurpose purpose_for_fallback,
      ReadIconsCallback callback);

  // Mimics WebAppShortcutsMenuItemInfo but stores timestamps instead of icons
  // for os integration.
  using ShortcutMenuIconTimes =
      base::flat_map<IconPurpose, base::flat_map<SquareSizePx, base::Time>>;
  using ShortcutIconDataVector = std::vector<ShortcutMenuIconTimes>;
  using ShortcutIconDataCallback =
      base::OnceCallback<void(ShortcutIconDataVector)>;

  void ReadAllShortcutMenuIconsWithTimestamp(const webapps::AppId& app_id,
                                             ShortcutIconDataCallback callback);

  using ReadIconsUpdateTimeCallback = base::OnceCallback<void(
      base::flat_map<SquareSizePx, base::Time> time_map)>;
  // Reads all the last updated time for all icons in the app. Returns empty map
  // in |callback| if IO error.
  void ReadIconsLastUpdateTime(const webapps::AppId& app_id,
                               ReadIconsUpdateTimeCallback callback);

  // Encapsulate the type of bitmaps being returned by ReadAllIcons().
  struct WebAppBitmaps {
    WebAppBitmaps(IconBitmaps manifest_icons, IconBitmaps trusted_icons)
        : manifest_icons(std::move(manifest_icons)),
          trusted_icons(std::move(trusted_icons)) {}
    WebAppBitmaps() = default;
    ~WebAppBitmaps() = default;

    IconBitmaps manifest_icons;
    IconBitmaps trusted_icons;
  };
  using ReadIconBitmapsCallback =
      base::OnceCallback<void(WebAppBitmaps disk_bitmap)>;
  // Reads all icon bitmaps for an app. Returns empty |icon_bitmaps| in
  // |callback| if IO error.
  void ReadAllIcons(const webapps::AppId& app_id,
                    ReadIconBitmapsCallback callback);

  using ReadShortcutsMenuIconsCallback = base::OnceCallback<void(
      ShortcutsMenuIconBitmaps shortcuts_menu_icon_bitmaps)>;
  // Reads bitmaps for all shortcuts menu icons for an app. Returns a vector of
  // map<SquareSizePx, SkBitmap>. The index of a map in the vector is the same
  // as that of its corresponding shortcut in the manifest's shortcuts vector.
  // Returns empty vector in |callback| if we hit any error.
  void ReadAllShortcutsMenuIcons(const webapps::AppId& app_id,
                                 ReadShortcutsMenuIconsCallback callback);

  using ReadIconWithPurposeCallback =
      base::OnceCallback<void(IconPurpose, SkBitmap)>;
  // For each of |purposes|, in the given order, looks for an icon with size at
  // least |min_icon_size|. Returns the first icon found, as a bitmap. Returns
  // an empty SkBitmap in |callback| if IO error.
  void ReadSmallestIcon(const webapps::AppId& app_id,
                        const std::vector<IconPurpose>& purposes,
                        SquareSizePx min_size_in_px,
                        ReadIconWithPurposeCallback callback);

  using ReadCompressedIconWithPurposeCallback =
      base::OnceCallback<void(IconPurpose, std::vector<uint8_t> data)>;
  // For each of |purposes|, in the given order, looks for an icon with size at
  // least |min_icon_size|. Returns the first icon found, compressed as PNG.
  // Returns empty |data| in |callback| if IO error.
  void ReadSmallestCompressedIcon(
      const webapps::AppId& app_id,
      const std::vector<IconPurpose>& purposes,
      SquareSizePx min_size_in_px,
      ReadCompressedIconWithPurposeCallback callback);

  using GetIconsSizeCallback = base::OnceCallback<void(uint64_t)>;
  void GetIconsSizeForApp(const webapps::AppId& app_id,
                          GetIconsSizeCallback callback) const;

  // Returns a square icon of gfx::kFaviconSize px, or an empty bitmap if not
  // found.
  SkBitmap GetFavicon(const webapps::AppId& app_id) const;

  gfx::ImageSkia GetFaviconImageSkia(const webapps::AppId& app_id) const;
  gfx::ImageSkia GetMonochromeFavicon(const webapps::AppId& app_id) const;

  // WebAppInstallManagerObserver:
  void OnWebAppInstalled(const webapps::AppId& app_id) override;
  void OnWebAppInstallManagerDestroyed() override;

  // Calls back with an icon of the |desired_icon_size| and |purpose|, resizing
  // an icon of a different size if necessary. If no icons were available, calls
  // back with an empty map. Prefers resizing a large icon smaller over resizing
  // a small icon larger.
  void ReadIconAndResize(const webapps::AppId& app_id,
                         IconPurpose purpose,
                         SquareSizePx desired_icon_size,
                         ReadIconsCallback callback);

  // Reads multiple densities of the icon for each supported UI scale factor.
  // See ui/base/resource/resource_scale_factor.h. Returns null image in
  // `callback` if no icons found for all supported UI scale factors (matches
  // only bigger icons, no upscaling).
  // TODO(crbug.com/427566193): Rename here and callsites that this might be
  // untrusted.
  void ReadFavicons(const webapps::AppId& app_id,
                    IconPurpose purpose,
                    ReadImageSkiaCallback callback);

  struct IconFilesCheck {
    size_t empty = 0;
    size_t missing = 0;
  };
  void CheckForEmptyOrMissingIconFiles(
      const webapps::AppId& app_id,
      base::OnceCallback<void(IconFilesCheck)> callback) const;

  void SetFaviconReadCallbackForTesting(FaviconReadCallback callback);
  void SetFaviconMonochromeReadCallbackForTesting(FaviconReadCallback callback);

  base::FilePath GetIconFilePathForTesting(const webapps::AppId& app_id,
                                           IconPurpose purpose,
                                           SquareSizePx size);

  // Collects icon read/write errors (unbounded) if the |kRecordWebAppDebugInfo|
  // flag is enabled to be used by: chrome://web-app-internals
  const std::vector<std::string>* error_log() const { return error_log_.get(); }
  std::vector<std::string>* error_log() { return error_log_.get(); }

 private:
  base::WeakPtr<const WebAppIconManager> GetWeakPtr() const;
  base::WeakPtr<WebAppIconManager> GetWeakPtr();

  std::optional<IconSizeAndPurpose> FindIconMatchSmaller(
      const webapps::AppId& app_id,
      const std::vector<IconPurpose>& purposes,
      SquareSizePx max_size) const;

  void OnReadFavicons(ReadImageSkiaCallback callback,
                      std::map<SquareSizePx, SkBitmap> icon_bitmaps);

  void ReadFavicon(const webapps::AppId& app_id);
  void OnReadFavicon(const webapps::AppId& app_id, gfx::ImageSkia image_skia);

  void ReadMonochromeFavicon(const webapps::AppId& app_id);
  void OnReadMonochromeFavicon(const webapps::AppId& app_id,
                               gfx::ImageSkia manifest_monochrome_image);
  void OnMonochromeIconConverted(const webapps::AppId& app_id,
                                 gfx::ImageSkia converted_image);

  raw_ptr<WebAppProvider> provider_ = nullptr;

  base::FilePath web_apps_directory_;
  scoped_refptr<base::SequencedTaskRunner> icon_task_runner_;

  base::ScopedObservation<WebAppInstallManager, WebAppInstallManagerObserver>
      install_manager_observation_{this};

  // We cache different densities for high-DPI displays per each app.
  std::map<webapps::AppId, gfx::ImageSkia> favicon_cache_;
  std::map<webapps::AppId, gfx::ImageSkia> favicon_monochrome_cache_;

  FaviconReadCallback favicon_read_callback_;
  FaviconReadCallback favicon_monochrome_read_callback_;

  std::unique_ptr<std::vector<std::string>> error_log_;

  base::WeakPtrFactory<WebAppIconManager> weak_ptr_factory_{this};
};

}  // namespace web_app

#endif  // CHROME_BROWSER_WEB_APPLICATIONS_WEB_APP_ICON_MANAGER_H_