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/keyed_service/core/keyed_service.h" 17 18 class GURL; 19 class HistoryService; 20 struct ImportedFaviconUsage; 21 class Profile; 22 23 // The favicon service provides methods to access favicons. It calls the history 24 // backend behind the scenes. 25 class FaviconService : public KeyedService { 26 public: 27 explicit FaviconService(Profile* profile); 28 29 virtual ~FaviconService(); 30 31 // Auxiliary argument structure for requesting favicons for URLs. 32 struct FaviconForPageURLParams { FaviconForPageURLParamsFaviconForPageURLParams33 FaviconForPageURLParams(const GURL& page_url, 34 int icon_types, 35 int desired_size_in_dip) 36 : page_url(page_url), 37 icon_types(icon_types), 38 desired_size_in_dip(desired_size_in_dip) {} 39 40 GURL page_url; 41 int icon_types; 42 int desired_size_in_dip; 43 }; 44 45 // We usually pass parameters with pointer to avoid copy. This function is a 46 // helper to run FaviconResultsCallback with pointer parameters. 47 static void FaviconResultsCallbackRunner( 48 const favicon_base::FaviconResultsCallback& callback, 49 const std::vector<favicon_base::FaviconRawBitmapResult>* results); 50 51 // The first argument of |callback| is a |const FaviconImageResult&|. Of which 52 // |FaviconImageResult::image| is constructed from the bitmaps for the 53 // passed in URL and icon types which most wich closely match the passed in 54 // |desired_size_in_dip| at the resource scale factors supported by the 55 // current platform (eg MacOS) in addition to 1x. 56 // |FaviconImageResult::icon_url| is the favicon that the favicon bitmaps in 57 // |image| originate from. 58 // TODO(pkotwicz): Enable constructing |image| from bitmaps from several 59 // icon URLs. 60 61 // Requests the favicon at |icon_url| of |icon_type| whose size most closely 62 // matches |desired_size_in_dip|. If |desired_size_in_dip| is 0, the largest 63 // favicon bitmap at |icon_url| is returned. |consumer| is notified when the 64 // bits have been fetched. |icon_url| is the URL of the icon itself, e.g. 65 // <http://www.google.com/favicon.ico>. 66 // Each of the three methods below differs in the format of the callback and 67 // the requested scales. All of the resource scale factors supported by the 68 // current platform (eg MacOS) are requested for GetFaviconImage(). 69 base::CancelableTaskTracker::TaskId GetFaviconImage( 70 const GURL& icon_url, 71 favicon_base::IconType icon_type, 72 int desired_size_in_dip, 73 const favicon_base::FaviconImageCallback& callback, 74 base::CancelableTaskTracker* tracker); 75 76 base::CancelableTaskTracker::TaskId GetRawFavicon( 77 const GURL& icon_url, 78 favicon_base::IconType icon_type, 79 int desired_size_in_dip, 80 float desired_favicon_scale, 81 const favicon_base::FaviconRawBitmapCallback& callback, 82 base::CancelableTaskTracker* tracker); 83 84 // The first argument for |callback| is the set of bitmaps for the passed in 85 // URL and icon types whose pixel sizes best match the passed in 86 // |desired_size_in_dip| at the resource scale factors supported by the 87 // current platform (eg MacOS) in addition to 1x. The vector has at most one 88 // result for each of the resource scale factors. There are less entries if a 89 // single/ result is the best bitmap to use for several resource scale 90 // factors. 91 base::CancelableTaskTracker::TaskId GetFavicon( 92 const GURL& icon_url, 93 favicon_base::IconType icon_type, 94 int desired_size_in_dip, 95 const favicon_base::FaviconResultsCallback& callback, 96 base::CancelableTaskTracker* tracker); 97 98 // Set the favicon mappings to |page_url| for |icon_types| in the history 99 // database. 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 106 // rejected. 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 reosurce scale factors supported by the current platform (eg MacOS) 115 // in addition to 1x from the favicons which were just mapped to |page_url| 116 // are returned. If |desired_size_in_dip| is 0, the largest favicon bitmap is 117 // returned. 118 base::CancelableTaskTracker::TaskId UpdateFaviconMappingsAndFetch( 119 const GURL& page_url, 120 const std::vector<GURL>& icon_urls, 121 int icon_types, 122 int desired_size_in_dip, 123 const favicon_base::FaviconResultsCallback& callback, 124 base::CancelableTaskTracker* tracker); 125 126 // Requests the favicons of any of |icon_types| whose pixel sizes most 127 // closely match |desired_size_in_dip| and desired scale 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 scales. All of the resource scale factors 134 // supported by the current platform (eg MacOS) are requested for 135 // GetFaviconImageForPageURL(). 136 // Note. |callback| is always run asynchronously. 137 base::CancelableTaskTracker::TaskId GetFaviconImageForPageURL( 138 const FaviconForPageURLParams& params, 139 const favicon_base::FaviconImageCallback& callback, 140 base::CancelableTaskTracker* tracker); 141 142 base::CancelableTaskTracker::TaskId GetRawFaviconForPageURL( 143 const FaviconForPageURLParams& params, 144 float desired_favicon_scale, 145 const favicon_base::FaviconRawBitmapCallback& callback, 146 base::CancelableTaskTracker* tracker); 147 148 // See HistoryService::GetLargestFaviconForPageURL(). 149 base::CancelableTaskTracker::TaskId GetLargestRawFaviconForPageURL( 150 Profile* profile, 151 const GURL& page_url, 152 const std::vector<int>& icon_types, 153 int minimum_size_in_pixels, 154 const favicon_base::FaviconRawBitmapCallback& callback, 155 base::CancelableTaskTracker* tracker); 156 157 base::CancelableTaskTracker::TaskId GetFaviconForPageURL( 158 const FaviconForPageURLParams& params, 159 const favicon_base::FaviconResultsCallback& callback, 160 base::CancelableTaskTracker* tracker); 161 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 165 // returned. 166 base::CancelableTaskTracker::TaskId GetLargestRawFaviconForID( 167 favicon_base::FaviconID favicon_id, 168 const favicon_base::FaviconRawBitmapCallback& callback, 169 base::CancelableTaskTracker* tracker); 170 171 // Marks all types of favicon for the page as being out of date. 172 void SetFaviconOutOfDateForPage(const GURL& page_url); 173 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); 180 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); 186 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 191 // known. 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); 197 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); 212 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(); 217 218 private: 219 typedef uint32 MissingFaviconURLHash; 220 base::hash_set<MissingFaviconURLHash> missing_favicon_urls_; 221 HistoryService* history_service_; 222 Profile* profile_; 223 224 // Helper function for GetFaviconImageForPageURL(), GetRawFaviconForPageURL() 225 // and GetFaviconForPageURL(). 226 base::CancelableTaskTracker::TaskId GetFaviconForPageURLImpl( 227 const FaviconForPageURLParams& params, 228 const std::vector<int>& desired_sizes_in_pixel, 229 const favicon_base::FaviconResultsCallback& callback, 230 base::CancelableTaskTracker* tracker); 231 232 // Intermediate callback for GetFaviconImage() and GetFaviconImageForPageURL() 233 // so that history service can deal solely with FaviconResultsCallback. 234 // Builds favicon_base::FaviconImageResult from |favicon_bitmap_results| and 235 // runs |callback|. 236 void RunFaviconImageCallbackWithBitmapResults( 237 const favicon_base::FaviconImageCallback& callback, 238 int desired_size_in_dip, 239 const std::vector<favicon_base::FaviconRawBitmapResult>& 240 favicon_bitmap_results); 241 242 // Intermediate callback for GetRawFavicon() and GetRawFaviconForPageURL() 243 // so that history service can deal solely with FaviconResultsCallback. 244 // Resizes favicon_base::FaviconRawBitmapResult if necessary and runs 245 // |callback|. 246 void RunFaviconRawBitmapCallbackWithBitmapResults( 247 const favicon_base::FaviconRawBitmapCallback& callback, 248 int desired_size_in_pixel, 249 const std::vector<favicon_base::FaviconRawBitmapResult>& 250 favicon_bitmap_results); 251 252 DISALLOW_COPY_AND_ASSIGN(FaviconService); 253 }; 254 255 #endif // CHROME_BROWSER_FAVICON_FAVICON_SERVICE_H_ 256