#ifndef CEF_TESTS_CEFCLIENT_BROWSER_IMAGE_CACHE_H_
#define CEF_TESTS_CEFCLIENT_BROWSER_IMAGE_CACHE_H_
#pragma once
#include <map>
#include <vector>
#include "include/base/cef_callback.h"
#include "include/base/cef_ref_counted.h"
#include "include/cef_image.h"
#include "include/wrapper/cef_closure_task.h"
#include "include/wrapper/cef_helpers.h"
namespace client {
class ImageCache
: public base::RefCountedThreadSafe<ImageCache, CefDeleteOnUIThread> {
public:
ImageCache();
struct ImageRep {
ImageRep(const std::string& path, float scale_factor);
std::string path_;
float scale_factor_;
};
using ImageRepSet = std::vector<ImageRep>;
struct ImageInfo {
ImageInfo(const std::string& id,
const ImageRepSet& reps,
bool internal,
bool force_reload);
static ImageInfo Empty();
static ImageInfo Create1x(const std::string& id,
const std::string& path_1x,
bool internal);
static ImageInfo Create2x(const std::string& id,
const std::string& path_1x,
const std::string& path_2x,
bool internal);
static ImageInfo Create2x(const std::string& id);
std::string id_;
ImageRepSet reps_;
bool internal_;
bool force_reload_;
};
using ImageInfoSet = std::vector<ImageInfo>;
using ImageSet = std::vector<CefRefPtr<CefImage>>;
using LoadImagesCallback =
base::OnceCallback<void(const ImageSet& )>;
void LoadImages(const ImageInfoSet& image_info, LoadImagesCallback callback);
CefRefPtr<CefImage> GetCachedImage(const std::string& image_id);
private:
friend struct CefDeleteOnThread<TID_UI>;
friend class base::RefCountedThreadSafe<ImageCache, CefDeleteOnUIThread>;
~ImageCache();
enum ImageType {
TYPE_NONE,
TYPE_PNG,
TYPE_JPEG,
};
static ImageType GetImageType(const std::string& path);
struct ImageContent;
using ImageContentSet = std::vector<ImageContent>;
void LoadMissing(const ImageInfoSet& image_info,
const ImageSet& images,
LoadImagesCallback callback);
static bool LoadImageContents(const ImageInfo& info, ImageContent* content);
static bool LoadImageContents(const std::string& path,
bool internal,
ImageType* type,
std::string* contents);
void UpdateCache(const ImageInfoSet& image_info,
const ImageContentSet& contents,
LoadImagesCallback callback);
static CefRefPtr<CefImage> CreateImage(const std::string& image_id,
const ImageContent& content);
using ImageMap = std::map<std::string, CefRefPtr<CefImage>>;
ImageMap image_map_;
};
}
#endif