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 #ifndef CHROME_BROWSER_FONT_FAMILY_CACHE_H_ |
| 6 #define CHROME_BROWSER_FONT_FAMILY_CACHE_H_ |
| 7 |
| 8 #include "base/containers/hash_tables.h" |
| 9 #include "base/gtest_prod_util.h" |
| 10 #include "base/prefs/pref_change_registrar.h" |
| 11 #include "base/strings/string16.h" |
| 12 #include "base/supports_user_data.h" |
| 13 #include "content/public/common/web_preferences.h" |
| 14 |
| 15 class PrefService; |
| 16 class Profile; |
| 17 |
| 18 FORWARD_DECLARE_TEST(FontFamilyCacheTest, Caching); |
| 19 |
| 20 // Caches font family preferences associated with a PrefService. This class |
| 21 // relies on the assumption that each concatenation of map_name + '.' + script |
| 22 // is a unique string. It also relies on the assumption that the (const char*) |
| 23 // keys used in both inner and outer hash_maps are compile time constants. |
| 24 class FontFamilyCache : public base::SupportsUserData::Data { |
| 25 public: |
| 26 explicit FontFamilyCache(PrefService* prefs); |
| 27 virtual ~FontFamilyCache(); |
| 28 |
| 29 // Gets or creates the relevant FontFamilyCache, and then fills |map|. |
| 30 static void FillFontFamilyMap(Profile* profile, |
| 31 const char* map_name, |
| 32 content::ScriptFontFamilyMap* map); |
| 33 |
| 34 // Fills |map| with font family preferences. |
| 35 void FillFontFamilyMap(const char* map_name, |
| 36 content::ScriptFontFamilyMap* map); |
| 37 |
| 38 protected: |
| 39 // Exposed and virtual for testing. |
| 40 // Fetches the font without checking the cache. |
| 41 virtual base::string16 FetchFont(const char* script, const char* map_name); |
| 42 |
| 43 private: |
| 44 FRIEND_TEST_ALL_PREFIXES(::FontFamilyCacheTest, Caching); |
| 45 |
| 46 // Map from script to font. |
| 47 // Key comparison uses pointer equality. |
| 48 typedef base::hash_map<const char*, base::string16> ScriptFontMap; |
| 49 |
| 50 // Map from font family to ScriptFontMap. |
| 51 // Key comparison uses pointer equality. |
| 52 typedef base::hash_map<const char*, ScriptFontMap> FontFamilyMap; |
| 53 |
| 54 // Checks the cache for the font. If not present, fetches the font and stores |
| 55 // the result in the cache. |
| 56 // This method needs to be very fast, because it's called ~20,000 times on a |
| 57 // fresh launch with an empty profile. It's important to avoid unnecessary |
| 58 // object construction, hence the heavy use of const char* and the minimal use |
| 59 // of std::string. |
| 60 // |script| and |map_name| must be compile time constants. Two behaviors rely |
| 61 // on this: key comparison uses pointer equality, and keys must outlive the |
| 62 // hash_maps. |
| 63 base::string16 FetchAndCacheFont(const char* script, const char* map_name); |
| 64 |
| 65 // Called when font family preferences changed. |
| 66 // Invalidates the cached entry, and removes the relevant observer. |
| 67 // Note: It is safe to remove the observer from the pref change callback. |
| 68 void OnPrefsChanged(const std::string& pref_name); |
| 69 |
| 70 // Cache of font family preferences. |
| 71 FontFamilyMap font_family_map_; |
| 72 |
| 73 // Weak reference. |
| 74 // Note: The lifetime of this object is tied to the lifetime of the |
| 75 // PrefService, so there is no worry about an invalid pointer. |
| 76 const PrefService* prefs_; |
| 77 |
| 78 // Reacts to profile changes. |
| 79 PrefChangeRegistrar profile_pref_registrar_; |
| 80 |
| 81 DISALLOW_COPY_AND_ASSIGN(FontFamilyCache); |
| 82 }; |
| 83 |
| 84 #endif // CHROME_BROWSER_FONT_FAMILY_CACHE_H_ |
OLD | NEW |