| 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_types.h" | 15 #include "components/favicon_base/favicon_types.h" |
| 15 #include "components/keyed_service/core/keyed_service.h" | 16 #include "components/keyed_service/core/keyed_service.h" |
| 16 #include "ui/base/layout.h" | 17 #include "ui/base/layout.h" |
| 17 | 18 |
| 18 class GURL; | 19 class GURL; |
| 19 class HistoryService; | 20 class HistoryService; |
| 20 struct ImportedFaviconUsage; | 21 struct ImportedFaviconUsage; |
| 21 class Profile; | 22 class Profile; |
| 22 | 23 |
| 23 namespace chrome { | |
| 24 struct FaviconImageResult; | |
| 25 } | |
| 26 | |
| 27 // 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 |
| 28 // backend behind the scenes. | 25 // backend behind the scenes. |
| 29 class FaviconService : public KeyedService { | 26 class FaviconService : public KeyedService { |
| 30 public: | 27 public: |
| 31 explicit FaviconService(Profile* profile); | 28 explicit FaviconService(Profile* profile); |
| 32 | 29 |
| 33 virtual ~FaviconService(); | 30 virtual ~FaviconService(); |
| 34 | 31 |
| 35 // Auxiliary argument structure for requesting favicons for URLs. | 32 // Auxiliary argument structure for requesting favicons for URLs. |
| 36 struct FaviconForURLParams { | 33 struct FaviconForURLParams { |
| 37 FaviconForURLParams(const GURL& page_url, | 34 FaviconForURLParams(const GURL& page_url, |
| 38 int icon_types, | 35 int icon_types, |
| 39 int desired_size_in_dip) | 36 int desired_size_in_dip) |
| 40 : page_url(page_url), | 37 : page_url(page_url), |
| 41 icon_types(icon_types), | 38 icon_types(icon_types), |
| 42 desired_size_in_dip(desired_size_in_dip) {} | 39 desired_size_in_dip(desired_size_in_dip) {} |
| 43 | 40 |
| 44 GURL page_url; | 41 GURL page_url; |
| 45 int icon_types; | 42 int icon_types; |
| 46 int desired_size_in_dip; | 43 int desired_size_in_dip; |
| 47 }; | 44 }; |
| 48 | 45 |
| 49 // Callback for GetFaviconImage() and GetFaviconImageForURL(). | 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); |
| 51 |
| 52 // The first argument of |callback| is a |const FaviconImageResult&|. Of which |
| 50 // |FaviconImageResult::image| is constructed from the bitmaps for the | 53 // |FaviconImageResult::image| is constructed from the bitmaps for the |
| 51 // passed in URL and icon types which most which closely match the passed in | 54 // passed in URL and icon types which most which closely match the passed in |
| 52 // |desired_size_in_dip| at the scale factors supported by the current | 55 // |desired_size_in_dip| at the scale factors supported by the current |
| 53 // platform (eg MacOS) in addition to 1x. | 56 // platform (eg MacOS) in addition to 1x. |
| 54 // |FaviconImageResult::icon_url| is the favicon that the favicon bitmaps in | 57 // |FaviconImageResult::icon_url| is the favicon that the favicon bitmaps in |
| 55 // |image| originate from. | 58 // |image| originate from. |
| 56 // TODO(pkotwicz): Enable constructing |image| from bitmaps from several | 59 // TODO(pkotwicz): Enable constructing |image| from bitmaps from several |
| 57 // icon URLs. | 60 // icon URLs. |
| 58 typedef base::Callback<void(const favicon_base::FaviconImageResult&)> | |
| 59 FaviconImageCallback; | |
| 60 | |
| 61 // Callback for GetRawFavicon(), GetRawFaviconForURL() and | |
| 62 // GetLargestRawFavicon(). | |
| 63 // See function for details on value. | |
| 64 typedef base::Callback<void(const favicon_base::FaviconBitmapResult&)> | |
| 65 FaviconRawCallback; | |
| 66 | |
| 67 // Callback for GetFavicon() and GetFaviconForURL(). | |
| 68 // | |
| 69 // The first argument is the set of bitmaps for the passed in URL and | |
| 70 // icon types whose pixel sizes best match the passed in | |
| 71 // |desired_size_in_dip| at the scale factors supported by the current | |
| 72 // platform (eg MacOS) in addition to 1x. The vector has at most one result | |
| 73 // for each of the scale factors. There are less entries if a single result | |
| 74 // is the best bitmap to use for several scale factors. | |
| 75 typedef base::Callback<void(const std::vector< | |
| 76 favicon_base::FaviconBitmapResult>&)> FaviconResultsCallback; | |
| 77 | |
| 78 // We usually pass parameters with pointer to avoid copy. This function is a | |
| 79 // helper to run FaviconResultsCallback with pointer parameters. | |
| 80 static void FaviconResultsCallbackRunner( | |
| 81 const FaviconResultsCallback& callback, | |
| 82 const std::vector<favicon_base::FaviconBitmapResult>* results); | |
| 83 | 61 |
| 84 // Requests the favicon at |icon_url| of |icon_type| whose size most closely | 62 // Requests the favicon at |icon_url| of |icon_type| whose size most closely |
| 85 // matches |desired_size_in_dip|. If |desired_size_in_dip| is 0, the largest | 63 // matches |desired_size_in_dip|. If |desired_size_in_dip| is 0, the largest |
| 86 // favicon bitmap at |icon_url| is returned. |consumer| is notified when the | 64 // favicon bitmap at |icon_url| is returned. |consumer| is notified when the |
| 87 // bits have been fetched. |icon_url| is the URL of the icon itself, e.g. | 65 // bits have been fetched. |icon_url| is the URL of the icon itself, e.g. |
| 88 // <http://www.google.com/favicon.ico>. | 66 // <http://www.google.com/favicon.ico>. |
| 89 // Each of the three methods below differs in the format of the callback and | 67 // Each of the three methods below differs in the format of the callback and |
| 90 // the requested scale factors. All of the scale factors supported by the | 68 // the requested scale factors. All of the scale factors supported by the |
| 91 // current platform (eg MacOS) are requested for GetFaviconImage(). | 69 // current platform (eg MacOS) are requested for GetFaviconImage(). |
| 92 base::CancelableTaskTracker::TaskId GetFaviconImage( | 70 base::CancelableTaskTracker::TaskId GetFaviconImage( |
| 93 const GURL& icon_url, | 71 const GURL& icon_url, |
| 94 favicon_base::IconType icon_type, | 72 favicon_base::IconType icon_type, |
| 95 int desired_size_in_dip, | 73 int desired_size_in_dip, |
| 96 const FaviconImageCallback& callback, | 74 const favicon_base::FaviconImageCallback& callback, |
| 97 base::CancelableTaskTracker* tracker); | 75 base::CancelableTaskTracker* tracker); |
| 98 | 76 |
| 99 base::CancelableTaskTracker::TaskId GetRawFavicon( | 77 base::CancelableTaskTracker::TaskId GetRawFavicon( |
| 100 const GURL& icon_url, | 78 const GURL& icon_url, |
| 101 favicon_base::IconType icon_type, | 79 favicon_base::IconType icon_type, |
| 102 int desired_size_in_dip, | 80 int desired_size_in_dip, |
| 103 ui::ScaleFactor desired_scale_factor, | 81 ui::ScaleFactor desired_scale_factor, |
| 104 const FaviconRawCallback& callback, | 82 const favicon_base::FaviconRawCallback& callback, |
| 105 base::CancelableTaskTracker* tracker); | 83 base::CancelableTaskTracker* tracker); |
| 106 | 84 |
| 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. |
| 107 base::CancelableTaskTracker::TaskId GetFavicon( | 91 base::CancelableTaskTracker::TaskId GetFavicon( |
| 108 const GURL& icon_url, | 92 const GURL& icon_url, |
| 109 favicon_base::IconType icon_type, | 93 favicon_base::IconType icon_type, |
| 110 int desired_size_in_dip, | 94 int desired_size_in_dip, |
| 111 const FaviconResultsCallback& callback, | 95 const favicon_base::FaviconResultsCallback& callback, |
| 112 base::CancelableTaskTracker* tracker); | 96 base::CancelableTaskTracker* tracker); |
| 113 | 97 |
| 114 // Set the favicon mappings to |page_url| for |icon_types| in the history | 98 // Set the favicon mappings to |page_url| for |icon_types| in the history |
| 115 // database. | 99 // database. |
| 116 // Sample |icon_urls|: | 100 // Sample |icon_urls|: |
| 117 // { ICON_URL1 -> TOUCH_ICON, known to the database, | 101 // { ICON_URL1 -> TOUCH_ICON, known to the database, |
| 118 // ICON_URL2 -> TOUCH_ICON, not known to the database, | 102 // ICON_URL2 -> TOUCH_ICON, not known to the database, |
| 119 // ICON_URL3 -> TOUCH_PRECOMPOSED_ICON, known to the database } | 103 // ICON_URL3 -> TOUCH_PRECOMPOSED_ICON, known to the database } |
| 120 // The new mappings are computed from |icon_urls| with these rules: | 104 // The new mappings are computed from |icon_urls| with these rules: |
| 121 // 1) Any urls in |icon_urls| which are not already known to the database are | 105 // 1) Any urls in |icon_urls| which are not already known to the database are |
| 122 // rejected. | 106 // rejected. |
| 123 // Sample new mappings to |page_url|: { ICON_URL1, ICON_URL3 } | 107 // Sample new mappings to |page_url|: { ICON_URL1, ICON_URL3 } |
| 124 // 2) If |icon_types| has multiple types, the mappings are only set for the | 108 // 2) If |icon_types| has multiple types, the mappings are only set for the |
| 125 // largest icon type. | 109 // largest icon type. |
| 126 // Sample new mappings to |page_url|: { ICON_URL3 } | 110 // Sample new mappings to |page_url|: { ICON_URL3 } |
| 127 // |icon_types| can only have multiple IconTypes if | 111 // |icon_types| can only have multiple IconTypes if |
| 128 // |icon_types| == TOUCH_ICON | TOUCH_PRECOMPOSED_ICON. | 112 // |icon_types| == TOUCH_ICON | TOUCH_PRECOMPOSED_ICON. |
| 129 // The favicon bitmaps which most closely match |desired_size_in_dip| | 113 // The favicon bitmaps which most closely match |desired_size_in_dip| |
| 130 // at the scale factors supported by the current platform (eg MacOS) in | 114 // at the scale factors supported by the current platform (eg MacOS) in |
| 131 // addition to 1x from the favicons which were just mapped to |page_url| are | 115 // addition to 1x from the favicons which were just mapped to |page_url| are |
| 132 // returned. If |desired_size_in_dip| is 0, the largest favicon bitmap is | 116 // returned. If |desired_size_in_dip| is 0, the largest favicon bitmap is |
| 133 // returned. | 117 // returned. |
| 134 base::CancelableTaskTracker::TaskId UpdateFaviconMappingsAndFetch( | 118 base::CancelableTaskTracker::TaskId UpdateFaviconMappingsAndFetch( |
| 135 const GURL& page_url, | 119 const GURL& page_url, |
| 136 const std::vector<GURL>& icon_urls, | 120 const std::vector<GURL>& icon_urls, |
| 137 int icon_types, | 121 int icon_types, |
| 138 int desired_size_in_dip, | 122 int desired_size_in_dip, |
| 139 const FaviconResultsCallback& callback, | 123 const favicon_base::FaviconResultsCallback& callback, |
| 140 base::CancelableTaskTracker* tracker); | 124 base::CancelableTaskTracker* tracker); |
| 141 | 125 |
| 142 // Requests the favicons of any of |icon_types| whose pixel sizes most | 126 // Requests the favicons of any of |icon_types| whose pixel sizes most |
| 143 // closely match |desired_size_in_dip| and desired scale factors for a web | 127 // closely match |desired_size_in_dip| and desired scale factors for a web |
| 144 // page URL. If |desired_size_in_dip| is 0, the largest favicon for the web | 128 // page URL. If |desired_size_in_dip| is 0, the largest favicon for the web |
| 145 // page URL is returned. |callback| is run when the bits have been fetched. | 129 // page URL is returned. |callback| is run when the bits have been fetched. |
| 146 // |icon_types| can be any combination of IconType value, but only one icon | 130 // |icon_types| can be any combination of IconType value, but only one icon |
| 147 // will be returned in the priority of TOUCH_PRECOMPOSED_ICON, TOUCH_ICON and | 131 // will be returned in the priority of TOUCH_PRECOMPOSED_ICON, TOUCH_ICON and |
| 148 // FAVICON. Each of the three methods below differs in the format of the | 132 // FAVICON. Each of the three methods below differs in the format of the |
| 149 // callback and the requested scale factors. All of the scale factors | 133 // callback and the requested scale factors. All of the scale factors |
| 150 // supported by the current platform (eg MacOS) are requested for | 134 // supported by the current platform (eg MacOS) are requested for |
| 151 // GetFaviconImageForURL(). | 135 // GetFaviconImageForURL(). |
| 152 // Note. |callback| is always run asynchronously. | 136 // Note. |callback| is always run asynchronously. |
| 153 base::CancelableTaskTracker::TaskId GetFaviconImageForURL( | 137 base::CancelableTaskTracker::TaskId GetFaviconImageForURL( |
| 154 const FaviconForURLParams& params, | 138 const FaviconForURLParams& params, |
| 155 const FaviconImageCallback& callback, | 139 const favicon_base::FaviconImageCallback& callback, |
| 156 base::CancelableTaskTracker* tracker); | 140 base::CancelableTaskTracker* tracker); |
| 157 | 141 |
| 158 base::CancelableTaskTracker::TaskId GetRawFaviconForURL( | 142 base::CancelableTaskTracker::TaskId GetRawFaviconForURL( |
| 159 const FaviconForURLParams& params, | 143 const FaviconForURLParams& params, |
| 160 ui::ScaleFactor desired_scale_factor, | 144 ui::ScaleFactor desired_scale_factor, |
| 161 const FaviconRawCallback& callback, | 145 const favicon_base::FaviconRawCallback& callback, |
| 162 base::CancelableTaskTracker* tracker); | 146 base::CancelableTaskTracker* tracker); |
| 163 | 147 |
| 164 // See HistoryService::GetLargestFaviconForURL(). | 148 // See HistoryService::GetLargestFaviconForURL(). |
| 165 base::CancelableTaskTracker::TaskId GetLargestRawFaviconForURL( | 149 base::CancelableTaskTracker::TaskId GetLargestRawFaviconForURL( |
| 166 Profile* profile, | 150 Profile* profile, |
| 167 const GURL& page_url, | 151 const GURL& page_url, |
| 168 const std::vector<int>& icon_types, | 152 const std::vector<int>& icon_types, |
| 169 int minimum_size_in_pixels, | 153 int minimum_size_in_pixels, |
| 170 const FaviconRawCallback& callback, | 154 const favicon_base::FaviconRawCallback& callback, |
| 171 base::CancelableTaskTracker* tracker); | 155 base::CancelableTaskTracker* tracker); |
| 172 | 156 |
| 173 base::CancelableTaskTracker::TaskId GetFaviconForURL( | 157 base::CancelableTaskTracker::TaskId GetFaviconForURL( |
| 174 const FaviconForURLParams& params, | 158 const FaviconForURLParams& params, |
| 175 const FaviconResultsCallback& callback, | 159 const favicon_base::FaviconResultsCallback& callback, |
| 176 base::CancelableTaskTracker* tracker); | 160 base::CancelableTaskTracker* tracker); |
| 177 | 161 |
| 178 // Used to request a bitmap for the favicon with |favicon_id| which is not | 162 // Used to request a bitmap for the favicon with |favicon_id| which is not |
| 179 // resized from the size it is stored at in the database. If there are | 163 // resized from the size it is stored at in the database. If there are |
| 180 // multiple favicon bitmaps for |favicon_id|, the largest favicon bitmap is | 164 // multiple favicon bitmaps for |favicon_id|, the largest favicon bitmap is |
| 181 // returned. | 165 // returned. |
| 182 base::CancelableTaskTracker::TaskId GetLargestRawFaviconForID( | 166 base::CancelableTaskTracker::TaskId GetLargestRawFaviconForID( |
| 183 favicon_base::FaviconID favicon_id, | 167 favicon_base::FaviconID favicon_id, |
| 184 const FaviconRawCallback& callback, | 168 const favicon_base::FaviconRawCallback& callback, |
| 185 base::CancelableTaskTracker* tracker); | 169 base::CancelableTaskTracker* tracker); |
| 186 | 170 |
| 187 // Marks all types of favicon for the page as being out of date. | 171 // Marks all types of favicon for the page as being out of date. |
| 188 void SetFaviconOutOfDateForPage(const GURL& page_url); | 172 void SetFaviconOutOfDateForPage(const GURL& page_url); |
| 189 | 173 |
| 190 // Clones all icons from an existing page. This associates the icons from | 174 // Clones all icons from an existing page. This associates the icons from |
| 191 // |old_page_url| with |new_page_url|, provided |new_page_url| has no | 175 // |old_page_url| with |new_page_url|, provided |new_page_url| has no |
| 192 // recorded associations to any other icons. | 176 // recorded associations to any other icons. |
| 193 // Needed if you want to declare favicons (tentatively) in advance, before a | 177 // Needed if you want to declare favicons (tentatively) in advance, before a |
| 194 // page is ever visited. | 178 // page is ever visited. |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 235 typedef uint32 MissingFaviconURLHash; | 219 typedef uint32 MissingFaviconURLHash; |
| 236 base::hash_set<MissingFaviconURLHash> missing_favicon_urls_; | 220 base::hash_set<MissingFaviconURLHash> missing_favicon_urls_; |
| 237 HistoryService* history_service_; | 221 HistoryService* history_service_; |
| 238 Profile* profile_; | 222 Profile* profile_; |
| 239 | 223 |
| 240 // Helper function for GetFaviconImageForURL(), GetRawFaviconForURL() and | 224 // Helper function for GetFaviconImageForURL(), GetRawFaviconForURL() and |
| 241 // GetFaviconForURL(). | 225 // GetFaviconForURL(). |
| 242 base::CancelableTaskTracker::TaskId GetFaviconForURLImpl( | 226 base::CancelableTaskTracker::TaskId GetFaviconForURLImpl( |
| 243 const FaviconForURLParams& params, | 227 const FaviconForURLParams& params, |
| 244 const std::vector<ui::ScaleFactor>& desired_scale_factors, | 228 const std::vector<ui::ScaleFactor>& desired_scale_factors, |
| 245 const FaviconResultsCallback& callback, | 229 const favicon_base::FaviconResultsCallback& callback, |
| 246 base::CancelableTaskTracker* tracker); | 230 base::CancelableTaskTracker* tracker); |
| 247 | 231 |
| 248 // Intermediate callback for GetFaviconImage() and GetFaviconImageForURL() | 232 // Intermediate callback for GetFaviconImage() and GetFaviconImageForURL() |
| 249 // so that history service can deal solely with FaviconResultsCallback. | 233 // so that history service can deal solely with FaviconResultsCallback. |
| 250 // Builds favicon_base::FaviconImageResult from |favicon_bitmap_results| and | 234 // Builds favicon_base::FaviconImageResult from |favicon_bitmap_results| and |
| 251 // runs | 235 // runs |
| 252 // |callback|. | 236 // |callback|. |
| 253 void RunFaviconImageCallbackWithBitmapResults( | 237 void RunFaviconImageCallbackWithBitmapResults( |
| 254 const FaviconImageCallback& callback, | 238 const favicon_base::FaviconImageCallback& callback, |
| 255 int desired_size_in_dip, | 239 int desired_size_in_dip, |
| 256 const std::vector<favicon_base::FaviconBitmapResult>& | 240 const std::vector<favicon_base::FaviconBitmapResult>& |
| 257 favicon_bitmap_results); | 241 favicon_bitmap_results); |
| 258 | 242 |
| 259 // Intermediate callback for GetRawFavicon() and GetRawFaviconForURL() | 243 // Intermediate callback for GetRawFavicon() and GetRawFaviconForURL() |
| 260 // so that history service can deal solely with FaviconResultsCallback. | 244 // so that history service can deal solely with FaviconResultsCallback. |
| 261 // Resizes favicon_base::FaviconBitmapResult if necessary and runs |callback|. | 245 // Resizes favicon_base::FaviconBitmapResult if necessary and runs |callback|. |
| 262 void RunFaviconRawCallbackWithBitmapResults( | 246 void RunFaviconRawCallbackWithBitmapResults( |
| 263 const FaviconRawCallback& callback, | 247 const favicon_base::FaviconRawCallback& callback, |
| 264 int desired_size_in_dip, | 248 int desired_size_in_dip, |
| 265 ui::ScaleFactor desired_scale_factor, | 249 ui::ScaleFactor desired_scale_factor, |
| 266 const std::vector<favicon_base::FaviconBitmapResult>& | 250 const std::vector<favicon_base::FaviconBitmapResult>& |
| 267 favicon_bitmap_results); | 251 favicon_bitmap_results); |
| 268 | 252 |
| 269 DISALLOW_COPY_AND_ASSIGN(FaviconService); | 253 DISALLOW_COPY_AND_ASSIGN(FaviconService); |
| 270 }; | 254 }; |
| 271 | 255 |
| 272 #endif // CHROME_BROWSER_FAVICON_FAVICON_SERVICE_H_ | 256 #endif // CHROME_BROWSER_FAVICON_FAVICON_SERVICE_H_ |
| OLD | NEW |