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