Commit d4564dcc authored by Dominik Röttsches's avatar Dominik Röttsches Committed by Commit Bot

Reland: Sequentialise access to callbacks in DWriteFontLookupTableBuilder

This reland fixes the sequenced task runner initialisation. Set the
task runner in the constructor and reset it for each unit test execution.

Since there may be multiple instance of DWriteFontProxyImpl instantiated
for multiple RenderProcessHosts, and
DWriteFontProxyImpl::GetUniqueNameLookupTable may access
DWriteFontLookupTableBuilder::QueueShareMemoryRegionWhenReady from
separate threads, there may be race conditions around the
pending_callbacks_ member of DWriteFontLookupTableBuilder.

Sequentialise and guard access to pending_callbacks_ with a separate
sequenced task runner.

Remove explicit configuration of the FontUniqueNameBrowserTest cache
directory as [1] introduced a callback function in
ShellContentBrowserClient which sets this correctly. This avoids
instantiating DWriteFontLookupTableBuilder too early when the ThreadPool
is not yet available in a BrowserTest.

[1] https://chromium-review.googlesource.com/c/chromium/src/+/1776358/9/content/shell/browser/shell_content_browser_client.cc#422

Fixed: 1047054
Change-Id: I38cf8b84a48315980624b68bbf55d3727be457b8
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2032119Reviewed-by: default avatarMatthew Denton <mpdenton@chromium.org>
Commit-Queue: Dominik Röttsches <drott@chromium.org>
Cr-Commit-Position: refs/heads/master@{#737466}
parent 7ff998a4
......@@ -117,20 +117,10 @@ class FontUniqueNameBrowserTest : public DevToolsProtocolTest {
}
#if defined(OS_WIN)
// The Windows service for font unique name lookup needs a cache directory to
// persist the cached information. Configure a temporary one before running
// this test.
void SetUpInProcessBrowserTestFixture() override {
DevToolsProtocolTest::SetUpInProcessBrowserTestFixture();
DWriteFontLookupTableBuilder* table_builder =
DWriteFontLookupTableBuilder::GetInstance();
ASSERT_TRUE(cache_directory_.CreateUniqueTempDir());
table_builder->SetCacheDirectoryForTesting(cache_directory_.GetPath());
}
void PreRunTestOnMainThread() override {
DWriteFontLookupTableBuilder* table_builder =
DWriteFontLookupTableBuilder::GetInstance();
table_builder->ResetStateForTesting();
table_builder->SchedulePrepareFontUniqueNameTableIfNeeded();
DevToolsProtocolTest::PreRunTestOnMainThread();
}
......
......@@ -23,6 +23,7 @@
#include "base/strings/utf_string_conversions.h"
#include "base/task/post_task.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "base/threading/scoped_blocking_call.h"
#include "base/threading/sequenced_task_runner_handle.h"
#include "base/threading/thread_restrictions.h"
......@@ -129,16 +130,29 @@ DWriteFontLookupTableBuilder::FamilyResult::~FamilyResult() = default;
DWriteFontLookupTableBuilder::DWriteFontLookupTableBuilder()
: font_indexing_timeout_(kFontIndexingTimeoutDefault) {
ResetCallbacksAccessTaskRunner();
InitializeCacheDirectoryFromProfile();
}
void DWriteFontLookupTableBuilder::ResetCallbacksAccessTaskRunner() {
callbacks_access_task_runner_ = base::ThreadPool::CreateSequencedTaskRunner({
base::TaskPriority::USER_VISIBLE,
#if DCHECK_IS_ON()
// Needed for DCHECK in DuplicateMemoryRegion() which performs file
// operations to detect cache directory.
base::MayBlock(),
#endif
base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN
});
DETACH_FROM_SEQUENCE(callbacks_access_sequence_checker_);
}
void DWriteFontLookupTableBuilder::InitializeCacheDirectoryFromProfile() {
// In FontUniqueNameBrowserTest the DWriteFontLookupTableBuilder is
// instantiated to configure the cache directory for testing explicitly before
// GetContentClient() is available. Catch this case here. It is safe to not
// set the cache directory here, as an invalid cache directory would be
// detected by TableCacheFilePath and the LoadFromFile and PersistToFile
// methods.
// Unit tests that do not launch a full browser environment usually don't need
// testing of src:local()-style font matching. Check that an environment is
// present here and configcure the cache directory based on that. If none is
// configured, catch this in DuplicateMemoryRegion(), i.e. when a client
// tries to use this API.
cache_directory_ =
GetContentClient() && GetContentClient()->browser()
? GetContentClient()->browser()->GetFontLookupTableCacheDir()
......@@ -250,7 +264,8 @@ base::TimeDelta DWriteFontLookupTableBuilder::IndexingTimeout() {
return font_indexing_timeout_;
}
void DWriteFontLookupTableBuilder::PostCallbacks() {
void DWriteFontLookupTableBuilder::PostCallbacksImpl() {
DCHECK_CALLED_ON_VALID_SEQUENCE(callbacks_access_sequence_checker_);
for (auto& pending_callback : pending_callbacks_) {
pending_callback.task_runner->PostTask(
FROM_HERE, base::BindOnce(std::move(pending_callback.mojo_callback),
......@@ -259,6 +274,13 @@ void DWriteFontLookupTableBuilder::PostCallbacks() {
pending_callbacks_.clear();
}
void DWriteFontLookupTableBuilder::PostCallbacks() {
callbacks_access_task_runner_->PostTask(
FROM_HERE,
base::BindOnce(&DWriteFontLookupTableBuilder::PostCallbacksImpl,
base::Unretained(this)));
}
base::FilePath DWriteFontLookupTableBuilder::TableCacheFilePath() {
if (!EnsureCacheDirectory(cache_directory_))
return base::FilePath();
......@@ -300,18 +322,38 @@ DWriteFontLookupTableBuilder::CallbackOnTaskRunner::CallbackOnTaskRunner(
DWriteFontLookupTableBuilder::CallbackOnTaskRunner::~CallbackOnTaskRunner() =
default;
void DWriteFontLookupTableBuilder::QueueShareMemoryRegionWhenReadyImpl(
scoped_refptr<base::SequencedTaskRunner> task_runner,
blink::mojom::DWriteFontProxy::GetUniqueNameLookupTableCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(callbacks_access_sequence_checker_);
// Don't queue but post response directly if the table is already ready for
// sharing with renderers to cover the condition in which the font table
// becomes ready briefly after a renderer asking for
// GetUniqueNameLookupTableIfAvailable(), receiving the information that it
// wasn't ready. (https://crbug.com/977283)
if (font_table_built_.IsSignaled()) {
task_runner->PostTask(FROM_HERE, base::BindOnce(std::move(callback),
DuplicateMemoryRegion()));
return;
}
pending_callbacks_.push_back(
CallbackOnTaskRunner(std::move(task_runner), std::move(callback)));
}
void DWriteFontLookupTableBuilder::QueueShareMemoryRegionWhenReady(
scoped_refptr<base::SequencedTaskRunner> task_runner,
blink::mojom::DWriteFontProxy::GetUniqueNameLookupTableCallback callback) {
TRACE_EVENT0("dwrite,fonts",
"DWriteFontLookupTableBuilder::QueueShareMemoryRegionWhenReady");
DCHECK(!HasDWriteUniqueFontLookups());
pending_callbacks_.emplace_back(std::move(task_runner), std::move(callback));
// Cover for the condition in which the font table becomes ready briefly after
// a renderer asking for GetUniqueNameLookupTableIfAvailable(), receiving the
// information that it wasn't ready.
if (font_table_built_.IsSignaled())
PostCallbacks();
CHECK(callbacks_access_task_runner_);
callbacks_access_task_runner_->PostTask(
FROM_HERE,
base::BindOnce(
&DWriteFontLookupTableBuilder::QueueShareMemoryRegionWhenReadyImpl,
base::Unretained(this), std::move(task_runner), std::move(callback)));
}
bool DWriteFontLookupTableBuilder::FontUniqueNameTableReady() {
......@@ -730,6 +772,7 @@ void DWriteFontLookupTableBuilder::ResetLookupTableForTesting() {
font_indexing_timeout_ = kFontIndexingTimeoutDefault;
font_table_memory_ = base::MappedReadOnlyRegion();
caching_enabled_ = true;
ResetCallbacksAccessTaskRunner();
font_table_built_.Reset();
}
......
......@@ -176,6 +176,21 @@ class CONTENT_EXPORT DWriteFontLookupTableBuilder {
// constructed protobuf to disk.
void FinalizeFontTable();
// Internal implementation of adding a callback request to the list in order
// to sequentialise access to pending_callbacks_.
void QueueShareMemoryRegionWhenReadyImpl(
scoped_refptr<base::SequencedTaskRunner> task_runner,
blink::mojom::DWriteFontProxy::GetUniqueNameLookupTableCallback callback);
// Internal implementation of posting the callbacks, running on the sequence
// that sequentialises access to pending_callbacks_.
void PostCallbacksImpl();
// Resets the internal task runner guarding access to pending_callbacks_, used
// in unit tests, as the TaskEnvironment used in tests tears down and resets
// the ThreadPool between tests, and the TaskRunner depends on it.
void ResetCallbacksAccessTaskRunner();
void OnTimeout();
bool IsFontUniqueNameTableValid();
......@@ -232,6 +247,8 @@ class CONTENT_EXPORT DWriteFontLookupTableBuilder {
std::vector<CallbackOnTaskRunner> pending_callbacks_;
std::map<HRESULT, unsigned> scanning_error_reasons_;
scoped_refptr<base::SequencedTaskRunner> callbacks_access_task_runner_;
SEQUENCE_CHECKER(callbacks_access_sequence_checker_);
DISALLOW_COPY_AND_ASSIGN(DWriteFontLookupTableBuilder);
};
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment