Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(837)

Side by Side Diff: chrome/browser/font_family_cache.cc

Issue 439913002: Add a cache for FillFontFamilyMap. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Comments from avi, round 2. Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
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 <map>
8
9 #include "base/prefs/pref_service.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/strings/utf_string_conversions.cc"
12 #include "chrome/common/pref_font_webkit_names.h"
13 #include "chrome/common/pref_names.h"
14
15 namespace chrome {
16
17 const char* kFontFamilyCacheKey = "FontFamilyCacheKey";
Lei Zhang 2014/08/05 19:26:49 const char kFontFamilyCacheKey[]
erikchen 2014/08/05 20:49:22 Done.
18
19 FontFamilyCache::FontFamilyCache(PrefService* prefs) : prefs_(prefs) {
20 profile_pref_registrar_.Init(prefs);
21 }
22
23 FontFamilyCache::~FontFamilyCache() {
24 }
25
26 void FontFamilyCache::FillFontFamilyMap(const char* map_name,
27 content::ScriptFontFamilyMap* map) {
28 // TODO(falken): Get rid of the brute-force scan over possible
29 // (font family / script) combinations - see http://crbug.com/308095.
30 for (size_t i = 0; i < prefs::kWebKitScriptsForFontFamilyMapsLength; ++i) {
31 const char* script = prefs::kWebKitScriptsForFontFamilyMaps[i];
32 base::string16 result = FetchAndCacheFont(script, map_name);
33 if (!result.empty())
34 (*map)[script] = result;
35 }
36 }
37
38 base::string16 FontFamilyCache::FetchFont(const char* script,
39 const char* map_name) {
40 std::string pref_name = base::StringPrintf("%s.%s", map_name, script);
41 std::string font = prefs_->GetString(pref_name.c_str());
42 base::string16 font16 = base::UTF8ToUTF16(font);
43
44 // Lazily constructs the map if it doesn't already exist.
45 ScriptFontMap& map = font_family_map_[map_name];
46 map[script] = font16;
47
48 // Register for profile preference changes.
49 profile_pref_registrar_.Add(
50 pref_name.c_str(),
51 base::Bind(&FontFamilyCache::OnPrefsChanged, base::Unretained(this)));
52 return font16;
53 }
54
55 base::string16 FontFamilyCache::FetchAndCacheFont(const char* script,
56 const char* map_name) {
57 FontFamilyMap::const_iterator it = font_family_map_.find(map_name);
58 if (it != font_family_map_.end()) {
59 ScriptFontMap::const_iterator it2 = it->second.find(script);
60 if (it2 != it->second.end())
61 return it2->second;
62 }
63
64 return FetchFont(script, map_name);
65 }
66
67 // There are ~1000 entries in the cache. Avoid unnecessary object construction,
68 // including std::string.
69 void FontFamilyCache::OnPrefsChanged(const std::string& pref_name) {
70 // The delimiter is '.'
71 size_t delimiter_length = 1;
Lei Zhang 2014/08/05 19:26:49 nit: const
erikchen 2014/08/05 20:49:23 Done.
72 for (FontFamilyMap::iterator it = font_family_map_.begin();
Lei Zhang 2014/08/05 19:26:49 Not sure why you are iterating through the key val
erikchen 2014/08/05 20:49:23 I tried out your suggestion. For find() to work, a
Lei Zhang 2014/08/05 21:15:08 Ok, but can't you still split the pref_name to hel
erikchen 2014/08/05 21:34:16 Not without making another assumption about delimi
Lei Zhang 2014/08/05 21:48:55 Ah, n/m, I had a mental model that |pref_name| is
73 it != font_family_map_.end();
74 ++it) {
75 const char* map_name = it->first;
76 size_t map_name_length = strlen(map_name);
77
78 // If the map name doesn't match, move on.
79 if (pref_name.compare(0, map_name_length, map_name) != 0)
80 continue;
81
82 ScriptFontMap& map = it->second;
83 for (ScriptFontMap::iterator it2 = map.begin(); it2 != map.end(); ++it2) {
84 const char* script = it2->first;
85 size_t script_length = strlen(script);
86
87 // If the length doesn't match, move on.
88 if (pref_name.size() !=
89 map_name_length + script_length + delimiter_length)
90 continue;
91
92 // If the script doesn't match, move on.
93 if (pref_name.compare(
94 map_name_length + delimiter_length, script_length, script) != 0)
95 continue;
96
97 // Clear the cache and the observer.
98 map.erase(it2);
99 profile_pref_registrar_.Remove(pref_name.c_str());
100 break;
101 }
102 }
103 }
104
105 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698