Skip to content

Commit

Permalink
[fetch-later] Implement basic FetchLater method
Browse files Browse the repository at this point in the history
Add the basic algorithm steps from [FetchLater method][1], which mostly includes:
- Checks abort signal
- Checks request URL
- Adds extra abort steps

Subsequent CL will implement the missing steps:
- Add quota checking steps from [Deferred fetch][2]
  https://crrev.com/c/4835437
- Add backgroundTimeout https://crrev.com/c/4803283
- throw TypeError for ReadableStream (TBD from spec discussion)

[1]: https://whatpr.org/fetch/1647/53e4c3d...71fd383.html#fetch-later-method
[2]: https://whatpr.org/fetch/1647/53e4c3d...71fd383.html#deferred-fetching

Bug: 1465781
Change-Id: Ifc1a401ae000aa695dbdb3dc375557fac972b519
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4820528
Commit-Queue: Ming-Ying Chung <[email protected]>
Reviewed-by: Nidhi Jaju <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1191973}
  • Loading branch information
mingyc authored and Lightning00Blade committed Dec 11, 2023
1 parent 4688de7 commit 068c242
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 29 deletions.
24 changes: 23 additions & 1 deletion fetch/fetch-later/basic.tentative.https.window.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,29 @@ test(() => {
assert_throws_js(TypeError, () => fetchLater());
}, `fetchLater() cannot be called without request.`);

test(() => {
assert_throws_js(TypeError, () => fetchLater('http://www.google.com'));
assert_throws_js(TypeError, () => fetchLater('file://tmp'));
assert_throws_js(TypeError, () => fetchLater('ssh://example.com'));
assert_throws_js(TypeError, () => fetchLater('wss://example.com'));
assert_throws_js(TypeError, () => fetchLater('about:blank'));
assert_throws_js(TypeError, () => fetchLater(`javascript:alert('');`));
}, `fetchLater() throws TypeError on non-HTTPS URL.`);

test(() => {
const result = fetchLater('/');
assert_false(result.sent);
assert_false(result.activated);
}, `fetchLater()'s return tells the deferred request is not yet sent.`);

test(() => {
const result = fetchLater('/');
assert_throws_js(TypeError, () => result.activated = true);
}, `fetchLater() throws TypeError when mutating its returned state.`);

test(() => {
const controller = new AbortController();
// Immediately aborts the controller.
controller.abort();
assert_throws_dom(
'AbortError', () => fetchLater('/', {signal: controller.signal}));
}, `fetchLater() throws AbortError when its initial abort signal is aborted.`);
46 changes: 46 additions & 0 deletions fetch/fetch-later/send-on-discard.tentative.https.window.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// META: script=/resources/testharness.js
// META: script=/resources/testharnessreport.js
// META: script=/common/utils.js
// META: script=/pending-beacon/resources/pending_beacon-helper.js

'use strict';

parallelPromiseTest(async t => {
const uuid = token();
const url = generateSetBeaconURL(uuid);
const numPerMethod = 20;
const total = numPerMethod * 2;

// Loads an iframe that creates `numPerMethod` GET & POST fetchLater requests.
const iframe = await loadScriptAsIframe(`
const url = "${url}";
for (let i = 0; i < ${numPerMethod}; i++) {
fetchLater(url);
fetchLater(url, {method: 'POST'});
}
`);
// Delete the iframe to trigger deferred request sending.
document.body.removeChild(iframe);

// The iframe should have sent all requests.
await expectBeacon(uuid, {count: total});
}, 'A discarded document sends all its fetchLater requests.');

parallelPromiseTest(async t => {
const uuid = token();
const url = generateSetBeaconURL(uuid);

// Loads an iframe that creates 2 fetchLater requests. One of them is aborted.
const iframe = await loadScriptAsIframe(`
const url = "${url}";
const controller = new AbortController();
fetchLater(url, {signal: controller.signal});
fetchLater(url, {method: 'POST'});
controller.abort();
`);
// Delete the iframe to trigger deferred request sending.
document.body.removeChild(iframe);

// The iframe should not send the aborted request.
await expectBeacon(uuid, {count: 1});
}, 'A discarded document does not send an already aborted fetchLater request.');
28 changes: 0 additions & 28 deletions fetch/fetch-later/sendondiscard.tentative.https.window.js

This file was deleted.

0 comments on commit 068c242

Please sign in to comment.