blob: a124b2c21d2c0a72ab1ebd77388cbf0a2c20c77d [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;
mastiz73693f12017-01-27 15:52:5728} // namespace favicon_base
29
30namespace gfx {
31class Image;
32} // namespace gfx
33
34namespace image_fetcher {
35class ImageFetcher;
treib0a6cfc62017-03-20 13:10:3036struct RequestMetadata;
mastiz73693f12017-01-27 15:52:5737} // namespace image_fetcher
38
39namespace ntp_tiles {
40
41class IconCacherImpl : public IconCacher {
42 public:
jkrcal43f24f82017-05-12 18:01:5243 // TODO(jkrcal): Make this eventually use only LargeIconService.
44 // crbug.com/696563
mastiz73693f12017-01-27 15:52:5745 IconCacherImpl(favicon::FaviconService* favicon_service,
jkrcal43f24f82017-05-12 18:01:5246 favicon::LargeIconService* large_icon_service,
mastiz73693f12017-01-27 15:52:5747 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher);
48 ~IconCacherImpl() override;
49
jkrcal43f24f82017-05-12 18:01:5250 void StartFetchPopularSites(
51 PopularSites::Site site,
52 const base::Closure& icon_available,
53 const base::Closure& preliminary_icon_available) override;
54 void StartFetchMostLikely(const GURL& page_url,
55 const base::Closure& icon_available) override;
mastiz73693f12017-01-27 15:52:5756
57 private:
fhorschig85e2d0b92017-03-23 11:16:3658 using CancelableImageCallback =
59 base::CancelableCallback<void(const gfx::Image&)>;
60
mastiz73693f12017-01-27 15:52:5761 void OnGetFaviconImageForPageURLFinished(
62 PopularSites::Site site,
fhorschigfed34be2017-03-02 23:16:0963 const base::Closure& preliminary_icon_available,
mastiz73693f12017-01-27 15:52:5764 const favicon_base::FaviconImageResult& result);
65
jkrcal43f24f82017-05-12 18:01:5266 void OnPopularSitesFaviconDownloaded(
fhorschig85e2d0b92017-03-23 11:16:3667 PopularSites::Site site,
68 std::unique_ptr<CancelableImageCallback> preliminary_callback,
fhorschig85e2d0b92017-03-23 11:16:3669 const std::string& id,
70 const gfx::Image& fetched_image,
71 const image_fetcher::RequestMetadata& metadata);
mastiz73693f12017-01-27 15:52:5772
fhorschig85e2d0b92017-03-23 11:16:3673 std::unique_ptr<CancelableImageCallback> MaybeProvideDefaultIcon(
74 const PopularSites::Site& site,
jkrcalfc015b32017-05-18 17:43:2175 const base::Closure& preliminary_icon_available);
76 void SaveAndNotifyDefaultIconForSite(
77 const PopularSites::Site& site,
78 const base::Closure& preliminary_icon_available,
79 const gfx::Image& image);
80 void SaveIconForSite(const PopularSites::Site& site, const gfx::Image& image);
fhorschigfed34be2017-03-02 23:16:0981
jkrcal43f24f82017-05-12 18:01:5282 void OnGetLargeIconOrFallbackStyleFinished(
83 const GURL& page_url,
jkrcal43f24f82017-05-12 18:01:5284 const favicon_base::LargeIconResult& result);
85
jkrcal00addb832017-05-23 16:54:0486 void OnMostLikelyFaviconDownloaded(const GURL& request_url, bool success);
87
jkrcalfc015b32017-05-18 17:43:2188 bool StartRequest(const GURL& request_url,
89 const base::Closure& icon_available);
90 void FinishRequestAndNotifyIconAvailable(const GURL& request_url,
91 bool newly_available);
jkrcal43f24f82017-05-12 18:01:5292
mastiz73693f12017-01-27 15:52:5793 base::CancelableTaskTracker tracker_;
94 favicon::FaviconService* const favicon_service_;
jkrcal43f24f82017-05-12 18:01:5295 favicon::LargeIconService* const large_icon_service_;
mastiz73693f12017-01-27 15:52:5796 std::unique_ptr<image_fetcher::ImageFetcher> const image_fetcher_;
jkrcalfc015b32017-05-18 17:43:2197 std::map<GURL, std::vector<base::Closure>> in_flight_requests_;
mastiz73693f12017-01-27 15:52:5798
fhorschig85e2d0b92017-03-23 11:16:3699 base::WeakPtrFactory<IconCacherImpl> weak_ptr_factory_;
100
mastiz73693f12017-01-27 15:52:57101 DISALLOW_COPY_AND_ASSIGN(IconCacherImpl);
102};
103
104} // namespace ntp_tiles
105
106#endif // COMPONENTS_NTP_TILES_ICON_CACHER_IMPL_H_