From 92c07bc4ad12c03ff627b3c78422662abe4ca98e Mon Sep 17 00:00:00 2001 From: Blake Kaplan Date: Mon, 21 Dec 2015 14:12:52 -0800 Subject: [PATCH] Bug 1229177 - Show the tracking protection shield for fetch and XHR requests. r=jduell --HG-- extra : rebase_source : 81f1819355ee85b323cdfff4505e20613c4c5296 --- browser/base/content/test/general/browser.ini | 6 +++ .../test/general/browser_trackingUI_6.js | 46 +++++++++++++++++++ .../test/general/file_trackingUI_6.html | 16 +++++++ .../content/test/general/file_trackingUI_6.js | 2 + .../general/file_trackingUI_6.js^headers^ | 1 + netwerk/protocol/http/HttpChannelChild.cpp | 11 +++-- netwerk/protocol/http/HttpChannelChild.h | 2 +- 7 files changed, 79 insertions(+), 5 deletions(-) create mode 100644 browser/base/content/test/general/browser_trackingUI_6.js create mode 100644 browser/base/content/test/general/file_trackingUI_6.html create mode 100644 browser/base/content/test/general/file_trackingUI_6.js create mode 100644 browser/base/content/test/general/file_trackingUI_6.js^headers^ diff --git a/browser/base/content/test/general/browser.ini b/browser/base/content/test/general/browser.ini index 47edbde1e030..169ba828b629 100644 --- a/browser/base/content/test/general/browser.ini +++ b/browser/base/content/test/general/browser.ini @@ -451,6 +451,12 @@ support-files = tags = trackingprotection support-files = trackingPage.html +[browser_trackingUI_6.js] +tags = trackingprotection +support-files = + file_trackingUI_6.html + file_trackingUI_6.js + file_trackingUI_6.js^headers^ [browser_trackingUI_telemetry.js] tags = trackingprotection support-files = diff --git a/browser/base/content/test/general/browser_trackingUI_6.js b/browser/base/content/test/general/browser_trackingUI_6.js new file mode 100644 index 000000000000..01670210d5ad --- /dev/null +++ b/browser/base/content/test/general/browser_trackingUI_6.js @@ -0,0 +1,46 @@ +const URL = "http://mochi.test:8888/browser/browser/base/content/test/general/file_trackingUI_6.html"; + +function waitForSecurityChange(numChanges = 1) { + return new Promise(resolve => { + let n = 0; + let listener = { + onSecurityChange: function() { + n = n + 1; + info ("Received onSecurityChange event " + n + " of " + numChanges); + if (n >= numChanges) { + gBrowser.removeProgressListener(listener); + resolve(); + } + } + }; + gBrowser.addProgressListener(listener); + }); +} + +add_task(function* test_fetch() { + yield new Promise(resolve => { + SpecialPowers.pushPrefEnv({ set: [['privacy.trackingprotection.enabled', true]] }, + resolve); + }); + + yield BrowserTestUtils.withNewTab({ gBrowser, url: URL }, function* (newTabBrowser) { + let securityChange = waitForSecurityChange(); + yield ContentTask.spawn(newTabBrowser, null, function* () { + yield content.wrappedJSObject.test_fetch() + .then((response) => { ok(false, "should have denied the request"); }) + .catch((e) => { ok(true, `Caught exception: ${e}`); }); + }); + yield securityChange; + + var TrackingProtection = newTabBrowser.ownerGlobal.TrackingProtection; + ok(TrackingProtection, "got TP object"); + ok(TrackingProtection.enabled, "TP is enabled"); + + is(TrackingProtection.content.getAttribute("state"), "blocked-tracking-content", + 'content: state="blocked-tracking-content"'); + is(TrackingProtection.icon.getAttribute("state"), "blocked-tracking-content", + 'icon: state="blocked-tracking-content"'); + is(TrackingProtection.icon.getAttribute("tooltiptext"), + gNavigatorBundle.getString("trackingProtection.icon.activeTooltip"), "correct tooltip"); + }); +}); diff --git a/browser/base/content/test/general/file_trackingUI_6.html b/browser/base/content/test/general/file_trackingUI_6.html new file mode 100644 index 000000000000..52e1ae63ffd7 --- /dev/null +++ b/browser/base/content/test/general/file_trackingUI_6.html @@ -0,0 +1,16 @@ + + + + + Testing the shield from fetch and XHR + + +

Hello there!

+ + + diff --git a/browser/base/content/test/general/file_trackingUI_6.js b/browser/base/content/test/general/file_trackingUI_6.js new file mode 100644 index 000000000000..f7ac687cfc94 --- /dev/null +++ b/browser/base/content/test/general/file_trackingUI_6.js @@ -0,0 +1,2 @@ +/* Some code goes here! */ +void 0; diff --git a/browser/base/content/test/general/file_trackingUI_6.js^headers^ b/browser/base/content/test/general/file_trackingUI_6.js^headers^ new file mode 100644 index 000000000000..cb762eff8068 --- /dev/null +++ b/browser/base/content/test/general/file_trackingUI_6.js^headers^ @@ -0,0 +1 @@ +Access-Control-Allow-Origin: * diff --git a/netwerk/protocol/http/HttpChannelChild.cpp b/netwerk/protocol/http/HttpChannelChild.cpp index 93a8db0d6c6f..b7ff2857282b 100644 --- a/netwerk/protocol/http/HttpChannelChild.cpp +++ b/netwerk/protocol/http/HttpChannelChild.cpp @@ -154,7 +154,7 @@ InterceptStreamListener::OnStopRequest(nsIRequest* aRequest, nsISupports* aConte { if (mOwner) { mOwner->DoPreOnStopRequest(aStatusCode); - mOwner->DoOnStopRequest(mOwner, mContext); + mOwner->DoOnStopRequest(mOwner, aStatusCode, mContext); } Cleanup(); return NS_OK; @@ -873,7 +873,7 @@ HttpChannelChild::OnStopRequest(const nsresult& channelStatus, // so make sure this goes out of scope before then. AutoEventEnqueuer ensureSerialDispatch(mEventQ); - DoOnStopRequest(this, mListenerContext); + DoOnStopRequest(this, channelStatus, mListenerContext); } ReleaseListeners(); @@ -902,12 +902,15 @@ HttpChannelChild::DoPreOnStopRequest(nsresult aStatus) } void -HttpChannelChild::DoOnStopRequest(nsIRequest* aRequest, nsISupports* aContext) +HttpChannelChild::DoOnStopRequest(nsIRequest* aRequest, nsresult aChannelStatus, nsISupports* aContext) { LOG(("HttpChannelChild::DoOnStopRequest [this=%p]\n", this)); MOZ_ASSERT(!mIsPending); - if (mStatus == NS_ERROR_TRACKING_URI) { + // NB: We use aChannelStatus here instead of mStatus because if there was an + // nsCORSListenerProxy on this request, it will override the tracking + // protection's return value. + if (aChannelStatus == NS_ERROR_TRACKING_URI) { nsChannelClassifier::SetBlockedTrackingContent(this); } diff --git a/netwerk/protocol/http/HttpChannelChild.h b/netwerk/protocol/http/HttpChannelChild.h index 8e08e9581beb..228a0875e9a2 100644 --- a/netwerk/protocol/http/HttpChannelChild.h +++ b/netwerk/protocol/http/HttpChannelChild.h @@ -166,7 +166,7 @@ private: void DoOnDataAvailable(nsIRequest* aRequest, nsISupports* aContext, nsIInputStream* aStream, uint64_t offset, uint32_t count); void DoPreOnStopRequest(nsresult aStatus); - void DoOnStopRequest(nsIRequest* aRequest, nsISupports* aContext); + void DoOnStopRequest(nsIRequest* aRequest, nsresult aChannelStatus, nsISupports* aContext); // Discard the prior interception and continue with the original network request. void ResetInterception();