OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 CHROME_BROWSER_FAVICON_FAVICON_SERVICE_H_ | 5 #ifndef CHROME_BROWSER_FAVICON_FAVICON_SERVICE_H_ |
6 #define CHROME_BROWSER_FAVICON_FAVICON_SERVICE_H_ | 6 #define CHROME_BROWSER_FAVICON_FAVICON_SERVICE_H_ |
7 | 7 |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
11 #include "base/containers/hash_tables.h" | 11 #include "base/containers/hash_tables.h" |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "base/task/cancelable_task_tracker.h" | 13 #include "base/task/cancelable_task_tracker.h" |
14 #include "components/favicon_base/favicon_callback.h" | 14 #include "components/favicon_base/favicon_callback.h" |
15 #include "components/favicon_base/favicon_types.h" | 15 #include "components/favicon_base/favicon_types.h" |
16 #include "components/keyed_service/core/keyed_service.h" | 16 #include "components/keyed_service/core/keyed_service.h" |
17 | 17 |
18 class FaviconClient; | 18 class FaviconClient; |
19 class GURL; | 19 class GURL; |
20 class HistoryService; | 20 class HistoryService; |
21 struct ImportedFaviconUsage; | 21 struct ImportedFaviconUsage; |
22 class Profile; | 22 class Profile; |
23 | 23 |
24 // The favicon service provides methods to access favicons. It calls the history | 24 // The favicon service provides methods to access favicons. It calls the history |
25 // backend behind the scenes. | 25 // backend behind the scenes. The callbacks are run asynchronously, even in the |
| 26 // case of an error. |
26 class FaviconService : public KeyedService { | 27 class FaviconService : public KeyedService { |
27 public: | 28 public: |
28 // TODO(jif): Remove usage of Profile. http://crbug.com/378208. | 29 // TODO(jif): Remove usage of Profile. http://crbug.com/378208. |
29 // The FaviconClient must outlive the constructed FaviconService. | 30 // The FaviconClient must outlive the constructed FaviconService. |
30 FaviconService(Profile* profile, FaviconClient* favicon_client); | 31 FaviconService(Profile* profile, FaviconClient* favicon_client); |
31 | 32 |
32 virtual ~FaviconService(); | 33 virtual ~FaviconService(); |
33 | 34 |
34 // Auxiliary argument structure for requesting favicons for URLs. | |
35 struct FaviconForPageURLParams { | |
36 FaviconForPageURLParams(const GURL& page_url, | |
37 int icon_types, | |
38 int desired_size_in_dip) | |
39 : page_url(page_url), | |
40 icon_types(icon_types), | |
41 desired_size_in_dip(desired_size_in_dip) {} | |
42 | |
43 GURL page_url; | |
44 int icon_types; | |
45 int desired_size_in_dip; | |
46 }; | |
47 | |
48 // We usually pass parameters with pointer to avoid copy. This function is a | 35 // We usually pass parameters with pointer to avoid copy. This function is a |
49 // helper to run FaviconResultsCallback with pointer parameters. | 36 // helper to run FaviconResultsCallback with pointer parameters. |
50 static void FaviconResultsCallbackRunner( | 37 static void FaviconResultsCallbackRunner( |
51 const favicon_base::FaviconResultsCallback& callback, | 38 const favicon_base::FaviconResultsCallback& callback, |
52 const std::vector<favicon_base::FaviconRawBitmapResult>* results); | 39 const std::vector<favicon_base::FaviconRawBitmapResult>* results); |
53 | 40 |
54 // The first argument of |callback| is a |const FaviconImageResult&|. Of which | 41 ////////////////////////////////////////////////////////////////////////////// |
55 // |FaviconImageResult::image| is constructed from the bitmaps for the | 42 // Methods to request favicon bitmaps from the history backend for |icon_url|. |
56 // passed in URL and icon types which most wich closely match the passed in | 43 // |icon_url| is the URL of the icon itself. |
57 // |desired_size_in_dip| at the resource scale factors supported by the | 44 // (e.g. <http://www.google.com/favicon.ico>) |
58 // current platform (eg MacOS) in addition to 1x. | |
59 // |FaviconImageResult::icon_url| is the favicon that the favicon bitmaps in | |
60 // |image| originate from. | |
61 // TODO(pkotwicz): Enable constructing |image| from bitmaps from several | |
62 // icon URLs. | |
63 | 45 |
64 // Requests the favicon at |icon_url| of |icon_type| whose size most closely | 46 // Requests the favicon at |icon_url| of type favicon_base::FAVICON and of |
65 // matches |desired_size_in_dip|. If |desired_size_in_dip| is 0, the largest | 47 // size gfx::kFaviconSize. The returned gfx::Image is populated with |
66 // favicon bitmap at |icon_url| is returned. |consumer| is notified when the | 48 // representations for all of the scale factors supported by the platform |
67 // bits have been fetched. |icon_url| is the URL of the icon itself, e.g. | 49 // (e.g. MacOS). If data is unavailable for some or all of the scale factors, |
68 // <http://www.google.com/favicon.ico>. | 50 // the bitmaps with the best matching sizes are resized. |
69 // Each of the three methods below differs in the format of the callback and | |
70 // the requested scales. All of the resource scale factors supported by the | |
71 // current platform (eg MacOS) are requested for GetFaviconImage(). | |
72 base::CancelableTaskTracker::TaskId GetFaviconImage( | 51 base::CancelableTaskTracker::TaskId GetFaviconImage( |
73 const GURL& icon_url, | 52 const GURL& icon_url, |
74 favicon_base::IconType icon_type, | |
75 int desired_size_in_dip, | |
76 const favicon_base::FaviconImageCallback& callback, | 53 const favicon_base::FaviconImageCallback& callback, |
77 base::CancelableTaskTracker* tracker); | 54 base::CancelableTaskTracker* tracker); |
78 | 55 |
| 56 // Requests the favicon at |icon_url| of |icon_type| of size |
| 57 // |desired_size_in_pixel|. If there is no favicon of size |
| 58 // |desired_size_in_pixel|, the favicon bitmap which best matches |
| 59 // |desired_size_in_pixel| is resized. If |desired_size_in_pixel| is 0, |
| 60 // the largest favicon bitmap is returned. |
79 base::CancelableTaskTracker::TaskId GetRawFavicon( | 61 base::CancelableTaskTracker::TaskId GetRawFavicon( |
80 const GURL& icon_url, | 62 const GURL& icon_url, |
81 favicon_base::IconType icon_type, | 63 favicon_base::IconType icon_type, |
82 int desired_size_in_dip, | 64 int desired_size_in_pixel, |
83 float desired_favicon_scale, | |
84 const favicon_base::FaviconRawBitmapCallback& callback, | 65 const favicon_base::FaviconRawBitmapCallback& callback, |
85 base::CancelableTaskTracker* tracker); | 66 base::CancelableTaskTracker* tracker); |
86 | 67 |
87 // The first argument for |callback| is the set of bitmaps for the passed in | 68 // The first argument for |callback| is the set of bitmaps for the passed in |
88 // URL and icon types whose pixel sizes best match the passed in | 69 // URL and icon types whose pixel sizes best match the passed in |
89 // |desired_size_in_dip| at the resource scale factors supported by the | 70 // |desired_size_in_dip| at the resource scale factors supported by the |
90 // current platform (eg MacOS) in addition to 1x. The vector has at most one | 71 // current platform (eg MacOS) in addition to 1x. The vector has at most one |
91 // result for each of the resource scale factors. There are less entries if a | 72 // result for each of the resource scale factors. There are less entries if a |
92 // single/ result is the best bitmap to use for several resource scale | 73 // single/ result is the best bitmap to use for several resource scale |
93 // factors. | 74 // factors. |
94 base::CancelableTaskTracker::TaskId GetFavicon( | 75 base::CancelableTaskTracker::TaskId GetFavicon( |
95 const GURL& icon_url, | 76 const GURL& icon_url, |
96 favicon_base::IconType icon_type, | 77 favicon_base::IconType icon_type, |
97 int desired_size_in_dip, | 78 int desired_size_in_dip, |
98 const favicon_base::FaviconResultsCallback& callback, | 79 const favicon_base::FaviconResultsCallback& callback, |
99 base::CancelableTaskTracker* tracker); | 80 base::CancelableTaskTracker* tracker); |
100 | 81 |
| 82 ////////////////////////////////////////////////////////////////////////////// |
| 83 // Methods to request favicon bitmaps from the history backend for |page_url|. |
| 84 // |page_url| is the web page the favicon is associated with. |
| 85 // (e.g. <http://www.google.com>) |
| 86 |
| 87 // Requests the favicon for the page at |page_url| of type |
| 88 // favicon_base::FAVICON and of size gfx::kFaviconSize. The returned |
| 89 // gfx::Image is populated with representations for all of the scale factors |
| 90 // supported by the platform (e.g. MacOS). If data is unavailable for some or |
| 91 // all of the scale factors, the bitmaps with the best matching sizes are |
| 92 // resized. |
| 93 base::CancelableTaskTracker::TaskId GetFaviconImageForPageURL( |
| 94 const GURL& page_url, |
| 95 const favicon_base::FaviconImageCallback& callback, |
| 96 base::CancelableTaskTracker* tracker); |
| 97 |
| 98 // Requests the favicon for the page at |page_url| with one of |icon_types| |
| 99 // and with |desired_size_in_pixel|. |icon_types| can be any combination of |
| 100 // IconTypes. If favicon bitmaps for several IconTypes are available, the |
| 101 // favicon bitmap is chosen in the priority of TOUCH_PRECOMPOSED_ICON, |
| 102 // TOUCH_ICON and FAVICON. If there is no favicon bitmap of size |
| 103 // |desired_size_in_pixel|, the favicon bitmap which best matches |
| 104 // |desired_size_in_pixel| is resized. If |desired_size_in_pixel| is 0, |
| 105 // the largest favicon bitmap is returned. Results with a higher priority |
| 106 // IconType are preferred over an exact match of the favicon bitmap size. |
| 107 base::CancelableTaskTracker::TaskId GetRawFaviconForPageURL( |
| 108 const GURL& page_url, |
| 109 int icon_types, |
| 110 int desired_size_in_pixel, |
| 111 const favicon_base::FaviconRawBitmapCallback& callback, |
| 112 base::CancelableTaskTracker* tracker); |
| 113 |
| 114 // See HistoryService::GetLargestFaviconForPageURL(). |
| 115 base::CancelableTaskTracker::TaskId GetLargestRawFaviconForPageURL( |
| 116 const GURL& page_url, |
| 117 const std::vector<int>& icon_types, |
| 118 int minimum_size_in_pixels, |
| 119 const favicon_base::FaviconRawBitmapCallback& callback, |
| 120 base::CancelableTaskTracker* tracker); |
| 121 |
| 122 base::CancelableTaskTracker::TaskId GetFaviconForPageURL( |
| 123 const GURL& page_url, |
| 124 int icon_types, |
| 125 int desired_size_in_dip, |
| 126 const favicon_base::FaviconResultsCallback& callback, |
| 127 base::CancelableTaskTracker* tracker); |
| 128 |
101 // Set the favicon mappings to |page_url| for |icon_types| in the history | 129 // Set the favicon mappings to |page_url| for |icon_types| in the history |
102 // database. | 130 // database. |
103 // Sample |icon_urls|: | 131 // Sample |icon_urls|: |
104 // { ICON_URL1 -> TOUCH_ICON, known to the database, | 132 // { ICON_URL1 -> TOUCH_ICON, known to the database, |
105 // ICON_URL2 -> TOUCH_ICON, not known to the database, | 133 // ICON_URL2 -> TOUCH_ICON, not known to the database, |
106 // ICON_URL3 -> TOUCH_PRECOMPOSED_ICON, known to the database } | 134 // ICON_URL3 -> TOUCH_PRECOMPOSED_ICON, known to the database } |
107 // The new mappings are computed from |icon_urls| with these rules: | 135 // The new mappings are computed from |icon_urls| with these rules: |
108 // 1) Any urls in |icon_urls| which are not already known to the database are | 136 // 1) Any urls in |icon_urls| which are not already known to the database are |
109 // rejected. | 137 // rejected. |
110 // Sample new mappings to |page_url|: { ICON_URL1, ICON_URL3 } | 138 // Sample new mappings to |page_url|: { ICON_URL1, ICON_URL3 } |
111 // 2) If |icon_types| has multiple types, the mappings are only set for the | 139 // 2) If |icon_types| has multiple types, the mappings are only set for the |
112 // largest icon type. | 140 // largest icon type. |
113 // Sample new mappings to |page_url|: { ICON_URL3 } | 141 // Sample new mappings to |page_url|: { ICON_URL3 } |
114 // |icon_types| can only have multiple IconTypes if | 142 // |icon_types| can only have multiple IconTypes if |
115 // |icon_types| == TOUCH_ICON | TOUCH_PRECOMPOSED_ICON. | 143 // |icon_types| == TOUCH_ICON | TOUCH_PRECOMPOSED_ICON. |
116 // The favicon bitmaps which most closely match |desired_size_in_dip| | 144 // The favicon bitmaps which most closely match |desired_size_in_dip| |
117 // at the reosurce scale factors supported by the current platform (eg MacOS) | 145 // at the reosurce scale factors supported by the current platform (eg MacOS) |
118 // in addition to 1x from the favicons which were just mapped to |page_url| | 146 // in addition to 1x from the favicons which were just mapped to |page_url| |
119 // are returned. If |desired_size_in_dip| is 0, the largest favicon bitmap is | 147 // are returned. If |desired_size_in_dip| is 0, the largest favicon bitmap is |
120 // returned. | 148 // returned. |
121 base::CancelableTaskTracker::TaskId UpdateFaviconMappingsAndFetch( | 149 base::CancelableTaskTracker::TaskId UpdateFaviconMappingsAndFetch( |
122 const GURL& page_url, | 150 const GURL& page_url, |
123 const std::vector<GURL>& icon_urls, | 151 const std::vector<GURL>& icon_urls, |
124 int icon_types, | 152 int icon_types, |
125 int desired_size_in_dip, | 153 int desired_size_in_dip, |
126 const favicon_base::FaviconResultsCallback& callback, | 154 const favicon_base::FaviconResultsCallback& callback, |
127 base::CancelableTaskTracker* tracker); | 155 base::CancelableTaskTracker* tracker); |
128 | 156 |
129 // Requests the favicons of any of |icon_types| whose pixel sizes most | |
130 // closely match |desired_size_in_dip| and desired scale for a web | |
131 // page URL. If |desired_size_in_dip| is 0, the largest favicon for the web | |
132 // page URL is returned. |callback| is run when the bits have been fetched. | |
133 // |icon_types| can be any combination of IconType value, but only one icon | |
134 // will be returned in the priority of TOUCH_PRECOMPOSED_ICON, TOUCH_ICON and | |
135 // FAVICON. Each of the three methods below differs in the format of the | |
136 // callback and the requested scales. All of the resource scale factors | |
137 // supported by the current platform (eg MacOS) are requested for | |
138 // GetFaviconImageForPageURL(). | |
139 // Note. |callback| is always run asynchronously. | |
140 base::CancelableTaskTracker::TaskId GetFaviconImageForPageURL( | |
141 const FaviconForPageURLParams& params, | |
142 const favicon_base::FaviconImageCallback& callback, | |
143 base::CancelableTaskTracker* tracker); | |
144 | |
145 base::CancelableTaskTracker::TaskId GetRawFaviconForPageURL( | |
146 const FaviconForPageURLParams& params, | |
147 float desired_favicon_scale, | |
148 const favicon_base::FaviconRawBitmapCallback& callback, | |
149 base::CancelableTaskTracker* tracker); | |
150 | |
151 // See HistoryService::GetLargestFaviconForPageURL(). | |
152 base::CancelableTaskTracker::TaskId GetLargestRawFaviconForPageURL( | |
153 const GURL& page_url, | |
154 const std::vector<int>& icon_types, | |
155 int minimum_size_in_pixels, | |
156 const favicon_base::FaviconRawBitmapCallback& callback, | |
157 base::CancelableTaskTracker* tracker); | |
158 | |
159 base::CancelableTaskTracker::TaskId GetFaviconForPageURL( | |
160 const FaviconForPageURLParams& params, | |
161 const favicon_base::FaviconResultsCallback& callback, | |
162 base::CancelableTaskTracker* tracker); | |
163 | |
164 // Used to request a bitmap for the favicon with |favicon_id| which is not | 157 // Used to request a bitmap for the favicon with |favicon_id| which is not |
165 // resized from the size it is stored at in the database. If there are | 158 // resized from the size it is stored at in the database. If there are |
166 // multiple favicon bitmaps for |favicon_id|, the largest favicon bitmap is | 159 // multiple favicon bitmaps for |favicon_id|, the largest favicon bitmap is |
167 // returned. | 160 // returned. |
168 base::CancelableTaskTracker::TaskId GetLargestRawFaviconForID( | 161 base::CancelableTaskTracker::TaskId GetLargestRawFaviconForID( |
169 favicon_base::FaviconID favicon_id, | 162 favicon_base::FaviconID favicon_id, |
170 const favicon_base::FaviconRawBitmapCallback& callback, | 163 const favicon_base::FaviconRawBitmapCallback& callback, |
171 base::CancelableTaskTracker* tracker); | 164 base::CancelableTaskTracker* tracker); |
172 | 165 |
173 // Marks all types of favicon for the page as being out of date. | 166 // Marks all types of favicon for the page as being out of date. |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 private: | 213 private: |
221 typedef uint32 MissingFaviconURLHash; | 214 typedef uint32 MissingFaviconURLHash; |
222 base::hash_set<MissingFaviconURLHash> missing_favicon_urls_; | 215 base::hash_set<MissingFaviconURLHash> missing_favicon_urls_; |
223 HistoryService* history_service_; | 216 HistoryService* history_service_; |
224 Profile* profile_; | 217 Profile* profile_; |
225 FaviconClient* favicon_client_; | 218 FaviconClient* favicon_client_; |
226 | 219 |
227 // Helper function for GetFaviconImageForPageURL(), GetRawFaviconForPageURL() | 220 // Helper function for GetFaviconImageForPageURL(), GetRawFaviconForPageURL() |
228 // and GetFaviconForPageURL(). | 221 // and GetFaviconForPageURL(). |
229 base::CancelableTaskTracker::TaskId GetFaviconForPageURLImpl( | 222 base::CancelableTaskTracker::TaskId GetFaviconForPageURLImpl( |
230 const FaviconForPageURLParams& params, | 223 const GURL& page_url, |
| 224 int icon_types, |
231 const std::vector<int>& desired_sizes_in_pixel, | 225 const std::vector<int>& desired_sizes_in_pixel, |
232 const favicon_base::FaviconResultsCallback& callback, | 226 const favicon_base::FaviconResultsCallback& callback, |
233 base::CancelableTaskTracker* tracker); | 227 base::CancelableTaskTracker* tracker); |
234 | 228 |
235 // Intermediate callback for GetFaviconImage() and GetFaviconImageForPageURL() | 229 // Intermediate callback for GetFaviconImage() and GetFaviconImageForPageURL() |
236 // so that history service can deal solely with FaviconResultsCallback. | 230 // so that history service can deal solely with FaviconResultsCallback. |
237 // Builds favicon_base::FaviconImageResult from |favicon_bitmap_results| and | 231 // Builds favicon_base::FaviconImageResult from |favicon_bitmap_results| and |
238 // runs |callback|. | 232 // runs |callback|. |
239 void RunFaviconImageCallbackWithBitmapResults( | 233 void RunFaviconImageCallbackWithBitmapResults( |
240 const favicon_base::FaviconImageCallback& callback, | 234 const favicon_base::FaviconImageCallback& callback, |
241 int desired_size_in_dip, | 235 int desired_size_in_dip, |
242 const std::vector<favicon_base::FaviconRawBitmapResult>& | 236 const std::vector<favicon_base::FaviconRawBitmapResult>& |
243 favicon_bitmap_results); | 237 favicon_bitmap_results); |
244 | 238 |
245 // Intermediate callback for GetRawFavicon() and GetRawFaviconForPageURL() | 239 // Intermediate callback for GetRawFavicon() and GetRawFaviconForPageURL() |
246 // so that history service can deal solely with FaviconResultsCallback. | 240 // so that history service can deal solely with FaviconResultsCallback. |
247 // Resizes favicon_base::FaviconRawBitmapResult if necessary and runs | 241 // Resizes favicon_base::FaviconRawBitmapResult if necessary and runs |
248 // |callback|. | 242 // |callback|. |
249 void RunFaviconRawBitmapCallbackWithBitmapResults( | 243 void RunFaviconRawBitmapCallbackWithBitmapResults( |
250 const favicon_base::FaviconRawBitmapCallback& callback, | 244 const favicon_base::FaviconRawBitmapCallback& callback, |
251 int desired_size_in_pixel, | 245 int desired_size_in_pixel, |
252 const std::vector<favicon_base::FaviconRawBitmapResult>& | 246 const std::vector<favicon_base::FaviconRawBitmapResult>& |
253 favicon_bitmap_results); | 247 favicon_bitmap_results); |
254 | 248 |
255 DISALLOW_COPY_AND_ASSIGN(FaviconService); | 249 DISALLOW_COPY_AND_ASSIGN(FaviconService); |
256 }; | 250 }; |
257 | 251 |
258 #endif // CHROME_BROWSER_FAVICON_FAVICON_SERVICE_H_ | 252 #endif // CHROME_BROWSER_FAVICON_FAVICON_SERVICE_H_ |
OLD | NEW |