#include "base/callback.h"
#include "base/containers/hash_tables.h"
#include "base/memory/ref_counted.h"
-#include "chrome/browser/common/cancelable_request.h"
-#include "chrome/common/cancelable_task_tracker.h"
-#include "chrome/common/favicon/favicon_types.h"
-#include "chrome/common/ref_counted_util.h"
-#include "components/browser_context_keyed_service/browser_context_keyed_service.h"
-#include "ui/base/layout.h"
+#include "base/task/cancelable_task_tracker.h"
+#include "components/favicon_base/favicon_callback.h"
+#include "components/favicon_base/favicon_types.h"
+#include "components/keyed_service/core/keyed_service.h"
+class FaviconClient;
class GURL;
class HistoryService;
struct ImportedFaviconUsage;
class Profile;
-namespace chrome {
-struct FaviconImageResult;
-}
-
// The favicon service provides methods to access favicons. It calls the history
-// backend behind the scenes.
-//
-// This service is thread safe. Each request callback is invoked in the
-// thread that made the request.
-class FaviconService : public CancelableRequestProvider,
- public BrowserContextKeyedService {
+// backend behind the scenes. The callbacks are run asynchronously, even in the
+// case of an error.
+class FaviconService : public KeyedService {
public:
- explicit FaviconService(Profile* profile);
+ // TODO(jif): Remove usage of Profile. http://crbug.com/378208.
+ // The FaviconClient must outlive the constructed FaviconService.
+ FaviconService(Profile* profile, FaviconClient* favicon_client);
virtual ~FaviconService();
- // Auxiliary argument structure for requesting favicons for URLs.
- struct FaviconForURLParams {
- FaviconForURLParams(const GURL& page_url,
- int icon_types,
- int desired_size_in_dip)
- : page_url(page_url),
- icon_types(icon_types),
- desired_size_in_dip(desired_size_in_dip) {}
-
- GURL page_url;
- int icon_types;
- int desired_size_in_dip;
- };
-
- // Callback for GetFaviconImage() and GetFaviconImageForURL().
- // |FaviconImageResult::image| is constructed from the bitmaps for the
- // passed in URL and icon types which most which closely match the passed in
- // |desired_size_in_dip| at the scale factors supported by the current
- // platform (eg MacOS) in addition to 1x.
- // |FaviconImageResult::icon_url| is the favicon that the favicon bitmaps in
- // |image| originate from.
- // TODO(pkotwicz): Enable constructing |image| from bitmaps from several
- // icon URLs.
- typedef base::Callback<void(const chrome::FaviconImageResult&)>
- FaviconImageCallback;
-
- // Callback for GetRawFavicon(), GetRawFaviconForURL() and
- // GetLargestRawFavicon().
- // See function for details on value.
- typedef base::Callback<void(const chrome::FaviconBitmapResult&)>
- FaviconRawCallback;
-
- // Callback for GetFavicon() and GetFaviconForURL().
- //
- // The first argument is the set of bitmaps for the passed in URL and
- // icon types whose pixel sizes best match the passed in
- // |desired_size_in_dip| at the scale factors supported by the current
- // platform (eg MacOS) in addition to 1x. The vector has at most one result
- // for each of the scale factors. There are less entries if a single result
- // is the best bitmap to use for several scale factors.
- typedef base::Callback<void(const std::vector<chrome::FaviconBitmapResult>&)>
- FaviconResultsCallback;
-
// We usually pass parameters with pointer to avoid copy. This function is a
// helper to run FaviconResultsCallback with pointer parameters.
static void FaviconResultsCallbackRunner(
- const FaviconResultsCallback& callback,
- const std::vector<chrome::FaviconBitmapResult>* results);
-
- // Requests the favicon at |icon_url| of |icon_type| whose size most closely
- // matches |desired_size_in_dip|. If |desired_size_in_dip| is 0, the largest
- // favicon bitmap at |icon_url| is returned. |consumer| is notified when the
- // bits have been fetched. |icon_url| is the URL of the icon itself, e.g.
- // <http://www.google.com/favicon.ico>.
- // Each of the three methods below differs in the format of the callback and
- // the requested scale factors. All of the scale factors supported by the
- // current platform (eg MacOS) are requested for GetFaviconImage().
- CancelableTaskTracker::TaskId GetFaviconImage(
+ const favicon_base::FaviconResultsCallback& callback,
+ const std::vector<favicon_base::FaviconRawBitmapResult>* results);
+
+ //////////////////////////////////////////////////////////////////////////////
+ // Methods to request favicon bitmaps from the history backend for |icon_url|.
+ // |icon_url| is the URL of the icon itself.
+ // (e.g. <http://www.google.com/favicon.ico>)
+
+ // Requests the favicon at |icon_url| of type favicon_base::FAVICON and of
+ // size gfx::kFaviconSize. The returned gfx::Image is populated with
+ // representations for all of the scale factors supported by the platform
+ // (e.g. MacOS). If data is unavailable for some or all of the scale factors,
+ // the bitmaps with the best matching sizes are resized.
+ base::CancelableTaskTracker::TaskId GetFaviconImage(
const GURL& icon_url,
- chrome::IconType icon_type,
- int desired_size_in_dip,
- const FaviconImageCallback& callback,
- CancelableTaskTracker* tracker);
-
- CancelableTaskTracker::TaskId GetRawFavicon(
+ const favicon_base::FaviconImageCallback& callback,
+ base::CancelableTaskTracker* tracker);
+
+ // Requests the favicon at |icon_url| of |icon_type| of size
+ // |desired_size_in_pixel|. If there is no favicon of size
+ // |desired_size_in_pixel|, the favicon bitmap which best matches
+ // |desired_size_in_pixel| is resized. If |desired_size_in_pixel| is 0,
+ // the largest favicon bitmap is returned.
+ base::CancelableTaskTracker::TaskId GetRawFavicon(
+ const GURL& icon_url,
+ favicon_base::IconType icon_type,
+ int desired_size_in_pixel,
+ const favicon_base::FaviconRawBitmapCallback& callback,
+ base::CancelableTaskTracker* tracker);
+
+ // The first argument for |callback| is the set of bitmaps for the passed in
+ // URL and icon types whose pixel sizes best match the passed in
+ // |desired_size_in_dip| at the resource scale factors supported by the
+ // current platform (eg MacOS) in addition to 1x. The vector has at most one
+ // result for each of the resource scale factors. There are less entries if a
+ // single/ result is the best bitmap to use for several resource scale
+ // factors.
+ base::CancelableTaskTracker::TaskId GetFavicon(
const GURL& icon_url,
- chrome::IconType icon_type,
+ favicon_base::IconType icon_type,
int desired_size_in_dip,
- ui::ScaleFactor desired_scale_factor,
- const FaviconRawCallback& callback,
- CancelableTaskTracker* tracker);
+ const favicon_base::FaviconResultsCallback& callback,
+ base::CancelableTaskTracker* tracker);
+
+ //////////////////////////////////////////////////////////////////////////////
+ // Methods to request favicon bitmaps from the history backend for |page_url|.
+ // |page_url| is the web page the favicon is associated with.
+ // (e.g. <http://www.google.com>)
+
+ // Requests the favicon for the page at |page_url| of type
+ // favicon_base::FAVICON and of size gfx::kFaviconSize. The returned
+ // gfx::Image is populated with representations for all of the scale factors
+ // supported by the platform (e.g. MacOS). If data is unavailable for some or
+ // all of the scale factors, the bitmaps with the best matching sizes are
+ // resized.
+ base::CancelableTaskTracker::TaskId GetFaviconImageForPageURL(
+ const GURL& page_url,
+ const favicon_base::FaviconImageCallback& callback,
+ base::CancelableTaskTracker* tracker);
+
+ // Requests the favicon for the page at |page_url| with one of |icon_types|
+ // and with |desired_size_in_pixel|. |icon_types| can be any combination of
+ // IconTypes. If favicon bitmaps for several IconTypes are available, the
+ // favicon bitmap is chosen in the priority of TOUCH_PRECOMPOSED_ICON,
+ // TOUCH_ICON and FAVICON. If there is no favicon bitmap of size
+ // |desired_size_in_pixel|, the favicon bitmap which best matches
+ // |desired_size_in_pixel| is resized. If |desired_size_in_pixel| is 0,
+ // the largest favicon bitmap is returned. Results with a higher priority
+ // IconType are preferred over an exact match of the favicon bitmap size.
+ base::CancelableTaskTracker::TaskId GetRawFaviconForPageURL(
+ const GURL& page_url,
+ int icon_types,
+ int desired_size_in_pixel,
+ const favicon_base::FaviconRawBitmapCallback& callback,
+ base::CancelableTaskTracker* tracker);
- CancelableTaskTracker::TaskId GetFavicon(
- const GURL& icon_url,
- chrome::IconType icon_type,
+ // See HistoryService::GetLargestFaviconForPageURL().
+ base::CancelableTaskTracker::TaskId GetLargestRawFaviconForPageURL(
+ const GURL& page_url,
+ const std::vector<int>& icon_types,
+ int minimum_size_in_pixels,
+ const favicon_base::FaviconRawBitmapCallback& callback,
+ base::CancelableTaskTracker* tracker);
+
+ base::CancelableTaskTracker::TaskId GetFaviconForPageURL(
+ const GURL& page_url,
+ int icon_types,
int desired_size_in_dip,
- const FaviconResultsCallback& callback,
- CancelableTaskTracker* tracker);
+ const favicon_base::FaviconResultsCallback& callback,
+ base::CancelableTaskTracker* tracker);
// Set the favicon mappings to |page_url| for |icon_types| in the history
// database.
// |icon_types| can only have multiple IconTypes if
// |icon_types| == TOUCH_ICON | TOUCH_PRECOMPOSED_ICON.
// The favicon bitmaps which most closely match |desired_size_in_dip|
- // at the scale factors supported by the current platform (eg MacOS) in
- // addition to 1x from the favicons which were just mapped to |page_url| are
- // returned. If |desired_size_in_dip| is 0, the largest favicon bitmap is
+ // at the reosurce scale factors supported by the current platform (eg MacOS)
+ // in addition to 1x from the favicons which were just mapped to |page_url|
+ // are returned. If |desired_size_in_dip| is 0, the largest favicon bitmap is
// returned.
- CancelableTaskTracker::TaskId UpdateFaviconMappingsAndFetch(
+ base::CancelableTaskTracker::TaskId UpdateFaviconMappingsAndFetch(
const GURL& page_url,
const std::vector<GURL>& icon_urls,
int icon_types,
int desired_size_in_dip,
- const FaviconResultsCallback& callback,
- CancelableTaskTracker* tracker);
-
- // Requests the favicons of any of |icon_types| whose pixel sizes most
- // closely match |desired_size_in_dip| and desired scale factors for a web
- // page URL. If |desired_size_in_dip| is 0, the largest favicon for the web
- // page URL is returned. |callback| is run when the bits have been fetched.
- // |icon_types| can be any combination of IconType value, but only one icon
- // will be returned in the priority of TOUCH_PRECOMPOSED_ICON, TOUCH_ICON and
- // FAVICON. Each of the three methods below differs in the format of the
- // callback and the requested scale factors. All of the scale factors
- // supported by the current platform (eg MacOS) are requested for
- // GetFaviconImageForURL().
- // Note. |callback| is always run asynchronously.
- CancelableTaskTracker::TaskId GetFaviconImageForURL(
- const FaviconForURLParams& params,
- const FaviconImageCallback& callback,
- CancelableTaskTracker* tracker);
-
- CancelableTaskTracker::TaskId GetRawFaviconForURL(
- const FaviconForURLParams& params,
- ui::ScaleFactor desired_scale_factor,
- const FaviconRawCallback& callback,
- CancelableTaskTracker* tracker);
-
- // See HistoryService::GetLargestFaviconForURL().
- CancelableTaskTracker::TaskId GetLargestRawFaviconForURL(
- Profile* profile,
- const GURL& page_url,
- const std::vector<int>& icon_types,
- int minimum_size_in_pixels,
- const FaviconRawCallback& callback,
- CancelableTaskTracker* tracker);
-
- CancelableTaskTracker::TaskId GetFaviconForURL(
- const FaviconForURLParams& params,
- const FaviconResultsCallback& callback,
- CancelableTaskTracker* tracker);
+ const favicon_base::FaviconResultsCallback& callback,
+ base::CancelableTaskTracker* tracker);
// Used to request a bitmap for the favicon with |favicon_id| which is not
// resized from the size it is stored at in the database. If there are
// multiple favicon bitmaps for |favicon_id|, the largest favicon bitmap is
// returned.
- CancelableTaskTracker::TaskId GetLargestRawFaviconForID(
- chrome::FaviconID favicon_id,
- const FaviconRawCallback& callback,
- CancelableTaskTracker* tracker);
+ base::CancelableTaskTracker::TaskId GetLargestRawFaviconForID(
+ favicon_base::FaviconID favicon_id,
+ const favicon_base::FaviconRawBitmapCallback& callback,
+ base::CancelableTaskTracker* tracker);
// Marks all types of favicon for the page as being out of date.
void SetFaviconOutOfDateForPage(const GURL& page_url);
// known.
void MergeFavicon(const GURL& page_url,
const GURL& icon_url,
- chrome::IconType icon_type,
+ favicon_base::IconType icon_type,
scoped_refptr<base::RefCountedMemory> bitmap_data,
const gfx::Size& pixel_size);
// thumbnail database.
void SetFavicons(const GURL& page_url,
const GURL& icon_url,
- chrome::IconType icon_type,
+ favicon_base::IconType icon_type,
const gfx::Image& image);
// Avoid repeated requests to download missing favicon.
base::hash_set<MissingFaviconURLHash> missing_favicon_urls_;
HistoryService* history_service_;
Profile* profile_;
+ FaviconClient* favicon_client_;
- // Helper function for GetFaviconImageForURL(), GetRawFaviconForURL() and
- // GetFaviconForURL().
- CancelableTaskTracker::TaskId GetFaviconForURLImpl(
- const FaviconForURLParams& params,
- const std::vector<ui::ScaleFactor>& desired_scale_factors,
- const FaviconResultsCallback& callback,
- CancelableTaskTracker* tracker);
+ // Helper function for GetFaviconImageForPageURL(), GetRawFaviconForPageURL()
+ // and GetFaviconForPageURL().
+ base::CancelableTaskTracker::TaskId GetFaviconForPageURLImpl(
+ const GURL& page_url,
+ int icon_types,
+ const std::vector<int>& desired_sizes_in_pixel,
+ const favicon_base::FaviconResultsCallback& callback,
+ base::CancelableTaskTracker* tracker);
- // Intermediate callback for GetFaviconImage() and GetFaviconImageForURL()
+ // Intermediate callback for GetFaviconImage() and GetFaviconImageForPageURL()
// so that history service can deal solely with FaviconResultsCallback.
- // Builds chrome::FaviconImageResult from |favicon_bitmap_results| and runs
- // |callback|.
+ // Builds favicon_base::FaviconImageResult from |favicon_bitmap_results| and
+ // runs |callback|.
void RunFaviconImageCallbackWithBitmapResults(
- const FaviconImageCallback& callback,
+ const favicon_base::FaviconImageCallback& callback,
int desired_size_in_dip,
- const std::vector<chrome::FaviconBitmapResult>& favicon_bitmap_results);
+ const std::vector<favicon_base::FaviconRawBitmapResult>&
+ favicon_bitmap_results);
- // Intermediate callback for GetRawFavicon() and GetRawFaviconForURL()
+ // Intermediate callback for GetRawFavicon() and GetRawFaviconForPageURL()
// so that history service can deal solely with FaviconResultsCallback.
- // Resizes chrome::FaviconBitmapResult if necessary and runs |callback|.
- void RunFaviconRawCallbackWithBitmapResults(
- const FaviconRawCallback& callback,
- int desired_size_in_dip,
- ui::ScaleFactor desired_scale_factor,
- const std::vector<chrome::FaviconBitmapResult>& favicon_bitmap_results);
+ // Resizes favicon_base::FaviconRawBitmapResult if necessary and runs
+ // |callback|.
+ void RunFaviconRawBitmapCallbackWithBitmapResults(
+ const favicon_base::FaviconRawBitmapCallback& callback,
+ int desired_size_in_pixel,
+ const std::vector<favicon_base::FaviconRawBitmapResult>&
+ favicon_bitmap_results);
DISALLOW_COPY_AND_ASSIGN(FaviconService);
};