diff --git a/netwerk/url-classifier/UrlClassifierCommon.cpp b/netwerk/url-classifier/UrlClassifierCommon.cpp index 8156e58d635f..20be2903172c 100644 --- a/netwerk/url-classifier/UrlClassifierCommon.cpp +++ b/netwerk/url-classifier/UrlClassifierCommon.cpp @@ -215,6 +215,11 @@ nsresult UrlClassifierCommon::SetBlockedContent(nsIChannel* channel, return NS_OK; } + // TODO: ReportToConsole is called in the child process, + // If nsContentUtils::ReportToConsole is not fission compatiable(cannot report + // to correct top-level window), we need to do this in the parent process + // instead (find the top-level window in the parent and send an IPC to child + // processes to report console). nsCOMPtr thirdPartyUtil = services::GetThirdPartyUtil(); if (NS_WARN_IF(!thirdPartyUtil)) { return NS_OK; diff --git a/toolkit/components/antitracking/AntiTrackingCommon.cpp b/toolkit/components/antitracking/AntiTrackingCommon.cpp index 6f8ab7ded1e4..f25a3de1e19d 100644 --- a/toolkit/components/antitracking/AntiTrackingCommon.cpp +++ b/toolkit/components/antitracking/AntiTrackingCommon.cpp @@ -378,9 +378,10 @@ void RunConsoleReportingRunnable(already_AddRefed&& aRunnable) { } } -void ReportBlockingToConsole(nsPIDOMWindowOuter* aWindow, nsIURI* aURI, +void ReportBlockingToConsole(uint64_t aWindowID, nsIURI* aURI, uint32_t aRejectedReason) { - MOZ_ASSERT(aWindow && aURI); + MOZ_ASSERT(aWindowID); + MOZ_ASSERT(aURI); MOZ_ASSERT( aRejectedReason == 0 || aRejectedReason == @@ -394,11 +395,6 @@ void ReportBlockingToConsole(nsPIDOMWindowOuter* aWindow, nsIURI* aURI, aRejectedReason == nsIWebProgressListener::STATE_COOKIES_BLOCKED_ALL || aRejectedReason == nsIWebProgressListener::STATE_COOKIES_BLOCKED_FOREIGN); - RefPtr doc = aWindow->GetExtantDoc(); - if (NS_WARN_IF(!doc)) { - return; - } - nsAutoString sourceLine; uint32_t lineNumber = 0, columnNumber = 0; JSContext* cx = nsContentUtils::GetCurrentJSContext(); @@ -409,8 +405,8 @@ void ReportBlockingToConsole(nsPIDOMWindowOuter* aWindow, nsIURI* aURI, nsCOMPtr uri(aURI); RefPtr runnable = NS_NewRunnableFunction( - "ReportBlockingToConsoleDelayed", - [doc, sourceLine, lineNumber, columnNumber, uri, aRejectedReason]() { + "ReportBlockingToConsoleDelayed", [aWindowID, sourceLine, lineNumber, + columnNumber, uri, aRejectedReason]() { const char* message = nullptr; nsAutoCString category; // When changing this list, please make sure to update the corresponding @@ -455,15 +451,58 @@ void ReportBlockingToConsole(nsPIDOMWindowOuter* aWindow, nsIURI* aURI, CopyUTF8toUTF16(exposableURI->GetSpecOrDefault(), *params.AppendElement()); - nsContentUtils::ReportToConsole(nsIScriptError::warningFlag, category, - doc, nsContentUtils::eNECKO_PROPERTIES, - message, params, nullptr, sourceLine, - lineNumber, columnNumber); + nsAutoString errorText; + rv = nsContentUtils::FormatLocalizedString( + nsContentUtils::eNECKO_PROPERTIES, message, params, errorText); + NS_ENSURE_SUCCESS_VOID(rv); + + nsContentUtils::ReportToConsoleByWindowID( + errorText, nsIScriptError::warningFlag, category, aWindowID, + nullptr, sourceLine, lineNumber, columnNumber); }); RunConsoleReportingRunnable(runnable.forget()); } +void ReportBlockingToConsole(nsIChannel* aChannel, nsIURI* aURI, + uint32_t aRejectedReason) { + MOZ_ASSERT(aChannel && aURI); + + uint64_t windowID; + + if (XRE_IsParentProcess()) { + // Get the top-level window ID from the top-level BrowsingContext + nsCOMPtr loadInfo = aChannel->LoadInfo(); + RefPtr bc; + loadInfo->GetBrowsingContext(getter_AddRefs(bc)); + + if (!bc || bc->IsDiscarded()) { + return; + } + + bc = bc->Top(); + RefPtr wgp = + bc->Canonical()->GetCurrentWindowGlobal(); + if (!wgp) { + return; + } + + windowID = wgp->InnerWindowId(); + } else { + nsresult rv; + nsCOMPtr httpChannel = do_QueryInterface(aChannel, &rv); + + // TODO(bug 1611755): When fission is enabled, we might not be able to get + // the correct top-level window ID by using the API below. + rv = httpChannel->GetTopLevelContentWindowId(&windowID); + if (NS_FAILED(rv) || !windowID) { + windowID = nsContentUtils::GetInnerWindowID(httpChannel); + } + } + + ReportBlockingToConsole(windowID, aURI, aRejectedReason); +} + void ReportUnblockingToConsole( nsPIDOMWindowInner* aWindow, const nsAString& aTrackingOrigin, AntiTrackingCommon::StorageAccessGrantedReason aReason) { @@ -902,7 +941,7 @@ void NotifyBlockingDecisionInternal( aTrackingChannel, true, aRejectedReason, aURI); - ReportBlockingToConsole(aWindow, aURI, aRejectedReason); + ReportBlockingToConsole(aReportingChannel, aURI, aRejectedReason); } NotifyAllowDecisionInternal(aReportingChannel, aTrackingChannel, aURI, @@ -920,6 +959,8 @@ void NotifyBlockingDecisionInternal( AntiTrackingCommon::NotifyContentBlockingEvent(nullptr, aReportingChannel, aTrackingChannel, true, aRejectedReason, aURI); + + ReportBlockingToConsole(aReportingChannel, aURI, aRejectedReason); } NotifyAllowDecisionInternal(aReportingChannel, aTrackingChannel, aURI,