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
  114
  115
  116
  117

content / browser / font_access / font_enumeration_cache.cc [blame]

// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/font_access/font_enumeration_cache.h"

#include <algorithm>
#include <memory>
#include <optional>
#include <string>
#include <utility>

#include "base/i18n/rtl.h"
#include "base/memory/read_only_shared_memory_region.h"
#include "base/memory/scoped_refptr.h"
#include "base/sequence_checker.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "base/threading/sequence_bound.h"
#include "base/types/pass_key.h"
#include "content/browser/font_access/font_enumeration_data_source.h"
#include "third_party/blink/public/common/font_access/font_enumeration_table.pb.h"
#include "third_party/blink/public/mojom/font_access/font_access.mojom.h"

namespace content {

// static
base::SequenceBound<FontEnumerationCache> FontEnumerationCache::Create() {
  return base::SequenceBound<FontEnumerationCache>(
      base::ThreadPool::CreateSequencedTaskRunner(
          {base::MayBlock(), base::TaskPriority::BEST_EFFORT}),
      FontEnumerationDataSource::Create(), std::nullopt,
      base::PassKey<FontEnumerationCache>());
}

// static
base::SequenceBound<FontEnumerationCache>
FontEnumerationCache::CreateForTesting(
    scoped_refptr<base::SequencedTaskRunner> task_runner,
    std::unique_ptr<FontEnumerationDataSource> data_source,
    std::optional<std::string> locale_override) {
  DCHECK(data_source);
  return base::SequenceBound<FontEnumerationCache>(
      std::move(task_runner), std::move(data_source),
      std::move(locale_override), base::PassKey<FontEnumerationCache>());
}

FontEnumerationCache::FontEnumerationCache(
    std::unique_ptr<FontEnumerationDataSource> data_source,
    std::optional<std::string> locale_override,
    base::PassKey<FontEnumerationCache>)
    : data_source_(std::move(data_source)),
      locale_override_(std::move(locale_override)) {
  DCHECK(data_source_);
}

FontEnumerationCache::~FontEnumerationCache() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
}

FontEnumerationData FontEnumerationCache::GetFontEnumerationData() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  if (!initialized_) {
    std::string locale =
        locale_override_.value_or(base::i18n::GetConfiguredLocale());

    blink::FontEnumerationTable font_data = data_source_->GetFonts(locale);
    BuildEnumerationCache(font_data);
    initialized_ = true;
  }

  DCHECK(initialized_);
  return {.status = data_.status, .font_data = data_.font_data.Duplicate()};
}

void FontEnumerationCache::BuildEnumerationCache(
    blink::FontEnumerationTable& table) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  if (!FontEnumerationDataSource::IsOsSupported()) {
    data_.status = blink::mojom::FontEnumerationStatus::kUnimplemented;
    return;
  }

  // Postscript names, according to spec, are expected to be encoded in a subset
  // of ASCII. See:
  // https://docs.microsoft.com/en-us/typography/opentype/spec/name This is why
  // a "simple" byte-wise comparison is used.
  std::sort(table.mutable_fonts()->begin(), table.mutable_fonts()->end(),
            [](const blink::FontEnumerationTable_FontData& a,
               const blink::FontEnumerationTable_FontData& b) {
              return a.postscript_name() < b.postscript_name();
            });

  base::MappedReadOnlyRegion font_data_region =
      base::ReadOnlySharedMemoryRegion::Create(table.ByteSizeLong());

  if (!font_data_region.mapping.IsValid()) {
    data_.status = blink::mojom::FontEnumerationStatus::kUnexpectedError;
    return;
  }

  DCHECK_GE(font_data_region.mapping.size(), table.ByteSizeLong());
  base::span<uint8_t> font_mem(font_data_region.mapping);
  if (!table.SerializeToArray(font_mem.data(), font_mem.size())) {
    data_.status = blink::mojom::FontEnumerationStatus::kUnexpectedError;
    return;
  }

  data_.status = blink::mojom::FontEnumerationStatus::kOk;
  data_.font_data = std::move(font_data_region.region);
  return;
}

}  // namespace content