OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef COMPONENTS_FAVICON_CORE_FAVICON_SERVICE_H_ | 5 #ifndef COMPONENTS_FAVICON_CORE_FAVICON_SERVICE_H_ |
6 #define COMPONENTS_FAVICON_CORE_FAVICON_SERVICE_H_ | 6 #define COMPONENTS_FAVICON_CORE_FAVICON_SERVICE_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <memory> | 10 #include <memory> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/callback.h" | 13 #include "base/callback.h" |
14 #include "base/containers/hash_tables.h" | |
15 #include "base/macros.h" | |
16 #include "base/memory/ref_counted.h" | |
17 #include "base/task/cancelable_task_tracker.h" | 14 #include "base/task/cancelable_task_tracker.h" |
18 #include "components/favicon_base/favicon_callback.h" | 15 #include "components/favicon_base/favicon_callback.h" |
19 #include "components/favicon_base/favicon_types.h" | 16 #include "components/favicon_base/favicon_types.h" |
20 #include "components/favicon_base/favicon_usage_data.h" | 17 #include "components/favicon_base/favicon_usage_data.h" |
21 #include "components/keyed_service/core/keyed_service.h" | 18 #include "components/keyed_service/core/keyed_service.h" |
22 | 19 |
23 class GURL; | 20 class GURL; |
24 | 21 |
25 namespace history { | |
26 class HistoryService; | |
27 } | |
28 | |
29 namespace favicon { | 22 namespace favicon { |
30 | 23 |
31 class FaviconClient; | |
32 | |
33 // The favicon service provides methods to access favicons. It calls the history | |
34 // backend behind the scenes. The callbacks are run asynchronously, even in the | |
35 // case of an error. | |
36 class FaviconService : public KeyedService { | 24 class FaviconService : public KeyedService { |
37 public: | 25 public: |
38 // The FaviconClient must outlive the constructed FaviconService. | |
39 FaviconService(std::unique_ptr<FaviconClient> favicon_client, | |
40 history::HistoryService* history_service); | |
41 | |
42 ~FaviconService() override; | |
43 | |
44 // We usually pass parameters with pointer to avoid copy. This function is a | 26 // We usually pass parameters with pointer to avoid copy. This function is a |
45 // helper to run FaviconResultsCallback with pointer parameters. | 27 // helper to run FaviconResultsCallback with pointer parameters. |
46 static void FaviconResultsCallbackRunner( | 28 static void FaviconResultsCallbackRunner( |
47 const favicon_base::FaviconResultsCallback& callback, | 29 const favicon_base::FaviconResultsCallback& callback, |
48 const std::vector<favicon_base::FaviconRawBitmapResult>* results); | 30 const std::vector<favicon_base::FaviconRawBitmapResult>* results); |
pkotwicz
2017/02/16 04:07:27
In a separate CL we should find a new home for thi
mastiz
2017/02/16 09:46:25
Ack. I think a better alternative is to get rid of
| |
49 | 31 |
50 ////////////////////////////////////////////////////////////////////////////// | 32 ////////////////////////////////////////////////////////////////////////////// |
51 // Methods to request favicon bitmaps from the history backend for |icon_url|. | 33 // Methods to request favicon bitmaps from the history backend for |icon_url|. |
52 // |icon_url| is the URL of the icon itself. | 34 // |icon_url| is the URL of the icon itself. |
53 // (e.g. <http://www.google.com/favicon.ico>) | 35 // (e.g. <http://www.google.com/favicon.ico>) |
54 | 36 |
55 // Requests the favicon at |icon_url| of type favicon_base::FAVICON and of | 37 // Requests the favicon at |icon_url| of type favicon_base::FAVICON and of |
56 // size gfx::kFaviconSize. The returned gfx::Image is populated with | 38 // size gfx::kFaviconSize. The returned gfx::Image is populated with |
57 // representations for all of the scale factors supported by the platform | 39 // representations for all of the scale factors supported by the platform |
58 // (e.g. MacOS). If data is unavailable for some or all of the scale factors, | 40 // (e.g. MacOS). If data is unavailable for some or all of the scale factors, |
59 // the bitmaps with the best matching sizes are resized. | 41 // the bitmaps with the best matching sizes are resized. |
60 base::CancelableTaskTracker::TaskId GetFaviconImage( | 42 virtual base::CancelableTaskTracker::TaskId GetFaviconImage( |
61 const GURL& icon_url, | 43 const GURL& icon_url, |
62 const favicon_base::FaviconImageCallback& callback, | 44 const favicon_base::FaviconImageCallback& callback, |
63 base::CancelableTaskTracker* tracker); | 45 base::CancelableTaskTracker* tracker) = 0; |
64 | 46 |
65 // Requests the favicon at |icon_url| of |icon_type| of size | 47 // Requests the favicon at |icon_url| of |icon_type| of size |
66 // |desired_size_in_pixel|. If there is no favicon of size | 48 // |desired_size_in_pixel|. If there is no favicon of size |
67 // |desired_size_in_pixel|, the favicon bitmap which best matches | 49 // |desired_size_in_pixel|, the favicon bitmap which best matches |
68 // |desired_size_in_pixel| is resized. If |desired_size_in_pixel| is 0, | 50 // |desired_size_in_pixel| is resized. If |desired_size_in_pixel| is 0, |
69 // the largest favicon bitmap is returned. | 51 // the largest favicon bitmap is returned. |
70 base::CancelableTaskTracker::TaskId GetRawFavicon( | 52 virtual base::CancelableTaskTracker::TaskId GetRawFavicon( |
71 const GURL& icon_url, | 53 const GURL& icon_url, |
72 favicon_base::IconType icon_type, | 54 favicon_base::IconType icon_type, |
73 int desired_size_in_pixel, | 55 int desired_size_in_pixel, |
74 const favicon_base::FaviconRawBitmapCallback& callback, | 56 const favicon_base::FaviconRawBitmapCallback& callback, |
75 base::CancelableTaskTracker* tracker); | 57 base::CancelableTaskTracker* tracker) = 0; |
76 | 58 |
77 // The first argument for |callback| is the set of bitmaps for the passed in | 59 // The first argument for |callback| is the set of bitmaps for the passed in |
78 // URL and icon types whose pixel sizes best match the passed in | 60 // URL and icon types whose pixel sizes best match the passed in |
79 // |desired_size_in_dip| at the resource scale factors supported by the | 61 // |desired_size_in_dip| at the resource scale factors supported by the |
80 // current platform (eg MacOS) in addition to 1x. The vector has at most one | 62 // current platform (eg MacOS) in addition to 1x. The vector has at most one |
81 // result for each of the resource scale factors. There are less entries if a | 63 // result for each of the resource scale factors. There are less entries if a |
82 // single/ result is the best bitmap to use for several resource scale | 64 // single/ result is the best bitmap to use for several resource scale |
83 // factors. | 65 // factors. |
84 base::CancelableTaskTracker::TaskId GetFavicon( | 66 virtual base::CancelableTaskTracker::TaskId GetFavicon( |
85 const GURL& icon_url, | 67 const GURL& icon_url, |
86 favicon_base::IconType icon_type, | 68 favicon_base::IconType icon_type, |
87 int desired_size_in_dip, | 69 int desired_size_in_dip, |
88 const favicon_base::FaviconResultsCallback& callback, | 70 const favicon_base::FaviconResultsCallback& callback, |
89 base::CancelableTaskTracker* tracker); | 71 base::CancelableTaskTracker* tracker) = 0; |
90 | 72 |
91 ////////////////////////////////////////////////////////////////////////////// | 73 ////////////////////////////////////////////////////////////////////////////// |
92 // Methods to request favicon bitmaps from the history backend for |page_url|. | 74 // Methods to request favicon bitmaps from the history backend for |page_url|. |
93 // |page_url| is the web page the favicon is associated with. | 75 // |page_url| is the web page the favicon is associated with. |
94 // (e.g. <http://www.google.com>) | 76 // (e.g. <http://www.google.com>) |
95 | 77 |
96 // Requests the favicon for the page at |page_url| of type | 78 // Requests the favicon for the page at |page_url| of type |
97 // favicon_base::FAVICON and of size gfx::kFaviconSize. The returned | 79 // favicon_base::FAVICON and of size gfx::kFaviconSize. The returned |
98 // gfx::Image is populated with representations for all of the scale factors | 80 // gfx::Image is populated with representations for all of the scale factors |
99 // supported by the platform (e.g. MacOS). If data is unavailable for some or | 81 // supported by the platform (e.g. MacOS). If data is unavailable for some or |
100 // all of the scale factors, the bitmaps with the best matching sizes are | 82 // all of the scale factors, the bitmaps with the best matching sizes are |
101 // resized. | 83 // resized. |
102 base::CancelableTaskTracker::TaskId GetFaviconImageForPageURL( | 84 virtual base::CancelableTaskTracker::TaskId GetFaviconImageForPageURL( |
103 const GURL& page_url, | 85 const GURL& page_url, |
104 const favicon_base::FaviconImageCallback& callback, | 86 const favicon_base::FaviconImageCallback& callback, |
105 base::CancelableTaskTracker* tracker); | 87 base::CancelableTaskTracker* tracker) = 0; |
106 | 88 |
107 // Requests the favicon for the page at |page_url| with one of |icon_types| | 89 // Requests the favicon for the page at |page_url| with one of |icon_types| |
108 // and with |desired_size_in_pixel|. |icon_types| can be any combination of | 90 // and with |desired_size_in_pixel|. |icon_types| can be any combination of |
109 // IconTypes. If favicon bitmaps for several IconTypes are available, the | 91 // IconTypes. If favicon bitmaps for several IconTypes are available, the |
110 // favicon bitmap is chosen in the priority of TOUCH_PRECOMPOSED_ICON, | 92 // favicon bitmap is chosen in the priority of TOUCH_PRECOMPOSED_ICON, |
111 // TOUCH_ICON and FAVICON. If there is no favicon bitmap of size | 93 // TOUCH_ICON and FAVICON. If there is no favicon bitmap of size |
112 // |desired_size_in_pixel|, the favicon bitmap which best matches | 94 // |desired_size_in_pixel|, the favicon bitmap which best matches |
113 // |desired_size_in_pixel| is resized. If |desired_size_in_pixel| is 0, | 95 // |desired_size_in_pixel| is resized. If |desired_size_in_pixel| is 0, |
114 // the largest favicon bitmap is returned. Results with a higher priority | 96 // the largest favicon bitmap is returned. Results with a higher priority |
115 // IconType are preferred over an exact match of the favicon bitmap size. | 97 // IconType are preferred over an exact match of the favicon bitmap size. |
116 base::CancelableTaskTracker::TaskId GetRawFaviconForPageURL( | 98 virtual base::CancelableTaskTracker::TaskId GetRawFaviconForPageURL( |
117 const GURL& page_url, | 99 const GURL& page_url, |
118 int icon_types, | 100 int icon_types, |
119 int desired_size_in_pixel, | 101 int desired_size_in_pixel, |
120 const favicon_base::FaviconRawBitmapCallback& callback, | 102 const favicon_base::FaviconRawBitmapCallback& callback, |
121 base::CancelableTaskTracker* tracker); | 103 base::CancelableTaskTracker* tracker) = 0; |
122 | 104 |
123 // See HistoryService::GetLargestFaviconForPageURL(). | 105 // See HistoryService::GetLargestFaviconForPageURL(). |
124 virtual base::CancelableTaskTracker::TaskId GetLargestRawFaviconForPageURL( | 106 virtual base::CancelableTaskTracker::TaskId GetLargestRawFaviconForPageURL( |
125 const GURL& page_url, | 107 const GURL& page_url, |
126 const std::vector<int>& icon_types, | 108 const std::vector<int>& icon_types, |
127 int minimum_size_in_pixels, | 109 int minimum_size_in_pixels, |
128 const favicon_base::FaviconRawBitmapCallback& callback, | 110 const favicon_base::FaviconRawBitmapCallback& callback, |
129 base::CancelableTaskTracker* tracker); | 111 base::CancelableTaskTracker* tracker) = 0; |
130 | 112 |
131 base::CancelableTaskTracker::TaskId GetFaviconForPageURL( | 113 virtual base::CancelableTaskTracker::TaskId GetFaviconForPageURL( |
132 const GURL& page_url, | 114 const GURL& page_url, |
133 int icon_types, | 115 int icon_types, |
134 int desired_size_in_dip, | 116 int desired_size_in_dip, |
135 const favicon_base::FaviconResultsCallback& callback, | 117 const favicon_base::FaviconResultsCallback& callback, |
136 base::CancelableTaskTracker* tracker); | 118 base::CancelableTaskTracker* tracker) = 0; |
137 | 119 |
138 // Set the favicon mappings to |page_url| for |icon_types| in the history | 120 // Set the favicon mappings to |page_url| for |icon_types| in the history |
139 // database. | 121 // database. |
140 // Sample |icon_urls|: | 122 // Sample |icon_urls|: |
141 // { ICON_URL1 -> TOUCH_ICON, known to the database, | 123 // { ICON_URL1 -> TOUCH_ICON, known to the database, |
142 // ICON_URL2 -> TOUCH_ICON, not known to the database, | 124 // ICON_URL2 -> TOUCH_ICON, not known to the database, |
143 // ICON_URL3 -> TOUCH_PRECOMPOSED_ICON, known to the database } | 125 // ICON_URL3 -> TOUCH_PRECOMPOSED_ICON, known to the database } |
144 // The new mappings are computed from |icon_urls| with these rules: | 126 // The new mappings are computed from |icon_urls| with these rules: |
145 // 1) Any urls in |icon_urls| which are not already known to the database are | 127 // 1) Any urls in |icon_urls| which are not already known to the database are |
146 // rejected. | 128 // rejected. |
147 // Sample new mappings to |page_url|: { ICON_URL1, ICON_URL3 } | 129 // Sample new mappings to |page_url|: { ICON_URL1, ICON_URL3 } |
148 // 2) If |icon_types| has multiple types, the mappings are only set for the | 130 // 2) If |icon_types| has multiple types, the mappings are only set for the |
149 // largest icon type. | 131 // largest icon type. |
150 // Sample new mappings to |page_url|: { ICON_URL3 } | 132 // Sample new mappings to |page_url|: { ICON_URL3 } |
151 // |icon_types| can only have multiple IconTypes if | 133 // |icon_types| can only have multiple IconTypes if |
152 // |icon_types| == TOUCH_ICON | TOUCH_PRECOMPOSED_ICON. | 134 // |icon_types| == TOUCH_ICON | TOUCH_PRECOMPOSED_ICON. |
153 // The favicon bitmaps which most closely match |desired_size_in_dip| | 135 // The favicon bitmaps which most closely match |desired_size_in_dip| |
154 // at the reosurce scale factors supported by the current platform (eg MacOS) | 136 // at the reosurce scale factors supported by the current platform (eg MacOS) |
155 // in addition to 1x from the favicons which were just mapped to |page_url| | 137 // in addition to 1x from the favicons which were just mapped to |page_url| |
156 // are returned. If |desired_size_in_dip| is 0, the largest favicon bitmap is | 138 // are returned. If |desired_size_in_dip| is 0, the largest favicon bitmap is |
157 // returned. | 139 // returned. |
158 base::CancelableTaskTracker::TaskId UpdateFaviconMappingsAndFetch( | 140 virtual base::CancelableTaskTracker::TaskId UpdateFaviconMappingsAndFetch( |
159 const GURL& page_url, | 141 const GURL& page_url, |
160 const std::vector<GURL>& icon_urls, | 142 const std::vector<GURL>& icon_urls, |
161 int icon_types, | 143 int icon_types, |
162 int desired_size_in_dip, | 144 int desired_size_in_dip, |
163 const favicon_base::FaviconResultsCallback& callback, | 145 const favicon_base::FaviconResultsCallback& callback, |
164 base::CancelableTaskTracker* tracker); | 146 base::CancelableTaskTracker* tracker) = 0; |
165 | 147 |
166 // Used to request a bitmap for the favicon with |favicon_id| which is not | 148 // Used to request a bitmap for the favicon with |favicon_id| which is not |
167 // resized from the size it is stored at in the database. If there are | 149 // resized from the size it is stored at in the database. If there are |
168 // multiple favicon bitmaps for |favicon_id|, the largest favicon bitmap is | 150 // multiple favicon bitmaps for |favicon_id|, the largest favicon bitmap is |
169 // returned. | 151 // returned. |
170 base::CancelableTaskTracker::TaskId GetLargestRawFaviconForID( | 152 virtual base::CancelableTaskTracker::TaskId GetLargestRawFaviconForID( |
171 favicon_base::FaviconID favicon_id, | 153 favicon_base::FaviconID favicon_id, |
172 const favicon_base::FaviconRawBitmapCallback& callback, | 154 const favicon_base::FaviconRawBitmapCallback& callback, |
173 base::CancelableTaskTracker* tracker); | 155 base::CancelableTaskTracker* tracker) = 0; |
174 | 156 |
175 // Marks all types of favicon for the page as being out of date. | 157 // Marks all types of favicon for the page as being out of date. |
176 void SetFaviconOutOfDateForPage(const GURL& page_url); | 158 virtual void SetFaviconOutOfDateForPage(const GURL& page_url) = 0; |
177 | 159 |
178 // Allows the importer to set many favicons for many pages at once. The pages | 160 // 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 | 161 // must exist, any favicon sets for unknown pages will be discarded. Existing |
180 // favicons will not be overwritten. | 162 // favicons will not be overwritten. |
181 void SetImportedFavicons( | 163 virtual void SetImportedFavicons( |
182 const favicon_base::FaviconUsageDataList& favicon_usage); | 164 const favicon_base::FaviconUsageDataList& favicon_usage) = 0; |
183 | 165 |
184 // Set the favicon for |page_url| for |icon_type| in the thumbnail database. | 166 // Set the favicon for |page_url| for |icon_type| in the thumbnail database. |
185 // Unlike SetFavicons(), this method will not delete preexisting bitmap data | 167 // 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 | 168 // 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 | 169 // the favicon bitmaps for any of ui::GetSupportedScaleFactors() are not |
188 // known. | 170 // known. |
189 void MergeFavicon(const GURL& page_url, | 171 virtual void MergeFavicon(const GURL& page_url, |
190 const GURL& icon_url, | 172 const GURL& icon_url, |
191 favicon_base::IconType icon_type, | 173 favicon_base::IconType icon_type, |
192 scoped_refptr<base::RefCountedMemory> bitmap_data, | 174 scoped_refptr<base::RefCountedMemory> bitmap_data, |
193 const gfx::Size& pixel_size); | 175 const gfx::Size& pixel_size) = 0; |
194 | 176 |
195 // Set the favicon for |page_url| for |icon_type| in the thumbnail database. | 177 // 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 | 178 // |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. | 179 // |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 | 180 // 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| | 181 // bitmap data for |icon_url| is deleted. It is important that |image| |
200 // contains image reps for all of ui::GetSupportedScaleFactors(). Use | 182 // contains image reps for all of ui::GetSupportedScaleFactors(). Use |
201 // MergeFavicon() if it does not. | 183 // MergeFavicon() if it does not. |
202 // TODO(pkotwicz): Save unresized favicon bitmaps to the database. | 184 // TODO(pkotwicz): Save unresized favicon bitmaps to the database. |
203 // TODO(pkotwicz): Support adding favicons for multiple icon URLs to the | 185 // TODO(pkotwicz): Support adding favicons for multiple icon URLs to the |
204 // thumbnail database. | 186 // thumbnail database. |
205 void SetFavicons(const GURL& page_url, | 187 virtual void SetFavicons(const GURL& page_url, |
206 const GURL& icon_url, | 188 const GURL& icon_url, |
207 favicon_base::IconType icon_type, | 189 favicon_base::IconType icon_type, |
208 const gfx::Image& image); | 190 const gfx::Image& image) = 0; |
209 | 191 |
210 // Avoid repeated requests to download missing favicon. | 192 // Avoid repeated requests to download missing favicon. |
211 void UnableToDownloadFavicon(const GURL& icon_url); | 193 virtual void UnableToDownloadFavicon(const GURL& icon_url) = 0; |
212 bool WasUnableToDownloadFavicon(const GURL& icon_url) const; | 194 virtual bool WasUnableToDownloadFavicon(const GURL& icon_url) const = 0; |
213 void ClearUnableToDownloadFavicons(); | 195 virtual void ClearUnableToDownloadFavicons() = 0; |
214 | |
215 private: | |
216 typedef uint32_t 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 std::unique_ptr<FaviconClient> favicon_client_; | |
249 history::HistoryService* history_service_; | |
250 | |
251 DISALLOW_COPY_AND_ASSIGN(FaviconService); | |
252 }; | 196 }; |
253 | 197 |
254 } // namespace favicon | 198 } // namespace favicon |
255 | 199 |
256 #endif // COMPONENTS_FAVICON_CORE_FAVICON_SERVICE_H_ | 200 #endif // COMPONENTS_FAVICON_CORE_FAVICON_SERVICE_H_ |
OLD | NEW |