Chromium Code Reviews| OLD | NEW |
|---|---|
| (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"; | |
| 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 void FontFamilyCache::OnPrefsChanged(const std::string& pref_name) { | |
| 68 // The delimiter is '.' | |
| 69 size_t delimiter_length = 1; | |
| 70 for (FontFamilyMap::iterator it = font_family_map_.begin(); | |
| 71 it != font_family_map_.end(); | |
| 72 ++it) { | |
| 73 const char* map_name = it->first; | |
| 74 size_t map_name_length = strlen(map_name); | |
|
Avi (use Gerrit)
2014/08/05 18:07:36
OnPrefsChanged doesn't need to be fast, so can't w
erikchen
2014/08/05 18:45:59
There are 7 entries in the outer hash_map, ~150 pe
| |
| 75 | |
| 76 // If the map name doesn't match, move on. | |
| 77 if (pref_name.compare(0, map_name_length, map_name) != 0) | |
| 78 continue; | |
| 79 | |
| 80 ScriptFontMap& map = it->second; | |
| 81 for (ScriptFontMap::iterator it2 = map.begin(); it2 != map.end(); ++it2) { | |
| 82 const char* script = it2->first; | |
| 83 size_t script_length = strlen(script); | |
| 84 | |
| 85 // If the length doesn't match, move on. | |
| 86 if (pref_name.size() != | |
| 87 map_name_length + script_length + delimiter_length) | |
| 88 continue; | |
| 89 | |
| 90 // If the script doesn't match, move on. | |
| 91 if (pref_name.compare( | |
| 92 map_name_length + delimiter_length, script_length, script) != 0) | |
| 93 continue; | |
| 94 | |
| 95 // Clear the cache and the observer. | |
| 96 map.erase(it2); | |
| 97 profile_pref_registrar_.Remove(pref_name.c_str()); | |
| 98 break; | |
| 99 } | |
| 100 } | |
| 101 } | |
| 102 | |
| 103 } // namespace chrome | |
| OLD | NEW |