Roll ANGLE cc54ab3..c5b2ba5
[chromium-blink-merge.git] / components / favicon / core / favicon_service.h
blob747da389eda63c82cfbebe76cc168900e9aedb20
1 // Copyright (c) 2012 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.
5 #ifndef COMPONENTS_FAVICON_CORE_FAVICON_SERVICE_H_
6 #define COMPONENTS_FAVICON_CORE_FAVICON_SERVICE_H_
8 #include <vector>
10 #include "base/callback.h"
11 #include "base/containers/hash_tables.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/task/cancelable_task_tracker.h"
14 #include "components/favicon_base/favicon_callback.h"
15 #include "components/favicon_base/favicon_types.h"
16 #include "components/favicon_base/favicon_usage_data.h"
17 #include "components/keyed_service/core/keyed_service.h"
19 class GURL;
21 namespace history {
22 class HistoryService;
25 namespace favicon {
27 class FaviconClient;
29 // The favicon service provides methods to access favicons. It calls the history
30 // backend behind the scenes. The callbacks are run asynchronously, even in the
31 // case of an error.
32 class FaviconService : public KeyedService {
33 public:
34 // The FaviconClient must outlive the constructed FaviconService.
35 FaviconService(scoped_ptr<FaviconClient> favicon_client,
36 history::HistoryService* history_service);
38 ~FaviconService() override;
40 // We usually pass parameters with pointer to avoid copy. This function is a
41 // helper to run FaviconResultsCallback with pointer parameters.
42 static void FaviconResultsCallbackRunner(
43 const favicon_base::FaviconResultsCallback& callback,
44 const std::vector<favicon_base::FaviconRawBitmapResult>* results);
46 //////////////////////////////////////////////////////////////////////////////
47 // Methods to request favicon bitmaps from the history backend for |icon_url|.
48 // |icon_url| is the URL of the icon itself.
49 // (e.g. <http://www.google.com/favicon.ico>)
51 // Requests the favicon at |icon_url| of type favicon_base::FAVICON and of
52 // size gfx::kFaviconSize. The returned gfx::Image is populated with
53 // representations for all of the scale factors supported by the platform
54 // (e.g. MacOS). If data is unavailable for some or all of the scale factors,
55 // the bitmaps with the best matching sizes are resized.
56 base::CancelableTaskTracker::TaskId GetFaviconImage(
57 const GURL& icon_url,
58 const favicon_base::FaviconImageCallback& callback,
59 base::CancelableTaskTracker* tracker);
61 // Requests the favicon at |icon_url| of |icon_type| of size
62 // |desired_size_in_pixel|. If there is no favicon of size
63 // |desired_size_in_pixel|, the favicon bitmap which best matches
64 // |desired_size_in_pixel| is resized. If |desired_size_in_pixel| is 0,
65 // the largest favicon bitmap is returned.
66 base::CancelableTaskTracker::TaskId GetRawFavicon(
67 const GURL& icon_url,
68 favicon_base::IconType icon_type,
69 int desired_size_in_pixel,
70 const favicon_base::FaviconRawBitmapCallback& callback,
71 base::CancelableTaskTracker* tracker);
73 // The first argument for |callback| is the set of bitmaps for the passed in
74 // URL and icon types whose pixel sizes best match the passed in
75 // |desired_size_in_dip| at the resource scale factors supported by the
76 // current platform (eg MacOS) in addition to 1x. The vector has at most one
77 // result for each of the resource scale factors. There are less entries if a
78 // single/ result is the best bitmap to use for several resource scale
79 // factors.
80 base::CancelableTaskTracker::TaskId GetFavicon(
81 const GURL& icon_url,
82 favicon_base::IconType icon_type,
83 int desired_size_in_dip,
84 const favicon_base::FaviconResultsCallback& callback,
85 base::CancelableTaskTracker* tracker);
87 //////////////////////////////////////////////////////////////////////////////
88 // Methods to request favicon bitmaps from the history backend for |page_url|.
89 // |page_url| is the web page the favicon is associated with.
90 // (e.g. <http://www.google.com>)
92 // Requests the favicon for the page at |page_url| of type
93 // favicon_base::FAVICON and of size gfx::kFaviconSize. The returned
94 // gfx::Image is populated with representations for all of the scale factors
95 // supported by the platform (e.g. MacOS). If data is unavailable for some or
96 // all of the scale factors, the bitmaps with the best matching sizes are
97 // resized.
98 base::CancelableTaskTracker::TaskId GetFaviconImageForPageURL(
99 const GURL& page_url,
100 const favicon_base::FaviconImageCallback& callback,
101 base::CancelableTaskTracker* tracker);
103 // Requests the favicon for the page at |page_url| with one of |icon_types|
104 // and with |desired_size_in_pixel|. |icon_types| can be any combination of
105 // IconTypes. If favicon bitmaps for several IconTypes are available, the
106 // favicon bitmap is chosen in the priority of TOUCH_PRECOMPOSED_ICON,
107 // TOUCH_ICON and FAVICON. If there is no favicon bitmap of size
108 // |desired_size_in_pixel|, the favicon bitmap which best matches
109 // |desired_size_in_pixel| is resized. If |desired_size_in_pixel| is 0,
110 // the largest favicon bitmap is returned. Results with a higher priority
111 // IconType are preferred over an exact match of the favicon bitmap size.
112 base::CancelableTaskTracker::TaskId GetRawFaviconForPageURL(
113 const GURL& page_url,
114 int icon_types,
115 int desired_size_in_pixel,
116 const favicon_base::FaviconRawBitmapCallback& callback,
117 base::CancelableTaskTracker* tracker);
119 // See HistoryService::GetLargestFaviconForPageURL().
120 virtual base::CancelableTaskTracker::TaskId GetLargestRawFaviconForPageURL(
121 const GURL& page_url,
122 const std::vector<int>& icon_types,
123 int minimum_size_in_pixels,
124 const favicon_base::FaviconRawBitmapCallback& callback,
125 base::CancelableTaskTracker* tracker);
127 base::CancelableTaskTracker::TaskId GetFaviconForPageURL(
128 const GURL& page_url,
129 int icon_types,
130 int desired_size_in_dip,
131 const favicon_base::FaviconResultsCallback& callback,
132 base::CancelableTaskTracker* tracker);
134 // Set the favicon mappings to |page_url| for |icon_types| in the history
135 // database.
136 // Sample |icon_urls|:
137 // { ICON_URL1 -> TOUCH_ICON, known to the database,
138 // ICON_URL2 -> TOUCH_ICON, not known to the database,
139 // ICON_URL3 -> TOUCH_PRECOMPOSED_ICON, known to the database }
140 // The new mappings are computed from |icon_urls| with these rules:
141 // 1) Any urls in |icon_urls| which are not already known to the database are
142 // rejected.
143 // Sample new mappings to |page_url|: { ICON_URL1, ICON_URL3 }
144 // 2) If |icon_types| has multiple types, the mappings are only set for the
145 // largest icon type.
146 // Sample new mappings to |page_url|: { ICON_URL3 }
147 // |icon_types| can only have multiple IconTypes if
148 // |icon_types| == TOUCH_ICON | TOUCH_PRECOMPOSED_ICON.
149 // The favicon bitmaps which most closely match |desired_size_in_dip|
150 // at the reosurce scale factors supported by the current platform (eg MacOS)
151 // in addition to 1x from the favicons which were just mapped to |page_url|
152 // are returned. If |desired_size_in_dip| is 0, the largest favicon bitmap is
153 // returned.
154 base::CancelableTaskTracker::TaskId UpdateFaviconMappingsAndFetch(
155 const GURL& page_url,
156 const std::vector<GURL>& icon_urls,
157 int icon_types,
158 int desired_size_in_dip,
159 const favicon_base::FaviconResultsCallback& callback,
160 base::CancelableTaskTracker* tracker);
162 // Used to request a bitmap for the favicon with |favicon_id| which is not
163 // resized from the size it is stored at in the database. If there are
164 // multiple favicon bitmaps for |favicon_id|, the largest favicon bitmap is
165 // returned.
166 base::CancelableTaskTracker::TaskId GetLargestRawFaviconForID(
167 favicon_base::FaviconID favicon_id,
168 const favicon_base::FaviconRawBitmapCallback& callback,
169 base::CancelableTaskTracker* tracker);
171 // Marks all types of favicon for the page as being out of date.
172 void SetFaviconOutOfDateForPage(const GURL& page_url);
174 // Allows the importer to set many favicons for many pages at once. The pages
175 // must exist, any favicon sets for unknown pages will be discarded. Existing
176 // favicons will not be overwritten.
177 void SetImportedFavicons(
178 const favicon_base::FaviconUsageDataList& favicon_usage);
180 // Set the favicon for |page_url| for |icon_type| in the thumbnail database.
181 // Unlike SetFavicons(), this method will not delete preexisting bitmap data
182 // which is associated to |page_url| if at all possible. Use this method if
183 // the favicon bitmaps for any of ui::GetSupportedScaleFactors() are not
184 // known.
185 void MergeFavicon(const GURL& page_url,
186 const GURL& icon_url,
187 favicon_base::IconType icon_type,
188 scoped_refptr<base::RefCountedMemory> bitmap_data,
189 const gfx::Size& pixel_size);
191 // Set the favicon for |page_url| for |icon_type| in the thumbnail database.
192 // |icon_url| is the single favicon to map to |page_url|. Mappings from
193 // |page_url| to favicons at different icon URLs will be deleted.
194 // A favicon bitmap is added for each image rep in |image|. Any preexisting
195 // bitmap data for |icon_url| is deleted. It is important that |image|
196 // contains image reps for all of ui::GetSupportedScaleFactors(). Use
197 // MergeFavicon() if it does not.
198 // TODO(pkotwicz): Save unresized favicon bitmaps to the database.
199 // TODO(pkotwicz): Support adding favicons for multiple icon URLs to the
200 // thumbnail database.
201 void SetFavicons(const GURL& page_url,
202 const GURL& icon_url,
203 favicon_base::IconType icon_type,
204 const gfx::Image& image);
206 // Avoid repeated requests to download missing favicon.
207 void UnableToDownloadFavicon(const GURL& icon_url);
208 bool WasUnableToDownloadFavicon(const GURL& icon_url) const;
209 void ClearUnableToDownloadFavicons();
211 private:
212 typedef uint32 MissingFaviconURLHash;
214 // Helper function for GetFaviconImageForPageURL(), GetRawFaviconForPageURL()
215 // and GetFaviconForPageURL().
216 base::CancelableTaskTracker::TaskId GetFaviconForPageURLImpl(
217 const GURL& page_url,
218 int icon_types,
219 const std::vector<int>& desired_sizes_in_pixel,
220 const favicon_base::FaviconResultsCallback& callback,
221 base::CancelableTaskTracker* tracker);
223 // Intermediate callback for GetFaviconImage() and GetFaviconImageForPageURL()
224 // so that history service can deal solely with FaviconResultsCallback.
225 // Builds favicon_base::FaviconImageResult from |favicon_bitmap_results| and
226 // runs |callback|.
227 void RunFaviconImageCallbackWithBitmapResults(
228 const favicon_base::FaviconImageCallback& callback,
229 int desired_size_in_dip,
230 const std::vector<favicon_base::FaviconRawBitmapResult>&
231 favicon_bitmap_results);
233 // Intermediate callback for GetRawFavicon() and GetRawFaviconForPageURL()
234 // so that history service can deal solely with FaviconResultsCallback.
235 // Resizes favicon_base::FaviconRawBitmapResult if necessary and runs
236 // |callback|.
237 void RunFaviconRawBitmapCallbackWithBitmapResults(
238 const favicon_base::FaviconRawBitmapCallback& callback,
239 int desired_size_in_pixel,
240 const std::vector<favicon_base::FaviconRawBitmapResult>&
241 favicon_bitmap_results);
243 base::hash_set<MissingFaviconURLHash> missing_favicon_urls_;
244 scoped_ptr<FaviconClient> favicon_client_;
245 history::HistoryService* history_service_;
247 DISALLOW_COPY_AND_ASSIGN(FaviconService);
250 } // namespace favicon
252 #endif // COMPONENTS_FAVICON_CORE_FAVICON_SERVICE_H_