Skip to content

Commit 21adc15

Browse files
WebMCP: Add modelContext.waitForToolsUntil()
Pages often register tools after asynchronous work, and consumers of the tools have no signal for when registration is complete. waitForToolsUntil(promise) lets a document say that its tools are not ready until the promise settles. The browser exposes each frame's state via RenderFrameHost::GetScriptToolsReadyState(), and notifies changes via WebContentsObserver::ScriptToolsReadyStateChanged(). Which part of the document lifecycle a page has to pass its promises by is still being discussed on the spec (webmachinelearning/webmcp#328). This CL uses DOMContentLoaded as the milestone to read this state, which means promises have to be registered before the Document finishes parsing. Behind the WebMCPWaitForToolsUntil feature (experimental), which implies WebMCP. Bug: 555173199 Test: content_browsertests --gtest_filter=ModelContextUserData* Test: blink_unittests --gtest_filter=ModelContextWaitForToolsUntil* Test: WPT webmcp/imperative/*wait-for-tools-until* Test: WPT webmcp/imperative/detached-frame-waitForToolsUntil.https.html Change-Id: I9ea58b2f78b4315856764013ec56190f5b7ac712 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/8594281 Reviewed-by: Dominic Farolino <[email protected]> Reviewed-by: Dave Tapuska <[email protected]> Auto-Submit: Khushal Sagar <[email protected]> Commit-Queue: Khushal Sagar <[email protected]> Reviewed-by: Mark Foltz <[email protected]> Cr-Commit-Position: refs/heads/main@{#1736883}
1 parent cac9892 commit 21adc15

4 files changed

Lines changed: 135 additions & 0 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>waitForToolsUntil() in detached frame</title>
5+
<script src="/resources/testharness.js"></script>
6+
<script src="/resources/testharnessreport.js"></script>
7+
</head>
8+
<body>
9+
<script>
10+
promise_test(async t => {
11+
const iframe = document.createElement('iframe');
12+
iframe.src = '/common/blank.html';
13+
14+
const load_promise = new Promise(resolve => iframe.onload = resolve);
15+
document.body.append(iframe);
16+
await load_promise;
17+
18+
const iDOMException = iframe.contentWindow.DOMException;
19+
const iframe_modelContext = iframe.contentWindow.document.modelContext;
20+
iframe.remove();
21+
22+
assert_throws_dom('InvalidStateError', iDOMException,
23+
() => iframe_modelContext.waitForToolsUntil(
24+
new Promise(() => {})));
25+
}, 'waitForToolsUntil() throws `InvalidStateError` in detached frame');
26+
</script>
27+
</body>
28+
</html>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Helper: calls waitForToolsUntil() on request</title>
5+
</head>
6+
<body>
7+
<script>
8+
// Calls `document.modelContext.waitForToolsUntil()` when asked to, and reports
9+
// back the name of the exception it threw, or 'ok' if it didn't throw.
10+
window.addEventListener('message', e => {
11+
if (e.data !== 'waitForToolsUntil') {
12+
return;
13+
}
14+
let result = 'ok';
15+
try {
16+
document.modelContext.waitForToolsUntil(Promise.resolve());
17+
} catch (error) {
18+
result = error.name;
19+
}
20+
e.source.postMessage(result, '*');
21+
});
22+
</script>
23+
</body>
24+
</html>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<!DOCTYPE html>
2+
<html class="test-wait">
3+
<meta charset="utf-8">
4+
<title>WebMCP: Settling waitForToolsUntil() promises after the frame is removed</title>
5+
<body>
6+
<script>
7+
// Promises passed to an iframe's `waitForToolsUntil()` can settle after the
8+
// iframe is removed. This must not crash, whether they fulfill or reject.
9+
window.onload = async () => {
10+
const iframe = document.createElement('iframe');
11+
const loaded = new Promise(resolve => iframe.onload = resolve);
12+
document.body.append(iframe);
13+
await loaded;
14+
15+
const fulfilled = Promise.withResolvers();
16+
const rejected = Promise.withResolvers();
17+
const modelContext = iframe.contentDocument.modelContext;
18+
modelContext.waitForToolsUntil(fulfilled.promise);
19+
modelContext.waitForToolsUntil(rejected.promise);
20+
21+
iframe.remove();
22+
fulfilled.resolve();
23+
rejected.reject(new Error('nope'));
24+
25+
// The iframe's reactions were added first, so they've run by the time these
26+
// continue.
27+
await fulfilled.promise;
28+
await rejected.promise.catch(() => {});
29+
document.documentElement.classList.remove('test-wait');
30+
};
31+
</script>
32+
</body>
33+
</html>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>document.modelContext.waitForToolsUntil()</title>
5+
<script src="/resources/testharness.js"></script>
6+
<script src="/resources/testharnessreport.js"></script>
7+
<script src="/common/get-host-info.sub.js"></script>
8+
</head>
9+
<body>
10+
<script>
11+
// Covers the web-observable contract of `waitForToolsUntil()`; whether
12+
// consumers of the tools actually wait is up to the user agent.
13+
14+
test(() => {
15+
// The argument is a `Promise<any>`, so non-promise values are wrapped.
16+
document.modelContext.waitForToolsUntil(42);
17+
document.modelContext.waitForToolsUntil(undefined);
18+
}, 'waitForToolsUntil() accepts non-promise values');
19+
20+
test(() => {
21+
assert_throws_js(TypeError, () => document.modelContext.waitForToolsUntil());
22+
}, 'waitForToolsUntil() requires an argument');
23+
24+
test(() => {
25+
const doc = document.implementation.createHTMLDocument();
26+
assert_throws_dom('InvalidStateError',
27+
() => doc.modelContext.waitForToolsUntil(Promise.resolve()));
28+
}, 'waitForToolsUntil() throws InvalidStateError in a document without a frame');
29+
30+
promise_test(async t => {
31+
const iframe = document.createElement('iframe');
32+
iframe.src = get_host_info().HTTPS_REMOTE_ORIGIN +
33+
'/webmcp/imperative/resources/iframe-wait-for-tools-until.html';
34+
// No `allow` attribute, so the `tools` permissions policy is disabled in
35+
// this cross-origin iframe.
36+
37+
const loaded = new Promise(resolve => iframe.onload = resolve);
38+
document.body.appendChild(iframe);
39+
t.add_cleanup(() => iframe.remove());
40+
await loaded;
41+
42+
const response = new Promise(resolve => {
43+
window.addEventListener('message', e => resolve(e.data), {once: true});
44+
});
45+
iframe.contentWindow.postMessage('waitForToolsUntil', '*');
46+
assert_equals(await response, 'NotAllowedError');
47+
}, 'waitForToolsUntil() throws NotAllowedError when the tools permissions policy is disabled');
48+
</script>
49+
</body>
50+
</html>

0 commit comments

Comments
 (0)