[M120][Tizen][Onscreen] Fix build errors for TV profile
[platform/framework/web/chromium-efl.git] / chrome / browser / font_family_cache.cc
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.
4
5 #include "chrome/browser/font_family_cache.h"
6
7 #include <stddef.h>
8
9 #include <map>
10
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"
20
21 // Identifies the user data on the profile.
22 const char kFontFamilyCacheKey[] = "FontFamilyCacheKey";
23
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)));
30 }
31
32 FontFamilyCache::~FontFamilyCache() = default;
33
34 void FontFamilyCache::FillFontFamilyMap(
35     Profile* profile,
36     const char* map_name,
37     blink::web_pref::ScriptFontFamilyMap* map) {
38   FontFamilyCache* cache =
39       static_cast<FontFamilyCache*>(profile->GetUserData(&kFontFamilyCacheKey));
40   if (!cache) {
41     cache = new FontFamilyCache(profile);
42     profile->SetUserData(&kFontFamilyCacheKey, base::WrapUnique(cache));
43   }
44
45   cache->FillFontFamilyMap(map_name, map);
46 }
47
48 void FontFamilyCache::FillFontFamilyMap(
49     const char* map_name,
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);
56     if (!result.empty())
57       (*map)[script] = result;
58   }
59 }
60
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);
66
67   // Lazily constructs the map if it doesn't already exist.
68   ScriptFontMap& map = font_family_map_[map_name];
69   map[script] = font16;
70   return font16;
71 }
72
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())
79       return it2->second;
80   }
81
82   return FetchFont(script, map_name);
83 }
84
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);
93
94     // If the map name doesn't match, move on.
95     if (pref_name.compare(0, map_name_length, map_name) != 0)
96       continue;
97
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);
102
103       // If the length doesn't match, move on.
104       if (pref_name.size() !=
105           map_name_length + script_length + delimiter_length)
106         continue;
107
108       // If the script doesn't match, move on.
109       if (pref_name.compare(
110               map_name_length + delimiter_length, script_length, script) != 0)
111         continue;
112
113       // If the delimiter doesn't match, move on.
114       if (pref_name[map_name_length] != delimiter)
115         continue;
116
117       // Clear the cache.
118       map.erase(it2);
119       break;
120     }
121   }
122 }