File: icon_cacher_impl.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; 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 (117 lines) | stat: -rw-r--r-- 3,909 bytes parent folder | download | duplicates (11)
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
// Copyright 2016 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_NTP_TILES_ICON_CACHER_IMPL_H_
#define COMPONENTS_NTP_TILES_ICON_CACHER_IMPL_H_

#include <memory>
#include <set>
#include <vector>

#include "base/cancelable_callback.h"
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/task/cancelable_task_tracker.h"
#include "components/ntp_tiles/icon_cacher.h"
#include "components/ntp_tiles/popular_sites.h"

namespace data_decoder {
class DataDecoder;
}  // namespace data_decoder

namespace favicon {
class FaviconService;
class LargeIconService;
}  // namespace favicon

namespace favicon_base {
struct FaviconImageResult;
struct LargeIconResult;
enum class GoogleFaviconServerRequestStatus;
}  // namespace favicon_base

namespace gfx {
class Image;
}  // namespace gfx

namespace image_fetcher {
class ImageFetcher;
struct RequestMetadata;
}  // namespace image_fetcher

namespace ntp_tiles {

class IconCacherImpl : public IconCacher {
 public:
  // TODO(jkrcal): Make this eventually use only LargeIconService.
  // crbug.com/696563
  IconCacherImpl(favicon::FaviconService* favicon_service,
                 favicon::LargeIconService* large_icon_service,
                 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher,
                 std::unique_ptr<data_decoder::DataDecoder> data_decoder);

  IconCacherImpl(const IconCacherImpl&) = delete;
  IconCacherImpl& operator=(const IconCacherImpl&) = delete;

  ~IconCacherImpl() override;

  void StartFetchPopularSites(
      PopularSites::Site site,
      base::OnceClosure icon_available,
      base::OnceClosure preliminary_icon_available) override;

  // TODO(jkrcal): Rename all instances of "MostLikely" to "ChromeSuggestions".
  void StartFetchMostLikely(const GURL& page_url,
                            base::OnceClosure icon_available) override;

 private:
  using CancelableImageCallback =
      base::CancelableOnceCallback<void(const gfx::Image&)>;

  void OnGetFaviconImageForPageURLFinished(
      PopularSites::Site site,
      base::OnceClosure preliminary_icon_available,
      const favicon_base::FaviconImageResult& result);

  void OnPopularSitesFaviconDownloaded(
      PopularSites::Site site,
      std::unique_ptr<CancelableImageCallback> preliminary_callback,
      const gfx::Image& fetched_image,
      const image_fetcher::RequestMetadata& metadata);

  std::unique_ptr<CancelableImageCallback> MaybeProvideDefaultIcon(
      const PopularSites::Site& site,
      base::OnceClosure preliminary_icon_available);
  void SaveAndNotifyDefaultIconForSite(
      const PopularSites::Site& site,
      base::OnceClosure preliminary_icon_available,
      const gfx::Image& image);
  void SaveIconForSite(const PopularSites::Site& site, const gfx::Image& image);

  void OnGetLargeIconOrFallbackStyleFinished(
      const GURL& page_url,
      const favicon_base::LargeIconResult& result);

  void OnMostLikelyFaviconDownloaded(
      const GURL& request_url,
      favicon_base::GoogleFaviconServerRequestStatus status);

  bool StartRequest(const GURL& request_url, base::OnceClosure icon_available);
  void FinishRequestAndNotifyIconAvailable(const GURL& request_url,
                                           bool newly_available);

  base::CancelableTaskTracker tracker_;
  const raw_ptr<favicon::FaviconService> favicon_service_;
  const raw_ptr<favicon::LargeIconService> large_icon_service_;
  std::unique_ptr<image_fetcher::ImageFetcher> const image_fetcher_;
  std::map<GURL, std::vector<base::OnceClosure>> in_flight_requests_;
  std::unique_ptr<data_decoder::DataDecoder> data_decoder_;

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

}  // namespace ntp_tiles

#endif  // COMPONENTS_NTP_TILES_ICON_CACHER_IMPL_H_