Bug 1799435 - Make RFP checks faster. r=smaug

Differential Revision: https://phabricator.services.mozilla.com/D161447
This commit is contained in:
Emilio Cobos Álvarez
2022-11-08 02:36:25 +00:00
parent 21a047b345
commit 12e596995e
22 changed files with 185 additions and 134 deletions

View File

@@ -1413,6 +1413,12 @@ Document::Document(const char* aContentType)
mIsRunningExecCommand(false),
mSetCompleteAfterDOMContentLoaded(false),
mDidHitCompleteSheetCache(false),
mUseCountersInitialized(false),
mShouldReportUseCounters(false),
mShouldSendPageUseCounters(false),
mUserHasInteracted(false),
mHasUserInteractionTimerScheduled(false),
mShouldResistFingerprinting(false),
mPendingFullscreenRequests(0),
mXMLDeclarationBits(0),
mOnloadBlockCount(0),
@@ -1441,11 +1447,6 @@ Document::Document(const char* aContentType)
mBFCacheEntry(nullptr),
mInSyncOperationCount(0),
mBlockDOMContentLoaded(0),
mUseCountersInitialized(false),
mShouldReportUseCounters(false),
mShouldSendPageUseCounters(false),
mUserHasInteracted(false),
mHasUserInteractionTimerScheduled(false),
mStackRefCnt(0),
mUpdateNestLevel(0),
mHttpsOnlyStatus(nsILoadInfo::HTTPS_ONLY_UNINITIALIZED),
@@ -2833,6 +2834,7 @@ void Document::Reset(nsIChannel* aChannel, nsILoadGroup* aLoadGroup) {
}
mChannel = aChannel;
RecomputeResistFingerprinting();
}
void Document::DisconnectNodeTree() {
@@ -3454,6 +3456,7 @@ nsresult Document::StartDocumentLoad(const char* aCommand, nsIChannel* aChannel,
RetrieveRelevantHeaders(aChannel);
mChannel = aChannel;
RecomputeResistFingerprinting();
nsCOMPtr<nsIInputStreamChannel> inStrmChan = do_QueryInterface(mChannel);
if (inStrmChan) {
bool isSrcdocChannel;
@@ -11867,6 +11870,7 @@ nsresult Document::CloneDocHelper(Document* clone) const {
uri = Document::GetDocumentURI();
}
clone->mChannel = channel;
clone->mShouldResistFingerprinting = mShouldResistFingerprinting;
if (uri) {
clone->ResetToURI(uri, loadGroup, NodePrincipal(), mPartitionedPrincipal);
}
@@ -15687,6 +15691,12 @@ void Document::SendPageUseCounters() {
wgc->SendAccumulatePageUseCounters(counters);
}
void Document::RecomputeResistFingerprinting() {
mShouldResistFingerprinting =
!nsContentUtils::IsChromeDoc(this) &&
nsContentUtils::ShouldResistFingerprinting(mChannel);
}
WindowContext* Document::GetWindowContextForPageUseCounters() const {
if (mDisplayDocument) {
// If we are a resource document, then go through it to find the
@@ -17842,8 +17852,7 @@ ColorScheme Document::DefaultColorScheme() const {
}
ColorScheme Document::PreferredColorScheme(IgnoreRFP aIgnoreRFP) const {
if (aIgnoreRFP == IgnoreRFP::No &&
nsContentUtils::ShouldResistFingerprinting(this)) {
if (ShouldResistFingerprinting() && aIgnoreRFP == IgnoreRFP::No) {
return ColorScheme::Light;
}

View File

@@ -4087,6 +4087,12 @@ class Document : public nsINode,
bool DidHitCompleteSheetCache() const { return mDidHitCompleteSheetCache; }
bool ShouldResistFingerprinting() const {
return mShouldResistFingerprinting;
}
void RecomputeResistFingerprinting();
protected:
// Returns the WindowContext for the document that we will contribute
// page use counters to.
@@ -4827,6 +4833,29 @@ class Document : public nsINode,
// Set the true if a completed cached stylesheet was created for the document.
bool mDidHitCompleteSheetCache : 1;
// Whether we have initialized mShouldReportUseCounters and
// mShouldSendPageUseCounters, and sent any needed message to the parent
// process to indicate that use counter data will be sent at some later point.
bool mUseCountersInitialized : 1;
// Whether this document should report use counters.
bool mShouldReportUseCounters : 1;
// Whether this document should send page use counters. Set to true after
// we've called SendExpectPageUseCounters on the top-level WindowGlobal.
bool mShouldSendPageUseCounters : 1;
// Whether the user has interacted with the document or not:
bool mUserHasInteracted : 1;
// We constantly update the user-interaction anti-tracking permission at any
// user-interaction using a timer. This boolean value is set to true when this
// timer is scheduled.
bool mHasUserInteractionTimerScheduled : 1;
// Whether we should resist fingerprinting.
bool mShouldResistFingerprinting : 1;
uint8_t mPendingFullscreenRequests;
uint8_t mXMLDeclarationBits;
@@ -5033,26 +5062,6 @@ class Document : public nsINode,
// The CSS property use counters.
UniquePtr<StyleUseCounters> mStyleUseCounters;
// Whether we have initialized mShouldReportUseCounters and
// mShouldSendPageUseCounters, and sent any needed message to the parent
// process to indicate that use counter data will be sent at some later point.
bool mUseCountersInitialized : 1;
// Whether this document should report use counters.
bool mShouldReportUseCounters : 1;
// Whether this document should send page use counters. Set to true after
// we've called SendExpectPageUseCounters on the top-level WindowGlobal.
bool mShouldSendPageUseCounters : 1;
// Whether the user has interacted with the document or not:
bool mUserHasInteracted;
// We constantly update the user-interaction anti-tracking permission at any
// user-interaction using a timer. This boolean value is set to true when this
// timer is scheduled.
bool mHasUserInteractionTimerScheduled;
TimeStamp mPageUnloadingEventTimeStamp;
RefPtr<DocGroup> mDocGroup;

View File

@@ -278,7 +278,7 @@ nsDOMAttributeMap* Element::Attributes() {
}
void Element::SetPointerCapture(int32_t aPointerId, ErrorResult& aError) {
if (nsContentUtils::ShouldResistFingerprinting(GetComposedDoc()) &&
if (OwnerDoc()->ShouldResistFingerprinting() &&
aPointerId != PointerEventHandler::GetSpoofedPointerIdForRFP()) {
aError.ThrowNotFoundError("Invalid pointer id");
return;
@@ -307,7 +307,7 @@ void Element::SetPointerCapture(int32_t aPointerId, ErrorResult& aError) {
}
void Element::ReleasePointerCapture(int32_t aPointerId, ErrorResult& aError) {
if (nsContentUtils::ShouldResistFingerprinting(GetComposedDoc()) &&
if (OwnerDoc()->ShouldResistFingerprinting() &&
aPointerId != PointerEventHandler::GetSpoofedPointerIdForRFP()) {
aError.ThrowNotFoundError("Invalid pointer id");
return;

View File

@@ -650,7 +650,7 @@ uint64_t Navigator::HardwareConcurrency() {
}
return rts->ClampedHardwareConcurrency(
nsContentUtils::ShouldResistFingerprinting(mWindow->GetExtantDoc()));
nsGlobalWindowInner::Cast(mWindow)->ShouldResistFingerprinting());
}
namespace {
@@ -1564,6 +1564,11 @@ void Navigator::ValidateShareData(const ShareData& aData, ErrorResult& aRv) {
}
}
static bool ShouldResistFingerprinting(const Document* aDoc) {
return aDoc ? aDoc->ShouldResistFingerprinting()
: nsContentUtils::ShouldResistFingerprinting("Fallback");
}
already_AddRefed<LegacyMozTCPSocket> Navigator::MozTCPSocket() {
RefPtr<LegacyMozTCPSocket> socket = new LegacyMozTCPSocket(GetWindow());
return socket.forget();
@@ -1808,9 +1813,9 @@ network::Connection* Navigator::GetConnection(ErrorResult& aRv) {
aRv.Throw(NS_ERROR_UNEXPECTED);
return nullptr;
}
nsCOMPtr<Document> doc = mWindow->GetExtantDoc();
mConnection = network::Connection::CreateForWindow(
mWindow, nsContentUtils::ShouldResistFingerprinting(doc));
mWindow,
nsGlobalWindowInner::Cast(mWindow)->ShouldResistFingerprinting());
}
return mConnection;
@@ -1918,7 +1923,7 @@ nsresult Navigator::GetPlatform(nsAString& aPlatform, Document* aCallerDoc,
if (aUsePrefOverriddenValue) {
// If fingerprinting resistance is on, we will spoof this value. See
// nsRFPService.h for details about spoofed values.
if (nsContentUtils::ShouldResistFingerprinting(aCallerDoc)) {
if (ShouldResistFingerprinting(aCallerDoc)) {
aPlatform.AssignLiteral(SPOOFED_PLATFORM);
return NS_OK;
}
@@ -1961,7 +1966,7 @@ nsresult Navigator::GetAppVersion(nsAString& aAppVersion, Document* aCallerDoc,
if (aUsePrefOverriddenValue) {
// If fingerprinting resistance is on, we will spoof this value. See
// nsRFPService.h for details about spoofed values.
if (nsContentUtils::ShouldResistFingerprinting(aCallerDoc)) {
if (ShouldResistFingerprinting(aCallerDoc)) {
aAppVersion.AssignLiteral(SPOOFED_APPVERSION);
return NS_OK;
}
@@ -2005,7 +2010,7 @@ void Navigator::AppName(nsAString& aAppName, Document* aCallerDoc,
if (aUsePrefOverriddenValue) {
// If fingerprinting resistance is on, we will spoof this value. See
// nsRFPService.h for details about spoofed values.
if (nsContentUtils::ShouldResistFingerprinting(aCallerDoc)) {
if (ShouldResistFingerprinting(aCallerDoc)) {
aAppName.AssignLiteral(SPOOFED_APPNAME);
return;
}
@@ -2050,7 +2055,7 @@ nsresult Navigator::GetUserAgent(nsPIDOMWindowInner* aWindow,
bool shouldResistFingerprinting =
aShouldResistFingerprinting.isSome()
? aShouldResistFingerprinting.value()
: nsContentUtils::ShouldResistFingerprinting(aCallerDoc);
: ShouldResistFingerprinting(aCallerDoc);
// We will skip the override and pass to httpHandler to get spoofed userAgent
// when 'privacy.resistFingerprinting' is true.

View File

@@ -97,6 +97,7 @@
#include "mozilla/MacroForEach.h"
#include "mozilla/ManualNAC.h"
#include "mozilla/Maybe.h"
#include "mozilla/MediaFeatureChange.h"
#include "mozilla/MouseEvents.h"
#include "mozilla/NotNull.h"
#include "mozilla/NullPrincipal.h"
@@ -731,6 +732,33 @@ class nsContentUtils::UserInteractionObserver final
~UserInteractionObserver() = default;
};
static constexpr nsLiteralCString kRfpPrefs[] = {
"privacy.resistFingerprinting"_ns,
"privacy.resistFingerprinting.testGranularityMask"_ns,
};
static void RecomputeResistFingerprintingAllDocs(const char*, void*) {
AutoTArray<RefPtr<BrowsingContextGroup>, 5> bcGroups;
BrowsingContextGroup::GetAllGroups(bcGroups);
for (auto& bcGroup : bcGroups) {
AutoTArray<DocGroup*, 5> docGroups;
bcGroup->GetDocGroups(docGroups);
for (auto* docGroup : docGroups) {
for (Document* doc : *docGroup) {
const bool old = doc->ShouldResistFingerprinting();
doc->RecomputeResistFingerprinting();
if (old != doc->ShouldResistFingerprinting()) {
if (auto* pc = doc->GetPresContext()) {
pc->MediaFeatureValuesChanged(
{MediaFeatureChangeReason::PreferenceChange},
MediaFeatureChangePropagation::JustThisDocument);
}
}
}
}
}
}
// static
nsresult nsContentUtils::Init() {
if (sInitialized) {
@@ -804,6 +832,10 @@ nsresult nsContentUtils::Init() {
uio->Init();
uio.forget(&sUserInteractionObserver);
for (const auto& pref : kRfpPrefs) {
Preferences::RegisterCallback(RecomputeResistFingerprintingAllDocs, pref);
}
sInitialized = true;
return NS_OK;
@@ -1932,6 +1964,10 @@ void nsContentUtils::Shutdown() {
NS_RELEASE(sUserInteractionObserver);
}
for (const auto& pref : kRfpPrefs) {
Preferences::UnregisterCallback(RecomputeResistFingerprintingAllDocs, pref);
}
TextControlState::Shutdown();
nsMappedAttributes::Shutdown();
}
@@ -2179,27 +2215,18 @@ bool nsContentUtils::ShouldResistFingerprinting(const char* aJustification) {
bool nsContentUtils::ShouldResistFingerprinting(nsIDocShell* aDocShell) {
if (!aDocShell) {
MOZ_LOG(nsContentUtils::ResistFingerprintingLog(), LogLevel::Info,
("Called nsContentUtils::ShouldResistFingerprinting(const "
"nsIDocShell* aDocShell) with NULL docshell"));
("Called nsContentUtils::ShouldResistFingerprinting(nsIDocShell*) "
"with NULL docshell"));
return ShouldResistFingerprinting();
}
return ShouldResistFingerprinting(aDocShell->GetDocument());
}
// --------------------------------------------------------------------
/* static */
bool nsContentUtils::ShouldResistFingerprinting(const Document* aDoc) {
if (!aDoc) {
Document* doc = aDocShell->GetDocument();
if (!doc) {
MOZ_LOG(nsContentUtils::ResistFingerprintingLog(), LogLevel::Info,
("Called nsContentUtils::ShouldResistFingerprinting(const "
"Document* aDoc) with NULL document"));
("Called nsContentUtils::ShouldResistFingerprinting(nsIDocShell*) "
"with NULL doc"));
return ShouldResistFingerprinting();
}
bool isChrome = nsContentUtils::IsChromeDoc(aDoc);
if (isChrome) {
return false;
}
return ShouldResistFingerprinting(aDoc->GetChannel());
return doc->ShouldResistFingerprinting();
}
// ----------------------------------------------------------------------

View File

@@ -350,7 +350,6 @@ class nsContentUtils {
static bool ShouldResistFingerprinting(nsIGlobalObject* aGlobalObject);
static bool ShouldResistFingerprinting(nsIDocShell* aDocShell);
// These functions are the new, nuanced functions
static bool ShouldResistFingerprinting(const Document* aDoc);
static bool ShouldResistFingerprinting(nsIChannel* aChannel);
static bool ShouldResistFingerprinting(nsILoadInfo* aPrincipal);
// These functions are labeled as dangerous because they will do the wrong

View File

@@ -1625,7 +1625,7 @@ bool nsGlobalWindowInner::IsBlackForCC(bool aTracingNeeded) {
bool nsGlobalWindowInner::ShouldResistFingerprinting() const {
if (mDoc) {
return nsContentUtils::ShouldResistFingerprinting(mDoc);
return mDoc->ShouldResistFingerprinting();
}
return nsIScriptGlobalObject::ShouldResistFingerprinting();
}
@@ -4982,10 +4982,8 @@ Storage* nsGlobalWindowInner::GetLocalStorage(ErrorResult& aError) {
if (mDoc) {
cookieJarSettings = mDoc->CookieJarSettings();
} else {
bool shouldResistFingerprinting =
nsContentUtils::ShouldResistFingerprinting(this->GetExtantDoc());
cookieJarSettings =
net::CookieJarSettings::GetBlockingAll(shouldResistFingerprinting);
net::CookieJarSettings::GetBlockingAll(ShouldResistFingerprinting());
}
// Note that this behavior is observable: if we grant storage permission to a
@@ -6584,7 +6582,7 @@ void nsGlobalWindowInner::DisableDeviceSensor(uint32_t aType) {
#if defined(MOZ_WIDGET_ANDROID)
void nsGlobalWindowInner::EnableOrientationChangeListener() {
if (!nsContentUtils::ShouldResistFingerprinting(GetDocShell())) {
if (!ShouldResistFingerprinting()) {
mHasOrientationChangeListeners = true;
mOrientationAngle = Orientation(CallerType::System);
}
@@ -6911,7 +6909,7 @@ void nsGlobalWindowInner::GetGamepads(nsTArray<RefPtr<Gamepad>>& aGamepads) {
// navigator.getGamepads() always returns an empty array when
// privacy.resistFingerprinting is true.
if (nsContentUtils::ShouldResistFingerprinting(GetDocShell())) {
if (ShouldResistFingerprinting()) {
return;
}
@@ -7559,7 +7557,7 @@ void nsGlobalWindowInner::SetReplaceableWindowCoord(
return;
}
if (nsContentUtils::ShouldResistFingerprinting(GetDocShell())) {
if (ShouldResistFingerprinting()) {
bool innerWidthSpecified = false;
bool innerHeightSpecified = false;
bool outerWidthSpecified = false;

View File

@@ -1668,7 +1668,7 @@ bool nsGlobalWindowOuter::IsBlackForCC(bool aTracingNeeded) {
bool nsGlobalWindowOuter::ShouldResistFingerprinting() const {
if (mDoc) {
return nsContentUtils::ShouldResistFingerprinting(mDoc);
return mDoc->ShouldResistFingerprinting();
}
return nsIScriptGlobalObject::ShouldResistFingerprinting();
}

View File

@@ -8,6 +8,7 @@
#include "mozilla/dom/MimeTypeArrayBinding.h"
#include "mozilla/dom/MimeTypeBinding.h"
#include "nsGlobalWindowInner.h"
#include "nsPluginArray.h"
#include "mozilla/StaticPrefs_pdfjs.h"
#include "nsContentUtils.h"
@@ -81,7 +82,7 @@ void nsMimeTypeArray::GetSupportedNames(nsTArray<nsString>& retval) {
bool nsMimeTypeArray::ForceNoPlugins() {
return StaticPrefs::pdfjs_disabled() &&
!nsContentUtils::ShouldResistFingerprinting(mWindow->GetExtantDoc());
!nsGlobalWindowInner::Cast(mWindow)->ShouldResistFingerprinting();
}
NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(nsMimeType, AddRef)

View File

@@ -12,6 +12,7 @@
#include "nsMimeTypeArray.h"
#include "nsPIDOMWindow.h"
#include "nsGlobalWindowInner.h"
#include "nsContentUtils.h"
using namespace mozilla;
@@ -99,7 +100,7 @@ void nsPluginArray::GetSupportedNames(nsTArray<nsString>& aRetval) {
bool nsPluginArray::ForceNoPlugins() {
return StaticPrefs::pdfjs_disabled() &&
!nsContentUtils::ShouldResistFingerprinting(mWindow->GetExtantDoc());
!nsGlobalWindowInner::Cast(mWindow)->ShouldResistFingerprinting();
}
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsPluginArray)

View File

@@ -53,12 +53,20 @@ namespace mozilla::CanvasUtils {
bool IsImageExtractionAllowed(dom::Document* aDocument, JSContext* aCx,
Maybe<nsIPrincipal*> aPrincipal) {
// Do the rest of the checks only if privacy.resistFingerprinting is on.
if (!nsContentUtils::ShouldResistFingerprinting(aDocument)) {
if (!nsContentUtils::ShouldResistFingerprinting()) {
return true;
}
if (!aDocument) {
return false;
}
if (!aDocument->ShouldResistFingerprinting()) {
return true;
}
// Don't proceed if we don't have a document or JavaScript context.
if (!aDocument || !aCx || !aPrincipal) {
if (!aCx || !aPrincipal) {
return false;
}

View File

@@ -5653,8 +5653,8 @@ void ClientWebGLContext::GetSupportedProfilesASTC(
bool ClientWebGLContext::ShouldResistFingerprinting() const {
if (mCanvasElement) {
// If we're constructed from a canvas element
return nsContentUtils::ShouldResistFingerprinting(GetOwnerDoc());
// If we're constructed from a canvas element.
return mCanvasElement->OwnerDoc()->ShouldResistFingerprinting();
}
if (mOffscreenCanvas) {
// If we're constructed from an offscreen canvas

View File

@@ -360,23 +360,22 @@ void KeyboardEvent::InitKeyboardEventJS(
bool KeyboardEvent::ShouldResistFingerprinting(CallerType aCallerType) {
// There are five situations we don't need to spoof this keyboard event.
// 1. This event is initialized by scripts.
// 2. This event is from Numpad.
// 3. This event is in the system group.
// 4. The caller type is system.
// 5. The pref privcy.resistFingerprinting' is false, we fast return here
// 1. The pref privcy.resistFingerprinting' is false, we fast return here
// since we don't need to do any QI of following codes.
if (mInitializedByJS || aCallerType == CallerType::System ||
mEvent->mFlags.mInSystemGroup ||
!nsContentUtils::ShouldResistFingerprinting() ||
// 2. This event is initialized by scripts.
// 3. This event is from Numpad.
// 4. This event is in the system group.
// 5. The caller type is system.
if (!nsContentUtils::ShouldResistFingerprinting() || mInitializedByJS ||
aCallerType == CallerType::System || mEvent->mFlags.mInSystemGroup ||
mEvent->AsKeyboardEvent()->mLocation ==
KeyboardEvent_Binding::DOM_KEY_LOCATION_NUMPAD) {
return false;
}
nsCOMPtr<Document> doc = GetDocument();
return nsContentUtils::ShouldResistFingerprinting(doc);
// We've checked the pref above, so use true as fallback if doc is null.
return doc ? doc->ShouldResistFingerprinting() : true;
}
bool KeyboardEvent::GetSpoofedModifierStates(const Modifiers aModifierKey,

View File

@@ -278,23 +278,23 @@ void PointerEvent::GetPredictedEvents(
bool PointerEvent::ShouldResistFingerprinting(CallerType aCallerType) {
// There are four situations we don't need to spoof this pointer event.
// 1. This event is generated by scripts.
// 2. This event is a mouse pointer event.
// 3. The caller type is system.
// 4. The pref privcy.resistFingerprinting' is false, we fast return here
// 1. The pref privcy.resistFingerprinting' is false, we fast return here
// since we don't need to do any QI of following codes.
// 2. This event is generated by scripts.
// 3. This event is a mouse pointer event.
// 4. The caller type is system.
// We don't need to check for the system group since pointer events won't be
// dispatched to the system group.
if (!mEvent->IsTrusted() || aCallerType == CallerType::System ||
!nsContentUtils::ShouldResistFingerprinting() ||
if (!nsContentUtils::ShouldResistFingerprinting() || !mEvent->IsTrusted() ||
aCallerType == CallerType::System ||
mEvent->AsPointerEvent()->mInputSource ==
MouseEvent_Binding::MOZ_SOURCE_MOUSE) {
return false;
}
// Pref is checked above, so use true as fallback.
nsCOMPtr<Document> doc = GetDocument();
return nsContentUtils::ShouldResistFingerprinting(doc);
return doc ? doc->ShouldResistFingerprinting() : true;
}
} // namespace mozilla::dom

View File

@@ -144,8 +144,7 @@ void GamepadManager::AddListener(nsGlobalWindowInner* aWindow) {
}
}
if (!mEnabled || mShuttingDown ||
nsContentUtils::ShouldResistFingerprinting(aWindow->GetExtantDoc())) {
if (!mEnabled || mShuttingDown || aWindow->ShouldResistFingerprinting()) {
return;
}
@@ -280,8 +279,7 @@ void GamepadManager::NewConnectionEvent(GamepadHandle aHandle,
// Do not fire gamepadconnected and gamepaddisconnected events when
// privacy.resistFingerprinting is true.
if (nsContentUtils::ShouldResistFingerprinting(
listeners[i]->GetExtantDoc())) {
if (listeners[i]->ShouldResistFingerprinting()) {
continue;
}
@@ -314,8 +312,7 @@ void GamepadManager::NewConnectionEvent(GamepadHandle aHandle,
// Do not fire gamepadconnected and gamepaddisconnected events when
// privacy.resistFingerprinting is true.
if (nsContentUtils::ShouldResistFingerprinting(
listeners[i]->GetExtantDoc())) {
if (listeners[i]->ShouldResistFingerprinting()) {
continue;
}
@@ -350,8 +347,7 @@ void GamepadManager::FireConnectionEvent(EventTarget* aTarget,
void GamepadManager::SyncGamepadState(GamepadHandle aHandle,
nsGlobalWindowInner* aWindow,
Gamepad* aGamepad) {
if (mShuttingDown || !mEnabled ||
nsContentUtils::ShouldResistFingerprinting(aWindow->GetExtantDoc())) {
if (mShuttingDown || !mEnabled || aWindow->ShouldResistFingerprinting()) {
return;
}

View File

@@ -259,7 +259,7 @@ uint32_t HTMLVideoElement::MozParsedFrames() const {
return 0;
}
if (nsContentUtils::ShouldResistFingerprinting(OwnerDoc())) {
if (OwnerDoc()->ShouldResistFingerprinting()) {
return nsRFPService::GetSpoofedTotalFrames(TotalPlayTime());
}
@@ -272,7 +272,7 @@ uint32_t HTMLVideoElement::MozDecodedFrames() const {
return 0;
}
if (nsContentUtils::ShouldResistFingerprinting(OwnerDoc())) {
if (OwnerDoc()->ShouldResistFingerprinting()) {
return nsRFPService::GetSpoofedTotalFrames(TotalPlayTime());
}
@@ -285,7 +285,7 @@ uint32_t HTMLVideoElement::MozPresentedFrames() {
return 0;
}
if (nsContentUtils::ShouldResistFingerprinting(OwnerDoc())) {
if (OwnerDoc()->ShouldResistFingerprinting()) {
return nsRFPService::GetSpoofedPresentedFrames(TotalPlayTime(),
VideoWidth(), VideoHeight());
}
@@ -299,7 +299,7 @@ uint32_t HTMLVideoElement::MozPaintedFrames() {
return 0;
}
if (nsContentUtils::ShouldResistFingerprinting(OwnerDoc())) {
if (OwnerDoc()->ShouldResistFingerprinting()) {
return nsRFPService::GetSpoofedPresentedFrames(TotalPlayTime(),
VideoWidth(), VideoHeight());
}
@@ -311,8 +311,7 @@ uint32_t HTMLVideoElement::MozPaintedFrames() {
double HTMLVideoElement::MozFrameDelay() {
MOZ_ASSERT(NS_IsMainThread(), "Should be on main thread.");
if (!IsVideoStatsEnabled() ||
nsContentUtils::ShouldResistFingerprinting(OwnerDoc())) {
if (!IsVideoStatsEnabled() || OwnerDoc()->ShouldResistFingerprinting()) {
return 0.0;
}
@@ -349,7 +348,7 @@ HTMLVideoElement::GetVideoPlaybackQuality() {
}
if (mDecoder) {
if (nsContentUtils::ShouldResistFingerprinting(OwnerDoc())) {
if (OwnerDoc()->ShouldResistFingerprinting()) {
totalFrames = nsRFPService::GetSpoofedTotalFrames(TotalPlayTime());
droppedFrames = nsRFPService::GetSpoofedDroppedFrames(
TotalPlayTime(), VideoWidth(), VideoHeight());

View File

@@ -34,12 +34,13 @@ MediaError::MediaError(HTMLMediaElement* aParent, uint16_t aCode,
void MediaError::GetMessage(nsAString& aResult) const {
// When fingerprinting resistance is enabled, only messages in this list
// can be returned to content script.
// FIXME: An unordered_set seems overkill for this.
static const std::unordered_set<std::string> whitelist = {
"404: Not Found"
// TODO
};
bool shouldBlank = (whitelist.find(mMessage.get()) == whitelist.end());
const bool shouldBlank = whitelist.find(mMessage.get()) == whitelist.end();
if (shouldBlank) {
// Print a warning message to JavaScript console to alert developers of
@@ -65,13 +66,12 @@ void MediaError::GetMessage(nsAString& aResult) const {
NS_ConvertASCIItoUTF16(message), nsIScriptError::warningFlag,
"MediaError"_ns, ownerDoc);
}
}
if (!nsContentUtils::IsCallerChrome() &&
nsContentUtils::ShouldResistFingerprinting(mParent->OwnerDoc()) &&
shouldBlank) {
aResult.Truncate();
return;
if (!nsContentUtils::IsCallerChrome() &&
ownerDoc->ShouldResistFingerprinting()) {
aResult.Truncate();
return;
}
}
CopyUTF8toUTF16(mMessage, aResult);

View File

@@ -24,6 +24,7 @@
#include "nsINamed.h"
#include "nsIScriptGlobalObject.h"
#include "nsPIDOMWindow.h"
#include "nsGlobalWindowInner.h"
#include "nsQueryObject.h"
namespace mozilla::dom {
@@ -226,8 +227,7 @@ RefPtr<MediaDeviceSetRefCnt> MediaDevices::FilterExposedDevices(
!Preferences::GetBool("media.setsinkid.enabled") ||
!FeaturePolicyUtils::IsFeatureAllowed(doc, u"speaker-selection"_ns);
bool resistFingerprinting = nsContentUtils::ShouldResistFingerprinting(doc);
if (resistFingerprinting) {
if (doc->ShouldResistFingerprinting()) {
RefPtr fakeEngine = new MediaEngineFake();
fakeEngine->EnumerateDevices(MediaSourceEnum::Microphone,
MediaSinkEnum::Other, exposed);
@@ -731,8 +731,7 @@ void MediaDevices::OnDeviceChange() {
return;
}
Document* doc = window->GetExtantDoc();
if (nsContentUtils::ShouldResistFingerprinting(doc)) {
if (nsGlobalWindowInner::Cast(window)->ShouldResistFingerprinting()) {
return;
}
}

View File

@@ -10,6 +10,7 @@
#include "nsContentUtils.h"
#include "nsDeviceSensors.h"
#include "nsGlobalWindowInner.h"
#include "nsPIDOMWindow.h"
#include "nsIScriptObjectPrincipal.h"
#include "mozilla/Preferences.h"
@@ -509,7 +510,8 @@ bool nsDeviceSensors::IsSensorAllowedByPref(uint32_t aType,
// checks "device.sensors.motion.enabled" pref
if (!StaticPrefs::device_sensors_motion_enabled()) {
return false;
} else if (doc) {
}
if (doc) {
doc->WarnOnceAbout(DeprecatedOperations::eMotionEvent);
}
break;
@@ -519,7 +521,8 @@ bool nsDeviceSensors::IsSensorAllowedByPref(uint32_t aType,
// checks "device.sensors.orientation.enabled" pref
if (!StaticPrefs::device_sensors_orientation_enabled()) {
return false;
} else if (doc) {
}
if (doc) {
doc->WarnOnceAbout(DeprecatedOperations::eOrientationEvent);
}
break;
@@ -527,7 +530,8 @@ bool nsDeviceSensors::IsSensorAllowedByPref(uint32_t aType,
// checks "device.sensors.proximity.enabled" pref
if (!StaticPrefs::device_sensors_proximity_enabled()) {
return false;
} else if (doc) {
}
if (doc) {
doc->WarnOnceAbout(DeprecatedOperations::eProximityEvent, true);
}
break;
@@ -535,7 +539,8 @@ bool nsDeviceSensors::IsSensorAllowedByPref(uint32_t aType,
// checks "device.sensors.ambientLight.enabled" pref
if (!StaticPrefs::device_sensors_ambientLight_enabled()) {
return false;
} else if (doc) {
}
if (doc) {
doc->WarnOnceAbout(DeprecatedOperations::eAmbientLightEvent, true);
}
break;
@@ -547,6 +552,5 @@ bool nsDeviceSensors::IsSensorAllowedByPref(uint32_t aType,
if (!window) {
return true;
}
return !nsContentUtils::ShouldResistFingerprinting(doc);
return !nsGlobalWindowInner::Cast(window)->ShouldResistFingerprinting();
}

View File

@@ -2887,7 +2887,7 @@ nsresult WorkerPrivate::GetLoadInfo(JSContext* aCx, nsPIDOMWindowInner* aWindow,
loadInfo.mHasStorageAccessPermissionGranted =
document->HasStorageAccessPermissionGranted();
loadInfo.mShouldResistFingerprinting =
nsContentUtils::ShouldResistFingerprinting(document);
document->ShouldResistFingerprinting();
// This is an hack to deny the storage-access-permission for workers of
// sub-iframes.

View File

@@ -331,7 +331,6 @@ static const char* gExactCallbackPrefs[] = {
"intl.accept_languages",
"layout.css.devPixelsPerPx",
"layout.css.dpi",
"privacy.resistFingerprinting",
"privacy.trackingprotection.enabled",
"ui.use_standins_for_native_colors",
nullptr,
@@ -577,7 +576,6 @@ void nsPresContext::PreferenceChanged(const char* aPrefName) {
// because that pref doesn't just affect the "live" value of the media query;
// it affects whether it is parsed at all.
if (prefName.EqualsLiteral("browser.display.document_color_use") ||
prefName.EqualsLiteral("privacy.resistFingerprinting") ||
prefName.EqualsLiteral("browser.display.foreground_color") ||
prefName.EqualsLiteral("browser.display.background_color")) {
MediaFeatureValuesChanged({MediaFeatureChangeReason::PreferenceChange},

View File

@@ -37,8 +37,8 @@ using mozilla::dom::DisplayMode;
using mozilla::dom::Document;
// A helper for four features below
static nsSize GetSize(const Document* aDocument) {
nsPresContext* pc = aDocument->GetPresContext();
static nsSize GetSize(const Document& aDocument) {
nsPresContext* pc = aDocument.GetPresContext();
// Per spec, return a 0x0 viewport if we're not being rendered. See:
//
@@ -60,20 +60,20 @@ static nsSize GetSize(const Document* aDocument) {
}
// A helper for three features below.
static nsSize GetDeviceSize(const Document* aDocument) {
if (nsContentUtils::ShouldResistFingerprinting(aDocument)) {
static nsSize GetDeviceSize(const Document& aDocument) {
if (aDocument.ShouldResistFingerprinting()) {
return GetSize(aDocument);
}
// Media queries in documents in an RDM pane should use the simulated
// device size.
Maybe<CSSIntSize> deviceSize =
nsGlobalWindowOuter::GetRDMDeviceSize(*aDocument);
nsGlobalWindowOuter::GetRDMDeviceSize(aDocument);
if (deviceSize.isSome()) {
return CSSPixel::ToAppUnits(deviceSize.value());
}
nsPresContext* pc = aDocument->GetPresContext();
nsPresContext* pc = aDocument.GetPresContext();
// NOTE(emilio): We should probably figure out how to return an appropriate
// device size here, though in a multi-screen world that makes no sense
// really.
@@ -124,7 +124,7 @@ static nsDeviceContext* GetDeviceContextFor(const Document* aDocument) {
void Gecko_MediaFeatures_GetDeviceSize(const Document* aDocument,
nscoord* aWidth, nscoord* aHeight) {
nsSize size = GetDeviceSize(aDocument);
nsSize size = GetDeviceSize(*aDocument);
*aWidth = size.width;
*aHeight = size.height;
}
@@ -159,7 +159,7 @@ uint32_t Gecko_MediaFeatures_GetColorDepth(const Document* aDocument) {
// rendered.
uint32_t depth = 24;
if (!nsContentUtils::ShouldResistFingerprinting(aDocument)) {
if (!aDocument->ShouldResistFingerprinting()) {
if (nsDeviceContext* dx = GetDeviceContextFor(aDocument)) {
depth = dx->GetDepth();
}
@@ -185,7 +185,7 @@ float Gecko_MediaFeatures_GetResolution(const Document* aDocument) {
return pc->GetOverrideDPPX();
}
if (nsContentUtils::ShouldResistFingerprinting(aDocument)) {
if (aDocument->ShouldResistFingerprinting()) {
return pc->DeviceContext()->GetFullZoom();
}
// Get the actual device pixel ratio, which also takes zoom into account.
@@ -264,7 +264,7 @@ bool Gecko_MediaFeatures_MatchesPlatform(StylePlatform aPlatform) {
}
bool Gecko_MediaFeatures_PrefersReducedMotion(const Document* aDocument) {
if (nsContentUtils::ShouldResistFingerprinting(aDocument)) {
if (aDocument->ShouldResistFingerprinting()) {
return false;
}
return LookAndFeel::GetInt(LookAndFeel::IntID::PrefersReducedMotion, 0) == 1;
@@ -283,7 +283,7 @@ StylePrefersColorScheme Gecko_MediaFeatures_PrefersColorScheme(
// as a signal.
StylePrefersContrast Gecko_MediaFeatures_PrefersContrast(
const Document* aDocument) {
if (nsContentUtils::ShouldResistFingerprinting(aDocument)) {
if (aDocument->ShouldResistFingerprinting()) {
return StylePrefersContrast::NoPreference;
}
const auto& prefs = PreferenceSheet::PrefsFor(*aDocument);
@@ -313,7 +313,7 @@ StyleDynamicRange Gecko_MediaFeatures_DynamicRange(const Document* aDocument) {
StyleDynamicRange Gecko_MediaFeatures_VideoDynamicRange(
const Document* aDocument) {
if (nsContentUtils::ShouldResistFingerprinting(aDocument)) {
if (aDocument->ShouldResistFingerprinting()) {
return StyleDynamicRange::Standard;
}
// video-dynamic-range: high has 3 requirements:
@@ -354,8 +354,7 @@ static PointerCapabilities GetPointerCapabilities(const Document* aDocument,
#else
PointerCapabilities::Fine | PointerCapabilities::Hover;
#endif
if (nsContentUtils::ShouldResistFingerprinting(aDocument)) {
if (aDocument->ShouldResistFingerprinting()) {
return kDefaultCapabilities;
}