Skip to content

Commit

Permalink
Bug 1322576 - [1.2] Add tracking protection attribute to nsILoadConte…
Browse files Browse the repository at this point in the history
…xt to allow for overriding of the global preference setting for individual DocShells. r=smaug
  • Loading branch information
Eugen Sawin committed Feb 22, 2017
1 parent 66d7152 commit c6533be
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 36 deletions.
22 changes: 13 additions & 9 deletions docshell/base/LoadContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ LoadContext::LoadContext(nsIPrincipal* aPrincipal,
, mNestedFrameId(0)
, mIsContent(true)
, mUseRemoteTabs(false)
, mUseTrackingProtection(false)
#ifdef DEBUG
, mIsNotNull(true)
#endif
Expand All @@ -57,6 +58,7 @@ LoadContext::LoadContext(nsIPrincipal* aPrincipal,

MOZ_ALWAYS_SUCCEEDS(aOptionalBase->GetIsContent(&mIsContent));
MOZ_ALWAYS_SUCCEEDS(aOptionalBase->GetUseRemoteTabs(&mUseRemoteTabs));
MOZ_ALWAYS_SUCCEEDS(aOptionalBase->GetUseTrackingProtection(&mUseTrackingProtection));
}

//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -180,22 +182,24 @@ LoadContext::GetOriginAttributes(JS::MutableHandleValue aAttrs)
}

NS_IMETHODIMP
LoadContext::IsTrackingProtectionOn(bool* aIsTrackingProtectionOn)
LoadContext::GetUseTrackingProtection(bool* aUseTrackingProtection)
{
MOZ_ASSERT(mIsNotNull);

if (Preferences::GetBool("privacy.trackingprotection.enabled", false)) {
*aIsTrackingProtectionOn = true;
} else if ((mOriginAttributes.mPrivateBrowsingId > 0) &&
Preferences::GetBool("privacy.trackingprotection.pbmode.enabled", false)) {
*aIsTrackingProtectionOn = true;
} else {
*aIsTrackingProtectionOn = false;
}
NS_ENSURE_ARG_POINTER(aUseTrackingProtection);

*aUseTrackingProtection = mUseTrackingProtection;
return NS_OK;
}

NS_IMETHODIMP
LoadContext::SetUseTrackingProtection(bool aUseTrackingProtection)
{
MOZ_ASSERT_UNREACHABLE("Should only be set through nsDocShell");

return NS_ERROR_UNEXPECTED;
}

//-----------------------------------------------------------------------------
// LoadContext::nsIInterfaceRequestor
//-----------------------------------------------------------------------------
Expand Down
6 changes: 6 additions & 0 deletions docshell/base/LoadContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class LoadContext final
, mNestedFrameId(0)
, mIsContent(aToCopy.mIsContent)
, mUseRemoteTabs(aToCopy.mUseRemoteTabs)
, mUseTrackingProtection(aToCopy.mUseTrackingProtection)
, mOriginAttributes(aAttrs)
#ifdef DEBUG
, mIsNotNull(aToCopy.mIsNotNull)
Expand All @@ -61,6 +62,7 @@ class LoadContext final
, mNestedFrameId(aNestedFrameId)
, mIsContent(aToCopy.mIsContent)
, mUseRemoteTabs(aToCopy.mUseRemoteTabs)
, mUseTrackingProtection(aToCopy.mUseTrackingProtection)
, mOriginAttributes(aAttrs)
#ifdef DEBUG
, mIsNotNull(aToCopy.mIsNotNull)
Expand All @@ -72,11 +74,13 @@ class LoadContext final
bool aIsContent,
bool aUsePrivateBrowsing,
bool aUseRemoteTabs,
bool aUseTrackingProtection,
const OriginAttributes& aAttrs)
: mTopFrameElement(do_GetWeakReference(aTopFrameElement))
, mNestedFrameId(0)
, mIsContent(aIsContent)
, mUseRemoteTabs(aUseRemoteTabs)
, mUseTrackingProtection(aUseTrackingProtection)
, mOriginAttributes(aAttrs)
#ifdef DEBUG
, mIsNotNull(true)
Expand All @@ -91,6 +95,7 @@ class LoadContext final
, mNestedFrameId(0)
, mIsContent(false)
, mUseRemoteTabs(false)
, mUseTrackingProtection(false)
, mOriginAttributes(aAttrs)
#ifdef DEBUG
, mIsNotNull(true)
Expand All @@ -110,6 +115,7 @@ class LoadContext final
uint64_t mNestedFrameId;
bool mIsContent;
bool mUseRemoteTabs;
bool mUseTrackingProtection;
OriginAttributes mOriginAttributes;
#ifdef DEBUG
bool mIsNotNull;
Expand Down
2 changes: 2 additions & 0 deletions docshell/base/SerializedLoadContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ SerializedLoadContext::Init(nsILoadContext* aLoadContext)
mIsPrivateBitValid = true;
aLoadContext->GetIsContent(&mIsContent);
aLoadContext->GetUseRemoteTabs(&mUseRemoteTabs);
aLoadContext->GetUseTrackingProtection(&mUseTrackingProtection);
if (!aLoadContext->GetOriginAttributes(mOriginAttributes)) {
NS_WARNING("GetOriginAttributes failed");
}
Expand All @@ -71,6 +72,7 @@ SerializedLoadContext::Init(nsILoadContext* aLoadContext)
// we won't be GetInterfaced to nsILoadContext
mIsContent = true;
mUseRemoteTabs = false;
mUseTrackingProtection = false;
}
}

Expand Down
4 changes: 4 additions & 0 deletions docshell/base/SerializedLoadContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class SerializedLoadContext
, mIsPrivateBitValid(false)
, mIsContent(false)
, mUseRemoteTabs(false)
, mUseTrackingProtection(false)
{
Init(nullptr);
}
Expand All @@ -52,6 +53,7 @@ class SerializedLoadContext
bool mIsPrivateBitValid;
bool mIsContent;
bool mUseRemoteTabs;
bool mUseTrackingProtection;
mozilla::OriginAttributes mOriginAttributes;
};

Expand All @@ -70,6 +72,7 @@ struct ParamTraits<SerializedLoadContext>
WriteParam(aMsg, aParam.mIsContent);
WriteParam(aMsg, aParam.mIsPrivateBitValid);
WriteParam(aMsg, aParam.mUseRemoteTabs);
WriteParam(aMsg, aParam.mUseTrackingProtection);
WriteParam(aMsg, suffix);
}

Expand All @@ -80,6 +83,7 @@ struct ParamTraits<SerializedLoadContext>
!ReadParam(aMsg, aIter, &aResult->mIsContent) ||
!ReadParam(aMsg, aIter, &aResult->mIsPrivateBitValid) ||
!ReadParam(aMsg, aIter, &aResult->mUseRemoteTabs) ||
!ReadParam(aMsg, aIter, &aResult->mUseTrackingProtection) ||
!ReadParam(aMsg, aIter, &suffix)) {
return false;
}
Expand Down
40 changes: 32 additions & 8 deletions docshell/base/nsDocShell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,7 @@ nsDocShell::nsDocShell()
, mIsAppTab(false)
, mUseGlobalHistory(false)
, mUseRemoteTabs(false)
, mUseTrackingProtection(false)
, mDeviceSizeIsPageSize(false)
, mWindowDraggingAllowed(false)
, mInFrameSwap(false)
Expand Down Expand Up @@ -13679,17 +13680,40 @@ nsDocShell::GetNestedFrameId(uint64_t* aId)
}

NS_IMETHODIMP
nsDocShell::IsTrackingProtectionOn(bool* aIsTrackingProtectionOn)
nsDocShell::GetUseTrackingProtection(bool* aUseTrackingProtection)
{
if (Preferences::GetBool("privacy.trackingprotection.enabled", false)) {
*aIsTrackingProtectionOn = true;
} else if (UsePrivateBrowsing() &&
Preferences::GetBool("privacy.trackingprotection.pbmode.enabled", false)) {
*aIsTrackingProtectionOn = true;
} else {
*aIsTrackingProtectionOn = false;
*aUseTrackingProtection = false;

static bool sTPEnabled = false;
static bool sTPInPBEnabled = false;
static bool sPrefsInit = false;

if (!sPrefsInit) {
sPrefsInit = true;
Preferences::AddBoolVarCache(&sTPEnabled,
"privacy.trackingprotection.enabled", false);
Preferences::AddBoolVarCache(&sTPInPBEnabled,
"privacy.trackingprotection.pbmode.enabled", false);
}

if (mUseTrackingProtection || sTPEnabled ||
(UsePrivateBrowsing() && sTPInPBEnabled)) {
*aUseTrackingProtection = true;
return NS_OK;
}

RefPtr<nsDocShell> parent = GetParentDocshell();
if (parent) {
return parent->GetUseTrackingProtection(aUseTrackingProtection);
}

return NS_OK;
}

NS_IMETHODIMP
nsDocShell::SetUseTrackingProtection(bool aUseTrackingProtection)
{
mUseTrackingProtection = aUseTrackingProtection;
return NS_OK;
}

Expand Down
2 changes: 1 addition & 1 deletion docshell/base/nsDocShell.h
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,6 @@ class nsDocShell final
NS_IMETHOD GetUseRemoteTabs(bool*) override;
NS_IMETHOD SetRemoteTabs(bool) override;
NS_IMETHOD GetOriginAttributes(JS::MutableHandle<JS::Value>) override;
NS_IMETHOD IsTrackingProtectionOn(bool*) override;

// Restores a cached presentation from history (mLSHE).
// This method swaps out the content viewer and simulates loads for
Expand Down Expand Up @@ -961,6 +960,7 @@ class nsDocShell final
bool mIsAppTab : 1;
bool mUseGlobalHistory : 1;
bool mUseRemoteTabs : 1;
bool mUseTrackingProtection : 1;
bool mDeviceSizeIsPageSize : 1;
bool mWindowDraggingAllowed : 1;
bool mInFrameSwap : 1;
Expand Down
5 changes: 5 additions & 0 deletions docshell/base/nsIDocShell.idl
Original file line number Diff line number Diff line change
Expand Up @@ -1131,4 +1131,9 @@ interface nsIDocShell : nsIDocShellTreeItem
* header, and has not seen the initiating load yet.
*/
[infallible] readonly attribute boolean awaitingLargeAlloc;

/**
* Attribute that determines whether tracking protection is enabled.
*/
attribute boolean useTrackingProtection;
};
28 changes: 12 additions & 16 deletions docshell/base/nsILoadContext.idl
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ interface nsILoadContext : nsISupports
*/
readonly attribute boolean useRemoteTabs;

/*
* Attribute that determines if tracking protection should be used. May not be
* changed after a document has been loaded in this context.
*/
attribute boolean useTrackingProtection;

%{C++
/**
* De-XPCOMed getter to make call-sites cleaner.
Expand All @@ -93,6 +99,12 @@ interface nsILoadContext : nsISupports
GetUseRemoteTabs(&usingRT);
return usingRT;
}

bool UseTrackingProtection() {
bool usingTP;
GetUseTrackingProtection(&usingTP);
return usingTP;
}
%}

/**
Expand Down Expand Up @@ -129,21 +141,5 @@ interface nsILoadContext : nsISupports
*/
bool GetOriginAttributes(mozilla::OriginAttributes& aAttrs);
#endif
%}

/**
* Returns true if tracking protection is enabled for the load context.
*/
boolean IsTrackingProtectionOn();

%{C++
/**
* De-XPCOMed getter to make call-sites cleaner.
*/
bool UseTrackingProtection() {
bool usingTP;
IsTrackingProtectionOn(&usingTP);
return usingTP;
}
%}
};
9 changes: 8 additions & 1 deletion dom/ipc/TabParent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2681,10 +2681,16 @@ TabParent::GetLoadContext()
} else {
bool isPrivate = mChromeFlags & nsIWebBrowserChrome::CHROME_PRIVATE_WINDOW;
SetPrivateBrowsingAttributes(isPrivate);
bool useTrackingProtection = false;
nsCOMPtr<nsIDocShell> docShell = mFrameElement->OwnerDoc()->GetDocShell();
if (docShell) {
docShell->GetUseTrackingProtection(&useTrackingProtection);
}
loadContext = new LoadContext(GetOwnerElement(),
true /* aIsContent */,
isPrivate,
mChromeFlags & nsIWebBrowserChrome::CHROME_REMOTE_WINDOW,
useTrackingProtection,
OriginAttributesRef());
mLoadContext = loadContext;
}
Expand Down Expand Up @@ -2994,7 +3000,8 @@ class FakeChannel final : public nsIChannel,
NS_IMETHOD GetOriginAttributes(JS::MutableHandleValue) NO_IMPL
NS_IMETHOD GetUseRemoteTabs(bool*) NO_IMPL
NS_IMETHOD SetRemoteTabs(bool) NO_IMPL
NS_IMETHOD IsTrackingProtectionOn(bool*) NO_IMPL
NS_IMETHOD GetUseTrackingProtection(bool*) NO_IMPL
NS_IMETHOD SetUseTrackingProtection(bool) NO_IMPL
#undef NO_IMPL

protected:
Expand Down
8 changes: 7 additions & 1 deletion uriloader/prefetch/OfflineCacheUpdateParent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,13 @@ OfflineCacheUpdateParent::GetOriginAttributes(JS::MutableHandleValue aAttrs)
}

NS_IMETHODIMP
OfflineCacheUpdateParent::IsTrackingProtectionOn(bool* aIsTrackingProtectionOn)
OfflineCacheUpdateParent::GetUseTrackingProtection(bool *aUseTrackingProtection)
{
return NS_ERROR_NOT_IMPLEMENTED;
}

NS_IMETHODIMP
OfflineCacheUpdateParent::SetUseTrackingProtection(bool aUseTrackingProtection)
{
return NS_ERROR_NOT_IMPLEMENTED;
}
Expand Down

0 comments on commit c6533be

Please sign in to comment.