From 3f336cb71f3da8f68ce54e141cc3d3b2a605bc9b Mon Sep 17 00:00:00 2001 From: Dimi Lee Date: Mon, 27 Jan 2020 19:23:05 +0000 Subject: [PATCH] Bug 1599046 - P4. Report content blocking log to console while running in the parent process r=Ehsan Depends on D57630 Differential Revision: https://phabricator.services.mozilla.com/D57807 --HG-- extra : moz-landing-system : lando --- .../url-classifier/UrlClassifierCommon.cpp | 5 ++ .../antitracking/AntiTrackingCommon.cpp | 71 +++++++++++++++---- 2 files changed, 62 insertions(+), 14 deletions(-) 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..d2d065989ea9 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,60 @@ 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); + + if (!httpChannel) { + return; + } + + 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 +943,7 @@ void NotifyBlockingDecisionInternal( aTrackingChannel, true, aRejectedReason, aURI); - ReportBlockingToConsole(aWindow, aURI, aRejectedReason); + ReportBlockingToConsole(aReportingChannel, aURI, aRejectedReason); } NotifyAllowDecisionInternal(aReportingChannel, aTrackingChannel, aURI, @@ -920,6 +961,8 @@ void NotifyBlockingDecisionInternal( AntiTrackingCommon::NotifyContentBlockingEvent(nullptr, aReportingChannel, aTrackingChannel, true, aRejectedReason, aURI); + + ReportBlockingToConsole(aReportingChannel, aURI, aRejectedReason); } NotifyAllowDecisionInternal(aReportingChannel, aTrackingChannel, aURI,