summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/webrtc/browser_devices_get_user_media_in_xorigin_frame_chain.js
blob: ad398994f0b7315973e1847e1d5e5f12eb6a0649 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/* 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/. */
const permissionError =
  "error: NotAllowedError: The request is not allowed " +
  "by the user agent or the platform in the current context.";

const PromptResult = {
  ALLOW: "allow",
  DENY: "deny",
  PROMPT: "prompt",
};

const Perms = Services.perms;

function expectObserverCalledAncestor(aTopic, browsingContext) {
  if (!gMultiProcessBrowser) {
    return expectObserverCalledInProcess(aTopic);
  }

  return BrowserTestUtils.contentTopicObserved(browsingContext, aTopic);
}

function enableObserverVerificationAncestor(browsingContext) {
  // Skip these checks in single process mode as it isn't worth implementing it.
  if (!gMultiProcessBrowser) {
    return Promise.resolve();
  }

  return BrowserTestUtils.startObservingTopics(browsingContext, observerTopics);
}

function disableObserverVerificationAncestor(browsingContextt) {
  if (!gMultiProcessBrowser) {
    return Promise.resolve();
  }

  return BrowserTestUtils.stopObservingTopics(
    browsingContextt,
    observerTopics
  ).catch(reason => {
    ok(false, "Failed " + reason);
  });
}

function promiseRequestDeviceAncestor(
  aRequestAudio,
  aRequestVideo,
  aType,
  aBrowser,
  aBadDevice = false
) {
  info("requesting devices");
  return SpecialPowers.spawn(
    aBrowser,
    [{ aRequestAudio, aRequestVideo, aType, aBadDevice }],
    async function (args) {
      let global =
        content.wrappedJSObject.document.getElementById("frame4").contentWindow;
      global.requestDevice(
        args.aRequestAudio,
        args.aRequestVideo,
        args.aType,
        args.aBadDevice
      );
    }
  );
}

async function closeStreamAncestor(browser) {
  let observerPromises = [];
  observerPromises.push(
    expectObserverCalledAncestor("recording-device-events", browser)
  );
  observerPromises.push(
    expectObserverCalledAncestor("recording-window-ended", browser)
  );

  info("closing the stream");
  await SpecialPowers.spawn(browser, [], async () => {
    let global =
      content.wrappedJSObject.document.getElementById("frame4").contentWindow;
    global.closeStream();
  });

  await Promise.all(observerPromises);

  await assertWebRTCIndicatorStatus(null);
}

var gTests = [
  {
    desc: "getUserMedia use persistent permissions from first party if third party is explicitly trusted",
    skipObserverVerification: true,
    run: async function checkPermissionsAncestorChain() {
      async function checkPermission(aPermission, aRequestType, aExpect) {
        info(
          `Test persistent permission ${aPermission} type ${aRequestType} expect ${aExpect}`
        );
        const uri = gBrowser.selectedBrowser.documentURI;
        // Persistent allow/deny for first party uri
        PermissionTestUtils.add(uri, aRequestType, aPermission);

        let audio = aRequestType == "microphone";
        let video = aRequestType == "camera";
        const screen = aRequestType == "screen" ? "screen" : undefined;
        if (screen) {
          audio = false;
          video = true;
        }
        const iframeAncestor = await SpecialPowers.spawn(
          gBrowser.selectedBrowser,
          [],
          () => {
            return content.document.getElementById("frameAncestor")
              .browsingContext;
          }
        );

        if (aExpect == PromptResult.PROMPT) {
          // Check that we get a prompt.
          const observerPromise = expectObserverCalledAncestor(
            "getUserMedia:request",
            iframeAncestor
          );
          const promise = promisePopupNotificationShown("webRTC-shareDevices");

          await promiseRequestDeviceAncestor(
            audio,
            video,
            screen,
            iframeAncestor
          );
          await promise;
          await observerPromise;

          // Check the label of the notification should be the first party
          is(
            PopupNotifications.getNotification("webRTC-shareDevices").options
              .name,
            uri.host,
            "Use first party's origin"
          );
          const observerPromise1 = expectObserverCalledAncestor(
            "getUserMedia:response:deny",
            iframeAncestor
          );
          const observerPromise2 = expectObserverCalledAncestor(
            "recording-window-ended",
            iframeAncestor
          );
          // Deny the request to cleanup...
          activateSecondaryAction(kActionDeny);
          await observerPromise1;
          await observerPromise2;
          let browser = gBrowser.selectedBrowser;
          SitePermissions.removeFromPrincipal(null, aRequestType, browser);
        } else if (aExpect == PromptResult.ALLOW) {
          const observerPromise = expectObserverCalledAncestor(
            "getUserMedia:request",
            iframeAncestor
          );
          const observerPromise1 = expectObserverCalledAncestor(
            "getUserMedia:response:allow",
            iframeAncestor
          );
          const observerPromise2 = expectObserverCalledAncestor(
            "recording-device-events",
            iframeAncestor
          );
          await promiseRequestDeviceAncestor(
            audio,
            video,
            screen,
            iframeAncestor
          );
          await observerPromise;

          await promiseNoPopupNotification("webRTC-shareDevices");
          await observerPromise1;
          await observerPromise2;

          let expected = {};
          if (audio) {
            expected.audio = audio;
          }
          if (video) {
            expected.video = video;
          }

          Assert.deepEqual(
            await getMediaCaptureState(),
            expected,
            "expected " + Object.keys(expected).join(" and ") + " to be shared"
          );

          await closeStreamAncestor(iframeAncestor);
        } else if (aExpect == PromptResult.DENY) {
          const observerPromise = expectObserverCalledAncestor(
            "recording-window-ended",
            iframeAncestor
          );
          await promiseRequestDeviceAncestor(
            audio,
            video,
            screen,
            iframeAncestor
          );
          await observerPromise;
        }

        PermissionTestUtils.remove(uri, aRequestType);
      }

      await checkPermission(Perms.PROMPT_ACTION, "camera", PromptResult.PROMPT);
      await checkPermission(Perms.DENY_ACTION, "camera", PromptResult.DENY);
      await checkPermission(Perms.ALLOW_ACTION, "camera", PromptResult.ALLOW);

      await checkPermission(
        Perms.PROMPT_ACTION,
        "microphone",
        PromptResult.PROMPT
      );
      await checkPermission(Perms.DENY_ACTION, "microphone", PromptResult.DENY);
      await checkPermission(
        Perms.ALLOW_ACTION,
        "microphone",
        PromptResult.ALLOW
      );

      await checkPermission(Perms.PROMPT_ACTION, "screen", PromptResult.PROMPT);
      await checkPermission(Perms.DENY_ACTION, "screen", PromptResult.DENY);
      // Always prompt screen sharing
      await checkPermission(Perms.ALLOW_ACTION, "screen", PromptResult.PROMPT);
    },
  },
];

add_task(async function test() {
  await SpecialPowers.pushPrefEnv({
    set: [
      ["permissions.delegation.enabled", true],
      ["dom.security.featurePolicy.header.enabled", true],
      ["dom.security.featurePolicy.webidl.enabled", true],
    ],
  });

  await runTests(gTests, {
    relativeURI: "get_user_media_in_xorigin_frame_ancestor.html",
  });
});