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