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

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: 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 // Caches font family preferences associated with a PrefService. This class
6 // relies on the assumption that each concatenation of map_name + '.' + script
7 // is a unique string.
8
9 #ifndef CHROME_BROWSER_FONT_FAMILY_CACHE_H_
10 #define CHROME_BROWSER_FONT_FAMILY_CACHE_H_
11
12 #include "base/containers/hash_tables.h"
13 #include "base/gtest_prod_util.h"
14 #include "base/prefs/pref_change_registrar.h"
15 #include "base/prefs/pref_service.h"
16 #include "base/strings/string16.h"
17 #include "content/public/common/web_preferences.h"
18
19 class PrefService;
20
21 FORWARD_DECLARE_TEST(FontFamilyCacheTest, Caching);
22
23 namespace chrome {
24
25 // Identifies this PrefService cache.
26 extern const char* kFontFamilyCacheKey;
27
28 class FontFamilyCache : public PrefServiceCache {
29 public:
30 explicit FontFamilyCache(PrefService* prefs);
31 ~FontFamilyCache();
Avi (use Gerrit) 2014/08/05 01:50:26 Since FontFamilyCache is going to be deleted throu
erikchen 2014/08/05 17:46:17 ack, thanks. Too much ObjC.
32
33 // Fills |map| with font family preferences.
34 void FillFontFamilyMap(const char* map_name,
35 content::ScriptFontFamilyMap* map);
36
37 protected:
38 // Exposed and virtual for testing.
39 // Fetches the font without checking the cache.
40 virtual base::string16 FetchFont(const char* script, const char* map_name);
41
42 private:
43 FRIEND_TEST_ALL_PREFIXES(::FontFamilyCacheTest, Caching);
44
45 // Map from script to font.
46 typedef base::hash_map<const char*, base::string16> ScriptFontMap;
47 // Map from font family to ScriptFontMap.
48 typedef base::hash_map<const char*, ScriptFontMap> FontFamilyMap;
49
50 // Checks the cache for the font. If not present, fetches the font and stores
51 // the result in the cache.
52 // This method needs to be very fast, because its called ~20,000 times on a
Avi (use Gerrit) 2014/08/05 01:50:26 s/its/it's/
erikchen 2014/08/05 17:46:17 Done.
53 // fresh launch with an empty profile. It's important to avoid unnecessary
54 // object construction, hence the heavy use of const char* and the minimal use
55 // of std::string.
56 base::string16 FetchAndCacheFont(const char* script, const char* map_name);
57
58 // Called when font family preferences changed.
59 // Invalidates the cached entry, and removes the relevant observer.
60 // Note: It is safe to remove the observer from the pref change callback.
61 void OnPrefsChanged(const std::string& pref_name);
62
63 // Cache of font family preferences.
64 FontFamilyMap font_family_map_;
65
66 // Weak reference.
67 // Note: The lifetime of this object is tied to the lifetime of the
68 // PrefService, so there is no worry about an invalid pointer.
69 const PrefService* prefs_;
70
71 // Reacts to profile changes.
72 PrefChangeRegistrar profile_pref_registrar_;
73
74 DISALLOW_COPY_AND_ASSIGN(FontFamilyCache);
75 };
76
77 } // namespace chrome
78
79 #endif // CHROME_BROWSER_FONT_FAMILY_CACHE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698