Bug 1312515 - Part 1: Lower the channel's priority if this XHR is created from tracking script. r=baku

With nsIDocument::IsScriptTracking, we know that whether a script is a tracking script. If the XHR is created by a tracking script, we want to lower the priority of the http channel.

--HG--
extra : rebase_source : 7c9d2a545968a50c8ec34a3395132f0d99087058
This commit is contained in:
Kershaw Chang 2017-05-19 01:28:00 -04:00
Родитель 2c432eef31
Коммит 383c9e3e60
4 изменённых файлов: 49 добавлений и 0 удалений

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

@ -301,6 +301,7 @@ bool nsContentUtils::sAnimationsAPIElementAnimateEnabled = false;
bool nsContentUtils::sGetBoxQuadsEnabled = false;
bool nsContentUtils::sSkipCursorMoveForSameValueSet = false;
bool nsContentUtils::sRequestIdleCallbackEnabled = false;
bool nsContentUtils::sLowerNetworkPriority = false;
int32_t nsContentUtils::sPrivacyMaxInnerWidth = 1000;
int32_t nsContentUtils::sPrivacyMaxInnerHeight = 1000;
@ -715,6 +716,9 @@ nsContentUtils::Init()
Preferences::AddBoolVarCache(&sRequestIdleCallbackEnabled,
"dom.requestIdleCallback.enabled", false);
Preferences::AddBoolVarCache(&sLowerNetworkPriority,
"privacy.trackingprotection.lower_network_priority", false);
Element::InitCCCallbacks();
Unused << nsRFPService::GetOrCreate();

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

@ -2953,6 +2953,10 @@ public:
static bool
GetUserIsInteracting();
// Check pref "privacy.trackingprotection.lower_network_priority" to see
// if we want to lower the priority of the channel.
static bool IsLowerNetworkPriority() { return sLowerNetworkPriority; }
private:
static bool InitializeEventTable();
@ -3079,6 +3083,7 @@ private:
static bool sGetBoxQuadsEnabled;
static bool sSkipCursorMoveForSameValueSet;
static bool sRequestIdleCallbackEnabled;
static bool sLowerNetworkPriority;
static uint32_t sCookiesLifetimePolicy;
static uint32_t sCookiesBehavior;

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

@ -46,6 +46,7 @@
#include "nsIAuthPrompt2.h"
#include "nsIOutputStream.h"
#include "nsISupportsPrimitives.h"
#include "nsISupportsPriority.h"
#include "nsIInterfaceRequestorUtils.h"
#include "nsStreamUtils.h"
#include "nsThreadUtils.h"
@ -2550,6 +2551,37 @@ XMLHttpRequestMainThread::CreateChannel()
return NS_OK;
}
void
XMLHttpRequestMainThread::MaybeLowerChannelPriority()
{
nsCOMPtr<nsIDocument> doc = GetDocumentIfCurrent();
if (!doc) {
return;
}
AutoJSAPI jsapi;
if (!jsapi.Init(GetOwnerGlobal())) {
return;
}
JSContext* cx = jsapi.cx();
nsAutoCString fileNameString;
if (!nsJSUtils::GetCallingLocation(cx, fileNameString)) {
return;
}
if (!doc->IsScriptTracking(fileNameString)) {
return;
}
nsCOMPtr<nsISupportsPriority> p = do_QueryInterface(mChannel);
if (!p) {
return;
}
p->SetPriority(nsISupportsPriority::PRIORITY_LOWEST);
}
nsresult
XMLHttpRequestMainThread::InitiateFetch(nsIInputStream* aUploadStream,
int64_t aUploadLength,
@ -2738,6 +2770,12 @@ XMLHttpRequestMainThread::InitiateFetch(nsIInputStream* aUploadStream,
// Make sure to hold a strong reference so that we don't leak the wrapper.
nsCOMPtr<nsIStreamListener> listener = new net::nsStreamListenerWrapper(this);
// Check if this XHR is created from a tracking script.
// If yes, lower the channel's priority.
if (nsContentUtils::IsLowerNetworkPriority()) {
MaybeLowerChannelPriority();
}
// Start reading from the channel
rv = mChannel->AsyncOpen2(listener);
listener = nullptr;

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

@ -314,6 +314,8 @@ private:
// response header is supported.
static bool IsLowercaseResponseHeader();
void MaybeLowerChannelPriority();
public:
virtual void
Send(JSContext* /*aCx*/, ErrorResult& aRv) override