OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_FONT_FAMILY_CACHE_H_ | 5 #ifndef CHROME_BROWSER_FONT_FAMILY_CACHE_H_ |
6 #define CHROME_BROWSER_FONT_FAMILY_CACHE_H_ | 6 #define CHROME_BROWSER_FONT_FAMILY_CACHE_H_ |
7 | 7 |
8 #include "base/containers/hash_tables.h" | 8 #include <unordered_map> |
9 | |
9 #include "base/gtest_prod_util.h" | 10 #include "base/gtest_prod_util.h" |
10 #include "base/macros.h" | 11 #include "base/macros.h" |
11 #include "base/strings/string16.h" | 12 #include "base/strings/string16.h" |
12 #include "base/supports_user_data.h" | 13 #include "base/supports_user_data.h" |
13 #include "components/prefs/pref_change_registrar.h" | 14 #include "components/prefs/pref_change_registrar.h" |
14 #include "content/public/browser/notification_observer.h" | 15 #include "content/public/browser/notification_observer.h" |
15 #include "content/public/browser/notification_registrar.h" | 16 #include "content/public/browser/notification_registrar.h" |
16 #include "content/public/common/web_preferences.h" | 17 #include "content/public/common/web_preferences.h" |
17 | 18 |
18 class PrefService; | 19 class PrefService; |
19 class Profile; | 20 class Profile; |
20 | 21 |
21 FORWARD_DECLARE_TEST(FontFamilyCacheTest, Caching); | 22 FORWARD_DECLARE_TEST(FontFamilyCacheTest, Caching); |
22 | 23 |
23 // Caches font family preferences associated with a PrefService. This class | 24 // Caches font family preferences associated with a PrefService. This class |
24 // relies on the assumption that each concatenation of map_name + '.' + script | 25 // 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*) | 26 // is a unique string. It also relies on the assumption that the (const char*) |
26 // keys used in both inner and outer hash_maps are compile time constants. | 27 // keys used in both inner and outer aps are compile time constants. |
chrisha
2017/05/23 20:07:59
maps*
| |
27 class FontFamilyCache : public base::SupportsUserData::Data, | 28 class FontFamilyCache : public base::SupportsUserData::Data, |
28 public content::NotificationObserver { | 29 public content::NotificationObserver { |
29 public: | 30 public: |
30 explicit FontFamilyCache(Profile* profile); | 31 explicit FontFamilyCache(Profile* profile); |
31 ~FontFamilyCache() override; | 32 ~FontFamilyCache() override; |
32 | 33 |
33 // Gets or creates the relevant FontFamilyCache, and then fills |map|. | 34 // Gets or creates the relevant FontFamilyCache, and then fills |map|. |
34 static void FillFontFamilyMap(Profile* profile, | 35 static void FillFontFamilyMap(Profile* profile, |
35 const char* map_name, | 36 const char* map_name, |
36 content::ScriptFontFamilyMap* map); | 37 content::ScriptFontFamilyMap* map); |
37 | 38 |
38 // Fills |map| with font family preferences. | 39 // Fills |map| with font family preferences. |
39 void FillFontFamilyMap(const char* map_name, | 40 void FillFontFamilyMap(const char* map_name, |
40 content::ScriptFontFamilyMap* map); | 41 content::ScriptFontFamilyMap* map); |
41 | 42 |
42 protected: | 43 protected: |
43 // Exposed and virtual for testing. | 44 // Exposed and virtual for testing. |
44 // Fetches the font without checking the cache. | 45 // Fetches the font without checking the cache. |
45 virtual base::string16 FetchFont(const char* script, const char* map_name); | 46 virtual base::string16 FetchFont(const char* script, const char* map_name); |
46 | 47 |
47 private: | 48 private: |
48 FRIEND_TEST_ALL_PREFIXES(::FontFamilyCacheTest, Caching); | 49 FRIEND_TEST_ALL_PREFIXES(::FontFamilyCacheTest, Caching); |
49 | 50 |
50 // Map from script to font. | 51 // Map from script to font. |
51 // Key comparison uses pointer equality. | 52 // Key comparison uses pointer equality. |
52 typedef base::hash_map<const char*, base::string16> ScriptFontMap; | 53 using ScriptFontMap = std::unordered_map<const char*, base::string16>; |
53 | 54 |
54 // Map from font family to ScriptFontMap. | 55 // Map from font family to ScriptFontMap. |
55 // Key comparison uses pointer equality. | 56 // Key comparison uses pointer equality. |
56 typedef base::hash_map<const char*, ScriptFontMap> FontFamilyMap; | 57 using FontFamilyMap = std::unordered_map<const char*, ScriptFontMap>; |
57 | 58 |
58 // Checks the cache for the font. If not present, fetches the font and stores | 59 // Checks the cache for the font. If not present, fetches the font and stores |
59 // the result in the cache. | 60 // the result in the cache. |
60 // This method needs to be very fast, because it's called ~20,000 times on a | 61 // This method needs to be very fast, because it's called ~20,000 times on a |
61 // fresh launch with an empty profile. It's important to avoid unnecessary | 62 // fresh launch with an empty profile. It's important to avoid unnecessary |
62 // object construction, hence the heavy use of const char* and the minimal use | 63 // object construction, hence the heavy use of const char* and the minimal use |
63 // of std::string. | 64 // of std::string. |
64 // |script| and |map_name| must be compile time constants. Two behaviors rely | 65 // |script| and |map_name| must be compile time constants. Two behaviors rely |
65 // on this: key comparison uses pointer equality, and keys must outlive the | 66 // on this: key comparison uses pointer equality, and keys must outlive the |
66 // hash_maps. | 67 // maps. |
67 base::string16 FetchAndCacheFont(const char* script, const char* map_name); | 68 base::string16 FetchAndCacheFont(const char* script, const char* map_name); |
68 | 69 |
69 // Called when font family preferences changed. | 70 // Called when font family preferences changed. |
70 // Invalidates the cached entry, and removes the relevant observer. | 71 // Invalidates the cached entry, and removes the relevant observer. |
71 // Note: It is safe to remove the observer from the pref change callback. | 72 // Note: It is safe to remove the observer from the pref change callback. |
72 void OnPrefsChanged(const std::string& pref_name); | 73 void OnPrefsChanged(const std::string& pref_name); |
73 | 74 |
74 // content::NotificationObserver override. | 75 // content::NotificationObserver override. |
75 // Called when the profile is being destructed. | 76 // Called when the profile is being destructed. |
76 void Observe(int type, | 77 void Observe(int type, |
(...skipping 11 matching lines...) Expand all Loading... | |
88 // Reacts to profile changes. | 89 // Reacts to profile changes. |
89 PrefChangeRegistrar profile_pref_registrar_; | 90 PrefChangeRegistrar profile_pref_registrar_; |
90 | 91 |
91 // Listens for profile destruction. | 92 // Listens for profile destruction. |
92 content::NotificationRegistrar notification_registrar_; | 93 content::NotificationRegistrar notification_registrar_; |
93 | 94 |
94 DISALLOW_COPY_AND_ASSIGN(FontFamilyCache); | 95 DISALLOW_COPY_AND_ASSIGN(FontFamilyCache); |
95 }; | 96 }; |
96 | 97 |
97 #endif // CHROME_BROWSER_FONT_FAMILY_CACHE_H_ | 98 #endif // CHROME_BROWSER_FONT_FAMILY_CACHE_H_ |
OLD | NEW |