File: large_icon_service.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 (186 lines) | stat: -rw-r--r-- 8,521 bytes parent folder | download | duplicates (6)
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
// Copyright 2015 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_FAVICON_CORE_LARGE_ICON_SERVICE_H_
#define COMPONENTS_FAVICON_CORE_LARGE_ICON_SERVICE_H_

#include <optional>

#include "base/task/cancelable_task_tracker.h"
#include "components/favicon_base/favicon_callback.h"
#include "components/keyed_service/core/keyed_service.h"

namespace net {
struct NetworkTrafficAnnotationTag;
}

class GURL;

namespace favicon {

// The large icon service provides methods to access large icons. The actual
// implementation of this uses Google's favicon service.
class LargeIconService : public KeyedService {
 public:
  // Controls the icon size in pixels returned by the
  // `GetLargeIconFromCacheFallbackToGoogleServer()`. This enum is used to
  // ensure icons are requested at standard sizes, because the underlying
  // favicon database stores at most 1 icon per domain and favicons are
  // requested from the Google server at a single hard-coded size.
  enum class StandardIconSize {
    k16x16 = 0,
    k32x32 = 1,
  };
  // Controls the behavior when there is no icon bigger than the minimum size to
  // return.
  enum class NoBigEnoughIconBehavior {
    // Return the biggest favicon bitmap available.
    kReturnBitmap = 0,
    // Extract the dominant color of the smaller image.
    kReturnFallbackColor = 1,
    // Empty return.
    kReturnEmpty = 2,
  };

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

  // Requests the best large icon for the page at `page_url`.
  // Case 1. An icon exists whose size is >= MAX(`min_source_size_in_pixel`,
  // `desired_size_in_pixel`):
  // - If `desired_size_in_pixel` == 0: returns icon as is.
  // - Else: returns the icon resized to `desired_size_in_pixel`.
  // Case 2. An icon exists whose size is >= `min_source_size_in_pixel` and <
  // `desired_size_in_pixel`:
  // - Same as 1 with the biggest icon.
  // Case 3. An icon exists whose size is < `min_source_size_in_pixel`:
  // - Extracts dominant color of smaller image, returns a fallback icon style
  //   that has a matching background.
  // Case 4. No icon exists.
  // - Returns the default fallback icon style.
  // For cases 3 and 4, this function returns the style of the fallback icon
  // instead of rendering an icon so clients can render the icon themselves.
  virtual base::CancelableTaskTracker::TaskId
  GetLargeIconRawBitmapOrFallbackStyleForPageUrl(
      const GURL& page_url,
      int min_source_size_in_pixel,
      int desired_size_in_pixel,
      favicon_base::LargeIconCallback callback,
      base::CancelableTaskTracker* tracker) = 0;

  // Behaves the same as GetLargeIconRawBitmapOrFallbackStyleForPageUrl(), only
  // returns the large icon (if available) decoded.
  virtual base::CancelableTaskTracker::TaskId
  GetLargeIconImageOrFallbackStyleForPageUrl(
      const GURL& page_url,
      int min_source_size_in_pixel,
      int desired_size_in_pixel,
      favicon_base::LargeIconImageCallback callback,
      base::CancelableTaskTracker* tracker) = 0;

  // Queries the favicon database for an icon larger than
  // `min_source_size_in_pixel`. If `size_in_pixel_to_resize_to` is specified,
  // the returned icon will be resized to the passed-in size. The resizing also
  // occurs if there is no big enough icon and `no_big_enough_icon_behavior` ==
  // NoBigEnoughIconBehavior::kReturnBitmap. `no_big_enough_icon_behavior`
  // controls the returned value if there is no icon larger than
  // `min_source_size_in_pixel` in the database.
  virtual base::CancelableTaskTracker::TaskId GetLargeIconRawBitmapForPageUrl(
      const GURL& page_url,
      int min_source_size_in_pixel,
      std::optional<int> size_in_pixel_to_resize_to,
      NoBigEnoughIconBehavior no_big_enough_icon_behavior,
      favicon_base::LargeIconCallback callback,
      base::CancelableTaskTracker* tracker) = 0;

  // Behaves the same as GetLargeIconRawBitmapOrFallbackStyleForPageUrl, except
  // uses icon URL instead of page URL.
  virtual base::CancelableTaskTracker::TaskId
  GetLargeIconRawBitmapOrFallbackStyleForIconUrl(
      const GURL& icon_url,
      int min_source_size_in_pixel,
      int desired_size_in_pixel,
      favicon_base::LargeIconCallback callback,
      base::CancelableTaskTracker* tracker) = 0;

  // Requests the best icon for the page at `page_url`. Fallbacks to the host's
  // favicon, and resizes the most similar bitmat to `desired_size_in_pizel` if
  // no exact match is found.
  virtual base::CancelableTaskTracker::TaskId
  GetIconRawBitmapOrFallbackStyleForPageUrl(
      const GURL& page_url,
      int desired_size_in_pixel,
      favicon_base::LargeIconCallback callback,
      base::CancelableTaskTracker* tracker) = 0;

  // Fetches the best large icon for the page at `page_url` from a Google
  // favicon server and stores the result in the FaviconService database
  // (implemented in HistoryService). The write will be a no-op if the local
  // favicon database contains an icon for `page_url`, so clients are
  // encouraged to use GetLargeIconOrFallbackStyle() first.
  //
  // A parameter in the server request representing the desired favicon size is
  // set according solely to the device and scale factor. However, it serves
  // only as a hint to the service, no guarantees on the fetched size are
  // provided.
  //
  // If `should_trim_page_url_path` is set to true, the path will be removed
  // from the URL used to query the server but the result will be stored under
  // the full URL provided to the API.
  //
  // The callback is triggered when the operation finishes, where `success`
  // tells whether the fetch actually managed to database a new icon in the
  // FaviconService.
  //
  // WARNING: This function will share the `page_url` with a Google server. This
  // can be used only for urls that are not privacy sensitive or for users that
  // sync their history with Google servers.
  // TODO(crbug.com/41425581): It is not clear from the name of this function,
  // that it actually adds the icon to the local cache. Maybe
  // "StoreLargeIcon..."?
  virtual void GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache(
      const GURL& page_url,
      bool should_trim_page_url_path,
      const net::NetworkTrafficAnnotationTag& traffic_annotation,
      favicon_base::GoogleFaviconServerCallback callback) = 0;

  // Requests the best large icon for the `page_url` via
  // `GetLargeIconRawBitmapForPageUrl()`. `min_source_size` and
  // `size_to_resize_to` are converted to integer values before being processed
  // further. They and `no_big_enough_icon_behavior` control the behavior of the
  // `GetLargeIconRawBitmapForPageUrl()`. If no icon (of any size) is found in
  // the local cache for `page_url`, the icon is queried from the Google server
  // via `GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache()`.
  //
  // Note: it's possible to obtain an image bigger than the largest standard
  // size (32x32) if the user has visited the `page_url` previously a mobile
  // device and the passed-in `size_to_resize_to` is `std::nullopt`.
  //
  // WARNING: This function may share the `page_url` with a Google server if the
  // icon is not found locally. This can be used only for urls that are not
  // privacy sensitive or for users that sync their history with Google servers.
  virtual void GetLargeIconFromCacheFallbackToGoogleServer(
      const GURL& page_url,
      StandardIconSize min_source_size,
      std::optional<StandardIconSize> size_to_resize_to,
      NoBigEnoughIconBehavior no_big_enough_icon_behavior,
      bool should_trim_page_url_path,
      const net::NetworkTrafficAnnotationTag& traffic_annotation,
      favicon_base::LargeIconCallback callback,
      base::CancelableTaskTracker* tracker) = 0;

  // Update the time that the icon at `icon_url` was requested. This should be
  // called after obtaining the icon by GetLargeIcon*OrFallbackStyle() for any
  // icon that _may_ originate from the Google favicon server (i.e. if the
  // caller uses
  // GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache()). This
  // postpones the automatic eviction of the favicon from the database.
  virtual void TouchIconFromGoogleServer(const GURL& icon_url) = 0;
 protected:
  LargeIconService() = default;
};

}  // namespace favicon

#endif  // COMPONENTS_FAVICON_CORE_LARGE_ICON_SERVICE_H_