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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/font_family_cache.h
diff --git a/chrome/browser/font_family_cache.h b/chrome/browser/font_family_cache.h
new file mode 100644
index 0000000000000000000000000000000000000000..4076072bfc09dcfeb25f43e8f6624ba568f3a54e
--- /dev/null
+++ b/chrome/browser/font_family_cache.h
@@ -0,0 +1,82 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_BROWSER_FONT_FAMILY_CACHE_H_
+#define CHROME_BROWSER_FONT_FAMILY_CACHE_H_
+
+#include "base/containers/hash_tables.h"
+#include "base/gtest_prod_util.h"
+#include "base/prefs/pref_change_registrar.h"
+#include "base/strings/string16.h"
+#include "base/supports_user_data.h"
+#include "content/public/common/web_preferences.h"
+
+class PrefService;
+
+FORWARD_DECLARE_TEST(FontFamilyCacheTest, Caching);
+
+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.
+
+// Identifies this PrefService cache.
+extern const char* kFontFamilyCacheKey;
+
+// Caches font family preferences associated with a PrefService. This class
+// relies on the assumption that each concatenation of map_name + '.' + script
+// is a unique string.
+class FontFamilyCache : public base::SupportsUserData::Data {
+ public:
+ explicit FontFamilyCache(PrefService* prefs);
+ virtual ~FontFamilyCache();
+
+ // Fills |map| with font family preferences.
+ void FillFontFamilyMap(const char* map_name,
+ content::ScriptFontFamilyMap* map);
+
+ protected:
+ // Exposed and virtual for testing.
+ // Fetches the font without checking the cache.
+ virtual base::string16 FetchFont(const char* script, const char* map_name);
+
+ private:
+ FRIEND_TEST_ALL_PREFIXES(::FontFamilyCacheTest, Caching);
+
+ // Map from script to font.
+ typedef base::hash_map<const char*, base::string16> ScriptFontMap;
+ // Map from font family to ScriptFontMap.
+ typedef base::hash_map<const char*, ScriptFontMap> FontFamilyMap;
+
+ // Checks the cache for the font. If not present, fetches the font and stores
+ // the result in the cache.
+ // This method needs to be very fast, because it's called ~20,000 times on a
+ // fresh launch with an empty profile. It's important to avoid unnecessary
+ // object construction, hence the heavy use of const char* and the minimal use
+ // of std::string.
+ // Both |script| and |map_name| might be used as the key in a hash_map.
+ // |script| and |map_name| must outlive the cache. This is true in the
+ // current implementation since |script| and |map_name| are both defined at
+ // compile time.
+ base::string16 FetchAndCacheFont(const char* script, const char* map_name);
+
+ // Called when font family preferences changed.
+ // Invalidates the cached entry, and removes the relevant observer.
+ // Note: It is safe to remove the observer from the pref change callback.
+ void OnPrefsChanged(const std::string& pref_name);
+
+ // Cache of font family preferences.
+ FontFamilyMap font_family_map_;
+
+ // Weak reference.
+ // Note: The lifetime of this object is tied to the lifetime of the
+ // PrefService, so there is no worry about an invalid pointer.
+ const PrefService* prefs_;
+
+ // Reacts to profile changes.
+ PrefChangeRegistrar profile_pref_registrar_;
+
+ DISALLOW_COPY_AND_ASSIGN(FontFamilyCache);
+};
+
+} // namespace chrome
+
+#endif // CHROME_BROWSER_FONT_FAMILY_CACHE_H_

Powered by Google App Engine
This is Rietveld 408576698