Bug 1141814 - Part2: Lower the priority of channel loading tracking resource, r=honzab

--HG--
extra : rebase_source : 8b79d23c4d2f8a813a9e76816bd540dd91bc6f8e
This commit is contained in:
Kershaw Chang 2016-11-23 22:23:00 +01:00
Родитель 90c2d1f846
Коммит d7704a975d
4 изменённых файлов: 68 добавлений и 19 удалений

Просмотреть файл

@ -312,9 +312,9 @@ nsBaseChannel::ClassifyURI()
} }
if (mLoadFlags & LOAD_CLASSIFY_URI) { if (mLoadFlags & LOAD_CLASSIFY_URI) {
RefPtr<nsChannelClassifier> classifier = new nsChannelClassifier(); RefPtr<nsChannelClassifier> classifier = new nsChannelClassifier(this);
if (classifier) { if (classifier) {
classifier->Start(this); classifier->Start();
} else { } else {
Cancel(NS_ERROR_OUT_OF_MEMORY); Cancel(NS_ERROR_OUT_OF_MEMORY);
} }

Просмотреть файл

@ -25,6 +25,7 @@
#include "nsIScriptSecurityManager.h" #include "nsIScriptSecurityManager.h"
#include "nsISecureBrowserUI.h" #include "nsISecureBrowserUI.h"
#include "nsISecurityEventSink.h" #include "nsISecurityEventSink.h"
#include "nsISupportsPriority.h"
#include "nsIURL.h" #include "nsIURL.h"
#include "nsIWebProgressListener.h" #include "nsIWebProgressListener.h"
#include "nsNetUtil.h" #include "nsNetUtil.h"
@ -50,14 +51,25 @@ static LazyLogModule gChannelClassifierLog("nsChannelClassifier");
NS_IMPL_ISUPPORTS(nsChannelClassifier, NS_IMPL_ISUPPORTS(nsChannelClassifier,
nsIURIClassifierCallback) nsIURIClassifierCallback)
nsChannelClassifier::nsChannelClassifier() nsChannelClassifier::nsChannelClassifier(nsIChannel *aChannel)
: mIsAllowListed(false), : mIsAllowListed(false),
mSuspendedChannel(false) mSuspendedChannel(false),
mChannel(aChannel),
mTrackingProtectionEnabled(Nothing())
{ {
MOZ_ASSERT(mChannel);
} }
nsresult nsresult
nsChannelClassifier::ShouldEnableTrackingProtection(nsIChannel *aChannel, nsChannelClassifier::ShouldEnableTrackingProtection(bool *result)
{
nsresult rv = ShouldEnableTrackingProtectionInternal(mChannel, result);
mTrackingProtectionEnabled = Some(*result);
return rv;
}
nsresult
nsChannelClassifier::ShouldEnableTrackingProtectionInternal(nsIChannel *aChannel,
bool *result) bool *result)
{ {
// Should only be called in the parent process. // Should only be called in the parent process.
@ -252,10 +264,8 @@ nsChannelClassifier::NotifyTrackingProtectionDisabled(nsIChannel *aChannel)
} }
void void
nsChannelClassifier::Start(nsIChannel *aChannel) nsChannelClassifier::Start()
{ {
mChannel = aChannel;
nsresult rv = StartInternal(); nsresult rv = StartInternal();
if (NS_FAILED(rv)) { if (NS_FAILED(rv)) {
// If we aren't getting a callback for any reason, assume a good verdict and // If we aren't getting a callback for any reason, assume a good verdict and
@ -344,7 +354,19 @@ nsChannelClassifier::StartInternal()
bool expectCallback; bool expectCallback;
bool trackingProtectionEnabled = false; bool trackingProtectionEnabled = false;
(void)ShouldEnableTrackingProtection(mChannel, &trackingProtectionEnabled); if (mTrackingProtectionEnabled.isNothing()) {
(void)ShouldEnableTrackingProtection(&trackingProtectionEnabled);
} else {
trackingProtectionEnabled = mTrackingProtectionEnabled.value();
}
static bool sAnnotateChannelEnabled = false;
static bool sIsInited = false;
if (!sIsInited) {
sIsInited = true;
Preferences::AddBoolVarCache(&sAnnotateChannelEnabled,
"privacy.trackingprotection.annotate_channels");
}
if (LOG_ENABLED()) { if (LOG_ENABLED()) {
nsCOMPtr<nsIURI> principalURI; nsCOMPtr<nsIURI> principalURI;
@ -353,8 +375,8 @@ nsChannelClassifier::StartInternal()
"uri %s", this, principalURI->GetSpecOrDefault().get(), "uri %s", this, principalURI->GetSpecOrDefault().get(),
uri->GetSpecOrDefault().get())); uri->GetSpecOrDefault().get()));
} }
rv = uriClassifier->Classify(principal, trackingProtectionEnabled, this, rv = uriClassifier->Classify(principal, sAnnotateChannelEnabled | trackingProtectionEnabled,
&expectCallback); this, &expectCallback);
if (NS_FAILED(rv)) { if (NS_FAILED(rv)) {
return rv; return rv;
} }
@ -667,6 +689,26 @@ nsChannelClassifier::OnClassifyComplete(nsresult aErrorCode)
} }
MarkEntryClassified(aErrorCode); MarkEntryClassified(aErrorCode);
// The value of |mTrackingProtectionEnabled| should be assigned at
// |ShouldEnableTrackingProtection| before.
MOZ_ASSERT(mTrackingProtectionEnabled, "Should contain a value.");
if (aErrorCode == NS_ERROR_TRACKING_URI &&
!mTrackingProtectionEnabled.valueOr(false)) {
if (LOG_ENABLED()) {
nsCOMPtr<nsIURI> uri;
mChannel->GetURI(getter_AddRefs(uri));
LOG(("nsChannelClassifier[%p]: lower the priority of channel %p"
", since %s is a tracker", this, mChannel.get(),
uri->GetSpecOrDefault().get()));
}
nsCOMPtr<nsISupportsPriority> p = do_QueryInterface(mChannel);
if (p) {
p->SetPriority(nsISupportsPriority::PRIORITY_LOWEST);
}
aErrorCode = NS_OK;
}
if (NS_FAILED(aErrorCode)) { if (NS_FAILED(aErrorCode)) {
if (LOG_ENABLED()) { if (LOG_ENABLED()) {
nsCOMPtr<nsIURI> uri; nsCOMPtr<nsIURI> uri;

Просмотреть файл

@ -8,6 +8,7 @@
#include "nsIURIClassifier.h" #include "nsIURIClassifier.h"
#include "nsCOMPtr.h" #include "nsCOMPtr.h"
#include "mozilla/Attributes.h" #include "mozilla/Attributes.h"
#include "mozilla/Maybe.h"
class nsIChannel; class nsIChannel;
class nsIHttpChannelInternal; class nsIHttpChannelInternal;
@ -19,16 +20,16 @@ namespace net {
class nsChannelClassifier final : public nsIURIClassifierCallback class nsChannelClassifier final : public nsIURIClassifierCallback
{ {
public: public:
nsChannelClassifier(); explicit nsChannelClassifier(nsIChannel* aChannel);
NS_DECL_ISUPPORTS NS_DECL_ISUPPORTS
NS_DECL_NSIURICLASSIFIERCALLBACK NS_DECL_NSIURICLASSIFIERCALLBACK
// Calls nsIURIClassifier.Classify with the principal of the given channel, // Calls nsIURIClassifier.Classify with the principal of the given channel,
// and cancels the channel on a bad verdict. // and cancels the channel on a bad verdict.
void Start(nsIChannel *aChannel); void Start();
// Whether or not tracking protection should be enabled on this channel. // Whether or not tracking protection should be enabled on this channel.
nsresult ShouldEnableTrackingProtection(nsIChannel *aChannel, bool *result); nsresult ShouldEnableTrackingProtection(bool *result);
private: private:
// True if the channel is on the allow list. // True if the channel is on the allow list.
@ -36,6 +37,7 @@ private:
// True if the channel has been suspended. // True if the channel has been suspended.
bool mSuspendedChannel; bool mSuspendedChannel;
nsCOMPtr<nsIChannel> mChannel; nsCOMPtr<nsIChannel> mChannel;
Maybe<bool> mTrackingProtectionEnabled;
~nsChannelClassifier() {} ~nsChannelClassifier() {}
// Caches good classifications for the channel principal. // Caches good classifications for the channel principal.
@ -52,6 +54,9 @@ private:
// Checks that the channel was loaded by the URI currently loaded in aDoc // Checks that the channel was loaded by the URI currently loaded in aDoc
static bool SameLoadingURI(nsIDocument *aDoc, nsIChannel *aChannel); static bool SameLoadingURI(nsIDocument *aDoc, nsIChannel *aChannel);
nsresult ShouldEnableTrackingProtectionInternal(nsIChannel *aChannel,
bool *result);
public: public:
// If we are blocking tracking content, update the corresponding flag in // If we are blocking tracking content, update the corresponding flag in
// the respective docshell and call nsISecurityEventSink::onSecurityChange. // the respective docshell and call nsISecurityEventSink::onSecurityChange.

Просмотреть файл

@ -5885,12 +5885,14 @@ nsHttpChannel::BeginConnect()
return AsyncCall(&nsHttpChannel::HandleAsyncAPIRedirect); return AsyncCall(&nsHttpChannel::HandleAsyncAPIRedirect);
} }
// Check to see if this principal exists on local blocklists. // Check to see if this principal exists on local blocklists.
RefPtr<nsChannelClassifier> channelClassifier = new nsChannelClassifier(); RefPtr<nsChannelClassifier> channelClassifier = new nsChannelClassifier(this);
if (mLoadFlags & LOAD_CLASSIFY_URI) { if (mLoadFlags & LOAD_CLASSIFY_URI) {
nsCOMPtr<nsIURIClassifier> classifier = do_GetService(NS_URICLASSIFIERSERVICE_CONTRACTID); nsCOMPtr<nsIURIClassifier> classifier = do_GetService(NS_URICLASSIFIERSERVICE_CONTRACTID);
bool tpEnabled = false; bool tpEnabled = false;
channelClassifier->ShouldEnableTrackingProtection(this, &tpEnabled); channelClassifier->ShouldEnableTrackingProtection(&tpEnabled);
if (classifier && tpEnabled) { bool annotateChannelEnabled =
Preferences::GetBool("privacy.trackingprotection.annotate_channels");
if (classifier && (tpEnabled || annotateChannelEnabled)) {
// We skip speculative connections by setting mLocalBlocklist only // We skip speculative connections by setting mLocalBlocklist only
// when tracking protection is enabled. Though we could do this for // when tracking protection is enabled. Though we could do this for
// both phishing and malware, it is not necessary for correctness, // both phishing and malware, it is not necessary for correctness,
@ -6012,7 +6014,7 @@ nsHttpChannel::BeginConnect()
// be overridden. // be overridden.
LOG(("nsHttpChannel::Starting nsChannelClassifier %p [this=%p]", LOG(("nsHttpChannel::Starting nsChannelClassifier %p [this=%p]",
channelClassifier.get(), this)); channelClassifier.get(), this));
channelClassifier->Start(this); channelClassifier->Start();
if (callContinueBeginConnect) { if (callContinueBeginConnect) {
return ContinueBeginConnectWithResult(); return ContinueBeginConnectWithResult();
} }