blob: a99372e6744c11ce78b7e23129d69a0dd8c75f0b [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
brettw84cff3f2017-06-29 18:26:508#include <unordered_map>
9
[email protected]354de9e2014-08-07 03:27:1910#include "base/gtest_prod_util.h"
avi6846aef2015-12-26 01:09:3811#include "base/macros.h"
[email protected]354de9e2014-08-07 03:27:1912#include "base/strings/string16.h"
13#include "base/supports_user_data.h"
Brett Wilson21cf626a2017-09-07 00:30:2014#include "chrome/browser/font_pref_change_notifier.h"
[email protected]354de9e2014-08-07 03:27:1915#include "content/public/common/web_preferences.h"
16
17class PrefService;
18class Profile;
19
20FORWARD_DECLARE_TEST(FontFamilyCacheTest, Caching);
21
22// Caches font family preferences associated with a PrefService. This class
23// relies on the assumption that each concatenation of map_name + '.' + script
24// is a unique string. It also relies on the assumption that the (const char*)
brettw84cff3f2017-06-29 18:26:5025// keys used in both inner and outer maps are compile time constants.
Erik Chen8be820612017-08-29 23:38:1826// This class caches the strings necessary to update
27// "content::ScriptFontFamilyMap". This is necessary since Chrome attempts to
28// update content::ScriptFontFamilyMap 20000 times at startup. See
29// https://crbug.com/308095.
Evan Stadecf0da232019-10-03 15:25:2130class FontFamilyCache : public base::SupportsUserData::Data {
[email protected]354de9e2014-08-07 03:27:1931 public:
32 explicit FontFamilyCache(Profile* profile);
Daniel Chenga542fca2014-10-21 09:51:2933 ~FontFamilyCache() override;
[email protected]354de9e2014-08-07 03:27:1934
35 // Gets or creates the relevant FontFamilyCache, and then fills |map|.
36 static void FillFontFamilyMap(Profile* profile,
37 const char* map_name,
38 content::ScriptFontFamilyMap* map);
39
40 // Fills |map| with font family preferences.
41 void FillFontFamilyMap(const char* map_name,
42 content::ScriptFontFamilyMap* map);
43
44 protected:
45 // Exposed and virtual for testing.
46 // Fetches the font without checking the cache.
47 virtual base::string16 FetchFont(const char* script, const char* map_name);
48
49 private:
50 FRIEND_TEST_ALL_PREFIXES(::FontFamilyCacheTest, Caching);
51
52 // Map from script to font.
53 // Key comparison uses pointer equality.
brettw84cff3f2017-06-29 18:26:5054 using ScriptFontMap = std::unordered_map<const char*, base::string16>;
[email protected]354de9e2014-08-07 03:27:1955
56 // Map from font family to ScriptFontMap.
57 // Key comparison uses pointer equality.
brettw84cff3f2017-06-29 18:26:5058 using FontFamilyMap = std::unordered_map<const char*, ScriptFontMap>;
[email protected]354de9e2014-08-07 03:27:1959
60 // Checks the cache for the font. If not present, fetches the font and stores
61 // the result in the cache.
62 // This method needs to be very fast, because it's called ~20,000 times on a
63 // fresh launch with an empty profile. It's important to avoid unnecessary
64 // object construction, hence the heavy use of const char* and the minimal use
65 // of std::string.
66 // |script| and |map_name| must be compile time constants. Two behaviors rely
67 // on this: key comparison uses pointer equality, and keys must outlive the
brettw84cff3f2017-06-29 18:26:5068 // maps.
[email protected]354de9e2014-08-07 03:27:1969 base::string16 FetchAndCacheFont(const char* script, const char* map_name);
70
71 // Called when font family preferences changed.
72 // Invalidates the cached entry, and removes the relevant observer.
73 // Note: It is safe to remove the observer from the pref change callback.
74 void OnPrefsChanged(const std::string& pref_name);
75
[email protected]354de9e2014-08-07 03:27:1976 // Cache of font family preferences.
77 FontFamilyMap font_family_map_;
78
79 // Weak reference.
80 // Note: The lifetime of this object is tied to the lifetime of the
81 // PrefService, so there is no worry about an invalid pointer.
82 const PrefService* prefs_;
83
Evan Stadecf0da232019-10-03 15:25:2184 // Reacts to profile font changes. |font_change_registrar_| will be
85 // automatically unregistered when the FontPrefChangeNotifier is destroyed as
86 // part of Profile destruction, thus ensuring safe unregistration even though
87 // |this| is destroyed after the Profile destructor completes as part of
88 // Profile's super class destructor ~base::SupportsUserData.
Brett Wilson21cf626a2017-09-07 00:30:2089 FontPrefChangeNotifier::Registrar font_change_registrar_;
[email protected]354de9e2014-08-07 03:27:1990
[email protected]354de9e2014-08-07 03:27:1991 DISALLOW_COPY_AND_ASSIGN(FontFamilyCache);
92};
93
94#endif // CHROME_BROWSER_FONT_FAMILY_CACHE_H_