From c6533bef506dd0369268176a812a733aedcc372a Mon Sep 17 00:00:00 2001 From: Eugen Sawin Date: Tue, 14 Feb 2017 15:53:21 +0100 Subject: [PATCH] Bug 1322576 - [1.2] Add tracking protection attribute to nsILoadContext to allow for overriding of the global preference setting for individual DocShells. r=smaug --- docshell/base/LoadContext.cpp | 22 +++++----- docshell/base/LoadContext.h | 6 +++ docshell/base/SerializedLoadContext.cpp | 2 + docshell/base/SerializedLoadContext.h | 4 ++ docshell/base/nsDocShell.cpp | 40 +++++++++++++++---- docshell/base/nsDocShell.h | 2 +- docshell/base/nsIDocShell.idl | 5 +++ docshell/base/nsILoadContext.idl | 28 ++++++------- dom/ipc/TabParent.cpp | 9 ++++- .../prefetch/OfflineCacheUpdateParent.cpp | 8 +++- 10 files changed, 90 insertions(+), 36 deletions(-) diff --git a/docshell/base/LoadContext.cpp b/docshell/base/LoadContext.cpp index 7eba5ae58c8eb..c45926960ed92 100644 --- a/docshell/base/LoadContext.cpp +++ b/docshell/base/LoadContext.cpp @@ -46,6 +46,7 @@ LoadContext::LoadContext(nsIPrincipal* aPrincipal, , mNestedFrameId(0) , mIsContent(true) , mUseRemoteTabs(false) + , mUseTrackingProtection(false) #ifdef DEBUG , mIsNotNull(true) #endif @@ -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)); } //----------------------------------------------------------------------------- @@ -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 //----------------------------------------------------------------------------- diff --git a/docshell/base/LoadContext.h b/docshell/base/LoadContext.h index 4b71a8041e66a..0b05899ab9346 100644 --- a/docshell/base/LoadContext.h +++ b/docshell/base/LoadContext.h @@ -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) @@ -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) @@ -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) @@ -91,6 +95,7 @@ class LoadContext final , mNestedFrameId(0) , mIsContent(false) , mUseRemoteTabs(false) + , mUseTrackingProtection(false) , mOriginAttributes(aAttrs) #ifdef DEBUG , mIsNotNull(true) @@ -110,6 +115,7 @@ class LoadContext final uint64_t mNestedFrameId; bool mIsContent; bool mUseRemoteTabs; + bool mUseTrackingProtection; OriginAttributes mOriginAttributes; #ifdef DEBUG bool mIsNotNull; diff --git a/docshell/base/SerializedLoadContext.cpp b/docshell/base/SerializedLoadContext.cpp index b8e3eb92981d6..501a417a5125a 100644 --- a/docshell/base/SerializedLoadContext.cpp +++ b/docshell/base/SerializedLoadContext.cpp @@ -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"); } @@ -71,6 +72,7 @@ SerializedLoadContext::Init(nsILoadContext* aLoadContext) // we won't be GetInterfaced to nsILoadContext mIsContent = true; mUseRemoteTabs = false; + mUseTrackingProtection = false; } } diff --git a/docshell/base/SerializedLoadContext.h b/docshell/base/SerializedLoadContext.h index 234a17932d25a..143f6a28e5f90 100644 --- a/docshell/base/SerializedLoadContext.h +++ b/docshell/base/SerializedLoadContext.h @@ -32,6 +32,7 @@ class SerializedLoadContext , mIsPrivateBitValid(false) , mIsContent(false) , mUseRemoteTabs(false) + , mUseTrackingProtection(false) { Init(nullptr); } @@ -52,6 +53,7 @@ class SerializedLoadContext bool mIsPrivateBitValid; bool mIsContent; bool mUseRemoteTabs; + bool mUseTrackingProtection; mozilla::OriginAttributes mOriginAttributes; }; @@ -70,6 +72,7 @@ struct ParamTraits WriteParam(aMsg, aParam.mIsContent); WriteParam(aMsg, aParam.mIsPrivateBitValid); WriteParam(aMsg, aParam.mUseRemoteTabs); + WriteParam(aMsg, aParam.mUseTrackingProtection); WriteParam(aMsg, suffix); } @@ -80,6 +83,7 @@ struct ParamTraits !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; } diff --git a/docshell/base/nsDocShell.cpp b/docshell/base/nsDocShell.cpp index b1319c3638be3..e92d54a48fe1e 100644 --- a/docshell/base/nsDocShell.cpp +++ b/docshell/base/nsDocShell.cpp @@ -795,6 +795,7 @@ nsDocShell::nsDocShell() , mIsAppTab(false) , mUseGlobalHistory(false) , mUseRemoteTabs(false) + , mUseTrackingProtection(false) , mDeviceSizeIsPageSize(false) , mWindowDraggingAllowed(false) , mInFrameSwap(false) @@ -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 parent = GetParentDocshell(); + if (parent) { + return parent->GetUseTrackingProtection(aUseTrackingProtection); + } + + return NS_OK; +} + +NS_IMETHODIMP +nsDocShell::SetUseTrackingProtection(bool aUseTrackingProtection) +{ + mUseTrackingProtection = aUseTrackingProtection; return NS_OK; } diff --git a/docshell/base/nsDocShell.h b/docshell/base/nsDocShell.h index d806c20008af4..beaf4dc321f38 100644 --- a/docshell/base/nsDocShell.h +++ b/docshell/base/nsDocShell.h @@ -234,7 +234,6 @@ class nsDocShell final NS_IMETHOD GetUseRemoteTabs(bool*) override; NS_IMETHOD SetRemoteTabs(bool) override; NS_IMETHOD GetOriginAttributes(JS::MutableHandle) override; - NS_IMETHOD IsTrackingProtectionOn(bool*) override; // Restores a cached presentation from history (mLSHE). // This method swaps out the content viewer and simulates loads for @@ -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; diff --git a/docshell/base/nsIDocShell.idl b/docshell/base/nsIDocShell.idl index 25af89525cc22..f457f12a14f0d 100644 --- a/docshell/base/nsIDocShell.idl +++ b/docshell/base/nsIDocShell.idl @@ -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; }; diff --git a/docshell/base/nsILoadContext.idl b/docshell/base/nsILoadContext.idl index 612f8ef288c68..b12b865e48147 100644 --- a/docshell/base/nsILoadContext.idl +++ b/docshell/base/nsILoadContext.idl @@ -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. @@ -93,6 +99,12 @@ interface nsILoadContext : nsISupports GetUseRemoteTabs(&usingRT); return usingRT; } + + bool UseTrackingProtection() { + bool usingTP; + GetUseTrackingProtection(&usingTP); + return usingTP; + } %} /** @@ -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; - } %} }; diff --git a/dom/ipc/TabParent.cpp b/dom/ipc/TabParent.cpp index 8f77041f63ec0..9588d71b20db6 100644 --- a/dom/ipc/TabParent.cpp +++ b/dom/ipc/TabParent.cpp @@ -2681,10 +2681,16 @@ TabParent::GetLoadContext() } else { bool isPrivate = mChromeFlags & nsIWebBrowserChrome::CHROME_PRIVATE_WINDOW; SetPrivateBrowsingAttributes(isPrivate); + bool useTrackingProtection = false; + nsCOMPtr 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; } @@ -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: diff --git a/uriloader/prefetch/OfflineCacheUpdateParent.cpp b/uriloader/prefetch/OfflineCacheUpdateParent.cpp index 9ab8884732e49..9539fa0126981 100644 --- a/uriloader/prefetch/OfflineCacheUpdateParent.cpp +++ b/uriloader/prefetch/OfflineCacheUpdateParent.cpp @@ -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; }