1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_SUPERVISED_USER_ANDROID_FAVICON_FETCHER_H_
#define CHROME_BROWSER_SUPERVISED_USER_ANDROID_FAVICON_FETCHER_H_
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/task/cancelable_task_tracker.h"
#include "components/favicon/core/large_icon_service.h"
#include "components/favicon_base/favicon_callback.h"
#include "components/favicon_base/favicon_types.h"
#include "url/gurl.h"
class FaviconFetcher {
public:
explicit FaviconFetcher(
raw_ptr<favicon::LargeIconService> large_icon_service);
FaviconFetcher() = delete;
FaviconFetcher(const FaviconFetcher&) = delete;
FaviconFetcher& operator=(const FaviconFetcher&) = delete;
virtual ~FaviconFetcher();
// Initiates a request to fetch a favicon for a specific url.
// Wraps the calls to a service that obtains the favicon and returns
// through the provided callback. The FaviconFetcher will delete
// itself upon callback completion.
void FetchFavicon(
const GURL& url,
bool continue_to_server,
int min_source_side_size_in_pixel,
int desired_side_size_in_pixel,
const base::android::ScopedJavaGlobalRef<jobject>& callback);
base::WeakPtr<FaviconFetcher> GetWeakPtr();
private:
// Wrapper for favicon specs.
struct FaviconDimensions {
int min_source_size_in_pixel;
// Set to zero to return icon without rescaling.
int desired_size_in_pixel;
};
// Provides access to the status of a request for fetching a favicon.
void OnFaviconDownloaded(
const GURL& url,
const base::android::ScopedJavaGlobalRef<jobject>& callback,
FaviconDimensions faviconDimensions,
favicon_base::GoogleFaviconServerRequestStatus status);
// Returns the downloaded favicon through the provided callback.
// If the favicon is not present in cache, requests it from google servers.
void OnGetFaviconFromCacheFinished(
const GURL& url,
bool continue_to_server,
const base::android::ScopedJavaGlobalRef<jobject>& callback,
FaviconDimensions faviconDimensions,
const favicon_base::LargeIconImageResult& image_result);
// Helper method for returning the favicon to the caller.
virtual void ExecuteFaviconCallback(
const base::android::ScopedJavaGlobalRef<jobject>& callback,
SkBitmap bitmap);
// Helper destructor wrapper.
virtual void Destroy();
// Required for execution of icon fetching.
raw_ptr<favicon::LargeIconService> large_icon_service_;
base::CancelableTaskTracker task_tracker_;
base::WeakPtrFactory<FaviconFetcher> weak_factory_{this};
};
#endif // CHROME_BROWSER_SUPERVISED_USER_ANDROID_FAVICON_FETCHER_H_
|