File: font_family_cache.h

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (96 lines) | stat: -rw-r--r-- 4,032 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright 2014 The Chromium Authors
// 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 <string>
#include <unordered_map>

#include "base/gtest_prod_util.h"
#include "base/memory/raw_ptr.h"
#include "base/supports_user_data.h"
#include "chrome/browser/font_pref_change_notifier.h"
#include "third_party/blink/public/common/web_preferences/web_preferences.h"

class PrefService;
class Profile;

FORWARD_DECLARE_TEST(FontFamilyCacheTest, Caching);

// 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. It also relies on the assumption that the (const char*)
// keys used in both inner and outer maps are compile time constants.
// This class caches the strings necessary to update
// "blink::web_pref::ScriptFontFamilyMap". This is necessary since Chrome
// attempts to update blink::web_pref::ScriptFontFamilyMap 20000 times at
// startup. See https://crbug.com/308095.
class FontFamilyCache : public base::SupportsUserData::Data {
 public:
  explicit FontFamilyCache(Profile* profile);

  FontFamilyCache(const FontFamilyCache&) = delete;
  FontFamilyCache& operator=(const FontFamilyCache&) = delete;

  ~FontFamilyCache() override;

  // Gets or creates the relevant FontFamilyCache, and then fills |map|.
  static void FillFontFamilyMap(Profile* profile,
                                const char* map_name,
                                blink::web_pref::ScriptFontFamilyMap* map);

  // Fills |map| with font family preferences.
  void FillFontFamilyMap(const char* map_name,
                         blink::web_pref::ScriptFontFamilyMap* map);

 protected:
  // Exposed and virtual for testing.
  // Fetches the font without checking the cache.
  virtual std::u16string FetchFont(const char* script, const char* map_name);

 private:
  FRIEND_TEST_ALL_PREFIXES(::FontFamilyCacheTest, Caching);

  // Map from script to font.
  // Key comparison uses pointer equality.
  using ScriptFontMap = std::unordered_map<const char*, std::u16string>;

  // Map from font family to ScriptFontMap.
  // Key comparison uses pointer equality.
  using FontFamilyMap = std::unordered_map<const char*, ScriptFontMap>;

  // 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.
  // |script| and |map_name| must be compile time constants. Two behaviors rely
  // on this: key comparison uses pointer equality, and keys must outlive the
  // maps.
  std::u16string 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.
  raw_ptr<const PrefService, AcrossTasksDanglingUntriaged> prefs_;

  // Reacts to profile font changes. |font_change_registrar_| will be
  // automatically unregistered when the FontPrefChangeNotifier is destroyed as
  // part of Profile destruction, thus ensuring safe unregistration even though
  // |this| is destroyed after the Profile destructor completes as part of
  // Profile's super class destructor ~base::SupportsUserData.
  FontPrefChangeNotifier::Registrar font_change_registrar_;
};

#endif  // CHROME_BROWSER_FONT_FAMILY_CACHE_H_