blob: e15ceafab99d3d797addfd26221a87ab73609ab1 [file] [log] [blame]
mastiz73693f12017-01-27 15:52:571// Copyright 2016 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 COMPONENTS_NTP_TILES_ICON_CACHER_IMPL_H_
6#define COMPONENTS_NTP_TILES_ICON_CACHER_IMPL_H_
7
8#include <memory>
jkrcalfc015b32017-05-18 17:43:219#include <set>
mastiz73693f12017-01-27 15:52:5710#include <string>
jkrcalfc015b32017-05-18 17:43:2111#include <vector>
mastiz73693f12017-01-27 15:52:5712
13#include "base/callback.h"
fhorschig85e2d0b92017-03-23 11:16:3614#include "base/cancelable_callback.h"
mastiz73693f12017-01-27 15:52:5715#include "base/memory/weak_ptr.h"
16#include "base/task/cancelable_task_tracker.h"
17#include "components/ntp_tiles/icon_cacher.h"
18#include "components/ntp_tiles/popular_sites.h"
19
20namespace favicon {
21class FaviconService;
jkrcal43f24f82017-05-12 18:01:5222class LargeIconService;
mastiz73693f12017-01-27 15:52:5723} // namespace favicon
24
25namespace favicon_base {
26struct FaviconImageResult;
jkrcal43f24f82017-05-12 18:01:5227struct LargeIconResult;
Jan Krcalf3e5733e2017-07-10 09:06:1128enum class GoogleFaviconServerRequestStatus;
mastiz73693f12017-01-27 15:52:5729} // namespace favicon_base
30
31namespace gfx {
32class Image;
33} // namespace gfx
34
35namespace image_fetcher {
36class ImageFetcher;
treib0a6cfc62017-03-20 13:10:3037struct RequestMetadata;
mastiz73693f12017-01-27 15:52:5738} // namespace image_fetcher
39
40namespace ntp_tiles {
41
42class IconCacherImpl : public IconCacher {
43 public:
jkrcal43f24f82017-05-12 18:01:5244 // TODO(jkrcal): Make this eventually use only LargeIconService.
45 // crbug.com/696563
mastiz73693f12017-01-27 15:52:5746 IconCacherImpl(favicon::FaviconService* favicon_service,
jkrcal43f24f82017-05-12 18:01:5247 favicon::LargeIconService* large_icon_service,
mastiz73693f12017-01-27 15:52:5748 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher);
49 ~IconCacherImpl() override;
50
jkrcal43f24f82017-05-12 18:01:5251 void StartFetchPopularSites(
52 PopularSites::Site site,
53 const base::Closure& icon_available,
54 const base::Closure& preliminary_icon_available) override;
55 void StartFetchMostLikely(const GURL& page_url,
56 const base::Closure& icon_available) override;
mastiz73693f12017-01-27 15:52:5757
58 private:
fhorschig85e2d0b92017-03-23 11:16:3659 using CancelableImageCallback =
60 base::CancelableCallback<void(const gfx::Image&)>;
61
mastiz73693f12017-01-27 15:52:5762 void OnGetFaviconImageForPageURLFinished(
63 PopularSites::Site site,
fhorschigfed34be2017-03-02 23:16:0964 const base::Closure& preliminary_icon_available,
mastiz73693f12017-01-27 15:52:5765 const favicon_base::FaviconImageResult& result);
66
jkrcal43f24f82017-05-12 18:01:5267 void OnPopularSitesFaviconDownloaded(
fhorschig85e2d0b92017-03-23 11:16:3668 PopularSites::Site site,
69 std::unique_ptr<CancelableImageCallback> preliminary_callback,
fhorschig85e2d0b92017-03-23 11:16:3670 const std::string& id,
71 const gfx::Image& fetched_image,
72 const image_fetcher::RequestMetadata& metadata);
mastiz73693f12017-01-27 15:52:5773
fhorschig85e2d0b92017-03-23 11:16:3674 std::unique_ptr<CancelableImageCallback> MaybeProvideDefaultIcon(
75 const PopularSites::Site& site,
jkrcalfc015b32017-05-18 17:43:2176 const base::Closure& preliminary_icon_available);
77 void SaveAndNotifyDefaultIconForSite(
78 const PopularSites::Site& site,
79 const base::Closure& preliminary_icon_available,
80 const gfx::Image& image);
81 void SaveIconForSite(const PopularSites::Site& site, const gfx::Image& image);
fhorschigfed34be2017-03-02 23:16:0982
jkrcal43f24f82017-05-12 18:01:5283 void OnGetLargeIconOrFallbackStyleFinished(
84 const GURL& page_url,
jkrcal43f24f82017-05-12 18:01:5285 const favicon_base::LargeIconResult& result);
86
Jan Krcalf3e5733e2017-07-10 09:06:1187 void OnMostLikelyFaviconDownloaded(
88 const GURL& request_url,
89 favicon_base::GoogleFaviconServerRequestStatus status);
jkrcal00addb832017-05-23 16:54:0490
jkrcalfc015b32017-05-18 17:43:2191 bool StartRequest(const GURL& request_url,
92 const base::Closure& icon_available);
93 void FinishRequestAndNotifyIconAvailable(const GURL& request_url,
94 bool newly_available);
jkrcal43f24f82017-05-12 18:01:5295
mastiz73693f12017-01-27 15:52:5796 base::CancelableTaskTracker tracker_;
97 favicon::FaviconService* const favicon_service_;
jkrcal43f24f82017-05-12 18:01:5298 favicon::LargeIconService* const large_icon_service_;
mastiz73693f12017-01-27 15:52:5799 std::unique_ptr<image_fetcher::ImageFetcher> const image_fetcher_;
jkrcalfc015b32017-05-18 17:43:21100 std::map<GURL, std::vector<base::Closure>> in_flight_requests_;
mastiz73693f12017-01-27 15:52:57101
fhorschig85e2d0b92017-03-23 11:16:36102 base::WeakPtrFactory<IconCacherImpl> weak_ptr_factory_;
103
mastiz73693f12017-01-27 15:52:57104 DISALLOW_COPY_AND_ASSIGN(IconCacherImpl);
105};
106
107} // namespace ntp_tiles
108
109#endif // COMPONENTS_NTP_TILES_ICON_CACHER_IMPL_H_