blob: 930716ab9a00ca86b2469e555d96076d32387492 [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;
Jan Krcal142a048ed2017-07-13 12:47:3555
56 // TODO(jkrcal): Rename all instances of "MostLikely" to "ChromeSuggestions".
jkrcal43f24f82017-05-12 18:01:5257 void StartFetchMostLikely(const GURL& page_url,
58 const base::Closure& icon_available) override;
mastiz73693f12017-01-27 15:52:5759
60 private:
fhorschig85e2d0b92017-03-23 11:16:3661 using CancelableImageCallback =
62 base::CancelableCallback<void(const gfx::Image&)>;
63
mastiz73693f12017-01-27 15:52:5764 void OnGetFaviconImageForPageURLFinished(
65 PopularSites::Site site,
fhorschigfed34be2017-03-02 23:16:0966 const base::Closure& preliminary_icon_available,
mastiz73693f12017-01-27 15:52:5767 const favicon_base::FaviconImageResult& result);
68
jkrcal43f24f82017-05-12 18:01:5269 void OnPopularSitesFaviconDownloaded(
fhorschig85e2d0b92017-03-23 11:16:3670 PopularSites::Site site,
71 std::unique_ptr<CancelableImageCallback> preliminary_callback,
fhorschig85e2d0b92017-03-23 11:16:3672 const gfx::Image& fetched_image,
73 const image_fetcher::RequestMetadata& metadata);
mastiz73693f12017-01-27 15:52:5774
fhorschig85e2d0b92017-03-23 11:16:3675 std::unique_ptr<CancelableImageCallback> MaybeProvideDefaultIcon(
76 const PopularSites::Site& site,
jkrcalfc015b32017-05-18 17:43:2177 const base::Closure& preliminary_icon_available);
78 void SaveAndNotifyDefaultIconForSite(
79 const PopularSites::Site& site,
80 const base::Closure& preliminary_icon_available,
81 const gfx::Image& image);
82 void SaveIconForSite(const PopularSites::Site& site, const gfx::Image& image);
fhorschigfed34be2017-03-02 23:16:0983
jkrcal43f24f82017-05-12 18:01:5284 void OnGetLargeIconOrFallbackStyleFinished(
85 const GURL& page_url,
jkrcal43f24f82017-05-12 18:01:5286 const favicon_base::LargeIconResult& result);
87
Jan Krcalf3e5733e2017-07-10 09:06:1188 void OnMostLikelyFaviconDownloaded(
89 const GURL& request_url,
90 favicon_base::GoogleFaviconServerRequestStatus status);
jkrcal00addb832017-05-23 16:54:0491
jkrcalfc015b32017-05-18 17:43:2192 bool StartRequest(const GURL& request_url,
93 const base::Closure& icon_available);
94 void FinishRequestAndNotifyIconAvailable(const GURL& request_url,
95 bool newly_available);
jkrcal43f24f82017-05-12 18:01:5296
mastiz73693f12017-01-27 15:52:5797 base::CancelableTaskTracker tracker_;
98 favicon::FaviconService* const favicon_service_;
jkrcal43f24f82017-05-12 18:01:5299 favicon::LargeIconService* const large_icon_service_;
mastiz73693f12017-01-27 15:52:57100 std::unique_ptr<image_fetcher::ImageFetcher> const image_fetcher_;
jkrcalfc015b32017-05-18 17:43:21101 std::map<GURL, std::vector<base::Closure>> in_flight_requests_;
mastiz73693f12017-01-27 15:52:57102
Jeremy Roman5c341f6d2019-07-15 15:56:10103 base::WeakPtrFactory<IconCacherImpl> weak_ptr_factory_{this};
fhorschig85e2d0b92017-03-23 11:16:36104
mastiz73693f12017-01-27 15:52:57105 DISALLOW_COPY_AND_ASSIGN(IconCacherImpl);
106};
107
108} // namespace ntp_tiles
109
110#endif // COMPONENTS_NTP_TILES_ICON_CACHER_IMPL_H_