blob: fce0016844f334fd52cb79854f23b0dc8cc727c7 [file] [log] [blame]
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <memory>
#include <string>
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "base/task/cancelable_task_tracker.h"
#include "components/image_fetcher/core/image_fetcher.h"
#include "components/image_fetcher/core/image_fetcher_types.h"
#include "content/public/browser/url_data_source.h"
class GURL;
class Profile;
class SkBitmap;
namespace favicon_base {
struct FaviconRawBitmapResult;
namespace gfx {
class Image;
// NTP Icon Source is the gateway between network-level chrome: requests for
// NTP icons and the various backends that may serve them.
class NtpIconSource : public content::URLDataSource {
explicit NtpIconSource(Profile* profile);
~NtpIconSource() override;
// content::URLDataSource implementation.
std::string GetSource() const override;
void StartDataRequest(
const std::string& path,
const content::ResourceRequestInfo::WebContentsGetter& wc_getter,
const content::URLDataSource::GotDataCallback& callback) override;
std::string GetMimeType(const std::string& path) const override;
bool ShouldServiceRequest(const GURL& url,
content::ResourceContext* resource_context,
int render_process_id) const override;
struct NtpIconRequest;
void OnLocalFaviconAvailable(
const NtpIconRequest& request,
const favicon_base::FaviconRawBitmapResult& bitmap_result);
// Returns whether |url| is in the set of server suggestions.
bool IsRequestedUrlInServerSuggestions(const GURL& url);
void RequestServerFavicon(const NtpIconRequest& request);
void OnServerFaviconAvailable(const NtpIconRequest& request,
const std::string& id,
const gfx::Image& fetched_image,
const image_fetcher::RequestMetadata& metadata);
// Will call |request.callback| with the rendered icon. |bitmap| can be empty,
// in which case the returned icon is a fallback circle with a letter drawn
// into it.
void ReturnRenderedIconForRequest(const NtpIconRequest& request,
const SkBitmap& bitmap);
base::CancelableTaskTracker cancelable_task_tracker_;
Profile* profile_;
std::unique_ptr<image_fetcher::ImageFetcher> const image_fetcher_;
base::WeakPtrFactory<NtpIconSource> weak_ptr_factory_;