1 // Copyright 2014 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/font_family_cache.h"
11 #include "base/functional/bind.h"
12 #include "base/memory/ptr_util.h"
13 #include "base/strings/stringprintf.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "chrome/browser/font_pref_change_notifier_factory.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/common/pref_font_webkit_names.h"
18 #include "chrome/common/pref_names.h"
19 #include "components/prefs/pref_service.h"
21 // Identifies the user data on the profile.
22 const char kFontFamilyCacheKey[] = "FontFamilyCacheKey";
24 FontFamilyCache::FontFamilyCache(Profile* profile)
25 : prefs_(profile->GetPrefs()) {
26 font_change_registrar_.Register(
27 FontPrefChangeNotifierFactory::GetForProfile(profile),
28 base::BindRepeating(&FontFamilyCache::OnPrefsChanged,
29 base::Unretained(this)));
32 FontFamilyCache::~FontFamilyCache() = default;
34 void FontFamilyCache::FillFontFamilyMap(
37 blink::web_pref::ScriptFontFamilyMap* map) {
38 FontFamilyCache* cache =
39 static_cast<FontFamilyCache*>(profile->GetUserData(&kFontFamilyCacheKey));
41 cache = new FontFamilyCache(profile);
42 profile->SetUserData(&kFontFamilyCacheKey, base::WrapUnique(cache));
45 cache->FillFontFamilyMap(map_name, map);
48 void FontFamilyCache::FillFontFamilyMap(
50 blink::web_pref::ScriptFontFamilyMap* map) {
51 // TODO(falken): Get rid of the brute-force scan over possible
52 // (font family / script) combinations - see http://crbug.com/308095.
53 for (size_t i = 0; i < prefs::kWebKitScriptsForFontFamilyMapsLength; ++i) {
54 const char* script = prefs::kWebKitScriptsForFontFamilyMaps[i];
55 std::u16string result = FetchAndCacheFont(script, map_name);
57 (*map)[script] = result;
61 std::u16string FontFamilyCache::FetchFont(const char* script,
62 const char* map_name) {
63 std::string pref_name = base::StringPrintf("%s.%s", map_name, script);
64 std::string font = prefs_->GetString(pref_name.c_str());
65 std::u16string font16 = base::UTF8ToUTF16(font);
67 // Lazily constructs the map if it doesn't already exist.
68 ScriptFontMap& map = font_family_map_[map_name];
73 std::u16string FontFamilyCache::FetchAndCacheFont(const char* script,
74 const char* map_name) {
75 FontFamilyMap::const_iterator it = font_family_map_.find(map_name);
76 if (it != font_family_map_.end()) {
77 auto it2 = it->second.find(script);
78 if (it2 != it->second.end())
82 return FetchFont(script, map_name);
85 // There are ~1000 entries in the cache. Avoid unnecessary object construction,
86 // including std::string.
87 void FontFamilyCache::OnPrefsChanged(const std::string& pref_name) {
88 const size_t delimiter_length = 1;
89 const char delimiter = '.';
90 for (auto& it : font_family_map_) {
91 const char* map_name = it.first;
92 size_t map_name_length = strlen(map_name);
94 // If the map name doesn't match, move on.
95 if (pref_name.compare(0, map_name_length, map_name) != 0)
98 ScriptFontMap& map = it.second;
99 for (auto it2 = map.begin(); it2 != map.end(); ++it2) {
100 const char* script = it2->first;
101 size_t script_length = strlen(script);
103 // If the length doesn't match, move on.
104 if (pref_name.size() !=
105 map_name_length + script_length + delimiter_length)
108 // If the script doesn't match, move on.
109 if (pref_name.compare(
110 map_name_length + delimiter_length, script_length, script) != 0)
113 // If the delimiter doesn't match, move on.
114 if (pref_name[map_name_length] != delimiter)