Skip to content

Commit

Permalink
Document::Document shouldn't construct Editor object
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=267901

Reviewed by Yusuke Suzuki and Cameron McCormack.

Don't create Editor object inside Document::Document. This was getting triggered via
shouldAlwaysUseDirectionalSelection inside FrameSelection::FrameSelection.

Also added a bunch of assertions to ensure we don't initialize these lazily constructed
objects at the end of Document::Document.

* Source/WebCore/dom/Document.cpp:
(WebCore::m_frameIdentifier):
(WebCore::Document::ensureQuirks):
(WebCore::Document::ensureCachedResourceLoader):
(WebCore::Document::ensureExtensionStyleSheets):
(WebCore::Document::ensureMarkers):
(WebCore::Document::ensureVisitedLinkState):
(WebCore::Document::ensureFullscreenManager):
(WebCore::Document::fontLoader):
(WebCore::Document::ensureFontLoader):
(WebCore::Document::ensureFontSelector):
(WebCore::Document::ensureUndoManager):
(WebCore::Document::ensureEditor):
(WebCore::Document::ensureReportingScope):
(WebCore::Document::originIdentifierForPasteboard const):
(WebCore::Document::isDNSPrefetchEnabled const):
(WebCore::Document::parseDNSPrefetchControlHeader):
* Source/WebCore/dom/Document.h:
(WebCore::Document::isDNSPrefetchEnabled const): Deleted.
* Source/WebCore/editing/FrameSelection.cpp:
(WebCore::shouldAlwaysUseDirectionalSelection):

Canonical link: https://commits.webkit.org/273350@main
  • Loading branch information
rniwa committed Jan 23, 2024
1 parent e9a06d2 commit f1b26b9
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 24 deletions.
69 changes: 49 additions & 20 deletions Source/WebCore/dom/Document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -642,20 +642,25 @@ Document::Document(LocalFrame* frame, const Settings& settings, const URL& url,
if ((frame && frame->ownerElement()) || !url.isEmpty())
setURL(url);

protectedCachedResourceLoader()->setDocument(this);

resetLinkColor();
resetVisitedLinkColor();
resetActiveLinkColor();

initSecurityContext();
initDNSPrefetch();

for (auto& nodeListAndCollectionCount : m_nodeListAndCollectionCounts)
nodeListAndCollectionCount = 0;

InspectorInstrumentation::addEventListenersToNode(*this);
setStorageBlockingPolicy(m_settings->storageBlockingPolicy());

ASSERT(!m_quirks);
ASSERT(!m_cachedResourceLoader);
ASSERT(!m_extensionStyleSheets);
ASSERT(!m_visitedLinkState);
ASSERT(!m_markers);
ASSERT(!m_scriptRunner);
ASSERT(!m_moduleLoader);
ASSERT(!m_fullscreenManager);
ASSERT(!m_fontSelector);
ASSERT(!m_fontLoader);
ASSERT(!m_undoManager);
ASSERT(!m_editor);
ASSERT(!m_reportingScope);
m_constructionDidFinish = true;
}

void Document::createNewIdentifier()
Expand Down Expand Up @@ -906,40 +911,46 @@ void Document::commonTeardown()

Quirks& Document::ensureQuirks()
{
ASSERT(m_constructionDidFinish);
ASSERT(!m_quirks);
m_quirks = makeUnique<Quirks>(*this);
return *m_quirks;
}

CachedResourceLoader& Document::ensureCachedResourceLoader()
{
ASSERT(m_constructionDidFinish);
ASSERT(!m_cachedResourceLoader);
if (RefPtr frame = this->frame()) {
if (RefPtr loader = frame->loader().activeDocumentLoader()) {
m_cachedResourceLoader = &loader->cachedResourceLoader();
return *m_cachedResourceLoader;
m_cachedResourceLoader = [&]() -> Ref<CachedResourceLoader> {
if (RefPtr frame = this->frame()) {
if (RefPtr loader = frame->loader().activeDocumentLoader())
return loader->cachedResourceLoader();
}
}
m_cachedResourceLoader = CachedResourceLoader::create(nullptr);
return CachedResourceLoader::create(nullptr);
}();
m_cachedResourceLoader->setDocument(this);
return *m_cachedResourceLoader;
}

ExtensionStyleSheets& Document::ensureExtensionStyleSheets()
{
ASSERT(m_constructionDidFinish);
ASSERT(!m_extensionStyleSheets);
m_extensionStyleSheets = makeUnique<ExtensionStyleSheets>(*this);
return *m_extensionStyleSheets;
}

DocumentMarkerController& Document::ensureMarkers()
{
ASSERT(m_constructionDidFinish);
ASSERT(!m_markers);
m_markers = makeUnique<DocumentMarkerController>(*this);
return *m_markers;
}

VisitedLinkState& Document::ensureVisitedLinkState()
{
ASSERT(m_constructionDidFinish);
ASSERT(!m_visitedLinkState);
m_visitedLinkState = makeUnique<VisitedLinkState>(*this);
return *m_visitedLinkState;
Expand All @@ -948,6 +959,7 @@ VisitedLinkState& Document::ensureVisitedLinkState()
#if ENABLE(FULLSCREEN_API)
FullscreenManager& Document::ensureFullscreenManager()
{
ASSERT(m_constructionDidFinish);
ASSERT(!m_fullscreenManager);
m_fullscreenManager = makeUnique<FullscreenManager>(*this);
return *m_fullscreenManager;
Expand All @@ -956,13 +968,15 @@ FullscreenManager& Document::ensureFullscreenManager()

inline DocumentFontLoader& Document::fontLoader()
{
ASSERT(m_constructionDidFinish);
if (!m_fontLoader)
return ensureFontLoader();
return *m_fontLoader;
}

DocumentFontLoader& Document::ensureFontLoader()
{
ASSERT(m_constructionDidFinish);
ASSERT(!m_fontLoader);
m_fontLoader = makeUnique<DocumentFontLoader>(*this);
return *m_fontLoader;
Expand All @@ -975,6 +989,7 @@ CSSFontSelector* Document::cssFontSelector()

CSSFontSelector& Document::ensureFontSelector()
{
ASSERT(m_constructionDidFinish);
ASSERT(!m_fontSelector);
m_fontSelector = CSSFontSelector::create(*this);
m_fontSelector->registerForInvalidationCallbacks(*this);
Expand All @@ -983,6 +998,7 @@ CSSFontSelector& Document::ensureFontSelector()

UndoManager& Document::ensureUndoManager()
{
ASSERT(m_constructionDidFinish);
ASSERT(!m_undoManager);
m_undoManager = UndoManager::create(*this);
return *m_undoManager;
Expand All @@ -1004,13 +1020,15 @@ const Editor& Document::editor() const

Editor& Document::ensureEditor()
{
ASSERT(m_constructionDidFinish);
ASSERT(!m_editor);
m_editor = makeUnique<Editor>(*this);
return *m_editor;
}

ReportingScope& Document::ensureReportingScope()
{
ASSERT(m_constructionDidFinish);
ASSERT(!m_reportingScope);
m_reportingScope = ReportingScope::create(*this);
return *m_reportingScope;
Expand Down Expand Up @@ -7178,6 +7196,7 @@ bool Document::isTelephoneNumberParsingAllowed() const

String Document::originIdentifierForPasteboard() const
{
ASSERT(m_constructionDidFinish);
auto origin = securityOrigin().toString();
if (origin != "null"_s)
return origin;
Expand Down Expand Up @@ -7507,26 +7526,36 @@ TextAutoSizing& Document::textAutoSizing()
void Document::initDNSPrefetch()
{
m_haveExplicitlyDisabledDNSPrefetch = false;
m_isDNSPrefetchEnabled = settings().dnsPrefetchingEnabled() && securityOrigin().protocol() == "http"_s;
m_isDNSPrefetchEnabled = settings().dnsPrefetchingEnabled() && securityOrigin().protocol() == "http"_s ? TriState::True : TriState::False;

// Inherit DNS prefetch opt-out from parent frame
if (RefPtr parent = parentDocument()) {
if (!parent->isDNSPrefetchEnabled())
m_isDNSPrefetchEnabled = false;
m_isDNSPrefetchEnabled = TriState::False;
}
}

bool Document::isDNSPrefetchEnabled() const
{
if (m_isDNSPrefetchEnabled == TriState::Indeterminate)
const_cast<Document&>(*this).initDNSPrefetch();
return m_isDNSPrefetchEnabled == TriState::True;
}

void Document::parseDNSPrefetchControlHeader(const String& dnsPrefetchControl)
{
if (!settings().dnsPrefetchingEnabled())
return;

if (m_isDNSPrefetchEnabled == TriState::Indeterminate)
initDNSPrefetch();

if (equalLettersIgnoringASCIICase(dnsPrefetchControl, "on"_s) && !m_haveExplicitlyDisabledDNSPrefetch) {
m_isDNSPrefetchEnabled = true;
m_isDNSPrefetchEnabled = TriState::True;
return;
}

m_isDNSPrefetchEnabled = false;
m_isDNSPrefetchEnabled = TriState::False;
m_haveExplicitlyDisabledDNSPrefetch = true;
}

Expand Down
9 changes: 6 additions & 3 deletions Source/WebCore/dom/Document.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
#include <wtf/ObjectIdentifier.h>
#include <wtf/Observer.h>
#include <wtf/RobinHoodHashMap.h>
#include <wtf/TriState.h>
#include <wtf/UniqueRef.h>
#include <wtf/WeakHashMap.h>
#include <wtf/WeakHashSet.h>
Expand Down Expand Up @@ -1244,7 +1245,7 @@ class Document
HTMLCanvasElement* getCSSCanvasElement(const String& name);
String nameForCSSCanvasElement(const HTMLCanvasElement&) const;

bool isDNSPrefetchEnabled() const { return m_isDNSPrefetchEnabled; }
bool isDNSPrefetchEnabled() const;
void parseDNSPrefetchControlHeader(const String&);

WEBCORE_EXPORT void postTask(Task&&) final; // Executes the task on context's thread asynchronously.
Expand Down Expand Up @@ -2164,7 +2165,7 @@ class Document

HashSet<LiveNodeList*> m_listsInvalidatedAtDocument;
HashSet<HTMLCollection*> m_collectionsInvalidatedAtDocument;
unsigned m_nodeListAndCollectionCounts[numNodeListInvalidationTypes];
unsigned m_nodeListAndCollectionCounts[numNodeListInvalidationTypes] { 0 };

RefPtr<XPathEvaluator> m_xpathEvaluator;

Expand Down Expand Up @@ -2440,6 +2441,8 @@ class Document
StandaloneStatus m_xmlStandalone { StandaloneStatus::Unspecified };
bool m_hasXMLDeclaration { false };

bool m_constructionDidFinish { false };

#if ENABLE(DARK_MODE_CSS)
OptionSet<ColorScheme> m_colorScheme;
bool m_allowsColorSchemeTransformations { true };
Expand Down Expand Up @@ -2481,7 +2484,7 @@ class Document
bool m_isResolvingContainerQueries { false };

bool m_gotoAnchorNeededAfterStylesheetsLoad { false };
bool m_isDNSPrefetchEnabled { false };
TriState m_isDNSPrefetchEnabled { TriState::Indeterminate };
bool m_haveExplicitlyDisabledDNSPrefetch { false };

bool m_isSynthesized { false };
Expand Down
2 changes: 1 addition & 1 deletion Source/WebCore/editing/FrameSelection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ IntRect DragCaretController::editableElementRectInRootViewCoordinates() const

static inline bool shouldAlwaysUseDirectionalSelection(Document* document)
{
return !document || document->editor().behavior().shouldConsiderSelectionAsDirectional();
return !document || document->editingBehavior().shouldConsiderSelectionAsDirectional();
}

static inline bool isPageActive(Document* document)
Expand Down

0 comments on commit f1b26b9

Please sign in to comment.