From 0a5870dc7ba4662f8a943ca53ffc26ce219e6652 Mon Sep 17 00:00:00 2001 From: Karl Tomlinson Date: Mon, 11 Oct 2021 05:18:59 +0000 Subject: [PATCH] Bug 1732410 use object instead of subject principal for chrome enumerateDevices() resistFingerprinting override r=jib,emilio This will simplify data structures for pending enumerateDevices() operations. The subject principal was added for https://bugzilla.mozilla.org/show_bug.cgi?id=1372073#c28 I don't see any current chrome callers, but chrome should be able to call this method on a chrome document for real values. Chrome should have no need to call this on a content document. Differential Revision: https://phabricator.services.mozilla.com/D127049 --- dom/media/MediaDevices.cpp | 121 ++++++++++++++++++++--------------------- dom/media/MediaDevices.h | 3 +- dom/media/MediaManager.cpp | 4 +- dom/media/MediaManager.h | 3 +- dom/webidl/MediaDevices.webidl | 2 +- 5 files changed, 63 insertions(+), 70 deletions(-) diff --git a/dom/media/MediaDevices.cpp b/dom/media/MediaDevices.cpp index 247aa9fc38be..c318a740ff32 100644 --- a/dom/media/MediaDevices.cpp +++ b/dom/media/MediaDevices.cpp @@ -121,8 +121,7 @@ already_AddRefed MediaDevices::GetUserMedia( return p.forget(); } -already_AddRefed MediaDevices::EnumerateDevices(CallerType aCallerType, - ErrorResult& aRv) { +already_AddRefed MediaDevices::EnumerateDevices(ErrorResult& aRv) { MOZ_ASSERT(NS_IsMainThread()); nsCOMPtr global = xpc::NativeGlobal(GetWrapper()); nsCOMPtr owner = do_QueryInterface(global); @@ -141,68 +140,64 @@ already_AddRefed MediaDevices::EnumerateDevices(CallerType aCallerType, return nullptr; } RefPtr self(this); - MediaManager::Get() - ->EnumerateDevices(owner, aCallerType) - ->Then( - GetCurrentSerialEventTarget(), __func__, - [this, self, - p](RefPtr&& aDevices) { - nsPIDOMWindowInner* window = GetWindowIfCurrent(); - if (!window) { - return; // Leave Promise pending after navigation by design. - } - auto windowId = window->WindowID(); - nsTArray> infos; - bool allowLabel = - aDevices->Length() == 0 || - MediaManager::Get()->IsActivelyCapturingOrHasAPermission( - windowId); - nsTHashSet exposedMicrophoneGroupIds; - for (auto& device : *aDevices) { - nsString label; - MOZ_ASSERT(device->mKind < MediaDeviceKind::EndGuard_); - switch (device->mKind) { - case MediaDeviceKind::Audioinput: - if (mCanExposeMicrophoneInfo) { - exposedMicrophoneGroupIds.Insert(device->mGroupID); - } - [[fallthrough]]; - case MediaDeviceKind::Videoinput: - // Include name only if page currently has a gUM stream - // active or persistent permissions (audio or video) have - // been granted. See bug 1528042 for using - // mCanExposeMicrophoneInfo. - if (allowLabel || - Preferences::GetBool( - "media.navigator.permission.disabled", false)) { - label = device->mName; - } - break; - case MediaDeviceKind::Audiooutput: - if (!mExplicitlyGrantedAudioOutputIds.Contains(device->mID) && - // Assumes aDevices order has microphones before speakers. - !exposedMicrophoneGroupIds.Contains(device->mGroupID)) { - continue; - } - label = device->mName; - break; - case MediaDeviceKind::EndGuard_: - break; - // Avoid `default:` so that `-Wswitch` catches missing - // enumerators at compile time. + MediaManager::Get()->EnumerateDevices(owner)->Then( + GetCurrentSerialEventTarget(), __func__, + [this, self, p](RefPtr&& aDevices) { + nsPIDOMWindowInner* window = GetWindowIfCurrent(); + if (!window) { + return; // Leave Promise pending after navigation by design. + } + auto windowId = window->WindowID(); + nsTArray> infos; + bool allowLabel = + aDevices->Length() == 0 || + MediaManager::Get()->IsActivelyCapturingOrHasAPermission(windowId); + nsTHashSet exposedMicrophoneGroupIds; + for (auto& device : *aDevices) { + nsString label; + MOZ_ASSERT(device->mKind < MediaDeviceKind::EndGuard_); + switch (device->mKind) { + case MediaDeviceKind::Audioinput: + if (mCanExposeMicrophoneInfo) { + exposedMicrophoneGroupIds.Insert(device->mGroupID); } - infos.AppendElement(MakeRefPtr( - device->mID, device->mKind, label, device->mGroupID)); - } - p->MaybeResolve(std::move(infos)); - }, - [this, self, p](const RefPtr& error) { - nsPIDOMWindowInner* window = GetWindowIfCurrent(); - if (!window) { - return; // Leave Promise pending after navigation by design. - } - error->Reject(p); - }); + [[fallthrough]]; + case MediaDeviceKind::Videoinput: + // Include name only if page currently has a gUM stream + // active or persistent permissions (audio or video) have + // been granted. See bug 1528042 for using + // mCanExposeMicrophoneInfo. + if (allowLabel || + Preferences::GetBool("media.navigator.permission.disabled", + false)) { + label = device->mName; + } + break; + case MediaDeviceKind::Audiooutput: + if (!mExplicitlyGrantedAudioOutputIds.Contains(device->mID) && + // Assumes aDevices order has microphones before speakers. + !exposedMicrophoneGroupIds.Contains(device->mGroupID)) { + continue; + } + label = device->mName; + break; + case MediaDeviceKind::EndGuard_: + break; + // Avoid `default:` so that `-Wswitch` catches missing + // enumerators at compile time. + } + infos.AppendElement(MakeRefPtr( + device->mID, device->mKind, label, device->mGroupID)); + } + p->MaybeResolve(std::move(infos)); + }, + [this, self, p](const RefPtr& error) { + nsPIDOMWindowInner* window = GetWindowIfCurrent(); + if (!window) { + return; // Leave Promise pending after navigation by design. + } + error->Reject(p); + }); return p.forget(); } diff --git a/dom/media/MediaDevices.h b/dom/media/MediaDevices.h index 683647393391..3ce1f77ce1cd 100644 --- a/dom/media/MediaDevices.h +++ b/dom/media/MediaDevices.h @@ -58,8 +58,7 @@ class MediaDevices final : public DOMEventTargetHelper { const MediaStreamConstraints& aConstraints, CallerType aCallerType, ErrorResult& aRv); - already_AddRefed EnumerateDevices(CallerType aCallerType, - ErrorResult& aRv); + already_AddRefed EnumerateDevices(ErrorResult& aRv); already_AddRefed GetDisplayMedia( const DisplayMediaStreamConstraints& aConstraints, CallerType aCallerType, diff --git a/dom/media/MediaManager.cpp b/dom/media/MediaManager.cpp index be32bf72c8f1..4bd188776660 100644 --- a/dom/media/MediaManager.cpp +++ b/dom/media/MediaManager.cpp @@ -3066,7 +3066,7 @@ RefPtr MediaManager::EnumerateDevicesImpl( } RefPtr MediaManager::EnumerateDevices( - nsPIDOMWindowInner* aWindow, CallerType aCallerType) { + nsPIDOMWindowInner* aWindow) { MOZ_ASSERT(NS_IsMainThread()); if (sHasShutdown) { return DeviceSetPromise::CreateAndReject( @@ -3103,7 +3103,7 @@ RefPtr MediaManager::EnumerateDevices( return DeviceSetPromise::CreateAndResolve(devices, __func__); } - bool resistFingerprinting = nsContentUtils::ResistFingerprinting(aCallerType); + bool resistFingerprinting = nsContentUtils::ShouldResistFingerprinting(doc); // In order of precedence: resist fingerprinting > loopback > fake pref if (resistFingerprinting) { videoEnumerationType = DeviceEnumerationType::Fake; diff --git a/dom/media/MediaManager.h b/dom/media/MediaManager.h index 3b250261a6ba..2ea764811bbd 100644 --- a/dom/media/MediaManager.h +++ b/dom/media/MediaManager.h @@ -232,8 +232,7 @@ class MediaManager final : public nsIMediaManagerService, const dom::MediaStreamConstraints& aConstraints, dom::CallerType aCallerType); - RefPtr EnumerateDevices(nsPIDOMWindowInner* aWindow, - dom::CallerType aCallerType); + RefPtr EnumerateDevices(nsPIDOMWindowInner* aWindow); enum class DeviceEnumerationType : uint8_t { Normal, // Enumeration should not return loopback or fake devices diff --git a/dom/webidl/MediaDevices.webidl b/dom/webidl/MediaDevices.webidl index af56c1efba39..df18df94c465 100644 --- a/dom/webidl/MediaDevices.webidl +++ b/dom/webidl/MediaDevices.webidl @@ -17,7 +17,7 @@ interface MediaDevices : EventTarget { attribute EventHandler ondevicechange; MediaTrackSupportedConstraints getSupportedConstraints(); - [Throws, NeedsCallerType, UseCounter] + [Throws, UseCounter] Promise> enumerateDevices(); [Throws, NeedsCallerType, UseCounter] -- 2.11.4.GIT