From ec5f3a89f86615ac976b17b3b0881eb53464fe39 Mon Sep 17 00:00:00 2001 From: Alexandru Marc Date: Tue, 5 Nov 2024 14:49:01 +0200 Subject: [PATCH] Backed out changeset e374514be19b (bug 1863046) for causing Bug 1863046 . CLOSED TREE --- dom/base/Document.cpp | 11 ++++---- dom/base/nsContentUtils.cpp | 27 +++++++++++++++++-- dom/base/nsContentUtils.h | 5 ++++ dom/events/IMEStateManager.cpp | 5 ++-- dom/media/mediacontrol/MediaStatusManager.cpp | 2 +- dom/quota/StorageManager.cpp | 2 +- dom/security/sanitizer/Sanitizer.cpp | 2 +- image/imgLoader.cpp | 2 +- .../components/antitracking/StorageAccess.cpp | 5 ++-- 9 files changed, 46 insertions(+), 15 deletions(-) diff --git a/dom/base/Document.cpp b/dom/base/Document.cpp index aeab83687390..ae45ef47c84e 100644 --- a/dom/base/Document.cpp +++ b/dom/base/Document.cpp @@ -3010,8 +3010,8 @@ void Document::ResetToURI(nsIURI* aURI, nsILoadGroup* aLoadGroup, // This is asserting that if we previously set mIsInPrivateBrowsing // to true from the channel in Document::Reset, that the loadContext // also believes it to be true. - MOZ_ASSERT(!mIsInPrivateBrowsing || - mIsInPrivateBrowsing == loadContext->UsePrivateBrowsing()); + // MOZ_ASSERT(!mIsInPrivateBrowsing || + // mIsInPrivateBrowsing == loadContext->UsePrivateBrowsing()); mIsInPrivateBrowsing = loadContext->UsePrivateBrowsing(); } } @@ -18564,7 +18564,8 @@ already_AddRefed Document::RequestStorageAccessForOrigin( } if (AntiTrackingUtils::CheckStoragePermission( self->NodePrincipal(), type, - self->IsInPrivateBrowsing(), nullptr, 0)) { + nsContentUtils::IsInPrivateBrowsing(self), nullptr, + 0)) { return MozPromise::CreateAndResolve( true, __func__); } @@ -18896,8 +18897,8 @@ already_AddRefed Document::CompleteStorageAccessRequestFromSite( false, __func__); } if (AntiTrackingUtils::CheckStoragePermission( - self->NodePrincipal(), type, self->IsInPrivateBrowsing(), - nullptr, 0)) { + self->NodePrincipal(), type, + nsContentUtils::IsInPrivateBrowsing(self), nullptr, 0)) { return StorageAccessAPIHelper:: StorageAccessPermissionGrantPromise::CreateAndResolve( StorageAccessAPIHelper::eAllowAutoGrant, __func__); diff --git a/dom/base/nsContentUtils.cpp b/dom/base/nsContentUtils.cpp index d5db93db983f..e2739c817d8c 100644 --- a/dom/base/nsContentUtils.cpp +++ b/dom/base/nsContentUtils.cpp @@ -3943,12 +3943,35 @@ nsPresContext* nsContentUtils::GetContextForContent( return presShell->GetPresContext(); } +// static +bool nsContentUtils::IsInPrivateBrowsing(const Document* aDoc) { + if (!aDoc) { + return false; + } + + nsCOMPtr loadGroup = aDoc->GetDocumentLoadGroup(); + // See duplicated code below in IsInPrivateBrowsing(nsILoadGroup*) + // and Document::Reset/ResetToURI + if (loadGroup) { + nsCOMPtr callbacks; + loadGroup->GetNotificationCallbacks(getter_AddRefs(callbacks)); + if (callbacks) { + nsCOMPtr loadContext = do_GetInterface(callbacks); + if (loadContext) { + return loadContext->UsePrivateBrowsing(); + } + } + } + + nsCOMPtr channel = aDoc->GetChannel(); + return channel && NS_UsePrivateBrowsing(channel); +} + // static bool nsContentUtils::IsInPrivateBrowsing(nsILoadGroup* aLoadGroup) { if (!aLoadGroup) { return false; } - // See duplicated code in Document::Reset/ResetToURI bool isPrivate = false; nsCOMPtr callbacks; aLoadGroup->GetNotificationCallbacks(getter_AddRefs(callbacks)); @@ -3980,7 +4003,7 @@ imgLoader* nsContentUtils::GetImgLoaderForDocument(Document* aDoc) { if (!aDoc) { return imgLoader::NormalLoader(); } - const bool isPrivate = aDoc->IsInPrivateBrowsing(); + bool isPrivate = IsInPrivateBrowsing(aDoc); return isPrivate ? imgLoader::PrivateBrowsingLoader() : imgLoader::NormalLoader(); } diff --git a/dom/base/nsContentUtils.h b/dom/base/nsContentUtils.h index 30f2d7c47d05..81f771b44efc 100644 --- a/dom/base/nsContentUtils.h +++ b/dom/base/nsContentUtils.h @@ -1198,6 +1198,11 @@ class nsContentUtils { bool aIsForWindow, uint32_t* aArgCount, const char*** aArgNames); + /** + * Returns true if this document is in a Private Browsing window. + */ + static bool IsInPrivateBrowsing(const Document* aDoc); + /** * Returns true if this loadGroup uses Private Browsing. */ diff --git a/dom/events/IMEStateManager.cpp b/dom/events/IMEStateManager.cpp index 86e3725b4cc5..2ca06d40923b 100644 --- a/dom/events/IMEStateManager.cpp +++ b/dom/events/IMEStateManager.cpp @@ -1982,8 +1982,9 @@ void IMEStateManager::SetIMEState(const IMEState& aState, context.mHasHandledUserInput = aPresContext && aPresContext->PresShell()->HasHandledUserInput(); - context.mInPrivateBrowsing = aPresContext && aPresContext->Document() && - aPresContext->Document()->IsInPrivateBrowsing(); + context.mInPrivateBrowsing = + aPresContext && + nsContentUtils::IsInPrivateBrowsing(aPresContext->Document()); const RefPtr focusedElement = aElement ? Element::FromNodeOrNull( diff --git a/dom/media/mediacontrol/MediaStatusManager.cpp b/dom/media/mediacontrol/MediaStatusManager.cpp index 9d62b302fb51..651b0adfbb02 100644 --- a/dom/media/mediacontrol/MediaStatusManager.cpp +++ b/dom/media/mediacontrol/MediaStatusManager.cpp @@ -514,7 +514,7 @@ bool MediaStatusManager::IsInPrivateBrowsing() const { if (!element) { return false; } - return element->OwnerDoc()->IsInPrivateBrowsing(); + return nsContentUtils::IsInPrivateBrowsing(element->OwnerDoc()); } MediaSessionPlaybackState MediaStatusManager::PlaybackState() const { diff --git a/dom/quota/StorageManager.cpp b/dom/quota/StorageManager.cpp index 4ac76b3a6252..1b6fa704f0b6 100644 --- a/dom/quota/StorageManager.cpp +++ b/dom/quota/StorageManager.cpp @@ -324,7 +324,7 @@ already_AddRefed ExecuteOpOnMainOrWorkerThread( new PersistentStoragePermissionRequest(principal, window, promise); // In private browsing mode, no permission prompt. - if (doc->IsInPrivateBrowsing()) { + if (nsContentUtils::IsInPrivateBrowsing(doc)) { aRv = request->Cancel(); } else if (!request->CheckPermissionDelegate()) { aRv = request->Cancel(); diff --git a/dom/security/sanitizer/Sanitizer.cpp b/dom/security/sanitizer/Sanitizer.cpp index 216f2d95195e..9d087523ff52 100644 --- a/dom/security/sanitizer/Sanitizer.cpp +++ b/dom/security/sanitizer/Sanitizer.cpp @@ -153,7 +153,7 @@ void Sanitizer::LogLocalizedString(const char* aName, if (window && window->GetDoc()) { auto* doc = window->GetDoc(); innerWindowID = doc->InnerWindowID(); - isPrivateBrowsing = doc->IsInPrivateBrowsing(); + isPrivateBrowsing = nsContentUtils::IsInPrivateBrowsing(doc); } nsAutoString logMsg; nsContentUtils::FormatLocalizedString(nsContentUtils::eSECURITY_PROPERTIES, diff --git a/image/imgLoader.cpp b/image/imgLoader.cpp index dc6f412542a2..aad8b8a14236 100644 --- a/image/imgLoader.cpp +++ b/image/imgLoader.cpp @@ -2318,7 +2318,7 @@ nsresult imgLoader::LoadImage( bool isPrivate = false; if (aLoadingDocument) { - isPrivate = aLoadingDocument->IsInPrivateBrowsing(); + isPrivate = nsContentUtils::IsInPrivateBrowsing(aLoadingDocument); } else if (aLoadGroup) { isPrivate = nsContentUtils::IsInPrivateBrowsing(aLoadGroup); } diff --git a/toolkit/components/antitracking/StorageAccess.cpp b/toolkit/components/antitracking/StorageAccess.cpp index 037be2d9a36e..478d29166026 100644 --- a/toolkit/components/antitracking/StorageAccess.cpp +++ b/toolkit/components/antitracking/StorageAccess.cpp @@ -114,7 +114,7 @@ static StorageAccess InternalStorageAllowedCheck( } // Check if we are in private browsing, and record that fact - if (document && document->IsInPrivateBrowsing()) { + if (nsContentUtils::IsInPrivateBrowsing(document)) { access = StorageAccess::ePrivateBrowsing; } @@ -889,6 +889,7 @@ bool ApproximateAllowAccessForWithoutChannel( AntiTrackingUtils::CreateStoragePermissionKey(principal, type); return AntiTrackingUtils::CheckStoragePermission( - parentPrincipal, type, parentDocument->IsInPrivateBrowsing(), nullptr, 0); + parentPrincipal, type, + nsContentUtils::IsInPrivateBrowsing(parentDocument), nullptr, 0); } } // namespace mozilla