#ifndef CONTENT_CHILD_FONT_DATA_FONT_DATA_MANAGER_H_
#define CONTENT_CHILD_FONT_DATA_FONT_DATA_MANAGER_H_
#include <list>
#include <map>
#include <memory>
#include <string>
#include "base/containers/lru_cache.h"
#include "base/hash/hash.h"
#include "base/memory/scoped_refptr.h"
#include "base/synchronization/lock.h"
#include "base/task/single_thread_task_runner.h"
#include "base/threading/sequence_local_storage_slot.h"
#include "base/unguessable_token.h"
#include "components/services/font_data/public/mojom/font_data_service.mojom.h"
#include "content/common/content_export.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "third_party/abseil-cpp/absl/container/flat_hash_map.h"
#include "third_party/freetype_buildflags.h"
#include "third_party/skia/include/core/SkFontMgr.h"
#include "third_party/skia/include/core/SkFontStyle.h"
#include "third_party/skia/include/core/SkStream.h"
#include "third_party/skia/include/core/SkTypeface.h"
namespace base {
class MemoryMappedFile;
}
namespace font_data_service {
class CONTENT_EXPORT FontDataManager : public SkFontMgr {
public:
FontDataManager();
FontDataManager(const FontDataManager&) = delete;
FontDataManager& operator=(const FontDataManager&) = delete;
~FontDataManager() override;
int onCountFamilies() const override;
void onGetFamilyName(int index, SkString* familyName) const override;
sk_sp<SkFontStyleSet> onCreateStyleSet(int index) const override;
sk_sp<SkFontStyleSet> onMatchFamily(const char familyName[]) const override;
sk_sp<SkTypeface> onMatchFamilyStyle(const char familyName[],
const SkFontStyle&) const override;
sk_sp<SkTypeface> onMatchFamilyStyleCharacter(
const char familyName[],
const SkFontStyle&,
const char* bcp47[],
int bcp47Count,
SkUnichar character) const override;
sk_sp<SkTypeface> onMakeFromData(sk_sp<SkData>, int ttcIndex) const override;
sk_sp<SkTypeface> onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset>,
int ttcIndex) const override;
sk_sp<SkTypeface> onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset>,
const SkFontArguments&) const override;
sk_sp<SkTypeface> onMakeFromFile(const char path[],
int ttcIndex) const override;
sk_sp<SkTypeface> onLegacyMakeTypeface(const char familyName[],
SkFontStyle) const override;
void SetFontServiceForTesting(
mojo::PendingRemote<font_data_service::mojom::FontDataService>
font_data_service);
size_t GetMappedFilesCountForTesting() const;
private:
font_data_service::mojom::FontDataService& GetRemoteFontDataService() const;
sk_sp<SkTypeface> CreateTypefaceFromMatchResult(
mojom::MatchFamilyNameResultPtr match_result) const;
void GetAllFamilyNamesLockRequired() const;
struct MatchFamilyRequest {
MatchFamilyRequest(std::optional<std::string> name,
int weight,
int width,
SkFontStyle::Slant slant);
MatchFamilyRequest(const MatchFamilyRequest&);
MatchFamilyRequest(MatchFamilyRequest&&);
~MatchFamilyRequest();
std::optional<std::string> name;
int weight;
int width;
SkFontStyle::Slant slant;
};
std::optional<sk_sp<SkTypeface>> TryGetFromCache(
const MatchFamilyRequest& request) const;
void AddToCache(const MatchFamilyRequest& request,
sk_sp<SkTypeface> typeface) const;
struct MatchFamilyRequestHash {
size_t operator()(const MatchFamilyRequest& key) const {
return base::HashCombine(0ull, key.name, key.weight, key.width,
key.slant);
}
};
struct MatchFamilyRequestEqual {
size_t operator()(const MatchFamilyRequest& lhs,
const MatchFamilyRequest& rhs) const {
return lhs.name == rhs.name && lhs.weight == rhs.weight &&
lhs.width == rhs.width && lhs.slant == rhs.slant;
}
};
mutable base::Lock mapped_regions_lock_;
mutable base::Lock mapped_files_lock_;
mutable base::Lock family_names_lock_;
mutable base::Lock typeface_cache_lock_;
mutable base::HashingLRUCache<MatchFamilyRequest,
sk_sp<SkTypeface>,
MatchFamilyRequestHash,
MatchFamilyRequestEqual>
typeface_cache_ GUARDED_BY(typeface_cache_lock_);
mutable absl::flat_hash_map<base::UnguessableToken,
base::ReadOnlySharedMemoryMapping>
mapped_regions_ GUARDED_BY(mapped_regions_lock_);
mutable absl::flat_hash_map<uint64_t, std::unique_ptr<base::MemoryMappedFile>>
mapped_files_ GUARDED_BY(mapped_files_lock_);
mutable std::vector<std::string> family_names_ GUARDED_BY(family_names_lock_);
#if BUILDFLAG(ENABLE_FREETYPE)
sk_sp<SkFontMgr> custom_fnt_mgr_;
#endif
scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_;
mutable base::SequenceLocalStorageSlot<
mojo::Remote<font_data_service::mojom::FontDataService>>
font_data_service_slot_;
};
}
#endif