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/browser/profiles/profile.h" |
| 13 #include "chrome/common/pref_font_webkit_names.h" |
| 14 #include "chrome/common/pref_names.h" |
| 15 |
| 16 // Identifies the user data on the profile. |
| 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(Profile* profile, |
| 27 const char* map_name, |
| 28 content::ScriptFontFamilyMap* map) { |
| 29 FontFamilyCache* cache = |
| 30 static_cast<FontFamilyCache*>(profile->GetUserData(&kFontFamilyCacheKey)); |
| 31 if (!cache) { |
| 32 PrefService* prefs = profile->GetPrefs(); |
| 33 cache = new FontFamilyCache(prefs); |
| 34 // The profile takes ownership of |cache|. |
| 35 profile->SetUserData(&kFontFamilyCacheKey, cache); |
| 36 } |
| 37 |
| 38 cache->FillFontFamilyMap(map_name, map); |
| 39 } |
| 40 |
| 41 void FontFamilyCache::FillFontFamilyMap(const char* map_name, |
| 42 content::ScriptFontFamilyMap* map) { |
| 43 // TODO(falken): Get rid of the brute-force scan over possible |
| 44 // (font family / script) combinations - see http://crbug.com/308095. |
| 45 for (size_t i = 0; i < prefs::kWebKitScriptsForFontFamilyMapsLength; ++i) { |
| 46 const char* script = prefs::kWebKitScriptsForFontFamilyMaps[i]; |
| 47 base::string16 result = FetchAndCacheFont(script, map_name); |
| 48 if (!result.empty()) |
| 49 (*map)[script] = result; |
| 50 } |
| 51 } |
| 52 |
| 53 base::string16 FontFamilyCache::FetchFont(const char* script, |
| 54 const char* map_name) { |
| 55 std::string pref_name = base::StringPrintf("%s.%s", map_name, script); |
| 56 std::string font = prefs_->GetString(pref_name.c_str()); |
| 57 base::string16 font16 = base::UTF8ToUTF16(font); |
| 58 |
| 59 // Lazily constructs the map if it doesn't already exist. |
| 60 ScriptFontMap& map = font_family_map_[map_name]; |
| 61 map[script] = font16; |
| 62 |
| 63 // Register for profile preference changes. |
| 64 profile_pref_registrar_.Add( |
| 65 pref_name.c_str(), |
| 66 base::Bind(&FontFamilyCache::OnPrefsChanged, base::Unretained(this))); |
| 67 return font16; |
| 68 } |
| 69 |
| 70 base::string16 FontFamilyCache::FetchAndCacheFont(const char* script, |
| 71 const char* map_name) { |
| 72 FontFamilyMap::const_iterator it = font_family_map_.find(map_name); |
| 73 if (it != font_family_map_.end()) { |
| 74 ScriptFontMap::const_iterator it2 = it->second.find(script); |
| 75 if (it2 != it->second.end()) |
| 76 return it2->second; |
| 77 } |
| 78 |
| 79 return FetchFont(script, map_name); |
| 80 } |
| 81 |
| 82 // There are ~1000 entries in the cache. Avoid unnecessary object construction, |
| 83 // including std::string. |
| 84 void FontFamilyCache::OnPrefsChanged(const std::string& pref_name) { |
| 85 // The delimiter is '.' |
| 86 const size_t delimiter_length = 1; |
| 87 for (FontFamilyMap::iterator it = font_family_map_.begin(); |
| 88 it != font_family_map_.end(); |
| 89 ++it) { |
| 90 const char* map_name = it->first; |
| 91 size_t map_name_length = strlen(map_name); |
| 92 |
| 93 // If the map name doesn't match, move on. |
| 94 if (pref_name.compare(0, map_name_length, map_name) != 0) |
| 95 continue; |
| 96 |
| 97 ScriptFontMap& map = it->second; |
| 98 for (ScriptFontMap::iterator it2 = map.begin(); it2 != map.end(); ++it2) { |
| 99 const char* script = it2->first; |
| 100 size_t script_length = strlen(script); |
| 101 |
| 102 // If the length doesn't match, move on. |
| 103 if (pref_name.size() != |
| 104 map_name_length + script_length + delimiter_length) |
| 105 continue; |
| 106 |
| 107 // If the script doesn't match, move on. |
| 108 if (pref_name.compare( |
| 109 map_name_length + delimiter_length, script_length, script) != 0) |
| 110 continue; |
| 111 |
| 112 // Clear the cache and the observer. |
| 113 map.erase(it2); |
| 114 profile_pref_registrar_.Remove(pref_name.c_str()); |
| 115 break; |
| 116 } |
| 117 } |
| 118 } |
OLD | NEW |