#ifndef CHROME_BROWSER_BITMAP_FETCHER_BITMAP_FETCHER_SERVICE_H_
#define CHROME_BROWSER_BITMAP_FETCHER_BITMAP_FETCHER_SERVICE_H_
#include <memory>
#include <vector>
#include "base/containers/lru_cache.h"
#include "base/functional/callback_forward.h"
#include "base/memory/raw_ptr.h"
#include "chrome/browser/bitmap_fetcher/bitmap_fetcher_delegate.h"
#include "components/keyed_service/core/keyed_service.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
namespace content {
class BrowserContext;
}
namespace data_decoder {
class DataDecoder;
}
class BitmapFetcher;
class BitmapFetcherRequest;
class GURL;
class SkBitmap;
class BitmapFetcherService : public KeyedService, public BitmapFetcherDelegate {
public:
typedef int RequestId;
static const RequestId REQUEST_ID_INVALID = 0;
using BitmapFetchedCallback =
base::OnceCallback<void(const SkBitmap& bitmap)>;
class Observer {
public:
virtual ~Observer() = default;
virtual void OnImageChanged(RequestId request_id,
const SkBitmap& answers_image) = 0;
};
explicit BitmapFetcherService(content::BrowserContext* context);
BitmapFetcherService(const BitmapFetcherService&) = delete;
BitmapFetcherService& operator=(const BitmapFetcherService&) = delete;
~BitmapFetcherService() override;
void CancelRequest(RequestId requestId);
RequestId RequestImage(const GURL& url, BitmapFetchedCallback callback);
void Prefetch(const GURL& url);
bool IsCached(const GURL& url);
protected:
virtual std::unique_ptr<BitmapFetcher> CreateFetcher(
const GURL& url,
const net::NetworkTrafficAnnotationTag& traffic_annotation);
private:
friend class BitmapFetcherServiceTest;
RequestId RequestImageForTesting(
const GURL& url,
BitmapFetchedCallback callback,
const net::NetworkTrafficAnnotationTag& traffic_annotation);
RequestId RequestImageImpl(
const GURL& url,
BitmapFetchedCallback callback,
const net::NetworkTrafficAnnotationTag& traffic_annotation);
const BitmapFetcher* EnsureFetcherForUrl(
const GURL& url,
const net::NetworkTrafficAnnotationTag& traffic_annotation);
const BitmapFetcher* FindFetcherForUrl(const GURL& url);
void RemoveFetcher(const BitmapFetcher* fetcher);
void OnFetchComplete(const GURL& url, const SkBitmap* bitmap) override;
std::unique_ptr<data_decoder::DataDecoder> shared_data_decoder_;
std::vector<std::unique_ptr<BitmapFetcher>> active_fetchers_;
std::vector<std::unique_ptr<BitmapFetcherRequest>> requests_;
struct CacheEntry {
CacheEntry();
~CacheEntry();
std::unique_ptr<const SkBitmap> bitmap;
};
base::LRUCache<GURL, std::unique_ptr<CacheEntry>> cache_;
int current_request_id_;
raw_ptr<content::BrowserContext> context_;
};
#endif