Skip to content

Commit

Permalink
Prerender: Add WPT helper to get target hint from URLSearchParams
Browse files Browse the repository at this point in the history
For code simplicity, this CL adds getTargetHint() to get a target hint
param from URLSearchParams.

Bug: 1501674
Change-Id: I81d3c72d3e42b27937d0b87e7b6a65c9c1c387a4
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5048290
Reviewed-by: Lingqi Chi <lingqi@chromium.org>
Commit-Queue: Hiroki Nakagawa <nhiroki@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1227306}
  • Loading branch information
nhiroki authored and chromium-wpt-export-bot committed Nov 21, 2023
1 parent 242389a commit 3d1200b
Show file tree
Hide file tree
Showing 15 changed files with 32 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@
<audio controls id="beat" src="./bear-av1-opus.mp4" loop></audio>
<script>

const params = new URLSearchParams(location.search);
const target_hint = params.get('target_hint');
const rule_extras = {target_hint};

// The main test page (restriction-audio-setSinkId.https.html) loads the
// initiator page, then the initiator page will prerender itself with the
// `prerendering` parameter.
const params = new URLSearchParams(location.search);
const isPrerendering = params.has('prerendering');

if (!isPrerendering) {
const rule_extras = {'target_hint': getTargetHint()};
loadInitiatorPage(rule_extras);
} else {
const prerenderEventCollector = new PrerenderEventCollector();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@
<script src="/speculation-rules/prerender/resources/deferred-promise-utils.js"></script>
<script>

const params = new URLSearchParams(location.search);
const target_hint = params.get('target_hint');
const rule_extras = {target_hint};

// The main test page (restriction-background-fetch.https.html) loads the
// initiator page, then the initiator page will prerender itself with the
// `prerendering` parameter.
const params = new URLSearchParams(location.search);
const isPrerendering = params.has('prerendering');

if (!isPrerendering) {
const rule_extras = {'target_hint': getTargetHint()};
loadInitiatorPage(rule_extras);
} else {
async function loadPrerenderPage() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@
<script src="/speculation-rules/prerender/resources/deferred-promise-utils.js"></script>
<script>

const params = new URLSearchParams(location.search);
const target_hint = params.get('target_hint');
const rule_extras = {target_hint};

// The main test page (restriction-background-sync.tentative.https.html)
// loads the initiator page, then the initiator page will prerender itself
// with the `prerendering` parameter.
const params = new URLSearchParams(location.search);
const isPrerendering = params.has('prerendering');

if (!isPrerendering) {
const rule_extras = {'target_hint': getTargetHint()};
loadInitiatorPage(rule_extras);
} else {
async function loadPrerenderPage() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
<script src="/speculation-rules/prerender/resources/deferred-promise-utils.js"></script>
<script>

const params = new URLSearchParams(location.search);
const target_hint = params.get('target_hint');
const rule_extras = {target_hint};

// The main test page (restriction-battery-status.html) loads the initiator
// page, then the initiator page will prerender itself with the `prerendering`
// parameter.
const params = new URLSearchParams(location.search);
if (!params.has('prerendering')) {
const rule_extras = {'target_hint': getTargetHint()};
loadInitiatorPage(rule_extras);
} else {
const prerenderEventCollector = new PrerenderEventCollector();
Expand Down
12 changes: 12 additions & 0 deletions speculation-rules/prerender/resources/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,3 +434,15 @@ function failTest(reason, uid) {
bc.postMessage({result: 'FAILED', reason});
bc.close();
}

// Retrieves a target hint from URLSearchParams of the current window and
// returns it. Throw an Error if it doesn't have the valid target hint param.
function getTargetHint() {
const params = new URLSearchParams(window.location.search);
const target_hint = params.get('target_hint');
if (target_hint === null)
throw new Error('window.location does not have a target hint param');
if (target_hint !== '_self' && target_hint !== '_blank')
throw new Error('window.location does not have a valid target hint param');
return target_hint;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@
<script src="/speculation-rules/prerender/resources/deferred-promise-utils.js"></script>
<script>

const params = new URLSearchParams(location.search);
const target_hint = params.get('target_hint');
const rule_extras = {target_hint};

// The main test page (restriction-web-xr-immersive-vr-session.https.html) loads
// the initiator page, then the initiator page will prerender itself with the
// `prerendering` parameter.
const params = new URLSearchParams(location.search);
const isPrerendering = params.has('prerendering');

if (!isPrerendering) {
const rule_extras = {'target_hint': getTargetHint()};
loadInitiatorPage(rule_extras);
} else {
const prerenderEventCollector = new PrerenderEventCollector();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@
<script src="/speculation-rules/prerender/resources/deferred-promise-utils.js"></script>
<script>

const params = new URLSearchParams(location.search);
const target_hint = params.get('target_hint');
const rule_extras = {target_hint};

// The main test page (restriction-web-xr-inline-session.https.html) loads the
// initiator page, then the initiator page will prerender itself with the
// `prerendering` parameter.
const params = new URLSearchParams(location.search);
const isPrerendering = params.has('prerendering');

if (!isPrerendering) {
const rule_extras = {'target_hint': getTargetHint()};
loadInitiatorPage(rule_extras);
} else {
const prerenderEventCollector = new PrerenderEventCollector();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
<script>
setup(() => assertSpeculationRulesIsSupported());

const params = new URLSearchParams(window.location.search);
const target_hint = params.get('target_hint');

promise_test(async t => {
const uid = token();
const bc = new PrerenderChannel('test-channel', uid);
Expand All @@ -31,7 +28,7 @@
});
});

const url = `resources/audio-setSinkId.https.html?sinkId=invalid&uid=${uid}&target_hint=${target_hint}`;
const url = `resources/audio-setSinkId.https.html?sinkId=invalid&uid=${uid}&target_hint=${getTargetHint()}`;
window.open(url, '_blank', 'noopener');

const result = await gotMessage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
<script>
setup(() => assertSpeculationRulesIsSupported());

const params = new URLSearchParams(window.location.search);
const target_hint = params.get('target_hint');

promise_test(async t => {
const uid = token();
const bc = new PrerenderChannel('test-channel', uid);
Expand All @@ -29,7 +26,7 @@
});
});

const url = `resources/audio-setSinkId.https.html?sinkId=default&uid=${uid}&target_hint=${target_hint}`;
const url = `resources/audio-setSinkId.https.html?sinkId=default&uid=${uid}&target_hint=${getTargetHint()}`;
window.open(url, '_blank', 'noopener');

const result = await gotMessage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
<script>
setup(() => assertSpeculationRulesIsSupported());

const params = new URLSearchParams(window.location.search);
const target_hint = params.get('target_hint');

promise_test(async t => {
const uid = token();
const bc = new PrerenderChannel('test-channel', uid);
Expand Down Expand Up @@ -47,7 +44,7 @@
t.add_cleanup(() => registration.unregister());
await wait_for_state(t, registration.installing, 'activated');

const url = `resources/background-fetch.https.html?uid=${uid}&target_hint=${target_hint}`;
const url = `resources/background-fetch.https.html?uid=${uid}&target_hint=${getTargetHint()}`;
window.open(url, '_blank', 'noopener');

const result = await gotMessage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
<script>
setup(() => assertSpeculationRulesIsSupported());

const params = new URLSearchParams(window.location.search);
const target_hint = params.get('target_hint');

promise_test(async t => {
const uid = token();
const bc = new PrerenderChannel('test-channel', uid);
Expand Down Expand Up @@ -47,7 +44,7 @@
t.add_cleanup(() => registration.unregister());
await wait_for_state(t, registration.installing, 'activated');

const url = `resources/background-sync.https.html?uid=${uid}&target_hint=${target_hint}`;
const url = `resources/background-sync.https.html?uid=${uid}&target_hint=${getTargetHint()}`;
window.open(url, '_blank', 'noopener');

const result = await gotMessage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
<script>
setup(() => assertSpeculationRulesIsSupported());

const params = new URLSearchParams(window.location.search);
const target_hint = params.get('target_hint');

promise_test(async t => {
const uid = token();
const bc = new PrerenderChannel('test-channel', uid);
Expand All @@ -27,7 +24,7 @@
once: true
});
});
const url = `resources/battery-status.https.html?uid=${uid}&target_hint=${target_hint}`;
const url = `resources/battery-status.https.html?uid=${uid}&target_hint=${getTargetHint()}`;
window.open(url, '_blank', 'noopener');

const result = await gotMessage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@
<script>
setup(() => assertSpeculationRulesIsSupported());

const params = new URLSearchParams(window.location.search);
const target_hint = params.get('target_hint');
const rule_extras = {target_hint};

promise_test(async t => {
const uid = token();
const bc = new PrerenderChannel('prerender-channel', uid);
Expand All @@ -32,6 +28,7 @@
// Start prerendering a page that attempts to invoke the Screen Capture API.
// This API is activated-gated so it's expected to fail:
// https://wicg.github.io/nav-speculation/prerendering.html#implicitly-restricted
const rule_extras = {'target_hint': getTargetHint()};
startPrerendering(
`resources/screen-capture.https.html?uid=${uid}`, rule_extras);
const result = await gotMessage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
<script>
setup(() => assertSpeculationRulesIsSupported());

const params = new URLSearchParams(window.location.search);
const target_hint = params.get('target_hint');

promise_test(async t => {
const uid = token();
const bc = new PrerenderChannel('test-channel', uid);
Expand All @@ -29,7 +26,7 @@
});
});

const url = `resources/web-xr-immersive-vr-session.https.html?uid=${uid}&target_hint=${target_hint}`;
const url = `resources/web-xr-immersive-vr-session.https.html?uid=${uid}&target_hint=${getTargetHint()}`;
window.open(url, '_blank', 'noopener');

const result = await gotMessage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
<script>
setup(() => assertSpeculationRulesIsSupported());

const params = new URLSearchParams(window.location.search);
const target_hint = params.get('target_hint');

promise_test(async t => {
const uid = token();
const bc = new PrerenderChannel('test-channel', uid);
Expand All @@ -29,7 +26,7 @@
});
});

const url = `resources/web-xr-inline-session.https.html?uid=${uid}&target_hint=${target_hint}`;
const url = `resources/web-xr-inline-session.https.html?uid=${uid}&target_hint=${getTargetHint()}`;
window.open(url, '_blank', 'noopener');

const result = await gotMessage;
Expand Down

0 comments on commit 3d1200b

Please sign in to comment.