blob: 227955034f695511c69e4b684c089840ef0c6594 [file] [log] [blame]
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/core/css/remote_font_face_source.h"
#include "third_party/blink/renderer/platform/heap/handle.h"
#include "third_party/blink/renderer/platform/timer.h"
namespace blink {
class Document;
class FontResource;
class ResourceFinishObserver;
// This class monitors font preloading and notifies the relevant document, so
// that it can manage the first rendering timing to work with preloaded fonts.
// Design doc:
class CORE_EXPORT FontPreloadManager final {
explicit FontPreloadManager(Document&);
~FontPreloadManager() = default;
FontPreloadManager(const FontPreloadManager&) = delete;
FontPreloadManager& operator=(const FontPreloadManager&) = delete;
bool HasPendingRenderBlockingFonts() const;
void WillBeginRendering();
void FontPreloadingStarted(FontResource*);
void FontPreloadingFinished(FontResource*, ResourceFinishObserver*);
void FontPreloadingDelaysRenderingTimerFired(TimerBase*);
// Exposed to web tests via internals.
void SetRenderDelayTimeoutForTest(base::TimeDelta timeout);
void Trace(Visitor* visitor);
friend class FontPreloadManagerTest;
// State of font preloading before lifecycle updates begin
enum class State {
// Rendering hasn't begun. No font preloading yet.
// Rendering hasn't begun. There are ongoing font preloadings.
// Rendering hasn't begun. At least one font has been preloaded,
// and all font preloading so far has finished.
// Rendering will begin soon or has begun. Font preloading shouldn't block
// rendering any more.
Member<Document> document_;
// Need to hold strong references here, otherwise they'll be GC-ed immediately
// as Resource only holds weak references.
HeapHashSet<Member<ResourceFinishObserver>> finish_observers_;
TaskRunnerTimer<FontPreloadManager> render_delay_timer_;
base::TimeDelta render_delay_timeout_;
State state_ = State::kInitial;
// TODO(xiaochengh): Do the same for fonts preloaded with Font Loading API
// TODO(xiaochengh): Do the same for fonts loaded for other reasons?
} // namespace blink