blob: 743448d8744aa7d1119728e201fdf33b6ffc327c [file] [log] [blame]
[email protected]354de9e2014-08-07 03:27:191// 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"
avi6846aef2015-12-26 01:09:3810#include "base/macros.h"
[email protected]354de9e2014-08-07 03:27:1911#include "base/strings/string16.h"
12#include "base/supports_user_data.h"
brettwb1fc1b82016-02-02 00:19:0813#include "components/prefs/pref_change_registrar.h"
[email protected]354de9e2014-08-07 03:27:1914#include "content/public/browser/notification_observer.h"
15#include "content/public/browser/notification_registrar.h"
16#include "content/public/common/web_preferences.h"
17
18class PrefService;
19class Profile;
20
21FORWARD_DECLARE_TEST(FontFamilyCacheTest, Caching);
22
23// Caches font family preferences associated with a PrefService. This class
24// relies on the assumption that each concatenation of map_name + '.' + script
25// is a unique string. It also relies on the assumption that the (const char*)
26// keys used in both inner and outer hash_maps are compile time constants.
27class FontFamilyCache : public base::SupportsUserData::Data,
28 public content::NotificationObserver {
29 public:
30 explicit FontFamilyCache(Profile* profile);
Daniel Chenga542fca2014-10-21 09:51:2931 ~FontFamilyCache() override;
[email protected]354de9e2014-08-07 03:27:1932
33 // Gets or creates the relevant FontFamilyCache, and then fills |map|.
34 static void FillFontFamilyMap(Profile* profile,
35 const char* map_name,
36 content::ScriptFontFamilyMap* map);
37
38 // Fills |map| with font family preferences.
39 void FillFontFamilyMap(const char* map_name,
40 content::ScriptFontFamilyMap* map);
41
42 protected:
43 // Exposed and virtual for testing.
44 // Fetches the font without checking the cache.
45 virtual base::string16 FetchFont(const char* script, const char* map_name);
46
47 private:
48 FRIEND_TEST_ALL_PREFIXES(::FontFamilyCacheTest, Caching);
49
50 // Map from script to font.
51 // Key comparison uses pointer equality.
52 typedef base::hash_map<const char*, base::string16> ScriptFontMap;
53
54 // Map from font family to ScriptFontMap.
55 // Key comparison uses pointer equality.
56 typedef base::hash_map<const char*, ScriptFontMap> FontFamilyMap;
57
58 // Checks the cache for the font. If not present, fetches the font and stores
59 // the result in the cache.
60 // This method needs to be very fast, because it's called ~20,000 times on a
61 // fresh launch with an empty profile. It's important to avoid unnecessary
62 // object construction, hence the heavy use of const char* and the minimal use
63 // of std::string.
64 // |script| and |map_name| must be compile time constants. Two behaviors rely
65 // on this: key comparison uses pointer equality, and keys must outlive the
66 // hash_maps.
67 base::string16 FetchAndCacheFont(const char* script, const char* map_name);
68
69 // Called when font family preferences changed.
70 // Invalidates the cached entry, and removes the relevant observer.
71 // Note: It is safe to remove the observer from the pref change callback.
72 void OnPrefsChanged(const std::string& pref_name);
73
74 // content::NotificationObserver override.
75 // Called when the profile is being destructed.
Daniel Chenga542fca2014-10-21 09:51:2976 void Observe(int type,
77 const content::NotificationSource& source,
78 const content::NotificationDetails& details) override;
[email protected]354de9e2014-08-07 03:27:1979
80 // Cache of font family preferences.
81 FontFamilyMap font_family_map_;
82
83 // Weak reference.
84 // Note: The lifetime of this object is tied to the lifetime of the
85 // PrefService, so there is no worry about an invalid pointer.
86 const PrefService* prefs_;
87
88 // Reacts to profile changes.
89 PrefChangeRegistrar profile_pref_registrar_;
90
91 // Listens for profile destruction.
92 content::NotificationRegistrar notification_registrar_;
93
94 DISALLOW_COPY_AND_ASSIGN(FontFamilyCache);
95};
96
97#endif // CHROME_BROWSER_FONT_FAMILY_CACHE_H_