File: webapk_icons_hasher.h

package info (click to toggle)
chromium 139.0.7258.138-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,120,676 kB
  • sloc: cpp: 35,100,869; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (74 lines) | stat: -rw-r--r-- 2,525 bytes parent folder | download | duplicates (3)
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
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_WEBAPPS_BROWSER_ANDROID_WEBAPK_WEBAPK_ICONS_HASHER_H_
#define COMPONENTS_WEBAPPS_BROWSER_ANDROID_WEBAPK_WEBAPK_ICONS_HASHER_H_

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

#include "base/functional/callback.h"
#include "base/memory/weak_ptr.h"
#include "base/types/pass_key.h"
#include "components/webapps/browser/android/webapk/webapk_single_icon_hasher.h"
#include "url/gurl.h"
#include "url/origin.h"

namespace network::mojom {
class URLLoaderFactory;
}

namespace content {
class WebContents;
}  // namespace content

namespace webapps {

struct ShortcutInfo;
class WebappIcon;

// Downloads WebAPK icons and takes a Murmur2 hashes of the downloaded images.
class WebApkIconsHasher {
 public:
  using Murmur2HashMultipleCallback =
      base::OnceCallback<void(std::map<GURL, std::unique_ptr<WebappIcon>>)>;

  using PassKey = base::PassKey<WebApkIconsHasher>;
  static PassKey PassKeyForTesting();

  WebApkIconsHasher();
  ~WebApkIconsHasher();
  WebApkIconsHasher(const WebApkIconsHasher&) = delete;
  WebApkIconsHasher& operator=(const WebApkIconsHasher&) = delete;

  // Creates a self-owned WebApkIconHasher instance. The instance downloads all
  // the |icon_urls| and calls |callback| with the Murmur2 hash of the
  // downloaded images. The hash is taken over the raw image bytes (no image
  // encoding/decoding beforehand). |callback| is called with a std::nullopt if
  // any image cannot not be downloaded in time (e.g. 404 HTTP error code).
  void DownloadAndComputeMurmur2Hash(
      network::mojom::URLLoaderFactory* url_loader_factory,
      base::WeakPtr<content::WebContents> web_contents,
      const url::Origin& request_initiator,
      const ShortcutInfo& shortcut_info,
      const SkBitmap& primary_icon_bitmap,
      Murmur2HashMultipleCallback callback);

 private:
  void OnAllMurmur2Hashes(Murmur2HashMultipleCallback callback,
                          const GURL& primary_icon_url,
                          bool primary_icon_maskable,
                          const SkBitmap& primary_icon_bitmap);

  std::map<GURL, std::unique_ptr<WebappIcon>> webapk_icons_;
  std::vector<std::unique_ptr<WebApkSingleIconHasher>> hashers_;

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

}  // namespace webapps

#endif  // COMPONENTS_WEBAPPS_BROWSER_ANDROID_WEBAPK_WEBAPK_ICONS_HASHER_H_