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