From fed4ed04157e01745480c1d8c9fae82821bfd035 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Tue, 13 Jun 2023 20:12:21 +0000 Subject: [PATCH] Bug 1838265 - Clean-up code. r=smaug, a=dmeehan Trivialish clean-ups while I was looking at this code, and related clean-ups elsewhere. Differential Revision: https://phabricator.services.mozilla.com/D180850 --- dom/base/Document.cpp | 87 ++++++++++++++++--------------------------- 1 file changed, 33 insertions(+), 54 deletions(-) diff --git a/dom/base/Document.cpp b/dom/base/Document.cpp index f7853913a5e9..6d4a5df62daf 100644 --- a/dom/base/Document.cpp +++ b/dom/base/Document.cpp @@ -4097,9 +4097,8 @@ void Document::SetDocumentURI(nsIURI* aURI) { } // Tell our WindowGlobalParent that the document's URI has been changed. - nsPIDOMWindowInner* inner = GetInnerWindow(); - if (inner && inner->GetWindowGlobalChild()) { - inner->GetWindowGlobalChild()->SetDocumentURI(mDocumentURI); + if (WindowGlobalChild* wgc = GetWindowGlobalChild()) { + wgc->SetDocumentURI(mDocumentURI); } } @@ -7154,8 +7153,7 @@ void Document::DeletePresShell() { void Document::DisallowBFCaching(uint32_t aStatus) { NS_ASSERTION(!mBFCacheEntry, "We're already in the bfcache!"); if (!mBFCacheDisallowed) { - WindowGlobalChild* wgc = GetWindowGlobalChild(); - if (wgc) { + if (WindowGlobalChild* wgc = GetWindowGlobalChild()) { wgc->SendUpdateBFCacheStatus(aStatus, 0); } } @@ -8945,8 +8943,7 @@ void Document::SetDomain(const nsAString& aDomain, ErrorResult& rv) { MOZ_ALWAYS_SUCCEEDS(NodePrincipal()->SetDomain(newURI)); MOZ_ALWAYS_SUCCEEDS(PartitionedPrincipal()->SetDomain(newURI)); - WindowGlobalChild* wgc = GetWindowGlobalChild(); - if (wgc) { + if (WindowGlobalChild* wgc = GetWindowGlobalChild()) { wgc->SendSetDocumentDomain(newURI); } } @@ -9112,7 +9109,9 @@ Element* Document::GetTitleElement() { // we know there is nothing to do here. This avoids us having to search // the whole DOM if someone calls document.title on a large document // without a title. - if (!mMayHaveTitleElement) return nullptr; + if (!mMayHaveTitleElement) { + return nullptr; + } Element* root = GetRootElement(); if (root && root->IsSVGElement(nsGkAtoms::svg)) { @@ -9128,16 +9127,12 @@ Element* Document::GetTitleElement() { // We check the HTML namespace even for non-HTML documents, except SVG. This // matches the spec and the behavior of all tested browsers. - // We avoid creating a live nsContentList since we don't need to watch for DOM - // tree mutations. - RefPtr<nsContentList> list = new nsContentList( - this, kNameSpaceID_XHTML, nsGkAtoms::title, nsGkAtoms::title, - /* aDeep = */ true, - /* aLiveList = */ false); - - nsIContent* first = list->Item(0, false); - - return first ? first->AsElement() : nullptr; + for (nsINode* node = GetFirstChild(); node; node = node->GetNextNode(this)) { + if (node->IsHTMLElement(nsGkAtoms::title)) { + return node->AsElement(); + } + } + return nullptr; } void Document::GetTitle(nsAString& aTitle) { @@ -9148,20 +9143,15 @@ void Document::GetTitle(nsAString& aTitle) { return; } - nsAutoString tmp; - if (rootElement->IsXULElement()) { - rootElement->GetAttr(kNameSpaceID_None, nsGkAtoms::title, tmp); + rootElement->GetAttr(nsGkAtoms::title, aTitle); + } else if (Element* title = GetTitleElement()) { + nsContentUtils::GetNodeTextContent(title, false, aTitle); } else { - Element* title = GetTitleElement(); - if (!title) { - return; - } - nsContentUtils::GetNodeTextContent(title, false, tmp); + return; } - tmp.CompressWhitespace(); - aTitle = tmp; + aTitle.CompressWhitespace(); } void Document::SetTitle(const nsAString& aTitle, ErrorResult& aRv) { @@ -9230,16 +9220,18 @@ void Document::NotifyPossibleTitleChange(bool aBoundTitleElement) { if (aBoundTitleElement) { mMayHaveTitleElement = true; } - if (mPendingTitleChangeEvent.IsPending()) return; + if (mPendingTitleChangeEvent.IsPending()) { + return; + } MOZ_RELEASE_ASSERT(NS_IsMainThread()); RefPtr<nsRunnableMethod<Document, void, false>> event = NewNonOwningRunnableMethod("Document::DoNotifyPossibleTitleChange", this, &Document::DoNotifyPossibleTitleChange); - nsresult rv = Dispatch(TaskCategory::Other, do_AddRef(event)); - if (NS_SUCCEEDED(rv)) { - mPendingTitleChangeEvent = std::move(event); + if (NS_WARN_IF(NS_FAILED(Dispatch(TaskCategory::Other, do_AddRef(event))))) { + return; } + mPendingTitleChangeEvent = std::move(event); } void Document::DoNotifyPossibleTitleChange() { @@ -9253,22 +9245,18 @@ void Document::DoNotifyPossibleTitleChange() { nsAutoString title; GetTitle(title); - RefPtr<PresShell> presShell = GetPresShell(); - if (presShell) { + if (RefPtr<PresShell> presShell = GetPresShell()) { nsCOMPtr<nsISupports> container = presShell->GetPresContext()->GetContainerWeak(); if (container) { - nsCOMPtr<nsIBaseWindow> docShellWin = do_QueryInterface(container); - if (docShellWin) { + if (nsCOMPtr<nsIBaseWindow> docShellWin = do_QueryInterface(container)) { docShellWin->SetTitle(title); } } } - if (nsPIDOMWindowInner* inner = GetInnerWindow()) { - if (WindowGlobalChild* child = inner->GetWindowGlobalChild()) { - child->SendUpdateDocumentTitle(title); - } + if (WindowGlobalChild* child = GetWindowGlobalChild()) { + child->SendUpdateDocumentTitle(title); } // Fire a DOM event for the title change. @@ -12242,8 +12230,7 @@ void Document::GetReadyState(nsAString& aReadyState) const { void Document::SuppressEventHandling(uint32_t aIncrease) { mEventsSuppressed += aIncrease; if (mEventsSuppressed == aIncrease) { - WindowGlobalChild* wgc = GetWindowGlobalChild(); - if (wgc) { + if (WindowGlobalChild* wgc = GetWindowGlobalChild()) { wgc->BlockBFCacheFor(BFCacheStatus::EVENT_HANDLING_SUPPRESSED); } } @@ -12665,8 +12652,7 @@ void Document::UnsuppressEventHandlingAndFireEvents(bool aFireEvents) { for (nsCOMPtr<Document>& doc : documents) { if (!doc->EventHandlingSuppressed()) { - WindowGlobalChild* wgc = doc->GetWindowGlobalChild(); - if (wgc) { + if (WindowGlobalChild* wgc = doc->GetWindowGlobalChild()) { wgc->UnblockBFCacheFor(BFCacheStatus::EVENT_HANDLING_SUPPRESSED); } @@ -17188,12 +17174,7 @@ already_AddRefed<mozilla::dom::Promise> Document::HasStorageAccess( RefPtr<Document::GetContentBlockingEventsPromise> Document::GetContentBlockingEvents() { - RefPtr<nsPIDOMWindowInner> inner = GetInnerWindow(); - if (!inner) { - return nullptr; - } - - RefPtr<WindowGlobalChild> wgc = inner->GetWindowGlobalChild(); + RefPtr<WindowGlobalChild> wgc = GetWindowGlobalChild(); if (!wgc) { return nullptr; } @@ -18582,8 +18563,7 @@ void Document::DisableChildElementInPictureInPictureMode() { void Document::AddMediaElementWithMSE() { if (mMediaElementWithMSECount++ == 0) { - WindowGlobalChild* wgc = GetWindowGlobalChild(); - if (wgc) { + if (WindowGlobalChild* wgc = GetWindowGlobalChild()) { wgc->BlockBFCacheFor(BFCacheStatus::CONTAINS_MSE_CONTENT); } } @@ -18592,8 +18572,7 @@ void Document::AddMediaElementWithMSE() { void Document::RemoveMediaElementWithMSE() { MOZ_ASSERT(mMediaElementWithMSECount > 0); if (--mMediaElementWithMSECount == 0) { - WindowGlobalChild* wgc = GetWindowGlobalChild(); - if (wgc) { + if (WindowGlobalChild* wgc = GetWindowGlobalChild()) { wgc->UnblockBFCacheFor(BFCacheStatus::CONTAINS_MSE_CONTENT); } } -- GitLab