From 26ac53ba5e722a194fabab3e2030c7de5a3f2781 Mon Sep 17 00:00:00 2001 From: Dimi Lee Date: Wed, 8 Jul 2020 12:11:59 +0000 Subject: [PATCH] Bug 1437626 - P1. Use TYPE_DOCUMENT to determin whether a channel is a top-level load. r=baku We use whether channel->GetTopWindowURI returns a failure to determin if the channel is a top-level load. However, channels loaded by service worker or appcache don't have top-level window. This patch use TYPE_DOCUMENT to determin whether it is a top-level load. This patch also adds UrlClassifierCommon::GetTopWindowURI to print debug message because nsIChannel::GetTopWindowURI is not fission-compatible. Differential Revision: https://phabricator.services.mozilla.com/D80182 --- .../url-classifier/UrlClassifierCommon.cpp | 47 +++++++++++++++++-- netwerk/url-classifier/UrlClassifierCommon.h | 2 + 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/netwerk/url-classifier/UrlClassifierCommon.cpp b/netwerk/url-classifier/UrlClassifierCommon.cpp index e0abffaa686f..8e4bb0cb82f0 100644 --- a/netwerk/url-classifier/UrlClassifierCommon.cpp +++ b/netwerk/url-classifier/UrlClassifierCommon.cpp @@ -75,9 +75,12 @@ bool UrlClassifierCommon::ShouldEnableClassifier(nsIChannel* aChannel) { return false; } - rv = channel->GetTopWindowURI(getter_AddRefs(topWinURI)); - if (NS_FAILED(rv)) { - // Skipping top-level load. + nsCOMPtr loadInfo = aChannel->LoadInfo(); + MOZ_ASSERT(loadInfo); + + auto policyType = loadInfo->GetExternalContentPolicyType(); + if (policyType == nsIContentPolicy::TYPE_DOCUMENT) { + UC_LOG(("nsChannelClassifier: Skipping top-level load")); return false; } @@ -85,6 +88,10 @@ bool UrlClassifierCommon::ShouldEnableClassifier(nsIChannel* aChannel) { // the security state. If any channels are subsequently cancelled // (page elements blocked) the state will be then updated. if (UC_LOG_ENABLED()) { + nsCOMPtr topWinURI; + Unused << UrlClassifierCommon::GetTopWindowURI(aChannel, + getter_AddRefs(topWinURI)); + nsCString chanSpec = chanURI->GetSpecOrDefault(); chanSpec.Truncate( std::min(chanSpec.Length(), UrlClassifierCommon::sMaxSpecLength)); @@ -265,6 +272,37 @@ nsresult UrlClassifierCommon::SetBlockedContent(nsIChannel* channel, return NS_OK; } +/* static */ +nsresult UrlClassifierCommon::GetTopWindowURI(nsIChannel* aChannel, + nsIURI** aURI) { + MOZ_ASSERT(XRE_IsParentProcess()); + MOZ_ASSERT(aChannel); + + nsCOMPtr loadInfo = aChannel->LoadInfo(); + MOZ_ASSERT(loadInfo); + + RefPtr browsingContext; + nsresult rv = + loadInfo->GetTargetBrowsingContext(getter_AddRefs(browsingContext)); + if (NS_WARN_IF(NS_FAILED(rv)) || !browsingContext) { + return NS_ERROR_FAILURE; + } + + dom::CanonicalBrowsingContext* top = browsingContext->Canonical()->Top(); + dom::WindowGlobalParent* wgp = top->GetCurrentWindowGlobal(); + if (!wgp) { + return NS_ERROR_FAILURE; + } + + RefPtr uri = wgp->GetDocumentURI(); + if (!uri) { + return NS_ERROR_FAILURE; + } + + uri.forget(aURI); + return NS_OK; +} + /* static */ nsresult UrlClassifierCommon::CreatePairwiseEntityListURI(nsIChannel* aChannel, nsIURI** aURI) { @@ -279,7 +317,8 @@ nsresult UrlClassifierCommon::CreatePairwiseEntityListURI(nsIChannel* aChannel, } nsCOMPtr topWinURI; - rv = chan->GetTopWindowURI(getter_AddRefs(topWinURI)); + rv = + UrlClassifierCommon::GetTopWindowURI(aChannel, getter_AddRefs(topWinURI)); NS_ENSURE_SUCCESS(rv, rv); if (!topWinURI) { diff --git a/netwerk/url-classifier/UrlClassifierCommon.h b/netwerk/url-classifier/UrlClassifierCommon.h index 66209447f958..e49d02e63f71 100644 --- a/netwerk/url-classifier/UrlClassifierCommon.h +++ b/netwerk/url-classifier/UrlClassifierCommon.h @@ -86,6 +86,8 @@ class UrlClassifierCommon final { private: static uint32_t TableToClassificationFlag( const nsACString& aTable, const std::vector& aData); + + static nsresult GetTopWindowURI(nsIChannel* aChannel, nsIURI** aURI); }; } // namespace net