Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(26)

Side by Side Diff: chrome/browser/font_family_cache.h

Issue 439913002: Add a cache for FillFontFamilyMap. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Comments from avi, round 2. Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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
17 FORWARD_DECLARE_TEST(FontFamilyCacheTest, Caching);
18
19 namespace chrome {
Lei Zhang 2014/08/05 19:26:49 We decided at one point to no longer have 'namespa
erikchen 2014/08/05 20:49:23 Ah. Good to know. Done.
20
21 // Identifies this PrefService cache.
22 extern const char* kFontFamilyCacheKey;
23
24 // Caches font family preferences associated with a PrefService. This class
25 // relies on the assumption that each concatenation of map_name + '.' + script
26 // is a unique string.
27 class FontFamilyCache : public base::SupportsUserData::Data {
28 public:
29 explicit FontFamilyCache(PrefService* prefs);
30 virtual ~FontFamilyCache();
31
32 // Fills |map| with font family preferences.
33 void FillFontFamilyMap(const char* map_name,
34 content::ScriptFontFamilyMap* map);
35
36 protected:
37 // Exposed and virtual for testing.
38 // Fetches the font without checking the cache.
39 virtual base::string16 FetchFont(const char* script, const char* map_name);
40
41 private:
42 FRIEND_TEST_ALL_PREFIXES(::FontFamilyCacheTest, Caching);
43
44 // Map from script to font.
45 typedef base::hash_map<const char*, base::string16> ScriptFontMap;
46 // Map from font family to ScriptFontMap.
47 typedef base::hash_map<const char*, ScriptFontMap> FontFamilyMap;
48
49 // Checks the cache for the font. If not present, fetches the font and stores
50 // the result in the cache.
51 // This method needs to be very fast, because it's called ~20,000 times on a
52 // fresh launch with an empty profile. It's important to avoid unnecessary
53 // object construction, hence the heavy use of const char* and the minimal use
54 // of std::string.
55 // Both |script| and |map_name| might be used as the key in a hash_map.
56 // |script| and |map_name| must outlive the cache. This is true in the
57 // current implementation since |script| and |map_name| are both defined at
58 // compile time.
59 base::string16 FetchAndCacheFont(const char* script, const char* map_name);
60
61 // Called when font family preferences changed.
62 // Invalidates the cached entry, and removes the relevant observer.
63 // Note: It is safe to remove the observer from the pref change callback.
64 void OnPrefsChanged(const std::string& pref_name);
65
66 // Cache of font family preferences.
67 FontFamilyMap font_family_map_;
68
69 // Weak reference.
70 // Note: The lifetime of this object is tied to the lifetime of the
71 // PrefService, so there is no worry about an invalid pointer.
72 const PrefService* prefs_;
73
74 // Reacts to profile changes.
75 PrefChangeRegistrar profile_pref_registrar_;
76
77 DISALLOW_COPY_AND_ASSIGN(FontFamilyCache);
78 };
79
80 } // namespace chrome
81
82 #endif // CHROME_BROWSER_FONT_FAMILY_CACHE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698