File: font_table_persistence.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; 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 (113 lines) | stat: -rw-r--r-- 3,318 bytes parent folder | download | duplicates (6)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/public/common/font_unique_name_lookup/font_table_persistence.h"

#include <optional>

#include "base/compiler_specific.h"
#include "base/containers/span.h"
#include "base/hash/hash.h"
#include "base/pickle.h"
#include "base/threading/scoped_blocking_call.h"

namespace blink {

namespace font_table_persistence {

bool LoadFromFile(base::FilePath file_path,
                  base::MappedReadOnlyRegion* name_table_region) {
  DCHECK(!file_path.empty());
  // Reset to empty to ensure IsValid() is false if reading fails.
  *name_table_region = base::MappedReadOnlyRegion();
  std::vector<char> file_contents;
  {
    base::ScopedBlockingCall scoped_blocking_call(
        FROM_HERE, base::BlockingType::MAY_BLOCK);

    base::File table_cache_file(
        file_path, base::File::FLAG_OPEN | base::File::Flags::FLAG_READ);
    if (!table_cache_file.IsValid() || table_cache_file.GetLength() <= 0) {
      return false;
    }

    file_contents.resize(table_cache_file.GetLength());

    if (UNSAFE_TODO(table_cache_file.Read(0, file_contents.data(),
                                          file_contents.size())) <= 0) {
      return false;
    }
  }

  base::Pickle pickle =
      base::Pickle::WithUnownedBuffer(base::as_byte_span(file_contents));
  base::PickleIterator pickle_iterator(pickle);

  uint32_t checksum = 0;
  if (!pickle_iterator.ReadUInt32(&checksum)) {
    return false;
  }

  std::optional<base::span<const uint8_t>> read_result =
      pickle_iterator.ReadData();
  if (!read_result.has_value()) {
    return false;
  }
  base::span<const uint8_t> proto = read_result.value();
  if (proto.empty()) {
    return false;
  }

  if (checksum != base::PersistentHash(proto)) {
    return false;
  }

  blink::FontUniqueNameTable font_table;
  if (!font_table.ParseFromArray(proto.data(), proto.size())) {
    return false;
  }

  *name_table_region = base::ReadOnlySharedMemoryRegion::Create(proto.size());
  if (!name_table_region->IsValid() || !name_table_region->mapping.size()) {
    return false;
  }

  base::span(name_table_region->mapping).copy_from(proto);

  return true;
}

bool PersistToFile(const base::MappedReadOnlyRegion& name_table_region,
                   base::FilePath file_path) {
  DCHECK(name_table_region.mapping.IsValid());
  DCHECK(name_table_region.mapping.size());
  DCHECK(!file_path.empty());

  base::File table_cache_file(file_path, base::File::FLAG_CREATE_ALWAYS |
                                             base::File::Flags::FLAG_WRITE);
  if (!table_cache_file.IsValid()) {
    return false;
  }

  base::Pickle pickle;
  uint32_t checksum = base::PersistentHash(name_table_region.mapping);
  pickle.WriteUInt32(checksum);
  pickle.WriteData(name_table_region.mapping);
  DCHECK(pickle.size());
  {
    base::ScopedBlockingCall scoped_blocking_call(
        FROM_HERE, base::BlockingType::MAY_BLOCK);

    if (UNSAFE_TODO(table_cache_file.Write(0, pickle.data_as_char(),
                                           pickle.size())) == -1) {
      table_cache_file.SetLength(0);
      return false;
    }
  }
  return true;
}

}  // namespace font_table_persistence

}  // namespace blink