1// Copyright (c) 2011 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_SERVICE_H__
6#define CHROME_BROWSER_FAVICON_SERVICE_H__
7#pragma once
8
9#include <vector>
10
11#include "base/memory/ref_counted.h"
12#include "base/memory/ref_counted_memory.h"
13#include "base/task.h"
14#include "chrome/browser/history/history_types.h"
15#include "chrome/common/ref_counted_util.h"
16#include "content/browser/cancelable_request.h"
17
18class GURL;
19class Profile;
20
21// The favicon service provides methods to access favicons. It calls the history
22// backend behind the scenes.
23//
24// This service is thread safe. Each request callback is invoked in the
25// thread that made the request.
26class FaviconService : public CancelableRequestProvider,
27                       public base::RefCountedThreadSafe<FaviconService> {
28 public:
29  explicit FaviconService(Profile* profile);
30
31  // Callback for GetFavicon. If we have previously inquired about the favicon
32  // for this URL, |know_favicon| will be true, and the rest of the fields will
33  // be valid (otherwise they will be ignored).
34  //
35  // On |know_favicon| == true, |data| will either contain the PNG encoded
36  // favicon data, or it will be NULL to indicate that the site does not have
37  // a favicon (in other words, we know the site doesn't have a favicon, as
38  // opposed to not knowing anything). |expired| will be set to true if we
39  // refreshed the favicon "too long" ago and should be updated if the page
40  // is visited again.
41  typedef Callback2<Handle,                             // handle
42                    history::FaviconData>::Type  // the type of favicon
43                    FaviconDataCallback;
44
45  typedef CancelableRequest<FaviconDataCallback> GetFaviconRequest;
46
47  // Requests the |icon_type| of favicon. |consumer| is notified when the bits
48  // have been fetched.
49  Handle GetFavicon(const GURL& icon_url,
50                    history::IconType icon_type,
51                    CancelableRequestConsumerBase* consumer,
52                    FaviconDataCallback* callback);
53
54  // Fetches the |icon_type| of favicon at |icon_url|, sending the results to
55  // the given |callback|. If the favicon has previously been set via
56  // SetFavicon(), then the favicon URL for |page_url| and all redirects is set
57  // to |icon_url|. If the favicon has not been set, the database is not
58  // updated.
59  Handle UpdateFaviconMappingAndFetch(const GURL& page_url,
60                                      const GURL& icon_url,
61                                      history::IconType icon_type,
62                                      CancelableRequestConsumerBase* consumer,
63                                      FaviconDataCallback* callback);
64
65  // Requests any |icon_types| of favicon for a web page URL. |consumer| is
66  // notified when the bits have been fetched. |icon_types| can be any
67  // combination of IconType value, but only one icon will be returned in the
68  // priority of TOUCH_PRECOMPOSED_ICON, TOUCH_ICON and FAVICON.
69  //
70  // Note: this version is intended to be used to retrieve the favicon of a
71  // page that has been browsed in the past. |expired| in the callback is
72  // always false.
73  Handle GetFaviconForURL(const GURL& page_url,
74                          int icon_types,
75                          CancelableRequestConsumerBase* consumer,
76                          FaviconDataCallback* callback);
77
78  // Marks all types of favicon for the page as being out of date.
79  void SetFaviconOutOfDateForPage(const GURL& page_url);
80
81  // Allows the importer to set many favicons for many pages at once. The pages
82  // must exist, any favicon sets for unknown pages will be discarded. Existing
83  // favicons will not be overwritten.
84  void SetImportedFavicons(
85      const std::vector<history::ImportedFaviconUsage>& favicon_usage);
86
87  // Sets the favicon for a page.
88  void SetFavicon(const GURL& page_url,
89                  const GURL& icon_url,
90                  const std::vector<unsigned char>& image_data,
91                  history::IconType icon_type);
92
93 private:
94  friend class base::RefCountedThreadSafe<FaviconService>;
95
96  ~FaviconService();
97
98  Profile* profile_;
99
100  // Helper to forward an empty result if we cannot get the history service.
101  void ForwardEmptyResultAsync(GetFaviconRequest* request);
102
103  DISALLOW_COPY_AND_ASSIGN(FaviconService);
104};
105
106#endif  // CHROME_BROWSER_FAVICON_SERVICE_H__
107