[email protected] | 354de9e | 2014-08-07 03:27:19 | [diff] [blame] | 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 | |
brettw | 84cff3f | 2017-06-29 18:26:50 | [diff] [blame] | 8 | #include <unordered_map> |
| 9 | |
[email protected] | 354de9e | 2014-08-07 03:27:19 | [diff] [blame] | 10 | #include "base/gtest_prod_util.h" |
avi | 6846aef | 2015-12-26 01:09:38 | [diff] [blame] | 11 | #include "base/macros.h" |
[email protected] | 354de9e | 2014-08-07 03:27:19 | [diff] [blame] | 12 | #include "base/strings/string16.h" |
| 13 | #include "base/supports_user_data.h" |
Brett Wilson | 21cf626a | 2017-09-07 00:30:20 | [diff] [blame] | 14 | #include "chrome/browser/font_pref_change_notifier.h" |
[email protected] | 354de9e | 2014-08-07 03:27:19 | [diff] [blame] | 15 | #include "content/public/common/web_preferences.h" |
| 16 | |
| 17 | class PrefService; |
| 18 | class Profile; |
| 19 | |
| 20 | FORWARD_DECLARE_TEST(FontFamilyCacheTest, Caching); |
| 21 | |
| 22 | // Caches font family preferences associated with a PrefService. This class |
| 23 | // relies on the assumption that each concatenation of map_name + '.' + script |
| 24 | // is a unique string. It also relies on the assumption that the (const char*) |
brettw | 84cff3f | 2017-06-29 18:26:50 | [diff] [blame] | 25 | // keys used in both inner and outer maps are compile time constants. |
Erik Chen | 8be82061 | 2017-08-29 23:38:18 | [diff] [blame] | 26 | // This class caches the strings necessary to update |
| 27 | // "content::ScriptFontFamilyMap". This is necessary since Chrome attempts to |
| 28 | // update content::ScriptFontFamilyMap 20000 times at startup. See |
| 29 | // https://crbug.com/308095. |
Evan Stade | cf0da23 | 2019-10-03 15:25:21 | [diff] [blame^] | 30 | class FontFamilyCache : public base::SupportsUserData::Data { |
[email protected] | 354de9e | 2014-08-07 03:27:19 | [diff] [blame] | 31 | public: |
| 32 | explicit FontFamilyCache(Profile* profile); |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 33 | ~FontFamilyCache() override; |
[email protected] | 354de9e | 2014-08-07 03:27:19 | [diff] [blame] | 34 | |
| 35 | // Gets or creates the relevant FontFamilyCache, and then fills |map|. |
| 36 | static void FillFontFamilyMap(Profile* profile, |
| 37 | const char* map_name, |
| 38 | content::ScriptFontFamilyMap* map); |
| 39 | |
| 40 | // Fills |map| with font family preferences. |
| 41 | void FillFontFamilyMap(const char* map_name, |
| 42 | content::ScriptFontFamilyMap* map); |
| 43 | |
| 44 | protected: |
| 45 | // Exposed and virtual for testing. |
| 46 | // Fetches the font without checking the cache. |
| 47 | virtual base::string16 FetchFont(const char* script, const char* map_name); |
| 48 | |
| 49 | private: |
| 50 | FRIEND_TEST_ALL_PREFIXES(::FontFamilyCacheTest, Caching); |
| 51 | |
| 52 | // Map from script to font. |
| 53 | // Key comparison uses pointer equality. |
brettw | 84cff3f | 2017-06-29 18:26:50 | [diff] [blame] | 54 | using ScriptFontMap = std::unordered_map<const char*, base::string16>; |
[email protected] | 354de9e | 2014-08-07 03:27:19 | [diff] [blame] | 55 | |
| 56 | // Map from font family to ScriptFontMap. |
| 57 | // Key comparison uses pointer equality. |
brettw | 84cff3f | 2017-06-29 18:26:50 | [diff] [blame] | 58 | using FontFamilyMap = std::unordered_map<const char*, ScriptFontMap>; |
[email protected] | 354de9e | 2014-08-07 03:27:19 | [diff] [blame] | 59 | |
| 60 | // Checks the cache for the font. If not present, fetches the font and stores |
| 61 | // the result in the cache. |
| 62 | // This method needs to be very fast, because it's called ~20,000 times on a |
| 63 | // fresh launch with an empty profile. It's important to avoid unnecessary |
| 64 | // object construction, hence the heavy use of const char* and the minimal use |
| 65 | // of std::string. |
| 66 | // |script| and |map_name| must be compile time constants. Two behaviors rely |
| 67 | // on this: key comparison uses pointer equality, and keys must outlive the |
brettw | 84cff3f | 2017-06-29 18:26:50 | [diff] [blame] | 68 | // maps. |
[email protected] | 354de9e | 2014-08-07 03:27:19 | [diff] [blame] | 69 | base::string16 FetchAndCacheFont(const char* script, const char* map_name); |
| 70 | |
| 71 | // Called when font family preferences changed. |
| 72 | // Invalidates the cached entry, and removes the relevant observer. |
| 73 | // Note: It is safe to remove the observer from the pref change callback. |
| 74 | void OnPrefsChanged(const std::string& pref_name); |
| 75 | |
[email protected] | 354de9e | 2014-08-07 03:27:19 | [diff] [blame] | 76 | // Cache of font family preferences. |
| 77 | FontFamilyMap font_family_map_; |
| 78 | |
| 79 | // Weak reference. |
| 80 | // Note: The lifetime of this object is tied to the lifetime of the |
| 81 | // PrefService, so there is no worry about an invalid pointer. |
| 82 | const PrefService* prefs_; |
| 83 | |
Evan Stade | cf0da23 | 2019-10-03 15:25:21 | [diff] [blame^] | 84 | // Reacts to profile font changes. |font_change_registrar_| will be |
| 85 | // automatically unregistered when the FontPrefChangeNotifier is destroyed as |
| 86 | // part of Profile destruction, thus ensuring safe unregistration even though |
| 87 | // |this| is destroyed after the Profile destructor completes as part of |
| 88 | // Profile's super class destructor ~base::SupportsUserData. |
Brett Wilson | 21cf626a | 2017-09-07 00:30:20 | [diff] [blame] | 89 | FontPrefChangeNotifier::Registrar font_change_registrar_; |
[email protected] | 354de9e | 2014-08-07 03:27:19 | [diff] [blame] | 90 | |
[email protected] | 354de9e | 2014-08-07 03:27:19 | [diff] [blame] | 91 | DISALLOW_COPY_AND_ASSIGN(FontFamilyCache); |
| 92 | }; |
| 93 | |
| 94 | #endif // CHROME_BROWSER_FONT_FAMILY_CACHE_H_ |