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_callback.h"
15 #include "components/favicon_base/favicon_types.h"
16 #include "components/keyed_service/core/keyed_service.h"
17 #include "ui/base/layout.h"
21 struct ImportedFaviconUsage
;
24 // The favicon service provides methods to access favicons. It calls the history
25 // backend behind the scenes.
26 class FaviconService
: public KeyedService
{
28 explicit FaviconService(Profile
* profile
);
30 virtual ~FaviconService();
32 // Auxiliary argument structure for requesting favicons for URLs.
33 struct FaviconForURLParams
{
34 FaviconForURLParams(const GURL
& page_url
,
36 int desired_size_in_dip
)
38 icon_types(icon_types
),
39 desired_size_in_dip(desired_size_in_dip
) {}
43 int desired_size_in_dip
;
46 // We usually pass parameters with pointer to avoid copy. This function is a
47 // helper to run FaviconResultsCallback with pointer parameters.
48 static void FaviconResultsCallbackRunner(
49 const favicon_base::FaviconResultsCallback
& callback
,
50 const std::vector
<favicon_base::FaviconBitmapResult
>* results
);
52 // The first argument of |callback| is a |const FaviconImageResult&|. Of which
53 // |FaviconImageResult::image| is constructed from the bitmaps for the
54 // passed in URL and icon types which most which closely match the passed in
55 // |desired_size_in_dip| at the scale factors supported by the current
56 // platform (eg MacOS) in addition to 1x.
57 // |FaviconImageResult::icon_url| is the favicon that the favicon bitmaps in
58 // |image| originate from.
59 // TODO(pkotwicz): Enable constructing |image| from bitmaps from several
62 // Requests the favicon at |icon_url| of |icon_type| whose size most closely
63 // matches |desired_size_in_dip|. If |desired_size_in_dip| is 0, the largest
64 // favicon bitmap at |icon_url| is returned. |consumer| is notified when the
65 // bits have been fetched. |icon_url| is the URL of the icon itself, e.g.
66 // <http://www.google.com/favicon.ico>.
67 // Each of the three methods below differs in the format of the callback and
68 // the requested scale factors. All of the scale factors supported by the
69 // current platform (eg MacOS) are requested for GetFaviconImage().
70 base::CancelableTaskTracker::TaskId
GetFaviconImage(
72 favicon_base::IconType icon_type
,
73 int desired_size_in_dip
,
74 const favicon_base::FaviconImageCallback
& callback
,
75 base::CancelableTaskTracker
* tracker
);
77 base::CancelableTaskTracker::TaskId
GetRawFavicon(
79 favicon_base::IconType icon_type
,
80 int desired_size_in_dip
,
81 ui::ScaleFactor desired_scale_factor
,
82 const favicon_base::FaviconRawCallback
& callback
,
83 base::CancelableTaskTracker
* tracker
);
85 // The first argument for |callback| is the set of bitmaps for the passed in
86 // URL and icon types whose pixel sizes best match the passed in
87 // |desired_size_in_dip| at the scale factors supported by the current
88 // platform (eg MacOS) in addition to 1x. The vector has at most one result
89 // for each of the scale factors. There are less entries if a single result
90 // is the best bitmap to use for several scale factors.
91 base::CancelableTaskTracker::TaskId
GetFavicon(
93 favicon_base::IconType icon_type
,
94 int desired_size_in_dip
,
95 const favicon_base::FaviconResultsCallback
& callback
,
96 base::CancelableTaskTracker
* tracker
);
98 // Set the favicon mappings to |page_url| for |icon_types| in the history
100 // Sample |icon_urls|:
101 // { ICON_URL1 -> TOUCH_ICON, known to the database,
102 // ICON_URL2 -> TOUCH_ICON, not known to the database,
103 // ICON_URL3 -> TOUCH_PRECOMPOSED_ICON, known to the database }
104 // The new mappings are computed from |icon_urls| with these rules:
105 // 1) Any urls in |icon_urls| which are not already known to the database are
107 // Sample new mappings to |page_url|: { ICON_URL1, ICON_URL3 }
108 // 2) If |icon_types| has multiple types, the mappings are only set for the
109 // largest icon type.
110 // Sample new mappings to |page_url|: { ICON_URL3 }
111 // |icon_types| can only have multiple IconTypes if
112 // |icon_types| == TOUCH_ICON | TOUCH_PRECOMPOSED_ICON.
113 // The favicon bitmaps which most closely match |desired_size_in_dip|
114 // at the scale factors supported by the current platform (eg MacOS) in
115 // addition to 1x from the favicons which were just mapped to |page_url| are
116 // returned. If |desired_size_in_dip| is 0, the largest favicon bitmap is
118 base::CancelableTaskTracker::TaskId
UpdateFaviconMappingsAndFetch(
119 const GURL
& page_url
,
120 const std::vector
<GURL
>& icon_urls
,
122 int desired_size_in_dip
,
123 const favicon_base::FaviconResultsCallback
& callback
,
124 base::CancelableTaskTracker
* tracker
);
126 // Requests the favicons of any of |icon_types| whose pixel sizes most
127 // closely match |desired_size_in_dip| and desired scale factors for a web
128 // page URL. If |desired_size_in_dip| is 0, the largest favicon for the web
129 // page URL is returned. |callback| is run when the bits have been fetched.
130 // |icon_types| can be any combination of IconType value, but only one icon
131 // will be returned in the priority of TOUCH_PRECOMPOSED_ICON, TOUCH_ICON and
132 // FAVICON. Each of the three methods below differs in the format of the
133 // callback and the requested scale factors. All of the scale factors
134 // supported by the current platform (eg MacOS) are requested for
135 // GetFaviconImageForURL().
136 // Note. |callback| is always run asynchronously.
137 base::CancelableTaskTracker::TaskId
GetFaviconImageForURL(
138 const FaviconForURLParams
& params
,
139 const favicon_base::FaviconImageCallback
& callback
,
140 base::CancelableTaskTracker
* tracker
);
142 base::CancelableTaskTracker::TaskId
GetRawFaviconForURL(
143 const FaviconForURLParams
& params
,
144 ui::ScaleFactor desired_scale_factor
,
145 const favicon_base::FaviconRawCallback
& callback
,
146 base::CancelableTaskTracker
* tracker
);
148 // See HistoryService::GetLargestFaviconForURL().
149 base::CancelableTaskTracker::TaskId
GetLargestRawFaviconForURL(
151 const GURL
& page_url
,
152 const std::vector
<int>& icon_types
,
153 int minimum_size_in_pixels
,
154 const favicon_base::FaviconRawCallback
& callback
,
155 base::CancelableTaskTracker
* tracker
);
157 base::CancelableTaskTracker::TaskId
GetFaviconForURL(
158 const FaviconForURLParams
& params
,
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
166 base::CancelableTaskTracker::TaskId
GetLargestRawFaviconForID(
167 favicon_base::FaviconID favicon_id
,
168 const favicon_base::FaviconRawCallback
& 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 // Clones all icons from an existing page. This associates the icons from
175 // |old_page_url| with |new_page_url|, provided |new_page_url| has no
176 // recorded associations to any other icons.
177 // Needed if you want to declare favicons (tentatively) in advance, before a
178 // page is ever visited.
179 void CloneFavicon(const GURL
& old_page_url
, const GURL
& new_page_url
);
181 // Allows the importer to set many favicons for many pages at once. The pages
182 // must exist, any favicon sets for unknown pages will be discarded. Existing
183 // favicons will not be overwritten.
184 void SetImportedFavicons(
185 const std::vector
<ImportedFaviconUsage
>& favicon_usage
);
187 // Set the favicon for |page_url| for |icon_type| in the thumbnail database.
188 // Unlike SetFavicons(), this method will not delete preexisting bitmap data
189 // which is associated to |page_url| if at all possible. Use this method if
190 // the favicon bitmaps for any of ui::GetSupportedScaleFactors() are not
192 void MergeFavicon(const GURL
& page_url
,
193 const GURL
& icon_url
,
194 favicon_base::IconType icon_type
,
195 scoped_refptr
<base::RefCountedMemory
> bitmap_data
,
196 const gfx::Size
& pixel_size
);
198 // Set the favicon for |page_url| for |icon_type| in the thumbnail database.
199 // |icon_url| is the single favicon to map to |page_url|. Mappings from
200 // |page_url| to favicons at different icon URLs will be deleted.
201 // A favicon bitmap is added for each image rep in |image|. Any preexisting
202 // bitmap data for |icon_url| is deleted. It is important that |image|
203 // contains image reps for all of ui::GetSupportedScaleFactors(). Use
204 // MergeFavicon() if it does not.
205 // TODO(pkotwicz): Save unresized favicon bitmaps to the database.
206 // TODO(pkotwicz): Support adding favicons for multiple icon URLs to the
207 // thumbnail database.
208 void SetFavicons(const GURL
& page_url
,
209 const GURL
& icon_url
,
210 favicon_base::IconType icon_type
,
211 const gfx::Image
& image
);
213 // Avoid repeated requests to download missing favicon.
214 void UnableToDownloadFavicon(const GURL
& icon_url
);
215 bool WasUnableToDownloadFavicon(const GURL
& icon_url
) const;
216 void ClearUnableToDownloadFavicons();
219 typedef uint32 MissingFaviconURLHash
;
220 base::hash_set
<MissingFaviconURLHash
> missing_favicon_urls_
;
221 HistoryService
* history_service_
;
224 // Helper function for GetFaviconImageForURL(), GetRawFaviconForURL() and
225 // GetFaviconForURL().
226 base::CancelableTaskTracker::TaskId
GetFaviconForURLImpl(
227 const FaviconForURLParams
& params
,
228 const std::vector
<ui::ScaleFactor
>& desired_scale_factors
,
229 const favicon_base::FaviconResultsCallback
& callback
,
230 base::CancelableTaskTracker
* tracker
);
232 // Intermediate callback for GetFaviconImage() and GetFaviconImageForURL()
233 // so that history service can deal solely with FaviconResultsCallback.
234 // Builds favicon_base::FaviconImageResult from |favicon_bitmap_results| and
237 void RunFaviconImageCallbackWithBitmapResults(
238 const favicon_base::FaviconImageCallback
& callback
,
239 int desired_size_in_dip
,
240 const std::vector
<favicon_base::FaviconBitmapResult
>&
241 favicon_bitmap_results
);
243 // Intermediate callback for GetRawFavicon() and GetRawFaviconForURL()
244 // so that history service can deal solely with FaviconResultsCallback.
245 // Resizes favicon_base::FaviconBitmapResult if necessary and runs |callback|.
246 void RunFaviconRawCallbackWithBitmapResults(
247 const favicon_base::FaviconRawCallback
& callback
,
248 int desired_size_in_dip
,
249 ui::ScaleFactor desired_scale_factor
,
250 const std::vector
<favicon_base::FaviconBitmapResult
>&
251 favicon_bitmap_results
);
253 DISALLOW_COPY_AND_ASSIGN(FaviconService
);
256 #endif // CHROME_BROWSER_FAVICON_FAVICON_SERVICE_H_