0
Files
src/content/browser/font_access/font_enumeration_cache.cc
Arthur Sonzogni c686e8f4fd Rename {absl => std}::optional in //content/
Automated patch, intended to be effectively a no-op.

Context:
https://groups.google.com/a/chromium.org/g/cxx/c/nBD_1LaanTc/m/ghh-ZZhWAwAJ?utm_medium=email&utm_source=footer

As of https://crrev.com/1204351, absl::optional is now a type alias for
std::optional. We should migrate toward it.

Script:
```
function replace {
  echo "Replacing $1 by $2"
  git grep -l "$1" \
    | cut -f1 -d: \
    | grep \
      -e "^content" \
    | sort \
    | uniq \
    | grep \
      -e "\.h" \
      -e "\.cc" \
      -e "\.mm" \
      -e "\.py" \
    | xargs sed -i "s/$1/$2/g"
}
replace "absl::make_optional" "std::make_optional"
replace "absl::optional" "std::optional"
replace "absl::nullopt" "std::nullopt"
replace "absl::in_place" "std::in_place"
replace "absl::in_place_t" "std::in_place_t"
replace "\"third_party\/abseil-cpp\/absl\/types\/optional.h\"" "<optional>"
git cl format
```

# Skipping unrelated "check_network_annotation" errors.
NOTRY=True

Bug: chromium:1500249
Change-Id: Icfd31a71d8faf63a2e8d5401127e7ee74cc1c413
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5185537
Auto-Submit: Arthur Sonzogni <arthursonzogni@chromium.org>
Commit-Queue: Arthur Sonzogni <arthursonzogni@chromium.org>
Owners-Override: Avi Drissman <avi@chromium.org>
Reviewed-by: Avi Drissman <avi@chromium.org>
Reviewed-by: danakj <danakj@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1245739}
2024-01-11 08:36:37 +00:00

118 lines
4.0 KiB
C++

// 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());
if (!table.SerializeToArray(font_data_region.mapping.memory(),
font_data_region.mapping.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