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

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 thestig. 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 class Profile;
17
18 FORWARD_DECLARE_TEST(FontFamilyCacheTest, Caching);
19
20 // Caches font family preferences associated with a PrefService. This class
21 // relies on the assumption that each concatenation of map_name + '.' + script
22 // is a unique string. It also relies on the assumption that the (const char*)
23 // keys used in both inner and outer hash_maps are compile time constants.
24 class FontFamilyCache : public base::SupportsUserData::Data {
25 public:
26 explicit FontFamilyCache(PrefService* prefs);
27 virtual ~FontFamilyCache();
28
29 // Gets or creates the relevant FontFamilyCache, and then fills |map|.
30 static void FillFontFamilyMap(Profile* profile,
31 const char* map_name,
32 content::ScriptFontFamilyMap* map);
33
34 // Fills |map| with font family preferences.
35 void FillFontFamilyMap(const char* map_name,
36 content::ScriptFontFamilyMap* map);
37
38 protected:
39 // Exposed and virtual for testing.
40 // Fetches the font without checking the cache.
41 virtual base::string16 FetchFont(const char* script, const char* map_name);
42
43 private:
44 FRIEND_TEST_ALL_PREFIXES(::FontFamilyCacheTest, Caching);
45
46 // Map from script to font.
47 // Key comparison uses pointer equality.
48 typedef base::hash_map<const char*, base::string16> ScriptFontMap;
Lei Zhang 2014/08/05 21:15:08 nit: add a new line after.
erikchen 2014/08/05 21:34:17 Done.
49 // Map from font family to ScriptFontMap.
50 // Key comparison uses pointer equality.
51 typedef base::hash_map<const char*, ScriptFontMap> FontFamilyMap;
52
53 // Checks the cache for the font. If not present, fetches the font and stores
54 // the result in the cache.
55 // This method needs to be very fast, because it's called ~20,000 times on a
56 // fresh launch with an empty profile. It's important to avoid unnecessary
57 // object construction, hence the heavy use of const char* and the minimal use
58 // of std::string.
59 // |script| and |map_name| must be compile time constants. Two behaviors rely
60 // on this: key comparison uses pointer equality, and keys must outlive the
61 // hash_maps.
62 base::string16 FetchAndCacheFont(const char* script, const char* map_name);
63
64 // Called when font family preferences changed.
65 // Invalidates the cached entry, and removes the relevant observer.
66 // Note: It is safe to remove the observer from the pref change callback.
67 void OnPrefsChanged(const std::string& pref_name);
68
69 // Cache of font family preferences.
70 FontFamilyMap font_family_map_;
71
72 // Weak reference.
73 // Note: The lifetime of this object is tied to the lifetime of the
74 // PrefService, so there is no worry about an invalid pointer.
75 const PrefService* prefs_;
76
77 // Reacts to profile changes.
78 PrefChangeRegistrar profile_pref_registrar_;
79
80 DISALLOW_COPY_AND_ASSIGN(FontFamilyCache);
81 };
82
83 #endif // CHROME_BROWSER_FONT_FAMILY_CACHE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698