Bug 1372073 - Spoof MediaStreamTrack. draft
authorChung-Sheng Fu <cfu@mozilla.com>
Fri, 13 Oct 2017 11:57:25 +0800
changeset 696990 58cc1c803cde15f7fbe7a9106916dd83c1eacdfa
parent 696989 2904213f878b38c7269c3b1e0170d259c9656b4a
child 696991 8ce8c703070ac55cf51e3a2a95315b4878f8af11
push id88850
push userbmo:cfu@mozilla.com
push dateMon, 13 Nov 2017 06:48:26 +0000
bugs1372073
milestone59.0a1
Bug 1372073 - Spoof MediaStreamTrack. MozReview-Commit-ID: 71UOGrJ9cgm
dom/html/HTMLMediaElement.cpp
dom/media/AudioStreamTrack.cpp
dom/media/AudioStreamTrack.h
dom/media/MediaStreamTrack.cpp
dom/media/MediaStreamTrack.h
dom/media/VideoStreamTrack.cpp
dom/media/VideoStreamTrack.h
dom/webidl/MediaStreamTrack.webidl
--- a/dom/html/HTMLMediaElement.cpp
+++ b/dom/html/HTMLMediaElement.cpp
@@ -5338,29 +5338,29 @@ void HTMLMediaElement::EndSrcMediaStream
 }
 
 static already_AddRefed<AudioTrack>
 CreateAudioTrack(AudioStreamTrack* aStreamTrack)
 {
   nsAutoString id;
   nsAutoString label;
   aStreamTrack->GetId(id);
-  aStreamTrack->GetLabel(label);
+  aStreamTrack->GetLabel(label, CallerType::System);
 
   return MediaTrackList::CreateAudioTrack(id, NS_LITERAL_STRING("main"),
                                           label, EmptyString(), true);
 }
 
 static already_AddRefed<VideoTrack>
 CreateVideoTrack(VideoStreamTrack* aStreamTrack)
 {
   nsAutoString id;
   nsAutoString label;
   aStreamTrack->GetId(id);
-  aStreamTrack->GetLabel(label);
+  aStreamTrack->GetLabel(label, CallerType::System);
 
   return MediaTrackList::CreateVideoTrack(id, NS_LITERAL_STRING("main"),
                                           label, EmptyString(),
                                           aStreamTrack);
 }
 
 void
 HTMLMediaElement::NotifyMediaStreamTrackAdded(const RefPtr<MediaStreamTrack>& aTrack)
--- a/dom/media/AudioStreamTrack.cpp
+++ b/dom/media/AudioStreamTrack.cpp
@@ -1,20 +1,32 @@
 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-*/
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "AudioStreamTrack.h"
 
+#include "nsContentUtils.h"
+
 #include "mozilla/dom/AudioStreamTrackBinding.h"
 
 namespace mozilla {
 namespace dom {
 
 JSObject*
 AudioStreamTrack::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
 {
   return AudioStreamTrackBinding::Wrap(aCx, this, aGivenProto);
 }
 
+void
+AudioStreamTrack::GetLabel(nsAString& aLabel, CallerType aCallerType)
+{
+  if (nsContentUtils::ResistFingerprinting(aCallerType)) {
+    aLabel.AssignLiteral("Internal Microphone");
+    return;
+  }
+  MediaStreamTrack::GetLabel(aLabel, aCallerType);
+}
+
 } // namespace dom
 } // namespace mozilla
--- a/dom/media/AudioStreamTrack.h
+++ b/dom/media/AudioStreamTrack.h
@@ -24,16 +24,18 @@ public:
 
   AudioStreamTrack* AsAudioStreamTrack() override { return this; }
 
   const AudioStreamTrack* AsAudioStreamTrack() const override { return this; }
 
   // WebIDL
   void GetKind(nsAString& aKind) override { aKind.AssignLiteral("audio"); }
 
+  void GetLabel(nsAString& aLabel, CallerType aCallerType) override;
+
 protected:
   already_AddRefed<MediaStreamTrack> CloneInternal(DOMMediaStream* aOwningStream,
                                                    TrackID aTrackID) override
   {
     return do_AddRef(new AudioStreamTrack(aOwningStream,
                                           aTrackID,
                                           mInputTrackID,
                                           mSource,
--- a/dom/media/MediaStreamTrack.cpp
+++ b/dom/media/MediaStreamTrack.cpp
@@ -254,19 +254,28 @@ MediaStreamTrack::Stop()
 
 void
 MediaStreamTrack::GetConstraints(dom::MediaTrackConstraints& aResult)
 {
   aResult = mConstraints;
 }
 
 void
-MediaStreamTrack::GetSettings(dom::MediaTrackSettings& aResult)
+MediaStreamTrack::GetSettings(dom::MediaTrackSettings& aResult, CallerType aCallerType)
 {
   GetSource().GetSettings(aResult);
+
+  // Spoof values when privacy.resistFingerprinting is true.
+  if (!nsContentUtils::ResistFingerprinting(aCallerType)) {
+    return;
+  }
+  if (aResult.mFacingMode.WasPassed()) {
+    aResult.mFacingMode.Value().Assign(NS_ConvertASCIItoUTF16(
+        VideoFacingModeEnumValues::strings[uint8_t(VideoFacingModeEnum::User)].value));
+  }
 }
 
 already_AddRefed<Promise>
 MediaStreamTrack::ApplyConstraints(const MediaTrackConstraints& aConstraints,
                                    CallerType aCallerType,
                                    ErrorResult &aRv)
 {
   if (MOZ_LOG_TEST(gMediaStreamTrackLog, LogLevel::Info)) {
--- a/dom/media/MediaStreamTrack.h
+++ b/dom/media/MediaStreamTrack.h
@@ -10,16 +10,17 @@
 #include "PrincipalChangeObserver.h"
 #include "StreamTracks.h"
 #include "mozilla/CORSMode.h"
 #include "mozilla/DOMEventTargetHelper.h"
 #include "mozilla/dom/MediaStreamTrackBinding.h"
 #include "mozilla/dom/MediaTrackSettingsBinding.h"
 #include "mozilla/media/MediaUtils.h"
 #include "mozilla/WeakPtr.h"
+#include "nsContentUtils.h"
 #include "nsError.h"
 #include "nsID.h"
 #include "nsIPrincipal.h"
 
 namespace mozilla {
 
 class DOMMediaStream;
 class MediaEnginePhotoCallback;
@@ -275,22 +276,22 @@ public:
   virtual VideoStreamTrack* AsVideoStreamTrack() { return nullptr; }
 
   virtual const AudioStreamTrack* AsAudioStreamTrack() const { return nullptr; }
   virtual const VideoStreamTrack* AsVideoStreamTrack() const { return nullptr; }
 
   // WebIDL
   virtual void GetKind(nsAString& aKind) = 0;
   void GetId(nsAString& aID) const;
-  void GetLabel(nsAString& aLabel) { GetSource().GetLabel(aLabel); }
+  virtual void GetLabel(nsAString& aLabel, CallerType /* aCallerType */) { GetSource().GetLabel(aLabel); }
   bool Enabled() { return mEnabled; }
   void SetEnabled(bool aEnabled);
   void Stop();
   void GetConstraints(dom::MediaTrackConstraints& aResult);
-  void GetSettings(dom::MediaTrackSettings& aResult);
+  void GetSettings(dom::MediaTrackSettings& aResult, CallerType aCallerType);
 
   already_AddRefed<Promise>
   ApplyConstraints(const dom::MediaTrackConstraints& aConstraints,
                    CallerType aCallerType, ErrorResult &aRv);
   already_AddRefed<MediaStreamTrack> Clone();
   MediaStreamTrackState ReadyState() { return mReadyState; }
 
   IMPL_EVENT_HANDLER(ended)
--- a/dom/media/VideoStreamTrack.cpp
+++ b/dom/media/VideoStreamTrack.cpp
@@ -2,16 +2,17 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "VideoStreamTrack.h"
 
 #include "MediaStreamVideoSink.h"
 #include "MediaStreamGraph.h"
+#include "nsContentUtils.h"
 
 #include "mozilla/dom/VideoStreamTrackBinding.h"
 
 namespace mozilla {
 namespace dom {
 
 JSObject*
 VideoStreamTrack::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
@@ -26,10 +27,20 @@ VideoStreamTrack::AddVideoOutput(MediaSt
 }
 
 void
 VideoStreamTrack::RemoveVideoOutput(MediaStreamVideoSink* aSink)
 {
   GetOwnedStream()->RemoveVideoOutput(aSink, mTrackID);
 }
 
+void
+VideoStreamTrack::GetLabel(nsAString& aLabel, CallerType aCallerType)
+{
+  if (nsContentUtils::ResistFingerprinting(aCallerType)) {
+    aLabel.AssignLiteral("Internal Camera");
+    return;
+  }
+  MediaStreamTrack::GetLabel(aLabel, aCallerType);
+}
+
 } // namespace dom
 } // namespace mozilla
--- a/dom/media/VideoStreamTrack.h
+++ b/dom/media/VideoStreamTrack.h
@@ -30,16 +30,18 @@ public:
   const VideoStreamTrack* AsVideoStreamTrack() const override { return this; }
 
   void AddVideoOutput(MediaStreamVideoSink* aSink);
   void RemoveVideoOutput(MediaStreamVideoSink* aSink);
 
   // WebIDL
   void GetKind(nsAString& aKind) override { aKind.AssignLiteral("video"); }
 
+  void GetLabel(nsAString& aLabel, CallerType aCallerType) override;
+
 protected:
   already_AddRefed<MediaStreamTrack> CloneInternal(DOMMediaStream* aOwningStream,
                                                    TrackID aTrackID) override
   {
     return do_AddRef(new VideoStreamTrack(aOwningStream,
                                           aTrackID,
                                           mInputTrackID,
                                           mSource,
--- a/dom/webidl/MediaStreamTrack.webidl
+++ b/dom/webidl/MediaStreamTrack.webidl
@@ -72,27 +72,29 @@ enum MediaStreamTrackState {
     "live",
     "ended"
 };
 
 [Exposed=Window]
 interface MediaStreamTrack : EventTarget {
     readonly    attribute DOMString             kind;
     readonly    attribute DOMString             id;
+    [NeedsCallerType]
     readonly    attribute DOMString             label;
                 attribute boolean               enabled;
 //  readonly    attribute boolean               muted;
 //              attribute EventHandler          onmute;
 //              attribute EventHandler          onunmute;
 //  readonly    attribute boolean               _readonly;
 //  readonly    attribute boolean               remote;
     readonly    attribute MediaStreamTrackState readyState;
                 attribute EventHandler          onended;
     MediaStreamTrack       clone ();
     void                   stop ();
 //  MediaTrackCapabilities getCapabilities ();
     MediaTrackConstraints  getConstraints ();
+    [NeedsCallerType]
     MediaTrackSettings     getSettings ();
 
     [Throws, NeedsCallerType]
     Promise<void>          applyConstraints (optional MediaTrackConstraints constraints);
 //              attribute EventHandler          onoverconstrained;
 };