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/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
12 #include "chrome/browser/cancelable_request.h" | 12 #include "chrome/browser/cancelable_request.h" |
13 #include "chrome/browser/history/history_types.h" | 13 #include "chrome/browser/history/history_types.h" |
14 #include "chrome/browser/profiles/profile_keyed_service.h" | 14 #include "chrome/browser/profiles/profile_keyed_service.h" |
15 #include "chrome/common/ref_counted_util.h" | 15 #include "chrome/common/ref_counted_util.h" |
16 #include "ui/base/layout.h" | |
16 | 17 |
17 class GURL; | 18 class GURL; |
18 class HistoryService; | 19 class HistoryService; |
19 class Profile; | 20 class Profile; |
20 | 21 |
21 // The favicon service provides methods to access favicons. It calls the history | 22 // The favicon service provides methods to access favicons. It calls the history |
22 // backend behind the scenes. | 23 // backend behind the scenes. |
23 // | 24 // |
24 // This service is thread safe. Each request callback is invoked in the | 25 // This service is thread safe. Each request callback is invoked in the |
25 // thread that made the request. | 26 // thread that made the request. |
26 class FaviconService : public CancelableRequestProvider, | 27 class FaviconService : public CancelableRequestProvider, |
27 public ProfileKeyedService { | 28 public ProfileKeyedService { |
28 public: | 29 public: |
29 explicit FaviconService(HistoryService* history_service); | 30 explicit FaviconService(HistoryService* history_service); |
30 | 31 |
31 virtual ~FaviconService(); | 32 virtual ~FaviconService(); |
32 | 33 |
33 // Callback for GetFavicon. If we have previously inquired about the favicon | 34 // Callback for GetFaviconImage() and GetFaviconImageForURL(). |
34 // for this URL, |know_favicon| will be true, and the rest of the fields will | 35 // |FaviconImageResult::image| is constructed from the bitmaps for the |
35 // be valid (otherwise they will be ignored). | 36 // passed in URL and icon types which most which closely match the passed in |
37 // |desired_size_in_dip| at the scale factors supported by the current | |
38 // platform (eg MacOS). | |
39 // |FaviconImageResult::icon_url| is the favicon that the favicon bitmaps in | |
40 // |image| originate from. | |
41 // TODO(pkotwicz): Enable constructing |image| from bitmaps from several | |
42 // icon URLs. | |
43 typedef base::Callback<void(Handle, const history::FaviconImageResult&)> | |
44 FaviconImageCallback; | |
45 | |
46 // Callback for GetRawFavicon() and GetRawFaviconForURL(). | |
47 // FaviconBitmapResult::bitmap_data is the bitmap in the thumbnail database | |
48 // for the passed in URL and icon types whose pixel size best matches the | |
49 // passed in |desired_size_in_dip| and |desired_scale_factor|. Returns an | |
50 // invalid history::FaviconBitmapResult if there are no matches. | |
51 typedef base::Callback<void(Handle, const history::FaviconBitmapResult&)> | |
52 FaviconRawCallback; | |
53 | |
54 // Callback for GetFavicon() and GetFaviconForURL(). | |
36 // | 55 // |
37 // On |know_favicon| == true, |data| will either contain the PNG encoded | 56 // The second argument is the set of bitmaps for the passed in URL and |
38 // favicon data, or it will be NULL to indicate that the site does not have | 57 // icon types whose pixel sizes best match the passed in |
39 // a favicon (in other words, we know the site doesn't have a favicon, as | 58 // |desired_size_in_dip| and |desired_scale_factors|. The vector has at most |
40 // opposed to not knowing anything). |expired| will be set to true if we | 59 // one result for each of |desired_scale_factors|. There are less entries if |
41 // refreshed the favicon "too long" ago and should be updated if the page | 60 // a single result is the best bitmap to use for several scale factors. |
42 // is visited again. | 61 // |
62 // Third argument: | |
63 // a) If the callback is called as a result of GetFaviconForURL(): | |
64 // The third argument is a map of the icon URLs mapped to |page_url| to | |
65 // the sizes at which the favicon is available from the web. | |
66 // b) If the callback is called as a result of GetFavicon() or | |
67 // UpdateFaviconMappingAndFetch(): | |
68 // The third argument is a map with a single element with the passed in | |
69 // |icon_url| to the vector of sizes of the favicon bitmaps at that URL. If | |
70 // |icon_url| is not known to the history backend, an empty map is | |
71 // returned. | |
72 // See history_types.h for more information about IconURLSizesMap. | |
43 typedef base::Callback< | 73 typedef base::Callback< |
44 void(Handle, // handle | 74 void(Handle, // handle |
45 history::FaviconData)> // the type of favicon | 75 std::vector<history::FaviconBitmapResult>, |
46 FaviconDataCallback; | 76 history::IconURLSizesMap)> |
77 FaviconResultsCallback; | |
47 | 78 |
48 typedef CancelableRequest<FaviconDataCallback> GetFaviconRequest; | 79 typedef CancelableRequest<FaviconResultsCallback> GetFaviconRequest; |
49 | 80 |
50 // Requests the |icon_type| of favicon. |consumer| is notified when the bits | 81 // Requests the favicon at |icon_url| of |icon_type| whose size most closely |
51 // have been fetched. |icon_url| is the URL of the icon itself, e.g. | 82 // matches |desired_size_in_dip|. |consumer| is notified when the bits have |
83 // been fetched. |icon_url| is the URL of the icon itself, e.g. | |
52 // <http://www.google.com/favicon.ico>. | 84 // <http://www.google.com/favicon.ico>. |
85 // Each of the three methods below differs in the format of the callback and | |
86 // the requested scale factors. All of the scale factors supported by the | |
87 // current platform (eg MacOS) are requested for GetFaviconImage(). | |
88 Handle GetFaviconImage(const GURL& icon_url, | |
89 history::IconType icon_type, | |
90 int desired_size_in_dip, | |
91 CancelableRequestConsumerBase* consumer, | |
92 const FaviconImageCallback& callback); | |
93 | |
94 Handle GetRawFavicon(const GURL& icon_url, | |
95 history::IconType icon_type, | |
96 int desired_size_in_dip, | |
97 ui::ScaleFactor desired_scale_factor, | |
98 CancelableRequestConsumerBase* consumer, | |
99 const FaviconRawCallback& callback); | |
100 | |
53 Handle GetFavicon(const GURL& icon_url, | 101 Handle GetFavicon(const GURL& icon_url, |
54 history::IconType icon_type, | 102 history::IconType icon_type, |
103 int desired_size_in_dip, | |
104 const std::vector<ui::ScaleFactor>& desired_scale_factors, | |
55 CancelableRequestConsumerBase* consumer, | 105 CancelableRequestConsumerBase* consumer, |
56 const FaviconDataCallback& callback); | 106 const FaviconResultsCallback& callback); |
57 | 107 |
58 // Fetches the |icon_type| of favicon at |icon_url|, sending the results to | 108 // Fetches the |icon_type| of favicon at |icon_url|, sending the results to |
59 // the given |callback|. If the favicon has previously been set via | 109 // the given |callback|. If the favicon has previously been set via |
60 // SetFavicon(), then the favicon URL for |page_url| and all redirects is set | 110 // SetFavicon(), then the favicon URL for |page_url| and all redirects is set |
61 // to |icon_url|. If the favicon has not been set, the database is not | 111 // to |icon_url|. If the favicon has not been set, the database is not |
62 // updated. | 112 // updated. |
63 Handle UpdateFaviconMappingAndFetch(const GURL& page_url, | 113 Handle UpdateFaviconMappingAndFetch(const GURL& page_url, |
64 const GURL& icon_url, | 114 const GURL& icon_url, |
65 history::IconType icon_type, | 115 history::IconType icon_type, |
66 CancelableRequestConsumerBase* consumer, | 116 CancelableRequestConsumerBase* consumer, |
67 const FaviconDataCallback& callback); | 117 const FaviconResultsCallback& callback); |
68 | 118 |
69 // Requests any |icon_types| of favicon for a web page URL. |consumer| is | 119 // Requests the favicons of any of |icon_types| whose pixel sizes most |
70 // notified when the bits have been fetched. |icon_types| can be any | 120 // closely match |desired_size_in_dip| and desired scale factors for a web |
71 // combination of IconType value, but only one icon will be returned in the | 121 // page URL. |consumer| is notified when the bits have been fetched. |
72 // priority of TOUCH_PRECOMPOSED_ICON, TOUCH_ICON and FAVICON. | 122 // |icon_types| can be any combination of IconType value, but only one icon |
73 // | 123 // will be returned in the priority of TOUCH_PRECOMPOSED_ICON, TOUCH_ICON |
74 // Note: this version is intended to be used to retrieve the favicon of a | 124 // and FAVICON. Each of the three methods below differs in the format of the |
75 // page that has been browsed in the past. |expired| in the callback is | 125 // callback and the requested scale factors. All of the scale factors |
76 // always false. | 126 // supported by the current platform (eg MacOS) are requested for |
77 Handle GetFaviconForURL(Profile* profile, | 127 // GetFaviconImageForURL(). |
78 const GURL& page_url, | 128 Handle GetFaviconImageForURL(Profile* profile, |
79 int icon_types, | 129 const GURL& page_url, |
80 CancelableRequestConsumerBase* consumer, | 130 int icon_types, |
81 const FaviconDataCallback& callback); | 131 int desired_size_in_dip, |
132 CancelableRequestConsumerBase* consumer, | |
133 const FaviconImageCallback& callback); | |
82 | 134 |
83 // Requests the favicon for |favicon_id|. The |consumer| is notified when the | 135 Handle GetRawFaviconForURL(Profile* profile, |
84 // bits have been fetched. | 136 const GURL& page_url, |
85 Handle GetFaviconForID(history::FaviconID favicon_id, | 137 int icon_types, |
86 CancelableRequestConsumerBase* consumer, | 138 int desired_size_in_dip, |
87 const FaviconDataCallback& callback); | 139 ui::ScaleFactor desired_scale_factor, |
140 CancelableRequestConsumerBase* consumer, | |
141 const FaviconRawCallback& callback); | |
142 | |
143 Handle GetFaviconForURL( | |
144 Profile* profile, | |
145 const GURL& page_url, | |
146 int icon_types, | |
147 int desired_size_in_dip, | |
148 const std::vector<ui::ScaleFactor>& desired_scale_factors, | |
149 CancelableRequestConsumerBase* consumer, | |
150 const FaviconResultsCallback& callback); | |
151 | |
152 // Requests the favicon for |favicon_id| which most closely matches | |
153 // |desired_size_in_dip| and |desired_scale_factor|. The |consumer| is | |
154 // notified when the bits have been fetched. | |
155 Handle GetRawFaviconForID(history::FaviconID favicon_id, | |
156 int desired_size_in_dip, | |
157 ui::ScaleFactor desired_scale_factor, | |
158 CancelableRequestConsumerBase* consumer, | |
159 const FaviconRawCallback& callback); | |
michaelbai
2012/08/31 21:41:14
I don't understand this method, Is favicon_id the
| |
88 | 160 |
89 // Marks all types of favicon for the page as being out of date. | 161 // Marks all types of favicon for the page as being out of date. |
90 void SetFaviconOutOfDateForPage(const GURL& page_url); | 162 void SetFaviconOutOfDateForPage(const GURL& page_url); |
91 | 163 |
92 // Clones all icons from an existing page. This associates the icons from | 164 // Clones all icons from an existing page. This associates the icons from |
93 // |old_page_url| with |new_page_url|, provided |new_page_url| has no | 165 // |old_page_url| with |new_page_url|, provided |new_page_url| has no |
94 // recorded associations to any other icons. | 166 // recorded associations to any other icons. |
95 // Needed if you want to declare favicons (tentatively) in advance, before a | 167 // Needed if you want to declare favicons (tentatively) in advance, before a |
96 // page is ever visited. | 168 // page is ever visited. |
97 void CloneFavicon(const GURL& old_page_url, const GURL& new_page_url); | 169 void CloneFavicon(const GURL& old_page_url, const GURL& new_page_url); |
98 | 170 |
99 // Allows the importer to set many favicons for many pages at once. The pages | 171 // Allows the importer to set many favicons for many pages at once. The pages |
100 // must exist, any favicon sets for unknown pages will be discarded. Existing | 172 // must exist, any favicon sets for unknown pages will be discarded. Existing |
101 // favicons will not be overwritten. | 173 // favicons will not be overwritten. |
102 void SetImportedFavicons( | 174 void SetImportedFavicons( |
103 const std::vector<history::ImportedFaviconUsage>& favicon_usage); | 175 const std::vector<history::ImportedFaviconUsage>& favicon_usage); |
104 | 176 |
105 // Sets the favicon for a page. | 177 // Sets the favicon for a page. |
106 void SetFavicon(const GURL& page_url, | 178 void SetFavicon(const GURL& page_url, |
107 const GURL& icon_url, | 179 const GURL& icon_url, |
108 const std::vector<unsigned char>& image_data, | 180 const std::vector<unsigned char>& image_data, |
109 history::IconType icon_type); | 181 history::IconType icon_type); |
110 | 182 |
111 private: | 183 private: |
112 HistoryService* history_service_; | 184 HistoryService* history_service_; |
113 | 185 |
114 // Helper to forward an empty result if we cannot get the history service. | 186 // Helper to forward an empty result if we cannot get the history service. |
115 void ForwardEmptyResultAsync(GetFaviconRequest* request); | 187 void ForwardEmptyResultAsync(GetFaviconRequest* request); |
116 | 188 |
189 // Helper function for GetFaviconImageForURL(), GetRawFaviconForURL() and | |
190 // GetFaviconForURL(). | |
191 Handle GetFaviconForURLImpl( | |
192 Profile* profile, | |
193 const GURL& page_url, | |
194 int icon_types, | |
195 int desired_size_in_dip, | |
196 const std::vector<ui::ScaleFactor>& desired_scale_factors, | |
197 CancelableRequestConsumerBase* consumer, | |
198 GetFaviconRequest* request); | |
199 | |
200 // Intermediate callback for GetFaviconImage() and GetFaviconImageForURL() | |
201 // so that history service can deal solely with FaviconResultsCallback. | |
202 // Builds history::FaviconImageResult from |favicon_bitmap_results| and runs | |
203 // |callback|. | |
204 void GetFaviconImageCallback( | |
205 int desired_size_in_dip, | |
206 FaviconImageCallback callback, | |
207 Handle handle, | |
208 std::vector<history::FaviconBitmapResult> favicon_bitmap_results, | |
209 history::IconURLSizesMap icon_url_sizes_map); | |
210 | |
211 // Intermediate callback for GetRawFavicon() and GetRawFaviconForURL() | |
212 // so that history service can deal solely with FaviconResultsCallback. | |
213 // Resizes history::FaviconBitmapResult if necessary and runs |callback|. | |
214 void GetRawFaviconCallback( | |
215 int desired_size_in_dip, | |
216 ui::ScaleFactor desired_scale_factor, | |
217 FaviconRawCallback callback, | |
218 Handle handle, | |
219 std::vector<history::FaviconBitmapResult> favicon_bitmap_results, | |
220 history::IconURLSizesMap icon_url_sizes_map); | |
221 | |
117 DISALLOW_COPY_AND_ASSIGN(FaviconService); | 222 DISALLOW_COPY_AND_ASSIGN(FaviconService); |
118 }; | 223 }; |
119 | 224 |
120 #endif // CHROME_BROWSER_FAVICON_FAVICON_SERVICE_H_ | 225 #endif // CHROME_BROWSER_FAVICON_FAVICON_SERVICE_H_ |
OLD | NEW |