<html>
<head>
<title>Conditional Focus Test - Capturing Page</title>
<link rel="icon" href="data:," />
<script>
"use strict";
let mediaStreamKeepAlive; // Keeps the capture alive.
// Calls getDisplayMedia().
// Calling with |busyWaitMs| set to a positive value leads to hogging
// the CPU for that long *before* calling setFocusBehavior().
// Then:
// * If (focusCallValue == ''), don't call setFocusBehavior().
// * If (focusCallValue != ''), call setFocusBehavior(focusCallValue).
// * If |onCorrectMicrotask|, setFocusBehavior() is called on the
// microtask on which getDisplayMedia's Promise was resolved.
// Otherwise, it is scheduled to be called in a later task.
async function captureOtherTab(busyWaitMs, focusCallValue, onCorrectMicrotask) {
try {
const controller = new CaptureController();
const mediaStream = await navigator.mediaDevices.getDisplayMedia({ controller });
if (onCorrectMicrotask) {
return handleCaptureSuccess(mediaStream, busyWaitMs, focusCallValue, controller);
} else {
return new Promise(resolve => {
setTimeout(resolve, 0);
}).then(() => handleCaptureSuccess(mediaStream, busyWaitMs, focusCallValue, controller));
}
} catch (error) {
return "capture-failure";
}
}
function busyWait(durationMs) {
const targetTime = new Date().getTime() + durationMs;
while (new Date().getTime() < targetTime) {
// Busy-wait.
}
}
function handleCaptureSuccess(mediaStream, busyWaitMs, focusCallValue, controller) {
mediaStreamKeepAlive = mediaStream;
if (busyWaitMs > 0) {
busyWait(busyWaitMs);
}
if (focusCallValue) {
try {
controller.setFocusBehavior(focusCallValue);
} catch (error) {
return `${error}`;
}
}
// Allow the microtask to fire before unblocking the test.
return new Promise(resolve => {
setTimeout(resolve);
}).then(() => "capture-success");
}
// Calls setFocusBehavior(focusCallValueBeforeCapture) before
// getDisplayMedia({ controller }). Then
// if (focusCallValueAfterCapture != '') call
// setFocusBehavior(focusCallValueAfterCapture) on the microtask on which
// getDisplayMedia's Promise was resolved.
async function callSetFocusBehaviorBeforeCapture(focusCallValueBeforeCapture, focusCallValueAfterCapture) {
try {
const controller = new CaptureController();
controller.setFocusBehavior(focusCallValueBeforeCapture);
await navigator.mediaDevices.getDisplayMedia({ controller });
if (focusCallValueAfterCapture) {
controller.setFocusBehavior(focusCallValueAfterCapture);
}
return "capture-success";
} catch (error) {
return `${error}`;
}
}
</script>
</head>
<body>
<h1>Conditional Focus Test - Capturing Page</h1>
</body>
</html>