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 CHROME_BROWSER_FAVICON_FAVICON_SERVICE_H_
6 #define CHROME_BROWSER_FAVICON_FAVICON_SERVICE_H_
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_types.h"
15 #include "components/keyed_service/core/keyed_service.h"
16 #include "ui/base/layout.h"
20 struct ImportedFaviconUsage;
24 struct FaviconImageResult;
27 // The favicon service provides methods to access favicons. It calls the history
28 // backend behind the scenes.
29 class FaviconService : public KeyedService {
31 explicit FaviconService(Profile* profile);
33 virtual ~FaviconService();
35 // Auxiliary argument structure for requesting favicons for URLs.
36 struct FaviconForURLParams {
37 FaviconForURLParams(const GURL& page_url,
39 int desired_size_in_dip)
41 icon_types(icon_types),
42 desired_size_in_dip(desired_size_in_dip) {}
46 int desired_size_in_dip;
49 // Callback for GetFaviconImage() and GetFaviconImageForURL().
50 // |FaviconImageResult::image| is constructed from the bitmaps for the
51 // passed in URL and icon types which most which closely match the passed in
52 // |desired_size_in_dip| at the scale factors supported by the current
53 // platform (eg MacOS) in addition to 1x.
54 // |FaviconImageResult::icon_url| is the favicon that the favicon bitmaps in
55 // |image| originate from.
56 // TODO(pkotwicz): Enable constructing |image| from bitmaps from several
58 typedef base::Callback<void(const favicon_base::FaviconImageResult&)>
61 // Callback for GetRawFavicon(), GetRawFaviconForURL() and
62 // GetLargestRawFavicon().
63 // See function for details on value.
64 typedef base::Callback<void(const favicon_base::FaviconBitmapResult&)>
67 // Callback for GetFavicon() and GetFaviconForURL().
69 // The first argument is the set of bitmaps for the passed in URL and
70 // icon types whose pixel sizes best match the passed in
71 // |desired_size_in_dip| at the scale factors supported by the current
72 // platform (eg MacOS) in addition to 1x. The vector has at most one result
73 // for each of the scale factors. There are less entries if a single result
74 // is the best bitmap to use for several scale factors.
75 typedef base::Callback<void(const std::vector<
76 favicon_base::FaviconBitmapResult>&)> FaviconResultsCallback;
78 // We usually pass parameters with pointer to avoid copy. This function is a
79 // helper to run FaviconResultsCallback with pointer parameters.
80 static void FaviconResultsCallbackRunner(
81 const FaviconResultsCallback& callback,
82 const std::vector<favicon_base::FaviconBitmapResult>* results);
84 // Requests the favicon at |icon_url| of |icon_type| whose size most closely
85 // matches |desired_size_in_dip|. If |desired_size_in_dip| is 0, the largest
86 // favicon bitmap at |icon_url| is returned. |consumer| is notified when the
87 // bits have been fetched. |icon_url| is the URL of the icon itself, e.g.
88 // <http://www.google.com/favicon.ico>.
89 // Each of the three methods below differs in the format of the callback and
90 // the requested scale factors. All of the scale factors supported by the
91 // current platform (eg MacOS) are requested for GetFaviconImage().
92 base::CancelableTaskTracker::TaskId GetFaviconImage(
94 favicon_base::IconType icon_type,
95 int desired_size_in_dip,
96 const FaviconImageCallback& callback,
97 base::CancelableTaskTracker* tracker);
99 base::CancelableTaskTracker::TaskId GetRawFavicon(
100 const GURL& icon_url,
101 favicon_base::IconType icon_type,
102 int desired_size_in_dip,
103 ui::ScaleFactor desired_scale_factor,
104 const FaviconRawCallback& callback,
105 base::CancelableTaskTracker* tracker);
107 base::CancelableTaskTracker::TaskId GetFavicon(
108 const GURL& icon_url,
109 favicon_base::IconType icon_type,
110 int desired_size_in_dip,
111 const FaviconResultsCallback& callback,
112 base::CancelableTaskTracker* tracker);
114 // Set the favicon mappings to |page_url| for |icon_types| in the history
116 // Sample |icon_urls|:
117 // { ICON_URL1 -> TOUCH_ICON, known to the database,
118 // ICON_URL2 -> TOUCH_ICON, not known to the database,
119 // ICON_URL3 -> TOUCH_PRECOMPOSED_ICON, known to the database }
120 // The new mappings are computed from |icon_urls| with these rules:
121 // 1) Any urls in |icon_urls| which are not already known to the database are
123 // Sample new mappings to |page_url|: { ICON_URL1, ICON_URL3 }
124 // 2) If |icon_types| has multiple types, the mappings are only set for the
125 // largest icon type.
126 // Sample new mappings to |page_url|: { ICON_URL3 }
127 // |icon_types| can only have multiple IconTypes if
128 // |icon_types| == TOUCH_ICON | TOUCH_PRECOMPOSED_ICON.
129 // The favicon bitmaps which most closely match |desired_size_in_dip|
130 // at the scale factors supported by the current platform (eg MacOS) in
131 // addition to 1x from the favicons which were just mapped to |page_url| are
132 // returned. If |desired_size_in_dip| is 0, the largest favicon bitmap is
134 base::CancelableTaskTracker::TaskId UpdateFaviconMappingsAndFetch(
135 const GURL& page_url,
136 const std::vector<GURL>& icon_urls,
138 int desired_size_in_dip,
139 const FaviconResultsCallback& callback,
140 base::CancelableTaskTracker* tracker);
142 // Requests the favicons of any of |icon_types| whose pixel sizes most
143 // closely match |desired_size_in_dip| and desired scale factors for a web
144 // page URL. If |desired_size_in_dip| is 0, the largest favicon for the web
145 // page URL is returned. |callback| is run when the bits have been fetched.
146 // |icon_types| can be any combination of IconType value, but only one icon
147 // will be returned in the priority of TOUCH_PRECOMPOSED_ICON, TOUCH_ICON and
148 // FAVICON. Each of the three methods below differs in the format of the
149 // callback and the requested scale factors. All of the scale factors
150 // supported by the current platform (eg MacOS) are requested for
151 // GetFaviconImageForURL().
152 // Note. |callback| is always run asynchronously.
153 base::CancelableTaskTracker::TaskId GetFaviconImageForURL(
154 const FaviconForURLParams& params,
155 const FaviconImageCallback& callback,
156 base::CancelableTaskTracker* tracker);
158 base::CancelableTaskTracker::TaskId GetRawFaviconForURL(
159 const FaviconForURLParams& params,
160 ui::ScaleFactor desired_scale_factor,
161 const FaviconRawCallback& callback,
162 base::CancelableTaskTracker* tracker);
164 // See HistoryService::GetLargestFaviconForURL().
165 base::CancelableTaskTracker::TaskId GetLargestRawFaviconForURL(
167 const GURL& page_url,
168 const std::vector<int>& icon_types,
169 int minimum_size_in_pixels,
170 const FaviconRawCallback& callback,
171 base::CancelableTaskTracker* tracker);
173 base::CancelableTaskTracker::TaskId GetFaviconForURL(
174 const FaviconForURLParams& params,
175 const FaviconResultsCallback& callback,
176 base::CancelableTaskTracker* tracker);
178 // Used to request a bitmap for the favicon with |favicon_id| which is not
179 // resized from the size it is stored at in the database. If there are
180 // multiple favicon bitmaps for |favicon_id|, the largest favicon bitmap is
182 base::CancelableTaskTracker::TaskId GetLargestRawFaviconForID(
183 favicon_base::FaviconID favicon_id,
184 const FaviconRawCallback& callback,
185 base::CancelableTaskTracker* tracker);
187 // Marks all types of favicon for the page as being out of date.
188 void SetFaviconOutOfDateForPage(const GURL& page_url);
190 // Clones all icons from an existing page. This associates the icons from
191 // |old_page_url| with |new_page_url|, provided |new_page_url| has no
192 // recorded associations to any other icons.
193 // Needed if you want to declare favicons (tentatively) in advance, before a
194 // page is ever visited.
195 void CloneFavicon(const GURL& old_page_url, const GURL& new_page_url);
197 // Allows the importer to set many favicons for many pages at once. The pages
198 // must exist, any favicon sets for unknown pages will be discarded. Existing
199 // favicons will not be overwritten.
200 void SetImportedFavicons(
201 const std::vector<ImportedFaviconUsage>& favicon_usage);
203 // Set the favicon for |page_url| for |icon_type| in the thumbnail database.
204 // Unlike SetFavicons(), this method will not delete preexisting bitmap data
205 // which is associated to |page_url| if at all possible. Use this method if
206 // the favicon bitmaps for any of ui::GetSupportedScaleFactors() are not
208 void MergeFavicon(const GURL& page_url,
209 const GURL& icon_url,
210 favicon_base::IconType icon_type,
211 scoped_refptr<base::RefCountedMemory> bitmap_data,
212 const gfx::Size& pixel_size);
214 // Set the favicon for |page_url| for |icon_type| in the thumbnail database.
215 // |icon_url| is the single favicon to map to |page_url|. Mappings from
216 // |page_url| to favicons at different icon URLs will be deleted.
217 // A favicon bitmap is added for each image rep in |image|. Any preexisting
218 // bitmap data for |icon_url| is deleted. It is important that |image|
219 // contains image reps for all of ui::GetSupportedScaleFactors(). Use
220 // MergeFavicon() if it does not.
221 // TODO(pkotwicz): Save unresized favicon bitmaps to the database.
222 // TODO(pkotwicz): Support adding favicons for multiple icon URLs to the
223 // thumbnail database.
224 void SetFavicons(const GURL& page_url,
225 const GURL& icon_url,
226 favicon_base::IconType icon_type,
227 const gfx::Image& image);
229 // Avoid repeated requests to download missing favicon.
230 void UnableToDownloadFavicon(const GURL& icon_url);
231 bool WasUnableToDownloadFavicon(const GURL& icon_url) const;
232 void ClearUnableToDownloadFavicons();
235 typedef uint32 MissingFaviconURLHash;
236 base::hash_set<MissingFaviconURLHash> missing_favicon_urls_;
237 HistoryService* history_service_;
240 // Helper function for GetFaviconImageForURL(), GetRawFaviconForURL() and
241 // GetFaviconForURL().
242 base::CancelableTaskTracker::TaskId GetFaviconForURLImpl(
243 const FaviconForURLParams& params,
244 const std::vector<ui::ScaleFactor>& desired_scale_factors,
245 const FaviconResultsCallback& callback,
246 base::CancelableTaskTracker* tracker);
248 // Intermediate callback for GetFaviconImage() and GetFaviconImageForURL()
249 // so that history service can deal solely with FaviconResultsCallback.
250 // Builds favicon_base::FaviconImageResult from |favicon_bitmap_results| and
253 void RunFaviconImageCallbackWithBitmapResults(
254 const FaviconImageCallback& callback,
255 int desired_size_in_dip,
256 const std::vector<favicon_base::FaviconBitmapResult>&
257 favicon_bitmap_results);
259 // Intermediate callback for GetRawFavicon() and GetRawFaviconForURL()
260 // so that history service can deal solely with FaviconResultsCallback.
261 // Resizes favicon_base::FaviconBitmapResult if necessary and runs |callback|.
262 void RunFaviconRawCallbackWithBitmapResults(
263 const FaviconRawCallback& callback,
264 int desired_size_in_dip,
265 ui::ScaleFactor desired_scale_factor,
266 const std::vector<favicon_base::FaviconBitmapResult>&
267 favicon_bitmap_results);
269 DISALLOW_COPY_AND_ASSIGN(FaviconService);
272 #endif // CHROME_BROWSER_FAVICON_FAVICON_SERVICE_H_