Bug 1315858 - Test ability to detect screensharing sources that are firefox. draft
authorJan-Ivar Bruaroey <jib@mozilla.com>
Mon, 07 Nov 2016 12:20:41 -0500
changeset 435818 7ac5e8d3ffd21a5946c2a9732d81c0c66bf03eaf
parent 435113 a43567a3ee03fdbb8b3fd24536764e2bd97c0a91
child 536385 8875009d868801ed1b7f378de052c05cad11dbf0
push id35128
push userjbruaroey@mozilla.com
push dateWed, 09 Nov 2016 07:43:04 +0000
bugs1315858
milestone52.0a1
Bug 1315858 - Test ability to detect screensharing sources that are firefox. MozReview-Commit-ID: K6Kkdaso7dd
dom/media/MediaManager.cpp
dom/media/tests/mochitest/mochitest.ini
dom/media/tests/mochitest/test_getUserMedia_scarySources.html
--- a/dom/media/MediaManager.cpp
+++ b/dom/media/MediaManager.cpp
@@ -2295,17 +2295,18 @@ if (privileged) {
 
   nsString callID;
   rv = GenerateUUID(callID);
   NS_ENSURE_SUCCESS(rv, rv);
 
   bool fake = c.mFake.WasPassed()? c.mFake.Value() :
       Preferences::GetBool("media.navigator.streams.fake");
 
-  bool askPermission = !privileged &&
+  bool askPermission =
+      (!privileged || Preferences::GetBool("media.navigator.permission.force")) &&
       (!fake || Preferences::GetBool("media.navigator.permission.fake"));
 
   RefPtr<PledgeSourceSet> p = EnumerateDevicesImpl(windowID, videoType,
                                                    audioType, fake);
   p->Then([this, onSuccess, onFailure, windowID, c, listener, askPermission,
            prefs, isHTTPS, callID, origin, isChrome](SourceSet*& aDevices) mutable {
 
     RefPtr<Refcountable<UniquePtr<SourceSet>>> devices(
--- a/dom/media/tests/mochitest/mochitest.ini
+++ b/dom/media/tests/mochitest/mochitest.ini
@@ -68,16 +68,18 @@ skip-if = android_version == '18' # andr
 [test_getUserMedia_mediaElementCapture_video.html]
 skip-if = android_version == '18' # android(Bug 1189784, timeouts on 4.3 emulator)
 [test_getUserMedia_mediaStreamClone.html]
 [test_getUserMedia_mediaStreamConstructors.html]
 [test_getUserMedia_mediaStreamTrackClone.html]
 [test_getUserMedia_playAudioTwice.html]
 [test_getUserMedia_playVideoAudioTwice.html]
 [test_getUserMedia_playVideoTwice.html]
+[test_getUserMedia_scarySources.html]
+skip-if = toolkit == 'android' # no screenshare or windowshare on android
 [test_getUserMedia_spinEventLoop.html]
 [test_getUserMedia_stopAudioStream.html]
 [test_getUserMedia_stopAudioStreamWithFollowupAudio.html]
 [test_getUserMedia_stopVideoAudioStream.html]
 [test_getUserMedia_stopVideoAudioStreamWithFollowupVideoAudio.html]
 [test_getUserMedia_stopVideoStream.html]
 [test_getUserMedia_stopVideoStreamWithFollowupVideo.html]
 [test_getUserMedia_trackCloneCleanup.html]
new file mode 100644
--- /dev/null
+++ b/dom/media/tests/mochitest/test_getUserMedia_scarySources.html
@@ -0,0 +1,66 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+  <script type="application/javascript" src="head.js"></script>
+  <script type="application/javascript" src="mediaStreamPlayback.js"></script>
+</head>
+<body>
+<pre id="test">
+<script type="application/javascript">
+
+createHTML({title: "Detect screensharing sources that are firefox", bug: "1311048"});
+
+netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
+
+const { Services } = SpecialPowers.Cu.import('resource://gre/modules/Services.jsm');
+
+let observe = topic => new Promise(r => Services.obs.addObserver(function o(...args) {
+  Services.obs.removeObserver(o, topic);
+  r(args);
+}, topic, false));
+
+let getDevices = async constraints => {
+  let [{ windowID, innerWindowID, callID }] = await Promise.race([
+    getUserMedia(constraints),
+    observe("getUserMedia:request")
+  ]);
+  let window = Services.wm.getOuterWindowWithId(windowID);
+  let devices = await new Promise((resolve, reject) =>
+      window.navigator.mozGetUserMediaDevices({}, resolve, reject,
+                                              innerWindowID, callID));
+  return devices.map(d => d.QueryInterface(Ci.nsIMediaDevice));
+};
+
+runTest(async () => {
+  try {
+    const isWinXP = navigator.userAgent.indexOf("Windows NT 5.1") != -1;
+    if (IsMacOSX10_6orOlder() || isWinXP) {
+      ok(true, "Screensharing disabled for OSX10.6 and WinXP");
+      return;
+    }
+
+    await pushPrefs(["media.navigator.permission.disabled", true],
+                    ["media.navigator.permission.fake", true],
+                    ["media.navigator.permission.force", true]);
+    let devices = await getDevices({video: { mediaSource: "window" }});
+    ok(devices.length, "Found one or more windows.");
+    devices = devices.filter(d => d.scary);
+    ok(devices.length, "Found one or more scary windows (our own counts).");
+    devices.filter(d => d.name.includes("MochiTest"));
+    ok(devices.length,
+       "Our own window is among the scary: " + devices.map(d => `"${d.name}"`));
+
+    devices = await getDevices({video: { mediaSource: "screen" }});
+    let numScreens = devices.length;
+    ok(numScreens, "Found one or more screens.");
+    devices = devices.filter(d => d.scary);
+    is(devices.length, numScreens, "All screens are scary.");
+  } catch(e) {
+    ok(false, e);
+  }
+});
+
+</script>
+</pre>
+</body>
+</html>