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
This commit is contained in:
Dimi Lee 2020-07-08 12:11:59 +00:00
parent 226bd5539f
commit 26ac53ba5e
2 changed files with 45 additions and 4 deletions

View File

@ -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<nsILoadInfo> 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<nsIURI> 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<nsILoadInfo> loadInfo = aChannel->LoadInfo();
MOZ_ASSERT(loadInfo);
RefPtr<dom::BrowsingContext> 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<nsIURI> 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<nsIURI> topWinURI;
rv = chan->GetTopWindowURI(getter_AddRefs(topWinURI));
rv =
UrlClassifierCommon::GetTopWindowURI(aChannel, getter_AddRefs(topWinURI));
NS_ENSURE_SUCCESS(rv, rv);
if (!topWinURI) {

View File

@ -86,6 +86,8 @@ class UrlClassifierCommon final {
private:
static uint32_t TableToClassificationFlag(
const nsACString& aTable, const std::vector<ClassificationData>& aData);
static nsresult GetTopWindowURI(nsIChannel* aChannel, nsIURI** aURI);
};
} // namespace net