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