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 "base/containers/hash_tables.h" |
9 #include "base/gtest_prod_util.h" | 9 #include "base/gtest_prod_util.h" |
10 #include "base/prefs/pref_change_registrar.h" | 10 #include "base/prefs/pref_change_registrar.h" |
11 #include "base/strings/string16.h" | 11 #include "base/strings/string16.h" |
12 #include "base/supports_user_data.h" | 12 #include "base/supports_user_data.h" |
13 #include "content/public/browser/notification_observer.h" | 13 #include "content/public/browser/notification_observer.h" |
14 #include "content/public/browser/notification_registrar.h" | 14 #include "content/public/browser/notification_registrar.h" |
15 #include "content/public/common/web_preferences.h" | 15 #include "content/public/common/web_preferences.h" |
16 | 16 |
17 class PrefService; | 17 class PrefService; |
18 class Profile; | 18 class Profile; |
19 | 19 |
20 FORWARD_DECLARE_TEST(FontFamilyCacheTest, Caching); | 20 FORWARD_DECLARE_TEST(FontFamilyCacheTest, Caching); |
21 | 21 |
22 // Caches font family preferences associated with a PrefService. This class | 22 // Caches font family preferences associated with a PrefService. This class |
23 // relies on the assumption that each concatenation of map_name + '.' + script | 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*) | 24 // is a unique string. It also relies on the assumption that the (const char*) |
25 // keys used in both inner and outer hash_maps are compile time constants. | 25 // keys used in both inner and outer hash_maps are compile time constants. |
26 class FontFamilyCache : public base::SupportsUserData::Data, | 26 class FontFamilyCache : public base::SupportsUserData::Data, |
27 public content::NotificationObserver { | 27 public content::NotificationObserver { |
28 public: | 28 public: |
29 explicit FontFamilyCache(Profile* profile); | 29 explicit FontFamilyCache(Profile* profile); |
30 virtual ~FontFamilyCache(); | 30 ~FontFamilyCache() override; |
31 | 31 |
32 // Gets or creates the relevant FontFamilyCache, and then fills |map|. | 32 // Gets or creates the relevant FontFamilyCache, and then fills |map|. |
33 static void FillFontFamilyMap(Profile* profile, | 33 static void FillFontFamilyMap(Profile* profile, |
34 const char* map_name, | 34 const char* map_name, |
35 content::ScriptFontFamilyMap* map); | 35 content::ScriptFontFamilyMap* map); |
36 | 36 |
37 // Fills |map| with font family preferences. | 37 // Fills |map| with font family preferences. |
38 void FillFontFamilyMap(const char* map_name, | 38 void FillFontFamilyMap(const char* map_name, |
39 content::ScriptFontFamilyMap* map); | 39 content::ScriptFontFamilyMap* map); |
40 | 40 |
(...skipping 24 matching lines...) Expand all Loading... |
65 // hash_maps. | 65 // hash_maps. |
66 base::string16 FetchAndCacheFont(const char* script, const char* map_name); | 66 base::string16 FetchAndCacheFont(const char* script, const char* map_name); |
67 | 67 |
68 // Called when font family preferences changed. | 68 // Called when font family preferences changed. |
69 // Invalidates the cached entry, and removes the relevant observer. | 69 // Invalidates the cached entry, and removes the relevant observer. |
70 // Note: It is safe to remove the observer from the pref change callback. | 70 // Note: It is safe to remove the observer from the pref change callback. |
71 void OnPrefsChanged(const std::string& pref_name); | 71 void OnPrefsChanged(const std::string& pref_name); |
72 | 72 |
73 // content::NotificationObserver override. | 73 // content::NotificationObserver override. |
74 // Called when the profile is being destructed. | 74 // Called when the profile is being destructed. |
75 virtual void Observe(int type, | 75 void Observe(int type, |
76 const content::NotificationSource& source, | 76 const content::NotificationSource& source, |
77 const content::NotificationDetails& details) override; | 77 const content::NotificationDetails& details) override; |
78 | 78 |
79 // Cache of font family preferences. | 79 // Cache of font family preferences. |
80 FontFamilyMap font_family_map_; | 80 FontFamilyMap font_family_map_; |
81 | 81 |
82 // Weak reference. | 82 // Weak reference. |
83 // Note: The lifetime of this object is tied to the lifetime of the | 83 // Note: The lifetime of this object is tied to the lifetime of the |
84 // PrefService, so there is no worry about an invalid pointer. | 84 // PrefService, so there is no worry about an invalid pointer. |
85 const PrefService* prefs_; | 85 const PrefService* prefs_; |
86 | 86 |
87 // Reacts to profile changes. | 87 // Reacts to profile changes. |
88 PrefChangeRegistrar profile_pref_registrar_; | 88 PrefChangeRegistrar profile_pref_registrar_; |
89 | 89 |
90 // Listens for profile destruction. | 90 // Listens for profile destruction. |
91 content::NotificationRegistrar notification_registrar_; | 91 content::NotificationRegistrar notification_registrar_; |
92 | 92 |
93 DISALLOW_COPY_AND_ASSIGN(FontFamilyCache); | 93 DISALLOW_COPY_AND_ASSIGN(FontFamilyCache); |
94 }; | 94 }; |
95 | 95 |
96 #endif // CHROME_BROWSER_FONT_FAMILY_CACHE_H_ | 96 #endif // CHROME_BROWSER_FONT_FAMILY_CACHE_H_ |
OLD | NEW |